PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/admin_functions.inc.php

https://gitlab.com/santiagosony/Mods-for-HESK
PHP | 709 lines | 443 code | 153 blank | 113 comment | 78 complexity | f3e8473e199ebc9da0aa38876889582f MD5 | raw file
  1. <?php
  2. /*******************************************************************************
  3. * Title: Help Desk Software HESK
  4. * Version: 2.6.7 from 18th April 2016
  5. * Author: Klemen Stirn
  6. * Website: http://www.hesk.com
  7. ********************************************************************************
  8. * COPYRIGHT AND TRADEMARK NOTICE
  9. * Copyright 2005-2015 Klemen Stirn. All Rights Reserved.
  10. * HESK is a registered trademark of Klemen Stirn.
  11. * The HESK may be used and modified free of charge by anyone
  12. * AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT.
  13. * By using this code you agree to indemnify Klemen Stirn from any
  14. * liability that might arise from it's use.
  15. * Selling the code for this program, in part or full, without prior
  16. * written consent is expressly forbidden.
  17. * Using this code, in part or full, to create derivate work,
  18. * new scripts or products is expressly forbidden. Obtain permission
  19. * before redistributing this software over the Internet or in
  20. * any other medium. In all cases copyright and header must remain intact.
  21. * This Copyright is in full effect in any country that has International
  22. * Trade Agreements with the United States of America or
  23. * with the European Union.
  24. * Removing any of the copyright notices without purchasing a license
  25. * is expressly forbidden. To remove HESK copyright notice you must purchase
  26. * a license for this script. For more information on how to obtain
  27. * a license please visit the page below:
  28. * https://www.hesk.com/buy.php
  29. *******************************************************************************/
  30. /* Check if this is a valid include */
  31. if (!defined('IN_SCRIPT')) {
  32. die('Invalid attempt');
  33. }
  34. // Possible fields to be displayed in ticket list
  35. $hesk_settings['possible_ticket_list'] = array(
  36. 'id' => $hesklang['id'],
  37. 'trackid' => $hesklang['trackID'],
  38. 'dt' => $hesklang['submitted'],
  39. 'lastchange' => $hesklang['last_update'],
  40. 'category' => $hesklang['category'],
  41. 'name' => $hesklang['name'],
  42. 'email' => $hesklang['email'],
  43. 'subject' => $hesklang['subject'],
  44. 'status' => $hesklang['status'],
  45. 'owner' => $hesklang['owner'],
  46. 'replies' => $hesklang['replies'],
  47. 'staffreplies' => $hesklang['replies'] . ' (' . $hesklang['staff'] . ')',
  48. 'lastreplier' => $hesklang['last_replier'],
  49. 'time_worked' => $hesklang['ts'],
  50. );
  51. // Also possible to display all custom fields
  52. for ($i = 1; $i <= 20; $i++) {
  53. if ($hesk_settings['custom_fields']['custom' . $i]['use']) {
  54. $hesk_settings['possible_ticket_list']['custom' . $i] = $hesk_settings['custom_fields']['custom' . $i]['name'];
  55. }
  56. }
  57. /*** FUNCTIONS ***/
  58. function hesk_show_column($column)
  59. {
  60. global $hesk_settings;
  61. return in_array($column, $hesk_settings['ticket_list']) ? true : false;
  62. } // END hesk_show_column()
  63. function hesk_getHHMMSS($in)
  64. {
  65. $in = hesk_getTime($in);
  66. return explode(':', $in);
  67. } // END hesk_getHHMMSS();
  68. function hesk_getTime($in)
  69. {
  70. $in = trim($in);
  71. /* If everything is OK this simple check should return true */
  72. if (preg_match('/^([0-9]{2,3}):([0-5][0-9]):([0-5][0-9])$/', $in)) {
  73. return $in;
  74. }
  75. /* No joy, let's try to figure out the correct values to use... */
  76. $h = 0;
  77. $m = 0;
  78. $s = 0;
  79. /* How many parts do we have? */
  80. $parts = substr_count($in, ':');
  81. switch ($parts) {
  82. /* Only two parts, let's assume minutes and seconds */
  83. case 1:
  84. list($m, $s) = explode(':', $in);
  85. break;
  86. /* Three parts, so explode to hours, minutes and seconds */
  87. case 2:
  88. list($h, $m, $s) = explode(':', $in);
  89. break;
  90. /* Something other was entered, let's assume just minutes */
  91. default:
  92. $m = $in;
  93. }
  94. /* Make sure all inputs are integers */
  95. $h = intval($h);
  96. $m = intval($m);
  97. $s = intval($s);
  98. /* Convert seconds to minutes if 60 or more seconds */
  99. if ($s > 59) {
  100. $m = floor($s / 60) + $m;
  101. $s = intval($s % 60);
  102. }
  103. /* Convert minutes to hours if 60 or more minutes */
  104. if ($m > 59) {
  105. $h = floor($m / 60) + $h;
  106. $m = intval($m % 60);
  107. }
  108. /* MySQL accepts max time value of 838:59:59 */
  109. if ($h > 838) {
  110. return '838:59:59';
  111. }
  112. /* That's it, let's send out formatted time string */
  113. return str_pad($h, 2, "0", STR_PAD_LEFT) . ':' . str_pad($m, 2, "0", STR_PAD_LEFT) . ':' . str_pad($s, 2, "0", STR_PAD_LEFT);
  114. } // END hesk_getTime();
  115. function hesk_mergeTickets($merge_these, $merge_into)
  116. {
  117. global $hesk_settings, $hesklang, $hesk_db_link;
  118. /* Target ticket must not be in the "merge these" list */
  119. if (in_array($merge_into, $merge_these)) {
  120. $merge_these = array_diff($merge_these, array($merge_into));
  121. }
  122. /* At least 1 ticket needs to be merged with target ticket */
  123. if (count($merge_these) < 1) {
  124. $_SESSION['error'] = $hesklang['merr1'];
  125. return false;
  126. }
  127. /* Make sure target ticket exists */
  128. $res = hesk_dbQuery("SELECT `id`,`trackid`,`category` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "tickets` WHERE `id`='" . intval($merge_into) . "' LIMIT 1");
  129. if (hesk_dbNumRows($res) != 1) {
  130. $_SESSION['error'] = $hesklang['merr2'];
  131. return false;
  132. }
  133. $ticket = hesk_dbFetchAssoc($res);
  134. /* Make sure user has access to ticket category */
  135. if (!hesk_okCategory($ticket['category'], 0)) {
  136. $_SESSION['error'] = $hesklang['merr3'];
  137. return false;
  138. }
  139. /* Set some variables for later */
  140. $merge['attachments'] = '';
  141. $merge['replies'] = array();
  142. $merge['notes'] = array();
  143. $sec_worked = 0;
  144. $history = '';
  145. $merged = '';
  146. /* Get messages, replies, notes and attachments of tickets that will be merged */
  147. foreach ($merge_these as $this_id) {
  148. /* Validate ID */
  149. if (is_array($this_id)) {
  150. continue;
  151. }
  152. $this_id = intval($this_id) or hesk_error($hesklang['id_not_valid']);
  153. /* Get required ticket information */
  154. $res = hesk_dbQuery("SELECT `id`,`trackid`,`category`,`name`,`message`,`dt`,`time_worked`,`attachments` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "tickets` WHERE `id`='" . intval($this_id) . "' LIMIT 1");
  155. if (hesk_dbNumRows($res) != 1) {
  156. continue;
  157. }
  158. $row = hesk_dbFetchAssoc($res);
  159. /* Has this user access to the ticket category? */
  160. if (!hesk_okCategory($row['category'], 0)) {
  161. continue;
  162. }
  163. /* Insert ticket message as a new reply to target ticket */
  164. hesk_dbQuery("INSERT INTO `" . hesk_dbEscape($hesk_settings['db_pfix']) . "replies` (`replyto`,`name`,`message`,`dt`,`attachments`) VALUES ('" . intval($ticket['id']) . "','" . hesk_dbEscape($row['name']) . "','" . hesk_dbEscape($row['message']) . "','" . hesk_dbEscape($row['dt']) . "','" . hesk_dbEscape($row['attachments']) . "')");
  165. /* Update attachments */
  166. hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "attachments` SET `ticket_id`='" . hesk_dbEscape($ticket['trackid']) . "' WHERE `ticket_id`='" . hesk_dbEscape($row['trackid']) . "'");
  167. /* Get old ticket replies and insert them as new replies */
  168. $res = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "replies` WHERE `replyto`='" . intval($row['id']) . "' ORDER BY `id` ASC");
  169. while ($reply = hesk_dbFetchAssoc($res)) {
  170. hesk_dbQuery("INSERT INTO `" . hesk_dbEscape($hesk_settings['db_pfix']) . "replies` (`replyto`,`name`,`message`,`dt`,`attachments`,`staffid`,`rating`,`read`) VALUES ('" . intval($ticket['id']) . "','" . hesk_dbEscape($reply['name']) . "','" . hesk_dbEscape($reply['message']) . "','" . hesk_dbEscape($reply['dt']) . "','" . hesk_dbEscape($reply['attachments']) . "','" . intval($reply['staffid']) . "','" . intval($reply['rating']) . "','" . intval($reply['read']) . "')");
  171. }
  172. /* Delete replies to the old ticket */
  173. hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "replies` WHERE `replyto`='" . intval($row['id']) . "'");
  174. /* Get old ticket notes and insert them as new notes */
  175. $res = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "notes` WHERE `ticket`='" . intval($row['id']) . "' ORDER BY `id` ASC");
  176. while ($note = hesk_dbFetchAssoc($res)) {
  177. hesk_dbQuery("INSERT INTO `" . hesk_dbEscape($hesk_settings['db_pfix']) . "notes` (`ticket`,`who`,`dt`,`message`,`attachments`) VALUES ('" . intval($ticket['id']) . "','" . intval($note['who']) . "','" . hesk_dbEscape($note['dt']) . "','" . hesk_dbEscape($note['message']) . "','" . hesk_dbEscape($note['attachments']) . "')");
  178. }
  179. /* Delete replies to the old ticket */
  180. hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "notes` WHERE `ticket`='" . intval($row['id']) . "'");
  181. /* Delete old ticket */
  182. hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "tickets` WHERE `id`='" . intval($row['id']) . "'");
  183. /* Log that ticket has been merged */
  184. $history .= sprintf($hesklang['thist13'], hesk_date(), $row['trackid'], $_SESSION['name'] . ' (' . $_SESSION['user'] . ')');
  185. /* Add old ticket ID to target ticket "merged" field */
  186. $merged .= '#' . $row['trackid'];
  187. /* Convert old ticket "time worked" to seconds and add to $sec_worked variable */
  188. list ($hr, $min, $sec) = explode(':', $row['time_worked']);
  189. $sec_worked += (((int)$hr) * 3600) + (((int)$min) * 60) + ((int)$sec);
  190. }
  191. /* Convert seconds to HHH:MM:SS */
  192. $sec_worked = hesk_getTime('0:' . $sec_worked);
  193. // Get number of replies
  194. $total = 0;
  195. $staffreplies = 0;
  196. $res = hesk_dbQuery("SELECT COUNT(*) as `cnt`, `staffid` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "replies` WHERE `replyto`=" . intval($ticket['id']) . " GROUP BY CASE WHEN `staffid` = 0 THEN 0 ELSE 1 END ASC");
  197. while ($row = hesk_dbFetchAssoc($res)) {
  198. $total += $row['cnt'];
  199. $staffreplies += ($row['staffid'] ? $row['cnt'] : 0);
  200. }
  201. $replies_sql = " `replies`={$total}, `staffreplies`={$staffreplies} , ";
  202. // Get first staff reply
  203. if ($staffreplies) {
  204. $res = hesk_dbQuery("SELECT `dt`, `staffid` FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "replies` WHERE `replyto`=" . intval($ticket['id']) . " AND `staffid`>0 ORDER BY `dt` ASC LIMIT 1");
  205. $reply = hesk_dbFetchAssoc($res);
  206. $replies_sql .= " `firstreply`='" . hesk_dbEscape($reply['dt']) . "', `firstreplyby`=" . intval($reply['staffid']) . " , ";
  207. }
  208. /* Update history (log) and merged IDs of target ticket */
  209. hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "tickets` SET $replies_sql `time_worked`=ADDTIME(`time_worked`, '" . hesk_dbEscape($sec_worked) . "'), `merged`=CONCAT(`merged`,'" . hesk_dbEscape($merged . '#') . "'), `history`=CONCAT(`history`,'" . hesk_dbEscape($history) . "') WHERE `id`='" . intval($merge_into) . "' LIMIT 1");
  210. return true;
  211. } // END hesk_mergeTickets()
  212. function hesk_updateStaffDefaults()
  213. {
  214. global $hesk_settings, $hesklang;
  215. // Demo mode
  216. if (defined('HESK_DEMO')) {
  217. return true;
  218. }
  219. // Remove the part that forces saving as default - we don't need it every time
  220. $default_list = str_replace('&def=1', '', $_SERVER['QUERY_STRING']);
  221. // Update database
  222. $res = hesk_dbQuery("UPDATE `" . hesk_dbEscape($hesk_settings['db_pfix']) . "users` SET `default_list`='" . hesk_dbEscape($default_list) . "' WHERE `id`='" . intval($_SESSION['id']) . "'");
  223. // Update session values so the changes take effect immediately
  224. $_SESSION['default_list'] = $default_list;
  225. return true;
  226. } // END hesk_updateStaffDefaults()
  227. function hesk_makeJsString($in)
  228. {
  229. return addslashes(preg_replace("/\s+/", ' ', $in));
  230. } // END hesk_makeJsString()
  231. function hesk_checkNewMail()
  232. {
  233. global $hesk_settings, $hesklang;
  234. $res = hesk_dbQuery("SELECT COUNT(*) FROM `" . hesk_dbEscape($hesk_settings['db_pfix']) . "mail` WHERE `to`='" . intval($_SESSION['id']) . "' AND `read`='0' AND `deletedby`!='" . intval($_SESSION['id']) . "' ");
  235. $num = hesk_dbResult($res, 0, 0);
  236. return $num;
  237. } // END hesk_checkNewMail()
  238. function hesk_getCategoriesArray($kb = 0)
  239. {
  240. global $hesk_settings, $hesklang, $hesk_db_link;
  241. $categories = array();
  242. if ($kb) {
  243. $result = hesk_dbQuery('SELECT `id`, `name` FROM `' . hesk_dbEscape($hesk_settings['db_pfix']) . 'kb_categories` ORDER BY `cat_order` ASC');
  244. } else {
  245. $result = hesk_dbQuery('SELECT `id`, `name` FROM `' . hesk_dbEscape($hesk_settings['db_pfix']) . 'categories` ORDER BY `cat_order` ASC');
  246. }
  247. while ($row = hesk_dbFetchAssoc($result)) {
  248. $categories[$row['id']] = $row['name'];
  249. }
  250. return $categories;
  251. } // END hesk_getCategoriesArray()
  252. function hesk_getHTML($in)
  253. {
  254. global $hesk_settings, $hesklang;
  255. $replace_from = array("\t", "<?", "?>", "$", "<%", "%>");
  256. $replace_to = array("", "&lt;?", "?&gt;", "\$", "&lt;%", "%&gt;");
  257. $in = trim($in);
  258. $in = str_replace($replace_from, $replace_to, $in);
  259. $in = preg_replace('/\<script(.*)\>(.*)\<\/script\>/Uis', "<script$1></script>", $in);
  260. $in = preg_replace('/\<\!\-\-(.*)\-\-\>/Uis', "<!-- comments have been removed -->", $in);
  261. if (HESK_SLASH === true) {
  262. $in = addslashes($in);
  263. }
  264. $in = str_replace('\"', '"', $in);
  265. return $in;
  266. } // END hesk_getHTML()
  267. function hesk_activeSessionValidate($username, $password_hash, $tag)
  268. {
  269. // Salt and hash need to be separated by a |
  270. if (!strpos($tag, '|')) {
  271. return false;
  272. }
  273. // Get two parts of the tag
  274. list($salt, $hash) = explode('|', $tag, 2);
  275. // Make sure the hash matches existing username and password
  276. if ($hash == sha1($salt . strtolower($username) . $password_hash)) {
  277. return true;
  278. }
  279. return false;
  280. } // hesk_activeSessionValidate
  281. function hesk_activeSessionCreateTag($username, $password_hash)
  282. {
  283. $salt = uniqid(mt_rand(), true);
  284. return $salt . '|' . sha1($salt . strtolower($username) . $password_hash);
  285. } // END hesk_activeSessionCreateTag()
  286. function hesk_autoLogin($noredirect = 0)
  287. {
  288. global $hesk_settings, $hesklang, $hesk_db_link;
  289. if (!$hesk_settings['autologin']) {
  290. return false;
  291. }
  292. $user = hesk_htmlspecialchars(hesk_COOKIE('hesk_username'));
  293. $hash = hesk_htmlspecialchars(hesk_COOKIE('hesk_p'));
  294. define('HESK_USER', $user);
  295. if (empty($user) || empty($hash)) {
  296. return false;
  297. }
  298. /* Login cookies exist, now lets limit brute force attempts */
  299. hesk_limitBfAttempts();
  300. /* Check username */
  301. $result = hesk_dbQuery('SELECT * FROM `' . $hesk_settings['db_pfix'] . "users` WHERE `user` = '" . hesk_dbEscape($user) . "' LIMIT 1");
  302. if (hesk_dbNumRows($result) != 1) {
  303. setcookie('hesk_username', '');
  304. setcookie('hesk_p', '');
  305. header('Location: index.php?a=login&notice=1');
  306. exit();
  307. }
  308. $res = hesk_dbFetchAssoc($result);
  309. /* Check password */
  310. if ($hash != hesk_Pass2Hash($res['pass'] . strtolower($user) . $res['pass'])) {
  311. setcookie('hesk_username', '');
  312. setcookie('hesk_p', '');
  313. header('Location: index.php?a=login&notice=1');
  314. exit();
  315. }
  316. // Set user details
  317. foreach ($res as $k => $v) {
  318. $_SESSION[$k] = $v;
  319. }
  320. /* Check if default password */
  321. if ($_SESSION['pass'] == '499d74967b28a841c98bb4baaabaad699ff3c079') {
  322. hesk_process_messages($hesklang['chdp'], 'NOREDIRECT', 'NOTICE');
  323. }
  324. // Set a tag that will be used to expire sessions after username or password change
  325. $_SESSION['session_verify'] = hesk_activeSessionCreateTag($user, $_SESSION['pass']);
  326. // We don't need the password hash anymore
  327. unset($_SESSION['pass']);
  328. /* Login successful, clean brute force attempts */
  329. hesk_cleanBfAttempts();
  330. /* Regenerate session ID (security) */
  331. hesk_session_regenerate_id();
  332. /* Get allowed categories */
  333. if (empty($_SESSION['isadmin'])) {
  334. $_SESSION['categories'] = explode(',', $_SESSION['categories']);
  335. }
  336. /* Renew cookies */
  337. setcookie('hesk_username', "$user", strtotime('+1 year'));
  338. setcookie('hesk_p', "$hash", strtotime('+1 year'));
  339. /* Close any old tickets here so Cron jobs aren't necessary */
  340. if ($hesk_settings['autoclose']) {
  341. $revision = sprintf($hesklang['thist3'], hesk_date(), $hesklang['auto']);
  342. $dt = date('Y-m-d H:i:s', time() - $hesk_settings['autoclose'] * 86400);
  343. // Notify customer of closed ticket?
  344. if ($hesk_settings['notify_closed']) {
  345. // Get list of tickets
  346. $result = hesk_dbQuery("SELECT * FROM `" . $hesk_settings['db_pfix'] . "tickets` WHERE `status` = '2' AND `lastchange` <= '" . hesk_dbEscape($dt) . "' ");
  347. if (hesk_dbNumRows($result) > 0) {
  348. global $ticket;
  349. // Load required functions?
  350. if (!function_exists('hesk_notifyCustomer')) {
  351. require(HESK_PATH . 'inc/email_functions.inc.php');
  352. }
  353. while ($ticket = hesk_dbFetchAssoc($result)) {
  354. $ticket['dt'] = hesk_date($ticket['dt'], true);
  355. $ticket['lastchange'] = hesk_date($ticket['lastchange'], true);
  356. $ticket = hesk_ticketToPlain($ticket, 1, 0);
  357. $modsForHesk_settings = mfh_getSettings();
  358. hesk_notifyCustomer($modsForHesk_settings, 'ticket_closed');
  359. }
  360. }
  361. }
  362. // Update ticket statuses and history in database
  363. hesk_dbQuery("UPDATE `" . $hesk_settings['db_pfix'] . "tickets` SET `status`='3', `closedat`=NOW(), `closedby`='-1', `history`=CONCAT(`history`,'" . hesk_dbEscape($revision) . "') WHERE `status` = '2' AND `lastchange` <= '" . hesk_dbEscape($dt) . "' ");
  364. }
  365. /* If session expired while a HESK page is open just continue using it, don't redirect */
  366. if ($noredirect) {
  367. return true;
  368. }
  369. /* Redirect to the destination page */
  370. header('Location: ' . hesk_verifyGoto());
  371. exit();
  372. } // END hesk_autoLogin()
  373. function hesk_isLoggedIn()
  374. {
  375. global $hesk_settings;
  376. $referer = hesk_input($_SERVER['REQUEST_URI']);
  377. $referer = str_replace('&amp;', '&', $referer);
  378. if (empty($_SESSION['id']) || empty($_SESSION['session_verify'])) {
  379. if ($hesk_settings['autologin'] && hesk_autoLogin(1)) {
  380. // Users online
  381. if ($hesk_settings['online']) {
  382. require(HESK_PATH . 'inc/users_online.inc.php');
  383. hesk_initOnline($_SESSION['id']);
  384. }
  385. return true;
  386. }
  387. hesk_session_stop();
  388. $url = 'index.php?a=login&notice=1&goto=' . urlencode($referer);
  389. header('Location: ' . $url);
  390. exit();
  391. } else {
  392. hesk_session_regenerate_id();
  393. // Let's make sure access data is up-to-date
  394. $res = hesk_dbQuery("SELECT `user`, `pass`, `isadmin`, `categories`, `heskprivileges` FROM `" . $hesk_settings['db_pfix'] . "users` WHERE `id` = '" . intval($_SESSION['id']) . "' LIMIT 1");
  395. // Exit if user not found
  396. if (hesk_dbNumRows($res) != 1) {
  397. hesk_session_stop();
  398. $url = 'index.php?a=login&notice=1&goto=' . urlencode($referer);
  399. header('Location: ' . $url);
  400. exit();
  401. }
  402. // Fetch results from database
  403. $me = hesk_dbFetchAssoc($res);
  404. // Verify this session is still valid
  405. if (!hesk_activeSessionValidate($me['user'], $me['pass'], $_SESSION['session_verify'])) {
  406. hesk_session_stop();
  407. $url = 'index.php?a=login&notice=1&goto=' . urlencode($referer);
  408. header('Location: ' . $url);
  409. exit();
  410. }
  411. // Update session variables as needed
  412. if ($me['isadmin'] == 1) {
  413. $_SESSION['isadmin'] = 1;
  414. } else {
  415. $_SESSION['isadmin'] = 0;
  416. $_SESSION['categories'] = explode(',', $me['categories']);
  417. $_SESSION['heskprivileges'] = $me['heskprivileges'];
  418. }
  419. // Users online
  420. if ($hesk_settings['online']) {
  421. require(HESK_PATH . 'inc/users_online.inc.php');
  422. hesk_initOnline($_SESSION['id']);
  423. }
  424. return true;
  425. }
  426. } // END hesk_isLoggedIn()
  427. function hesk_verifyGoto()
  428. {
  429. // Default redirect URL
  430. $url_default = 'admin_main.php';
  431. // If no "goto" parameter is set, redirect to the default page
  432. if (!hesk_isREQUEST('goto')) {
  433. return $url_default;
  434. }
  435. // Get the "goto" parameter
  436. $url = hesk_REQUEST('goto');
  437. // Fix encoded "&"
  438. $url = str_replace('&amp;', '&', $url);
  439. // Parse the URL for verification
  440. $url_parts = parse_url($url);
  441. // The "path" part is required
  442. if (!isset($url_parts['path'])) {
  443. return $url_default;
  444. }
  445. // Extract the file name from path
  446. $url = basename($url_parts['path']);
  447. // Allowed files for redirect
  448. $OK_urls = array(
  449. 'admin_main.php' => '',
  450. 'admin_settings.php' => '',
  451. 'admin_settings_save.php' => 'admin_settings.php',
  452. 'admin_ticket.php' => '',
  453. 'archive.php' => '',
  454. 'assign_owner.php' => '',
  455. 'change_status.php' => '',
  456. 'edit_post.php' => '',
  457. 'export.php' => '',
  458. 'find_tickets.php' => '',
  459. 'generate_spam_question.php' => '',
  460. 'knowledgebase_private.php' => '',
  461. 'lock.php' => '',
  462. 'mail.php' => '',
  463. 'manage_canned.php' => '',
  464. 'manage_categories.php' => '',
  465. 'manage_knowledgebase.php' => '',
  466. 'manage_users.php' => '',
  467. 'new_ticket.php' => '',
  468. 'profile.php' => '',
  469. 'reports.php' => '',
  470. 'show_tickets.php' => '',
  471. );
  472. // URL must match one of the allowed ones
  473. if (!isset($OK_urls[$url])) {
  474. return $url_default;
  475. }
  476. // Modify redirect?
  477. if (strlen($OK_urls[$url])) {
  478. $url = $OK_urls[$url];
  479. }
  480. // All OK, return the URL with query if set
  481. return isset($url_parts['query']) ? $url . '?' . $url_parts['query'] : $url;
  482. } // END hesk_verifyGoto()
  483. function hesk_Pass2Hash($plaintext)
  484. {
  485. $majorsalt = '';
  486. $len = strlen($plaintext);
  487. for ($i = 0; $i < $len; $i++) {
  488. $majorsalt .= sha1(substr($plaintext, $i, 1));
  489. }
  490. $corehash = sha1($majorsalt);
  491. return $corehash;
  492. } // END hesk_Pass2Hash()
  493. function hesk_formatDate($dt, $from_database = true)
  494. {
  495. $dt = hesk_date($dt, $from_database);
  496. $dt = str_replace(' ', '<br />', $dt);
  497. return $dt;
  498. } // End hesk_formatDate()
  499. function hesk_jsString($str)
  500. {
  501. $str = str_replace(array('\'', '<br />'), array('\\\'', ''), $str);
  502. $from = array("/\r\n|\n|\r/", '/\<a href="mailto\:([^"]*)"\>([^\<]*)\<\/a\>/i', '/\<a href="([^"]*)" target="_blank"\>([^\<]*)\<\/a\>/i');
  503. $to = array("\\r\\n' + \r\n'", "$1", "$1");
  504. return preg_replace($from, $to, $str);
  505. } // END hesk_jsString()
  506. function hesk_myCategories($what = 'category')
  507. {
  508. if (!empty($_SESSION['isadmin'])) {
  509. return '1';
  510. } else {
  511. return " `" . hesk_dbEscape($what) . "` IN ('" . implode("','", array_map('intval', $_SESSION['categories'])) . "')";
  512. }
  513. } // END hesk_myCategories()
  514. function hesk_okCategory($cat, $error = 1, $user_isadmin = false, $user_cat = false)
  515. {
  516. global $hesklang;
  517. /* Checking for current user or someone else? */
  518. if ($user_isadmin === false) {
  519. $user_isadmin = $_SESSION['isadmin'];
  520. }
  521. if ($user_cat === false) {
  522. $user_cat = $_SESSION['categories'];
  523. }
  524. /* Is admin? */
  525. if ($user_isadmin) {
  526. return true;
  527. } /* Staff with access? */
  528. elseif (in_array($cat, $user_cat)) {
  529. return true;
  530. } /* No access */
  531. else {
  532. if ($error) {
  533. hesk_error($hesklang['not_authorized_tickets']);
  534. } else {
  535. return false;
  536. }
  537. }
  538. } // END hesk_okCategory()
  539. function hesk_checkPermission($feature, $showerror = 1)
  540. {
  541. global $hesklang;
  542. /* Admins have full access to all features */
  543. if (isset($_SESSION['isadmin']) && $_SESSION['isadmin']) {
  544. return true;
  545. }
  546. /* Check other staff for permissions */
  547. if (isset($_SESSION['heskprivileges']) && strpos($_SESSION['heskprivileges'], $feature) === false) {
  548. if ($showerror) {
  549. hesk_error($hesklang['no_permission'] . '<p>&nbsp;</p><p align="center"><a href="index.php">' . $hesklang['click_login'] . '</a>');
  550. } else {
  551. return false;
  552. }
  553. } else {
  554. return true;
  555. }
  556. } // END hesk_checkPermission()