[ PHPXref.com ] [ Generated: Sun Jul 20 19:05:01 2008 ] [ OneOrZero 1.6.3 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/mailgate/ -> mailGate.php (source)

   1  <?php
   2  /*

   3   *  Date: Oct 28,2005

   4   *  Filename: mailGate.php

   5   *  Author: Chris Rothbauer

   6   *  Description: This file serves as an email gateway for OneOrZero Helpdesk v1.6.

   7   *               It allows users to open tickets by sending emails to a designated

   8   *               email address. The script collects the email, either via a stream

   9   *               or pop3 server, and creates a ticket from the information.

  10   *  Requires: mimeDecode.php, Mail_Mime PEAR extensions

  11  /***************************************************************************

  12   *  This program is free software; you can redistribute it and/or

  13   *  modify it under the terms of the GNU General Public

  14   *  License as published by the Free Software Foundation; either

  15   *  version 2.1 of the License, or (at your option) any later version.

  16   *

  17   *  This program is distributed in the hope that it will be useful,

  18   *  but WITHOUT ANY WARRANTY; without even the implied warranty of

  19   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU

  20   *  General Public License for more details.

  21   *

  22   *  You should have received a copy of the GNU General Public

  23   *  License along with This program; if not, write to:

  24   *    Free Software Foundation, Inc.

  25   *    59 Temple Place

  26   *    Suite 330

  27   *    Boston, MA  02111-1307  USA

  28   *

  29   *  Copyright 2005 One or Zero

  30   *  info@oneorzero.com

  31   *  http://www.oneorzero.com

  32   *  Developers: OneOrZero Team / Contributors: OneOrZero Community

  33  /***************************************************************************

  34   *  There are several configurable options, all of which are set via the OOZ Settings

  35   *  page.

  36   *

  37   *  Features and Options:

  38   *

  39   *    1. Collect email from an input stream (ie. Sendmail) or a pop3 server.

  40   *       To collect email via a sendmail, alter the aliases file to look like the following...

  41   *           helpdesk: "|php /path/to/mailGate.php"

  42   *       To collect email via a pop3 server, setup a cron job to collect emails.

  43   *

  44   *    2. Choose whether to page supporters when tickets are updated or created.

  45   *

  46   *    3. Choose whether to autofill user fields, such as office and phone, from the DB.

  47   *

  48   *    4. Automatic routing of tickets, to supporters, based on email subject. This operatino

  49   *       is performed by querying the properties of the individual supporter.

  50   *       For example, to assign all proxy related tickets to Bob (with subject containing

  51   *       either proxy, squid, or dansguardian, add the following string to the routing

  52   *       string in the user properties...

  53   *            "proxy|squid|dansguardian"

  54   *

  55   *    5. Text only ticket descriptions, even from html multipart messages. However, if the

  56   *       email client is configured to send HTML only email, no suitable description will

  57   *       be gathered and the script will indicate such.

  58   *

  59   *    6. Parsing of attached files and embedded images.

  60   *

  61   *    7. Customizeable receipt and supporter page templates.

  62   *       To set up email templates, create/modify the following templates using the 

  63   *       Templates page of the Control Panel.

  64   *               mailGate_new_page                  page sent to supporters and admins for new tickets

  65   *               mailGate_update_page           page sent to supporters and admins for ticket updates

  66   *               mailGate_new_receipt           receipt sent to user for new tickets

  67   *               mailGate_update_receipt        receipt sent to user for ticket updates

  68   *

  69   *    8. Customizeable check list reminders

  70   *       To set up checklists for certain tickets, modify the control panel with 

  71   *       keywords separated by the or "|" symbol. Then create message templates 

  72   *       using the keyword as part of the name.

  73   *       For example, to create a checklist for new hires...

  74   *            Keyword: newhire      template: mailGate_newhire_checklist

  75  /***************************************************************************/

  76  

  77  

  78  /* *********** Setup *********** */

  79  $chdir= dirname(__FILE__);

  80  chdir("$chdir");

  81  

  82  $server_settings_path= "../configuration/server_settings.php";

  83  $website_settings_path= "../configuration/website_settings.php";

  84  

  85  if (file_exists($server_settings_path))

  86  {

  87      $ooz_server_settings= parse_ini_file($server_settings_path);

  88  }

  89  else

  90  {

  91      printError("The server_settings.php file was not found !");

  92      exit;

  93  }

  94  if (file_exists($website_settings_path))

  95  {

  96      $ooz_website_settings= parse_ini_file($website_settings_path);

  97  }

  98  else

  99  {

 100      printError("The website_settings.php file was not found !");

 101      exit;

 102  }

 103  $MDpath= $ooz_website_settings[MDpath];

 104  $Pearpath= $ooz_website_settings[Pearpath];

 105  $dbPre = $ooz_server_settings[table_prefix];

 106  $database= $ooz_server_settings[database];

 107  $db_host= $ooz_server_settings[db_host];

 108  $db_user= $ooz_server_settings[db_user];

 109  $db_pwd= $ooz_server_settings[db_pwd];

 110  $db_name= $ooz_server_settings[db_name];

 111  $language= $ooz_website_settings[language];

 112  $default_language= $ooz_website_settings[default_language];

 113  

 114  ini_set("include_path", "$Pearpath");

 115  include ("$MDpath");

 116  if ($language == '')

 117  {

 118      require_once "../lang/$default_language.lang.php";

 119  }

 120  else

 121  {

 122      require_once "../lang/$language.lang.php";

 123  }

 124  

 125  require_once "../common/$database.class.php";

 126  $db= new $database ();

 127  

 128  if ($database == "mysql")

 129  {

 130      $db->set($db_host, $db_user, $db_pwd, $db_name);

 131  }

 132  elseif ($database == "sqlite")

 133  {

 134      $db->set($sqlite_dbfile);

 135  }

 136  

 137  $path= $ooz_website_settings[path]; // Path to One Or Zero, notice NO trailing slash

 138  $attach= $ooz_server_settings[file_upload_directory]; // Path to file attachments, note the trailing slash

 139  $hdMail= $ooz_website_settings[hdMail]; // Email address used to send/receive messages with system

 140  $pageMe= $ooz_website_settings[pageMe]; // Decide whether to page supporters on updates

 141  $pageAll= $ooz_website_settings[pageAll]; // Decide whether to page whole team, or just supporter, on new tickets

 142  $pageAdmin= $ooz_website_settings[pageAdmin]; // Decide whether to page the admin, in addition to individuals

 143  $sendReceipt= $ooz_website_settings[sendReceipt]; // Decide whether to send receipts to users

 144  $input= $ooz_website_settings[input]; // Can be either "stream" or "pop3"

 145  $autoFill= $ooz_website_settings[autoFill]; // Set this to autofill user information fields from mysql

 146  $noThreads= $ooz_website_settings[noThreads]; // Only take first message out of an email thread, on updates

 147  $sendCheckLists= $ooz_website_settings[checkLists]; // Set this to send out checklists (populated via templates)

 148  

 149  /* **** Mail Server Settings **** */

 150  $popHost= $ooz_website_settings[popHost]; // Mail server hostname

 151  $popPort= $ooz_website_settings[popPort]; // Mail server port

 152  $popUser= $ooz_website_settings[popUser]; // Mail server username

 153  $popPass= $ooz_website_settings[popPass]; // Mail server password

 154  $popConn= 0;

 155  $gotMail= false;

 156  

 157  /* ******** Script Flow ******* *

 158   * The brain of the entire script. It controls the actual flow of the functions included in the script. */
 159  
 160  $time= time();
 161  
 162  if ($input == "stream")
 163  {
 164      $email= getStream();
 165      if (isset ($email))
 166      {
 167          $msg= mimeDecode($email);
 168      }
 169      if (isset ($msg))
 170      {
 171          $tkt= buildTicket($msg);
 172      }
 173      if (isset ($tkt))
 174      {
 175          getTicketContent($msg);
 176          routeTicket();
 177          if ($autoFill == "On")
 178          {
 179              autoFillUser();
 180          }
 181          $tid= writeTicket();
 182      }
 183      if (isset ($tid))
 184      {
 185          sendReceipt();
 186          sendPage();
 187          checkLists();
 188      }
 189  }
 190  elseif ($input == "pop3")
 191  {
 192      if (popConnect())
 193      {
 194          $emailArray= getMessages();
 195      }
 196      if (isset ($emailArray))
 197      {
 198          foreach ($emailArray as $email)
 199          {
 200              $msg= mimeDecode($email);
 201              if (isset ($msg))
 202              {
 203                  $tkt= buildTicket($msg);
 204              }
 205              if (isset ($tkt))
 206              {
 207                  getTicketContent($msg);
 208                  routeTicket();
 209                  if ($autoFill == "On")
 210                  {
 211                      autoFillUser();
 212                  }
 213                  $tid= writeTicket();
 214              }
 215              if (isset ($tid))
 216              {
 217                  sendReceipt();
 218                  sendPage();
 219                  checkLists();
 220              }
 221          }
 222          popDisconnect();
 223      }
 224  }
 225  else
 226  {
 227      print "Input method not set. \n";
 228  }
 229  
 230  /* *** Primary Functions ***** *

 231   * Functions called directly from the Script Flow portion of the script. */
 232  
 233  /* function getStream - Collects email input, as a data stream from STDIN. Returns a single email. */

 234  function getStream()
 235  {
 236      $in= fopen("php://stdin", "r");
 237      $email= "";
 238      while (!feof($in))
 239      {
 240          $email .= fgets($in);
 241      }
 242      fclose($in);
 243      return $email;
 244  }
 245  
 246  /* function popConnect - Connects to the designated pop3 mail server, using the supplied credentials. */

 247  function popConnect()
 248  {
 249      global $popHost, $popPort, $popUser, $popPass, $popConn;
 250  
 251      $popConn= fsockopen($popHost, $popPort, $errno, $errstr, 30);
 252      if (!$popConn)
 253      {
 254          print "Connect Failed: $errstr($errno)\n";
 255          return 0;
 256      }
 257      else
 258      {
 259          $output= fgets($popConn, 128);
 260          print "$output\n";
 261          fputs($popConn, "USER $popUser\r\n");
 262          $output= fgets($popConn, 128);
 263          print "$output\n";
 264          fputs($popConn, "PASS $popPass\r\n");
 265          $output= fgets($popConn, 128);
 266          print "$output\n";
 267          return 1;
 268      }
 269  }
 270  
 271  /* function getMessages - Collects email messages from a pop3 server, and returns them as an array of messages.

 272   *                        Calls function getEmail to download each individual message. Returns an array of 

 273   *                        of email messages. */
 274  function getMessages()
 275  {
 276      global $popHost, $popPort, $popConn, $gotMail;
 277      ;
 278  
 279      $numMessages= checkForMail();
 280  
 281      if ($gotMail)
 282      {
 283          $mailArray= array ();
 284          for ($i= 0; $i < $numMessages; $i ++)
 285          {
 286              $mailArray[$i]= getEmail($i +1);
 287              deleteMessage($i +1);
 288          }
 289          return $mailArray;
 290      }
 291  }
 292  
 293  /* function popDisconnect - Disconnects the pop3 session. */

 294  function popDisconnect()
 295  {
 296      global $popConn;
 297      fputs($popConn, "QUIT\r\n");
 298      $output= fgets($popConn, 128);
 299      print "$output\n";
 300      fclose($popConn);
 301      $popConn= 0;
 302  }
 303  
 304  /* function mimeDecode - decodes the MIME message of the supplied email. Returns an array of all

 305   *                       components of the message. */
 306  function mimeDecode($email)
 307  {
 308      $p['include_bodies']= true;
 309      $p['include_headers']= true;
 310      $p['decode_headers']= true;
 311      $p['crlf']= "\r\n";
 312      $p['input']= $email;
 313  
 314      $msg= Mail_mimeDecode :: decode($p);
 315      return $msg;
 316  }
 317  
 318  /* function buildTicket - Builds,and returns, the major structure of the ticket to be entered into OOZ. */

 319  function buildTicket($msg)
 320  {
 321      global $time;
 322  
 323      $head= $msg->headers;
 324      $from= getFrom($head);
 325      $uname= explode("@", $from);
 326      $subject= textCleaner($head['subject']);
 327  
 328      $tkt= array ();
 329      $tkt['Date']= $time;
 330      $tkt['UserName']= $uname[0];
 331      $tkt['UserEmail']= $from;
 332      $tkt['Category']= "Imported by Mail Gateway";
 333      $tkt['Subject']= $subject;
 334      $tkt['gid']= 1;
 335      $tkt['sid']= 1;
 336      $tkt['Supporter']= "support_pool";
 337      $tkt['Priority']= "Medium";
 338      $tkt['Status']= "Unassigned";
 339      $tkt['Office']= "";
 340      $tkt['Phone']= "";
 341      $tkt['Platform']= "Windows";
 342      $tkt['tid']= "";
 343      $tkt['NewTicket']= "";
 344  
 345      if ($msg->ctype_primary == "text")
 346      {
 347          $tkt['Description']= $msg->body;
 348      }
 349  
 350      return $tkt;
 351  }
 352  
 353  /* function getTicketContent - Reads all the parts of the decoded message and assigns attachments, 

 354   *                             description, and subject of the created/updated ticket. Saves updates

 355   *                             to the global $tkt array. */
 356  function getTicketContent($msg)
 357  {
 358      global $tkt, $time, $attach, $path, $noThreads;
 359      foreach ($msg->parts as $part)
 360      {
 361          if (($part->ctype_primary == 'text') and ($part->ctype_secondary == 'plain'))
 362          {
 363              $tkt['Description']= $part->body;
 364          }
 365          if ($part->ctype_primary == 'image')
 366          {
 367              $enc= $part->headers['content-transfer-encoding'];
 368              $fileName= fileNameCleaner("image.".$part->ctype_secondary);
 369              $fileDest= $path."/".$attach."/".$fileName;
 370              $fileSize= writeFile($enc, $fileDest, $part->body);
 371  
 372              $tkt[]= array ("filename" => $fileName, "filetype" => $part->ctype_primary."/".$part->ctype_secondary, "filesize" => $fileSize);
 373          }
 374          elseif (isset ($part->disposition) and ($part->disposition == 'attachment'))
 375          {
 376              if (isset ($part->ctype_parameters['filename']))
 377              {
 378                  $fileName= $part->ctype_parameters['filename'];
 379              }
 380              elseif (isset ($part->ctype_parameters['name']))
 381              {
 382                  $fileName= $part->ctype_parameters['name'];
 383              }
 384              else
 385              {
 386                  $fileName= "not_named";
 387              }
 388              $enc= $part->headers['content-transfer-encoding'];
 389              $fileName= fileNameCleaner($fileName);
 390              $fileDest= $path."/".$attach."/".$fileName;
 391              $fileSize= writeFile($enc, $fileDest, $part->body);
 392              $tkt[]= array ("filename" => $fileName, "filetype" => $part->ctype_primary."/".$part->ctype_secondary, "filesize" => $fileSize);
 393          }
 394          elseif (isset ($part->disposition) and ($part->disposition == 'inline'))
 395          {
 396              if (isset ($part->ctype_parameters['filename']))
 397              {
 398                  $fileName= $part->ctype_parameters['filename'];
 399              }
 400              elseif (isset ($part->ctype_parameters['name']))
 401              {
 402                  $fileName= $part->ctype_parameters['name'];
 403              }
 404              else
 405              {
 406                  $fileName= "not_named";
 407              }
 408              $enc= $part->headers['content-transfer-encoding'];
 409              $fileName= fileNameCleaner($fileName);
 410              $fileDest= $path."/".$attach."/".$fileName;
 411              $fileSize= writeFile($enc, $fileDest, $part->body);
 412              $tkt[]= array ("filename" => $fileName, "filetype" => $part->ctype_primary."/".$part->ctype_secondary, "filesize" => $fileSize);
 413          }
 414          if (isset ($part->parts) and (is_array($part->parts)))
 415          {
 416              getTicketContent($part);
 417          }
 418      }
 419      if ($tkt['Description'] == "")
 420      {
 421          $tkt['Description']= "No suitable message found";
 422      }
 423      $tkt['Description']= textCleaner($tkt['Description']);
 424  }
 425  
 426  /* function writeTicket - writes the ticket to the database and attached files to the filesystem. */

 427  function writeTicket()
 428  {
 429      global $tkt, $time, $dbPre, $path, $attach, $db, $lang_ticket, $lang_MG_reOpen, $noThreads;
 430  
 431      if (ereg("$lang_ticket #[0-9]+", $tkt['Subject']))
 432      {
 433          $tid= explode("#", $tkt['Subject']);
 434          $tid= explode(" ", $tid[1]);
 435          $tid= $tid[0];
 436          $subject= strstr($tkt['Subject'], " - ");
 437          $subject= str_replace(" - ", "", $subject);
 438          if ($noThreads == "On")
 439          {
 440              if ($thread= explode("-Original Message-", $tkt['Description']))
 441              {
 442                  $tkt['Description']= $thread[0];
 443              }
 444          }
 445  
 446          $updateString= $time.' $lang_by Mail Gate --//--<i>'.$tkt['UserEmail'].' wrote:</i> <br><br>'.$tkt['Description'].'--//--';
 447          $sql= "SELECT update_log FROM ".$dbPre."tickets WHERE id = '$tid'";
 448          $result= $db->query($sql);
 449  
 450          $row= $db->fetch_array($result);
 451          $oldLog= $row[0];
 452          $newLog= $oldLog.$updateString;
 453          $newLog= mysql_escape_string($newLog);
 454  
 455          $sql= "UPDATE ".$dbPre."tickets set update_log='$newLog', lastupdate = '$time' WHERE id = '$tid'";
 456          $db->query($sql);
 457  
 458          if (ereg("$lang_MG_reOpen", $tkt['Subject']))
 459          {
 460              $sql= "UPDATE ".$dbPre."tickets set status='Open', lastupdate = '$time' WHERE id = '$tid'";
 461              $db->query($sql);
 462          }
 463      }
 464      else
 465      {
 466          $dt= $tkt['Date'];
 467          $gid= $tkt['gid'];
 468          $supp= $tkt['Supporter'];
 469          $sid= $tkt['sid'];
 470          $pri= $tkt['Priority'];
 471          $stat= $tkt['Status'];
 472          $user= $tkt['UserName'];
 473          $mail= $tkt['UserEmail'];
 474          $off= $tkt['Office'];
 475          $ph= $tkt['Phone'];
 476          $cat= $tkt['Category'];
 477          $plat= $tkt['Platform'];
 478          $subj= $tkt['Subject'];
 479          $desc= $tkt['Description'];
 480          $log= $time.' $lang_by Mail Gate --//--<i>$lang_taskcreatedby Mail Gate</i>--//--';
 481  
 482          $sql= "INSERT into ".$dbPre."tickets VALUES(NULL, $dt, $gid, '$supp', $sid, '$pri', '$stat', '$user', '$mail', '$off',";
 483          $sql .= " '$ph', '$cat', '$plat', '$subj', '$desc', '$log', 0, $dt)";
 484          $db->query($sql);
 485          $tid= $db->insert_id();
 486          $tkt['NewTicket']= "yes";
 487      }
 488      foreach ($tkt as $key => $val)
 489      {
 490          if (is_array($val))
 491          {
 492              foreach ($val as $k => $v)
 493              {
 494                  if ($k == 'filename')
 495                  {
 496                      $fname= $val['filename'];
 497                      $ftype= $val['filetype'];
 498                      $fsize= $val['filesize'];
 499  
 500                      $sql= "INSERT into ".$dbPre."attachments VALUES(NULL, NULL, $tid, '$fname', '$path/$attach/',
 501                                         '$ftype', $fsize, NULL, 0, 'Mail Gate', ".$time.")";
 502                      $db->query($sql);
 503                  }
 504              }
 505          }
 506      }
 507      $tkt['tid']= $tid;
 508      return $tid;
 509  }
 510  
 511  /* function sendReceipt - Sends an email receipt to the ticket creator, as a service courtesy. */

 512  function sendReceipt()
 513  {
 514      global $tkt, $hdMail, $sendReceipt, $lang_ticket;
 515      if ($sendReceipt == "On")
 516      {
 517          $subj= $lang_ticket." #".$tkt['tid'];
 518          $header= "From: ".$hdMail."\n";
 519  
 520          if ($tkt['NewTicket'] == "yes")
 521          {
 522              $newReceipt= getMessageTemplate("mailGate_new_receipt");
 523              $reply= $reply.$newReceipt;
 524          }
 525          else
 526          {
 527              $updReceipt= getMessageTemplate("mailGate_update_receipt");
 528              $reply= $reply.$updReceipt;
 529          }
 530  
 531          if (mail($tkt['UserEmail'], $subj, $reply, $header))
 532          {
 533              print "Receipt emailed for ticket #".$tkt['tid']."\n";
 534          }
 535          else
 536          {
 537              print "Receipt failed for ticket #".$tkt['tid']."\n";
 538          }
 539      }
 540  }
 541  
 542  /* function sendPage - Sends email page to designated support staff. */

 543  function sendPage()
 544  {
 545      global $tkt, $url, $pageMe, $pageAll, $pageAdmin, $hdMail, $db, $dbPre, $lang_ticket, $lang_MG_latest_update;
 546      $to= "";
 547      $header= "From: ".$hdMail."\n";
 548      $subject= $lang_ticket." #".$tkt['tid'];
 549  
 550      if ($tkt['NewTicket'] == "yes")
 551      {
 552          if ($pageAll == "On")
 553          {
 554              $message= getMessageTemplate("mailGate_new_page");
 555              $sql= "SELECT pager_email FROM ".$dbPre."users WHERE supporter='1'";
 556              $result= $db->query($sql);
 557              while ($suppList= $db->fetch_array($result))
 558              {
 559                  $to .= $suppList['pager_email'].",";
 560              }
 561          }
 562          else
 563          {
 564              if ($pageMe == "On")
 565              {
 566                  $message= getMessageTemplate("mailGate_new_page");
 567                  $sql= "SELECT pager_email FROM ".$dbPre."users WHERE user_name='".$tkt['Supporter']."'";
 568                  $result= $db->query($sql);
 569                  $row= $db->fetch_array($result);
 570                  $suppEmail= $row[0];
 571                  $to .= $suppEmail.",";
 572              }
 573              if ($pageAdmin == "On")
 574              {
 575                  $message= getMessageTemplate("mailGate_new_page");
 576                  $sql= "SELECT pager_email FROM ".$dbPre."users WHERE admin='1'";
 577                  $result= $db->query($sql);
 578                  while ($suppList= $db->fetch_array($result))
 579                  {
 580                      $to .= $suppList['pager_email'].",";
 581                  }
 582  
 583              }
 584          }
 585      }
 586      else
 587      {
 588          if ($pageMe == "On")
 589          {
 590              $message= getMessageTemplate("mailGate_update_page");
 591              $message .= "\n\n".$lang_MG_latest_update.":\n\n";
 592              $message .= $tkt['Description'];
 593              $sql= "SELECT supporter FROM ".$dbPre."tickets WHERE id='".$tkt['tid']."'";
 594              $result= $db->query($sql);
 595              $row= $db->fetch_array($result);
 596              $supporter= $row[0];
 597              $sql= "SELECT pager_email FROM ".$dbPre."users WHERE user_name='".$supporter."'";
 598              $result= $db->query($sql);
 599              $row= $db->fetch_array($result);
 600              $suppEmail= $row[0];
 601              $to .= $suppEmail.",";
 602          }
 603          if ($pageAdmin == "On")
 604          {
 605              $message= getMessageTemplate("mailGate_update_page");
 606              $message .= "\n\n".$lang_MG_latest_update.":\n\n";
 607              $message .= $tkt['Description'];
 608              $sql= "SELECT pager_email FROM ".$dbPre."users WHERE admin='1'";
 609              $result= $db->query($sql);
 610              while ($suppList= $db->fetch_array($result))
 611              {
 612                  $to .= $suppList['pager_email'].",";
 613              }
 614          }
 615      }
 616  
 617      if (isset ($to))
 618      {
 619          if (mail($to, $subject, $message, $header))
 620          {
 621              print "Ticket #".$tkt['tid']." - Page emailed to: ".$to."\n";
 622          }
 623          else
 624          {
 625              print "Ticket #".$tkt['tid']." - Sending Page FAILED\n";
 626          }
 627      }
 628  }
 629  
 630  /* function getMessageTemplate - Collects language, ticket, and template information in order to build

 631  *                               a message based on templates stored in the templates table. */
 632  function getMessageTemplate($temp_name)
 633  {
 634      global $language, $default_language, $db, $ooz_server_settings, $tkt;
 635      $templates_table= $ooz_server_settings[table_prefix].templates;
 636      $tickets_table= $ooz_server_settings[table_prefix].tickets;
 637      $server_name= $ooz_server_settings[server_name];
 638      $oneorzero_web_alias= $ooz_server_settings[oneorzero_web_alias];
 639      $enable_ssl= $ooz_website_settings[socket];
 640      if ($language == '')
 641      {
 642          require "../lang/$default_language.lang.php";
 643      }
 644      else
 645      {
 646          require "../lang/$language.lang.php";
 647      }
 648  
 649      if ($oneorzero_web_alias == "")
 650      {
 651          $oneorzero_http_prefix= "http://".$server_name."/";
 652      }
 653      else
 654      {
 655          $oneorzero_http_prefix= "http://".$server_name."/".$oneorzero_web_alias."/";
 656      }
 657  
 658      if ($enable_ssl == "On")
 659      {
 660          $oneorzero_http_prefix= eregi_replace("http", "https", $oneorzero_http_prefix);
 661      }
 662      $supporter_site_url= $oneorzero_http_prefix."supporter"; //url for supporters

 663      $admin_site_url= $oneorzero_http_prefix."admin"; //url for admins

 664      $site_url= eregi_replace("/supporter.*", "", $supporter_site_url);
 665  
 666      $sql= "select * from $tickets_table where id=".$tkt[tid];
 667      $result= $db->query($sql);
 668      $ticket= $db->fetch_array($result);
 669  
 670      $sql= "select template from $templates_table where name='".$temp_name."'";
 671      $result= $db->query($sql);
 672      $template= $db->fetch_array($result);
 673      $template= str_replace("\\'", "'", $template[0]);
 674  
 675      eval ("\$messageBody = \"$template\";");
 676      return ($messageBody);
 677  }
 678  
 679  /* function routeTicket - Assigns ticket supporter automatically based on email subject and matching

 680   *                        criteria in the ticketRoute() array. */
 681  function routeTicket()
 682  {
 683      global $tkt, $tktRoute, $dbPre, $db, $dbRouting;
 684  
 685      if ($tkt['Subject'] == "")
 686      {
 687          $tkt['Subject']= strip_tags(substr($tkt['Description'], 0, 25));
 688      }
 689      $sql= "SELECT id, user_name, routing from ".$dbPre."users";
 690      $result= $db->query($sql);
 691      while ($row= $db->fetch_array($result))
 692      {
 693          if ($row[2] != "")
 694          {
 695              if (eregi($row[2], $tkt['Subject']))
 696              {
 697                  $tkt['Supporter']= $row[1];
 698                  $tkt['sid']= $row[0];
 699                  $tkt['Status']= "Open";
 700              }
 701          }
 702      }
 703  }
 704  
 705  /* function autoFillUser - Fills in user information, if it's available in the database, automatically. */

 706  function autoFillUser()
 707  {
 708      global $tkt, $dbPre, $db;
 709  
 710      $sql= "SELECT office, phone FROM ".$dbPre."users WHERE user_name = '".$tkt['UserName']."' limit 1";
 711      $result= $db->query($sql);
 712      $row= $db->fetch_array($result);
 713      $tkt['Office']= $row[0];
 714      $tkt['Phone']= $row[1];
 715  
 716  }
 717  /* *** Secondary Functions *** *

 718   * Functions called from within other Functions */
 719  
 720  /* function checkForMail - Check for the existence of email on the pop3 server. */

 721  function checkForMail()
 722  {
 723      global $popHost, $popPort, $popConn, $gotMail;
 724  
 725      fputs($popConn, "STAT\r\n");
 726      $output= fgets($popConn, 128);
 727      $ack= strtok($output, " "); // Bleed off +OK

 728      $numMessages= strtok(" "); // Get what we wanted

 729  
 730      print "Ack: $ack, Num Messages: $numMessages \n";
 731  
 732      if ($numMessages > 0)
 733      {
 734          print "***New mail***\n";
 735          $gotMail= true;
 736      }
 737      else
 738      {
 739          print "***No mail***\n";
 740          $gotMail= false;
 741      }
 742      return $numMessages;
 743  }
 744  
 745  /* function getEmail - Collect each individual message and return to the getMessages function. */

 746  function getEmail($num)
 747  {
 748      global $popConn;
 749      $message= "";
 750      fputs($popConn, "RETR $num\r\n");
 751      $output= fgets($popConn, 512);
 752  
 753      if (strtok($output, "+OK"))
 754      {
 755          while (!ereg("^\.\r\n", $output))
 756          {
 757              $output= fgets($popConn, 512);
 758              $message .= $output;
 759          }
 760          return $message;
 761      }
 762  }
 763  
 764  /* function deleteMessage - Delete messages from pop3 server after downloading them. */

 765  function deleteMessage($message)
 766  {
 767      global $popConn;
 768      fputs($popConn, "DELE $message\r\n");
 769  }
 770  
 771  /* function getFrom - Quickly pull out the email address of the sender. */

 772  function getFrom($head)
 773  {
 774      $regex= '\<*[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,6})\>*';
 775      if (array_key_exists('reply-to', $head) and ereg($regex, $head['reply-to']))
 776      {
 777          $from= $head['reply-to'];
 778      }
 779      elseif (array_key_exists('return-path', $head) and ereg($regex, $head['return-path']))
 780      {
 781          $from= $head['return-path'];
 782      }
 783      elseif (array_key_exists('from', $head) and ereg($regex, $head['from']))
 784      {
 785          $from= $head['from'];
 786      }
 787  
 788      $from= ereg_replace("^(.*)<", "", $from);
 789      $from= str_replace(array ("<", ">", " "), "", $from);
 790  
 791      return $from;
 792  }
 793  
 794  /* function writeFile - Write attached file, to the filesystem, using the proper encoding mechanism. */

 795  function writeFile($enc, $fileDest, $body)
 796  {
 797      $fp= fopen($fileDest, 'w');
 798      if ($enc == 'base64')
 799      {
 800          $fileSize= fwrite($fp, base64_decode($body));
 801      }
 802      else
 803      {
 804          $fileSize= fwrite($fp, $body);
 805      }
 806      fclose($fp);
 807      return $fileSize;
 808  }
 809  
 810  /* function fileNameCleaner - Strip out unwanted/unprintable, characters from filenames. */

 811  function fileNameCleaner($fileName)
 812  {
 813      $mtime= explode(" ", microtime());
 814      $mtime= $mtime[1].substr($mtime[0], 5, 3);
 815  
 816      $fileName= preg_replace('/[^a-zA-Z0-9\.\$\%\'\`\-\@\{\}\~\!\#\(\)\&\_\^]/', '', str_replace(array (' ', '%20'), array ('_', '_'), $fileName));
 817      $fileName= str_replace("'", "", $fileName);
 818      $fileName= $mtime."_".$fileName;
 819  
 820      return $fileName;
 821  
 822  }
 823  
 824  /* function textCleaner - Strip out unwanted/unprintable characters from the subject. */

 825  function textCleaner($text)
 826  {
 827      $text= str_replace("'", "", $text);
 828      $text= str_replace("=20", "\n", $text);
 829      return $text;
 830  }
 831  
 832  /* function checkLists - Send checklists when uncommon tickets are created and need specific processes. */

 833  function checkLists()
 834  {
 835      global $sendCheckLists, $dbPre, $tkt, $ooz_website_settings, $db, $hdMail;
 836      if ($sendCheckLists == 'On')
 837      {
 838          $header= "From: ".$hdMail."\n";
 839          $listNames= $ooz_website_settings[listNames];
 840          if (isset ($listNames))
 841          {
 842              if (preg_match("/$listNames/i", $tkt['Subject'], $matches))
 843              {
 844                  $match= strtolower($matches[0]);
 845                  $subject= $match." checklist";
 846                  $tempName= "mailGate_".$match."_checklist";
 847                  $message= getMessageTemplate($tempName);
 848                  if ($message != "")
 849                  {
 850                      $sql= "SELECT pager_email FROM ".$dbPre."users WHERE supporter='1'";
 851                      $result= $db->query($sql);
 852                      while ($suppList= $db->fetch_array($result))
 853                      {
 854                          $to .= $suppList['pager_email'].",";
 855                      }
 856                  }
 857                  else
 858                  {
 859                      print "no template\n";
 860                  }
 861                  if (isset ($to))
 862                  {
 863                      if (mail($to, $subject, $message, $header))
 864                      {
 865                          print "Ticket #".$tkt['tid']." - Checklist emailed to: ".$to."\n";
 866                      }
 867                      else
 868                      {
 869                          print "Ticket #".$tkt['tid']." - Sending Checklist FAILED\n";
 870                      }
 871                  }
 872              }
 873          }
 874      }
 875  }


[ Powered by PHPXref - Served by Debian GNU/Linux ]