2015-01-19 00:29:23 -05:00
< ? php
/*******************************************************************************
2015-09-12 00:46:46 -04:00
* Title : Help Desk Software HESK
* Version : 2.6 . 5 from 28 th August 2015
* Author : Klemen Stirn
* Website : http :// www . hesk . com
********************************************************************************
* COPYRIGHT AND TRADEMARK NOTICE
* Copyright 2005 - 2015 Klemen Stirn . All Rights Reserved .
* HESK is a registered trademark of Klemen Stirn .
* The HESK may be used and modified free of charge by anyone
* AS LONG AS COPYRIGHT NOTICES AND ALL THE COMMENTS REMAIN INTACT .
* By using this code you agree to indemnify Klemen Stirn from any
* liability that might arise from it ' s use .
* Selling the code for this program , in part or full , without prior
* written consent is expressly forbidden .
* Using this code , in part or full , to create derivate work ,
* new scripts or products is expressly forbidden . Obtain permission
* before redistributing this software over the Internet or in
* any other medium . In all cases copyright and header must remain intact .
* This Copyright is in full effect in any country that has International
* Trade Agreements with the United States of America or
* with the European Union .
* Removing any of the copyright notices without purchasing a license
* is expressly forbidden . To remove HESK copyright notice you must purchase
* a license for this script . For more information on how to obtain
* a license please visit the page below :
* https :// www . hesk . com / buy . php
*******************************************************************************/
2015-01-19 00:29:23 -05:00
/* Check if this is a valid include */
2015-09-12 00:46:46 -04:00
if ( ! defined ( 'IN_SCRIPT' )) {
die ( 'Invalid attempt' );
}
2015-01-19 00:29:23 -05:00
// Possible fields to be displayed in ticket list
$hesk_settings [ 'possible_ticket_list' ] = array (
2015-09-12 00:46:46 -04:00
'id' => $hesklang [ 'id' ],
'trackid' => $hesklang [ 'trackID' ],
'dt' => $hesklang [ 'submitted' ],
'lastchange' => $hesklang [ 'last_update' ],
'category' => $hesklang [ 'category' ],
'name' => $hesklang [ 'name' ],
'email' => $hesklang [ 'email' ],
'subject' => $hesklang [ 'subject' ],
'status' => $hesklang [ 'status' ],
'owner' => $hesklang [ 'owner' ],
'replies' => $hesklang [ 'replies' ],
'staffreplies' => $hesklang [ 'replies' ] . ' (' . $hesklang [ 'staff' ] . ')' ,
'lastreplier' => $hesklang [ 'last_replier' ],
'time_worked' => $hesklang [ 'ts' ],
2015-01-19 00:29:23 -05:00
);
// Also possible to display all custom fields
2015-09-12 00:46:46 -04:00
for ( $i = 1 ; $i <= 20 ; $i ++ ) {
if ( $hesk_settings [ 'custom_fields' ][ 'custom' . $i ][ 'use' ]) {
$hesk_settings [ 'possible_ticket_list' ][ 'custom' . $i ] = $hesk_settings [ 'custom_fields' ][ 'custom' . $i ][ 'name' ];
}
2015-01-19 00:29:23 -05:00
}
/*** FUNCTIONS ***/
function hesk_show_column ( $column )
{
2015-09-12 00:46:46 -04:00
global $hesk_settings ;
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
return in_array ( $column , $hesk_settings [ 'ticket_list' ]) ? true : false ;
2015-01-19 00:29:23 -05:00
} // END hesk_show_column()
function hesk_getHHMMSS ( $in )
{
2015-09-12 00:46:46 -04:00
$in = hesk_getTime ( $in );
2015-01-19 00:29:23 -05:00
return explode ( ':' , $in );
} // END hesk_getHHMMSS();
function hesk_getTime ( $in )
{
2015-09-12 00:46:46 -04:00
$in = trim ( $in );
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* If everything is OK this simple check should return true */
if ( preg_match ( '/^([0-9]{2,3}):([0-5][0-9]):([0-5][0-9])$/' , $in )) {
return $in ;
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
/* No joy, let's try to figure out the correct values to use... */
2015-01-19 00:29:23 -05:00
$h = 0 ;
$m = 0 ;
$s = 0 ;
/* How many parts do we have? */
$parts = substr_count ( $in , ':' );
2015-09-12 00:46:46 -04:00
switch ( $parts ) {
/* Only two parts, let's assume minutes and seconds */
case 1 :
list ( $m , $s ) = explode ( ':' , $in );
break ;
2015-01-19 00:29:23 -05:00
/* Three parts, so explode to hours, minutes and seconds */
case 2 :
2015-09-12 00:46:46 -04:00
list ( $h , $m , $s ) = explode ( ':' , $in );
break ;
2015-01-19 00:29:23 -05:00
/* Something other was entered, let's assume just minutes */
default :
2015-09-12 00:46:46 -04:00
$m = $in ;
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
/* Make sure all inputs are integers */
$h = intval ( $h );
2015-01-19 00:29:23 -05:00
$m = intval ( $m );
$s = intval ( $s );
2015-09-12 00:46:46 -04:00
/* Convert seconds to minutes if 60 or more seconds */
if ( $s > 59 ) {
$m = floor ( $s / 60 ) + $m ;
2015-01-19 00:29:23 -05:00
$s = intval ( $s % 60 );
}
2015-09-12 00:46:46 -04:00
/* Convert minutes to hours if 60 or more minutes */
if ( $m > 59 ) {
$h = floor ( $m / 60 ) + $h ;
2015-01-19 00:29:23 -05:00
$m = intval ( $m % 60 );
}
/* MySQL accepts max time value of 838:59:59 */
2015-09-12 00:46:46 -04:00
if ( $h > 838 ) {
return '838:59:59' ;
}
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* That's it, let's send out formatted time string */
2015-01-19 00:29:23 -05:00
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 );
} // END hesk_getTime();
function hesk_mergeTickets ( $merge_these , $merge_into )
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang , $hesk_db_link ;
2015-01-19 00:29:23 -05:00
/* Target ticket must not be in the "merge these" list */
2015-09-12 00:46:46 -04:00
if ( in_array ( $merge_into , $merge_these )) {
$merge_these = array_diff ( $merge_these , array ( $merge_into ));
2015-01-19 00:29:23 -05:00
}
/* At least 1 ticket needs to be merged with target ticket */
2015-09-12 00:46:46 -04:00
if ( count ( $merge_these ) < 1 ) {
$_SESSION [ 'error' ] = $hesklang [ 'merr1' ];
return false ;
2015-01-19 00:29:23 -05:00
}
/* Make sure target ticket exists */
2015-09-12 00:46:46 -04:00
$res = hesk_dbQuery ( " SELECT `id`,`trackid`,`category` FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " tickets` WHERE `id`=' " . intval ( $merge_into ) . " ' LIMIT 1 " );
if ( hesk_dbNumRows ( $res ) != 1 ) {
$_SESSION [ 'error' ] = $hesklang [ 'merr2' ];
return false ;
}
$ticket = hesk_dbFetchAssoc ( $res );
/* Make sure user has access to ticket category */
if ( ! hesk_okCategory ( $ticket [ 'category' ], 0 )) {
$_SESSION [ 'error' ] = $hesklang [ 'merr3' ];
return false ;
}
2015-01-19 00:29:23 -05:00
/* Set some variables for later */
$merge [ 'attachments' ] = '' ;
2015-09-12 00:46:46 -04:00
$merge [ 'replies' ] = array ();
2015-01-19 00:29:23 -05:00
$merge [ 'notes' ] = array ();
$sec_worked = 0 ;
$history = '' ;
$merged = '' ;
2015-09-12 00:46:46 -04:00
/* Get messages, replies, notes and attachments of tickets that will be merged */
foreach ( $merge_these as $this_id ) {
/* Validate ID */
if ( is_array ( $this_id )) {
continue ;
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
$this_id = intval ( $this_id ) or hesk_error ( $hesklang [ 'id_not_valid' ]);
2015-01-19 00:29:23 -05:00
/* Get required ticket information */
2015-09-12 00:46:46 -04:00
$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 " );
if ( hesk_dbNumRows ( $res ) != 1 ) {
continue ;
}
2015-01-19 00:29:23 -05:00
$row = hesk_dbFetchAssoc ( $res );
/* Has this user access to the ticket category? */
2015-09-12 00:46:46 -04:00
if ( ! hesk_okCategory ( $row [ 'category' ], 0 )) {
continue ;
2015-01-19 00:29:23 -05:00
}
/* Insert ticket message as a new reply to target ticket */
2015-09-12 00:46:46 -04:00
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' ]) . " ') " );
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* Update attachments */
hesk_dbQuery ( " UPDATE ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " attachments` SET `ticket_id`=' " . hesk_dbEscape ( $ticket [ 'trackid' ]) . " ' WHERE `ticket_id`=' " . hesk_dbEscape ( $row [ 'trackid' ]) . " ' " );
2015-01-19 00:29:23 -05:00
/* Get old ticket replies and insert them as new replies */
2015-09-12 00:46:46 -04:00
$res = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " replies` WHERE `replyto`=' " . intval ( $row [ 'id' ]) . " ' ORDER BY `id` ASC " );
while ( $reply = hesk_dbFetchAssoc ( $res )) {
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' ]) . " ') " );
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
/* Delete replies to the old ticket */
hesk_dbQuery ( " DELETE FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " replies` WHERE `replyto`=' " . intval ( $row [ 'id' ]) . " ' " );
2015-01-19 00:29:23 -05:00
/* Get old ticket notes and insert them as new notes */
2015-09-12 00:46:46 -04:00
$res = hesk_dbQuery ( " SELECT * FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " notes` WHERE `ticket`=' " . intval ( $row [ 'id' ]) . " ' ORDER BY `id` ASC " );
while ( $note = hesk_dbFetchAssoc ( $res )) {
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' ]) . " ') " );
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
/* Delete replies to the old ticket */
hesk_dbQuery ( " DELETE FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " notes` WHERE `ticket`=' " . intval ( $row [ 'id' ]) . " ' " );
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* Delete old ticket */
hesk_dbQuery ( " DELETE FROM ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " tickets` WHERE `id`=' " . intval ( $row [ 'id' ]) . " ' " );
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* Log that ticket has been merged */
$history .= sprintf ( $hesklang [ 'thist13' ], hesk_date (), $row [ 'trackid' ], $_SESSION [ 'name' ] . ' (' . $_SESSION [ 'user' ] . ')' );
2015-01-19 00:29:23 -05:00
/* Add old ticket ID to target ticket "merged" field */
$merged .= '#' . $row [ 'trackid' ];
2015-09-12 00:46:46 -04:00
/* Convert old ticket "time worked" to seconds and add to $sec_worked variable */
list ( $hr , $min , $sec ) = explode ( ':' , $row [ 'time_worked' ]);
$sec_worked += ((( int ) $hr ) * 3600 ) + ((( int ) $min ) * 60 ) + (( int ) $sec );
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
/* Convert seconds to HHH:MM:SS */
$sec_worked = hesk_getTime ( '0:' . $sec_worked );
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
// Get number of replies
$total = 0 ;
$staffreplies = 0 ;
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
$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 " );
while ( $row = hesk_dbFetchAssoc ( $res )) {
$total += $row [ 'cnt' ];
$staffreplies += ( $row [ 'staffid' ] ? $row [ 'cnt' ] : 0 );
}
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
$replies_sql = " `replies`= { $total } , `staffreplies`= { $staffreplies } , " ;
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
// Get first staff reply
if ( $staffreplies ) {
$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 " );
$reply = hesk_dbFetchAssoc ( $res );
$replies_sql .= " `firstreply`=' " . hesk_dbEscape ( $reply [ 'dt' ]) . " ', `firstreplyby`= " . intval ( $reply [ 'staffid' ]) . " , " ;
}
2015-01-19 00:29:23 -05:00
/* Update history (log) and merged IDs of target ticket */
2015-09-12 00:46:46 -04:00
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 " );
2015-01-19 00:29:23 -05:00
return true ;
} // END hesk_mergeTickets()
function hesk_updateStaffDefaults ()
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang ;
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
// Demo mode
if ( defined ( 'HESK_DEMO' )) {
return true ;
}
// Remove the part that forces saving as default - we don't need it every time
$default_list = str_replace ( '&def=1' , '' , $_SERVER [ 'QUERY_STRING' ]);
2015-01-19 00:29:23 -05:00
// Update database
2015-09-12 00:46:46 -04:00
$res = hesk_dbQuery ( " UPDATE ` " . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . " users` SET `default_list`=' " . hesk_dbEscape ( $default_list ) . " ' WHERE `id`=' " . intval ( $_SESSION [ 'id' ]) . " ' " );
2015-01-19 00:29:23 -05:00
// Update session values so the changes take effect immediately
$_SESSION [ 'default_list' ] = $default_list ;
return true ;
2015-09-12 00:46:46 -04:00
2015-01-19 00:29:23 -05:00
} // END hesk_updateStaffDefaults()
function hesk_makeJsString ( $in )
{
2015-09-12 00:46:46 -04:00
return addslashes ( preg_replace ( " / \ s+/ " , ' ' , $in ));
2015-01-19 00:29:23 -05:00
} // END hesk_makeJsString()
function hesk_checkNewMail ()
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang ;
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
$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' ]) . " ' " );
$num = hesk_dbResult ( $res , 0 , 0 );
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
return $num ;
2015-01-19 00:29:23 -05:00
} // END hesk_checkNewMail()
2015-09-12 00:46:46 -04:00
function hesk_getCategoriesArray ( $kb = 0 )
{
global $hesk_settings , $hesklang , $hesk_db_link ;
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
$categories = array ();
if ( $kb ) {
$result = hesk_dbQuery ( 'SELECT `id`, `name` FROM `' . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . 'kb_categories` ORDER BY `cat_order` ASC' );
} else {
$result = hesk_dbQuery ( 'SELECT `id`, `name` FROM `' . hesk_dbEscape ( $hesk_settings [ 'db_pfix' ]) . 'categories` ORDER BY `cat_order` ASC' );
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
while ( $row = hesk_dbFetchAssoc ( $result )) {
$categories [ $row [ 'id' ]] = $row [ 'name' ];
}
2015-01-19 00:29:23 -05:00
return $categories ;
} // END hesk_getCategoriesArray()
function hesk_getHTML ( $in )
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang ;
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
$replace_from = array ( " \t " , " <? " , " ?> " , " $ " , " <% " , " %> " );
$replace_to = array ( " " , " <? " , " ?> " , " \$ " , " <% " , " %> " );
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
$in = trim ( $in );
$in = str_replace ( $replace_from , $replace_to , $in );
$in = preg_replace ( '/\<script(.*)\>(.*)\<\/script\>/Uis' , " <script $ 1></script> " , $in );
$in = preg_replace ( '/\<\!\-\-(.*)\-\-\>/Uis' , " <!-- comments have been removed --> " , $in );
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
if ( HESK_SLASH === true ) {
$in = addslashes ( $in );
}
$in = str_replace ( '\"' , '"' , $in );
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
return $in ;
2015-01-19 00:29:23 -05:00
} // END hesk_getHTML()
2015-06-23 23:29:16 -04:00
function hesk_activeSessionValidate ( $username , $password_hash , $tag )
{
// Salt and hash need to be separated by a |
2015-09-12 00:46:46 -04:00
if ( ! strpos ( $tag , '|' )) {
2015-06-23 23:29:16 -04:00
return false ;
}
// Get two parts of the tag
list ( $salt , $hash ) = explode ( '|' , $tag , 2 );
// Make sure the hash matches existing username and password
2015-09-12 00:46:46 -04:00
if ( $hash == sha1 ( $salt . strtolower ( $username ) . $password_hash )) {
2015-06-23 23:29:16 -04:00
return true ;
}
return false ;
} // hesk_activeSessionValidate
function hesk_activeSessionCreateTag ( $username , $password_hash )
{
$salt = uniqid ( mt_rand (), true );
2015-06-23 23:30:49 -04:00
return $salt . '|' . sha1 ( $salt . strtolower ( $username ) . $password_hash );
2015-06-23 23:29:16 -04:00
} // END hesk_activeSessionCreateTag()
2015-09-12 00:46:46 -04:00
function hesk_autoLogin ( $noredirect = 0 )
2015-01-19 00:29:23 -05:00
{
2015-09-12 00:46:46 -04:00
global $hesk_settings , $hesklang , $hesk_db_link ;
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
if ( ! $hesk_settings [ 'autologin' ]) {
return false ;
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
$user = hesk_htmlspecialchars ( hesk_COOKIE ( 'hesk_username' ));
$hash = hesk_htmlspecialchars ( hesk_COOKIE ( 'hesk_p' ));
2015-01-19 00:29:23 -05:00
define ( 'HESK_USER' , $user );
2015-09-12 00:46:46 -04:00
if ( empty ( $user ) || empty ( $hash )) {
return false ;
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
/* Login cookies exist, now lets limit brute force attempts */
hesk_limitBfAttempts ();
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* Check username */
$result = hesk_dbQuery ( 'SELECT * FROM `' . $hesk_settings [ 'db_pfix' ] . " users` WHERE `user` = ' " . hesk_dbEscape ( $user ) . " ' LIMIT 1 " );
if ( hesk_dbNumRows ( $result ) != 1 ) {
2015-01-19 00:29:23 -05:00
setcookie ( 'hesk_username' , '' );
setcookie ( 'hesk_p' , '' );
header ( 'Location: index.php?a=login¬ice=1' );
exit ();
2015-09-12 00:46:46 -04:00
}
$res = hesk_dbFetchAssoc ( $result );
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* Check password */
if ( $hash != hesk_Pass2Hash ( $res [ 'pass' ] . strtolower ( $user ) . $res [ 'pass' ])) {
2015-02-26 18:42:43 -05:00
setcookie ( 'hesk_username' , '' );
2015-09-12 00:46:46 -04:00
setcookie ( 'hesk_p' , '' );
header ( 'Location: index.php?a=login¬ice=1' );
exit ();
}
2015-01-19 00:29:23 -05:00
2015-02-26 18:42:43 -05:00
// Set user details
2015-09-12 00:46:46 -04:00
foreach ( $res as $k => $v ) {
$_SESSION [ $k ] = $v ;
}
2015-01-19 00:29:23 -05:00
/* Check if default password */
2015-09-12 00:46:46 -04:00
if ( $_SESSION [ 'pass' ] == '499d74967b28a841c98bb4baaabaad699ff3c079' ) {
hesk_process_messages ( $hesklang [ 'chdp' ], 'NOREDIRECT' , 'NOTICE' );
2015-01-19 00:29:23 -05:00
}
2015-06-23 23:29:16 -04:00
// Set a tag that will be used to expire sessions after username or password change
$_SESSION [ 'session_verify' ] = hesk_activeSessionCreateTag ( $user , $_SESSION [ 'pass' ]);
// We don't need the password hash anymore
2015-09-12 00:46:46 -04:00
unset ( $_SESSION [ 'pass' ]);
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* Login successful, clean brute force attempts */
hesk_cleanBfAttempts ();
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* Regenerate session ID (security) */
hesk_session_regenerate_id ();
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* Get allowed categories */
if ( empty ( $_SESSION [ 'isadmin' ])) {
$_SESSION [ 'categories' ] = explode ( ',' , $_SESSION [ 'categories' ]);
}
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* Renew cookies */
setcookie ( 'hesk_username' , " $user " , strtotime ( '+1 year' ));
setcookie ( 'hesk_p' , " $hash " , strtotime ( '+1 year' ));
2015-01-19 00:29:23 -05:00
/* Close any old tickets here so Cron jobs aren't necessary */
2015-09-12 00:46:46 -04:00
if ( $hesk_settings [ 'autoclose' ]) {
$revision = sprintf ( $hesklang [ 'thist3' ], hesk_date (), $hesklang [ 'auto' ]);
$dt = date ( 'Y-m-d H:i:s' , time () - $hesk_settings [ 'autoclose' ] * 86400 );
// Notify customer of closed ticket?
if ( $hesk_settings [ 'notify_closed' ]) {
// Get list of tickets
$result = hesk_dbQuery ( " SELECT * FROM ` " . $hesk_settings [ 'db_pfix' ] . " tickets` WHERE `status` = '2' AND `lastchange` <= ' " . hesk_dbEscape ( $dt ) . " ' " );
if ( hesk_dbNumRows ( $result ) > 0 ) {
global $ticket ;
// Load required functions?
if ( ! function_exists ( 'hesk_notifyCustomer' )) {
require ( HESK_PATH . 'inc/email_functions.inc.php' );
}
while ( $ticket = hesk_dbFetchAssoc ( $result )) {
$ticket [ 'dt' ] = hesk_date ( $ticket [ 'dt' ], true );
$ticket [ 'lastchange' ] = hesk_date ( $ticket [ 'lastchange' ], true );
2015-02-22 22:16:30 -05:00
$ticket = hesk_ticketToPlain ( $ticket , 1 , 0 );
2015-09-12 00:46:46 -04:00
$modsForHesk_settings = mfh_getSettings ();
hesk_notifyCustomer ( $modsForHesk_settings , 'ticket_closed' );
}
}
}
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
// Update ticket statuses and history in database
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 ) . " ' " );
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
/* If session expired while a HESK page is open just continue using it, don't redirect */
if ( $noredirect ) {
return true ;
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
/* Redirect to the destination page */
header ( 'Location: ' . hesk_verifyGoto ());
exit ();
2015-01-19 00:29:23 -05:00
} // END hesk_autoLogin()
function hesk_isLoggedIn ()
{
2015-09-12 00:46:46 -04:00
global $hesk_settings ;
$referer = hesk_input ( $_SERVER [ 'REQUEST_URI' ]);
$referer = str_replace ( '&' , '&' , $referer );
if ( empty ( $_SESSION [ 'id' ]) || empty ( $_SESSION [ 'session_verify' ])) {
if ( $hesk_settings [ 'autologin' ] && hesk_autoLogin ( 1 )) {
// Users online
if ( $hesk_settings [ 'online' ]) {
require ( HESK_PATH . 'inc/users_online.inc.php' );
2015-01-19 00:29:23 -05:00
hesk_initOnline ( $_SESSION [ 'id' ]);
}
2015-09-12 00:46:46 -04:00
return true ;
2015-01-19 00:29:23 -05:00
}
2015-06-23 23:29:16 -04:00
hesk_session_stop ();
2015-09-12 00:46:46 -04:00
$url = 'index.php?a=login¬ice=1&goto=' . urlencode ( $referer );
header ( 'Location: ' . $url );
2015-01-19 00:29:23 -05:00
exit ();
2015-09-12 00:46:46 -04:00
} else {
2015-01-19 00:29:23 -05:00
hesk_session_regenerate_id ();
2015-06-23 23:29:16 -04:00
// Let's make sure access data is up-to-date
2015-09-12 00:46:46 -04:00
$res = hesk_dbQuery ( " SELECT `user`, `pass`, `isadmin`, `categories`, `heskprivileges` FROM ` " . $hesk_settings [ 'db_pfix' ] . " users` WHERE `id` = ' " . intval ( $_SESSION [ 'id' ]) . " ' LIMIT 1 " );
2015-06-23 23:29:16 -04:00
// Exit if user not found
2015-09-12 00:46:46 -04:00
if ( hesk_dbNumRows ( $res ) != 1 ) {
2015-06-23 23:29:16 -04:00
hesk_session_stop ();
2015-09-12 00:46:46 -04:00
$url = 'index.php?a=login¬ice=1&goto=' . urlencode ( $referer );
header ( 'Location: ' . $url );
2015-06-23 23:29:16 -04:00
exit ();
}
2015-01-19 00:29:23 -05:00
2015-06-23 23:29:16 -04:00
// Fetch results from database
$me = hesk_dbFetchAssoc ( $res );
// Verify this session is still valid
2015-09-12 00:46:46 -04:00
if ( ! hesk_activeSessionValidate ( $me [ 'user' ], $me [ 'pass' ], $_SESSION [ 'session_verify' ])) {
2015-06-23 23:29:16 -04:00
hesk_session_stop ();
2015-09-12 00:46:46 -04:00
$url = 'index.php?a=login¬ice=1&goto=' . urlencode ( $referer );
header ( 'Location: ' . $url );
2015-06-23 23:29:16 -04:00
exit ();
}
// Update session variables as needed
2015-09-12 00:46:46 -04:00
if ( $me [ 'isadmin' ] == 1 ) {
2015-06-23 23:29:16 -04:00
$_SESSION [ 'isadmin' ] = 1 ;
2015-09-12 00:46:46 -04:00
} else {
2015-06-23 23:29:16 -04:00
$_SESSION [ 'isadmin' ] = 0 ;
$_SESSION [ 'categories' ] = explode ( ',' , $me [ 'categories' ]);
$_SESSION [ 'heskprivileges' ] = $me [ 'heskprivileges' ];
}
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
// Users online
if ( $hesk_settings [ 'online' ]) {
require ( HESK_PATH . 'inc/users_online.inc.php' );
2015-01-19 00:29:23 -05:00
hesk_initOnline ( $_SESSION [ 'id' ]);
2015-09-12 00:46:46 -04:00
}
2015-01-19 00:29:23 -05:00
return true ;
}
} // END hesk_isLoggedIn()
function hesk_verifyGoto ()
{
2015-09-12 00:46:46 -04:00
// Default redirect URL
$url_default = 'admin_main.php' ;
// If no "goto" parameter is set, redirect to the default page
if ( ! hesk_isREQUEST ( 'goto' )) {
return $url_default ;
}
// Get the "goto" parameter
$url = hesk_REQUEST ( 'goto' );
// Fix encoded "&"
$url = str_replace ( '&' , '&' , $url );
// Parse the URL for verification
$url_parts = parse_url ( $url );
// The "path" part is required
if ( ! isset ( $url_parts [ 'path' ])) {
return $url_default ;
}
// Extract the file name from path
$url = basename ( $url_parts [ 'path' ]);
// Allowed files for redirect
$OK_urls = array (
'admin_main.php' => '' ,
'admin_settings.php' => '' ,
'admin_settings_save.php' => 'admin_settings.php' ,
'admin_ticket.php' => '' ,
'archive.php' => '' ,
'assign_owner.php' => '' ,
'change_status.php' => '' ,
'edit_post.php' => '' ,
'export.php' => '' ,
'find_tickets.php' => '' ,
'generate_spam_question.php' => '' ,
'knowledgebase_private.php' => '' ,
'lock.php' => '' ,
'mail.php' => '' ,
'manage_canned.php' => '' ,
'manage_categories.php' => '' ,
'manage_knowledgebase.php' => '' ,
'manage_users.php' => '' ,
'new_ticket.php' => '' ,
'profile.php' => '' ,
'reports.php' => '' ,
'show_tickets.php' => '' ,
);
// URL must match one of the allowed ones
if ( ! isset ( $OK_urls [ $url ])) {
return $url_default ;
}
// Modify redirect?
if ( strlen ( $OK_urls [ $url ])) {
$url = $OK_urls [ $url ];
}
// All OK, return the URL with query if set
return isset ( $url_parts [ 'query' ]) ? $url . '?' . $url_parts [ 'query' ] : $url ;
2015-01-19 00:29:23 -05:00
} // END hesk_verifyGoto()
2015-09-12 00:46:46 -04:00
function hesk_Pass2Hash ( $plaintext )
{
$majorsalt = '' ;
2015-01-19 00:29:23 -05:00
$len = strlen ( $plaintext );
2015-09-12 00:46:46 -04:00
for ( $i = 0 ; $i < $len ; $i ++ ) {
$majorsalt .= sha1 ( substr ( $plaintext , $i , 1 ));
2015-01-19 00:29:23 -05:00
}
$corehash = sha1 ( $majorsalt );
return $corehash ;
} // END hesk_Pass2Hash()
2015-09-12 00:46:46 -04:00
function hesk_formatDate ( $dt , $from_database = true )
2015-01-19 00:29:23 -05:00
{
2015-09-12 00:46:46 -04:00
$dt = hesk_date ( $dt , $from_database );
$dt = str_replace ( ' ' , '<br />' , $dt );
2015-01-19 00:29:23 -05:00
return $dt ;
} // End hesk_formatDate()
function hesk_jsString ( $str )
{
2015-09-12 00:46:46 -04:00
$str = str_replace ( array ( '\'' , '<br />' ), array ( '\\\'' , '' ), $str );
2015-01-19 00:29:23 -05:00
$from = array ( " / \r \n | \n | \r / " , '/\<a href="mailto\:([^"]*)"\>([^\<]*)\<\/a\>/i' , '/\<a href="([^"]*)" target="_blank"\>([^\<]*)\<\/a\>/i' );
2015-09-12 00:46:46 -04:00
$to = array ( " \\ r \\ n' + \r \n ' " , " $ 1 " , " $ 1 " );
return preg_replace ( $from , $to , $str );
2015-01-19 00:29:23 -05:00
} // END hesk_jsString()
2015-09-12 00:46:46 -04:00
function hesk_myCategories ( $what = 'category' )
2015-01-19 00:29:23 -05:00
{
2015-09-12 00:46:46 -04:00
if ( ! empty ( $_SESSION [ 'isadmin' ])) {
2015-01-19 00:29:23 -05:00
return '1' ;
2015-09-12 00:46:46 -04:00
} else {
return " ` " . hesk_dbEscape ( $what ) . " ` IN (' " . implode ( " ',' " , array_map ( 'intval' , $_SESSION [ 'categories' ])) . " ') " ;
2015-01-19 00:29:23 -05:00
}
} // END hesk_myCategories()
2015-09-12 00:46:46 -04:00
function hesk_okCategory ( $cat , $error = 1 , $user_isadmin = false , $user_cat = false )
2015-01-19 00:29:23 -05:00
{
2015-09-12 00:46:46 -04:00
global $hesklang ;
2015-01-19 00:29:23 -05:00
2015-09-12 00:46:46 -04:00
/* Checking for current user or someone else? */
if ( $user_isadmin === false ) {
$user_isadmin = $_SESSION [ 'isadmin' ];
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
if ( $user_cat === false ) {
$user_cat = $_SESSION [ 'categories' ];
2015-01-19 00:29:23 -05:00
}
/* Is admin? */
2015-09-12 00:46:46 -04:00
if ( $user_isadmin ) {
2015-01-19 00:29:23 -05:00
return true ;
2015-09-12 00:46:46 -04:00
} /* Staff with access? */
elseif ( in_array ( $cat , $user_cat )) {
2015-01-19 00:29:23 -05:00
return true ;
2015-09-12 00:46:46 -04:00
} /* No access */
else {
if ( $error ) {
hesk_error ( $hesklang [ 'not_authorized_tickets' ]);
} else {
return false ;
2015-01-19 00:29:23 -05:00
}
}
} // END hesk_okCategory()
2015-09-12 00:46:46 -04:00
function hesk_checkPermission ( $feature , $showerror = 1 )
{
global $hesklang ;
2015-01-19 00:29:23 -05:00
/* Admins have full access to all features */
2015-09-12 00:46:46 -04:00
if ( isset ( $_SESSION [ 'isadmin' ]) && $_SESSION [ 'isadmin' ]) {
2015-01-19 00:29:23 -05:00
return true ;
}
/* Check other staff for permissions */
2015-09-12 00:46:46 -04:00
if ( isset ( $_SESSION [ 'heskprivileges' ]) && strpos ( $_SESSION [ 'heskprivileges' ], $feature ) === false ) {
if ( $showerror ) {
hesk_error ( $hesklang [ 'no_permission' ] . '<p> </p><p align="center"><a href="index.php">' . $hesklang [ 'click_login' ] . '</a>' );
} else {
return false ;
2015-01-19 00:29:23 -05:00
}
2015-09-12 00:46:46 -04:00
} else {
2015-01-19 00:29:23 -05:00
return true ;
}
} // END hesk_checkPermission()