PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/sources/pop3.php

https://github.com/lewellyn/TrellisDesk
PHP | 839 lines | 783 code | 28 blank | 28 comment | 12 complexity | 969bbfb45cab9b613d08574feb90aa3a MD5 | raw file
  1. <?php
  2. /**
  3. * Trellis Desk
  4. *
  5. * @copyright Copyright (C) 2009-2012 ACCORD5. All rights reserved.
  6. * @license GNU General Public License version 3 or later; see LICENSE.txt
  7. */
  8. #=============================
  9. # Safe and Secure
  10. #=============================
  11. ini_set( 'register_globals', 0 );
  12. if ( function_exists('date_default_timezone_get') )
  13. {
  14. date_default_timezone_set( date_default_timezone_get() );
  15. }
  16. if ( @ini_get( 'register_globals' ) )
  17. {
  18. while ( list( $key, $value ) = each( $_REQUEST ) )
  19. {
  20. unset( $$key );
  21. }
  22. }
  23. #=============================
  24. # Itsy Bitsy Stuff
  25. #=============================
  26. define( 'IN_HD' , 1 );
  27. ini_set( 'display_errors', 0 );
  28. #=============================
  29. # Define Our Paths
  30. #=============================
  31. define( "TD_PATH", "../" );
  32. define( 'TD_INC', TD_PATH ."includes/" );
  33. define( 'TD_SRC', TD_PATH ."sources/" );
  34. define( 'TD_SKIN', TD_PATH ."skin/" );
  35. define( 'TD_CLASS', TD_PATH ."includes/classes/class_" );
  36. define( 'TD_FUNC', TD_PATH ."includes/functions/func_" );
  37. define( 'TD_DEBUG', false );
  38. #=============================
  39. # Main Class
  40. #=============================
  41. require_once TD_INC . "trellis.php";
  42. $trellis = new trellis(1);
  43. $trellis->load_lang('global');
  44. $trellis->load_lang('tickets');
  45. #=============================
  46. # Pre-Checks
  47. #=============================
  48. if ( ! $trellis->cache->data['settings']['tickets']['new_tickets'] ) exit();
  49. #=============================
  50. # Grab Incoming Email
  51. #=============================
  52. foreach( $trellis->cache->data['depart'] as $d )
  53. {
  54. if ( $d['email_pop3'] )
  55. {
  56. if ( $mbox = imap_open( "{". $d['pop3_host'] .":110/pop3}INBOX", $d['pop3_user'], $d['pop3_pass'] ) )
  57. {
  58. $MC = imap_check($mbox);
  59. $result = imap_fetch_overview( $mbox, "1:{$MC->Nmsgs}", 0 );
  60. foreach ( $result as $msg )
  61. {
  62. $email = array(); // Initialize for Security
  63. $raw_email = imap_fetchbody( $mbox, $msg->msgno, NULL );
  64. imap_delete( $mbox, $msg->msgno );
  65. #=============================
  66. # Now the Fun Begins :D
  67. #=============================
  68. $lines = explode( "\n", $raw_email );
  69. $headers_complete = 0;
  70. $boundary_active = 0;
  71. $boundary_count = 0;
  72. while ( list( , $line ) = each( $lines ) )
  73. {
  74. if ( ! $headers_complete )
  75. {
  76. // From
  77. if ( preg_match( "/^From:\s*(.*)$/", $line, $matches ) )
  78. {
  79. $email['from'] = $matches[1];
  80. if ( strpos( $email['from'], '<' ) !== false )
  81. {
  82. if ( preg_match( "/(.*?)<(.*)>/", $email['from'], $matches ) )
  83. {
  84. $email['nickname'] = $matches[1];
  85. $email['from'] = $matches[2];
  86. if ( preg_match( "/\"([^\"]*)\"/", $email['nickname'], $matches ) )
  87. {
  88. $email['nickname'] = $matches[1];
  89. }
  90. }
  91. }
  92. }
  93. // To
  94. if ( preg_match( "/^To:\s*(.*)$/", $line, $matches ) )
  95. {
  96. $email['to'] = $matches[1];
  97. if ( strpos( $email['to'], '<' ) !== false )
  98. {
  99. if ( preg_match( "/<(.*)>/", $line, $matches ) )
  100. {
  101. $email['to'] = $matches[1];
  102. }
  103. }
  104. }
  105. // Subject
  106. if ( preg_match( "/^Subject:\s*(.*)$/", $line, $matches ) )
  107. {
  108. $email['subject'] = $matches[1];
  109. }
  110. // Date
  111. if ( preg_match( "/^Date:\s*(.*)$/", $line, $matches ) )
  112. {
  113. $email['date'] = $matches[1];
  114. }
  115. // Content Type
  116. if ( preg_match( "/^Content-Type:\s*(.*)$/", $line, $matches ) )
  117. {
  118. $email['content_type_raw'] = $matches[1];
  119. if ( preg_match( "/^multipart\/alternative;/", $email['content_type_raw'], $matches ) )
  120. {
  121. $email['mixed_content'] = 1;
  122. }
  123. elseif ( preg_match( "/^text\/plain;/", $email['content_type_raw'], $matches ) )
  124. {
  125. $email['content_type'] = 'text/plain';
  126. }
  127. elseif ( preg_match( "/^text\/html;/", $email['content_type_raw'], $matches ) )
  128. {
  129. $email['content_type'] = 'text/html';
  130. }
  131. }
  132. // Boundary
  133. if ( $email['mixed_content'] )
  134. {
  135. if ( preg_match( "/(\s*)?boundary=\"(.*)\"/", $line, $matches ) )
  136. {
  137. $email['boundary'] = $matches[2];
  138. }
  139. }
  140. // Check For Header End
  141. if ( ! trim( $line ) )
  142. {
  143. $headers_complete = 1;
  144. }
  145. }
  146. else
  147. {
  148. // Multipart
  149. if ( $email['mixed_content'] )
  150. {
  151. $boundary_first = 0;
  152. if ( trim( $line ) == '--' .$email['boundary'] )
  153. {
  154. if ( $boundary_active )
  155. {
  156. $boundary_first = 1;
  157. $boundary_count ++;
  158. }
  159. else
  160. {
  161. $boundary_active = 1;
  162. $boundary_first = 1;
  163. $boundary_count ++;
  164. }
  165. }
  166. if ( $boundary_active && ! $boundary_first && $line != '--' .$email['boundary'] .'--' )
  167. {
  168. $email['parts'][ $boundary_count ]['raw_message'] .= $line ."\n";
  169. }
  170. }
  171. else
  172. {
  173. $email['message'] .= $line ."\n";
  174. }
  175. }
  176. }
  177. // Clean Up
  178. if ( $email['mixed_content'] )
  179. {
  180. $plain_text_found = 0;
  181. while( list( $pid, ) = each( $email['parts'] ) )
  182. {
  183. $lines = explode( "\n", $email['parts'][ $pid ]['raw_message'] );
  184. $part_headers_complete = 0;
  185. while( list( , $line ) = each( $lines ) )
  186. {
  187. if ( ! $part_headers_complete )
  188. {
  189. // Content Type
  190. if ( preg_match( "/^Content-Type:\s*(.*)$/", $line, $matches ) )
  191. {
  192. $email['parts'][ $pid ]['content_type_raw'] = $matches[1];
  193. if ( preg_match( "/^text\/plain;/", $email['parts'][ $pid ]['content_type_raw'], $matches ) )
  194. {
  195. $email['parts'][ $pid ]['content_type'] = 'text/plain';
  196. $plain_text_found = 1;
  197. }
  198. elseif ( preg_match( "/^text\/html;/", $email['parts'][ $pid ]['content_type_raw'], $matches ) )
  199. {
  200. $email['parts'][ $pid ]['content_type'] = 'text/html';
  201. }
  202. }
  203. // Check For Header End
  204. if ( ! trim( $line ) )
  205. {
  206. $part_headers_complete = 1;
  207. }
  208. }
  209. else
  210. {
  211. $email['parts'][ $pid ]['message'] .= $line ."\n";
  212. }
  213. }
  214. $email['parts'][ $pid ]['message'] = trim( $email['parts'][ $pid ]['message'] );
  215. if ( $plain_text_found )
  216. {
  217. if ( $email['parts'][ $pid ]['content_type'] == 'text/plain' )
  218. {
  219. $email['content_type'] = 'text/plain';
  220. $email['message'] = $email['parts'][ $pid ]['message'];
  221. }
  222. }
  223. else
  224. {
  225. if ( ! $email['message'] )
  226. {
  227. if ( $email['parts'][ $pid ]['content_type'] == 'text/html' )
  228. {
  229. $email['content_type'] = 'text/html';
  230. $email['message'] = str_replace( "\n", "", $email['parts'][ $pid ]['message'] );
  231. $email['message'] = preg_replace( '/<br(.*?)>/i', "\n", $email['message'] );
  232. $email['message'] = trim( strip_tags( $email['message'] ) );
  233. }
  234. }
  235. }
  236. }
  237. }
  238. else
  239. {
  240. $email['message'] = trim( $email['message'] );
  241. if ( $email['content_type'] == 'text/html' )
  242. {
  243. $email['message'] = str_replace( "\n", "", $email['message'] );
  244. $email['message'] = preg_replace( '/<br(.*?)>/i', "\n", $email['message'] );
  245. $email['message'] = trim( strip_tags( $email['message'] ) );
  246. }
  247. }
  248. // Finally, Sanitize
  249. $email['from'] = $trellis->sanitize_data( imap_utf8( $email['from'] ) );
  250. $email['nickname'] = $trellis->sanitize_data( imap_utf8( $email['nickname'] ) );
  251. $email['to'] = $trellis->sanitize_data( imap_utf8( $email['to'] ) );
  252. $email['subject'] = $trellis->sanitize_data( imap_utf8( $email['subject'] ) );
  253. $email['message'] = $trellis->sanitize_data( utf8_encode( decode_ISO88591( imap_utf8( $email['message'] ) ) ) );
  254. $email['date'] = $trellis->sanitize_data( $email['date'] );
  255. $email['date'] = strtotime( $email['date'] );
  256. if ( ! $email['nickname'] ) $email['nickname'] = $email['from'];
  257. if ( ! $email['from'] || ! $email['to'] || ! $email['subject'] || ! $email['message'] ) continue;
  258. if ( ! $trellis->validate_email( $email['from'] ) ) continue;
  259. #=============================
  260. # Find User
  261. #=============================
  262. $trellis->core->db->construct( array(
  263. 'select' => array( 'm' => array( 'id', 'name' ), 'g' => array( 'g_m_depart_perm' ) ),
  264. 'from' => array( 'm' => 'users' ),
  265. 'join' => array( array( 'from' => array( 'g' => 'groups' ), 'where' => array( 'g' => 'g_id', '=', 'm' => 'ugroup' ) ) ),
  266. 'where' => array( array( 'm' => 'email' ), '=', $email['from'] ),
  267. 'limit' => array( 0, 1 ),
  268. ) );
  269. $trellis->core->db->execute();
  270. if ( $trellis->core->db->get_num_rows() )
  271. {
  272. $m = $trellis->core->db->fetch_row();
  273. }
  274. else
  275. {
  276. if ( ! $d['guest_pipe'] )
  277. {
  278. $replace = array(); // Initialize for Security
  279. if ( $m['id'] )
  280. {
  281. $trellis->send_email( $m['id'], 'ticket_pipe_rejected', $replace, array( 'from_email' => $d['incoming_email'] ) );
  282. }
  283. else
  284. {
  285. $replace['MEM_NAME'] = $email['nickname'];
  286. $trellis->send_guest_email( $email['from'], 'ticket_pipe_rejected', $replace, array( 'from_email' => $d['incoming_email'] ) );
  287. }
  288. $trellis->log( 'security', "Guest Piping Not Allowed: ". $d['name'] );
  289. continue;
  290. }
  291. $trellis->core->db->construct( array(
  292. 'select' => array( 'g_m_depart_perm' ),
  293. 'from' => 'groups',
  294. 'where' => array( 'g_id', '=', 2 ),
  295. 'limit' => array( 0, 1 ),
  296. ) );
  297. $trellis->core->db->execute();
  298. $m = $trellis->core->db->fetch_row();
  299. }
  300. #=============================
  301. # Detect Type
  302. #=============================
  303. $ticket_found = 0;
  304. if ( preg_match_all( '/Ticket ID #([0-9]+)/i', $email['subject'], $matches, PREG_PATTERN_ORDER ) )
  305. {
  306. while( list( , $ptid ) = each( $matches[1] ) )
  307. {
  308. $trellis->core->db->construct( array(
  309. 'select' => 'all',
  310. 'from' => 'tickets',
  311. 'where' => array( array( 'id', '=', intval( $ptid ) ), array( 'email', '=', $email['from'], 'and' ) ),
  312. 'limit' => array( 0, 1 ),
  313. ) );
  314. $trellis->core->db->execute();
  315. if ( $trellis->core->db->get_num_rows() )
  316. {
  317. $ticket_found = 1;
  318. $t = $trellis->core->db->fetch_row();
  319. break;
  320. }
  321. }
  322. }
  323. if ( $ticket_found )
  324. {
  325. if ( $t['status'] == 6 )
  326. {
  327. $trellis->log( 'error', "Reply Rejected Ticket Closed &#039;". $t['subject'] ."&#039;", 1, $t['id'] );
  328. continue;
  329. }
  330. #=============================
  331. # Add Reply
  332. #=============================
  333. $db_array = array(
  334. 'tid' => $t['id'],
  335. 'uid' => $m['id'],
  336. 'uname' => $m['name'],
  337. 'message' => $email['message'],
  338. 'date' => $email['date'],
  339. 'ipadd' => $trellis->input['ip_address'],
  340. );
  341. if ( ! $m['id'] )
  342. {
  343. $db_array['uname'] = $email['nickname'];
  344. $db_array['guest'] = 1;
  345. }
  346. $trellis->core->db->construct( array(
  347. 'insert' => 'replies',
  348. 'set' => $db_array,
  349. ) );
  350. $trellis->core->db->execute();
  351. $reply_id = $trellis->core->db->get_insert_id();
  352. $trellis->log( 'user', "Ticket Reply &#039;". $t['subject'] ."&#039;", 1, $reply_id );
  353. $trellis->log( 'ticket', "Ticket Reply &#039;". $t['subject'] ."&#039;", 1, $t['id'] );
  354. #=============================
  355. # Email Staff
  356. #=============================
  357. $trellis->core->db->construct( array(
  358. 'select' => array( 'm' => array( 'id', 'ugroup', 'email_staff_ticket_reply' ),
  359. 'g' => array( 'g_depart_perm' ),
  360. ),
  361. 'from' => array( 'm' => 'users' ),
  362. 'join' => array( array( 'from' => array( 'g' => 'groups' ), 'where' => array( 'g' => 'g_id', '=', 'm' => 'ugroup' ) ) ),
  363. 'where' => array( array( 'g' => 'g_acp_access' ), '=', 1 ),
  364. ) );
  365. $trellis->core->db->execute();
  366. if ( $trellis->core->db->get_num_rows($staff_sql) )
  367. {
  368. while( $sm = $trellis->core->db->fetch_row($staff_sql) )
  369. {
  370. // Check Departments
  371. if ( is_array( unserialize( $sm['g_depart_perm'] ) ) )
  372. {
  373. $my_departs = "";
  374. $my_departs = unserialize( $sm['g_depart_perm'] );
  375. if ( $my_departs[ $d['id'] ] )
  376. {
  377. if ( $sm['email_staff_ticket_reply'] )
  378. {
  379. $s_email_staff = 1;
  380. }
  381. $do_feeds[ $sm['id'] ] = 1;
  382. }
  383. }
  384. else
  385. {
  386. if ( $sm['email_staff_ticket_reply'] )
  387. {
  388. $s_email_staff = 1;
  389. }
  390. $do_feeds[ $sm['id'] ] = 1;
  391. }
  392. if ( $s_email_staff )
  393. {
  394. $replace = array(); // Initialize for Security
  395. $replace['TICKET_ID'] = $t['id'];
  396. $replace['SUBJECT'] = $t['subject'];
  397. $replace['DEPARTMENT'] = $t['dname'];
  398. $replace['PRIORITY'] = $trellis->get_priority( $t['priority'] );
  399. $replace['SUB_DATE'] = $trellis->a5_date( $t['date'] );
  400. $replace['REPLY'] = $email['message'];
  401. $replace['TICKET_LINK'] = $trellis->cache->data['config']['hd_url'] ."/admin.php?section=manage&act=tickets&code=view&id=". $t['id'];
  402. if ( $m['id'] )
  403. {
  404. $replace['MEMBER'] = $m['name'];
  405. }
  406. else
  407. {
  408. $replace['MEMBER'] = $email['nickname'];
  409. }
  410. $trellis->send_email( $sm['id'], 'staff_reply_ticket', $replace );
  411. }
  412. $s_email_staff = 0; // Reset
  413. }
  414. if ( is_array( $do_feeds ) )
  415. {
  416. require_once TD_SRC .'feed.php';
  417. $feed = new feed();
  418. $feed->trellis =& $trellis;
  419. while( list( $suid, ) = each( $do_feeds ) )
  420. {
  421. $feed->show_feed( 'stickets', $suid, 1 );
  422. }
  423. }
  424. }
  425. #=============================
  426. # Update Ticket
  427. #=============================
  428. if ( $t['status'] == 4 )
  429. {
  430. if ( $m['id'] )
  431. {
  432. $db_array = array( 'last_reply' => $email['date'], 'last_uid' => $m['id'], 'last_uname' => $m['name'], 'replies' => ( $t['replies'] + 1 ), 'status' => 1 );
  433. }
  434. else
  435. {
  436. $db_array = array( 'last_reply' => $email['date'], 'last_uid' => $m['id'], 'last_uname' => $email['nickname'], 'replies' => ( $t['replies'] + 1 ), 'status' => 1 );
  437. }
  438. }
  439. else
  440. {
  441. if ( $m['id'] )
  442. {
  443. $db_array = array( 'last_reply' => $email['date'], 'last_uid' => $m['id'], 'last_uname' => $m['name'], 'replies' => ( $t['replies'] + 1 ) );
  444. }
  445. else
  446. {
  447. $db_array = array( 'last_reply' => $email['date'], 'last_uid' => $m['id'], 'last_uname' => $email['nickname'], 'replies' => ( $t['replies'] + 1 ) );
  448. }
  449. }
  450. $trellis->core->db->construct( array(
  451. 'update' => 'tickets',
  452. 'set' => $db_array,
  453. 'where' => array( 'id', '=', $t['id'] ),
  454. 'limit' => array( 1 ),
  455. ) );
  456. $trellis->core->db->next_shutdown();
  457. $trellis->core->db->execute();
  458. }
  459. else
  460. {
  461. #=============================
  462. # Department Security
  463. #=============================
  464. $d_allow = unserialize( $m['g_m_depart_perm'] );
  465. if ( ! $d_allow[ $d['id'] ] )
  466. {
  467. $replace = array(); // Initialize for Security
  468. if ( $m['id'] )
  469. {
  470. $trellis->send_email( $m['id'], 'ticket_pipe_rejected', $replace, array( 'from_email' => $d['incoming_email'] ) );
  471. }
  472. else
  473. {
  474. $replace['MEM_NAME'] = $email['nickname'];
  475. $trellis->send_guest_email( $email['from'], 'ticket_pipe_rejected', $replace, array( 'from_email' => $d['incoming_email'] ) );
  476. }
  477. $trellis->log( 'security', "New Ticket to &#039;". $d['name'] ."&#039; Denied", 1, $d['id'] );
  478. continue;
  479. }
  480. #=============================
  481. # Create Ticket
  482. #=============================
  483. $db_array = array(
  484. 'did' => $d['id'],
  485. 'dname' => $d['name'],
  486. 'uid' => $m['id'],
  487. 'uname' => $m['name'],
  488. 'email' => $email['from'],
  489. 'subject' => $email['subject'],
  490. 'priority' => 2,
  491. 'message' => $email['message'],
  492. 'date' => $email['date'],
  493. 'last_reply' => $email['date'],
  494. 'last_uid' => $m['id'],
  495. 'last_uname' => $m['name'],
  496. 'ipadd' => $trellis->input['ip_address'],
  497. 'status' => 1,
  498. );
  499. if ( ! $m['id'] )
  500. {
  501. $db_array['tkey'] = substr( md5( 'tk' . uniqid( rand(), true ) . time() ), 0, 11 );
  502. $db_array['uname'] = $email['nickname'];
  503. $db_array['last_uname'] = $email['nickname'];
  504. $db_array['guest'] = 1;
  505. $db_array['guest_email'] = 1;
  506. }
  507. $trellis->core->db->construct( array(
  508. 'insert' => 'tickets',
  509. 'set' => $db_array,
  510. ) );
  511. $trellis->core->db->execute();
  512. $ticket_id = $trellis->core->db->get_insert_id();
  513. $trellis->log( 'user', "Ticket Created &#039;". $email['subject'] ."&#039;", 1, $ticket_id );
  514. $trellis->log( 'ticket', "Ticket Created &#039;". $email['subject'] ."&#039;", 1, $ticket_id );
  515. #=============================
  516. # Update User
  517. #=============================
  518. if ( $m['id'] )
  519. {
  520. $trellis->core->db->next_no_quotes('set');
  521. $trellis->core->db->construct( array(
  522. 'update' => 'users',
  523. 'set' => array( 'open_tickets' => 'open_tickets+1', 'tickets' => 'tickets+1' ),
  524. 'where' => array( 'id', '=', $m['id'] ),
  525. 'limit' => array( 1 ),
  526. ) );
  527. $trellis->core->db->next_shutdown();
  528. $trellis->core->db->execute();
  529. }
  530. #=============================
  531. # Update Department
  532. #=============================
  533. $trellis->core->db->next_no_quotes('set');
  534. $trellis->core->db->construct( array(
  535. 'update' => 'departments',
  536. 'set' => array( 'tickets' => 'tickets+1' ),
  537. 'where' => array( 'id', '=', $d['id'] ),
  538. 'limit' => array( 1 ),
  539. ) );
  540. $trellis->core->db->next_shutdown();
  541. $trellis->core->db->execute();
  542. #=============================
  543. # Send Email
  544. #=============================
  545. if ( $m['id'] )
  546. {
  547. $replace = array(); // Initialize for Security
  548. $replace['TICKET_ID'] = $ticket_id;
  549. $replace['SUBJECT'] = $email['subject'];
  550. $replace['DEPARTMENT'] = $d['name'];
  551. $replace['PRIORITY'] = $trellis->get_priority( 2 );
  552. $replace['SUB_DATE'] = $trellis->a5_date( $email['date'] );
  553. $replace['TICKET_LINK'] = $trellis->cache->data['config']['hd_url'] ."/index.php?act=tickets&code=view&id=". $ticket_id;
  554. $trellis->send_email( $m['id'], 'new_ticket', $replace, array( 'from_email' => $d['incoming_email'] ) );
  555. }
  556. #=============================
  557. # Send Guest Email
  558. #=============================
  559. if ( ! $m['id'] )
  560. {
  561. $replace = array(); // Initialize for Security
  562. $replace['TICKET_ID'] = $ticket_id;
  563. $replace['SUBJECT'] = $email['subject'];
  564. $replace['DEPARTMENT'] = $d['name'];
  565. $replace['PRIORITY'] = $trellis->get_priority( 2 );
  566. $replace['SUB_DATE'] = $trellis->a5_date( $email['date'] );
  567. $replace['TICKET_LINK'] = $trellis->cache->data['config']['hd_url'] ."/index.php?act=tickets&code=view&id=". $ticket_id;
  568. $replace['MEM_NAME'] = $email['nickname'];
  569. $replace['TICKET_KEY'] = $db_array['tkey'];
  570. $trellis->send_guest_email( $email['from'], 'new_guest_ticket', $replace, array( 'from_email' => $d['incoming_email'] ) );
  571. }
  572. #=============================
  573. # Email Staff
  574. #=============================
  575. $trellis->core->db->construct( array(
  576. 'select' => array( 'm' => array( 'id', 'ugroup', 'email_staff_new_ticket' ),
  577. 'g' => array( 'g_depart_perm' ),
  578. ),
  579. 'from' => array( 'm' => 'users' ),
  580. 'join' => array( array( 'from' => array( 'g' => 'groups' ), 'where' => array( 'g' => 'g_id', '=', 'm' => 'ugroup' ) ) ),
  581. 'where' => array( array( 'g' => 'g_acp_access' ), '=', 1 ),
  582. ) );
  583. $trellis->core->db->execute();
  584. if ( $trellis->core->db->get_num_rows($staff_sql) )
  585. {
  586. while( $sm = $trellis->core->db->fetch_row($staff_sql) )
  587. {
  588. // Check Departments
  589. if ( is_array( unserialize( $sm['g_depart_perm'] ) ) )
  590. {
  591. $my_departs = "";
  592. $my_departs = unserialize( $sm['g_depart_perm'] );
  593. if ( $my_departs[ $d['id'] ] )
  594. {
  595. if ( $sm['email_staff_new_ticket'] )
  596. {
  597. $s_email_staff = 1;
  598. }
  599. $do_feeds[ $sm['id'] ] = 1;
  600. }
  601. }
  602. else
  603. {
  604. if ( $sm['email_staff_new_ticket'] )
  605. {
  606. $s_email_staff = 1;
  607. }
  608. $do_feeds[ $sm['id'] ] = 1;
  609. }
  610. if ( $s_email_staff )
  611. {
  612. $replace = array(); // Initialize for Security
  613. $replace['TICKET_ID'] = $ticket_id;
  614. $replace['SUBJECT'] = $email['subject'];
  615. $replace['DEPARTMENT'] = $d['name'];
  616. $replace['PRIORITY'] = $trellis->get_priority( 2 );
  617. $replace['SUB_DATE'] = $trellis->a5_date( $email['date'] );
  618. $replace['MESSAGE'] = $email['message'];
  619. $replace['TICKET_LINK'] = $trellis->cache->data['config']['hd_url'] ."/admin.php?section=manage&act=tickets&code=view&id=". $ticket_id;
  620. if ( $m['id'] )
  621. {
  622. $replace['MEMBER'] = $m['name'];
  623. $trellis->send_email( $sm['id'], 'staff_new_ticket', $replace );
  624. }
  625. else
  626. {
  627. $replace['MEMBER'] = $email['nickname'];
  628. $trellis->send_email( $sm['id'], 'staff_new_guest_ticket', $replace );
  629. }
  630. }
  631. $s_email_staff = 0; // Reset
  632. }
  633. }
  634. if ( is_array( $do_feeds ) )
  635. {
  636. require_once TD_SRC .'feed.php';
  637. $feed = new feed();
  638. $feed->trellis =& $trellis;
  639. while( list( $suid, ) = each( $do_feeds ) )
  640. {
  641. $feed->show_feed( 'stickets', $suid, 1 );
  642. }
  643. }
  644. }
  645. }
  646. imap_expunge( $mbox );
  647. imap_close( $mbox );
  648. }
  649. }
  650. }
  651. #=============================
  652. # Update Stats
  653. #=============================
  654. $trellis->r_ticket_stats(1);
  655. #=============================
  656. # Bye Bye
  657. #=============================
  658. $trellis->core->shut_down_q();
  659. $trellis->shut_down();
  660. $trellis->core->shut_down();
  661. #=============================
  662. # Decode ISO88591
  663. # Courtesy of PHP.net from
  664. # aperez at informatica dot 24ruedas dot com
  665. #=============================
  666. function decode_ISO88591( $string )
  667. {
  668. $string = str_replace( "=\r\n", "", $string );
  669. $string = str_replace("=?iso-8859-1?q?","",$string);
  670. $string = str_replace("=?iso-8859-1?Q?","",$string);
  671. $string = str_replace("?=","",$string);
  672. $charHex = array( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" );
  673. for( $z=0; $z < sizeof($charHex); $z++ )
  674. {
  675. for( $i=0; $i< sizeof($charHex); $i++ )
  676. {
  677. $string = str_replace( ("=" .( $charHex[$z].$charHex[$i] ) ), chr( hexdec( $charHex[$z].$charHex[$i]) ), $string );
  678. }
  679. }
  680. return($string);
  681. }
  682. ?>