A PHP Error was encountered

Severity: 8192

Message: Function create_function() is deprecated

Filename: geshi/geshi.php

Line Number: 4698

Backtrace:

File: /home/httpd/vhosts/scratchbook.ch/geopaste.scratchbook.ch/application/libraries/geshi/geshi.php
Line: 4698
Function: _error_handler

File: /home/httpd/vhosts/scratchbook.ch/geopaste.scratchbook.ch/application/libraries/geshi/geshi.php
Line: 4621
Function: _optimize_regexp_list_tokens_to_string

File: /home/httpd/vhosts/scratchbook.ch/geopaste.scratchbook.ch/application/libraries/geshi/geshi.php
Line: 1655
Function: optimize_regexp_list

File: /home/httpd/vhosts/scratchbook.ch/geopaste.scratchbook.ch/application/libraries/geshi/geshi.php
Line: 2029
Function: optimize_keyword_group

File: /home/httpd/vhosts/scratchbook.ch/geopaste.scratchbook.ch/application/libraries/geshi/geshi.php
Line: 2168
Function: build_parse_cache

File: /home/httpd/vhosts/scratchbook.ch/geopaste.scratchbook.ch/application/libraries/Process.php
Line: 45
Function: parse_code

File: /home/httpd/vhosts/scratchbook.ch/geopaste.scratchbook.ch/application/models/Pastes.php
Line: 517
Function: syntax

File: /home/httpd/vhosts/scratchbook.ch/geopaste.scratchbook.ch/application/controllers/Main.php
Line: 693
Function: getPaste

File: /home/httpd/vhosts/scratchbook.ch/geopaste.scratchbook.ch/index.php
Line: 315
Function: require_once

mail2itop.php - Stikked
From Sharp Meerkat, 13 Years ago, written in PHP.
Embed
  1. <?php
  2. /**
  3.  * Class and Function List:
  4.  * Function list:
  5.  * - __construct()
  6.  * - run()
  7.  * - _get_email()
  8.  * - _update_handler()
  9.  * - _resolve_ticket()
  10.  * - _update_duedate()
  11.  * - _update_priority()
  12.  * - _create_ticket()
  13.  * - _delete_email()
  14.  * - _alert()
  15.  * - __destruct()
  16.  * - __construct()
  17.  * - _get_subject()
  18.  * - _get_description()
  19.  * - _get_customer()
  20.  * - _get_jira_id()
  21.  * - _get_updates()
  22.  * - _get_priority()
  23.  * - _get_urgency()
  24.  * - logfile()
  25.  * - decode_subject()
  26.  * - string_in_values()
  27.  * Classes list:
  28.  * - Mail2itop
  29.  * - Ticket
  30.  */
  31. include ('../config-itop.php');
  32. require_once ('itopsoaptypes.class.inc.php');
  33. require_once ('simple_html_dom.php');
  34.  
  35. //
  36. $m = new Mail2itop($MySettings['db_host'], $MySettings['db_user'], $MySettings['db_pwd'], $MySettings['db_name']);
  37. $m->run();
  38.  
  39. class Mail2itop
  40. {
  41.         var $mailbox;
  42.        
  43.         function __construct($db_host, $db_user, $db_pwd, $db_name)
  44.         {
  45.                 mysql_connect($db_host, $db_user, $db_pwd) || die(mysql_error());
  46.                 mysql_select_db($db_name);
  47.                 $this->mailbox = imap_open("{mysite.com:143}", "user", "pass");
  48.         }
  49.        
  50.         function run()
  51.         {
  52.                 $this->_get_email();
  53.         }
  54.  
  55.         //
  56.         private
  57.         function _get_email()
  58.         {
  59.                 $imap_header = @imap_header($this->mailbox, 1);
  60.                
  61.                 if ($imap_header)
  62.                 {
  63.                         $imap_body = imap_body($this->mailbox, 1);
  64.                         $t = new Ticket($imap_header, $imap_body);
  65.                        
  66.                         if ($t->subject != '')
  67.                         {
  68.                                 logfile('got mail with subject: ' . $t->subject);
  69.                                
  70.                                 if (string_in_values($t->subject, 'created:,moved:,updated:'))
  71.                                 {
  72.                                        
  73.                                         if (string_in_values($t->subject, 'created:,moved:'))
  74.                                         {
  75.                                                 $this->_create_ticket($t);
  76.                                         }
  77.                                         else
  78.                                         if (string_in_values($t->subject, 'updated:'))
  79.                                         {
  80.                                                 $this->_update_handler($t);
  81.                                         }
  82.                                 }
  83.                         }
  84.  
  85.                         //delete email
  86.                         $this->_delete_email();
  87.  
  88.                         //get next email
  89.                         $this->_get_email();
  90.                 }
  91.         }
  92.  
  93.         //
  94.         private
  95.         function _update_handler($t)
  96.         {
  97.                 logfile(print_r($t->updates, true));
  98.                 foreach ($t->updates as $update)
  99.                 {
  100.                        
  101.                         if (string_in_values($update[0], 'status') && string_in_values($update[2], 'resolved'))
  102.                         {
  103.                                 logfile('resolving ticket');
  104.                                 $this->_resolve_ticket($t);
  105.                         }
  106.                         else
  107.                         if (string_in_values($update[0], 'due date'))
  108.                         {
  109.                                 logfile('updating due date');
  110.                                 $this->_update_duedate($t, $update[2]);
  111.                         }
  112.                         else
  113.                         if (string_in_values($update[0], 'priority'))
  114.                         {
  115.                                 logfile('updating priority');
  116.                                 $this->_update_priority($t, $update[2]);
  117.                         }
  118.                         else
  119.                         if (string_in_values($update[0], 'status') && string_in_values($update[2], 'reopened'))
  120.                         {
  121.                                 logfile('reopen ticket');
  122.                                 $this->_create_ticket($t);
  123.                         }
  124.                 }
  125.         }
  126.  
  127.         //
  128.         private
  129.         function _resolve_ticket($t)
  130.         {
  131.                 $q = mysql_query("SELECT id FROM ticket WHERE title LIKE '%" . $t->jira_id . "%'");
  132.                 $r = mysql_fetch_assoc($q);
  133.                 $ticked_id = $r['id'];
  134.                 mysql_query("UPDATE ticket_response SET status = 'resolved' WHERE id = '" . $ticked_id . "';");
  135.                 logfile('set status to resolved: ' . $t->jira_id);
  136.         }
  137.  
  138.         //
  139.         private
  140.         function _update_duedate($t, $duedate)
  141.         {
  142.                 $duedate = trim($duedate);
  143.                 $duedate = str_replace('/', '.', $duedate);
  144.                 $duedate = strtotime($duedate);
  145.                 $duedate = date('c', $duedate);
  146.                 $q = mysql_query("SELECT id FROM ticket WHERE title LIKE '%" . $t->jira_id . "%'");
  147.                 $r = mysql_fetch_assoc($q);
  148.                 $ticked_id = $r['id'];
  149.                 mysql_query("UPDATE ticket_response SET ttr_escalation_deadline = '" . $duedate . "' WHERE id = '" . $ticked_id . "';");
  150.                 logfile('set TTR to ' . $duedate . ': ' . $t->jira_id);
  151.         }
  152.  
  153.         //
  154.         private
  155.         function _update_priority($t, $priority)
  156.         {
  157.                
  158.                 if (string_in_values($priority, 'critical,blocker'))
  159.                 {
  160.                         $this->_alert($t, 'priority_change', 'alert-ops-email');
  161.                         $this->_alert($t, 'priority_change', 'alert-ops-sms');
  162.                         $this->_alert($t, 'priority_change', 'alert-ops-pager');
  163.                 }
  164.         }
  165.  
  166.         //
  167.         private
  168.         function _create_ticket($t)
  169.         {
  170.  
  171.                 //send alert
  172.                 $this->_alert($t, 'new_ticket', 'alert-ops-email');
  173.                 $this->_alert($t, 'new_ticket', 'alert-ops-sms');
  174.                
  175.                 if (string_in_values($t->priority, 'blocker'))
  176.                 {
  177.                         $this->_alert($t, 'new_ticket', 'alert-ops-pager');
  178.                 }
  179.  
  180.                 //soap
  181.                 $sItopRoot = 'http' . ((isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] != 'off')) ? 's' : '') . '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . dirname($_SERVER['SCRIPT_NAME']) . '/..';
  182.                 $sWsdlUri = $sItopRoot . '/webservices/itop.wsdl.php';
  183.  
  184.                 //$sWsdlUri .= '?service_category=';
  185.                 $aSOAPMapping = SOAPMapping::GetMapping();
  186.                 ini_set("soap.wsdl_cache_enabled", "0");
  187.                 $oSoapClient = new SoapClient($sWsdlUri, array(
  188.                         'trace' => 1,
  189.                         'classmap' => $aSOAPMapping,
  190.                 ));
  191.  
  192.                 //begin
  193.                 try
  194.                 {
  195.                         $oRes = $oSoapClient->CreateIncidentTicket('importer', /* login */
  196.  
  197.                         'cRDCcsAXSi3cU', /* password */
  198.  
  199.                         $t->subject, /* title */
  200.  
  201.                         $t->description, /* description */
  202.  
  203.                         null, /* caller */
  204.  
  205.                         new SOAPExternalKeySearch(array(
  206.                                 new SOAPSearchCondition('name', $t->customer)
  207.                         )) , /* customer */
  208.  
  209.                         new SOAPExternalKeySearch(array(
  210.                                 new SOAPSearchCondition('name', 'Jira-Import')
  211.                         )) , /* service */
  212.  
  213.                         new SOAPExternalKeySearch(array(
  214.                                 new SOAPSearchCondition('name', 'Jira-Import')
  215.                         )) , /* service subcategory */
  216.  
  217.                         '', /* product */
  218.  
  219.                         new SOAPExternalKeySearch(array(
  220.                                 new SOAPSearchCondition('name', 'Operation')
  221.                         )) , /* workgroup */
  222.  
  223.                         '', /* affected CIs */
  224.  
  225.                         '2', /* impact; a service, for the correct mapping :) */
  226.  
  227.                         $t->urgency
  228.  
  229.                         /* urgency */);
  230.                         echo "<p>CreateIncidentTicket() returned:\n";
  231.                         echo "<pre>\n";
  232.                         print_r($oRes);
  233.                         echo "</pre>\n";
  234.                         echo "</p>\n";
  235.                         logfile('CreateIncidentTicket() returned: ' . print_r($oRes, true));
  236.                 }
  237.                 catch(SoapFault $e)
  238.                 {
  239.                         echo "<h1>SoapFault Exception: {$e->getMessage() }</h1>\n";
  240.                         echo "<h2>Request</h2>\n";
  241.                         echo "<pre>\n";
  242.                         echo htmlspecialchars($oSoapClient->__getLastRequest()) . "\n";
  243.                         echo "</pre>";
  244.                         echo "<h2>Response</h2>";
  245.                         echo $oSoapClient->__getLastResponse() . "\n";
  246.                         logfile('ERROR');
  247.                         logfile('SoapFault Exception: ' . $e->getMessage());
  248.                         logfile('Request: ' . $oSoapClient->__getLastRequest());
  249.                         logfile('Response: ' . $oSoapClient->__getLastResponse());
  250.  
  251.                         //send email
  252.                         mail('e@mail.com', 'iTop Import Error', 'SoapFault Exception: ' . $e->getMessage() . ' Request: ' . $oSoapClient->__getLastRequest() . ' Response: ' . $oSoapClient->__getLastResponse());
  253.                 }
  254.                 logfile("\n");
  255.                 return true;
  256.         }
  257.  
  258.         //
  259.         private
  260.         function _delete_email()
  261.         {
  262.                 imap_delete($this->mailbox, 1);
  263.                 imap_expunge($this->mailbox);
  264.         }
  265.  
  266.         //
  267.         private
  268.         function _alert($t, $what, $who = 'alert-ops-sms')
  269.         {
  270.                
  271.                 if (!stristr($t->subject, '[jira] OPS'))
  272.                 {
  273.  
  274.                         //only handle OPS mails
  275.                         return true;
  276.                 }
  277.  
  278.                 //variables
  279.                 $subject = str_replace('[jira] ', '', $t->subject);
  280.                 $priority = $t->priority;
  281.  
  282.                 //what
  283.                 switch ($what)
  284.                 {
  285.                 case 'new_ticket':
  286.                         $text = '(' . $priority . ') ' . $subject . ' - http://jira.yoursite.com/browse/' . $t->jira_id;
  287.                         $subject = $text;
  288.                         $body = $text;
  289.                 break;
  290.                 case 'priority_change':
  291.                         $text = 'Priority changed: (' . $priority . ') ' . $subject . ' - http://jira.yoursite.com/browse/' . $t->jira_id;
  292.                         $subject = $text;
  293.                         $body = $text;
  294.                 break;
  295.                 }
  296.  
  297.                 //who
  298.                 $q = mysql_query("SELECT description FROM document WHERE name = '" . $who . "'");
  299.                 $r = mysql_fetch_assoc($q);
  300.                 $who_mails = $r['description'];
  301.  
  302.                 //send
  303.                 mail($who_mails, $subject, $body, 'From: ops <e@mail.com>');
  304.         }
  305.        
  306.         function __destruct()
  307.         {
  308.                 imap_close($this->mailbox);
  309.                 mysql_close();
  310.         }
  311. }
  312.  
  313. class Ticket
  314. {
  315.         var $imap_body;
  316.         var $imap_header;
  317.         var $subject;
  318.         var $description;
  319.         var $customer;
  320.         var $jira_id;
  321.         var $updates;
  322.         var $priority;
  323.         var $urgency;
  324.        
  325.         function __construct($imap_header, $imap_body)
  326.         {
  327.  
  328.                 //fill all attributes...
  329.                 $this->imap_header = $imap_header;
  330.                 $this->imap_body = $imap_body;
  331.                 $this->subject = $this->_get_subject();
  332.                 $this->description = $this->_get_description();
  333.                 $this->customer = $this->_get_customer();
  334.                 $this->jira_id = $this->_get_jira_id();
  335.                 $this->updates = $this->_get_updates();
  336.                 $this->priority = $this->_get_priority();
  337.                 $this->urgency = $this->_get_urgency();
  338.         }
  339.  
  340.         //
  341.         private
  342.         function _get_subject()
  343.         {
  344.                 $subject = '';
  345.                 $subject = decode_subject($this->imap_header->subject);
  346.                 $subject = utf8_encode($subject);
  347.                 $subject = iconv('UTF-8', 'ISO-8859-1', $subject);
  348.                 return $subject;
  349.         }
  350.  
  351.         //
  352.         private
  353.         function _get_description()
  354.         {
  355.                 $description = imap_qprint($this->imap_body);
  356.                
  357.                 if (stristr($description, '<td bgcolor="#ffffff" valign="top">'))
  358.                 {
  359.                         $description = explode('<td bgcolor="#ffffff" valign="top">', $description);
  360.                         $description = explode('</td>', $description[2]);
  361.                         $description = trim($description[0]);
  362.                         $description = strip_tags($description);
  363.                         $description = html_entity_decode($description, ENT_COMPAT, 'UTF-8');
  364.                         $description = utf8_encode($description);
  365.                         $description = iconv('UTF-8', 'ISO-8859-1', $description);
  366.                 }
  367.                
  368.                 if ($description == '')
  369.                 {
  370.                         $description = 'Keine Beschreibung vorhanden';
  371.                 }
  372.  
  373.                 //char limiter (iTop allows only 65535 chars)
  374.                 $description = substr($description, 0, 65000);
  375.                 return $description;
  376.         }
  377.  
  378.         //
  379.         private
  380.         function _get_customer()
  381.         {
  382.                 $customer_code = '';
  383.                
  384.                 if (stristr($this->subject, '[jira] '))
  385.                 {
  386.                         $customer_code = $this->subject;
  387.                         $customer_code = explode('[jira] ', $customer_code);
  388.                         $customer_code = explode(' ', $customer_code[1]);
  389.                         $customer_code = explode('-', $customer_code[0]);
  390.                         $customer_code = $customer_code[0];
  391.                 }
  392.                 $q = mysql_query("SELECT name FROM organization WHERE code = '" . $customer_code . "'");
  393.                 $r = mysql_fetch_assoc($q);
  394.                 $customer = $r['name'];
  395.                 return $customer;
  396.         }
  397.  
  398.         //
  399.         private
  400.         function _get_jira_id()
  401.         {
  402.                 $jira_id = $this->subject;
  403.                 $jira_id = explode('[jira] ', $jira_id);
  404.                 $jira_id = $jira_id[1];
  405.                 $jira_id = explode(' -', $jira_id);
  406.                 $jira_id = $jira_id[0];
  407.                 return $jira_id;
  408.         }
  409.  
  410.         //
  411.         private
  412.         function _get_updates()
  413.         {
  414.                
  415.                 if (!stristr($this->imap_body, '<b>New Value</b>'))
  416.                 {
  417.                         return false;
  418.                 }
  419.  
  420.                 //parse imap body
  421.                 $v = $this->imap_body;
  422.                 $v = explode('<b>New Value</b>', $v);
  423.                 $v = $v[1];
  424.                 $v = explode('</table>', $v);
  425.                 $v = $v[0];
  426.  
  427.                 //cleanup
  428.                 $v = preg_replace('/\s\s+/', '', $v);
  429.                 $v = strtolower($v);
  430.  
  431.                 //parse html
  432.                 $html = str_get_html($v);
  433.                 $tr = $html->find('tr');
  434.                 $updates = array();
  435.                 for ($i = 1;$i < count($tr);$i++)
  436.                 {
  437.                         $element = $tr[$i];
  438.                         $line = str_get_html($element->innertext);
  439.                         $line = $line->find('td');
  440.                         $update_element = array();
  441.                         foreach ($line as $parsed_line)
  442.                         {
  443.                                 $e = $parsed_line->innertext;
  444.                                 $e = strip_tags($e);
  445.                                 $update_element[] = $e;
  446.                         }
  447.                         $updates[] = $update_element;
  448.                 }
  449.                 return $updates;
  450.         }
  451.  
  452.         //
  453.         private
  454.         function _get_priority()
  455.         {
  456.                 $priority = $this->imap_body;
  457.                 $priority = explode('Priority:', $priority);
  458.                 $priority = $priority[1];
  459.                 $priority = explode('</tr>', $priority);
  460.                 $priority = $priority[0];
  461.                 $priority = strip_tags($priority);
  462.                 $priority = trim($priority);
  463.                 return $priority;
  464.         }
  465.  
  466.         //
  467.         private
  468.         function _get_urgency()
  469.         {
  470.                 $priority = $this->priority;
  471.  
  472.                 //get urgency
  473.                 $urgency = 3; //$priority = low: trivial,medium,major
  474.  
  475.                
  476.                 if (string_in_values($priority, 'critical'))
  477.                 {
  478.                         $urgency = 2;
  479.                 }
  480.                 else
  481.                 if (string_in_values($priority, 'blocker'))
  482.                 {
  483.                         $urgency = 1;
  484.                 }
  485.                 return $urgency;
  486.         }
  487. }
  488.  
  489. //utils
  490.  
  491. function logfile($string)
  492. {
  493.         $logstring = date('Y-m-d H:i', mktime()) . ':   ' . $string . "\n";
  494.         file_put_contents('log.txt', $logstring, FILE_APPEND);
  495. }
  496.  
  497. function decode_subject($text)
  498. {
  499.         $r = '';
  500.         $s = imap_mime_header_decode($text);
  501.         foreach ($s as $e)
  502.         {
  503.                 $r.= $e->text;
  504.         }
  505.         return $r;
  506. }
  507.  
  508. function string_in_values($string, $values)
  509. {
  510.         $r = false;
  511.         foreach (explode(',', $values) as $v)
  512.         {
  513.                
  514.                 if (stristr($string, $v))
  515.                 {
  516.                         $r = true;
  517.                 }
  518.         }
  519.         return $r;
  520. }
  521.