summaryrefslogtreecommitdiffstats
path: root/main.php
blob: 52d46162b7918b8425e05abd34297f7c21e50eea (plain) (blame)
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
<?php
function strip_tags_content($text, $tags = '', $invert = FALSE) {
  preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags);
  $tags = array_unique($tags[1]);
  if(is_array($tags) AND count($tags) > 0) {
    if($invert == FALSE) {
      return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text);
    }
    else {
      return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text);
    }
  }
  elseif($invert == FALSE) {
    return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text);
  }
  return $text;
} 
function DOMinnerHTML(DOMNode $element) { 
    $innerHTML = ""; 
    $children  = $element->childNodes;
    foreach ($children as $child) { 
        $innerHTML .= $element->ownerDocument->saveHTML($child);
    }
    return $innerHTML; 
}
function endsWith($haystack, $needle) {
    $length = strlen($needle);
    if ($length == 0) {
        return true;
    }
    return (substr($haystack, -$length) === $needle);
}
function startsWith ($string, $startString) { 
    $len = strlen($startString); 
    return (substr($string, 0, $len) === $startString); 
}
function get_string_between($string, $start, $end){
    $string = ' ' . $string;
    $ini = strpos($string, $start);
    if ($ini == 0) return '';
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}
/*
Errors:
 -1 no login info
 -2 not logged in
 -3 bad username or password
 -4 not written yet
 -5 unable to create cookie dir
 -6 non existing user
*/
	class gimsisextClient {
		private $username;
		private $password;
		public $version = array(0, 7, 1);
		private $programname = "gimsisextclient";
		private $programdomain = 'gimsisextclient.gimb.tk';
		private $cookiedir; // set at runtime, ker je get_curerent_user, v login()
		private $gimsisextlogin = "https://zgimsis.gimb.org/gse/Logon.aspx";
		private $gimsisexturnik = "https://zgimsis.gimb.org/gse/Page_Gim/Ucenec/DnevnikUcenec.aspx";
		private $gimsisextocenjevanja = "https://zgimsis.gimb.org/gse/Page_Gim/Ucenec/IzpitiUcenec.aspx";
		private $gimsisextocene	= "https://zgimsis.gimb.org/gse/Page_Gim/Ucenec/OceneUcenec.aspx";
		private $gimsisextprofesorji = "https://zgimsis.gimb.org/gse/Page_Gim/Ucenec/UciteljskiZbor.aspx";
		private $gimsisextprofil = "https://zgimsis.gimb.org/gse/Page_Gim/Uporabnik/Profil.aspx";
		private $gimsisextshraniprofil = "https://zgimsis.gimb.org/gse/WS_Gim/wsGimSisUtils.asmx/ShraniUporabnikPodatki";
		private $gimsisextabout = "https://zgimsis.gimb.org/gse/About.aspx";
		private $gimsisextdefault = "https://zgimsis.gimb.org/gse/Default.aspx";
		private $gimsisextsporocila = "https://zgimsis.gimb.org/gse/Page_Gim/Uporabnik/Sporocila.aspx";
		private $gimsisextposljisporocilo = "https://zgimsis.gimb.org/gse/Page_Gim/Uporabnik/modSporocilo.aspx?params=";
		private $gimsisextsetgeslo = "https://zgimsis.gimb.org/gse/WS_Gim/wsGimSisUtils.asmx/ShraniUporabnikGeslo";
		private $gimsisextizbrisisporocilo = "https://zgimsis.gimb.org/gse/Page_Gim/Uporabnik/Sporocila.aspx/DeleteMessage";
		private $gimsisextizostanki = "https://zgimsis.gimb.org/gse/Page_Gim/Ucenec/IzostankiUcenec.aspx";
		private $gimsisextresetgeslo = "https://zgimsis.gimb.org/gse/ResetPassword.aspx";
		public function setusername($value) { 
			$this->username = $value;
 		}
		public function setpassword($value) { 
			$this->password = $value;
 		}
   		private function get($property) {
			return $this->$property;
		}
		public function getversion() {
			return $this->version;
		}
		private function login() {
			if (empty($this->username) || empty($this->password)) {
				return -1;
			}
			$this->cookiedir = '/tmp/'.posix_getuid().'/'.$this->programdomain.'/cookiedir/';
			if (!is_dir($this->cookiedir.$this->username)) {
				if (!mkdir($this->cookiedir.$this->username, 0700, true)) { // x permišn mora bit', da lahko dela poddirektorije, hence true, hence 0700; group in others pa je 0, da ne morejo brati piškotkov!!! zeloo pomembno!
					return -5;
				}
			}
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_0);
			curl_setopt($ch, CURLOPT_COOKIESESSION, true );
			curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiedir.$this->username."/cookie.txt" ); // cookiejar
			curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiedir.$this->username."/cookie.txt" ); // coolie file // this scuks
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			// curl_setopt($ch, CURLOPT_HEADER, 1); // return headers?
			curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return transfer?
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow 3xx redirects?
			curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // max 3xx redirectas?
			curl_setopt($ch, CURLOPT_USERAGENT, $this->programdomain."/".implode(".", $this->version));
			curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // auto send refereres?
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // timeout for tcp connection
			curl_setopt($ch, CURLOPT_TIMEOUT, 10); // timeout for http response
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextlogin);
			curl_setopt($ch, CURLOPT_POST, 0);
			$login_page = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $login_page );
			$searchNode = $xmlDoc->getElementsByTagName( "input" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute('name') != "edtGSEUserPassword" && $sn->getAttribute('name') != "edtGSEUserId")
				$postvars .= urlencode($sn->getAttribute('name'))."=".urlencode($sn->getAttribute('value'))."&";
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextlogin);
			curl_setopt($ch, CURLOPT_POST, 1);
			$postbody = $postvars."edtGSEUserId=".$this->username."&edtGSEUserPassword=".$this->password;
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
			$login_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $login_output );
			$searchNode = $xmlDoc->getElementsByTagName( "span" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute('id') == "lblMsg") {
					if (DOMinnerHTML($sn) == "Napačno uporabniško ime ali geslo!") {
						return -3;
					}
					if (startsWith(DOMinnerHTML($sn), "Uporabnik je začasno zaklenjen")) {
						return array(-5, end(explode(" ", DOMinnerHTML($sn))));
					}
				}
			}
			return $ch;
		}
		public function fetchurnik($date = null) { // date ponedeljka v formatu 22.10.2019
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisexturnik);
			curl_setopt($ch, CURLOPT_POST, 0);
			$urnik_init_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $urnik_init_output );
			$searchNode = $xmlDoc->getElementsByTagName( "input" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute('name') != 'ctl00$ContentPlaceHolder1$wkgDnevnik_edtGridSelectDate') {
					$postvars .= urlencode($sn->getAttribute('name'))."=".urlencode($sn->getAttribute('value'))."&";
				} else {
					if(empty($date)) {
						$date = $sn->getAttribute('value');
					}
				}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisexturnik);
			curl_setopt($ch, CURLOPT_POST, 1);
			$postbody = $postvars . urlencode('ctl00$ContentPlaceHolder1$wkgDnevnik_edtGridSelectDate') . '=' . $date;
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
			$urnik_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $urnik_output );
			$searchNode = $xmlDoc->getElementsByTagName( "span" );
			foreach( $searchNode as $sn ) {
				if(startsWith($sn->getAttribute("id"), "ctl00_ContentPlaceHolder1_wkgDnevnik_btnCell_")) {
					$krneki = explode("_", $sn->getAttribute("id"));
					$ura = $krneki[4];
					$dan = $krneki[5];
					$desc = str_replace("\r", null, $sn->getAttribute("title"));
					$desc = explode("\n", $desc);
					$predmet = get_string_between($desc[1], "(", ")"); // 0 je prazen string!!1
					$kratica = explode(" (", $desc[1])[0];
					$razred = $desc[2];
					$profa = $desc[3];
					$mesto = $desc[4];
					$urnik[intval($dan)][intval($ura)] = array(
						"predmet" => $predmet,
						"kratica" => $kratica,
						"razred" => $razred,
						"profesor" => $profa,
						"prostor" => $mesto
					);
				}
			}
			return $urnik;
		}
		public function fetchocenjevanja() {
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextocenjevanja);
			curl_setopt($ch, CURLOPT_POST, 0);
			$ocenjevanja_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $ocenjevanja_output );
			$tabelaNode = $xmlDoc->getElementsByTagName( "table" );
			$tbodyNode = $tabelaNode[0]->getElementsByTagName( "tbody" );
			$rowNodes = $tbodyNode[0]->getElementsByTagName("tr");
			$ocenjevanja = array();
			foreach ($rowNodes as $rn) {
				$tdnode = $rn->getElementsByTagName("td");
				$datum = DOMinnerHTML($tdnode[0]);
				$tdspannode = $tdnode[1]->getElementsByTagName("span");
				if ($tdspannode != null) $kratica = DOMinnerHTML($tdspannode[0]);
				$predmet = explode(" (", strip_tags_content(DOMinnerHTML($tdnode[1])))[0];
				$opis = get_string_between(strip_tags_content(DOMinnerHTML($tdnode[1])), "(", ")");
				$ocenjevanja[] = array(
					"datum" => str_replace("\n", null, str_replace("\r", null, str_replace(" ", null, $datum))),
					"kratica" => str_replace("\n", null, str_replace("\r", null, str_replace(" ", null, $kratica))),
					"predmet" => substr(str_replace("\n", null, str_replace("\r", null, str_replace("  ", null, $predmet))), 0, -2),
					"opis" => str_replace("\n", null, str_replace("\r", null, str_replace("  ", null, $opis)))
					);
			}
			return $ocenjevanja;
		}
		public function fetchprofesorji() {
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextprofesorji);
			curl_setopt($ch, CURLOPT_POST, 0);
			$profesorji_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $profesorji_output );
			$tabelaNode = $xmlDoc->getElementsByTagName( "table" );
			$tbodyNode = $tabelaNode[0]->getElementsByTagName( "tbody" );
			$rowNodes = $tbodyNode[0]->getElementsByTagName("tr");
			$profesorji = array();
			foreach ($rowNodes as $rn) {
				$tdnode = $rn->getElementsByTagName("td");
				$ime = strip_tags(DOMinnerHTML($tdnode[0]));
				$predmetistrings = explode("<br />", DOMinnerHTML($tdnode[2]));
				$predmeti = array();
				foreach ($predmetistrings as $predmet) {
					$predmetime = strip_tags(get_string_between(strip_tags($predmet), "(", ")"));
					$predmetkratica = strip_tags(explode(" (", strip_tags($predmet))[0]);
					if(empty($predmetime)) { $predmetime = $predmetkratica; $predmetkratica = null; }
					if(empty($predmetkratica)) $predmetkratica = substr($predmetime, 0, 3);
					$predmeti[] = array( "ime" => $predmetime, "kratica" => $predmetkratica);
				}
				$govorilneurestring = DOMinnerHTML($tdnode[3]);
				$dan = explode(", ", $govorilneurestring)[0];
				if($dan == "ponedeljek") {
					$govorilneuredan = 0;
				} elseif ($dan == "torek") {
					$govorilneuredan = 1;
				} elseif ($dan == "sreda") {
					$govorilneuredan = 2;
				} elseif (endsWith($dan, "etrtek")) { // because č can be tricky
					$govorilneuredan = 3;
				} elseif ($dan == "petek") {
					$govorilneuredan = 4;
				}
				$solskaura = intval(get_string_between($govorilneurestring, ", ", ". ura"));
				$uraod = explode(" - ", get_string_between($govorilneurestring, "(", ")"))[0];
				$urado = explode(" - ", get_string_between($govorilneurestring, "(", ")"))[0];
				$profesorji[] = array(
					"ime" => $ime,
					"predmeti" => $predmeti,
					"govorilneure" => array("dan" => $govorilneuredan, "solskaura" => $solskaura, "uraod" => $uraod, "urado" => $urado)
				);
			}
			return $profesorji;
		}
		public function fetchprofil() {
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextprofil);
			curl_setopt($ch, CURLOPT_POST, 0);
			$profil_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $profil_output );
			$spanNodes = $xmlDoc->getElementsByTagName( "span" );
			$inputNodes = $xmlDoc->getElementsByTagName( "input" );
			foreach ($spanNodes as $sn) {
				switch($sn->getAttribute("id")) {
					case "ctl00_ContentPlaceHolder1_lblVrstaUporabnik":
						$vrsta = DOMinnerHTML($sn);
						break;
					case "ctl00_ContentPlaceHolder1_lblIme":
						$ime = DOMinnerHTML($sn);
						break;
					case "ctl00_ContentPlaceHolder1_lblPriimek":
						$priimek = DOMinnerHTML($sn);
						break;
					case "ctl00_ContentPlaceHolder1_lblSpol":
						$spol = DOMinnerHTML($sn);
						break;
					case "ctl00_ContentPlaceHolder1_lblEPosta":
						$eposta = DOMinnerHTML($sn);
						break;
					case "ctl00_ContentPlaceHolder1_lblTelefon":
						$telefon = DOMinnerHTML($sn);
						break;
				}
			}
			foreach ($inputNodes as $in) {
				switch($in->getAttribute("id")) {
					case "ctl00_ContentPlaceHolder1_chbEPosta":
						$checked = $in->getAttribute("checked");
						break;
				}
			}
			$obvestila = 0;
			if($checked == "checked") $obvestila = 1;
			return array("ime" => $ime, "priimek" => $priimek, "spol" => $spol, "vrsta" => $vrsta, "eposta" => $eposta, "obvestila" => $obvestila, "telefon" => $telefon);
		}
		public function setprofil($ime, $priimek, $spol, $eposta, $obvestila, $telefon) { // spol: "M"/"Ž" obvestila:"true"/"false" telefon: +tccpndddddd (npr. +38664176345)
			// PATCH! od verzije 1.0.7226.34224 dalje je blokirano spreminjanje osebnih podatkov dijakom
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			$podatki = 'IdUporabnik='.$this->username.'|edtIme='.$ime.'|edtPriimek='.$priimek.'|edtSpol='.$spol.'|edtEPosta='.$eposta.'|edtIndObvEPosta='.$obvestila.'|edtTelefon='.$telefon.'|edtIndObvSMS=undefined|';
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextshraniprofil);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/json',
                                            'X-Requested-With: XMLHttpRequest',
											'Referer: https://zgimsis.gimb.org/gse/Page_Gim/Uporabnik/modVnosPodatki.aspx?params='.base64_encode("Id=".$this->username."|Type=")
                                            ));
			$postbody = '{ "aPodatki": "'.base64_encode($podatki).'" }';
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
			$setprofil_output = curl_exec($ch);
			if(json_decode($setprofil_output)['d']['success'] == true) {
				return true;
			} else {
				return false;
			}
		}
		public function fetchabout() {
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextabout);
			curl_setopt($ch, CURLOPT_POST, 0);
			$profil_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $profil_output );
			$spanNodes = $xmlDoc->getElementsByTagName( "span" );
			foreach ($spanNodes as $sn) {
				switch($sn->getAttribute("id")) {
					case "ctl00_ContentPlaceHolder1_lblCopyright":
						$copyright = html_entity_decode(DOMinnerHTML($sn));
						break;
					case "ctl00_ContentPlaceHolder1_lblTitle":
						$ime = html_entity_decode(DOMinnerHTML($sn));
						break;
					case "ctl00_ContentPlaceHolder1_lblDescription":
						$opis = html_entity_decode(DOMinnerHTML($sn));
						break;
					case "ctl00_ContentPlaceHolder1_lblVersion":
						$ver = html_entity_decode(DOMinnerHTML($sn));
						break;
					case "ctl00_ContentPlaceHolder1_lblCompany":
						$podjetje = html_entity_decode(DOMinnerHTML($sn));
						break;
					case "ctl00_ContentPlaceHolder1_lblConfiguration":
						$konfiguracija = html_entity_decode(DOMinnerHTML($sn));
						break;
				}
			}
			return array("ime" => $ime, "opis" => $opis, "ver" => $ver, "copyright" => $copyright, "podjetje" => $podjetje, "konfiguracija" => $konfiguracija);
		}
		public function fetchneprebrana() {
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextdefault);
			curl_setopt($ch, CURLOPT_POST, 0);
			$prebrana_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $prebrana_output );
			$spanNodes = $xmlDoc->getElementsByTagName( "span" );
			foreach ($spanNodes as $sn) {
				switch($sn->getAttribute("class")) {
					case "titleRed":
						$unread = html_entity_decode(DOMinnerHTML($sn));
						break;
				}
			}
			return intval($unread);

		}
		public function fetchsporocilaseznam($katera = 0) { // katera sporočila? 0=prejeta 1=poslana 2=izbrisana
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextsporocila);
			switch($katera) {
				case 0:
					$kategorija = "msgReceived";
					$slokat = "prejeta";
					break;
				case 1:
					$kategorija = "msgSent";
					$slokat = "poslana";
					break;
				case 2:
					$kategorija = "msgDeleted";
					$slokat = "izbrisana";
					break;
			}
			curl_setopt($ch, CURLOPT_POST, 0);
			$sporocilaseznam_init_output = curl_exec($ch);
			$xmlDoc = new DOMDocument($sporocilaseznam_init_output);
			$xmlDoc->loadHTML( $sporocilaseznam_init_output );
			$searchNode = $xmlDoc->getElementsByTagName( "input" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute('name') != 'ctl00$ContentPlaceHolder1$ddlPrikaz' && $sn->getAttribute('name') != '__EVENTARGUMENT') {
					$postvars .= urlencode($sn->getAttribute('name'))."=".urlencode($sn->getAttribute('value'))."&";
				}
			}
			curl_setopt($ch, CURLOPT_POST, 1);
			$msgkat = array();
			$zadnjastran = 1;
			for ($stran = 1; $stran <= $zadnjastran; $stran++) {
				$postbody = $postvars . urlencode('ctl00$ContentPlaceHolder1$ddlPrikaz') . '=' . $kategorija . '&' . '__EVENTARGUMENT=Page%24' . $stran . '&' .
				'__EVENTTARGET=ctl00%24ContentPlaceHolder1%24gvwSporocila';
				curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
				$sporocilaseznam_output = curl_exec($ch);
				$xmlDoc = new DOMDocument();
				$xmlDoc->loadHTML( $sporocilaseznam_output );
				if ($stran == 1) {
					foreach ($xmlDoc->getElementsByTagName("tr") as $rn) {
						if ($rn->getAttribute("class") == "pager") {
							;
							$zadnjastran = sizeof($rn->getElementsByTagName("td")[0]->getElementsByTagName("table")[0]->getElementsByTagName("tr")[0]->getElementsByTagName("td"));
						}
					}
					if ($zadnjastran < 1) {
						$zadnjastran = 1;
					}
				}
				$tabelaNode = $xmlDoc->getElementsByTagName( "table" );
				foreach($tabelaNode as $tn) {
					if ($tn->getAttribute("id") == "ctl00_ContentPlaceHolder1_gvwSporocila") {
						$msg = array();
						foreach ($tn->getElementsByTagName("tbody")[0]->getElementsByTagName("td") as $dn) {
							foreach($dn->getElementsByTagName('span') as $sn) {
								$kles = $sn->getAttribute("class");
								if ( $kles == "msgSubDate") {
									$datum = null;
									$dan = null;
									$mesec = null;
									$leto = null;
									$datetime = DOMinnerHTML($sn);
									$datum = explode(" ", $datetime)[0];
									$ura = explode(" ", $datetime)[1];
									if (empty($ura)) {
										$ura = $datum;
										$datum = date("d.m.Y");
									}
									$dan = intval(explode(".", $datum)[0]);
									$mesec = intval(explode(".", $datum)[1]);
									$leto = intval(explode(".", $datum)[2]);
									if (empty($leto)) {
										$leto = intval(date("Y"));
									}
									$msg['datum'] = array( "dan" => $dan, "mesec" => $mesec, "leto" => $leto );
									$msg['cas'] = array("ura" => intval(explode(":", $ura)[0]), "minuta" => intval(explode(":", $ura)[1]));
								} elseif ( $kles == "msgDir") {
									$msg['posiljatelj'] = DOMinnerHTML($sn);
								} elseif (startsWith($kles, "msgSubject")) {
									$msg['zadeva'] = DOMinnerHTML($sn);
								}
								}
							$msg['id'] = $dn->getElementsByTagName("input")[0]->getAttribute("value");
							$msgkat[] = $msg;
						}
					}
				}
				// file_put_contents("/tmp/m".$stran.".json", json_encode($msgkat)); // debug
				// file_put_contents("/tmp/krneki".$stran.".txt", $postbody); // debug
				// file_put_contents("/tmp/o".$stran.".txt", $sporocilaseznam_output); // debug
			}
			return $msgkat;
		}
		public function fetchsporocilo($id) { // id formata ddddd|ddddd|d
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextsporocila);
			curl_setopt($ch, CURLOPT_POST, 0);
			$sporocilo_init_output = curl_exec($ch);
			$xmlDoc = new DOMDocument($sporocilo_init_output);
			$xmlDoc->loadHTML( $sporocilo_init_output );
			$searchNode = $xmlDoc->getElementsByTagName( "input" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute('name') != 'ctl00$ContentPlaceHolder1$hfIdSporocilo') {
					$postvars .= urlencode($sn->getAttribute('name'))."=".urlencode($sn->getAttribute('value'))."&";
				}
			}
			curl_setopt($ch, CURLOPT_POST, 1);
			$postbody = $postvars . urlencode('ctl00$ContentPlaceHolder1$hfIdSporocilo') . '=' . urlencode($id);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
			$sporocilo_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $sporocilo_output );
			$tabelaNode = $xmlDoc->getElementsByTagName( "table" );
			foreach($tabelaNode as $tn) {
				if ($tn->getAttribute("id") == "ctl00_ContentPlaceHolder1_gvwSporociloVrsta") {
					$msg = array();
					foreach ($tn->getElementsByTagName("tbody")[0]->getElementsByTagName("td") as $dn) {
						foreach($dn->getElementsByTagName('span') as $sn) {
							switch($sn->getAttribute("class")) {
								case "msgSubjectS":
									$msg['zadeva'] = DOMinnerHTML($sn);
									break;
							}
						}
						$msg['id'] = $id;
						foreach($dn->getElementsByTagName('div') as $divn) {
							if ($divn->getAttribute("class") == "msgInfo") {
								$msg['posiljatelj'] = explode(" (", DOMinnerHTML($divn->getElementsByTagName('span')[0]))[0];
								$msg['datum'] = explode(" ", get_string_between(DOMinnerHTML($divn->getElementsByTagName('span')[0]), "(", ")"))[0];
								$msg['ura'] = explode(" ", get_string_between(DOMinnerHTML($divn->getElementsByTagName('span')[0]), "(", ")"))[1];
								$msg['prejemnik'] = DOMinnerHTML($divn->getElementsByTagName('span')[1]);
							} elseif ($divn->getAttribute("class") == "gCursorAuto") {
								$msg['telo'] = explode("<hr>", DOMinnerHTML($divn))[0];
							}
						}
					}
				}
			}
			return $msg;
		}
		public function posljisporocilo($prejemnikst, $zadeva, $telo) { // prejemnikst je zajeban dobit (za zdej)
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_POST, 0);
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextposljisporocilo);
			$posljisporocilo_init_output = curl_exec($ch);
			$xmlDoc = new DOMDocument($posljisporocilo_init_output);
			$xmlDoc->loadHTML( $posljisporocilo_init_output );
			$searchNode = $xmlDoc->getElementsByTagName( "input" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute("name") != 'ctl00$ModalMasterBody$edtPrejemniki' && $sn->getAttribute("name") != 'ctl00$ModalMasterBody$edtZadeva' && $sn->getAttribute("name") != 'ctl00$ModalMasterBody$edtBesediloExt' && $sn->getAttribute("name") != 'ctl00$ModalMasterBody$hfPrejemniki') {
					$postvars .= urlencode($sn->getAttribute('name'))."=".urlencode($sn->getAttribute('value')).'&';
				}
			}
			curl_setopt($ch, CURLOPT_POST, 1);
			// curl_setopt($ch, CURLOPT_HEADER, array('array("Content-Type: multipart/form-data")'));
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextposljisporocilo);
			$postbody=$postvars.'&'.urlencode('ctl00$ModalMasterBody$edtZadeva').'='.urlencode($zadeva).'&'.urlencode('ctl00$ModalMasterBody$edtBesediloExt').'='.urlencode($telo).'&__EVENTTARGET=ctl00%24ModalMasterBody%24btnDogodekShrani&__EVENTARGUMENT=&'.urlencode('ctl00$ModalMasterBody$hfPrejemniki').'='.urlencode($prejemnikst);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
			$posljisporocilo_output = curl_exec($ch);
			// file_put_contents("/tmp/psio.html", $posljisporocilo_init_output); // debug
			// file_put_contents("/tmp/pso.html", $posljisporocilo_output); // debug
			// file_put_contents("/tmp/pb.html", $postbody); // debug
			// file_put_contents("/tmp/p.html", $prejemnikst); // debug
			return;
		}
		public function setgeslo($geslo, $spremenigeslovobjektu = true) { // geslo
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			$podatki = 'IdUporabnik='.$this->username.'|edtStaroGeslo='.$this->password.'|edtGeslo='.$geslo.'|edtGeslo2='.$geslo.'|';
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextsetgeslo);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/json',
                                            'X-Requested-With: XMLHttpRequest',
											'Referer: '.$this->gimsisextsetgeslo.'?params='.base64_encode("Id=".$this->username."|Type=")));
			$postbody = '{ "aPodatki": "'.base64_encode($podatki).'" }';
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
			$setgeslo_output = curl_exec($ch);
			if(json_decode($setgeslo_output, true)['d']['success'] == true) {
				if($spremenigeslovobjektu) {
					$this->password = $geslo;
				}
				return true;
			} else {
				return false;
			}
		}
		public function izbrisisporocilo($id) {
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextizbrisisporocilo);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                                            'Content-Type: application/json',
                                            'X-Requested-With: XMLHttpRequest',
											'Referer: '.$this->gimsisextsporocila));
			$postbody = '{"aIdSporocilo":'.explode("|", $id)[0].',"aIdZapis":'.explode("|", $id)[1].'}';
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
			$izbrisisporocilo_output = curl_exec($ch);
			if(json_decode($izbrisisporocilo_output, true)['d'] == true) {
				return true;
			} else {
				return false;
			}
		}
		public function fetchizostanki($datzacetka = "01.01.0001", $datkonca = "31.12.9999") { // ali pa arraya(dan, mesec, leto)
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			$datkategorije = array("datzacetka", "datkonca");
			foreach($datkategorije as $datkat) {
				if ( is_array($$datkat) ) {
					$$datkat = implode(".", $$datkat);
				}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextizostanki);
			curl_setopt($ch, CURLOPT_POST, 0);
			$izostanki_init_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $izostanki_init_output );
			$searchNode = $xmlDoc->getElementsByTagName( "input" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute('name') != 'ctl00$ContentPlaceHolder1$edtDatZacetka' && $sn->getAttribute('name') != 'ctl00$ContentPlaceHolder1$edtDatKonca') {
					$postvars .= urlencode($sn->getAttribute('name'))."=".urlencode($sn->getAttribute('value'))."&";
				} else {
					if(empty($date)) {
						$date = $sn->getAttribute('value');
					}
				}
			}
			curl_setopt($ch, CURLOPT_POST, 1);
			$postbody = $postvars . urlencode('ctl00$ContentPlaceHolder1$edtDatZacetka') . '=' . urlencode($datzacetka) . '&' . urlencode('ctl00$ContentPlaceHolder1$edtDatKonca') . '=' . urlencode($datkonca);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
			$izostanki_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $izostanki_output );
			$searchNode = $xmlDoc->getElementsByTagName( "table" );
			foreach( $searchNode as $sn ) {
				if(startsWith($sn->getAttribute("id"), "ctl00_ContentPlaceHolder1_gvwIzostankiGroup")) {
					$izostanki = array();
					foreach($sn->getElementsByTagName('tbody')[0]->getElementsByTagName('tr') as $rn) {
						$datum = DOMinnerHTML($rn->getElementsByTagName('td')[0]);
						$dat['dan'] = intval(explode(".", $datum)[0]);
						$dat['mesec'] = intval(explode(".", $datum)[1]);
						$dat['leto'] = intval(explode(".", $datum)[2]);
						$predmeti = DOMinnerHTML($rn->getElementsByTagName('td')[2]);
						$predmet = explode(", ", $predmeti);
						$predm = array();
						foreach($predmet as $pr) {
							$ime = explode(" (", $pr)[0];
							switch(get_string_between($pr, '(<span class="opr', '">')) {
								case "0":
									$opravicenoopis = "ni obdelano";
									break;
								case "1":
									$opravicenoopis = "opravičeno";
									break;
								case "2":
									$opravicenoopis = "neopravičeno";
									break;
								case "3":
									$opravicenoopis = "ne šteje";
									break;
								
							}
							$opravicenostatus = intval(get_string_between($pr, '(<span class="opr', '">'));
							$ura = intval(get_string_between($pr, '">', '</span>'));
							$predm[] = array("ime" => $ime, "opraviceno" => array( "status" => $opravicenostatus, "opis" => $opravicenoopis), "ura" => $ura);
						}
						$stevilo = intval(DOMinnerHTML($rn->getElementsByTagName('td')[1]));
						$izostanki[] = array("datum" => $dat, "stevilo" => $stevilo, "predmeti" => $predm);
					}
				}
			}
			return $izostanki;
		}
		public function resetgeslo($user) {
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_0);
			curl_setopt($ch, CURLOPT_COOKIESESSION, true );
			curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
			curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return transfer?
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // follow 3xx redirects?
			curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // max 3xx redirectas?
			curl_setopt($ch, CURLOPT_USERAGENT, $this->programdomain."/".implode(".", $this->version));
			curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // auto send refereres?
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // timeout for tcp connection
			curl_setopt($ch, CURLOPT_TIMEOUT, 10); // timeout for http response
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextresetgeslo);
			curl_setopt($ch, CURLOPT_POST, 0);
			$resetgeslo_init_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $resetgeslo_init_output );
			$searchNode = $xmlDoc->getElementsByTagName( "input" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute('name') != 'edtGSEUserId') {
					$postvars .= urlencode($sn->getAttribute('name'))."=".urlencode($sn->getAttribute('value'))."&";
				}
			}
			curl_setopt($ch, CURLOPT_URL, $this->gimsisextresetgeslo);
			curl_setopt($ch, CURLOPT_POST, 1);
			$postbody = $postvars . urlencode('edtGSEUserId') . '=' . $user;
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
			$resetgeslo_output = curl_exec($ch);
			if(get_string_between($resetgeslo_output, "Uporabnik z vne", "enim uporabni") == "s") {
				return -6;
			} else {
				$odg = get_string_between($resetgeslo_output, "na e-poštni naslov ", ".</span>");
				$odg2 = get_string_between($resetgeslo_output, "na e-poštni naslov ", ".</font>");
				if(strlen($odg) < strlen($odg2)) return $odg2;
			}
		}
	}
?>