home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / dotproject / modules / ticketsmith / common.inc.php < prev    next >
Encoding:
PHP Script  |  2004-01-29  |  14.9 KB  |  498 lines

  1. <?php
  2.  
  3. /* $Id: common.inc.php,v 1.17 2004/01/29 04:12:33 ajdonnison Exp $ */
  4.  
  5. /* program info */
  6. $program = "Dotproject";
  7. $version = "0.6.3";
  8. $xmailer = "dotproject (http://dotproject.net/)";
  9.  
  10. /* error handler */
  11. function fatal_error ($reason) {
  12.  
  13.     die($reason);
  14.  
  15. }
  16.  
  17.  
  18. /* do a MySQL query */
  19. function do_query ($query) {
  20.     $result = @mysql_query($query);
  21.     if (!$result) {
  22.         fatal_error("A database query error has occurred!<br>".mysql_error());
  23.     } else {
  24.         return($result);
  25.     }
  26.     
  27. }
  28.  
  29. /* get single result value */
  30. function query2result ($query) {
  31.  
  32.     $result = do_query($query);
  33.     $row = @mysql_result($result, 0);
  34.     return($row);
  35.  
  36. }
  37.  
  38. /* get result in numeric array */
  39. function query2array ($query) {
  40.  
  41.     $result = do_query($query);
  42.     $row = @mysql_fetch_row($result);
  43.     return($row);
  44.  
  45. }
  46.  
  47. /* get result in associative array */
  48. function query2hash ($query) {
  49.  
  50.     $result = do_query($query);
  51.     $row = @mysql_fetch_array($result);
  52.     return($row);
  53.     
  54. }
  55.  
  56. /* get row of result */
  57. function result2row ($result) {
  58.  
  59.     $row = @mysql_fetch_row($result);
  60.     return($row);
  61.  
  62. }
  63.  
  64. /* get row of result in hash */
  65. function result2hash ($result) {
  66.  
  67.     $row = @mysql_fetch_array($result);
  68.     return($row);
  69.  
  70. }
  71.  
  72. /* find number of rows in query result */
  73. function number_rows ($result) {
  74.  
  75.     $number_rows = @mysql_num_rows($result);
  76.     return($number_rows);
  77.  
  78. }
  79.  
  80. /* put rows from a column into an array */
  81. function column2array ($query) {
  82.  
  83.     $result = do_query($query);
  84.     while ($row = @mysql_fetch_array($result)) {
  85.         $array[] = $row[0];
  86.     }
  87.     return($array);
  88.  
  89. }
  90.  
  91. /* create read-only output of list values */
  92. function chooseSelectedValue ($name, $options, $selected) {
  93.     while(list($key, $val) = each($options)) {
  94.             if ($key == $selected) {
  95.                 $output = "$val\n";
  96.             }
  97.         }
  98.  return($output);
  99.  
  100. }
  101.  
  102. /* create drop-down box */
  103. function create_selectbox ($name, $options, $selected) {
  104.  
  105.     $output= "";
  106.     $output .= "<select name=\"$name\" onChange=\"document.ticketform.submit()\" class=\"text\">\n";
  107.  
  108.     while(list($key, $val) = each($options)) {
  109.         $output .= "<option value=\"$key\"";
  110.  
  111.         if ($key == $selected) {
  112.             $output .= " selected";
  113.         }
  114.  
  115.         $output .= ">$val\n";
  116.         //$loop++;
  117.     }
  118.  
  119.     $output .= "</select>\n";
  120.  
  121.  
  122.     return($output);
  123.  
  124. }
  125.  
  126. /* escape special characters */
  127. function escape_string ($string) {
  128.     
  129.     if (!get_magic_quotes_gpc()) {
  130.         $string = addslashes($string);
  131.     }
  132.     return($string);
  133.  
  134. }
  135.  
  136. /* format "time ago" date string */
  137. function get_time_ago ($timestamp) {
  138.     global $AppUI;
  139.  
  140.     $elapsed_seconds = time() - $timestamp;
  141.  
  142.     if ($elapsed_seconds < 60) { // seconds ago
  143.         if ($elapsed_seconds) {
  144.             $interval = $elapsed_seconds;
  145.         }
  146.         else {
  147.             $interval = 1;
  148.         }
  149.         $output = "second";
  150.     }
  151.     elseif ($elapsed_seconds < 3600) { // minutes ago
  152.         $interval = round($elapsed_seconds / 60);
  153.         $output = "minute";
  154.     }
  155.     elseif ($elapsed_seconds < 86400) { // hours ago
  156.         $interval = round($elapsed_seconds / 3600);
  157.         $output = "hour";
  158.     }
  159.     elseif ($elapsed_seconds < 604800) { // days ago
  160.         $interval = round($elapsed_seconds / 86400);
  161.         $output = "day";
  162.     }
  163.     elseif ($elapsed_seconds < 2419200) { // weeks ago
  164.         $interval = round($elapsed_seconds / 604800);
  165.         $output = "week";
  166.     }
  167.     elseif ($elapsed_seconds < 29030400) { // months ago
  168.         $interval = round($elapsed_seconds / 2419200);
  169.         $output = " month";
  170.     }
  171.     else { // years ago
  172.         $interval = round($elapsed_seconds / 29030400);
  173.         $output = "year";
  174.     }
  175.     
  176.     if ($interval > 1) {
  177.         $output .= "s";
  178.     }
  179.  
  180.     $output = " ".$AppUI->_($output);
  181.  
  182.     $output .= " ".$AppUI->_('ago');
  183.  
  184.     $output = $interval.$output;
  185.     return($output);
  186.  
  187. }
  188.  
  189. /* smart word wrapping */
  190. function smart_wrap ($text, $width) {
  191.  
  192.     if (function_exists("wordwrap")) {
  193.         if (preg_match("/[^\\n]{100,}/", $text)) {
  194.             $text = wordwrap($text, $width);
  195.         }
  196.     }
  197.     else {
  198.         $text = "Wordwrap unsupported in PHP " . phpversion() . "\n\n";;
  199.         $text .= "Please adjust your Ticketsmith configuration and/or upgrade PHP\n";
  200.     }
  201.  
  202.     return($text);
  203.  
  204. }
  205.  
  206. /* word_wrap($string, $cols, $quote_old, $prefix, $nice_prefix)
  207.  *
  208.  * Takes $string, and wraps it on a per-word boundary (does not clip
  209.  * words UNLESS the word is more than $cols long), no more than $cols per
  210.  * line. Allows for optional prefix string for each line. (Was written to
  211.  * easily format replies to e-mails, prefixing each line with "> ".
  212.  *
  213.  * Puts words that do not fit at the end of the line to the 
  214.  * beginning of the next line (keeping in mind prefixes) and not on a line
  215.  * by themselves.
  216.  *
  217.  * parameter $string         -- text to be wrapped
  218.  * parameter $cols          -- maximum width of text
  219.  * parameter $quote_old     -- adds the prefix for each line if true
  220.  * parameter $prefix         -- prefix used in message (nice_prefix but w/o spaces)
  221.  * parameter $nice_prefix    -- prefix that is easier to read (i.e. "> " instead of ">").
  222.  *
  223.  * $nice_prefix is actually written to the message (if $quote_old), but $prefix is used for 
  224.  * backward compatibility
  225.  * 
  226.  * Please note that though quote_old may be false, prefix and nice_prefix are still used to maintain
  227.  * the existing structure of a message (in case the message is a reply or etc...)
  228.  *
  229.  * Original by Dominic J. Eidson.
  230.  * Copyright 1999 Dominic J. Eidson, use as you wish, but give credit
  231.  * where credit due.
  232.  * Modified by Daniel Kigelman in 2003.
  233.  *
  234.  */
  235.  
  236. function word_wrap ($string, $cols = 78, $quote_old = false, $prefix = ">", $nice_prefix = "> ") {
  237.  
  238. if (preg_match("/^.*\r\n/", $string)) {
  239.     $t_lines = split( "\r\n", $string);
  240. } else if (preg_match("/^.*\n/", $string)) {
  241.     $t_lines = split( "\n", $string);
  242. } else {
  243.     $t_lines = split( "\r", $string);
  244. }
  245.  
  246. $outlines = "";
  247. $leftover = "";
  248.  
  249. // Loop through each line of message
  250. while(list(, $thisline) = each($t_lines)) {
  251.     // Process Leftover
  252.     if (strlen($leftover) > 0) {
  253.         $counter = 0;
  254.  
  255.         // Subtract all prefixes from the beginning of this line.
  256.         while (substr($thisline, 0, strlen($prefix)) == $prefix) {
  257.             $counter++;
  258.             
  259.             if (substr($thisline, 0, strlen($nice_prefix)) == $nice_prefix) {
  260.                 $thisline = substr($thisline, strlen($nice_prefix));
  261.             } else {
  262.                 $thisline = substr($thisline, strlen($prefix));
  263.             }
  264.         }
  265.         
  266.         // Add the leftover to the beginning of this line.
  267.         $thisline = $leftover . $thisline;
  268.         
  269.         // Add all the prefixes back on to the beginning of the line.
  270.         for ($i = 0; $i < $counter; $i++) {
  271.             $thisline = $nice_prefix . $thisline;
  272.         }
  273.     }
  274.  
  275.     if(strlen($thisline) + strlen($nice_prefix) > $cols) {
  276.         $newline = "";
  277.         $t_l_lines = split(" ", $thisline);
  278.         // This line is too big.  Break it up into words and add them one by one.
  279.         while(list(, $thisword) = each($t_l_lines)) {
  280.             // Process words that are longer than $cols
  281.             while((strlen($thisword) + strlen($nice_prefix)) > $cols) {
  282.                 $cur_pos = 0;
  283.                 $outlines .= $nice_prefix;
  284.                 for($num=0; $num < $cols-1; $num++) {
  285.                     $outlines .= $thisword[$num];
  286.                     $cur_pos++;
  287.                 }
  288.                 $outlines .= "\n";
  289.                 $thisword = substr($thisword, $cur_pos, (strlen($thisword)-$cur_pos));
  290.             }
  291.  
  292.             // Check that the line is within $cols.  If not, don't add the word; start a new line.
  293.             if((strlen($newline) + strlen($thisword) + strlen($nice_prefix) + 1) > $cols) {
  294.                 if ($quote_old) $outlines .= $nice_prefix.$newline."\n";
  295.                 else $outlines .= $newline."\n";
  296.                 $newline = $thisword." ";
  297.             } else {
  298.                 $newline .= $thisword." ";
  299.             }
  300.         }
  301.         // Whatever is leftover from processing the line, assign to $leftover
  302.         $leftover = $newline;
  303.     } else {
  304.         if ($quote_old) $outlines .= $nice_prefix . $thisline."\n";
  305.         else $outlines .= $thisline."\n";
  306.         $leftover = "";
  307.     }
  308. }
  309. return $outlines;
  310. }
  311.  
  312.  
  313. /* format display field */
  314. function format_field ($value, $type, $ticket = NULL) {
  315.  
  316.     global $CONFIG;
  317.     global $AppUI;
  318.     global $canEdit;
  319.     switch ($type) {
  320.         case "user":
  321.             if ($value) {
  322.                 $output = query2result("SELECT CONCAT_WS(' ',user_first_name,user_last_name) as name FROM users WHERE user_id = '$value'");
  323.             } else {
  324.                 $output = "-";
  325.             }
  326.             break;
  327.         case "status":
  328.         if ($canEdit) {
  329.                 $output = create_selectbox("type_toggle", array("Open" =>$AppUI->_("Open"), "Processing" => $AppUI->_("Processing"), "Closed" => $AppUI->_("Closed"), "Deleted" => $AppUI->_("Deleted")), $value);
  330.         }
  331.         else {
  332.         $output = chooseSelectedValue("type_toggle", array("Open" =>$AppUI->_("Open"), "Processing" => $AppUI->_("Processing"), "Closed" => $AppUI->_("Closed"), "Deleted" => $AppUI->_("Deleted")), $value);
  333.         }
  334.             break;
  335.         case "priority_view":
  336.             $priority = $CONFIG["priority_names"][$value];
  337.             $color = $CONFIG["priority_colors"][$value];
  338.         //$priority = $AppUI->_($priority);
  339.             if ($value == 3) {
  340.                 $priority = "<strong>$priority</strong>";
  341.             }
  342.             if ($value == 4) {
  343.                 $priority = "<blink><strong>$priority</strong></blink>";
  344.             }
  345.  
  346.             $output = "<font color=\"$color\">$priority</font>";
  347.             break;
  348.         case "priority_select":
  349.         if ($canEdit) {
  350.                 $output = create_selectbox("priority_toggle", $CONFIG["priority_names"], $value);
  351.         }
  352.         else {
  353.             $output = chooseSelectedValue("priority_toggle", $CONFIG["priority_names"], $value);
  354.         }
  355.             break;
  356.         case "assignment":
  357.             $options[0] = "-";
  358.             $query = "SELECT user_id as id, CONCAT_WS(' ',user_first_name,user_last_name) as name FROM users";
  359.             $result = do_query($query);
  360.             while ($row = result2hash($result)) {
  361.                 $options[$row["id"]] = $row["name"];
  362.             }
  363.         if ($canEdit) {
  364.                 $output = create_selectbox("assignment_toggle", $options, $value);
  365.         }
  366.         else {
  367.             $output = chooseSelectedValue("assignment_toggle", $options, $value);
  368.         }
  369.             break;
  370.         case "view":
  371.             if ($CONFIG["index_link"] == "latest") {
  372.                 $latest_value = query2result("SELECT ticket FROM tickets WHERE parent = '$value' ORDER BY ticket DESC LIMIT 1");
  373.                 if ($latest_value) {
  374.                     $value = $latest_value;
  375.                 }
  376.             }
  377.             $output = "<a href=index.php?m=ticketsmith&a=view&ticket=$value>";
  378.             $output .= "<img src=images/icons/pencil.gif border=0></a>";
  379.             break;
  380.     case "attach":
  381.         $output = "<A href=index.php?m=ticketsmith&a=attach&ticket=$value>";
  382.         $output .= "Link</a>";
  383.         break;
  384.     case "doattach":
  385.         $output = "<A href=index.php?m=ticketsmith&a=attach&newparent=$value&dosql=reattachticket&ticket=$ticket>";
  386.         $output .= "Link</a>";
  387.         break;
  388.         case "open_date":
  389.             $output = get_time_ago($value);
  390.             if ($CONFIG["warning_active"]) {
  391.                 if (time() - $value > $CONFIG["warning_age"] * 3600) {
  392.                     $output = "<font color=\"" . $CONFIG["warning_color"] . "\"><xb>" . $output . "</strong></font>";
  393.                 }
  394.             }
  395.             break;
  396.         case "activity_date":
  397.             if (!$value) {
  398.                 $output = "<em>".$AppUI->_('none')."</em>";
  399.             }
  400.             else {
  401.                 $output = get_time_ago($value);
  402.             }
  403.             $latest_followup_type = query2result("SELECT type FROM tickets WHERE parent = '$ticket' ORDER BY timestamp DESC LIMIT 1");
  404.             if ($latest_followup_type) {
  405.                 $latest_followup_type = preg_replace("/(\w+)\s.*/", "\\1", $latest_followup_type);
  406.                 $output .= " [$latest_followup_type]";
  407.             }
  408.             break;
  409.         case "elapsed_date":
  410.             $output = date($CONFIG["date_format"], $value);
  411.             $time_ago = get_time_ago($value);
  412.             $output .= " <em>($time_ago)</em>";
  413.             break;
  414.         case "body":
  415.         if ($CONFIG["wordwrap"]) {
  416.             $value = word_wrap($value, 78);
  417.         }
  418.             $value = htmlspecialchars($value);
  419.             $output = "<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"10\">\n";
  420.             $output .= "<tr><td bgcolor=\"" . $CONFIG["ticket_color"] . "\">\n<tt><pre>\n";
  421.             $url_find = "/(http|https|ftp|news|telnet|finger)(:\/\/[^ \">\\t\\r\\n]*)/";
  422.             $url_replace = "<a href=\"\\1\\2\" target=\"new\">";
  423.             $url_replace .= "<span style=\"font-size: 10pt;\">\\1\\2</span></a>";
  424.             $value = preg_replace($url_find, $url_replace, $value);
  425.         $output .= $value;
  426.             $output .= "\n</pre></tt>\n</td></tr>\n</table>\n";
  427.             break;
  428.         case "followup":
  429.             $output = "\n<tt>\n";
  430.             $output .= "<textarea style='font-family: monospace;' name=\"followup\" wrap=\"hard\" cols=\"72\" rows=\"20\">\n";
  431.             $signature = query2result("SELECT user_signature FROM users WHERE user_id = '$AppUI->user_id'");
  432.             if ($signature) {
  433.                 $output .= "\n";
  434.                 $output .= "-- \n";
  435.                 $output .= $signature;
  436.             }
  437.             $output .= "\n\n";
  438.             $output .= "---- ".$AppUI->_('Original message')." ----\n\n";
  439.             if ($CONFIG["wordwrap"]) {
  440.                 $value = word_wrap($value, 70, true);
  441.             }
  442.             $value = htmlspecialchars($value);
  443.             $output .= $value;
  444.             $output .= "\n</textarea>\n";
  445.             $output .= "</tt>\n";
  446.             break;
  447.         case "subject":
  448.             $value = preg_replace("/\s*Re:\s*/i", "", $value);
  449.             $value = preg_replace("/(\[\#\d+\])(\w+)/", "\\2", $value);
  450.             $value = "Re: " . $value;
  451.             $value = htmlspecialchars($value);
  452.             @$output .= "<input type=\"text\" name=\"subject\" value=\"$value\" size=\"70\">\n";
  453.             break;
  454.         case "cc":
  455.             $value = htmlspecialchars($value);
  456.             $output = "<input type=\"text\" name=\"cc\" value=\"$value\" size=\"70\">";
  457.             break;
  458.         case "recipient":
  459.             $value = htmlspecialchars($value);
  460.             $output = "<input type=\"text\" name=\"recipient\" value=\"$value\" size=\"70\">";
  461.             break;
  462.         case "original_author":
  463.             if ($value) {
  464.                 $value = ereg_replace("\"", "", $value);
  465.                 $output = htmlspecialchars($value);
  466.             }
  467.             else {
  468.                 $output = "<em>(".$AppUI->_('original ticket author').")</em>";
  469.             }
  470.             break;
  471.         case "email":
  472.             if ($value) {
  473.                 $value = ereg_replace("\"", "", $value);
  474.                 $output = htmlspecialchars($value);
  475.             }
  476.             else {
  477.                 $output = "<em>".$AppUI->_('none')."</em>";
  478.             }
  479.             break;
  480.         default:
  481.             $output = $value ? htmlspecialchars($value) : "<em>".$AppUI->_('none')."</em>";
  482.     }
  483.     return($output);
  484.  
  485. }
  486.  
  487. /* register login stuff */
  488. //session_register("login_id");
  489. //session_register("login_name");
  490.  
  491. /* figure out parent & type */
  492. if (isset($ticket)) {
  493.     list($ticket_type, $ticket_parent) = query2array("SELECT type, parent FROM tickets WHERE ticket = '$ticket'");
  494. }
  495.  
  496.  
  497. ?>
  498.