-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps-func.php
More file actions
1385 lines (1080 loc) · 41.6 KB
/
ps-func.php
File metadata and controls
1385 lines (1080 loc) · 41.6 KB
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
/*
* Functions related to communicating with the PowerSchool website in an automated fashion
* Author Wes Hampton 2013
* PowerSchool Version 7.8
*/
/*
* Works on PS 7.0.3
*/
function hex_hmac_md5($key, $data)
{
//Used in PSmAPI for hashing the login password
//Derived from: http://www.php.net/manual/en/function.mhash.php#27225
return bin2hex(mhash(MHASH_MD5, utf8_encode($data), utf8_encode($key)));
}
/*
* Works on PS 7.0.3
*/
function b64_md5($data) {
$result = base64_encode(pack('H*', md5(utf8_encode($data))));
//the PowerSchool implementation does not pad B64 values with "=" so we need to remove them.
return str_replace('=','',$result);
}
/*
* This is NOT USED, however it is a PHP rewrite of the Paul Johnston function of the same name that I wanted to preserve in case for the future.
* Original JavaScript here: http://pajhome.org.uk/crypt/md5/md5.html
*/
function rstr2binl($input)
{
$output = array_fill(0, (strlen($input) >> 2), 0);
//print_r($output);
for ($i = 0; $i < (strlen($input) * 8); $i += 8)
{
//for some reason sometimes the array should have had an extra value initialized to zero above. It continues just fine, assuming zeros apparently anyway.
$output[$i>>5] |= (ord(mb_substr($input, ($i / 8), 1)) & 0xFF) << ($i%32);
}
return $output;
}
/*
* Works on PS 7.0.3
*/
function PS_getAdminLogin ()
{
global $PScfg;
/*
* This function retrieves the PowerSchool Admin Login page
*
*/
$admin_prompt_url = $PScfg['admin_prompt_url'];
$PScookie = &$PScfg['cookie'];
//initialize the cURL Handle, which we'll pass around to various parts of this script
$ch = curl_init();
//we want this stored in a string, not output directly.
//also we don't want to worry about any SSL errors.
$options = array(
CURLOPT_URL => $admin_prompt_url,
CURLOPT_COOKIEJAR => $PScookie,
CURLOPT_COOKIEFILE => $PScookie,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
);
//actualy set the options for the cURL handle
curl_setopt_array($ch, $options);
//store the login page in a string for parsing
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
/*
* Works on PS 7.0.3
* This function establishes the login session to PowerSchool Admin
*/
function PS_loginAdmin ()
{
$result = false; //we switch this to true if our test for successful login is true
global $PScfg;
$admin_login_url = $PScfg['admin_login_url'];
$admin_user = $PScfg['admin_user'];
$admin_pass = $PScfg['admin_pass'];
$PScookie = &$PScfg['cookie'];
//Retrieve the PowerSchool Login page HTML source code
$PSLoginSource = PS_getAdminLogin();
//Parse the PS Login source code for the PSKey and PSToken
$PSSecrets = PS_scrapeSecrets($PSLoginSource);
//set POST variables
$fields = array(
'username'=>urlencode($admin_user),
'pstoken'=>urlencode($PSSecrets['pstoken']),
'request_locale'=>urlencode('en_US'),
//'password'=>urlencode(hex_hmac_md5($PSSecrets['pskey'],strtolower($admin_pass)))
'password'=>urlencode(hex_hmac_md5($PSSecrets['pskey'],b64_md5($admin_pass)))
);
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$admin_login_url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $PScookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $PScookie);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//if cURL attempt was false then we return false, other wise we look at the returned value
//to see if we landed on the home page properly, using the string "Last Sign In:" as the test
$result = ($result !== false && stripos($result, 'Last Sign In:') !== false) ? true : false;
return $result;
}
function PS_loginPublic ()
{
/*
*
*
*/
}
/*
* Works on PS 7.0.3
* Provide a $page that this script will request and return"
*/
function touch_ps_page($url)
{
global $PScfg;
$ps_touch_url = $url;
$PScookie = &$PScfg['cookie'];
//initialize the cURL Handle, which we'll pass around to various parts of this script
$ch = curl_init();
$options = array(
CURLOPT_URL => $ps_touch_url,
CURLOPT_COOKIEJAR => $PScookie,
CURLOPT_COOKIEFILE => $PScookie,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
);
//actualy set the options for the cURL handle
curl_setopt_array($ch, $options);
//store the login page in a string for parsing
$result = curl_exec($ch);
//close connection
curl_close($ch);
return $result;
}
/* Works on PS 7.0.3
* Based on PS Version: 6.2.2.0.0108 the response from the server should be a string of "success " on success,
* in that case we'll return bool true from this function, or false on any other value
* $scrn should be an integer value from the URL of the custom screen, screenid
* $stud should be the student's DCID from the student table in PowerSchool
* $data should be an array of key/value pairs where the key is the field number from the custom form (ex. "379" from the input field UF-0013792485)
* and the value is the desired data. This function will add the "UF-", the "001" (Student Table) and the DCID.
*/
function PS_SubmitCustomStudScreen($scrn, $stud, $data)
{
global $PScfg;
$result = false;
$domain = $PScfg['domain'];
$cookie = &$PScfg['cookie'];
//first we want to visit the proper page to trigger ?something? on the server side code or else this process will fail.
$url = $PScfg['custom_stud_screen_trigger_post'] . "?frn=001" . $stud . "&screenid=" . $scrn;
touch_ps_page($url);
//set POST variables
$url = $PScfg['custom_stud_screen_post'];
$fields = array(
'ac'=>urlencode('prim')
);
//add the custom data to the fields
foreach ($data as $key => $value)
{
$fields['UF-001'.$key.$stud] = urlencode($value);
}
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//add some more headers to the default
$headers = array_merge($PScfg['http_headers'],array(
"X-Requested-With: XMLHttpRequest",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
"Content-Length: " . strlen($fields_string),
)
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); //reuse the same cookiejar from above
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); //send the same cookie data from above
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //send along our custom headers from above
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//really an ODBC call can be make here to make sure this data was actually saved properly.
//return true/false if data was sent to server successfully
return ($result !== false) ? true : false;
}
function PS_PGAaddStuds($gai, $studAry, $relAry)
{
/* Based on PS Version: 6.2.2.0.0108 the response from the server should be a string of "success " on success,
in that case we'll return bool true from this function, or false on any other value */
global $PScfg;
$result = false;
$domain = $PScfg['domain'];
$cookie = &$PScfg['cookie'];
$studID = implode(',',$studAry);
$PGAgai = $gai;
$PGArel = implode(',',$relAry);
//set POST variables
$url = $PScfg['pga_add_stud'];
$fields = array(
'selectedGuardianRelationshipsForStudents'=>urlencode($PGArel),
'selectedStudentDcids'=>urlencode($studID),
'gai'=>urlencode($PGAgai),
'submitOperation'=>urlencode('add')
);
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//add some more headers to the default
$headers = array_merge($PScfg['http_headers'],array(
"X-Requested-With: XMLHttpRequest",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
"Content-Length: " . strlen($fields_string),
)
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); //reuse the same cookiejar from above
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); //send the same cookie data from above
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //send along our custom headers from above
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//really an ODBC call can be make here to make confirm these student are added.
//return true/false
return (trim($result) == 'success') ? true : false;
}
function PS_PGAdelStuds($gai, $studID)
{
/* Based on PS Version: 7.10 the response from the server should be a form to change student access info on success,
this function will return bool true in this case, false on fail. */
global $PScfg;
$result = false;
$domain = $PScfg['domain'];
$cookie = &$PScfg['cookie'];
$studID = $studID;
$PGAgai = $gai;
//first we want to visit the proper page to trigger ?something? on the server side code or else this process will fail.
$url = $PScfg['pga_trigger_del_stud'] . '?gai=' . urlencode($gai) . '&studentId=' . urlencode($studID) . '&guardianPortal=true';
touch_ps_page($url);
//set POST variables
$url = $PScfg['pga_del_stud'];
$fields = array(
'ac'=>urlencode('brij:admin-accountmanagement-pkg/DeleteGuardianFromStudent'),
'doc'=>urlencode('/admin/students/guardian_delete_dialog.html'),
'render_in_java'=>urlencode('true'),
'gai'=>urlencode($PGAgai),
'studentId'=>urlencode($studID)
);
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//add some more headers to the default
$headers = array_merge($PScfg['http_headers'],array(
"X-Requested-With: XMLHttpRequest",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
"Content-Length: " . strlen($fields_string),
)
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); //reuse the same cookiejar from above
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); //send the same cookie data from above
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //send along our custom headers from above
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//really an ODBC call can be made here to make confirm this student is removed.
//right now we'll just do a screen scrape to see if the result appears normal
//return true/false
return (strpos($result, 'brij:admin-accountmanagement-pkg/UpdateGuardianWebId') !== false) ? true : false;
}
function PS_PGAadd(&$dbh, $first, $last, $email, $username, $locked = false, $pass = '')
{
/* Based on PS Version: 6.2.2.0.0108 the response from the server should be a blank page on success.
this function returns true/false after executing an ODBC query to see if account exists at the end
It needs the database connection because it makes a call to see if there is an account with this username or email already */
global $PScfg;
$result = false;
$domain = $PScfg['domain'];
$cookie = &$PScfg['cookie'];
$PGAfirst = $first;
$PGAlast = $last;
$PGAemail = $email;
$PGAuser = $username;
$PGAlocked = $locked;
$PGApass = ($pass != '') ? $pass : $PScfg['pga_default_pass'];
//let's make sure this email address or username is not currently being used first
//in the sequence of main script we are actually sending a temporary email address, so this only check what this function receives.
if ((PS_getPGAbyUsername($dbh, $PGAuser) === false) && (PS_getPGAbyEmail($dbh, $PGAemail) === false))
{
//first we want to visit the proper page to trigger ?something? on the server side code or else this process will fail.
$url = $PScfg['pga_trigger_add_acct'];
touch_ps_page($url);
//set POST variables
$url = $PScfg['pga_add_acct'];
$fields = array(
'ac'=>urlencode('brij:admin-accountmanagement-pkg/submitAdminNoStudentsGuardianAccountForm'),
'doc'=>urlencode('/admin/guardians/new_guardian_account.html'),
'render_in_java'=>urlencode('true'),
'newGuardian.firstName'=>urlencode($PGAfirst),
'newGuardian.lastName'=>urlencode($PGAlast),
'newGuardian.email'=>urlencode($PGAemail),
'accountInfo.username'=>urlencode($PGAuser),
'accountInfo.password'=>urlencode($PGApass),
'passwordConfirm'=>urlencode($PGApass)
);
//add a POST value if the account should be locked
if ($locked) $fields['accountLocked'] = urlencode('true');
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//add some more headers to the default
$headers = array_merge($PScfg['http_headers'],array(
"X-Requested-With: XMLHttpRequest",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
"Content-Length: " . strlen($fields_string),
)
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); //reuse the same cookiejar from above
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); //send the same cookie data from above
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //send along our custom headers from above
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//if the data appeared to be sent properly then we should now find this record in the database
$result = ($result !== false && PS_getPGAbyUsername($dbh, $PGAuser) !== false) ? true : false;
}
return ($result !== false) ? true : false;
}
function PS_PGAedit(&$dbh, $gai, $first, $last, $email, $username, $locked = false, $pass = '')
{
/* Based on PS Version: 6.2.2.0.0108 the response from the server should be a blank page on success,
this function will return bool true in this case, false on fail.
It needs the database connection because it makes a call to get and compare the original information to confirm success */
global $PScfg;
$result = false;
$domain = $PScfg['domain'];
$cookie = &$PScfg['cookie'];
$PGAgai = $gai;
$PGAfirst = $first;
$PGAlast = $last;
$PGAemail = $email;
$PGAuser = $username;
$PGAlocked = $locked;
$PGApass = $pass;
$needs_pass_update = ($PGApass != '') ? true : false;
//it's possible that this is a new username, and in that case we need to know what the old one was
//either way it is probably a good idea to make sure this parent exists anyway or return false on error
$pgaOrig = PS_getPGAbyActID($dbh, $PGAgai);
if ($pgaOrig)
{
$pgaUserOrig = $pgaOrig['USERNAME'];
$pgaModPCAS = $pgaOrig['MOD_PCAS'];
//first we want to visit the proper page to trigger ?something? on the server side code or else this process will fail.
$url = $PScfg['pga_trigger_edit_info'] . '?gai=' . urlencode($gai);
touch_ps_page($url);
//set POST variables
$url = $PScfg['pga_edit_info'];
$fields = array(
'ac'=>urlencode('brij:admin-accountmanagement-pkg/SaveGuardianAccount'),
'doc'=>urlencode('/admin/guardians/home.html'),
'render_in_java'=>urlencode('true'),
'gai'=>urlencode($PGAgai),
'account.username'=>urlencode($pgaUserOrig),
'firstName'=>urlencode($PGAfirst),
'lastName'=>urlencode($PGAlast),
'email'=>urlencode($PGAemail),
'username'=>urlencode($PGAuser),
'newPassword'=>urlencode($PGApass),
'confirmPassword'=>urlencode($PGApass)
);
//add a POST value if the account should be locked
if ($locked) $fields['accountDisabled'] = urlencode('true');
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//add some more headers to the default
$headers = array_merge($PScfg['http_headers'],array(
"X-Requested-With: XMLHttpRequest",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
"Content-Length: " . strlen($fields_string),
)
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); //reuse the same cookiejar from above
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); //send the same cookie data from above
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //send along our custom headers from above
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//as long as the data appeared to be successfully sent to the server we now verify against the database
if ($result !== false)
{
$pgaConfirm = PS_getPGAbyActID($dbh, $PGAgai);
$isConfirmed = false; //we set this to true if everything now matches
//set up some simple variables from the matching PGA
$pgaFirstConfirm = $pgaConfirm['FIRSTNAME'];
$pgaLastConfirm = $pgaConfirm['LASTNAME'];
$pgaEmailConfirm = $pgaConfirm['EMAIL'];
$pgaUserConfirm = $pgaConfirm['USERNAME'];
$pgaCredEncConfirm = $pgaConfirm['ENCRYPTIONMODE']; //For PS7 changed from ISCREDENTIALENC TO ENCRYPTIONMODE
$pgaModPCASConfirm = $pgaConfirm['MOD_PCAS'];
//make sure record date changed and compare database fields to what was submitted to server, test password update separately if necessary below
$isConfirmed = ($pgaModPCASConfirm != $pgaModPCAS && $pgaFirstConfirm == $PGAfirst && $pgaLastConfirm == $PGAlast && $pgaEmailConfirm == $PGAemail && $pgaUserConfirm == $PGAuser) ? true : false;
//check if it really appeared to reset the credentials (if it was required)
//***specifically check if creds are set as expired which means they are ready to be changed by the user (or us via script/function)
//the pcas_account.credentialchangeddate field only changes is the value changed, which in certain repair/maintenance situations might not be the case
//a simple test if creds are different then the ones we got only moments ago is not sufficient because they could possibly
//have just been "reset" to the value they already were.
$isConfirmed = ($isConfirmed && (!$needs_pass_update || ($needs_pass_update && $pgaCredEncConfirm == 2))) ? true : false;
//set the result to true/false as determined by our confirmation tests directly above
$result = $isConfirmed;
}
} //end testing for existing account
return $result;
}
function PS_PGAchangePwd(&$PSdbh, &$AUXdbh, $user, $oldPwd, $newPwd)
{
/* Based on PS Version: 6.2.2.0.0108 the response from the server should be a blank page on success,
this function will return bool true in this case, false on fail.
Also, this function doesn't need a login or cookies, just need to know the existing password.
It does however use an ODBC connection to make sure the change took effect!
$PSdbh is the ODBC connection to the PowerSchool database
$AUXdbh is the connection to the database server holding the ps_parent table for updating the password hash
*/
global $PScfg;
$result = false;
$domain = $PScfg['domain'];
$PGAuser = $user;
$PGAoldPwd = $oldPwd;
$PGAnewPwd = $newPwd;
//get the current account details for this user so we can know if it has changed the password at the end of the procedure
$parInfo = PS_getPGAbyUsername($PSdbh, $PGAuser);
if ($parInfo !== false)
{
$preModCred = $parInfo['MOD_CRED'];
//first we want to visit the proper page to trigger ?something? on the server side code or else this process will fail.
//the trigger is unnecesary for this function
//$url = $PScfg['pga_trigger_change_pwd'] . '?credExpired=1&userName=$user';
//touch_ps_page($url);
//set POST variables
$url = $PScfg['pga_change_pwd'];
$fields = array(
'ac'=>urlencode('brij:public-parentaccess-pkg/SubmitChangePasswordForm'),
'doc'=>urlencode('/public/userprefpassword.html'),
'render_in_java'=>urlencode('true'),
'userType'=>urlencode('guardian'),
'userName'=>urlencode($PGAuser),
'credExpired'=>urlencode('2'),
'currentCredential'=>urlencode($PGAoldPwd),
'newCredential'=>urlencode($PGAnewPwd),
'newCredential1'=>urlencode($PGAnewPwd)
);
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
//add some more headers to the default
$headers = array_merge($PScfg['http_headers'],array(
"X-Requested-With: XMLHttpRequest",
"Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
"Content-Length: " . strlen($fields_string),
)
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //send along our custom headers from above
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//if the data seemed to be sent to the server properly, now we check to make sure that the database reflects the change
if ($result !== false)
{
$parInfo = PS_getPGAbyUsername($PSdbh, $PGAuser);
$postCred = $parInfo['CREDENTIAL'];
$postCredEnc = $parInfo['ENCRYPTIONMODE']; //For PS7 changed from ISCREDENTIALENC TO ENCRYPTIONMODE
$postModCred = $parInfo['MOD_CRED'];
$result = ($postModCred != $preModCred && $postCredEnc == 1) ? true : false;
//if the credential hash changed we need to update the ps_parent table so it matches on the next check.
if ($result)
{
//update the ps_parent table with the new encrypted password hash
//echo "\nStoring new password hash for user $PGAuser...";
$result = update_ps_parent_pcas_creds($AUXdbh, $PGAuser, $postCred);
//echo ($pcas_res) ? "success." : "failed!";
}
}
}
//return true/false, ONLY means the data was sent...not actually accepted
//For verification this record SHOULD be checked via ODBC somehow, because a simple test is not easily done here
//return ($result == 'success') ? true : false;
return $result;
}
function PS_getPGAProfileByEmail($email)
{
global $PScfg;
/*
* Queries the PS interface for a PGA with a specific e-mail address
* Input: E-mail Address (string)
* Outputs (inherited from PS_scrapePGAprofile): boolean false on no match found, or
* an array containing key/value pairs, 'gai', 'firstname', 'lastname', 'email' and 'username')
*/
//set POST variables
$url = $PScfg['guardian_query'];
$cookie = $PScfg['cookie'];
$fields = array(
'searchParameters.email'=>urlencode($email)
);
//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');
//add some more headers to the default
$headers = array_merge($PScfg['http_headers'],array(
"Content-Type: application/x-www-form-urlencoded",
"Content-Length: " . strlen($fields_string),
)
);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt ($ch, CURLOPT_COOKIEJAR, $cookie); //reuse the same cookiejar from above
curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookie); //send the same cookie data from above
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //send along our custom headers from above
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//search the source code for the important data
return PS_scrapePGAprofile($result);
}
function PS_scrapeSecrets($src)
{
/*
* Searches PS source code for the PSToken and PSKey
* Inputs: $src (The remote source code (as a string) just fetched from the PS login page)
* Outputs: $secrets (An array containing two key/value pairs, 'pskey' and 'pstoken')
*/
//PSKey pattern
$patternPSKey = '/var pskey = "(?P<pskey>\w+)"/';
//PSToken pattern
$patternPSToken = '/input type="hidden" name="pstoken" value="(?P<pstoken>\w+)"/';
//lookup the pskey in the source code
preg_match($patternPSKey, $src, $matches);
$pskey = $matches['pskey'];
//lookup the pstoken in the source code
preg_match($patternPSToken, $src, $matches);
$pstoken = $matches['pstoken'];
return array('pskey'=>$pskey,'pstoken'=>$pstoken);
}
function PS_scrapePGAprofile($src)
{
/*
* Searches PS source code for the PGA gai ***This can be done more reliably with ODBC access***
* Inputs: $src (The remote source code (as a string) just fetched from the PGA profile page)
* Outputs: boolean false on no match found, or
* an array containing key/value pairs, 'gai', 'firstname', 'lastname', 'email' and 'username')
*/
//PGAgai pattern
$patternPGAgai = '/input type="hidden" id="guardian-edit-form-gai" name="gai" value="(?P<PGAgai>\S+)"/';
//PGAfirst pattern
$patternPGAfirst = '/input type="text" value="(?P<PGAfirst>.+)" name="firstName"/';
//PGAlast pattern
$patternPGAlast = '/input type="text" value="(?P<PGAlast>.+)" name="lastName"/';
//PGAemail pattern
$patternPGAemail = '/input type="text" value="(?P<PGAemail>\S+)" name="email"/';
//PGAusername pattern
$patternPGAusername = '/input type="text" value="(?P<PGAusername>\S+)" name="username"/';
//lookup the PGA GAI in the source code
preg_match($patternPGAgai, $src, $matches);
$PGAgai = $matches['PGAgai'];
//if after this first test we didn't have any results, there is no need to proceed, just return false
if (count($matches) == 0)
{
return false;
}
//lookup the First Name in the source code
preg_match($patternPGAfirst, $src, $matches);
$PGAfirst = $matches['PGAfirst'];
//lookup the Last Name in the source code
preg_match($patternPGAlast, $src, $matches);
$PGAlast = $matches['PGAlast'];
//lookup the Email in the source code
preg_match($patternPGAemail, $src, $matches);
$PGAemail = $matches['PGAemail'];
//lookup the Username in the source code
preg_match($patternPGAusername, $src, $matches);
$PGAusername = $matches['PGAusername'];
return array('gai'=>$PGAgai, 'firstname'=>$PGAfirst, 'lastname'=>$PGAlast, 'email'=>$PGAemail, 'username'=>$PGAusername);
}
function PS_getPGADetails (&$dbh = false, $type, $val)
{
global $PScfg;
$pgaAry = false;
//test that we have a database connection and should therefore use SQL to retrieve this information
if($dbh !== false)
{
$whereClause = "WHERE 1=2";
switch ($type)
{
case 'all':
$whereClause = "";
break;
case 'email':
$whereClause = "WHERE pe.emailaddress = '$val'";
break;
case 'actid':
$whereClause = "WHERE g.accountidentifier = '$val'" ;
break;
case 'gid':
$whereClause = "WHERE g.guardianid = '$val'" ;
break;
case 'username':
$whereClause = "WHERE pa.username = '$val'" ;
break;
default:
//keep the default value from above
break;
}
//For PS7, removed pa.iscredentialencrypted added pa.encryptionmode and added mod_pcas and mod_cred
$query = <<<EOD
SELECT g.accountidentifier accountidentifi, pe.emailaddress email, g.firstname, g.guardianid, g.lastname, pa.credential, pa.encryptionmode, pa.isenabled, pa.pcas_accountid, pa.username, TO_CHAR(pa.whenmodified, 'YYYY-MM-DD HH24:MI:SS') mod_pcas, TO_CHAR(pa.credentialchangeddate, 'YYYY-MM-DD HH24:MI:SS') mod_cred
FROM guardian g
INNER JOIN pcas_account pa ON g.accountidentifier = pa.pcas_accounttoken
INNER JOIN pcas_emailcontact pe ON pa.pcas_accountid = pe.pcas_accountid
$whereClause
ORDER BY g.lastname, g.firstname, g.guardianid
EOD;
$array = psdb_fetch_array($dbh, $query);
//return a multi-dimensional array if there were any records returned
if ($array !== false && count($array) > 0) $pgaAry = $array;
}
//else we have a false database connection and should attempt to use screen scraping techniques to retrieve this information
else
{
//remove the "==" sign at the end of the base64 Guardian AccountIdentifier field because PS won't recieve it over GET URL
$val = ($type == 'actid') ? str_replace('==','',$val) : $val;
$url = $PScfg['pgainfo'] . "?type=" . urlencode($type) . "&val=" . urlencode($val);
$src = touch_ps_page($url);
$ary = html_table_to_array($src);
//return a multi-dimensional array if there were any records returned
if (count($ary) > 0) $pgaAry = $ary;
}
return $pgaAry;
}
function PS_getPGADetailsAll (&$dbh = false)
{
//returns a multidimensional array: [0] => array(key1 => val1, key2 => val2), [1] => array(key1 => val1, key2 => val2), [3] => ...etc.
//returns false on error or no records returned.
return PS_getPGADetails($dbh, 'all', '');
}
function PS_getPGAbyEmail (&$dbh, $email)
{
//returns a simple array: (key1 => val1, key2 => val2, ...etc)
//returns false on error or if other than exactly one record returned.
$ary = PS_getPGADetails ($dbh, 'email', $email);
return ($ary !== false && count($ary) == 1) ? $ary[0] : false;
}
function PS_getPGAbyActID (&$dbh, $actID)
{
//returns a simple array: (key1 => val1, key2 => val2, ...etc)
//returns false on error or if other than exactly one record returned.
$ary = PS_getPGADetails ($dbh, 'actid', $actID);
return ($ary !== false && count($ary) == 1) ? $ary[0] : false;
}
function PS_getPGAbyGID (&$dbh, $gID)
{
//returns a simple array: (key1 => val1, key2 => val2, ...etc)
//returns false on error or if other than exactly one record returned.
$ary = PS_getPGADetails ($dbh, 'gid', $gID);
return ($ary !== false && count($ary) == 1) ? $ary[0] : false;
}
function PS_getPGAbyUsername (&$dbh, $gID)
{
//returns a simple array: (key1 => val1, key2 => val2, ...etc)
//returns false on error or if other than exactly one record returned.
$ary = PS_getPGADetails ($dbh, 'username', $gID);
return ($ary !== false && count($ary) == 1) ? $ary[0] : false;
}
function PS_getPGAKids (&$dbh = false, $gid)
{
global $PScfg;
$kidAry = false;
//test that we have a database connection and should therefore use SQL to retrieve this information
if($dbh !== false)
{
$query = <<<EOD
SELECT studentsdcid, guardianrelationshiptypeid guardianrelatio
FROM guardianstudent
WHERE guardianid = '$gid'
EOD;
$array = psdb_fetch_array($dbh, $query);
if ($array !== false && count($array) > 0)
{
$kidAry = array();
foreach ($array as $key => $kid)
{
$kidAry[$kid['STUDENTSDCID']] = $kid['GUARDIANRELATIO'];
}
}
}
//else we have a false database connection and should attempt to use screen scraping techniques to retrieve this information
else
{
$url = $PScfg['pgakids'] . "?gid=" . urlencode($gid);
$src = touch_ps_page($url);
$ary = html_table_to_array($src);