This repository was archived by the owner on Oct 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpua_api.php
1640 lines (1238 loc) · 67.5 KB
/
vpua_api.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
//Originally Programmed By: Kerick A. Howlett
//Original Date & Time of Completion: 8:15PM EST Thursday, December 4, 2014
//Update Notes:
//No Updates have been made as of yet.
//List of Functions used for the various methods of the API.
function con() //Function to quickly establish connection.
{
if(!file_exists("ost_con.php")) //Is the 'ost_con.php' file there?
die(http_response_code(404) . $result = __LINE__);
//OSTicket Database Connection Configurations are saved on a separate file for security reasons.
include 'ost_con.php';
return $con; //Returns the essential information needed to establish connection to database for all SQL
//queries.
}
function arrayFilter($var) //To establish desired parameters for an array filter that omits empty values EXCEPT ZERO VALUES!
{
return ($var !== NULL && $var !== '');
}
function dataArray() //Function to establish the entire array as a whole.
{
//The "TRUE" is needed in order to turn the JSON information from the other file and covert it back into an array.
$var = json_decode(file_get_contents("php://input"), TRUE);
if($_GET["method"] == 'create_ticket') //Error Handling for the "Create_Ticket" Method.
{
while (list($dk,$dv) = each($var))
{
$name = $var['name'];
$subject = $var['subject'];
$address = $var['address'];
if($dk === 'ip')
{
if(empty($dv))
die(http_response_code(428) . $result = __LINE__); //Is the IP address empty?
}
else
{
if(empty($dv))
die(http_response_code(406) . $result = __LINE__); //Are any of the critical fields empty?
}
}
if(strlen($name) > 128) //Is the name more than 128 characters?
die(http_response_code(413) . $result = __LINE__);
if(strlen($address) > 128) //Is the e-mail address more than 128 characters?
die(http_response_code(414) . $result = __LINE__);
if(strlen($subject) > 255) //Is the subject header more than 128 characters?
die(http_response_code(417) . $result = __LINE__);
}
if($_GET["method"] == 'update_ticket') //Error Handling for the "Update_Ticket" Method.
{
$ip = $var['ip_address'];
$time = $var['updated'];
$number = $var['number'];
$name = $var['name'];
$subject = $var['subject'];
$address = $var['address'];
if(empty($ip) || empty($time))
die(http_response_code(428) . $result = __LINE__); //Is there an IP address and/or date/time?
if(empty($number)) //Is there a Ticket Number?
die(http_response_code(406) . $result = __LINE__);
elseif(!is_numeric($number)) //Are there any letters in it?
die(http_response_code(415) . $result = __LINE__);
elseif(strlen($number) > 20) //Is it less than 20 characters?
die(http_response_code(415) . $result = __LINE__);
if(strlen($name) > 128) //Is the name more than 128 characters?
die(http_response_code(413) . $result = __LINE__);
if(strlen($address) > 128) //Is the e-mail address more than 128 characters?
die(http_response_code(414) . $result = __LINE__);
if(strlen($subject) > 255) //Is the subject header more than 128 characters?
die(http_response_code(417) . $result = __LINE__);
}
if($_GET["method"] == 'ticket_status') //Error Handling for the "Ticket_Status" Method.
{
$number = $var['number'];
if(empty($number)) //Is there a Ticket Number?
die(http_response_code(406) . $result = __LINE__);
elseif(!is_numeric($number)) //Are there any letters in it?
die(http_response_code(415) . $result = __LINE__);
elseif(strlen($number) > 20) //Is it less than 20 characters?
die(http_response_code(415) . $result = __LINE__);
}
if($_GET["method"] == 'ticket_assignment') //Error Handling for the "Ticket_Assignment" Method.
{
$ip = $var['ip_address'];
$time = $var['time'];
$admin = $var['admin'];
$username = $var['username'];
$number = $var['number'];
$body = $var['body'];
if(empty($ip) || empty($time))
die(http_response_code(428) . $result = __LINE__); //Is there an IP address and/or date/time?
if(empty($number)) //Is there a Ticket Number?
die(http_response_code(406) . $result = __LINE__);
elseif(!is_numeric($number)) //Are there any letters in it?
die(http_response_code(415) . $result = __LINE__);
elseif(strlen($number) > 20) //Is it less than 20 characters?
die(http_response_code(415) . $result = __LINE__);
if(empty($body)) //Is there any type of comment/note? Important Note: OSTicket UI says it's mandatory.
die(http_response_code(416) . $result = __LINE__);
if(empty($admin) Xor strlen($admin) > 32) //Is there an ADMIN Username? If so, is it longer than 32 characters?
die(http_response_code(413) . $result = __LINE__);
if(empty($username)) //Is there a STAFF Username?
die(http_response_code(407) . $result = __LINE__);
}
if($_GET["method"] == 'lookup_user') //Error Handling for the "Lookup_User" Method.
{
$con = con(); //Establish connection.
$username = $var['username']; //Pulls the entered Username belonging to the new Staff/Admin User.
if(empty($username)) //Was there a Username entered?
die(http_response_code(406) . $result = __LINE__);
if(strlen($username) > 32) //Was the Username longer than 32 characters?
die(http_response_code(414) . $result = __LINE__);
}
if($_GET["method"] == 'create_user') //Error Handling for the "Create_User" Method.
{
$con = con(); //Establish connection.
$username = $var['username']; //Pulls the entered Username belonging to the new Staff/Admin User.
$created = $var['created']; //Pulls the Date & Time of when the new Staff/Admin User was created.
$admin = $var['admin']; //Pulls the Username belonging to the Admin of whom is creating this new Staff/Admin User.
$isadmin = $var['isadmin']; //Pulls the binary setting regarding Access Privileges of when the new Staff/Admin User was created.
if(empty($created)) //Is there a Date & Time Record?
die(http_response_code(428) . $result = __LINE__);
if(empty($username)) //Was there a Username entered?
die(http_response_code(406) . $result = __LINE__);
if(strlen($username) > 32) //Was the Username longer than 32 characters?
die(http_response_code(414) . $result = __LINE__);
if(empty($admin) Xor strlen($admin) > 32) //Is there an ADMIN Username? If so, is it longer than 32 characters?
die(http_response_code(413) . $result = __LINE__);
// Was there anything wrong with setting the User Privileges setting?
switch($isadmin) {
case 0: //Staff Privileges.
break; //Does nothing.
case 1: //Admin Privileges.
break; //Does nothing.
case FALSE: //Is $isadmin empty/NULL?
die(http_response_code(416) . $result = __LINE__);
default: //If it's anything but 1 or 0, or even if it's empty.
die(http_response_code(416) . $result = __LINE__);
}
}
if($_GET["method"] == 'update_user') //Error Handling for the "Update_User" Method.
{
$con = con(); //Establish connection.
$username = $var['username']; //Pulls the entered Username.
$updated = $var['updated']; //Pulls the automatically entered
//Date & Time of when the User's
//information is being Updated.
$firstname = $var['firstname']; //Pulls the entered First Name.
$lastname = $var['lastname']; //Pulls the entered Last Name.
$email = $var['email']; //Pulls the entered E-Mail Address.
$phone = $var['phone']; //Pulls the entered Main/Primary/Office
//Phone Number.
//Pulls the entered Extension Number to the Main/Primary/Office Phone Number.
//It is also all caps just in case somebody types in "NULL".
$phone_ext = strtoupper($var['phone_ext']);
$mobile = $var['mobile']; //Pulls the entered Mobile/Cell Phone Number.
if(empty($username)) //Was there a Username entered?
die(http_response_code(406) . $result = __LINE__);
if(strlen($username) > 32) //Was the Username longer than 32 characters?
die(http_response_code(414) . $result = __LINE__);
if(empty($updated)) //Is there a Date & Time Record?
die(http_response_code(428) . $result = __LINE__);
//This error handling protocol only needs to take effect if there's actually
//something entered in the "firstname" or "lastname" fields.
if(isset($firstname) || isset($lastname))
{
//Was the First/Last Name longer than 32 characters?
if(strlen($username) > 32 || strlen($username) > 32)
die(http_response_code(418) . $result = __LINE__);
}
//This error handling protocol only needs to take effect if there's actually
//something entered in the "email" field.
if(isset($email))
{
//Was the E-Mail Address longer than 128 characters?
if(strlen($email) > 128)
die(http_response_code(418) . $result = __LINE__);
}
//This error handling protocol only needs to take effect if there's actually
//something entered in the "phone" or "phone_ext" fields.
if(isset($phone) || isset($mobile))
{
//Was the Main/Primary/Office/Mobile/Cell Phone Number longer than
//24 characters?
if(strlen($phone) > 24 || strlen($mobile) > 24)
die(http_response_code(418) . $result = __LINE__);
}
//This error handling protocol only needs to take effect if there's actually
//something entered in the "phone_ext" field.
if(isset($phone_ext))
{
//Was the Extension Number for their Main/Primary/Office Phone Number
//longer than 6 characters?
if(strlen($phone_ext) > 6)
die(http_response_code(418) . $result = __LINE__);
}
}
//Removing any records where the values are left NULL from the overall array.
$array = array_filter($var, "arrayFilter");
return $array; //Returning the post-filtered array.
}
function updateCountArray() //To ensure that the Users enters at least one field of data other than just the Ticket Number.
{
$array = dataArray(); //Pulls the Array from the dataArray() function and assigns it to a variable for
//later error handling.
// Two of the fields that need to be filled-out regardless, obviously, are the "Ticket Number"/"Username" and the
//"Updated" fields. Though there should also be, at the very least, one more field filled up in order to update at
//at least one field/record - otherwise, what's the point?
if(count($array) === 3)
die(http_response_code(405) . $result = __LINE__);
}
function data($field) //Function to pull out the desired data.
{
$array = dataArray(); // Pulls the post-filtered array from the dataArray() function.
//Confirms that the ticket number exists within the OSTicket database for only the desired/appropiate methods.
if($_GET["method"] == 'ticket_update' Xor $_GET["method"] == 'ticket_status' Xor $_GET["method"] == 'ticket_assignment')
{
$con = con(); //Establishes connection to database.
$number = $array['number']; //Pulling entered Ticket Number from the array.
//Executing the query that will search for a matching Ticket Number within the ost_ticket database table.
$test = mysqli_fetch_assoc(mysqli_query($con,"SELECT number
FROM ost_ticket
WHERE number = $number
LIMIT 1"));
if(!$test) //Could not find the Ticket Number?
die(http_response_code(400) . $result = __LINE__);
}
$data = $array[$field]; //To pull out the specific data which is desired by it's particular variable name,
//which are listed within the $data array of either the 'status.php' or 'update.php'
//files.
if($data == FALSE && $data != 0) //Could not find the data for the desired field? [Setup to where it can accept values of zero(0).]
die(http_response_code(409) . $result = $field . "-" . __LINE__);
return $data; //Returns the desired data.
}
function userLookup() //Function used for looking-up any possibly existing users that are stored within the OSTicket Database.
{
$con = con(); //Establish connection.
$key = 'username'; //Index Key for pulling value from data array, as well as name of database column header for SQL WHERE clause.
$value = data($key); //Pulling up the e-mail address or username to search for within the OSTicket Database.
//Executing query in order to see whether or not the username or e-mail address currently exists within the
//OSTicket Database.
$ost = mysqli_fetch_assoc(mysqli_query($con, "SELECT *
FROM ost_staff
WHERE $key = '$value'"));
if($_GET["method"] == 'update_user' && $ost == FALSE) //Error Handling for the "Update_User" Method.
die(http_response_code(406) . $result = __LINE__); //Couldn't find the Staff/Admin User in question by their Username.
return $ost; //Returning boolean results for later methods.
}
function singleUser() //Function for ensuring that there is never more than ONE(1) Staff/Admin User associated with any given e-mail address.
{
$ost = userLookup(); //Pulling up boolean results from the userLookup() function.
//Executing the following protocols based on whether it finds a matching e-mail address or not.
switch($ost) {
case TRUE: //Kills the program and returns the appropriate error message.
$line = __LINE__ + 3; //Acquiring accurate line number of where the program was killed for better error handling.
//Error Message without the usual method of sending back a "http_response_code", as the system will constantly
//keep overwriting the response code with that of a "500 - Internal Error" response code for some unknown reason.
die("<b>418-" . $line . ":</b>This user already exists. If you find this message an error, please contact
your system administrator.");
break;
case FALSE: //Does nothing, and continues on with creating the new Staff/Admin User.
break;
}
}
function LDAP() //Function to establish connection to LDAP system, and pull appropriate data.
{
if(!file_exists("ldap_config.php")) //Is the 'ldap_config.php' file there?
die(http_response_code(404) . $result = __LINE__);
include 'ldap_config.php'; //LDAP Configurations are saved on a separate file for security reasons.
$key = 'username'; //Data Array Index Key.
$ldap_key = data($key); //The Key that will uniquely identify the desired employee within the LDAP System.
$ldap_attr = 'cn'; //LDAP Attribute to look under for desired entry.
//The LDAP Attribute for an employee's Username in the system is "cn".
//The LDAP Attribute for an employee's E-Mail Address in the system is "mail".
$ldapcon = ldap_connect($server); // Establishing connection to the LDAP system.
if(!$ldapcon) //LDAP Error handling.
{
$error = ldap_error($ldapcon); //Pulling up the LDAP specific error code.
$pos = __LINE__ + 4; //Accurate line number of where the program was killed.
$line = substr($pos, 3); //Trimming away extra unneeded numbers.
die(http_response_code(445) . $result = "$error-$line" );
}
$bind = ldap_bind($ldapcon, $ldapuser, $pass); //Accessing desired LDAP through appropriate database path and authentication.
if(!$bind) //LDAP Error handling.
{
$error = ldap_error($ldapcon); //Pulling up the LDAP specific error code.
$pos = __LINE__ + 4; //Accurate line number of where the program was killed.
$line = substr($pos, 3); //Trimming away extra unneeded numbers.
die(http_response_code(445) . $result = "$error-$line" );
}
$search = ldap_search($ldapcon, $tree, "($ldap_attr = $ldap_key)"); //Looking-up the employee with desired LDAP Attribute & Key.
if(!$search) //LDAP Error handling.
{
$error = ldap_error($ldapcon); //Pulling up the LDAP specific error code.
$pos = __LINE__ + 4; //Accurate line number of where the program was killed.
$line = substr($pos, 3); //Trimming away extra unneeded numbers.
die(http_response_code(445) . $result = "$error-$line" );
}
$entry = ldap_get_entries($ldapcon, $search); //Assigning the record into an Array Variable.
if(!$entry) //LDAP Error handling.
{
$error = ldap_error($ldapcon); //Pulling up the LDAP specific error code.
$pos = __LINE__ + 4; //Accurate line number of where the program was killed.
$line = substr($pos, 3); //Trimming away extra unneeded numbers.
die(http_response_code(445) . $result = "$error-$line" ); //Error code.
}
for ($i=0; $i < $entry["count"]; $i++) //Extracting the needed information.
{
//The Username used for OSTicket will be the same as what they used for other various systems.
$username = $entry[$i]["cn"][0];
//The Last Name of the User for the OSTicket Database
$lastname = $entry[$i]["sn"][0];
//The First Name and Middle Initial of the User in the database, as that is how the LDAP database
//is setuped.
$givenname = $entry[$i]["givenname"][0];
//The e-mail address again, as to make data storage process a bit simpler.
$email = $entry[$i]["mail"][0];
}
//Error handling in the event that the entered e-mail address is not found in the LDAP system.
if(!isset($username) || !isset($lastname) || !isset($givenname) || !isset($email))
die(http_response_code(415) . $result = __LINE__);
//To determine the number of where the space is between the Employee's First Name and his/her middle initial.
$pos = strpos($givenname, ' ');
//Extracting JUST the Employee's First Name for the OSTicket Database.
$firstname = substr($givenname, 0, $pos);
$LDAPArray = Array(); //Creating the array to store the information that was just extracted.
$LDAPArray['username'] = $username; //Storing the Username into the LDAPArray.
$LDAPArray['firstname'] = $firstname; //Storing the First Name into the LDAPArray.
$LDAPArray['lastname'] = $lastname; //Storing the Last Name into the LDAPArray.
$LDAPArray['email'] = $email; //Storing the E-Mail Address into the LDAPArray.
ldap_close($ldapcon); //Closing LDAP connection.
return $LDAPArray; //Returning the now fully created array that's now containing all the needed data.
}
function dateCheck() //Function to make sure that date & time associated with the updated information proceeds that of what was there previously.
{
$con = con(); //Establish connection to the OSTicket Database.
if ($_GET["method"] === 'update_ticket') // Error Handling for the "Update_Ticket" method.
{
$number = data('number'); //Pulling Ticket Number from Data Array.
$time = data('updated'); //Pulling the date & time of this update.
//Executing the SQL Query to find the ticket in question.
$check = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_ticket
WHERE number = $number
LIMIT 1"));
if(!$check) //SQL Query Error Handling.
die(http_response_code(409) . $result = __LINE__);
$pre_date = $check['updated']; //Assigns variable with 'updated' value.
if($pre_date = '0000-00-00 00:00:00') //If it's '0000-00-00 00:00:00'(empty), than assign variable with 'created' value.
$pre_date = $check["created"];
//Was the created/previous-updated date & time recorded at the exact same moment or after this current/latest date & time?
//Or in other words: Do the words, "Does Not Compute!", come to mind?
if($pre_date >= $time)
die(http_response_code(428) . $result = __LINE__);
}
if ($_GET["method"] === 'ticket_assignment') // Error Handling for the "Ticket_Assignment" method.
{
$time = data('time'); //Pulling the date & time of this Ticket Assignment.
$ticket_id = ticketNumber(); //Pulling the Ticket ID Number using the ticketNumber() function.
//Executing the SQL Query to look and see if the ticket is currently assigned to someone or not.
$check = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_ticket_thread
WHERE ticket_id = $ticket_id
AND staff_id != 0
AND user_id = 0
AND thread_type = 'N'
AND title LIKE '%Ticket Assigned%'
AND poster NOT LIKE 'SYSTEM'
LIMIT 1"));
if($check): //Was it able to find any record of the ticket already being assigned?
{
$pre_date = $check['updated']; //Assigns variable with 'updated' value.
if($pre_date = '0000-00-00 00:00:00') //If it's '0000-00-00 00:00:00'(empty), than assign variable with 'created' value.
$pre_date = $check["created"];
//Was the created/previous-updated date & time recorded at the exact same moment or after this current/latest date & time?
//Or in other words: Do the words, "Does Not Compute!", come to mind?
if($pre_date >= $time)
die(http_response_code(428) . $result = __LINE__);
}
endif; //If it doesn't find a Ticket that's already assigned; than obviously, it doesn't need to do anything else
//here, and can continue on with the rest of the method.
}
if ($_GET["method"] === 'update_user') // Error Handling for the "Update_User" method.
{
$staff_record = userLookup(); //Pulls-up record pertaining to the Admin/Staff User in question into an Array.
$updated = data('updated'); //Pulling the date & time that the information pertaining to this Staff/Admin User was
//attempted at being Updated.
$pre_date = $staff_record['updated']; //Assigns variable with 'updated' value.
if($pre_date = '0000-00-00 00:00:00') //If it's '0000-00-00 00:00:00'(empty), than assign variable with 'created' value.
$pre_date = $staff_record["created"];
//Was the created/previous-updated date & time recorded at the exact same moment or after this current/latest date & time?
//Or in other words: Do the words, "Does Not Compute!", come to mind?
if($pre_date >= $updated)
die(http_response_code(428) . $result = __LINE__);
}
}
function _APIKey() //Function to confirm API Key.
{
if(isset($_SERVER['HTTP_X_API_KEY']))
{
$con = con(); //Establishing connection with the database.
$api = $_SERVER['HTTP_X_API_KEY'];
$key = mysqli_fetch_assoc(mysqli_query($con, "SELECT apikey
FROM ost_api_key
WHERE apikey = '$api'
LIMIT 1"));
if(!$key)
die(http_response_code(204) . $result = __LINE__);
return $api; //To bounce the API Key forward to the other API when executing the "Create Ticket" protocol.
}
else
die(http_response_code(214) . $result = __LINE__);
}
function staff_id() //To acquire the Staff Member's staff_id number.
{
$con = con(); //Establishing connection with the database.
$username = data('username');
$var = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_staff
WHERE username = '$username'
LIMIT 1"));
if(!$var)
{
//Error handling.
die(http_response_code(408) . $result = __LINE__);
}
else
{
$isactive = $var['isactive']; //Is the account Staff Member's account currently active?
if($isactive === 0)
die(http_response_code(412) . $result = __LINE__);
//Pulling staff_id from the $var array.
$staff_id = $var['staff_id'];
//Closing connection to the database.
mysqli_close($con);
//Returns the $ticket_id for output.
return $staff_id;
}
}
function staff_name($staff_id) //To acquire the Staff Member's full-name.
{
$con = con(); //Establishing connection with the database.
if ($_GET["method"] === 'ticket_status') //To look up a staff member by their internal staff_id number.
{
$key = 'staff_id';
$value = $staff_id;
}
elseif ($_GET["method"] === 'ticket_assignment') //To look up a staff member by their Username.
{
$username = data('username');
$key = 'username';
$value = $username;
}
if($value == 0) //In case there is no staff member assigned to the ticket.
{
$fullname = "Not Yet Assigned";
return $fullname;
}
else
{
$var = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_staff
WHERE $key = '$value'
LIMIT 1"));
if(!$var) //Error handling.
die(http_response_code(408) . $result = __LINE__);
//Pulling the staff member's FIRST name from the $var array.
$firstname = $var['firstname'];
//Pulling the staff member's LAST name from the $var array.
$lastname = $var['lastname'];
//Concatenating the staff member's first and last name to create his or her FULL name.
$fullname = $firstname . ' ' . $lastname;
//Closing connection to the database.
mysqli_close($con);
//Returns the $ticket_id for output.
return $fullname;
}
}
function adminVerification($admin) //Verification of Admin Privileges when necessary.
{
$con = con(); //Establishing connection with the database.
$var = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_staff
WHERE username = '$admin'
LIMIT 1"));
if(!$var)
{
//Error handling.
die(http_response_code(413) . $result = __LINE__);
}
else
{
//This part is to test to see if whether or not the User has proper authorization/is active or not.
$isadmin = $var['isadmin'];
if($isadmin === 0)
die(http_response_code(411) . $result = __LINE__);
$isactive = $var['isactive'];
if($isactive === 0)
die(http_response_code(412) . $result = __LINE__);
}
}
function admin_name() //To acquire admin's name and to test for proper authorization.
{
$con = con(); //Establishing connection with the database.
$admin = data('admin');
adminVerification($admin); //Validating that this is an authorized admin making the changes.
$var = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_staff
WHERE username = '$admin'
LIMIT 1"));
//Pulling the staff member's FIRST name from the $var array.
$firstname = $var['firstname'];
//Pulling the staff member's LAST name from the $var array.
$lastname = $var['lastname'];
//Concatenating the staff member's first and last name to create his or her FULL name.
$fullname = $firstname . ' ' . $lastname;
//Closing connection to the database.
mysqli_close($con);
//Returns the $ticket_id for output.
return $fullname;
}
function ticketNumber() //Function to convert the external Ticket Number into the internal ticket_id.
{
$con = con(); //Establishing connection with the database.
$number = data('number');
//Retrieving Ticket Information by it's External Ticket Number.
$var = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_ticket
WHERE number = $number
LIMIT 1"));
if(!$var)
{
//Error handling.
die(http_response_code(400) . $result = __LINE__);
}
else
{
//Pulling ticket_id from the $var array.
$ticket = $var['ticket_id'];
//Closing connection to the database.
mysqli_close($con);
//Returns the $ticket_id for output.
return $ticket;
}
}
function userID() //Function to acquire proper user_id.
{
$con = con(); //Establishing connection with the database.
$number = data('number');
//Retrieving Ticket Information by it's External Ticket Number.
$var = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_ticket
WHERE number = $number
LIMIT 1"));
if(!$var)
{
//Error handling.
die(http_response_code(409) . $result = __LINE__);
}
else
{
//Pulling user_id from the $var array.
$user_id = $var['user_id'];
//Closing connection to the database.
mysqli_close($con);
//Returns the $user_id for output.
return $user_id;
}
}
function entryID($form_id) //Function to acquire proper entry_id in order to find the desired information in the ost_form_entry_values table.
{
$con = con(); //Establishing connection with the database.
$user_id = userID(); //Pulling Internal User_ID from userID() function.
$ticket = ticketNumber(); //Pulling internal Ticket_ID from ticketNumber() function.
if ($form_id == 1)
{
$form = $form_id;
$id = $user_id;
$type = 'U';
}
elseif($form_id == 2)
{
$form = $form_id;
$id = $ticket;
$type = 'T';
}
$var = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_form_entry
WHERE object_id = $id
AND form_id = $form
AND object_type = '$type'
LIMIT 1"));
if(!$var) // Error handling.
die(http_response_code(409) . $result = __LINE__);
//Pulling entry_id from the $var2 array.
$entry_id = $var['id'];
//Closing connection to the database.
mysqli_close($con);
//Returns the $entry_id for output.
return $entry_id;
}
function defaultEMail() //Function to acquire the End User's "default_email_id" based on user_id.
{
$con = con(); //Establishing connection with the database.
$user_id = userID(); //Pulling Internal User_ID from userID() function.
$var = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_user
WHERE id = $user_id
LIMIT 1"));
if(!$var)
{
//Error handling.
die(http_response_code(409) . $result = __LINE__);
}
else
{
$email_id = $var['default_email_id'];
//Closing connection to the database.
mysqli_close($con);
return $email_id;
}
}
_APIKey(); //Executing API Key security function.
//Series of Methods used to execute the plethora of functions of the API.
//Method of the API for when the End User wishes to create a new ticket.
//Important Note: This Method primarily just bounces the processed data back-and-forth between this API and OSTicket's
//pre-existing API that actually creates the tickets.
if ($_GET["method"] === 'create_ticket')
{
$key = _APIKey();
// If 1, display things to debug.
$debug="0";
$config = array(
'url' => 'http://127.0.0.1/osticket/api/http.php/tickets.json', // URL
'key' => $key // API Key
);
$data = dataArray();
if($debug=='1') {
print_r($data);
die();
}
#pre-checks
function_exists('curl_version') or die('CURL support required');
function_exists('json_encode') or die('JSON support required');
#set timeout
set_time_limit(30);
#curl post
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $config['url']);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.8');
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key']));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result=curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code === 201)
{
echo "Your ticket was successfully created.<br><br>Your Ticket Number is: <b>$result</b>";
http_response_code(201);
}
else
die(http_response_code($code) . $result = __LINE__);
$ticket_id = (int) $result;
function IsNullOrEmptyString($question){
return (!isset($question) || trim($question)==='');
}
}
//Protocol of the API for when the End User wishes to check on a ticket's current status.
elseif ($_GET["method"] === 'ticket_status')
{
$con = con(); //Establishing quick connection.
$number = data('number'); //Pulling external Ticket Number.
$user_id = userID(); //Pulling internal UserID Number.
$email_id = defaultEMail(); //Pulling up internal Default Email ID.
$ticket_id = ticketNumber(); //Pulling up internal Ticket_ID Number.
//Looking up current data within the ost_ticket table by its associated Ticket Number.
$ticket_table = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_ticket
WHERE number = $number
LIMIT 1"));
if(!$ticket_table) //Error Handling.
die(http_response_code(409) . $result = __LINE__);
//Pulling up the status from the $ticket_table array.
$status = $ticket_table['status'];
//Pulling up the time & date the ticket was initially OPEN from the $ticket_table array.
$time = $ticket_table['created'];
if($status == 'closed') //To change the time & date to the of when it was closed.
{
unset($time); //Removing the time & date of when it was originally opened.
$time = $ticket_table['closed'];
}
//Pulling up the internal staff_id number from the $ticket_table array.
$staff_id = $ticket_table['staff_id'];
//Pulling up the staff member's name by their internal staff_id number.
$staff_name = staff_name($staff_id);
if($staff_id !== 0) //To see if the ticket is assigned to someone or not.
{
//Pulling up the date & time the staff member was assigned the ticket.
$assigned_query = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_ticket_thread
WHERE ticket_id = $ticket_id
AND staff_id = $staff_id
AND title LIKE '%Ticket Assigned%'
AND thread_type = 'N'
AND format = 'html'
LIMIT 1"));
if(!$assigned_query) //Error Handling.
die(http_response_code(409) . $result = __LINE__);
$assigned_time = $assigned_query['created']; //Establishes the initial time & date of assignment.
//To acquire the appropiate time & date if this ticket had ever been re-assigned.
if($assigned_time === '0000-00-00 00:00:00')
{
unset($assigned_time); //Removing the 'created' time & date.
$assigned_time = $assigned_query['updated']; //Re-assigning the 'updated' time & date.
}
}
//Pulling Ticket Holder Name
$user = mysqli_fetch_assoc(mysqli_query($con,"SELECT name
FROM ost_user
WHERE id = $user_id
LIMIT 1"));
if(!$user) //Error Handling.
die(http_response_code(409) . $result = __LINE__);
//Pulling Ticket Holder Name from the $user array.
$name = $user['name'];
//Pulling up User Email Address
$email = mysqli_fetch_assoc(mysqli_query($con,"SELECT address
FROM ost_user_email
WHERE id = $email_id
LIMIT 1"));
if(!$email) //Error Handling.
die(http_response_code(409) . $result = __LINE__);
//Pulling email address from the $email array.
$address = $email['address'];
//Pulling information from the ost_ticket_thread data table - namely the ticket body and title.
$thread = mysqli_fetch_assoc(mysqli_query($con,"SELECT *
FROM ost_ticket_thread
WHERE ticket_id = $ticket_id
AND user_id = $user_id
AND thread_type = 'M'
AND format = 'text'
LIMIT 1"));