
|
View Full Version : PHP and Ticketsmith. Can you solve my problem?
Domenico 10-23-2001, 11:43 AM Hi,
We use Ticketsmith (voxel.net) and it works very good. Now I got a script from Justin (maker of Ticketsmith) so people can get a ticket number from the website (just as used on the Voxel site).
I get the following error when runing that script (support.php);
A database selection error has occurred!
Since Justin is probably to busy to answer my question I will try to fins an answer here. I will post support.php and the required common.inc.ph below.
support.php;
-------------------------------
<?
include("common.inc.php");
if ($name && $problem && $email && $subject) {
$email = $name . " <" . $email . ">";
$query =
"INSERT INTO tickets (author,subject,priority,body,timestamp,type) ".
"VALUES('$email','$subject','$priority','$problem',UNIX_TIMESTAMP(),'Open')";
if (do_query($query,$db_link)) {
$ticketnumber = mysql_insert_id
$messagebody =
"There is a new support ticket\n".
"Subject: $subject\n".
Thank you for your support request. We will be responding shortly.<P>
If you need immediate help, feel free to call us at xxx.xxx.xxxx<P>
<b><center><font color=green>Your reference number is <? echo $ticketnumber; ?></font><BR>
</center></b>
</td></tr></table>
<? } else { ?>
<b><font color=red>Sorry!</font></b> There seems to be an error with our support database<BR>
Please try again or call xxx.xxx.xxxx if you need help
<? }
<body link=orange vlink=orange>
<table align="center" border=0 cellpadding=3 cellspacing=1 width=400>
<tr>
<td align=center bgcolor="#006699">
<? echo $def_font; ?><font color=white>
<b>WebSupport - Request Form</b></font>
</font>
</td>
</tr>
<tr>
<td bgcolor="#eeeeee">
<br>
<p>
<?
if ($priority) {
print "<b><font color=red>You need to fill in ALL the blanks</font></b><BR>";
}
?>
To submit a request, use the space below. Please fill in all of the
blanks and try to include as much detail as possible so that we
can help you quickly and efficiently.
<table border=0 cellpadding=1 cellspacing=5>
<tr>
<td width=100 align="right"><? echo $def_font; ?>
Name
</td>
<td>
<input type="text" size="20" maxlength="256" name="name">
</td>
</tr>
<tr>
<td align="right"><? echo $def_font; ?>
E-mail
</td>
<td>
<input type="text" size="20" maxlength="256" name="email">
</td>
</tr>
<!--
<tr>
<td align="right"><? echo $def_font; ?>
Shell Username
</td>
<td>
<input type="text" size="20" maxlength="256" name="username">
</td>
</tr>
-->
<tr>
<td align="right"><? echo $def_font; ?>
Subject
</td>
<td>
<input type="text" size="20" maxlength="256" name="subject">
</td>
</tr>
<tr>
<td align="right"><? echo $def_font; ?>
Problem
</td>
<td>
<textarea rows="5" name="problem" cols="35" wrap="hard"></textarea>
</td>
</tr>
<tr>
<td align="right"><? echo $def_font; ?>
Urgency
</td>
</B>
</select>
</font>
</td>
</tr>
</table>
<center><input type=submit value="Submit Support Request"></center>
</form>
</td></tr>
<!-- Copyright Notice -->
<tr>
<td align=right bgcolor="006699">
<font face="helvetica,arial,verdana" color="#ffffff" size="2">
Powered by <b><a href="http://www.voxel.net/projects/ticketsmith/" target="new">Ticketsmith</a></b>
</td>
</tr>
</table>
</center>
</body>
</html>
<? }
I will post common.inc.php in a reply.
Domenico 10-23-2001, 11:44 AM And now common.inc.php;
--------------------------------------------
<?php
/* $Id: common.inc.php,v 1.1.1.1.2.19 2001/06/05 14:15:06 justin Exp $ */
/* program info */
$program = "Ticketsmith";
$version = "0.6.3";
$xmailer = "Ticketsmith (http://www.voxel.net/projects/ticketsmith/)";
/* error handler */
function fatal_error ($reason) {
die($reason);
}
/* database connection */
if (!($db_link = @mysql_pconnect($CONFIG["db_hostname"], $CONFIG["db_username"], $CONFIG["db_password"]))) {
fatal_error("A database connection error has occurred!");
}
@mysql_select_db($CONFIG["db_database"], $db_link) || fatal_error("A database selection error has occurred!");
/* do a MySQL query */
function do_query ($query, $db_link) {
$result = @mysql_query($query, $db_link);
if (!$result) {
fatal_error("A database query error has occurred!");
} else {
return($result);
}
}
/* get single result value */
function query2result ($query, $db_link) {
$result = do_query($query, $db_link);
$row = @mysql_result($result, 0);
return($row);
}
/* get result in numeric array */
function query2array ($query, $db_link) {
$result = do_query($query, $db_link);
$row = @mysql_fetch_row($result);
return($row);
}
/* get result in associative array */
function query2hash ($query, $db_link) {
$result = do_query($query, $db_link);
$row = @mysql_fetch_array($result);
return($row);
}
/* get row of result */
function result2row ($result) {
$row = @mysql_fetch_row($result);
return($row);
}
/* get row of result in hash */
function result2hash ($result) {
$row = @mysql_fetch_array($result);
return($row);
}
/* find number of rows in query result */
function number_rows ($result) {
$number_rows = @mysql_num_rows($result);
return($number_rows);
}
/* put rows from a column into an array */
function column2array ($query, $db_link) {
$result = do_query($query, $db_link);
while ($row = @mysql_fetch_array($result)) {
$array[] = $row[0];
}
return($array);
}
/* common page header */
function common_header ($title) {
global $CONFIG;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title><?php print($GLOBALS["program"] . " v" . $GLOBALS["version"]) ?> - <?php print($title) ?></title>
<link rel="stylesheet" href="<?php print($CONFIG["relative_path"] . "/style.css") ?>" type="text/css">
</head>
<body bgcolor="<?php print($GLOBALS["CONFIG"]["background_color"]) ?>">
<center>
<?php
}
/* common page footer */
function common_footer () {
?>
<div class="footer"><?php print("Powered by " . $GLOBALS["program"] . " v" . $GLOBALS["version"]) ?></div>
</center>
</body>
</html>
<?php
}
/* create drop-down box */
function create_selectbox ($name, $options, $selected) {
$output .= "<select name=\"$name\" onChange=\"document.form.submit()\">\n";
while(list($key, $val) = each($options)) {
$output .= "<option value=\"$key\"";
if ($key == $selected) {
$output .= " selected";
}
$output .= ">$val\n";
$loop++;
}
$output .= "</select>\n";
return($output);
}
/* escape special characters */
function escape_string ($string) {
if (!get_magic_quotes_gpc()) {
$string = addslashes($string);
}
return($string);
}
/* format "time ago" date string */
function get_time_ago ($timestamp) {
$elapsed_seconds = time() - $timestamp;
if ($elapsed_seconds < 60) { // seconds ago
if ($elapsed_seconds) {
$interval = $elapsed_seconds;
}
else {
$interval = 1;
}
$output = "$interval second";
}
elseif ($elapsed_seconds < 3600) { // minutes ago
$interval = round($elapsed_seconds / 60);
$output = "$interval minute";
}
elseif ($elapsed_seconds < 86400) { // hours ago
$interval = round($elapsed_seconds / 3600);
$output = "$interval hour";
}
elseif ($elapsed_seconds < 604800) { // days ago
$interval = round($elapsed_seconds / 86400);
$output = "$interval day";
}
elseif ($elapsed_seconds < 2419200) { // weeks ago
$interval = round($elapsed_seconds / 604800);
$output = "$interval week";
}
elseif ($elapsed_seconds < 29030400) { // months ago
$interval = round($elapsed_seconds / 2419200);
$output = "$interval month";
}
else { // years ago
$interval = round($elapsed_seconds / 29030400);
$output = "$interval year";
}
if ($interval > 1) {
$output .= "s";
}
$output .= " ago";
return($output);
}
/* smart word wrapping */
function smart_wrap ($text, $width) {
if (function_exists("wordwrap")) {
if (preg_match("/[^\\n]{100,}/", $text)) {
$text = wordwrap($text, $width);
}
}
else {
$text = "Wordwrap unsupported in PHP " . phpversion() . "\n\n";;
$text .= "Please adjust your Ticketsmith configuration and/or upgrade PHP\n";
}
return($text);
}
Domenico 10-23-2001, 11:44 AM /* format display field */
function format_field ($value, $type, $ticket = NULL) {
global $CONFIG;
switch ($type) {
case "user":
if ($value) {
$output = query2result("SELECT name FROM users WHERE id = '$value'", $GLOBALS["db_link"]);
} else {
$output = "-";
}
break;
case "status":
$output = create_selectbox("type_toggle", array("Open" => "Open", "Closed" => "Closed", "Deleted" => "Deleted"), $value);
break;
case "priority_view":
$priority = $CONFIG["priority_names"][$value];
$color = $CONFIG["priority_colors"][$value];
if ($value == 3) {
$priority = "<b>$priority</b>";
}
if ($value == 4) {
$priority = "<blink><b>$priority</b></blink>";
}
$output = "<font color=\"$color\">$priority</font>";
break;
case "priority_select":
$output = create_selectbox("priority_toggle", $CONFIG["priority_names"], $value);
break;
case "assignment":
$options[0] = "-";
$query = "SELECT id, name FROM users WHERE username != 'admin' ORDER BY name";
$result = do_query($query, $GLOBALS["db_link"]);
while ($row = result2hash($result)) {
$options[$row["id"]] = $row["name"];
}
$output = create_selectbox("assignment_toggle", $options, $value);
break;
case "view":
if ($CONFIG["index_link"] == "latest") {
$latest_value = query2result("SELECT ticket FROM tickets WHERE parent = '$value' ORDER BY ticket DESC LIMIT 1", $GLOBALS["db_link"]);
if ($latest_value) {
$value = $latest_value;
}
}
$output = "<a href=\"" . $CONFIG["relative_path"] . "/view.php?ticket=$value\">";
$output .= "<img src=\"" . $CONFIG["relative_path"] . "/posticon.gif\" border=\"0\"></a>";
break;
case "open_date":
$output = get_time_ago($value);
if ($CONFIG["warning_active"]) {
if (time() - $value > $CONFIG["warning_age"] * 3600) {
$output = "<font color=\"" . $CONFIG["warning_color"] . "\"><xb>" . $output . "</b></font>";
}
}
break;
case "activity_date":
if (!$value) {
$output = "<i>none</i>";
}
else {
$output = get_time_ago($value);
}
$latest_followup_type = query2result("SELECT type FROM tickets WHERE parent = '$ticket' ORDER BY timestamp DESC LIMIT 1", $GLOBALS["db_link"]);
if ($latest_followup_type) {
$latest_followup_type = preg_replace("/(\w+)\s.*/", "\\1", $latest_followup_type);
$output .= " [$latest_followup_type]";
}
break;
case "elapsed_date":
$output = date($CONFIG["date_format"], $value);
$time_ago = get_time_ago($value);
$output .= " <i>($time_ago)</i>";
break;
case "body":
$value = htmlspecialchars($value);
$output = "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"10\">\n";
$output .= "<tr><td bgcolor=\"" . $CONFIG["ticket_color"] . "\">\n<tt><pre>\n";
$url_find = "/(http|https|ftp|news|telnet|finger)(:\/\/[^ \">\\t\\r\\n]*)/";
$url_replace = "<a href=\"\\1\\2\" target=\"new\">";
$url_replace .= "<span style=\"font-size: 10pt;\">\\1\\2</span></a>";
$value = preg_replace($url_find, $url_replace, $value);
if ($CONFIG["wordwrap"]) {
$output .= smart_wrap($value, 72);
}
else {
$output .= $value;
}
$output .= "\n</pre></tt>\n</td></tr>\n</table>\n";
break;
case "followup":
$output = "\n<tt>\n";
$output .= "<textarea name=\"followup\" wrap=\"hard\" cols=\"72\" rows=\"20\">\n";
$signature = query2result("SELECT signature FROM users WHERE id = '" . $GLOBALS["login_id"] . "'", $GLOBALS["db_link"]);
if ($signature) {
$output .= "\n";
$output .= "-- \n";
$output .= $signature;
}
$output .= "\n\n";
$output .= "---- Original message ----\n\n";
if ($CONFIG["wordwrap"]) {
$value = smart_wrap($value, 70);
}
$value = htmlspecialchars($value);
$lines = explode("\n", $value);
for ($loop = 0; $loop < count($lines); $loop++) {
$lines[$loop] = "> " . $lines[$loop];
}
$output .= join("\n", $lines);
$output .= "\n</textarea>\n";
$output .= "</tt>\n";
break;
case "subject":
$value = preg_replace("/\s*Re:\s*/i", "", $value);
$value = preg_replace("/(\[\#\d+\])(\w+)/", "\\2", $value);
$value = "Re: " . $value;
$value = htmlspecialchars($value);
$output .= "<input type=\"text\" name=\"subject\" value=\"$value\" size=\"70\">\n";
break;
case "cc":
$value = htmlspecialchars($value);
$output = "<input type=\"text\" name=\"cc\" value=\"$value\" size=\"70\">";
break;
case "recipient":
$value = htmlspecialchars($value);
$output = "<input type=\"text\" name=\"recipient\" value=\"$value\" size=\"70\">";
break;
case "original_author":
if ($value) {
$value = ereg_replace("\"", "", $value);
$output = htmlspecialchars($value);
}
else {
$output = "<i>(original ticket author)</i>";
}
break;
case "email":
if ($value) {
$value = ereg_replace("\"", "", $value);
$output = htmlspecialchars($value);
}
else {
$output = "<i>none</i>";
}
break;
default:
$output = $value ? htmlspecialchars($value) : "<i>none</i>";
}
return($output);
}
/* register login stuff */
session_register("login_id");
session_register("login_name");
/* figure out parent & type */
if ($ticket) {
list($ticket_type, $ticket_parent) = query2array("SELECT type, parent FROM tickets WHERE ticket = '$ticket'", $db_link);
}
/* login functionality */
if ($login_attempt && $password_attempt) {
list($select_id, $select_username, $select_password) = query2array("SELECT id, username, password FROM users WHERE username = '$login_attempt'", $db_link);
if ($password_attempt == $select_password) {
$login_id = $select_id;
$login_name = $select_username;
if ($login_name == "admin") {
header("Location: " . $CONFIG["relative_path"] . "/admin.php");
}
else {
header("Location: " . $CONFIG["relative_path"] . "/index.php");
}
exit();
}
}
/* logout functionality */
if ($logout == 1) {
session_destroy();
header("Location: " . $CONFIG["relative_path"] . "/login.php");
exit();
}
/* check login status */
if ($login_name == "admin" && ($PHP_SELF != $CONFIG["relative_path"] . "/admin.php")) {
header("Location: " . $CONFIG["relative_path"] . "/admin.php");
exit();
}
elseif (!$login_name && $PHP_SELF != $CONFIG["relative_path"] . "/login.php") {
header("Location: " . $CONFIG["relative_path"] . "/login.php");
exit();
}
?>
I hope someone will figure this out.
Again, Ticketsmith does work allready. The problem must be in support.php or common.inc.php or well, you say it.
THANKS!
Félix C.Courtemanche 10-23-2001, 01:18 PM From your files, the only that can generate this error is 'common' (only it contains the:
@mysql_select_db($CONFIG["db_database"], $db_link) || fatal_error("A database selection error has occurred!");
Now, this basically means that it cannot select the db you entered in your $CONFIG
Make sure it exists; you can also replace the line by
@mysql_select_db($CONFIG["db_database"], $db_link) || fatal_error(mysql_error());
that will display the real SQL error, may help.
Domenico 10-23-2001, 02:02 PM The database exists because Ticketsmith allready works and support.php is in the same directory.
Wait, does it read config.inc.php(same directory) ? There is my login data...
I mean, how does it get the info?
CWDSolutions 10-23-2001, 02:22 PM It gets all the information from the config file "include("common.inc.php");"
Yes this is in the same directory as the script itself. If you have it
in a different directory you should move it to the directory of the
script or change the path to it in each file that requires it.
Regards,
Ray
Domenico 10-23-2001, 02:32 PM Hmmm, after i did what Felix said I get;
No Database Selected
So it doesn't read the config file at all.
My config file is called config.inc.php so maybe I have to change common.inc.php to config.inc.php in support.php
i will try this now..
Ok, I tried it and now i get this;
Warning: SAFE MODE Restriction in effect. The script whose uid is 0 is not allowed to access ./config.inc.php owned by uid 500
LATE EDIT;
Well, I changed the userid with chown to root and now it works ;-)
BUT! When I fill in the form and press send I get;
Fatal error: Call to undefined function: do_query() in /path to/support.php on line 12
What is this...? Remember that I changed common.inc.php to config.inc.php in support.php
Thank you.
Domenico 10-23-2001, 04:12 PM I fixed it!
I put all the variables it needed in common.inc.php from the config file and put them into common.inc.php.
Now it works ;-)
Thanks all for helping me out!!!
Domenico 10-24-2001, 08:19 AM Strange, sometimes when I run support.php it goes straight to login.php but after I login support.php does work.
It probably can't browse the database and jumps straight to the administrators login.php
Should I give the user (login data supplied from common.inc.php) more rights then?
alchiba 10-24-2001, 10:37 AM Looks like there's code at the bottom of common.inc.php that checks if someone's logged on. You probably don't want this to happen if you want to just display a form. To fix this, you should probably copy support.php and common.inc.php into a new directory, then delete the login check code in common.inc.php. (You don't want to edit the common.inc.php that the main Ticketsmith script uses.)
Domenico 10-24-2001, 10:41 AM It looks like that is the problem indeed.
I will check it out and we will see what happens.
Thank you!!!
|