/
auto.php
1014 lines (889 loc) · 42.2 KB
/
auto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// auto.php - Regular SiT! maintenance tasks (for scheduling)
//
// SiT (Support Incident Tracker) - Support call tracking system
// Copyright (C) 2000-2009 Salford Software Ltd. and Contributors
//
// This software may be used and distributed according to the terms
// of the GNU General Public License, incorporated herein by reference.
//
// Author: Ivan Lucas <ivanlucas[at]users.sourceforge.net>
// This file should be called from a cron job (or similar) to run tasks periodically
require ('core.php');
include_once (APPLICATION_LIBPATH . 'strings.inc.php');
require_once (APPLICATION_LIBPATH . 'functions.inc.php');
include_once (APPLICATION_LIBPATH . 'billing.inc.php');
require_once (APPLICATION_LIBPATH . 'triggers.inc.php');
populate_syslang();
$crlg = "\n";
/**
* @author Ivan Lucas
**/
function saction_test()
{
echo "<h2>Testing testing 1 2 3.</h2>";
return TRUE;
}
/**
* Select incidents awaiting closure for more than a week where the next action time is not set or has passed
* @author Ivan Lucas
* @param $closure_delay int. The amount of time (in seconds) to wait before closing
**/
function saction_CloseIncidents($closure_delay)
{
$success = TRUE;
global $dbIncidents, $dbUpdates, $CONFIG, $crlf, $now;
if ($closure_delay < 1) $closure_delay = 554400; // Default six days and 10 hours
// Code added back in to fix mark as closure incidents
// http://bugs.sitracker.org/view.php?id=717
$sql = "UPDATE `{$dbIncidents}` SET lastupdated='{$now}', ";
$sql .= "closed='{$now}', status='".STATUS_CLOSED."', closingstatus='4', ";
$sql .= "timeofnextaction='0' WHERE status='".STATUS_CLOSING."' ";
$sql .= "AND (({$now} - lastupdated) > '{$closure_delay}') ";
$sql .= "AND (timeofnextaction='0' OR timeofnextaction <= '{$now}')";
$result = mysql_query($sql);
if (mysql_error())
{
trigger_error(mysql_error(),E_USER_WARNING);
$success = FALSE;
}
// Billing
$sql = "SELECT id, owner, status FROM `{$dbIncidents}` WHERE status='".STATUS_CLOSING."' ";
$sql .= "AND (({$now} - lastupdated) > '{$closure_delay}') ";
$sql .= "AND (timeofnextaction='0' OR timeofnextaction<='{$now}') ";
$result = mysql_query($sql);
if (mysql_error())
{
trigger_error(mysql_error(),E_USER_WARNING);
$success = FALSE;
}
while ($obj = mysql_fetch_object($result))
{
$bill = close_billable_incident($obj->id); // Do the close tasks if necessary
if ($bill)
{
$sqlc = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, currentowner, currentstatus, bodytext, timestamp, nextaction, customervisibility) ";
$sqlc .= "VALUES ('{$obj->id}', '0', 'closing', '{$obj->owner}', '{$obj->status}', 'Incident Closed by {$CONFIG['application_shortname']}', '{$now}', '', 'show' ) ";
$resultc = mysql_query($sqlc);
if (mysql_error())
{
trigger_error(mysql_error(),E_USER_WARNING);
$success = FALSE;
}
}
else
{
$success = FALSE;
}
}
return $success;
}
/**
* @author Ivan Lucas
**/
function saction_PurgeJournal()
{
global $dbJournal, $now, $CONFIG;
$success = TRUE;
$purgedate = date('YmdHis',($now - $CONFIG['journal_purge_after']));
$sql = "DELETE FROM `{$dbJournal}` WHERE timestamp < $purgedate";
$result = mysql_query($sql);
if (mysql_error())
{
trigger_error(mysql_error(),E_USER_WARNING);
$success = FALSE;
}
if ($CONFIG['debug']) //debug_log("Purged ".mysql_affected_rows()." journal entries");
return $success;
}
/** Calculate SLA times
* @author Tom Gerrard
* @note Moved from htdocs/auto/timecalc.php by INL for 3.40 release
**/
function saction_TimeCalc()
{
global $now;
global $dbIncidents, $dbServiceLevels, $dbMaintenance, $dbUpdates;
global $GLOBALS, $CONFIG;
$success = TRUE;
// FIXME this should only run INSIDE the working day
// FIXME ? this will not update the database fully if two SLAs have been met since last run - does it matter ?
if ($CONFIG['debug']) //debug_log("Calculating SLA times");
$sql = "SELECT id, title, maintenanceid, priority, slaemail, slanotice, servicelevel, status, owner ";
$sql .= "FROM `{$dbIncidents}` WHERE status != ".STATUS_CLOSED." AND status != ".STATUS_CLOSING;
$incident_result = mysql_query($sql);
if (mysql_error())
{
trigger_error(mysql_error(),E_USER_WARNING);
$success = FALSE;
}
while ($incident = mysql_fetch_array($incident_result))
{
// Get the service level timings for this class of incident, we may have one
// from the incident itself, otherwise look at contract type
if ($incident['servicelevel'] == '')
{
$sql = "SELECT tag FROM `{$dbServiceLevels}` s, `{$dbMaintenance}` m ";
$sql .= "WHERE m.id = '{$incident['maintenanceid']}' AND s.id = m.servicelevelid";
$result = mysql_query($sql);
if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
$t = mysql_fetch_row($sql);
$tag = $t[0];
mysql_free_result($result);
}
else $tag = $incident['servicelevel'];
if ($CONFIG['debug']) //debug_log($incident['id']." is a $tag incident");
$newReviewTime = -1;
$newSlaTime = -1;
$sql = "SELECT id, type, sla, timestamp, currentstatus FROM `{$dbUpdates}` WHERE incidentid='{$incident['id']}' ";
$sql .=" AND type = 'slamet' ORDER BY id DESC LIMIT 1";
$update_result = mysql_query($sql);
if (mysql_error())
{
trigger_error(mysql_error(),E_USER_WARNING);
$success = FALSE;
}
if (mysql_num_rows($update_result) != 1)
{
//if ($CONFIG['debug']) //debug_log("Cannot find SLA information for incident ".$incident['id'].", skipping");
}
else
{
$slaInfo = mysql_fetch_array($update_result);
$newSlaTime = calculate_incident_working_time($incident['id'],$slaInfo['timestamp'],$now);
if ($CONFIG['debug'])
{
//debug_log(" Last SLA record is ".$slaInfo['sla']." at ".date("jS F Y H:i",$slaInfo['timestamp'])." which is $newSlaTime working minutes ago");
}
}
mysql_free_result($update_result);
$sql = "SELECT id, type, sla, timestamp, currentstatus, currentowner FROM `{$dbUpdates}` WHERE incidentid='{$incident['id']}' ";
$sql .= "AND type='reviewmet' ORDER BY id DESC LIMIT 1";
$update_result = mysql_query($sql);
if (mysql_error())
{
trigger_error(mysql_error(),E_USER_WARNING);
$success = FALSE;
}
if (mysql_num_rows($update_result) != 1)
{
//if ($CONFIG['debug']) //debug_log("Cannot find review information for incident ".$incident['id'].", skipping");
}
else
{
$reviewInfo = mysql_fetch_array($update_result);
$newReviewTime = floor($now-$reviewInfo['timestamp'])/60;
if ($CONFIG['debug'])
{
//if ($reviewInfo['currentowner'] != 0) //debug_log("There has been no review on incident {$incident['id']}, which was opened $newReviewTime minutes ago");
}
trigger('TRIGGER_INCIDENT_REVIEW_DUE', array('incidentid' => $incident['id'], 'time' => $newReviewTime));
}
mysql_free_result($update_result);
if ($newSlaTime != -1)
{
// Get these time of NEXT SLA requirement in minutes
$coefficient = 1;
$NextslaName = $GLOBALS['strSLATarget'];
switch ($slaInfo['sla'])
{
case 'opened':
$slaRequest='initial_response_mins';
$NextslaName = $GLOBALS['strInitialResponse'];
break;
case 'initialresponse':
$slaRequest='prob_determ_mins';
$NextslaName = $GLOBALS['strProblemDefinition'];
break;
case 'probdef':
$slaRequest = 'action_plan_mins';
$NextslaName = $GLOBALS['strActionPlan'];
break;
case 'actionplan':
$slaRequest = 'resolution_days';
$NextslaName = $GLOBALS['strResolutionReprioritisation'];
$coefficient = ($CONFIG['end_working_day'] - $CONFIG['start_working_day']) / 60;
break;
case 'solution':
$slaRequest = 'initial_response_mins';
$NextslaName = $GLOBALS['strInitialResponse'];
break;
}
// Query the database for the next SLA and review times...
$sql = "SELECT ($slaRequest*$coefficient) as 'next_sla_time', review_days ";
$sql .= "FROM `{$dbServiceLevels}` WHERE tag = '$tag' AND priority = '{$incident['priority']}'";
$result = mysql_query($sql);
if (mysql_error())
{
trigger_error(mysql_error(),E_USER_WARNING);
$success = FALSE;
}
$times = mysql_fetch_assoc($result);
mysql_free_result($result);
if ($CONFIG['debug'])
{
//debug_log("The next SLA target should be met in ".$times['next_sla_time']." minutes");
//debug_log("Reviews need to be made every ".($times['review_days']*24*60)." minutes");
}
if ($incident['slanotice'] == 0)
{
//reaching SLA
if ($times['next_sla_time'] > 0) $reach = $newSlaTime / $times['next_sla_time'];
else $reach = 0;
if ($reach >= ($CONFIG['urgent_threshold'] * 0.01))
{
$timetil = $times['next_sla_time']-$newSlaTime;
trigger('TRIGGER_INCIDENT_NEARING_SLA', array('incidentid' => $incident['id'],
'nextslatime' => $times['next_sla_time'],
'nextsla' => $NextslaName));
$sql = "UPDATE `{$dbIncidents}` SET slanotice='1' WHERE id='{$incident['id']}'";
mysql_query($sql);
if (mysql_error()) trigger_error(mysql_error(),E_USER_WARNING);
}
}
}
}
mysql_free_result($incident_result);
return $success;
}
function saction_SetUserStatus()
{
global $dbHolidays, $dbUsers, $CONFIG, $crlf;
// Find users with holidays today who don't have correct status
$success = TRUE;
$startdate = mktime(0,0,0,date('m'),date('d'),date('Y'));
$enddate = mktime(23,59,59,date('m'),date('d'),date('Y'));
$sql = "SELECT * FROM `{$dbHolidays}` ";
$sql .= "WHERE `date` >= FROM_UNIXTIME($startdate) AND `date` < ";
$sql .= "FROM_UNIXTIME($enddate) AND (type >='".HOL_HOLIDAY."' AND type <= ".HOL_FREE.") ";
$sql .= "AND (approved=".HOL_APPROVAL_GRANTED." OR approved=".HOL_APPROVAL_DENIED;
$sql .= " OR approved=".HOL_APPROVAL_GRANTED_ARCHIVED;
$sql .= " OR approved=".HOL_APPROVAL_DENIED_ARCHIVED.")";
$result = mysql_query($sql);
if (mysql_error())
{
$success = FALSE;
trigger_error(mysql_error(),E_USER_WARNING);
}
while ($huser = mysql_fetch_object($result))
{
if ($huser->length == 'day'
OR ($huser->length == 'am' AND date('H') < 12)
OR ($huser->length == 'pm' AND date('H') > 12))
{
$currentstatus = user_status($huser->userid);
$newstatus = $currentstatus;
// Only enabled users
if ($currentstatus > 0)
{
if ($huser->type == HOL_HOLIDAY AND $currentstatus != USERSTATUS_ON_HOLIDAY) $newstatus = USERSTATUS_ON_HOLIDAY;
if ($huser->type == HOL_SICKNESS AND $currentstatus != USERSTATUS_ABSENT_SICK) $newstatus = USERSTATUS_ABSENT_SICK;
if ($huser->type == HOL_WORKING_AWAY AND
($currentstatus != USERSTATUS_WORKING_FROM_HOME AND
$currentstatus != USERSTATUS_WORKING_AWAY)) $newstatus = USERSTATUS_WORKING_AWAY;
if ($huser->type == HOL_TRAINING AND $currentstatus != USERSTATUS_ON_TRAINING_COURSE) $newstatus = USERSTATUS_ON_TRAINING_COURSE;
if ($huser->type == HOL_FREE AND
($currentstatus != USERSTATUS_NOT_IN_OFFICE AND
$currentstatus != USERSTATUS_ABSENT_SICK)) $newstatus = USERSTATUS_ABSENT_SICK; // Compassionate
}
if ($newstatus != $currentstatus)
{
$accepting = '';
switch ($newstatus)
{
case USERSTATUS_IN_OFFICE:
$accepting = 'Yes';
break;
case USERSTATUS_NOT_IN_OFFICE:
$accepting = 'No';
break;
case USERSTATUS_IN_MEETING:
// don't change
$accepting = '';
break;
case USERSTATUS_AT_LUNCH:
$accepting = '';
break;
case USERSTATUS_ON_HOLIDAY:
$accepting = 'No';
break;
case USERSTATUS_WORKING_FROM_HOME:
$accepting = 'Yes';
break;
case USERSTATUS_ON_TRAINING_COURSE:
$accepting = 'No';
break;
case USERSTATUS_ABSENT_SICK:
$accepting =' No';
break;
case USERSTATUS_WORKING_AWAY:
// don't change
$accepting = '';
break;
default:
$accepting='';
}
$usql = "UPDATE `{$dbUsers}` SET status='{$newstatus}'";
if ($accepting != '') $usql .= ", accepting='{$accepting}'";
$usql .= " WHERE id='{$huser->userid}' LIMIT 1";
if ($accepting == 'No') incident_backup_switchover($huser->userid, 'no');
if ($CONFIG['debug'])
{
//debug_log(user_realname($huser->userid).': '.userstatus_name($currentstatus).' -> '.userstatus_name($newstatus));
//debug_log($usql);
}
mysql_query($usql);
if (mysql_error())
{
$success = FALSE;
trigger_error(mysql_error(),E_USER_WARNING);
}
}
}
}
// Find users who are set away but have no entry in the holiday calendar
$sql = "SELECT * FROM `{$dbUsers}` WHERE status=".USERSTATUS_ON_HOLIDAY." OR ";
$sql .= "status=".USERSTATUS_ON_TRAINING_COURSE." OR ";
$sql .= "status=".USERSTATUS_ABSENT_SICK." OR status=".USERSTATUS_WORKING_AWAY." ";
$result = mysql_query($sql);
if (mysql_error())
{
$success = FALSE;
trigger_error(mysql_error(),E_USER_WARNING);
}
return $success;
}
/** Chase / Remind customers
* @author Paul Heaney
* @note Moved from htdocs/auto/chase_customer.php by INL for 3.40
*/
function saction_ChaseCustomers()
{
global $CONFIG, $now, $sit;
global $dbIncidents, $dbUpdates;
$success = TRUE;
/**
* @author Paul Heaney
*/
function not_auto_type($type)
{
if ($type != 'auto_chase_email' AND $type != 'auto_chase_phone' AND $type != 'auto_chase_manager')
{
return TRUE;
}
return FALSE;
}
if ($CONFIG['auto_chase'] == TRUE)
{
// if 'awaiting customer action' for more than $CONFIG['chase_email_minutes'] and NOT in an auto state, send auto email
//$sql = "SELECT incidents.id, contacts.forenames,contacts.surname,contacts.id AS managerid FROM incidents,contacts WHERE status = ".STATUS_CUSTOMER." AND contacts.notify_contactid = contacts.id";
$sql = "SELECT * FROM `{$dbIncidents}` AS i WHERE status = ".STATUS_CUSTOMER;
$result = mysql_query($sql);
if (mysql_error())
{
trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING);
$success = FALSE;
}
while ($obj = mysql_fetch_object($result))
{
if (!in_array($obj->maintenanceid, $CONFIG['dont_chase_maintids']))
{
// only annoy these people
$sql_update = "SELECT * FROM `{$dbUpdates}` WHERE incidentid = {$obj->id} ORDER BY timestamp DESC LIMIT 1";
$result_update = mysql_query($sql_update);
if (mysql_error())
{
trigger_error("MySQL Query Error ".mysql_error(), E_USER_WARNING);
$success = FALSE;
}
$obj_update = mysql_fetch_object($result_update);
if ($CONFIG['chase_email_minutes'] != 0)
{
//if (not_auto_type($obj_update->type) AND $obj_update->timestamp <= ($now-$CONFIG['chase_email_minutes']*60))
if (not_auto_type($obj_update->type) AND (($obj->timeofnextaction == 0 AND calculate_working_time($obj_update->timestamp, $now) >= $CONFIG['chase_email_minutes']) OR ($obj->timeofnextaction != 0 AND calculate_working_time($obj->timeofnextupdate, $now) >= $CONFIG['chase_email_minutes'])))
{
$paramarray = array('incidentid' => $obj->id, 'triggeruserid' => $sit[2]);
send_email_template($CONFIG['chase_email_template'], $paramarray);
$sql_insert = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, currentowner, currentstatus, bodytext, timestamp, customervisibility) VALUES ('{$obj_update->incidentid}','{$sit['2']}', 'auto_chase_email', '{$obj->owner}', '{$obj->status}', 'Sent auto chase email to customer','{$now}','show')";
mysql_query($sql_insert);
if (mysql_error())
{
trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
$success = FALSE;
}
$sql_update = "UPDATE `{$dbIncidents}` SET lastupdated = '{$now}', nextactiontime = 0 WHERE id = {$obj->id}";
mysql_query($sql_update);
if (mysql_error())
{
trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
$success = FALSE;
}
}
}
if ($CONFIG['chase_phone_minutes'] != 0)
{
//if ($obj_update->type == 'auto_chase_email' AND $obj_update->timestamp <= ($now-$CONFIG['chase_phone_minutes']*60))
if ($obj_update->type == 'auto_chase_email' AND (($obj->timeofnextaction == 0 AND calculate_working_time($obj_update->timestamp, $now) >= $CONFIG['chase_phone_minutes']) OR ($obj->timeofnextaction != 0 AND calculate_working_time($obj->timeofnextupdate, $now) >= $CONFIG['chase_phone_minutes'])))
{
$sql_insert = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, currentowner, currentstatus, bodytext, timestamp, customervisibility) VALUES ('{$obj_update->incidentid}','{$sit['2']}','auto_chase_phone', '{$obj->owner}', '{$obj->status}', 'Status: Awaiting Customer Action -> <b>Active</b><hr>Please phone the customer to get an update on this call as {$CONFIG['chase_phone_minutes']} have passed since the auto chase email was sent. Once you have done this please use the update type \"Chased customer - phone\"','{$now}','hide')";
mysql_query($sql_insert);
if (mysql_error())
{
trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
$success = FALSE;
}
$sql_update = "UPDATE `{$dbIncidents}` SET lastupdated = '{$now}', ";
$sql_update .= "nextactiontime = 0, status = ".STATUS_ACTIVE." WHERE id = {$obj->id}";
mysql_query($sql_update);
if (mysql_error())
{
trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
$success = FALSE;
}
}
}
if ($CONFIG['chase_manager_minutes'] != 0)
{
//if ($obj_update->type == 'auto_chased_phone' AND $obj_update->timestamp <= ($now-$CONFIG['chase_manager_minutes']*60))
if ($obj_update->type == 'auto_chased_phone' AND (($obj->timeofnextaction == 0 AND calculate_working_time($obj_update->timestamp, $now) >= $CONFIG['chase_manager_minutes']) OR ($obj->timeofnextaction != 0 AND calculate_working_time($obj->timeofnextupdate, $now) >= $CONFIG['chase_manager_minutes'])))
{
$update = "Status: Awaiting Customer Action -> <b>Active</b><hr>";
$update .= "Please phone the customers MANAGER to get an update on this call as ".$CONFIG['chase_manager_minutes']." have passed since the auto chase email was sent.<br />";
$update .= "The manager is <a href='contact_details.php?id={$obj->managerid}'>{$obj->forenames} {$obj->surname}</a><br />";
$update .= " Once you have done this please email the actions to the customer and select the \"Was this a customer chase?\"'";
$sql_insert = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, currentowner, currentstatus, bodytext, timestamp, customervisibility) VALUES ('{$obj_update->incidentid}','{$sit['2']}','auto_chase_manager', '{$obj->owner}', '{$obj->status}', $update,'{$now}','hide')";
mysql_query($sql_insert);
if (mysql_error())
{
trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
$success = FALSE;
}
$sql_update = "UPDATE `{$dbIncidents}` SET lastupdated = '{$now}', nextactiontime = 0, status = ".STATUS_ACTIVE." WHERE id = {$obj->id}";
mysql_query($sql_update);
if (mysql_error())
{
trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
$success = FALSE;
}
}
}
if ($CONFIG['chase_managers_manager_minutes'] != 0)
{
//if ($obj_update->type == 'auto_chased_manager' AND $obj_update->timestamp <= ($now-$CONFIG['chase_managers_manager_minutes']*60))
if ($obj_update->type == 'auto_chased_manager' AND (($obj->timeofnextaction == 0 AND calculate_working_time($obj_update->timestamp, $now) >= $CONFIG['chase_amanager_manager_minutes']) OR ($obj->timeofnextaction != 0 AND calculate_working_time($obj->timeofnextupdate, $now) >= $CONFIG['chase_amanager_manager_minutes'])))
{
$sql_insert = "INSERT INTO `{$dbUpdates}` (incidentid, userid, type, currentowner, currentstatus, bodytext, timestamp, customervisibility) VALUES ('{$obj_update->incidentid}','{$sit['2']}','auto_chase_managers_manager','{$obj->owner}', '{$obj->status}', 'Status: Awaiting Customer Action -> <b>Active</b><hr>Please phone the customers managers manager to get an update on this call as {$CONFIG['chase_manager_minutes']} have passed since the auto chase email was sent. Once you have done this please email the actions to the customer and manager and select the \"Was this a manager chase?\"','{$now}','hide')";
mysql_query($sql_insert);
if (mysql_error())
{
trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
$success = FALSE;
}
$sql_update = "UPDATE `{$dbIncidents}` SET lastupdated = '{$now}', nextactiontime = 0, status = ".STATUS_ACTIVE." WHERE id = {$obj->id}";
mysql_query($sql_update);
if (mysql_error())
{
trigger_error("MySQL Query Error ".mysql_error(), E_USER_ERROR);
$success = FALSE;
}
}
}
}
}
}
return $success;
}
/** Check the holding queue for waiting email
* @author Ivan Lucas
*/
function saction_CheckWaitingEmail()
{
global $dbTempIncoming, $dbUpdates;
$success = TRUE;
$sql = "SELECT COUNT(ti.id), UNIX_TIMESTAMP(NOW()) - `timestamp` AS minswaiting FROM `{$dbTempIncoming}` AS ti ";
$sql .= "LEFT JOIN `{$dbUpdates}` AS u ON ti.updateid = u.id GROUP BY ti.id";
$result = mysql_query($sql);
if (mysql_error())
{
trigger_error("MySQL Query Error".mysql_error(), E_USER_WARNING);
$success = FALSE;
}
list($count, $minswaiting) = mysql_fetch_row($result);
if ($count > 0)
{
trigger("TRIGGER_WAITING_HELD_EMAIL", array('minswaiting' => $minswaiting));
}
return $success;
}
/**
* Checks for expired FTP files (where expired is before now) and removes them
* @author Paul Heaney
*/
function saction_PurgeExpiredFTPItems()
{
global $dbFiles, $now;
$success = TRUE;
// Retreieve names first so we can delete them from FTP site
$sql = "SELECT * FROM `{$dbFiles}` WHERE expiry <= '{$now}' AND expiry != 0";
$result = mysql_query($sql);
if (mysql_error())
{
trigger_error("MySQL Query Error".mysql_error(), E_USER_WARNING);
$success = FALSE;
}
if (mysql_numrows($result) > 0)
{
$connection = create_ftp_connection();
while ($obj = mysql_fetch_object($result))
{
$success &= ftp_delete($connection, $obj->path."/".$obj->filename);
$sqlDel = "DELETE FROM `{$dbFiles}` WHERE id = {$obj->id}";
$resultdel = mysql_query($sqlDel);
if (mysql_error())
{
trigger_error("MySQL Query Error".mysql_error(), E_USER_WARNING);
$success = FALSE;
}
}
ftp_close($connection);
}
return $success;
}
// TODO PurgeAttachments
// Look for the review due trigger, where did it go
/**
*
* @author Paul Heaney
* @todo TODO document this
*/
function saction_MailPreviousMonthsTransactions()
{
global $CONFIG;
/*
Get todays date
Subtract one from the month and find last month
Find the last day of last month
fope(transactions.php?mode=csv&start=X&end=Y&breakdonw=yes
mail to people
TODO need a mechanism to subscribe to scheduled events? Could this be done with a trigger? Hmmhhhhhh
*/
$currentmonth = date('m');
$currentyear = date('y');
if ($currentmonth == 1)
{
$currentyear--;
$lastmonth = 12;
}
else
{
$lastmonth = $currentmonth - 1;
}
$startdate = "{$currentyear}-{$lastmonth}-01";
// Find last date of previous month, 5 day an arbitary choice
$lastday = date('t', strtotime('{$currentyear}-{$lastmonth}-05'));
$enddate = "{$currentyear}-{$lastmonth}-{$lastday}";
$csv = transactions_report('', $startdate, $enddate, '', 'csv', TRUE);
$extra_headers = "Reply-To: {$CONFIG['support_email']}\nErrors-To: {$CONFIG['support_email']}\n"; // TODO should probably be different
$extra_headers .= "X-Mailer: {$CONFIG['application_shortname']} {$application_version_string}/PHP " . phpversion() . "\n";
$extra_headers .= "X-Originating-IP: {$_SERVER['REMOTE_ADDR']}\n";
// if ($ccfield != '') $extra_headers .= "cc: $ccfield\n";
// if ($bccfield != '') $extra_headers .= "Bcc: $bccfield\n";
$extra_headers .= "\n"; // add an extra crlf to create a null line to separate headers from body
// this appears to be required by some email clients - INL
$subject = sprintf($GLOBALS['strBillableIncidentsForPeriodXtoX'], $startdate, $enddate);
$bodytext = $GLOBALS['strAttachedIsBillableIncidentsForAbovePeriod'];
$mime = new MIME_mail($CONFIG['support_email'], $CONFIG['billing_reports_email'], html_entity_decode($subject), $bodytext, $extra_headers, '');
$mime->attach($csv, "Billable report", OCTET, BASE64, "filename=billable_incidents_{$lastmonth}_{$currentyear}.csv");
return $mime->send_mail();
}
function saction_CheckIncomingMail()
{
global $CONFIG;
if ($CONFIG['enable_inbound_mail'] == 'POP/IMAP')
{
include 'inboundemail.php';
}
return TRUE;
}
function saction_CheckTasksDue()
{
$rtn = TRUE;
$sql = "SELECT `interval` FROM {$GLOBALS['dbScheduler']} ";
$sql .= "WHERE `s.action`='CheckTasksDue'";
if ($result = mysql_query($sql))
{
$intervalobj = mysql_fetch_object($result);
// check the tasks due between now and in N minutes time,
// where N is the time this action is run
$format = "Y-m-d H:i:s";
$startdue = date($format, $GLOBALS['now']);
$enddue = date($format, $GLOBALS['now'] + $intervalobj->interval);
$sql = "SELECT * FROM {$GLOBALS['dbTasks']} ";
$sql .= "WHERE duedate > {$startdue} AND duedate < {$enddue} ";
if ($result = mysql_query($sql))
{
while ($row = mysql_fetch_object($result))
{
trigger('TRIGGER_TASK_DUE', array('taskid' => $row->id));
}
}
}
return $rtn;
}
/**
* Perform the periodic sync of existing user and contact details from LDAP
* @author Paul Heaney
* @note This function does not create users or contacts it simply updates existing
* @note details.
*/
function saction_ldapSync()
{
global $CONFIG;
$success = FALSE;
if ($CONFIG['use_ldap'])
{
$ldap_conn = ldapOpen();
if ($ldap_conn)
{
// NOTE TODO FIXME would be more optimal to pass the user type into the create as in the case where the group membership isn't stored its looked up again
// Search for members of each group and then unique the members and loop through
// Populate an array ($users) with a list of SIT users in LDAP
// Only want GROUPS
$filter = "(objectClass={$CONFIG['ldap_grpobjecttype']})";
$attributesToGet = array($CONFIG['ldap_grpattributegrp']);
$users = array();
$userGrps = array($CONFIG['ldap_admin_group'], $CONFIG['ldap_manager_group'], $CONFIG['ldap_user_group'] );
foreach ($userGrps AS $grp)
{
if (!empty($grp))
{
$sr = ldap_search($ldap_conn, $grp, $filter, $attributesToGet);
if (ldap_count_entries($ldap_conn, $sr) != 1)
{
trigger_error ("Group {$grp} not found in LDAP");
}
else
{
$entry = ldap_first_entry($ldap_conn, $sr);
$attributes = ldap_get_attributes($ldap_conn, $entry);
for ($i = 0; $i < $attributes[$CONFIG['ldap_grpattributegrp']]['count']; $i++)
{
$member = $attributes[$CONFIG['ldap_grpattributegrp']][$i];
if (endsWith(strtolower($member), strtolower($CONFIG['ldap_user_base'])) AND $CONFIG['ldap_grpfulldn'])
{
$users[$member] = $member;
}
elseif (!$CONFIG['ldap_grpfulldn'])
{
$users[$member] = $member;
}
}
}
}
}
// Populate an array with the LDAP users already in the SiT database
$sit_db_users = array();
$sql = "SELECT id, username, status FROM `{$GLOBALS['dbUsers']}` WHERE user_source = 'ldap'";
$result = mysql_query($sql);
if (mysql_error()) trigger_error("MySQL Query Error".mysql_error(), E_USER_WARNING);
if (mysql_num_rows($result) > 0)
{
while ($obj = mysql_fetch_object($result))
{
$user_obj = new User();
$user_obj->id = $obj->id;
$user_obj->username = $obj->username;
$user_obj->status = $obj->status;
$sit_db_users[$obj->username] = $user_obj;
}
}
foreach ($users AS $u)
{
$e = ldap_getDetails($u, FALSE, $ldap_conn);
if ($e)
{
$user_attributes = ldap_get_attributes($ldap_conn, $e);
debug_log("user attributes: ".print_r($user_attributes, true), TRUE);
debug_log("db users: ".print_r($sit_db_users, true), TRUE);
// If the directory supports disabling of users
if (!empty($CONFIG['ldap_logindisabledattribute']))
{
if ($sit_db_users[$user_attributes[$CONFIG['ldap_userattribute']][0]]->status === USERSTATUS_ACCOUNT_DISABLED)
{
// User is disabled in the SIT db, check to see if we need to re-enable
if (!empty($user_attributes[$CONFIG['ldap_logindisabledattribute']]))
{
if (strtolower($user_attributes[$CONFIG['ldap_logindisabledattribute']][0]) != strtolower($CONFIG['ldap_logindisabledvalue']))
{
// The user is enabled in LDAP so we want to enable
debug_log("Re-enabling user '{$u}' in the SiT users database", TRUE);
$sit_db_users[$user_attributes[$CONFIG['ldap_userattribute']][0]]->status = $CONFIG['ldap_default_user_status'];
$sit_db_users[$user_attributes[$CONFIG['ldap_userattribute']][0]]->edit();
}
}
}
else
{
// User is not disabled in the SiT database, check to see if we need to disable
if (strtolower($user_attributes[$CONFIG['ldap_logindisabledattribute']][0]) == strtolower($CONFIG['ldap_logindisabledvalue']))
{
// User is disabled in LDAP so we want to disable
$sit_db_users[$user_attributes[$CONFIG['ldap_userattribute']][0]]->disable();
}
}
}
$userid = 0;
if (!empty($sit_db_users[$user_attributes[$CONFIG['ldap_userattribute']][0]]))
{
$userid = $sit_db_users[$user_attributes[$CONFIG['ldap_userattribute']][0]]->id;
unset ($sit_db_users[$user_attributes[$CONFIG['ldap_userattribute']][0]]);
}
if (!ldap_storeDetails('', $userid, TRUE, TRUE, $ldap_conn, $user_attributes))
{
trigger_error("Failed to store details for userid {$userid}", E_USER_WARNING);
$success = FALSE;
}
else
{
$success = TRUE;
}
}
else
{
debug_log ("Failed to get details for {$u}");
}
}
// Disable users we no longer know about
// TODO reassign incidents?
foreach ($sit_db_users AS $u)
{
debug_log ("Disabling {$u->username}");
$u->disable();
}
/** CONTACTS */
$contacts = array();
if (!empty($CONFIG["ldap_customer_group"]))
{
debug_log ("CONTACTS");
$sr = ldap_search($ldap_conn, $CONFIG["ldap_customer_group"], $filter, $attributesToGet);
if (ldap_count_entries($ldap_conn, $sr) != 1)
{
trigger_error ("No contact group found in LDAP");
}
else
{
$entry = ldap_first_entry($ldap_conn, $sr);
$attributes = ldap_get_attributes($ldap_conn, $entry);
for ($i = 0; $i < $attributes[$CONFIG['ldap_grpattributegrp']]['count']; $i++)
{
$member = $attributes[$CONFIG['ldap_grpattributegrp']][$i];
if (endsWith(strtolower($member), strtolower($CONFIG['ldap_user_base'])) AND $CONFIG['ldap_grpfulldn'])
{
$contacts[$member] = $member;
}
elseif (!$CONFIG['ldap_grpfulldn'])
{
$contacts[$member] = $member;
}
}
}
$sit_db_contacts = array();
$sql = "SELECT id, username, active FROM `{$GLOBALS['dbContacts']}` WHERE contact_source = 'ldap'";
$result = mysql_query($sql);
if (mysql_error()) trigger_error("MySQL Query Error".mysql_error(), E_USER_WARNING);
if (mysql_num_rows($result) > 0)
{
while ($obj = mysql_fetch_object($result))
{
$c = new Contact();
$c->id = $obj->id;
$c->username = $obj->username;
$c->status = $obj->active;
$sit_db_contacts[$c->username] = $c;
}
}
foreach ($contacts AS $c)
{
$e = ldap_getDetails($c, FALSE, $ldap_conn);
if ($e)
{
$contact_attributes = ldap_get_attributes($ldap_conn, $e);
if (isset($CONFIG['ldap_logindisabledattribute']))
{
// Directory supports disabling
if ($sit_db_contacts[$contact_attributes[$CONFIG['ldap_userattribute']][0]]->status == 'false')
{
// User disabled in SIT check if needs renameding
if (!empty($contact_attributes[$CONFIG['ldap_logindisabledattribute']]))
{
if (strtolower($contact_attributes[$CONFIG['ldap_logindisabledattribute']][0]) != strtolower($CONFIG['ldap_logindisabledvalue']))
{
// We want to enable
$sit_db_contacts[$contact_attributes[$CONFIG['ldap_userattribute']][0]]->active = 'true';
$sit_db_contacts[$contact_attributes[$CONFIG['ldap_userattribute']][0]]->edit();
}
}
}
elseif (!empty($contact_attributes[$CONFIG['ldap_logindisabledattribute']]))
{
// User not disabled in SiT though attribite is available to us
if (strtolower($contact_attributes[$CONFIG['ldap_logindisabledattribute']][0]) == strtolower($CONFIG['ldap_logindisabledvalue']))
{
// We want to disable
$sit_db_contacts[$contact_attributes[$CONFIG['ldap_userattribute']][0]]->disable();
}
}
}
$contactid = 0;
if (!empty($sit_db_contacts[$contact_attributes[$CONFIG['ldap_userattribute']][0]]))
{
$contactid = $sit_db_contacts[$contact_attributes[$CONFIG['ldap_userattribute']][0]]->id;
unset ($sit_db_contacts[$contact_attributes[$CONFIG['ldap_userattribute']][0]]);
}
if (!ldap_storeDetails('', $contactid, FALSE, TRUE, $ldap_conn, $contact_attributes))
{
trigger_error("Failed to store details for userid {$contactid}", E_USER_WARNING);
$success = FALSE;
}
}
}
// Disable users we no longer know about
// TODO reassign incidents?
foreach ($sit_db_contacts AS $c)
{
debug_log ("Disabling {$c->username}", TRUE);
$c->disable();
}
}
}
}
else
{
$success = TRUE;
}
return $success;
}
// =======================================================================================
$actions = schedule_actions_due();
if ($actions !== FALSE)
{
foreach ($actions AS $action => $params)
{
$fn = "saction_{$action}";
if ($verbose)
echo "<strong>{$fn}()</strong> ";
// Possibly initiate a trigger here named TRIGGER_SCHED_{$action} ?
if (function_exists($fn))