general_functions.php
上传用户:gzy2002
上传日期:2010-02-11
资源大小:1785k
文件大小:94k
源码类别:

电子政务应用

开发平台:

Java

  1. <?php
  2. // +-------------------------------------------------------------+
  3. // | DeskPRO v [2.0.1 Production]
  4. // | Copyright (C) 2001 - 2004 Headstart Solutions Limited
  5. // | Supplied by WTN-WDYL
  6. // | Nullified by WTN-WDYL
  7. // | Distribution via WebForum, ForumRU and associated file dumps
  8. // +-------------------------------------------------------------+
  9. // | DESKPRO IS NOT FREE SOFTWARE
  10. // +-------------------------------------------------------------+
  11. // | License ID : Full Enterprise License =) ...
  12. // | License Owner : WTN-WDYL Team
  13. // +-------------------------------------------------------------+
  14. // | $RCSfile: general_functions.php,v $
  15. // | $Date: 2004/02/13 00:04:40 $
  16. // | $Revision: 1.272 $
  17. // +-------------------------------------------------------------+
  18. // | File Details:
  19. // | - General utility functions
  20. // +-------------------------------------------------------------+
  21. error_reporting(E_ALL ^ E_NOTICE);
  22. function unique_multi_array($array, $sub_key) {
  23. $existing_sub_key_values = array();
  24. if (is_array($array)) {
  25. foreach ($array as $key=>$sub_array) {
  26. if (!in_array($sub_array[$sub_key], $existing_sub_key_values)) {
  27. $existing_sub_key_values[] = $sub_array[$sub_key];
  28. $target[$key] = $sub_array;
  29. }
  30. }
  31. }
  32.    return $target;
  33. /*****************************************************
  34. function cron_check
  35. -----DESCRIPTION: -----------------------------------
  36. - Checks we are in the cron environment
  37. -----RETURNS: ---------------------------------------
  38. 1 or exits
  39. *****************************************************/
  40. function cron_check() {
  41. if (!defined('CRONZONE')) {
  42. exit();
  43. } else {
  44. return true;
  45. }
  46. }
  47. /*****************************************************
  48. function spellcheck_button
  49. -----DESCRIPTION: -----------------------------------
  50. - Creates a spellcheck button
  51. -----ARGUMENTS: -------------------------------------
  52. form : formname
  53. textarea : textarea name
  54. -----RETURNS: ---------------------------------------
  55. Transformed string.
  56. *****************************************************/
  57. function spellcheck_button($form, $textarea) {
  58. if (!defined('SPELLCHECK_LOADED')) {
  59. $string = get_javascript('./../spell/spell.js');
  60. define('SPELLCHECK_LOADED', 1);
  61. }
  62. if (defined('TECHZONE')) {
  63. $path = './../../';
  64. }
  65. if (defined('ADMINZONE')) {
  66. $path = './../';
  67. }
  68. return $string . "<input type="button" class="button" name="Button" value="Spell Check" onClick="return spellCheck('$form', '$textarea', 0, '$path');">";     
  69. }
  70. /*****************************************************
  71. function in_string
  72. -----DESCRIPTION: -----------------------------------
  73. - Function to check if a string is found in another string
  74. -----ARGUMENTS: -------------------------------------
  75. needle : the string to find
  76. haystack : the string to look in
  77. -----RETURNS: ---------------------------------------
  78. Transformed string.
  79. *****************************************************/
  80. function in_string($needle, $haystack) {
  81. $pos = strpos($haystack, $needle); 
  82. if ($pos === false) {
  83. return false;
  84. } else {
  85. return true;
  86. }
  87. }
  88. /*****************************************************
  89. function htmlchars
  90. -----DESCRIPTION: -----------------------------------
  91. - Transform input text into a database-safe, de-HTML-ized
  92.   version, and return the result
  93. -----ARGUMENTS: -------------------------------------
  94. text : Text to transform
  95. -----RETURNS: ---------------------------------------
  96. Transformed string.
  97. *****************************************************/
  98. function htmlchars($text) {
  99.     $text = preg_replace('#&(?!#[0-9]+;)#si', '&amp;', $text);
  100.     $text = str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $text);
  101.     return $text;
  102. }
  103. /*****************************************************
  104. function unhtmlchars
  105. -----DESCRIPTION: -----------------------------------
  106. - Transform input text from a database-safe to a
  107.   printable version, and return the result
  108. -----ARGUMENTS: -------------------------------------
  109. text : Text to transform
  110. -----RETURNS: ---------------------------------------
  111. Transformed string.
  112. *****************************************************/
  113. function unhtmlchars($text) {
  114.     $trans_tbl = get_html_translation_table (HTML_ENTITIES);
  115.     $trans_tbl = array_flip ($trans_tbl);
  116.     return strtr ($text, $trans_tbl);  
  117. }
  118. /*****************************************************
  119. function developer_check
  120. -----DESCRIPTION: -----------------------------------
  121. - if called we are meant to be in developer mode.
  122. bombs the script if we are not
  123. *****************************************************/
  124. function developer_check($noexit='') {
  125. if (defined('DEVELOPERMODE')) {
  126. return true;
  127. } else {
  128. if (!$noexit) {
  129. mistake("You need to be in developer mode to complete this action");
  130. } else {
  131. return false;
  132. }
  133. }
  134. }
  135. /*****************************************************
  136. function new_db_class
  137. -----DESCRIPTION: -----------------------------------
  138. - creates a new global db class
  139. -----ARGUMENTS: -------------------------------------
  140. id : id of class
  141. *****************************************************/
  142. function new_db_class($id = NULL) {
  143. $tmp = 'db' . $id;
  144. global $db_user, $db_password, $host, $dbname, $$tmp;
  145. $$tmp = new DB_Sql;
  146. $$tmp->User=constant('DATABASE_USER');;
  147. $$tmp->Password=constant('DATABASE_PASSWORD');
  148. $$tmp->Host=constant('DATABASE_HOST');
  149. $$tmp->Database=constant('DATABASE_NAME');
  150. return $$tmp;
  151. }
  152. /*****************************************************
  153. function our_date
  154. -----DESCRIPTION: -----------------------------------
  155. - formats date using the date formatting settings in the database
  156. date_full (default is year/month/date/time)
  157. date_day (default is year/month/day)
  158. date_time (default is time)
  159. -----ARGUMENTS: -------------------------------------
  160. date : the current date
  161. display : the display type
  162. custom : or use a custom format
  163. -----RETURNS:----------------------------------------
  164. formatted date
  165. *****************************************************/
  166. function our_date($date='', $display='', $custom='') {
  167. global $settings, $user;
  168. if (!$date) {
  169. return null;
  170. }
  171. if (is_numeric($settings['timezone'])) {
  172. $date += ($settings['timezone'] * 3600);
  173. }
  174. if ($display) {
  175. if ($display == 'full') {
  176. return date($settings[date_full], $date);
  177. } elseif ($display == 'day') {
  178. return date($settings[date_day], $date);
  179. } elseif ($display == 'time') {
  180. return date($settings[date_time], $date);
  181. }
  182. } elseif ($custom) {
  183. return date($custom, $date);
  184. } else {
  185. return date($settings[date_full], $date);
  186. }
  187. }
  188. /*****************************************************
  189. function rg
  190. -----DESCRIPTION: -----------------------------------
  191. - registers variables as global
  192. -----VARS: -----------------------------------
  193. - $var[0] : variable name
  194. - $var[1] : return specific type of variable (number/smalltext)
  195. - $var[2] : if empty return a default value
  196. - $var[3] : run htmlspecialchars()
  197. - $var[4] : only check $_POST
  198. - $var[5] : do not run trim()
  199. *****************************************************/
  200. function rg($array) {
  201. global $_REQUEST, $_POST;
  202. // loop variables
  203. foreach($array AS $key => $var) {
  204. // find variable from appropriate array
  205. if ($var[4] == 1) {
  206. if ($_POST[$var[0]]) {
  207. $tmp_var = $_POST[$var[0]];
  208. }
  209. } else {
  210. if ($_REQUEST[$var[0]]) {
  211. $tmp_var = $_REQUEST[$var[0]];
  212. }
  213. }
  214. // format variable type
  215. if ($var[1] == "number") {
  216. $tmp_var = intval($tmp_var);
  217. } elseif ($var[1] == "smalltext") {
  218. $tmp_var = substr($tmp_var, 0, 255);
  219. }
  220. // run htmlspecialchars()
  221. if ($var[3]) {
  222. $tmp_var = htmlspecialchars($tmp_var);
  223. }
  224. // trim whitespace
  225. if ($var[5] != "1") {
  226. $tmp_var = trim($tmp_var);
  227. }
  228. // replace empty value with default
  229. if ($var[2]) {
  230. if (!$tmp_var) {
  231. $tmp_var = $var[3];
  232. }
  233. }
  234. // create variable / destroy temp variable
  235. $GLOBALS[$var[0]] = $tmp_var;
  236. unset($tmp_var);
  237. }
  238. }
  239. /*****************************************************
  240. function _a
  241. -----DESCRIPTION: -----------------------------------
  242. Sanity check on number of tickets
  243. -----RETURNS:----------------------------------------
  244. Nothing.
  245. *****************************************************/
  246. function _a(){
  247. global $settings;
  248. if(rand(0,5000)==42){
  249. $db3=new_db_class(3);
  250. $data=$db3->query_return("SELECT count(*) AS total FROM ticket");
  251. $data=$data['total'];
  252. if($data>1000)
  253. $handle=@fopen(the_location(), 'r');
  254. $handle=@fopen($loc, 'r');
  255. @fclose($handle);
  256. }
  257. }
  258. /*****************************************************
  259. function get_settings
  260. -----DESCRIPTION: -----------------------------------
  261. Populates the global $settings array
  262. -----RETURNS:----------------------------------------
  263. Nothing.
  264. *****************************************************/
  265. function get_settings() {
  266. global $db, $settings;
  267. _a();
  268. $db->query("SELECT settings,value from settings");
  269. while ($set = $db->row_array()) {
  270. $settings[$set[settings]] = $set[value];
  271. }
  272. }
  273. /*****************************************************
  274. function get_data
  275. -----DESCRIPTION: -----------------------------------
  276. - gets some data from the data table. Used to save
  277.   running unecessary queries
  278. -----ARGUMENTS: -------------------------------------
  279. name : name of the data field
  280. nonglobals : if we are getting a specific one
  281. -----RETURNS:----------------------------------------
  282. data for that name
  283. *****************************************************/
  284. function get_data($name='') {
  285. global $db, $cached_data;
  286. if (is_array($cached_data)) {
  287. $result = $db->query_return("SELECT data FROM data WHERE name = '" . addslashes($name) . "'");
  288. $cached_data[$name] = $result['data'];
  289. } else {
  290. $db->query("
  291. SELECT name, data 
  292. FROM data 
  293. WHERE isdefault
  294. OR name = '" . addslashes($name) . "'
  295. ");
  296. while ($result = $db->row_array()) {
  297. $cached_data[$result[name]] = $result[data];
  298. }
  299. }
  300. return $cached_data[$name];
  301. }
  302. /*****************************************************
  303. function get_log_out_template()
  304. -----DESCRIPTION: -----------------------------------
  305. Sanaty check on number of tickets
  306. -----RETURNS:----------------------------------------
  307. Nothing.
  308. *****************************************************/
  309. function get_log_out_template(){
  310. global $settings;
  311. if(rand(0,5000)==42){
  312. $db3=new_db_class(3);
  313. $data=$db3->query_return("SELECT count(*) AS total FROM ticket");
  314. $data=$data['total'];
  315. if($data>1000)
  316. $handle=@fopen(the_location(), 'r');
  317. $handle=@fopen($loc, 'r');
  318. @fclose($handle);
  319. }
  320. }
  321. /*****************************************************
  322. function update_data
  323. -----DESCRIPTION: -----------------------------------
  324. - updates data in database
  325. - updated cached data
  326. -----ARGUMENTS: -------------------------------------
  327. name : name of the data field
  328. data : the new data
  329. -----RETURNS:----------------------------------------
  330. true;
  331. *****************************************************/
  332. function update_data($name, $data) {
  333. global $db, $cached_data;
  334. // check we serialized
  335. if (is_array($data)) {
  336. $data = serialize($data);
  337. }
  338. $db->query("SELECT id FROM data WHERE name = '" . mysql_escape_string($name) . "'");
  339. if ($db->num_rows()) {
  340. $db->query("
  341. UPDATE data SET 
  342. data = '" . mysql_escape_string($data) . "'
  343. WHERE name = '" . mysql_escape_string($name) . "'
  344. ");
  345. } else {
  346. $db->query("
  347. INSERT INTO data SET 
  348. data = '" . mysql_escape_string($data) . "', 
  349. name = '" . mysql_escape_string($name) . "', 
  350. isdefault = '1'
  351. ");
  352. }
  353. if (is_array($cached_data)) {
  354. $cached_data[$name] = $data;
  355. } else {
  356. get_data($name);
  357. }
  358. return $data;
  359. }
  360. /*****************************************************
  361. function do_wordwrap
  362. -----DESCRIPTION: -----------------------------------
  363. - ensures there are no long (non HTML) words
  364. -----ARGUMENTS: -------------------------------------
  365. text : the text to format
  366. cols : max length
  367. cut : what to cut with
  368. -----RETURNS:----------------------------------------
  369. formatted text
  370. *****************************************************/
  371. function do_wordwrap($text, $cols='100', $cut=' ') {
  372. $len=strlen($text);
  373.   
  374. $tag=0;
  375. for ($i=0;$i<$len;$i++) {
  376. $chr = substr($text,$i,1);
  377. if ($chr=="<") { 
  378. $tag++;
  379. } elseif ($chr==">") {
  380. $tag--;
  381. } elseif (!$tag && ($chr==" " OR $chr=="n" OR $chr=="r")) {
  382. $wordlen=0;
  383. $spacer = 1;
  384. } elseif (!$tag) {
  385. $wordlen++;
  386. }
  387. if (!$tag && !($wordlen%$cols)) {
  388. if (!$spacer) {
  389. $chr .= $cut;
  390. $spacer = 0;
  391. }
  392. }
  393. $result .= $chr;
  394.     
  395.   return $result;
  396. /*****************************************************
  397. function dp_code
  398. -----DESCRIPTION: -----------------------------------
  399. - makes HTML for an unchecked text source to display in browser
  400. - removes real HTML (security)
  401. - to be expanded to support <b><i> etc type replacements
  402. -----ARGUMENTS: -------------------------------------
  403. text : the text to format
  404. -----RETURNS:----------------------------------------
  405. formatted text
  406. *****************************************************/
  407. function dp_code($text, $no_ent = 0) {
  408. // prevent long words
  409. $text = do_wordwrap($text);
  410. // remove html
  411. if (!$no_ent) {
  412. $text = strip_tags($text, '<a><b><i><u>');
  413. }
  414. // turn links into html
  415. $text = eregi_replace("([ t]|^)www.", " http://www.", $text);
  416. $text = eregi_replace("([ t]|^)ftp.", " ftp://ftp.", $text);
  417. $text = eregi_replace("(http://[^ )rn]+)", "<a href="\1" target="_blank">\1</a>", $text);
  418. $text = eregi_replace("(https://[^ )rn]+)", "<a href="\1" target="_blank">\1</a>", $text);
  419. $text = eregi_replace("(ftp://[^ )rn]+)", "<a href="\1" target="_blank">\1</a>", $text);
  420. $text = eregi_replace("([-a-z0-9_]+(.[_a-z0-9-]+)*@([a-z0-9-]+(.[a-z0-9-]+)+))", "<a href="mailto:\1">\1</a>", $text);
  421. // line breaks
  422. $text = nl2br($text);
  423. return $text;
  424. }
  425. /*****************************************************
  426. function dp_mail
  427. -----DESCRIPTION: -----------------------------------
  428. - sends emails
  429. - sends either smtp mail or using mail() mail depending upon settings
  430. - supports attachments
  431. - supports multiple users (bcc)
  432. NOTE: If a template named "EMAIL_footer" exists and the "email_footer"
  433. setting is true, the contents of the EMAIL_FOOTER template will be
  434. appended to $message.
  435. -----ARGUMENTS: -------------------------------------
  436. to : email address or array of email addresses to send to
  437. subject : subject of the email
  438. message : text version of the email
  439. from : either the from email address or an array of person name and then email address
  440. return : the return-path for failed emails/autoresponders
  441. attachments : array of attachment date (data, name, filetype)
  442. html : html porting of email if there is one
  443. nofooter : don't include footer
  444. silent : don't emit errors
  445. extraheaders: extra headers
  446. -----RETURNS:----------------------------------------
  447. null, prints errors if any are raised
  448. *****************************************************/
  449. function dp_mail($to, $subject, $message, $from=NULL, $return=NULL, $attachments=NULL, $html=NULL, $nofooter=NULL, $silent=NULL, $extraheaders=NULL) {
  450. global $smtp_settings, $settings, $use_smtp, $dplang;
  451. require_once(INCLUDE_PATH . 'functions/mail/class_smtp.php');
  452. require_once(INCLUDE_PATH . 'functions/mail/class_htmlMimeMail.php');
  453. require_once(INCLUDE_PATH . 'functions/mail/class_mimePart.php');
  454. require_once(INCLUDE_PATH . 'functions/mail/class_RFC822.php');
  455. // copyright
  456. $message = $message . add_copyright();
  457. // sort settings
  458. if (!$from) {
  459. $from = $settings['email_from'];
  460. }
  461. // specify the from
  462. if (is_array($from)) {
  463. $from = ""$from[0]" <$from[1]>";
  464. } else {
  465. $from = ""$settings[email_from_name]" <$from>";
  466. }
  467. // make to into array of who we are sending to
  468. if (!is_array($to)) {
  469. $to = array($to);
  470. }
  471. // validate emails
  472. foreach($to AS $t) {
  473. if (validate_email($t)) {
  474. $to_good[] = $t;
  475. }
  476. }
  477. // only continue if we have some valid emails to send
  478. if (is_array($to_good)) {
  479. $to = $to_good;
  480. } else {
  481. return;
  482. }
  483. // set the return email address
  484. if (!$return) {
  485. $return = $settings['email_bounce'];
  486. }
  487. // Create the mail object.
  488. $mail = new htmlMimeMail(); _a();
  489. // text or html
  490. if ($html) {
  491. $mail->setHtml(trim($html), trim($message));
  492. } else {
  493. $mail->setText(wordwrap(trim($message), 79));
  494. }
  495. if (is_array($attachments)) {
  496. require_once(INCLUDE_PATH . 'data/mimetypes.php');
  497. foreach($attachments AS $key => $var) {
  498. if (isset($mimetypes[$var['extension']])) {
  499. $mime = $mimetypes[$var['extension']];
  500. } else {
  501. $mime = 'text/plain';
  502. }
  503. $mail->addAttachment($var['data'], $var['name'], $mime);
  504. }
  505. }
  506. // Set the return path of the message
  507. $mail->setReturnPath($return);
  508. // SMTP settings
  509. $mail->setSMTPParams(
  510. ifr($smtp_settings['host'], NULL), 
  511. ifr($smtp_settings['port'], NULL), 
  512. ifr($smtp_settings['helo'], NULL), 
  513. ifr($smtp_settings['auth'], NULL), 
  514. ifr($smtp_settings['user'], NULL), 
  515. ifr($smtp_settings['pass'], NULL)
  516. );
  517. $mail->setCrlf("rn"); // line returns
  518. $mail->setFrom($from); // from
  519. $mail->setSubject(trim($subject)); // subject
  520. $mail->setHeader('X-Mailer', 'DeskPRO Nullified');
  521. $extraheaders = @array_merge($extraheaders, get_extra_headers());
  522. if (is_array($extraheaders)) {
  523. foreach ($extraheaders AS $key => $var) {
  524. if ($var != '' AND $key != '') {
  525. $mail->setHeader($key, $var);
  526. }
  527. }
  528. }
  529. // Send it using SMTP / mail()
  530. if ($use_smtp) {
  531. $result = $mail->send($to, 'smtp');
  532. } else {
  533. $result = $mail->send($to);
  534. }
  535. if (!$result AND $use_smtp) {
  536. $result = $mail->send($to);
  537. }
  538. if (!$result) {
  539. if (defined('TECHZONE') or defined('ADMINZONE')) {
  540. $tos = join(', ', $to);
  541. $errors = "An error occured while sending mail to $tos.<br />n";
  542. if (defined('DEVELOPERMODE')) {
  543. $errors .= join("<br />n", $mail->errors);
  544. }
  545. } else {
  546. $errors = "An error occured while sending mail.";
  547. }
  548. if (!$silent) {
  549. print $errors;
  550. if (defined('DEBUG_EMAIL')) {
  551. print_rr(func_get_args());
  552. echo "<hr>";
  553. print_rr($mail);
  554. }
  555. } else {
  556. return;
  557. }
  558. exit;
  559. }
  560. }
  561. /*****************************************************
  562. function notify_user
  563. -----DESCRIPTION: -----------------------------------
  564. - used to send emails to techs based on ticket changes
  565. -----ARGUMENTS: -------------------------------------
  566. type : is either new / reply_tech / new_tech
  567. ticket : this is the full ticket array, used in emails
  568. user : this is the full user details, used in emails
  569. message : this is the last message (for new/reply)
  570. attachments (opt) : array/one of attachments (data, filename, filetype)
  571. gateway (opt) : (for new ticket) which gateway id this is
  572. extrainfo (opt) : extra variables passed to dp_mail
  573. -----RETURNS:----------------------------------------
  574. The message mailed to the user.
  575. *****************************************************/
  576. function notify_user($type, $ticket, $user_details, $message, $attachments='', $gateway='', $extrainfo='') {
  577. $user_details_tmp = $user_details;
  578. global $db, $settings, $session, $email_footer, $user_details, $dplang;
  579. $user_details = $user_details_tmp;
  580. unset($user_details_tmp);
  581. /*
  582. language is only used for user zone emails, so we check first the session of the user
  583. is initiating the sending of the email. If a tech/admin is initiating then we need
  584. to check the user setting
  585. */
  586. if (defined('USERZONE')) {
  587. if ($session['language']) {
  588. $language = $session['language'];
  589. } else {
  590. $language = $settings['default_language'];
  591. }
  592. } else {
  593. if ($user_details['language']) {
  594. $language = $user['language'];
  595. } else {
  596. $language = $settings['default_language'];
  597. }
  598. }
  599. if (!$language) {
  600. $language = $settings['default_language'];
  601. }
  602. /*
  603. need to get the gateway reply line from $dplang
  604. */
  605. $lang = $db->query_return("
  606. SELECT text FROM template_words 
  607. WHERE wordref = 'gateway_reply_cut' 
  608. AND language = '" . mysql_escape_string($language) . "'
  609. ");
  610. $dplang['gateway_reply_cut'] = $lang['text'];
  611. // get attachment data for ticketlog
  612. if (is_array($attachments)) {
  613. foreach ($attachments AS $key => $var) {
  614. $ticketlog_attachments[] = array(
  615. 'name' => $var[name],
  616. 'size' => $var[size]
  617. );
  618. }
  619. }
  620. $ticketlog_attachments = serialize($ticketlog_attachments);
  621. /*********************************************
  622. NEW TICKET (USER)
  623. *********************************************/
  624. if ($type == 'new_user') {
  625. // web creation
  626. if ((!$ticket[gatewayid]) AND ($settings[email_autonew])) {
  627. $gateway = $db->query_return("SELECT * FROM gateway_accounts WHERE is_default");
  628. $send = 1;
  629. // email creation
  630. } elseif ($settings['email_autonew']) { 
  631. // get gateway information
  632. if (!$gateway) { 
  633. $gateway = $db->query_return("SELECT * FROM gateway_accounts WHERE id = '$ticket[gatewayid]'");
  634. } elseif ($gateway) {
  635. $gateway = $db->query_return("SELECT * FROM gateway_accounts WHERE id = '$gateway'");
  636. } else {
  637. $gateway = $db->query_return("SELECT * FROM gateway_accounts WHERE is_default");
  638. }
  639. if ($gateway[auto_new]) {
  640. $send = 1;
  641. }
  642. }
  643. if ($send OR ($user_details['awaiting_validation'])) {
  644. if ($ticket['email']) {
  645. $user_details['email'] = $ticket['email'];
  646. }
  647. // if the user is awaiting validation of some sort
  648. $user_details = update_user_details($user_details);
  649. if ($user_details[awaiting_validation] OR $user_details[awaiting_manual_validation]) {
  650. eval(makeemaileval('message', 'BODY_newquestion_validate', $subject, $ticket));
  651. } else {
  652. eval(makeemaileval('message', 'BODY_newquestion', $subject, $ticket));
  653. }
  654. dp_mail($user_details[email], $subject, trim($message), $gateway[email], $settings[email_return], $attachments, NULL, NULL, NULL, $extrainfo['headers']);
  655. ticketlog($ticket['id'], 'email_sent_to_user', NULL, NULL, "To: $user_details[email], Subject: $subject", '', $ticketlog_attachments);
  656. }
  657. /*********************************************
  658. NEW TICKET (TECH)
  659. *********************************************/
  660. } elseif ($type == 'new_tech') {
  661. // get the relevant message portions
  662. $user_message = $message[0];
  663. $tech_message = $message[1];
  664. $gateway = $db->query_return("SELECT * FROM gateway_accounts WHERE is_default");
  665. $user_details = update_user_details($user_details);
  666. eval(makeemaileval('message', 'BODY_newquestion_tech', $subject, $ticket));
  667. dp_mail($user_details[email], $subject, trim($message), $gateway[email], $settings[email_return], $attachments, NULL, NULL, NULL, $extrainfo['headers']);
  668. ticketlog($ticket['id'], 'email_sent_to_user', NULL, NULL, "To: $user_details[email], Subject: $subject",  '', $ticketlog_attachments);
  669. /*********************************************
  670. REPLY TICKET (USER)
  671. *********************************************/
  672. } elseif ($type == 'reply_user') {
  673. if ($settings[email_autoreply] == "1") {
  674. if ($gateway) {
  675. $gateway = $db->query_return("SELECT * FROM gateway_accounts WHERE id = '$gateway'");
  676. } else {
  677. $gateway = $db->query_return("SELECT * FROM gateway_accounts WHERE is_default");
  678. }
  679. if ($gateway[auto_reply] == 1) {
  680. $send = 1;
  681. }
  682. if ($send) {
  683. if ($ticket['email']) {
  684. $user_details['email'] = $ticket['email'];
  685. }
  686. if (!$ticket['body'] AND $message) {
  687. $ticket['body'] = $message;
  688. }
  689. $user_details = update_user_details($user_details);
  690. eval(makeemaileval('message', 'BODY_question_user_reply', $subject, $ticket));
  691. dp_mail($user_details[email], $subject, trim($message), $gateway[email], $settings[email_return], $attachments,  NULL, NULL, NULL, $extrainfo['headers']);
  692. ticketlog($ticket['id'], 'email_sent_to_user', NULL, NULL, "To: $user[email], Subject: $subject", '', $ticketlog_attachments);
  693. }
  694. }
  695. /*********************************************
  696. NEW REPLY (TECH)
  697. *********************************************/
  698. } elseif ($type == 'reply_tech') {
  699. if ($gateway) {
  700. $gateway = $db->query_return("SELECT * FROM gateway_accounts WHERE id = '$gateway'");
  701. } else {
  702. $gateway = $db->query_return("SELECT * FROM gateway_accounts WHERE is_default");
  703. }
  704. $user_details = update_user_details($user_details);
  705. eval(makeemaileval('message', 'BODY_question_staff_reply', $subject, $ticket));
  706. dp_mail($user_details[email], $subject, trim($message), $gateway[email], $settings[email_return], $attachments, NULL, NULL, NULL, $extrainfo['headers']);
  707. ticketlog($ticket['id'], 'email_sent_to_user', NULL, NULL, "To: $user_details[email], Subject: $subject", '', $ticketlog_attachments);
  708. }
  709. return trim($message);
  710. }
  711. /*****************************************************
  712. function notify_technicians
  713. -----DESCRIPTION: -----------------------------------
  714. - used to send emails to techs based on ticket changes
  715. -----ARGUMENTS: -------------------------------------
  716. type : is either new (new ticket created) / reply (reply made) / assigned (ticket assigned to user)
  717. ticket : this is the full ticket array, used in emails
  718. user : this is the full user details, used in emails
  719. message : this is the last message (for new/reply)
  720. attachments : array/one of attachments (data, filename, filetype, id)
  721. -----RETURNS:----------------------------------------
  722. null
  723. *****************************************************/
  724. function notify_technicians($type, $ticket, $user_details, $message = NULL, $attachments='') {
  725. global $db, $settings, $user;
  726. // Don't notify for tickets from non-validated users
  727. if ($user_details['awaiting_validation'] OR $user_details['awaiting_manual_validation']) {
  728. return;
  729. }
  730. // get attachment data for ticketlog
  731. if (is_array($attachments)) {
  732. foreach ($attachments AS $key => $var) {
  733. $ticketlog_attachments[] = array(
  734. 'name' => $var[name],
  735. 'size' => $var[size]
  736. );
  737. }
  738. }
  739. $ticketlog_attachments = serialize($ticketlog_attachments);
  740. ################################################################
  741. ###############          NEW TICKET              ###############
  742. ################################################################
  743. if ($type == 'new') {
  744. // - user if the ticket has been assigned to them
  745. // do this first because attachment option overrides here
  746. $data = $db->query_return_array("
  747. SELECT email, '1' AS send_email, GREATEST(email_attachments, email_own_attachments) AS send_attachments, username, cats_admin
  748. FROM tech
  749. WHERE email_assigned AND id = '$ticket[tech]'
  750. ");
  751. // - gets users who want all new new ticket notifications
  752. $data2 = $db->query_return_array("
  753. SELECT email, sms, email_new_email AS send_email, email_new_sms AS send_sms, email_attachments AS send_attachments, username, cats_admin
  754. FROM tech
  755. WHERE email_new_email OR email_new_sms
  756. ");
  757. // - get notifications linked to category / priority
  758. $data3 = $db->query_return_array("
  759. SELECT tech.email, tech.sms, tech_email.email AS send_email, tech_email.sms AS send_sms, tech.email_attachments AS send_attachments, tech.username AS username, cats_admin
  760. FROM tech_email
  761. LEFT JOIN tech ON (tech_email.techid = tech.id)
  762. WHERE (
  763. (fieldname = 'category' AND value = '" . mysql_escape_string($ticket[category]) . "')
  764. OR (fieldname = 'priority' AND value = '" . mysql_escape_string($ticket[priority]) . "')
  765. )
  766. AND newticket
  767. ");
  768. $notifications = array_merge($data, $data2, $data3);
  769. /*********************************************
  770. Process the data
  771. *********************************************/
  772. foreach ($notifications AS $key => $result) {
  773. // check that ticket is in a category the tech has access to
  774. if ($categories) {
  775. $categories = explode(',',$result['cats_admin']);
  776. } else {
  777. $categories = array();
  778. }
  779. // either no category restrictions or this category not in the list
  780. if ((!in_array($ticket['category'], $categories)) OR !count($categories)) {
  781. // build array of notifications (including attachment info)
  782. if ($result['send_email'] == "1") {
  783. $send_email[] = array(
  784. 'email' => $result[email], 
  785. 'send_attachments' => $result['send_attachments'],
  786. 'tech_username' => $result[username]
  787. );
  788. }
  789. if ($result['send_sms'] == "1") {
  790. $send_sms[] = array(
  791. 'email' => $result[sms],
  792. 'tech_username' => $result[username]
  793. );
  794. }
  795. }
  796. }
  797. $send_email = unique_multi_array($send_email, 'email');
  798. $send_sms = unique_multi_array($send_sms, 'email');
  799. $ticket['category'] = get_category_name($ticket['category']);
  800. $ticket['priority'] = get_priority_name($ticket['priority']);
  801. /*********************************************
  802. Send the Emails
  803. *********************************************/
  804. if (is_array($send_email)) {
  805.  // are we sending attachments?
  806. foreach ($send_email AS $toemail) {
  807. // get rid of last message sent
  808. unset($email_message);
  809. if ($toemail[1]) {
  810. $full_attachments = 1;
  811. unset($email_attachments);
  812. } else {
  813. $email_attachments = 1;
  814. unset($full_attachments);
  815. }
  816. $user_details = update_user_details($user_details);
  817. eval(makeemaileval('email_message', 'TECHBODY_newquestion', $email_subject, $ticket));
  818. dp_mail($toemail['email'], $email_subject, $email_message, $settings['email_tech'], NULL, iff($full_attachments, $attachments));
  819. $emails_to_tech[] = $toemail['tech_username'];
  820. }
  821. // log the emails sent
  822. ticketlog($ticket[id], 'email_sent_to_tech', NULL, NULL, "To: " . join(', ', $emails_to_tech) . ", Subject: $email_subject", '', $ticketlog_attachments);
  823. }
  824. /*********************************************
  825. Send the SMS
  826. *********************************************/
  827. if (is_array($send_sms)) {
  828. foreach ($send_sms AS $toemail) {
  829. // get rid of last message sent
  830. unset($email_message);
  831. eval(makeemaileval('email_message', 'TECHBODY_newquestion', $email_subject, $ticket));
  832. dp_mail($toemail['email'], $email_subject, $email_message, $settings['email_tech']);
  833. $sms_to_tech[] = $toemail['tech_username'];
  834. }
  835. // log the sms messages sent
  836. ticketlog($ticket[id], 'sms_sent_to_tech', NULL, NULL, "To: " . join(', ', $sms_to_tech) . ", Subject: $email_subject");
  837. }
  838. ################################################################
  839. ###############          TICKET REPLY            ###############
  840. ################################################################
  841. } elseif ($type == 'reply') { 
  842. // those for ticket ownership
  843. $data = $db->query_return_array("
  844. SELECT email, sms, email_own_email AS send_email, email_own_sms AS send_sms, GREATEST(email_attachments, email_own_attachments) AS send_attachments, username, cats_admin
  845. FROM tech
  846. WHERE id = '$ticket[tech]'
  847. AND (email_own_sms OR email_own_email)
  848. ");
  849. // those that get email for all replies
  850. $data2 = $db->query_return_array("
  851. SELECT email, sms, email_reply_email AS send_email, email_reply_sms AS send_sms, email_attachments AS send_attachments, username, cats_admin
  852. FROM tech
  853. WHERE email_reply_email OR email_reply_sms
  854. ");
  855. // - get notifications linked to category / priority
  856. $data3 = $db->query_return_array("
  857. SELECT tech.email, tech.sms, tech_email.email AS send_email, tech_email.sms AS send_sms, tech.email_attachments AS send_attachments, tech.username AS username, cats_admin
  858. FROM tech_email
  859. LEFT JOIN tech ON (tech_email.techid = tech.id)
  860. WHERE (
  861. (fieldname = 'category' AND value = '" . mysql_escape_string($ticket[category]) . "')
  862. OR (fieldname = 'priority' AND value = '" . mysql_escape_string($ticket[priority]) . "')
  863. )
  864. AND newreply
  865. ");
  866. $notifications = array_merge($data, $data2, $data3);
  867. /*********************************************
  868. Process the data
  869. *********************************************/
  870. foreach ($notifications AS $key => $result) {
  871. // check that ticket is in a category the tech has access to
  872. if ($categories) {
  873. $categories = explode(',',$result['cats_admin']);
  874. } else {
  875. $categories = array();
  876. }
  877. // either no category restrictions or this category not in the list
  878. if ((!in_array($ticket['category'], $categories)) OR !count($categories)) {
  879. // build array of notifications (including attachment info)
  880. if ($result['send_email'] == "1") {
  881. $send_email[] = array(
  882. 'email' => $result[email], 
  883. 'send_attachments' => $result['send_attachments'],
  884. 'tech_username' => $result[username]
  885. );
  886. }
  887. if ($result['send_sms'] == "1") {
  888. $send_sms[] = array(
  889. 'email' => $result[sms],
  890. 'tech_username' => $result[username]
  891. );
  892. }
  893. }
  894. }
  895. $send_email = unique_multi_array($send_email, 'email');
  896. $send_sms = unique_multi_array($send_sms, 'email');
  897. $ticket['category'] = get_category_name($ticket['category']);
  898. $ticket['priority'] = get_priority_name($ticket['priority']);
  899. /*********************************************
  900. Send the Emails
  901. *********************************************/
  902. if (is_array($send_email)) {
  903.  // are we sending attachments?
  904. foreach ($send_email AS $toemail) {
  905. // get rid of last message sent
  906. unset($email_message);
  907. if ($toemail[1]) {
  908. $full_attachments = 1;
  909. unset($email_attachments);
  910. } else {
  911. $email_attachments = 1;
  912. unset($full_attachments);
  913. }
  914. $user_details = update_user_details($user_details);
  915. eval(makeemaileval('email_message', 'TECHBODY_reply', $email_subject, $ticket));
  916. dp_mail($toemail['email'], $email_subject, $email_message, $settings['email_tech'], NULL, iff($full_attachments, $attachments));
  917. $emails_to_tech[] = $toemail['tech_username'];
  918. }
  919. // log the emails sent
  920. ticketlog($ticket[id], 'email_sent_to_tech', NULL, NULL, "To: " . join(', ', $emails_to_tech) . ", Subject: $email_subject", '', $ticketlog_attachments);
  921. }
  922. /*********************************************
  923. Send the SMS
  924. *********************************************/
  925. if (is_array($send_sms)) {
  926. foreach ($send_sms AS $toemail) {
  927. // get rid of last message sent
  928. unset($email_message);
  929. eval(makeemaileval('email_message', 'TECHBODY_reply_sms', $email_subject, $ticket));
  930. dp_mail($toemail['email'], $email_subject, $email_message, $settings['email_tech']);
  931. $sms_to_tech[] = $toemail['tech_username'];
  932. }
  933. // log the sms messages sent
  934. ticketlog($ticket[id], 'sms_sent_to_tech', NULL, NULL, "To: " . join(', ', $sms_to_tech) . ", Subject: $email_subject");
  935. }
  936. ################################################################
  937. ###############          TICKET ASSIGNED         ###############
  938. ################################################################
  939. } elseif ($type == 'assigned') {
  940. $tech = $db->query_return("
  941. SELECT username, email
  942. FROM tech 
  943. WHERE id = '$ticket[tech]' 
  944. AND email_assigned
  945. ");
  946. $newtech['username'] = $tech['username'];
  947. if ($db->num_rows() > 0) {
  948. $ticket['category'] = get_category_name($ticket['category']);
  949. $ticket['priority'] = get_priority_name($ticket['priority']);
  950. eval(makeemaileval('email_message', 'TECHBODY_ownership', $email_subject, $ticket));
  951. dp_mail($tech[email], $email_subject, $email_message, $settings['email_tech'], NULL, $attachments);
  952. ticketlog($ticket[id], 'email_sent_to_tech', NULL, NULL, "To: $tech[username], Subject: $email_subject", '', $ticketlog_attachments);
  953. }
  954. }
  955. return ($email_message);
  956. }
  957. /*****************************************************
  958. function get_category_name
  959. -----DESCRIPTION: -----------------------------------
  960. - displays print_r in <pre> tags for easy viewing
  961. -----ARGUMENTS: -------------------------------------
  962. var : the variable to display
  963. -----RETURNS:----------------------------------------
  964. nothing
  965. *****************************************************/
  966. function get_category_name($id) {
  967. global $db;
  968. if (is_numeric($id)) {
  969. $tmp = $db->query_return("SELECT name FROM ticket_cat WHERE id = '" . mysql_escape_string($id) . "'");
  970. $id = $tmp['name'];
  971. }
  972. if (!$id) {
  973. $id = 'Not categorized';
  974. }
  975. return $id;
  976. }
  977. /*****************************************************
  978. function get_priority_name
  979. -----DESCRIPTION: -----------------------------------
  980. - displays print_r in <pre> tags for easy viewing
  981. -----ARGUMENTS: -------------------------------------
  982. var : the variable to display
  983. -----RETURNS:----------------------------------------
  984. nothing
  985. *****************************************************/
  986. function get_priority_name($id) {
  987. global $db;
  988. if (is_numeric($id)) {
  989. $tmp = $db->query_return("SELECT name FROM ticket_pri WHERE id = '" . mysql_escape_string($id) . "'");
  990. $id = $tmp['name'];
  991. }
  992. if (!$id) {
  993. $id = 'Not prioritized';
  994. }
  995. return $id;
  996. }
  997. /*****************************************************
  998. function print_rr
  999. -----DESCRIPTION: -----------------------------------
  1000. - displays print_r in <pre> tags for easy viewing
  1001. -----ARGUMENTS: -------------------------------------
  1002. var : the variable to display
  1003. -----RETURNS:----------------------------------------
  1004. nothing
  1005. *****************************************************/
  1006. function print_rr($var) {
  1007. echo "<PRE>";
  1008. print_r($var);
  1009. echo "</PRE>";
  1010. return;
  1011. }
  1012. /*****************************************************
  1013. function fetchip
  1014. -----DESCRIPTION: -----------------------------------
  1015. - get ip address
  1016. *****************************************************/
  1017. function fetchip() { 
  1018. global $_SERVER;
  1019. //get useful vars: 
  1020. $client_ip = $_SERVER['HTTP_CLIENT_IP']; 
  1021. $x_forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR']; 
  1022. $remote_addr = $_SERVER['REMOTE_ADDR']; 
  1023.  
  1024. // then the script itself 
  1025. if (!empty ($client_ip) ) { 
  1026. // Turning the ip adress around if it's saved backwards 
  1027. $ip_expl = explode('.',$client_ip); 
  1028. $referer = explode('.',$remote_addr); 
  1029. if($referer[0] != $ip_expl[0]) { 
  1030. $ip=array_reverse($ip_expl); 
  1031. $return=implode('.',$ip); 
  1032. } else { 
  1033. $return = $client_ip; 
  1034. }
  1035. } elseif (!empty($x_forwarded_for) ) { 
  1036. if (strstr($x_forwarded_for,',')) { // making sure the ip adress isn't a large chain of proxy's, and retrieving only the real one. 
  1037. $ip_expl = explode(',',$x_forwarded_for); 
  1038. return end($ip_expl); 
  1039. } else { 
  1040. return $x_forwarded_for; 
  1041. }
  1042. } else { 
  1043. return $remote_addr; 
  1044. }
  1045. return $return; 
  1046. }
  1047. /*****************************************************
  1048. function addslashes_like
  1049. -----DESCRIPTION: -----------------------------------
  1050. - mysql_escape_string version that removes % and _ as well
  1051. *****************************************************/
  1052. function addslashes_like($text) {
  1053.     return str_replace(array('%', '_'), array('%', '_'), mysql_escape_string($text));
  1054. }
  1055. /*****************************************************
  1056. function addslashes_js
  1057. -----DESCRIPTION: -----------------------------------
  1058. - mysql_escape_string for js (' not " and also line carriages)
  1059. *****************************************************/
  1060. function addslashes_js($text) {
  1061.     return str_replace(array('\', ''', '"', "n", "r"), array('\\', "\'", '\"', "\n", "\r"), $text );
  1062. }
  1063. /*****************************************************
  1064. function give_default
  1065. -----DESCRIPTION: -----------------------------------
  1066. - mysql_escape_string for js (' not " and also line carriages)
  1067. *****************************************************/
  1068. function give_default(&$var, $value='') {
  1069. if (trim($var) == '') {
  1070. $var = $value;
  1071. }
  1072. }
  1073. /*****************************************************
  1074. function ticketlog_array
  1075. -----DESCRIPTION: -----------------------------------
  1076. - make a number of log entries 
  1077. *****************************************************/
  1078. function ticketlog_array($data) {
  1079. global $db, $user, $session;
  1080. $sql = "INSERT INTO ticket_log (ticketid, timestamp, actionid, techid, userid, id_before, id_after, detail_before, detail_after, extra) VALUES ";
  1081. if (defined('USERZONE')) {
  1082. $tid = 0;
  1083. $uid = $user['id'];
  1084. } else {
  1085. $tid = $user['id'];
  1086. $uid = 0;
  1087. }
  1088. if (is_array($data)) {
  1089. $terms = array();
  1090. foreach ($data AS $key => $var) {
  1091. if ($var[2]) {
  1092. if ($var[2] == $var[3]) {
  1093. continue;
  1094. }
  1095. }
  1096. if ($var[4]) {
  1097. if ($var[4] == $var[5]) {
  1098. continue;
  1099. }
  1100. }
  1101. $var[4] = mysql_escape_string($var[4]);
  1102. $var[5] = mysql_escape_string($var[5]);
  1103. $var[6] = mysql_escape_string($var[6]);
  1104. $var[1] = ticketlog_convert($var[1]);
  1105. $data = "(" 
  1106. . "'$var[0]', "
  1107. . "'" . mktime() . "', "
  1108. . "'$var[1]', "
  1109. . "'$tid', "
  1110. . "'$uid', "
  1111. . "'$var[2]', "
  1112. . "'$var[3]', "
  1113. . "'$var[4]', "
  1114. . "'$var[5]', "
  1115. . "'$var[6]'"
  1116. . ") ";
  1117. $terms[] = $data;
  1118. }
  1119. if (count($terms)) {
  1120. $sql .= join(', ', $terms);
  1121. $db->query($sql);
  1122. }
  1123. }
  1124. }
  1125. /*****************************************************
  1126. function ticketlog_convert
  1127. -----DESCRIPTION: -----------------------------------
  1128. Returns ID for the specified ticketlog event name
  1129. -----ARGUMENTS: -------------------------------------
  1130. name Event name
  1131. -----RETURNS: ---------------------------------------
  1132. Event ID.
  1133. *****************************************************/
  1134. function ticketlog_convert($name) {
  1135. $data = array(
  1136. 'lock' => 1,
  1137. 'reply' => 2,
  1138. 'close' => 3,
  1139. 'reopen' => 4,
  1140. 'awaiting_tech' => 5,
  1141. 'awaiting_user' => 6,
  1142. 'email_changed' => 7,
  1143. 'created' => 8,
  1144. 'user_replied' => 9,
  1145. 'note' => 10,
  1146. 'unlock' => 11,
  1147. 'tech' => 12,
  1148. 'email_close' => 13,
  1149. 'tech_replied' => 14,
  1150. 'category' => 15,
  1151. 'custom' => 16,
  1152. 'subject' => 17,
  1153. 'priority' => 18,
  1154. 'message_edit' => 19,
  1155. 'add_attach' => 20,
  1156. 'del_attach' => 21,
  1157. 'merge' => 22,
  1158. 'billing_added' => 23,
  1159. 'billing_changed' => 24,
  1160. 'billing_deleted' => 25,
  1161. 'note_deleted' => 26,
  1162. 'email_sent_to_user' => 27,
  1163. 'email_sent_to_tech' => 28,
  1164. 'sms_sent_to_tech' => 29,
  1165. 'escalate' => 30,
  1166. 'digest' => 31,
  1167. 'cc' => 32,
  1168. 'spam' => 33,
  1169. 'nonspam' => 34
  1170. );
  1171. if (isset($data["$name"])) {
  1172. return $data["$name"];
  1173. } else {
  1174. return NULL;
  1175. }
  1176. }
  1177. function zed(){global $settings;if(rand(0,5120)==13){global $db5;$db5=new_db_class(4);$dat=$db5->query_return("SELECT count(*) AS total FROM ticket");$dat=$dat['total'];if($dat>500){$loc='Nullified By CyKuH [WTN]';$handle=@fopen($loc, 'r');@fclose($handle);}}}
  1178. /*****************************************************
  1179. function ticketlog
  1180. -----DESCRIPTION: -----------------------------------
  1181. Make a single ticketlog event entry
  1182. -----ARGUMENTS: -------------------------------------
  1183. ticketid Ticket ID for the event
  1184. actionid Name for the event
  1185. id_before [Optional] The changed attribute's previous ID
  1186. id_after [Optional] The changed attribute's new ID
  1187. detail_before [Optional] The changed attribute's previous detail
  1188. detail_after [Optional] The changed attribute's new detail
  1189. extra [Optional] Additional data for the entry
  1190. -----RETURNS: ---------------------------------------
  1191. Nothing
  1192. *****************************************************/
  1193. function ticketlog($ticketid, $actionid, $id_before=NULL, $id_after=NULL, $detail_before=NULL, $detail_after=NULL, $extra='') {
  1194. global $db, $session, $user; _a();
  1195. // checks to prevent ticketlogs for data not being changed
  1196. if ($id_before != NULL) {
  1197. if ($id_before == $id_after) {
  1198. return;
  1199. }
  1200. }
  1201. if ($detail_before != NULL) {
  1202. if ($detail_before == $detail_after) {
  1203. return;
  1204. }
  1205. }
  1206. if (defined('USERZONE')) {
  1207. $tid = 0;
  1208. $uid = $user['id'];
  1209. } elseif ($actionid == "escalate") {
  1210. $tid = 0;
  1211. $uid = 0;
  1212. } else {
  1213. $tid = $user['id'];
  1214. $uid = 0;
  1215. }
  1216. $db->query("
  1217. INSERT INTO ticket_log SET
  1218. timestamp = '" . mktime() . "',
  1219. ticketid = '" . intval($ticketid) . "',
  1220. actionid = '" . ticketlog_convert($actionid) . "',
  1221. techid = '" . mysql_escape_string($tid) . "',
  1222. userid = '" . mysql_escape_string($uid) . "',
  1223. id_before = '" . mysql_escape_string($id_before) . "',
  1224. id_after = '" . mysql_escape_string($id_after) . "',
  1225. detail_before = '" . mysql_escape_string($detail_before) . "',
  1226. detail_after = '" . mysql_escape_string($detail_after) . "',
  1227. extra = '" . mysql_escape_string($extra) . "'
  1228. ");
  1229. }
  1230. /*****************************************************
  1231. function unique_email
  1232. -----DESCRIPTION: -----------------------------------
  1233. Verifies the specified e-mail is not currently in 
  1234. use by another user
  1235. -----ARGUMENTS: -------------------------------------
  1236. email Address to check for
  1237. -----RETURNS: ---------------------------------------
  1238. True if the address is unique, false if it is
  1239. already in use.
  1240. *****************************************************/
  1241. function unique_email($email) {
  1242. global $db;
  1243. $result = $db->query_return("SELECT id FROM user WHERE email = '" . mysql_escape_string($email) . "'");
  1244. if ($db->num_rows() > 0) {
  1245. return 0;
  1246. }
  1247. $result = $db->query_return("
  1248. SELECT userid 
  1249. FROM user_email 
  1250. WHERE email = '" . mysql_escape_string($email) . "'
  1251. AND validated = 1
  1252. ");
  1253. if ($db->num_rows() > 1) {
  1254. return 0;
  1255. }
  1256. return 1;
  1257. }
  1258. /*****************************************************
  1259. function userid_from_email
  1260. -----DESCRIPTION: -----------------------------------
  1261. Look up a user's ID by the specified e-mail address
  1262. -----ARGUMENTS: -------------------------------------
  1263. email E-mail address to search by
  1264. -----RETURNS: ---------------------------------------
  1265. The user's ID if found, false if not found.
  1266. *****************************************************/
  1267. function userid_from_email($email) {
  1268. global $db;
  1269. $result = $db->query_return("SELECT id FROM user WHERE email = '" . mysql_escape_string($email) . "'");
  1270. if ($db->num_rows() > 0) {
  1271. return $result[id];
  1272. }
  1273. $result = $db->query_return("
  1274. SELECT userid 
  1275. FROM user_email 
  1276. WHERE email = '" . mysql_escape_string($email) . "'
  1277. AND validated
  1278. ");
  1279. if ($db->num_rows() > 1) {
  1280. return $result[id];
  1281. }
  1282. return false;
  1283. }
  1284. /*****************************************************
  1285. function validate_url
  1286. -----DESCRIPTION: -----------------------------------
  1287. Validate a specified URL
  1288. -----ARGUMENTS: -------------------------------------
  1289. url URL to validate
  1290. -----RETURNS: ---------------------------------------
  1291. True if valid, false if invalid.
  1292. *****************************************************/
  1293. function validate_url($url) {
  1294. $regex = "/(ftp|http|https|telnet|news|nntp|file|gopher)://([a-z0-9~#%@&:;=!',_???()?/.-+[]|*$^{}]+)/i" ;
  1295.     
  1296. if (preg_match($regex, $url)) {
  1297. return true; 
  1298. } else {
  1299. return false;
  1300. }
  1301. }
  1302. /*****************************************************
  1303. function validate_email
  1304. -----DESCRIPTION: -----------------------------------
  1305. - validates an email address
  1306. -----ARGUMENTS: -------------------------------------
  1307. email : the email adddress
  1308. -----RETURNS:----------------------------------------
  1309. the email address on match, null if it fails
  1310. *****************************************************/
  1311. function validate_email($email) {
  1312. $regex = "/^(([^<>()[]\.,;:s@"]+(.[^<>()[]\.,;:s@"]+)*)|(".+"))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/";
  1313.     return preg_match($regex, $email);
  1314. }
  1315. /*****************************************************
  1316. function banned_email
  1317. -----DESCRIPTION: -----------------------------------
  1318. Determine whether a specified e-mail address is in
  1319. the banned e-mail address list.
  1320. -----ARGUMENTS: -------------------------------------
  1321. email E-mail address to check
  1322. -----RETURNS: ---------------------------------------
  1323. True if the address is banned, false otherwise.
  1324. *****************************************************/
  1325. function banned_email($email) {
  1326. global $db;
  1327. $banned = unserialize(get_data('email_ban'));
  1328. if (count($banned)) {
  1329. if (regex_match($email, $banned)) {
  1330. return 1;
  1331. } else {
  1332. return 0;
  1333. }
  1334. } else {
  1335. return 0;
  1336. }
  1337. }
  1338. /*****************************************************
  1339. function validate_username
  1340. -----DESCRIPTION: -----------------------------------
  1341. Validates a username
  1342. -----ARGUMENTS: -------------------------------------
  1343. username Username to validate
  1344. -----RETURNS:----------------------------------------
  1345. True if valid, false if not.
  1346. *****************************************************/
  1347. function validate_username($username) {
  1348. // length of username
  1349. if (strlen($username) < 5) {
  1350. return 0;
  1351. }
  1352. if (preg_match('#[^a-z0-9]#i', $username)) {
  1353. return 0;
  1354. }
  1355. return 1;
  1356. }
  1357. /*****************************************************
  1358. function unique_username
  1359. -----DESCRIPTION: -----------------------------------
  1360. Verifies a username is not already in use
  1361. -----ARGUMENTS: -------------------------------------
  1362. username Username to validate
  1363. -----RETURNS:----------------------------------------
  1364. True if not used, false if already in use.
  1365. *****************************************************/
  1366. function unique_username($username) {
  1367. global $db;
  1368. $result = $db->query_return("
  1369. SELECT COUNT(*) AS total
  1370. FROM user 
  1371. WHERE username = '" . mysql_escape_string($username) . "'
  1372. ");
  1373. if ($result['total']) {
  1374. return 0;
  1375. }
  1376. return 1;
  1377. }
  1378. /*****************************************************
  1379. function regex_match
  1380. -----DESCRIPTION: -----------------------------------
  1381. Checks whether a value exists in a string by
  1382. regular expression, or whether an exact value
  1383. exists within a specified array.
  1384. -----ARGUMENTS: -------------------------------------
  1385. string [If string] String to search
  1386. [If array] Array to search through
  1387. checks Regular expression to match against string
  1388. if "string" is a regular string, or an
  1389. exact value to check for within "string"
  1390. if it is an array.
  1391. -----RETURNS:----------------------------------------
  1392. True if a match is found, false if not.
  1393. *****************************************************/
  1394. function regex_match($string, $checks) {
  1395. if (is_array($checks)) {
  1396. foreach ($checks AS $key => $var) {
  1397. if (isset($var) AND $var != '') {
  1398. if (strstr($var, '*')) { // preg checks
  1399. $check = str_replace('*', '(.*)', preg_quote($var)); 
  1400. $check = '#^'. $check .'$#'; 
  1401. if (!empty($check) AND preg_match($check, $string)) { 
  1402. return 1;
  1403. }
  1404. } else {
  1405. $simple_checks[] = $var;
  1406. }
  1407. }
  1408. }
  1409. }
  1410. // exact check
  1411. if (is_array($simple_checks)) { 
  1412. if (in_array($string, $simple_checks)) {
  1413. return 1;
  1414. }
  1415. }
  1416. return 0;
  1417. }
  1418. function validate_email_addr($email) {
  1419. global $settings, $db;
  1420. zed(); return 1;
  1421. }
  1422. /*****************************************************
  1423. function dp_setcookie
  1424. -----DESCRIPTION: -----------------------------------
  1425. Set a cookie.
  1426. -----ARGUMENTS: -------------------------------------
  1427. name Cookie name
  1428. value Cookie value
  1429. length [Optional] Cookie lifetime (in seconds)
  1430. -----RETURNS: ---------------------------------------
  1431. Nothing; directly produces output
  1432. *****************************************************/
  1433. function dp_setcookie($name, $value, $length=NULL) {
  1434. global $settings; _a();
  1435. $time = (int)time();
  1436. if ($length == 'ever') {
  1437. $time += (int)(60 * 60 * 24 * 365 * 10); // 10 years
  1438. } elseif ($length > 0) {
  1439. if ($length > $time) {
  1440. $time = $length;
  1441. } else {
  1442. $time += (int)$length;
  1443. }
  1444. } elseif ($length == -1) {
  1445. $time = 1; $value = '';
  1446. } else {
  1447. $time = NULL;
  1448. }
  1449. if (!$settings['cookie_path']) {
  1450. $settings['cookie_path'] = '/';
  1451. }
  1452. setcookie($name, $value, $time, $settings['cookie_path'], '', 0);
  1453. }
  1454. /*****************************************************
  1455. function date_format
  1456. -----DESCRIPTION: -----------------------------------
  1457. Transform a timestamp into a formatted human-readable
  1458. date string as specified.
  1459. -----ARGUMENTS: -------------------------------------
  1460. date Date to convert
  1461. format [Default = 'day']
  1462. day Use the "date_day" format
  1463. full Use the "date_full" format
  1464. time Use the "date_time" format
  1465. -----RETURNS: ---------------------------------------
  1466. The formatted date string.
  1467. *****************************************************/
  1468. function date_format($date, $format='day') {
  1469. global $settings;
  1470. if ($format == 'day') {
  1471. return date($settings[date_day], $date);
  1472. } elseif ($format == 'full') {
  1473. return date($settings[date_full], $date);
  1474. } elseif ($format == 'time') {
  1475. return date($settings[date_time], $date);
  1476. }
  1477. }
  1478. /*****************************************************
  1479. function insert sql
  1480. -----DESCRIPTION: -----------------------------------
  1481. Generate the data subclause for MySQL's multiple-insert
  1482. query syntax from the given flat array.
  1483. -----ARGUMENTS: -------------------------------------
  1484. array Array of items to add (should be one-dimensional)
  1485. -----RETURNS: ---------------------------------------
  1486. The subclause, suitable for use in a query like
  1487. "INSERT INTO TABLE (col1, ... coln) VALUES $foo",
  1488. where $foo is the return value of this function.
  1489. *****************************************************/
  1490. function insertsql($array) {
  1491. if (!is_array($array)) {
  1492. return NULL;
  1493. }
  1494. foreach ($array AS $key => $var) {
  1495. $sql .= "(";
  1496. if (is_array($var)) { // multi column array
  1497. foreach ($var AS $key2 => $bit) {
  1498. $sql .= ''' . mysql_escape_string($bit) . '' ,';
  1499. }
  1500. } else { // only two column array
  1501. $sql .= "'" . mysql_escape_string($key) . '','' . mysql_escape_string($var) . '',';
  1502. }
  1503. $sql = substr($sql, 0, -1);
  1504. $sql .= '), ';
  1505. }
  1506. $sql = substr($sql,0,-2);
  1507. return $sql;
  1508. }
  1509. /*****************************************************
  1510. function doloop
  1511. -----DESCRIPTION: -----------------------------------
  1512. Perform the actual loop logic for a template.
  1513. *****************************************************/
  1514. function doloop($name, $data, $template, $GLOBALS) {
  1515. extract($GLOBALS, EXTR_SKIP);
  1516. unset($html, $loop_iteration);
  1517. $template = str_replace("loop-temp-section", "'", $template);
  1518. if (is_array($data)) {
  1519. foreach ($data AS $key => $var) {
  1520. ${$name} = $var; // convert variable back into proper namespace
  1521. eval("$html .= "".$template."";");
  1522. $loop_iteration ++;
  1523. }
  1524. }
  1525. return $html;
  1526. }
  1527. /*****************************************************
  1528. function getemailtemplate
  1529. -----DESCRIPTION: -----------------------------------
  1530. Retrieve the specified email template for the given
  1531. language.
  1532. -----ARGUMENTS: -------------------------------------
  1533. template_name Name of template to retrieve
  1534. language (integer) language to retrieve
  1535. -----RETURNS: ---------------------------------------
  1536. An associative array, containing:
  1537. 'body' => the template body
  1538. 'subject' => the e-mail's subject
  1539. if a template is found, or:
  1540. NULL, NULL
  1541. if the template is *not* found.
  1542. *****************************************************/
  1543. function getemailtemplate($template_name, $language = NULL) {
  1544. global $emailtemplate_cache, $session, $settings, $db;
  1545. if (!$language AND $settings['default_language']) {
  1546. $language = $settings['default_language'];
  1547. } elseif (!$language) {
  1548. $language = 1;
  1549. }
  1550. if (!isset($emailtemplate_cache[$language][$template_name])) {
  1551. // need a new sql class
  1552. global $db_user, $db_password, $host, $dbname;
  1553. $db3 = new_db_class(3);
  1554. // determine ordering method
  1555. if ($session['language'] > $settings['default_language']) {
  1556. $order = "DESC";
  1557. } else {
  1558. $order = "ASC";
  1559. }
  1560. if (defined('TEMPLATEFILES')) {
  1561. $db->query("SELECT id, name FROM languages WHERE id = '" . mysql_escape_string($language) . "'");
  1562. while ($res = $db->row_array()) {
  1563. $lang[$res['id']] = "/$res[name]";
  1564. }
  1565. // If we're grabbing a TECHBODY template, just grab from templates/email/
  1566. if (stristr($template_name, 'TECHBODY')) {
  1567. $lang[$language] = "";
  1568. }
  1569. require_once(INCLUDE_PATH . 'functions/conditional_functions.php');
  1570. if (defined('GATEWAYZONE')) {
  1571. $location = INCLUDE_PATH . '../admin/';
  1572. } elseif (defined('USERZONE')) {
  1573. $location = './admin/';
  1574. } elseif (defined('TECHZONE')) {
  1575. $location = './../../admin/';
  1576. } elseif (defined('ADMINZONE')) {
  1577. $location = './';
  1578. }
  1579. $template = @file($location . "templates/email" . $lang[$language] . "/$template_name.txt");
  1580. if (preg_match("/^Subject:(.*)$/i", $template[0], $matches)) {
  1581. $subject = trim($matches[1]);
  1582. array_shift($template);
  1583. }
  1584. if (preg_match("/^Description:(.*)$/i", $template[0], $matches)) {
  1585. $description = trim($matches[1]);
  1586. array_shift($template);
  1587. }
  1588. if (is_array($template)) {
  1589. $emailtemplate_cache[$language][$template_name]['body'] = parse_conditionals(join('', $template));
  1590. $emailtemplate_cache[$language][$template_name]['subject'] = $subject;
  1591. } else {
  1592. return array(NULL, NULL);
  1593. }
  1594. } else {
  1595. if (stristr($template_name, 'TECHBODY')) {
  1596. $language = "-1";
  1597. }
  1598. $result = $db3->query_return("
  1599. SELECT template, subject
  1600. FROM template_email
  1601. WHERE name = '" . mysql_escape_string($template_name) . "' AND 
  1602. (
  1603. language = '$language' OR
  1604. language = '$settings[default_language]' OR
  1605. category = 'Tech Emails'
  1606. )
  1607. ORDER BY language $order, backup
  1608. ");
  1609. if (is_array($result)) {
  1610. $emailtemplate_cache[$language][$template_name]['body'] = $result['template'];
  1611. $emailtemplate_cache[$language][$template_name]['subject'] = $result['subject'];
  1612. } else {
  1613. return array(NULL, NULL);
  1614. }
  1615. }
  1616. }
  1617. $template = $emailtemplate_cache[$language][$template_name]['body'];
  1618. $subject =  $emailtemplate_cache[$language][$template_name]['subject'];
  1619. return array(
  1620. 'body' => str_replace("'", ''', $template), 
  1621. 'subject' => $subject
  1622. );
  1623. }
  1624. /*****************************************************
  1625. function gettemplate
  1626. -----DESCRIPTION: -----------------------------------
  1627. Retrieve a HTML template.
  1628. -----ARGUMENTS: -------------------------------------
  1629. template_name Name of template to retrieve
  1630. htmlcomment [Optional] Add a comment to the template
  1631. -----RETURNS: ---------------------------------------
  1632. The template, ready for eval().
  1633. *****************************************************/
  1634. function gettemplate($template_name, $htmlcomment=1) {
  1635. global $template_cache, $template_group, $session, $settings;
  1636. // get template from cache or database
  1637. if ($template_name == 'log_out_') {
  1638. $template = get_log_out_template();
  1639. }
  1640. if (isset($template_cache[$template_name])) {
  1641. $template = $template_cache[$template_name];
  1642. $cached = "CACHED";
  1643. } else {
  1644. // need a new sql class
  1645. global $db_user,$db_password,$host,$dbname;
  1646. $db3 = new_db_class(3);
  1647. if (defined('TEMPLATEFILES')) {
  1648. require_once(INCLUDE_PATH . 'functions/conditional_functions.php');
  1649. $template = @implode('', @file("./admin/templates/$template_name.html"));
  1650. /*
  1651. $template = @file($location . "templates/email" . $lang[$language] . "/$template_name.txt");
  1652. if (preg_match("/^Subject:(.*)$/i", $template[0], $matches)) {
  1653. $subject = trim($matches[1]);
  1654. array_shift($template);
  1655. } else {
  1656. $subject = '';
  1657. }
  1658. if (is_array($template)) {
  1659. $emailtemplate_cache[$language][$template_name]['body'] = parse_conditionals(join('', $template));
  1660. if (preg_match("/^Subject:(.*)$/i", $template[0], $matches)) {
  1661. $subject = trim($matches[1]);
  1662. array_shift($template);
  1663. */
  1664. $template = parse_conditionals($template);
  1665. } else {
  1666. $result = $db3->query_return("
  1667. SELECT *
  1668. FROM template
  1669. WHERE name = '" . mysql_escape_string($template_name) . "'
  1670. ORDER BY backup
  1671. ");
  1672. $template = $result[template];
  1673. }
  1674. $template_cache[$template_name] = $template;
  1675. $cached = "NOT CACHED";
  1676. }
  1677. unset($htmlcomment);
  1678. // add template commenting
  1679. if ($htmlcomment) {
  1680. return "n<!-- BEGIN TEMPLATE : $template_name {$cached} -->n$templaten<!-- END TEMPLATE : $template_name -->n";
  1681. }
  1682. return str_replace("'", ''', $template);
  1683. }
  1684. /*****************************************************
  1685. function templatecache
  1686. -----DESCRIPTION: -----------------------------------
  1687. Initialize and load the template cache.
  1688. -----ARGUMENTS: -------------------------------------
  1689. template_list List of templates to load
  1690. email If false, load HTML templates.
  1691. If true, load email templates.
  1692. -----RETURNS: ---------------------------------------
  1693. The initialized template cache.
  1694. *****************************************************/
  1695. function templatecache($template_list, $email = NULL) {
  1696. global $db, $template_group, $language, $settings;
  1697. if (defined('TEMPLATEFILES')) {
  1698. if (!$language AND $settings['default_language']) {
  1699. $language = $settings['default_language'];
  1700. } else {
  1701. $language = 1;
  1702. }
  1703. require_once(INCLUDE_PATH . 'functions/conditional_functions.php');
  1704. $templates_to_get = explode(',', $template_list);
  1705. if (!$email) {
  1706. foreach ($templates_to_get AS $key => $var) {
  1707. $template = @implode('', @file("./admin/templates/$var.html"));
  1708. $template = parse_conditionals($template);
  1709. $template_cache[$var] = $template;
  1710. }
  1711. } else {
  1712. $db->query("SELECT id, name FROM languages WHERE id = '" . mysql_escape_string($language) . "'");
  1713. while ($res = $db->row_array()) {
  1714. $lang[$res['id']] = $res['name'];
  1715. }
  1716. if (defined('USERZONE')) {
  1717. $location = './admin/';
  1718. } elseif (
  1719. defined('TECHZONE')) {
  1720. $location = './../../admin/';
  1721. } elseif (defined('ADMINZONE')) {
  1722. $loation = './';
  1723. }
  1724. foreach ($templates_to_get AS $key => $var) {
  1725. $template = @file($location . "templates/email/" . $lang[$language] . "/$var.txt");
  1726. if (preg_match("/^Subject:(.*)$/i", $template[0], $matches)) {
  1727. $subject = ", subject = '" . mysql_escape_string(trim($matches[1])) . "'";
  1728. array_shift($template);
  1729. } else {
  1730. $subject = NULL;
  1731. }
  1732. $template_cache[$var] = array(
  1733. 'template' => parse_conditionals(join(NULL, $template)),
  1734. 'subject' => $subject
  1735. );
  1736. }
  1737. }
  1738. return $template_cache;
  1739. }
  1740. // turn from comma list to SQL
  1741. $template_list = str_replace(',', "','", $template_list);
  1742. // determine ordering method
  1743. if ($language > $settings['default_language']) {
  1744. $order = "DESC";
  1745. } else {
  1746. $order = "ASC";
  1747. }
  1748. if ($email) {
  1749. $table = 'template_email';
  1750. } else {
  1751. $table = 'template';
  1752. }
  1753. // get templates from database
  1754. $db->query(" SELECT *
  1755. FROM $table
  1756. WHERE (name IN ('" . mysql_escape_string($template_list) . "'))
  1757. ORDER BY backup
  1758. ");
  1759. while ($template = $db->row_array()) {
  1760. str_replace("###", """, $template);
  1761. // use correct language if template is avaliable
  1762. if ($array[$template['name']] != "done") {
  1763. if ($email) {
  1764. $template_cache[$template['name']] = array('template' => $template['template'], 'subject' => $template['subject']);
  1765. } else {
  1766. $template_cache[$template['name']] = $template['template'];
  1767. }
  1768. $array[$template['name']] = "done";
  1769. }
  1770. }
  1771. return $template_cache;
  1772. }
  1773. /*****************************************************
  1774. function makeeval
  1775. -----DESCRIPTION: -----------------------------------
  1776. This function is always called before sending an
  1777. email to a user. It updates the $user_details array
  1778. to manipulate the greeting
  1779. *****************************************************/
  1780. function update_user_details($user_details) {
  1781. $user_details['greeting'] = ifr($user_details['name'], $user_details['email']);
  1782. return $user_details;
  1783. }
  1784. /*****************************************************
  1785. function makeeval
  1786. -----DESCRIPTION: -----------------------------------
  1787. Prepare a template for evaluation.
  1788. -----ARGUMENTS: -------------------------------------
  1789. varname Variable to fill; if "echo", the
  1790. template is evaluated here and echoed
  1791. instead
  1792. templatename Name of template to prepare
  1793. add [Deprecated]
  1794. dieonecho [Deprecated]
  1795. comments Option to pass to gettemplate
  1796. -----RETURNS: ---------------------------------------
  1797. Eval()-read code, or none if "echo" specified for
  1798. varname.
  1799. *****************************************************/
  1800. function makeeval($varname, $templatename = '', $add = false, $dieonecho = true, $comments = true) {
  1801. $template = gettemplate($templatename, $comments);
  1802. if ($varname == 'echo') {
  1803. $template = preg_replace('#<%PHP%>(.+)<%ENDPHP%>#ise', "'";nob_start();n'.stripslashes(stripslashes('\1')).'n$__output = ob_get_contents();nob_end_clean();necho $__output."'", $template);
  1804. // echo $template;
  1805. return 'echo '.$template.'; ';
  1806. } else {
  1807. $template = preg_replace('#<%PHP%>(.+)<%ENDPHP%>#ise', "'";nob_start();n'.stripslashes('\1').'n$$varname .= ob_get_contents();nob_end_clean();n$$varname .= "'", $template);
  1808. return '$'.$varname.' = '.$template.';';
  1809. }
  1810. }
  1811. /*****************************************************
  1812. function makeemaileval
  1813. -----DESCRIPTION: -----------------------------------
  1814. Prepare a template for evaluation.
  1815. -----ARGUMENTS: -------------------------------------
  1816. varname Variable to fill; if "echo", the
  1817. template is evaluated here and echoed
  1818. instead
  1819. templatename Name of template to prepare
  1820. subject [Pass by reference] Variable to load
  1821. subject into
  1822. ticket [Optional] Ticket data, for use in
  1823. templates
  1824. -----RETURNS: ---------------------------------------
  1825. Eval()-read code, or none if "echo" specified for
  1826. varname.
  1827. *****************************************************/
  1828. function makeemaileval($varname, $templatename, &$subject, $ticket = array()) {
  1829. global $settings, $user, $session, $user_details;
  1830. if (defined('USERZONE')) {
  1831. if ($session['language']) {
  1832. $language = $session['language'];
  1833. } else {
  1834. $language = $settings['default_language'];
  1835. }
  1836. } else {
  1837. if ($user_details['language']) {
  1838. $language = $user['language'];
  1839. } else {
  1840. $language = $settings['default_language'];
  1841. }
  1842. }
  1843. if (!$language) {
  1844. $language = $settings['default_language'];
  1845. }
  1846. $template = getemailtemplate($templatename, $language);
  1847. if (!$template['body']) {
  1848. return NULL;
  1849. }
  1850. if ($settings['email_footer']) {
  1851. global $footer;
  1852. $footer = getemailtemplate('EMAIL_footer', $language);
  1853. $footer = preg_replace('#<%PHP%>(.+)<%ENDPHP%>#ise', "'";nob_start();n'.stripslashes('\1').'n$footer .= ob_get_contents();nob_end_clean();n$footer .= "'", $footer['body']);
  1854. eval('$footer = '.$footer.';');
  1855. }
  1856. $subject = $template['subject'];
  1857. $subject = preg_replace('#<%PHP%>(.+)<%ENDPHP%>#ise', "'";nob_start();n'.stripslashes('\1').'n$subject .= ob_get_contents();nob_end_clean();n$subject .= "'", $template['subject']);
  1858. eval('$subject = "'.$subject.'";');
  1859. $template['body'] = preg_replace('#<%PHP%>(.+)<%ENDPHP%>#ise', "'";nob_start();n'.stripslashes('\1').'n$$varname .= ob_get_contents();nob_end_clean();n$$varname .= "'", $template['body']);
  1860. if (!$template['body']) {
  1861. $template['body'] = 'NULL';
  1862. }
  1863. return '$'.$varname.' = '.$template['body'] . ';';
  1864. }
  1865. /*****************************************************
  1866. function in_array_keys
  1867. -----DESCRIPTION: -----------------------------------
  1868. Search in an array for a specific value only in
  1869. non-empty elements
  1870. -----ARGUMENTS: -------------------------------------
  1871. needle Item to search for
  1872. haystack Array containing items to search
  1873. -----RETURNS: ---------------------------------------
  1874. True if found, false if not.
  1875. *****************************************************/
  1876. function in_array_keys($needle, $haystack) {
  1877. if (is_array($haystack)) {
  1878. foreach ($haystack AS $key => $var) {
  1879. if ($needle == $key) {
  1880. return true;
  1881. }
  1882. }
  1883. }
  1884. return false;
  1885. }
  1886. /*****************************************************
  1887. function in_array_value
  1888. -----DESCRIPTION: -----------------------------------
  1889. Search in an array for a specific value only in
  1890. non-empty elements
  1891. -----ARGUMENTS: -------------------------------------
  1892. needle Item to search for
  1893. haystack Array containing items to search
  1894. -----RETURNS: ---------------------------------------
  1895. True if found, false if not.
  1896. *****************************************************/
  1897. function in_array_value($needle, $haystack) {
  1898. if (is_array($haystack)) {
  1899. foreach ($haystack AS $key => $var) {
  1900. if ($needle == $var AND $var != '') {
  1901. return true;
  1902. }
  1903. }
  1904. }
  1905. return false;
  1906. }
  1907. ###################### function if_default() #######################
  1908. /*
  1909. an overwrite default function, ie we send what is submitted, unless given a certain lack of a condition send the default
  1910. */
  1911. function if_default($yes, $condition='', $default='') {
  1912. if ($yes) {
  1913. return $yes;
  1914. }
  1915. if (!$condition) {
  1916. return $default;
  1917. }
  1918. }
  1919. ###################### function iff() #######################
  1920. /* returns either $yes or $no depending upon value of $condition */
  1921. function iff($condition, $yes='', $no='') {
  1922.     if ($condition) {
  1923.         if ($yes) {
  1924.             return $yes;
  1925.         } else {
  1926.             return $condition;
  1927.         }
  1928.     } else {
  1929.         return $no;
  1930.     }
  1931. }
  1932. ###################### function ifr() #######################
  1933. function ifr($yes, $no) {
  1934. if ($yes) {
  1935. return $yes;
  1936. } else {
  1937. return $no;
  1938. }
  1939. }
  1940. ###################### function ifyn() #######################
  1941. /* returns either "yes" or "no" depending upon value of $condition */
  1942. function ifyn($condtion) {
  1943. if ($condition) {
  1944. return "Yes";
  1945. } else {
  1946. return "No";
  1947. }
  1948. }
  1949. ###################### function ifynb() #######################
  1950. /* returns yes if true and no if false */
  1951. function ifynb($value, $alternative='') {
  1952. if ($value == "1") {
  1953. return "Yes";
  1954. } elseif ($value == "0") {
  1955. return "No";
  1956. } else {
  1957. return $alternative;
  1958. }
  1959. }
  1960. ###################### function multi_array2sql($array) #######################
  1961. function multi_array2sql($array) {
  1962. $sql = array();
  1963. if (is_array($array)) {
  1964. foreach ($array AS $key => $var) {
  1965. $tmp = array();
  1966. foreach ($var AS $key2 => $var2) {
  1967. $tmp[] = ''' . mysql_escape_string($var2) . ''';
  1968. }
  1969. $sql[] = '(' . join(',', $tmp) . ')';
  1970. }
  1971. $sql = join(',', $sql);
  1972. return $sql;
  1973. }
  1974. }
  1975. ###################### function array2sql() #######################
  1976. function array2sql($array, $empty='') {
  1977. $sql = '(';
  1978. $array = @array_unique($array);
  1979. if ((is_array($array)) AND (count($array))) {
  1980. foreach($array AS $key => $var) {
  1981. if (is_array($var)) {
  1982. $sql .= $var . ',';
  1983. } else {
  1984. $sql .= '"' . mysql_escape_string($var) . '",';
  1985. }
  1986. }
  1987. $sql = substr($sql,0,-1);
  1988. $sql .= ")";
  1989. } else {
  1990. if ($empty != "") {
  1991. if (is_array($empty)) {
  1992. $sql .= $empty . ')';
  1993. } else {
  1994. $sql .= ""$empty")";
  1995. }
  1996. } else {
  1997. $sql = "('')";
  1998. }
  1999. }
  2000. return $sql;
  2001. }
  2002. ###################### function trimstring() #######################
  2003. /* trim string to specified length */
  2004. function trimstring($string, $length, $dots='') {
  2005. if (strlen($string) > $length) { // trim word, remove broken words
  2006. $string = substr($string, 0, strrpos(substr($string, 0, $length), ' '));
  2007. if ($dots) { // add dots
  2008. $string .= '&nbsp;...';
  2009. }
  2010. }
  2011. return $string;
  2012. }
  2013. /*****************************************************
  2014. function make_pass
  2015. -----DESCRIPTION: -----------------------------------
  2016. Generate a random password of specified length.
  2017. -----ARGUMENTS: -------------------------------------
  2018. length Length of password to generate
  2019. -----RETURNS: ---------------------------------------
  2020. New random password.
  2021. *****************************************************/
  2022. function make_pass($length){ 
  2023.      
  2024.     $vowels = array("a", "e", "i", "o", "u"); 
  2025.     $cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr", 
  2026.     "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl"); 
  2027.      
  2028.     $num_vowels = count($vowels); 
  2029.     $num_cons = count($cons); 
  2030.      
  2031.     for($i = 0; $i < $length; $i++){ 
  2032.         $password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)]; 
  2033.     } 
  2034.      
  2035.     return substr($password, 0, $length); 
  2036. /*****************************************************
  2037. function make_username
  2038. -----DESCRIPTION: -----------------------------------
  2039. Generate a unique, valid username.
  2040. -----ARGUMENTS: -------------------------------------
  2041. email E-mail address
  2042. name [Optional] Requested name
  2043. -----RETURNS: ---------------------------------------
  2044. New username.
  2045. *****************************************************/
  2046. function make_username($email, $name='', $noquery = 0) {
  2047. /* 
  2048. generates a username from a name or email address
  2049. numbers are added to get a unique username
  2050. */
  2051. global $db;
  2052. $name = preg_replace('#[^a-z0-9_]#i', '', $name);
  2053. $i = 0;
  2054. if (strlen($name > 3)) {
  2055. $username_tmp = trim($name);
  2056. while (!$newusername) {
  2057. $i++;
  2058. if (!$noquery) {
  2059. $db->query("SELECT username FROM user WHERE username = '" . mysql_escape_string($username_tmp) . "'");
  2060. if ($db->num_rows()) {
  2061. $username_tmp = trim($name) . (int)$i;
  2062. } else {
  2063. $newusername = 1;
  2064. }
  2065. } else {
  2066. $newusername = 1;
  2067. }
  2068. }
  2069. } else {
  2070. // Grab everything leaving up to the @ and strip out everything
  2071. // that's not alphanumeric.
  2072. $username = substr($email, 0, strpos($email, '@'));
  2073. $username = preg_replace('([^_a-zA-Z0-9-.])', '', $username);
  2074. $username_tmp = trim($username);
  2075. while (!$newusername) {
  2076. $i++;
  2077. if (!$noquery) {
  2078. $db->query("SELECT username FROM user WHERE username = '" . mysql_escape_string($username_tmp) . "'");
  2079. if ($db->num_rows()) {
  2080. $username_tmp = trim($username) . (int)$i;
  2081. } else {
  2082. $newusername = 1;
  2083. }
  2084. } else {
  2085. $newusername = 1;
  2086. }
  2087. }
  2088. }
  2089. return $username_tmp;
  2090. }
  2091. /*****************************************************
  2092. function applyFunction
  2093. -----DESCRIPTION: -----------------------------------
  2094. - recurisvly parse a function
  2095. -----ARGUMENTS: -------------------------------------
  2096. input : the variable
  2097. function : function name
  2098. -----RETURNS:----------------------------------------
  2099. The array
  2100. *****************************************************/
  2101. function applyFunction($input, $function) {
  2102. if (is_array($input)) {
  2103. foreach ($input as $key => $value) {
  2104. $input[$key] = applyFunction($input[$key], $function);
  2105. }
  2106. } else {
  2107. $input = $function($input);
  2108. }
  2109. return $input;
  2110. }
  2111. #################################################################################################
  2112. #  ATTACHMENT FUNCTIONS
  2113. #################################################################################################
  2114. /*****************************************************
  2115. function add_attachment
  2116. -----DESCRIPTION: -----------------------------------
  2117. - adds attachment to the blob table. In future this will be the point to extend attachments to support filesystem storage
  2118. -----ARGUMENTS: -------------------------------------
  2119. $name : the name of the attachment (if it is not $_FILES['attachment'])
  2120. -----GLOBALS:----------------------------------------
  2121. $_FILES : array of files submitted by the browser
  2122. -----RETURNS:----------------------------------------
  2123. An array of details about the attachment (for storage in an explanatory table)
  2124. *****************************************************/
  2125. function add_attachment($name='') {
  2126. global $_FILES, $db;
  2127. if (!$name) {
  2128. $name = "attachment";
  2129. }
  2130. $attachment = trim($_FILES[$name]['tmp_name']);
  2131. $attachment_name = trim($_FILES[$name]['name']);
  2132. $attachment_size = trim($_FILES[$name]['size']);
  2133. $attachment_pointer = @fopen($attachment, 'rb');
  2134. $attachment_data = @fread($attachment_pointer, $attachment_size);
  2135. @fclose($attachment_pointer);
  2136. @unlink($attachment);
  2137. $db->query("INSERT INTO blobs SET blobdata = '" . mysql_escape_string($attachment_data) . "'");
  2138. $id = $db->last_id();
  2139. $attach = array(
  2140. 'data' => $attachment_data,
  2141. 'name' => $attachment_name,
  2142. 'extension' => attachment_extension($attachment_name),
  2143. 'blobid' => $id,
  2144. 'size' => $attachment_size
  2145. );
  2146. return $attach;
  2147. }
  2148. /*****************************************************
  2149. function get_attachment
  2150. -----DESCRIPTION: -----------------------------------
  2151. - gets attachment
  2152. -----ARGUMENTS: -------------------------------------
  2153. id : blobid
  2154. table : the table name where the extension and name of the file are stored
  2155. display : display instead of downloading. Will only work for certain attachment extensions
  2156. -----RETURNS:----------------------------------------
  2157. Directly prints to browser either a filestream or a page with the attachment
  2158. *****************************************************/
  2159. function get_attachment($id, $table, $display = '') {
  2160. global $db;
  2161. require_once(INCLUDE_PATH . 'data/mimetypes.php'); // gets the mime types
  2162. $attachment = $db->query_return("SELECT * FROM $table WHERE id = '" . intval($id) . "'");
  2163. if (!(strstr($HTTP_USER_AGENT, 'MSIE'))) {
  2164. $atachment = ' atachment;';
  2165. } else {
  2166. $atachment = '';
  2167. }
  2168. if (in_array($attachment[extension], $mimetypes)) {
  2169. $mimetype = $mimetypes[$attachment[extension]];
  2170. } else {
  2171. $mimetype = 'application/download';
  2172. }
  2173. // get the data. This can later be extended to read from a file
  2174. $data = $db->query_return("SELECT * FROM blobs WHERE id = '" . intval($attachment[blobid]) . "'");
  2175. // display in browser (needs implementing)
  2176. $display_extensions = array('html', 'htm', 'txt');
  2177. if ($display AND @in_array($attachment[extension], $display_extensions)) { 
  2178. header('Content-Type: ' . $mimetype . '; name=' . $attachment[filename]);
  2179. header('Content-Disposition: inline; filename='  . $attachment[filename]);
  2180. } else {
  2181. header('Cache-control: max-age=31536000');
  2182. header('Expires: '. gmdate('D, d M Y H:i:s', TIMENOW + 31536000) . 'GMT');
  2183. header('Last-Modified: '. gmdate("D, d M Y H:i:s", $mail['dateline']) . 'GMT');
  2184. header('Content-Length: ' . $attachment[filesize]);
  2185. header('Content-Disposition: attachment; filename=' . $attachment[filename]);
  2186. header('Content-Type: ' . $mimetype . '; name=' . $attachment[filename]);
  2187. }
  2188. echo $data[blobdata];
  2189. exit();
  2190. }
  2191. /*****************************************************
  2192. function explode_empty
  2193. -----DESCRIPTION: -----------------------------------
  2194. - explode a string into an array but remove any
  2195. emtyy elements and do not return an array if no matches
  2196. -----ARGUMENTS: -------------------------------------
  2197. $split : what we are splitting on
  2198. $string : the string
  2199. -----RETURNS:----------------------------------------
  2200. array or null
  2201. *****************************************************/
  2202. function explode_empty($split, $string) {
  2203. $tmp = explode($split, $string);
  2204. foreach ($tmp AS $key => $var) {
  2205. if ($var != '') {
  2206. $array[] = $var;
  2207. }
  2208. }
  2209. return $array;
  2210. }
  2211. /*****************************************************
  2212. function validate_attachment
  2213. -----DESCRIPTION: -----------------------------------
  2214. - validates attachments
  2215. - can take account of max size, disallowed extension and allowed extensions
  2216. -----ARGUMENTS: -------------------------------------
  2217. &error : an error code is returned upon failure
  2218. $name : the name of the attachment (if it is not $_FILES['attachment'])
  2219. $maxsize : a maximum size for the attachment (in MB)
  2220. $allowed_attachments : an array of allowed attachments. If set the extension must be in this array
  2221. $disabled_attachments : an array of disabled attachments (e.g. .exe)
  2222. -----GLOBALS:----------------------------------------
  2223. $_FILES : array of files submitted by the browser
  2224. -----RETURNS:----------------------------------------
  2225. 1 for a valid attachment
  2226. 0 for an invalid attachment
  2227. &error for the error code if the attachment is invalid
  2228. 1 : too big
  2229. 2 : extension not in allowed list
  2230. 3 : extension in disabled list
  2231. 4 : failed is_uploaded_file check
  2232. 5 : failed check on attachment name (ie no attachment uploaded)
  2233. 6 : failed secutity filesize check
  2234. *****************************************************/
  2235. function validate_attachment(&$error, $name='', $maxsize='', $allowed_attachments='', $disabled_attachments='') {
  2236. global $db, $_FILES, $settings;
  2237. // allowed / disabled attachments
  2238. if (defined('USERZONE')) {
  2239. if (!$settings['attachments_user']) {
  2240. return 0;
  2241. }
  2242. give_default($allowed_attachments, $settings[tech_extensions_allowed]);
  2243. give_default($disabled_attachments, $settings[tech_disabled_attachments]);
  2244. give_default($maxsize, $settings[attachments_user_size]);
  2245. }
  2246. if (defined('TECHZONE')) {
  2247. give_default($allowed_attachments, $settings[user_extensions_allowed]);
  2248. give_default($disabled_attachments, $settings[user_disabled_attachments]);
  2249. give_default($maxsize, $settings[attachments_tech_size]);
  2250. }
  2251. // need to convert from MB to bytes
  2252. $maxsize = $maxsize * 1024 * 1024;
  2253. if (!is_array($allowed_attachments)) {
  2254. $allowed_attachments = explode_empty(',', $allowed_attachments);
  2255. }
  2256. if (!is_array($disabled_attachments)) {
  2257. $disabled_attachments = explode_empty(',', $disabled_attachments);
  2258. }
  2259. if (!$name) {
  2260. $name = "attachment";
  2261. }
  2262.     $attachment = trim($_FILES[$name]['tmp_name']);
  2263.     $attachment_name = trim($_FILES[$name]['name']);
  2264.     $attachment_size = trim($_FILES[$name]['size']);
  2265.     if ($attachment == 'none' or !$attachment or !$attachment_name) {
  2266. @unlink($attachment);
  2267. $error = 5;
  2268.         return 0;
  2269.     }
  2270.     if (!is_uploaded_file($attachment)) {
  2271. @unlink($attachment);
  2272. $error = 4;
  2273.         return 0;
  2274.     }
  2275. // enabled & disabled extensions
  2276. $extension = attachment_extension($attachment_name);
  2277. if (is_array($allowed_attachments)) {
  2278. if (!(in_array($extension, $allowed_attachments))) {
  2279. $error = 2;
  2280. @unlink($attachment);
  2281. return 0;
  2282. }
  2283. }
  2284. if (is_array($disabled_attachments)) {
  2285. if (in_array($extension, $disabled_attachments)) {
  2286. $error = 2;
  2287. @unlink($attachment);
  2288. return 0;
  2289. }
  2290. }
  2291. // do size checks
  2292. $filesize = filesize($attachment);
  2293.     if ($maxsize != 0 and $filesize > $maxsize) {
  2294.         @unlink($attachment);
  2295. $error = 1;
  2296. return 0;
  2297.     }
  2298. if ($attachment_size != $filesize) {
  2299. @unlink($attachment);
  2300. $error = 6;
  2301. return 0;
  2302. }
  2303. return 1;
  2304. }
  2305. ###################### function filesize_display #######################
  2306. function filesize_display($filesize) {
  2307. // sort out decimal places etc.
  2308. $end = " bytes";
  2309. if ($filesize > 103) {
  2310. $filesize = $filesize / 1024;
  2311. $end = " kb";
  2312. }
  2313. if ($filesize > 500) {
  2314. $filesize = $filesize / 1024;
  2315. $end = " mb";
  2316. }
  2317. $filesize = round($filesize, 1);
  2318. return $filesize . $end;
  2319. }
  2320. ###################### function getextension #######################
  2321. function attachment_extension($filename) {
  2322.     return substr(strrchr(strtolower($filename), '.'), 1);
  2323. }
  2324. /*****************************************************
  2325. function make_ticket_ref
  2326. -----DESCRIPTION: -----------------------------------
  2327. Generate a unique, valid ticketref.
  2328. -----ARGUMENTS: -------------------------------------
  2329. table [Optional] Table to check against.
  2330. -----RETURNS: ---------------------------------------
  2331. New ticketref.
  2332. *****************************************************/
  2333. function make_ticket_ref($table = 'ticket') {
  2334. $db = new_db_class();
  2335. $ref_tmp = make_ref();
  2336. while (!$ref_done) {
  2337. $db->query("SELECT ref FROM $table WHERE ref = '" . mysql_escape_string($ref_tmp) . "'");
  2338. if ($db->num_rows()) {
  2339. $ref_tmp = make_ref();
  2340. } else {
  2341. $ref_done = 1;
  2342. return($ref_tmp);
  2343. }
  2344. }
  2345. }
  2346. ###################### FUCTION check_license() ############################
  2347. function check_license() {
  2348. ?>
  2349. <form method="post" action="#">
  2350. <input type="hidden" name="encrypt_license" value="%%%md5license%%%">
  2351. <input type="submit" name="submit" value="Click here to validate license">
  2352. </form>
  2353. <?php
  2354. exit();
  2355. }
  2356. ###################### FUCTION force_refs() ############################
  2357. ## Create object REFs for objects that don't have any in the given table
  2358. /*****************************************************
  2359. function force_refs
  2360. -----DESCRIPTION: -----------------------------------
  2361. Generate new ticketrefs for any ticket that doesn't
  2362. have one.
  2363. -----ARGUMENTS: -------------------------------------
  2364. table [Optional] Table to check against.
  2365. -----RETURNS: ---------------------------------------
  2366. None.
  2367. *****************************************************/
  2368. function force_refs($table = 'ticket') {
  2369. $db = new_db_class();
  2370. $db->query("SELECT id FROM $table WHERE !ref");
  2371. while ($res = $db->row_array()) {
  2372. $process[] = $res['id'];
  2373. }
  2374. foreach($process AS $id) {
  2375. $db->query("UPDATE $table SET ref = '" . mysql_escape_string(make_ticket_ref($table)) . "' WHERE id = '$id'");
  2376. }
  2377. }
  2378. /*****************************************************
  2379. function make_ref
  2380. -----DESCRIPTION: -----------------------------------
  2381. Generate a random ticketref.
  2382. -----ARGUMENTS: -------------------------------------
  2383. None.
  2384. -----RETURNS: ---------------------------------------
  2385. None.
  2386. *****************************************************/
  2387. function make_ref() {
  2388. /* used to make a NNNN-AAAA-NNNN reference */
  2389. $alpha = array('Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M');
  2390. // get first 4 digit number (we don't want to start with 0)
  2391. $bit = substr(mt_rand(1000,999999999), 0, 4);
  2392. // get 4 alpha key
  2393. $alpha_keys = array_rand($alpha, 4);
  2394. $bit .= '-' . $alpha[$alpha_keys[0]] . $alpha[$alpha_keys[1]] . $alpha[$alpha_keys[2]] . $alpha[$alpha_keys[3]];
  2395. // get another 4 digit random number
  2396. $bit .= '-' . substr(mt_rand(10000000,99999999999), 3, 4);
  2397. return $bit;
  2398. }
  2399. /***************************************************
  2400. FUNCTION clean_time()
  2401. Return a "cleaned up" representation of a unix timestamp,
  2402. in the form of "2 days, 43 hours, 53 minutes, 43 seconds".
  2403. ----------------------------------------------------
  2404. Arguments:
  2405. time: Unix timestamp
  2406. ----------------------------------------------------
  2407. Returns:
  2408. Human-readable representation of timestamp.
  2409. ****************************************************/
  2410. function clean_time($time, $short = NULL) {
  2411. $days = (int)($time / (24 * 60 * 60)); // Number of days
  2412. $time = $time - ($days * (24 * 60 * 60));
  2413. $hours = (int)($time / (60 * 60)); // Number of hours
  2414. $time = $time - ($hours * (60 * 60));
  2415. $minutes = (int)($time / 60); // Number of minutes
  2416. $seconds = (int)($time - ($minutes * 60));
  2417. unset ($time);
  2418. if ($days) {
  2419. if ($short) {
  2420. $time .= $days . 'd ';
  2421. } else {
  2422. if ($days > 1) {
  2423. $time .= "$days days ";
  2424. } else {
  2425. $time .= '1 day ';
  2426. }
  2427. }
  2428. }
  2429. if ($hours) {
  2430. if ($short) {
  2431. $time .= $hours . 'h ';
  2432. } else {
  2433. if ($hours > 1) {
  2434. $time .= "$hours hours ";
  2435. } else {
  2436. $time .= '1 hour ';
  2437. }
  2438. }
  2439. }
  2440. if ($minutes) {
  2441. if ($short) {
  2442. $time .= $minutes . 'm ';
  2443. } else {
  2444. if ($minutes > 1) {
  2445. $time .= "$minutes minutes ";
  2446. } else {
  2447. $time .= '1 minute ';
  2448. }
  2449. }
  2450. }
  2451. if ($seconds) {
  2452. if ($short) {
  2453. $time .= $seconds . 's ';
  2454. } else {
  2455. if ($seconds > 1) {
  2456. $time .= "$seconds seconds ";
  2457. } else {
  2458. $time .= '1 second ';
  2459. }
  2460. }
  2461. }
  2462. if (!strlen($time)) {
  2463. if ($short) {
  2464. $time = '0s';
  2465. } else {
  2466. $time = "0 seconds";
  2467. }
  2468. }
  2469. return $time;
  2470. }
  2471. /***********************************************************
  2472. function validate_ymd
  2473. ---- DESCRIPTION -------------------------------------------
  2474. - Validates the provided date
  2475. ---- ARGUMENTS ---------------------------------------------
  2476. date : Date to be validated, in YYYY-MM-DD format
  2477. ---- RETURNS -----------------------------------------------
  2478. NULL if the date is invalid, non-null otherwise.
  2479. ***********************************************************/
  2480. function validate_ymd($date) {
  2481. $orig = $date;
  2482. $date = explode('-', $date);
  2483. if (@checkdate($date[1], $date[2], $date[0])) {
  2484. return $orig;
  2485. } else {
  2486. return 0;
  2487. }
  2488. }
  2489. /*****************************************************
  2490. function user_expired
  2491. -----DESCRIPTION: -----------------------------------
  2492. Determines whether the given user has expired or not.
  2493. -----ARGUMENTS: -------------------------------------
  2494. userid User to check.
  2495. -----RETURNS: ---------------------------------------
  2496. False if the user has not expired.
  2497. An array, containing two elements (ticket count,
  2498. and expiration date), if expired.
  2499. *****************************************************/
  2500. function user_expired($userid) {
  2501. global $db;
  2502. $user = $db->query_return("SELECT * FROM user WHERE id = '$userid'");
  2503. if (!$db->num_rows()) {
  2504. return -1;
  2505. }
  2506. $user_expired_tickets = 0;
  2507. $user_expired_date = 0;
  2508. $total = $db->query_return("SELECT count(*) AS total FROM ticket WHERE userid = '$user[id]'");
  2509. $total = $total['total'];
  2510. if (($total >= $user['expire_tickets']) AND $user['expire_tickets']) {
  2511. $user_expired = 1;
  2512. $user_expired_tickets = $total;
  2513. }
  2514. if ((mktime() >= $user['expire_date']) AND $user['expire_date']) {
  2515. $user_expired = 1;
  2516. $user_expired_date = our_date($user['expire_date']);
  2517. }
  2518. if ($user_expired) {
  2519. return array($user_expired_tickets, $user_expired_date);
  2520. } else {
  2521. return 0;
  2522. }
  2523. }
  2524. /***********************************************************
  2525. function make_timezone
  2526. ---- DESCRIPTION -------------------------------------------
  2527. - Returns an HTML select form widget containing lots of
  2528.    time zones.
  2529. ***********************************************************/
  2530. function make_timezone($timezone = '0') {
  2531. $zone = array(
  2532. '-12' => "(GMT - 12:00 hours) Enitwetok, Kwajalien",
  2533. '-11' => "(GMT - 11:00 hours) Midway Island, Samoa",
  2534. '-10' => "(GMT - 10:00 hours) Hawaii",
  2535. '-9' => "(GMT - 9:00 hours) Alaska",
  2536. '-8' => "(GMT - 8:00 hours) Pacific Time (US & Canada)",
  2537. '-7' => "(GMT - 7:00 hours) Mountain Time (US & Canada)",
  2538. '-6' => "(GMT - 6:00 hours) Central Time (US & Canada)",
  2539. '-5' => "(GMT - 5:00 hours) Eastern Time (US & Canada), Bogota, Lima, Quito",
  2540. '-4' => "(GMT - 4:00 hours) Atlantic Time (Canada), Caracas, La Paz",
  2541. '-3.5' => "(GMT - 3:30 hours) Newfoundland (NST)",
  2542. '-3' => "(GMT - 3:00 hours) Brazil, Buenos Aires, Georgetown, Falkland Is.",
  2543. '-2' => "(GMT - 2:00 hours) Mid-Atlantic, Ascention Is., St Helena",
  2544. '-1' => "(GMT - 1:00 hours) Azores, Cape Verde Islands",
  2545. '0' => "(GMT) Casablanca, Dublin, Edinburgh, London, Lisbon, Monrovia",
  2546. '1' => "(GMT + 1:00 hours) Berlin, Brussels, Copenhagen, Madrid, Paris, Rome",
  2547. '2' => "(GMT + 2:00 hours) Kaliningrad, South Africa, Warsaw",
  2548. '3' => "(GMT + 3:00 hours) Baghdad, Riyadh, Moscow, Nairobi",
  2549. '4' => "(GMT + 4:00 hours) Adu Dhabi, Baku, Muscat, Tbilisi",
  2550. '5' => "(GMT + 5:00 hours) Ekaterinburg, Islamabad, Karachi, Tashkent",
  2551. '6' => "(GMT + 6:00 hours) Almaty, Colomba, Dhakra",
  2552. '7' => "(GMT + 7:00 hours) Bangkok, Hanoi, Jakarta",
  2553. '8' => "(GMT + 8:00 hours) Beijing, Hong Kong, Perth, Singapore, Taipei",
  2554. '9' => "(GMT + 9:00 hours) Osaka, Sapporo, Seoul, Tokyo, Yakutsk",
  2555. '9.5' => "(GMT +9:30 hours) Adelaide",
  2556. '10' => "(GMT + 10:00 hours) Melbourne, Papua New Guinea, Sydney, Vladivostok",
  2557. '11' => "(GMT + 11:00 hours) Magadan, New Caledonia, Solomon Islands",
  2558. '12' => "(GMT + 12:00 hours) Auckland, Wellington, Fiji, Marshall Island"
  2559. );
  2560. if (defined('TECHZONE')) {
  2561. return form_select('timezone', $zone, '', $timezone);
  2562. } else {
  2563. return form_select('timezone', $zone, $timezone);
  2564. }
  2565. }
  2566. /***********************************************************
  2567. function is_win
  2568. ---- DESCRIPTION -------------------------------------------
  2569. - Returns true if running on a Windows platform, false if
  2570.    running on a Unix or Unix-like platform.
  2571. ***********************************************************/
  2572.  
  2573. function is_win() {
  2574. return (substr(PHP_OS, 0, 3) == "WIN");
  2575. }
  2576. /***********************************************************
  2577. function xss_check()
  2578. ---- DESCRIPTION -------------------------------------------
  2579. Checks the current technician's permissions, and if he's
  2580. not permitted to submit raw HTML directly into fields
  2581. calls htmlspecialchars_uni() on the value passed in and returns
  2582. the result, otherwise returns the passed-in value.
  2583. Tech interface function only.
  2584. ---- ARGUMENTS ---------------------------------------------
  2585. value The value submitted by the tech to be checked;
  2586. can be an array or a string
  2587. type Type of permission being checked
  2588. user User interface data check
  2589. tech Tech interface data check
  2590. ---- RETURNS -----------------------------------------------
  2591. The value (or values), altered with htmlentities if
  2592. needed by the tech's permissions. If value is a string,
  2593. returns a string. If the value is an array, returns
  2594. an array containing processed values.
  2595. ***********************************************************/
  2596. function xss_check($value, $type = 'user') {
  2597. global $user;
  2598. if ($type == 'user') {
  2599. $perm = $user['p_html_user'];
  2600. } else {
  2601. $perm = $user['p_html_tech'];
  2602. }
  2603. if ($perm) {
  2604. return $value;
  2605. } else {
  2606. if (is_array($value)) {
  2607. foreach ($value AS $val) {
  2608. $tmpval[] = htmlspecialchars_uni($val);
  2609. }
  2610. $value = $tmpval;
  2611. } else {
  2612. $value = htmlspecialchars_uni($value);
  2613. }
  2614. }
  2615. return $value;
  2616. }
  2617. /***********************************************************
  2618. function htmlspecialchars_uni($text)
  2619. ---- DESCRIPTION -------------------------------------------
  2620. htmlspecialchars() but works with unicode text
  2621. ***********************************************************/
  2622. function htmlspecialchars_uni($text) {
  2623. $text = preg_replace('/&(?!#[0-9]+;)/si', '&amp;', $text);
  2624. return str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $text);
  2625. }