summaryrefslogtreecommitdiffstats
path: root/main.php
blob: b0cc1c788d1d0e6419e8cb385839c515d04411bd (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
<?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 is_domDocument($object)
{
    if($object instanceof DOMDocument) {
        return true;
    }
    else {
        return false;
    }
}
function DOMinnerHTML($element) { 
	try {
		$innerHTML = ""; 
		$children  = $element->childNodes;
		foreach ($children as $child) { 
			$innerHTML .= $element->ownerDocument->saveHTML($child);
		}
		return $innerHTML; 
	} catch(Exception $e) {
		echo "EXCEPTION IN DOMinnerHTML: ", $e->getMessage();
	}
}
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 username or password not specified (aj blin dis a mast sou dej hev jor informejšon)
 -2 you are not logged in (aj blin trms of srvis bi neseseri)
 -3 bad username and/or password (diplomatski odgovor ;-)
 -4 not written yet (suka bljet)
 -5 session exists in another system (sucky)
 -6 error in fetched data
*/
	class itisClient {
		private $username;
		private $password;
		public $version = array(0, 1, 2);
		private $programname = "itisclient";
		private $programdomain = 'itisclient.sijanec.tk';
		private $cookiedir; // set at runtime, ker je get_curerent_user, v login()
		private $mainpage = "https://www.itis.si/";
		private $mainpageaction = "https://www.itis.si/?rw=1";
		private $iskanjestran = "https://www.itis.si/iskanje/stran-";
		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->mainpage);
			curl_setopt($ch, CURLOPT_POST, 0);
			$login_page = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $login_page );
			$formNode = $xmlDoc->getElementsByTagName( "form" )[0]; // only one form
			$searchNode = $formNode->getElementsByTagName("input");
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute('name') != 'ctl00$navigationLogin$tbUsername' && $sn->getAttribute('name') != 'ctl00$navigationLogin$tbPassword' &&
					$sn->getAttribute("type") != "submit")
				$postvars .= urlencode($sn->getAttribute('name'))."=".urlencode($sn->getAttribute('value'))."&";
			}
			curl_setopt($ch, CURLOPT_URL, $this->mainpageaction);
			curl_setopt($ch, CURLOPT_POST, 1);
			$postbody = $postvars.urlencode('ctl00$navigationLogin$tbUsername')."=".urlencode($this->username)."&".
				urlencode('ctl00$navigationLogin$tbPassword')."=".urlencode($this->password)."&".urlencode('ctl00$navigationLogin$btnLogin')."=".
					urlencode("PRIJAVA");
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);
			$login_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $login_output );
			$searchNode = $xmlDoc->getElementsByTagName( "div" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute('id') == 'navigationLogin_divNapaka') {
					if (DOMinnerHTML($sn) == 'Napačno uporabniško ime in/ali geslo.') {
						return -3;
					} else if(DOMinnerHTML($sn) ==
						'To uporabniško ime je v že v uporabi na drugem računalniku. Odjavite se iz drugega računalnika ali počakajte, da vam poteče seja.') {
						return -5; // holy shit u fukkin kill.d her, TSMedia!
					}
				}
			}
			return $ch;
		}
		public function fetchregije() {
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $this->mainpage);
			curl_setopt($ch, CURLOPT_POST, 0);
			$request_output = curl_exec($ch);
			$xmlDoc = new DOMDocument();
			$xmlDoc->loadHTML( $request_output );
			$searchNode = $xmlDoc->getElementById( "search_ddlRegion" )->getElementsByTagName("option");
			$regije=array();
			foreach( $searchNode as $sn ) {
				if(!empty($sn->getAttribute("value"))) $regije[] = $sn->getAttribute("value");
			}
			return $regije;
		}
		public function fetchkontakti($kajsplohiscemo = 0, $regija = "GORENJSKA") { // kajsplohiscemo-bool=[0-osebe]/1-podjetja regija-string=[GORENJSKA]/...
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			if($kajsplohiscemo) {
				$barva = "yellow"; // wtf, Adacta
				return -4;
			} else {$barva = "white";}
			curl_setopt($ch, CURLOPT_POST, 0);
			curl_setopt($ch, CURLOPT_URL, $this->mainpage);
			$request_init_output = curl_exec($ch);
			$xmlDoc = new DOMDocument($request_init_output);
			$xmlDoc->loadHTML( $request_init_output );
			$searchNode = $xmlDoc->getElementsByTagName("form")[0]->getElementsByTagName( "input" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute("name") != 'ctl00$search$hfSearchMode' && $sn->getAttribute("name") != 'ctl00$search$ddlRegion') {
					$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->mainpageaction);
			$postbody=$postvars.urlencode('ctl00$search$hfSearchMode').'='.urlencode($barva).'&'.urlencode('ctl00$search$ddlRegion').'='.
				urlencode($regija).'&__EVENTTARGET=&__EVENTARGUMENT=&ctl00%24search%24hfSearchFormMode=2&ctl00%24search%24ddlAreaCode=&ctl00%24search%24ddlWorkingHoursDay=&ctl00%24search%24ddlWorkingHoursFrom=&ctl00%24search%24ddlWorkingHoursTo=&ctl00%24search%24ddlAdditionalOptions=&hiddenInputToUpdateATBuffer_CommonToolkitScripts=1'; // (-;
			if($kajsplohiscemo){ $postbody .= '&'.urlencode('ctl00$search$btnSearchAlt').'='.urlencode('NAJDI');}		// THIS FINNALY WORKS BUT ONLY FOR
				else {$postbody .= '&'.urlencode('ctl00$search$btnSearch').'='.urlencode('IŠČI');}						// LOGGED IN USERS! DON'T TOUCH
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);															// THE ANCIENT CODE!
			$request_output = curl_exec($ch);									// https://www.reddit.com/r/ProgrammerHumor/comments/9xat04/the_ancient_code/
			curl_setopt($ch, CURLOPT_POST, 0);
			curl_setopt($ch, CURLOPT_URL, $this->iskanjestran."064176345"); // just a big intager
			$request_after_output = curl_exec($ch);
			$xmlDoc = new DOMDocument($request_after_output);
			$xmlDoc->loadHTML($request_after_output); 
			$zadnjastran = explode("-", $xmlDoc->getElementById("CPH_bodyMain_SearchResultsStatic1_ResultsPagerStatic1_aLastPage")->getAttribute("href"))[1];
			$kontakti = array();
			for($i = 1; $i <= $zadnjastran; $i++) {
				curl_setopt($ch, CURLOPT_POST, 0);
				curl_setopt($ch, CURLOPT_URL, $this->iskanjestran.$i); // just a bing intager
				$request_loop_output = curl_exec($ch);
				$xmlDoc = new DOMDocument($request_loop_output);
				$xmlDoc->loadHTML($request_loop_output);
				$searchNode = $xmlDoc->getElementById('CPH_bodyMain_SearchResultsStatic1_UpdatePanelSearchResults')->getElementsByTagName("a");
				foreach($searchNode as $sn) {
					if(startsWith($sn->getAttribute("href"), "/oseba/")) $kontakti[] =
						preg_replace_callback('/[^\x20-\x7f]/', function($match) {
							return urlencode($match[0]);
						}, "https://www.itis.si".$sn->getAttribute("href")); // just don't change your domain
				}
			}
			return $kontakti;
		}
		public function fetchoseba($personlink) { // personlink, začenjši z https://www.itis.si/oseba/
			$ch = $this->login();
			if(!curl_getinfo($ch)) {
				if(!empty($ch)){return $ch;}else{return -2;}
			}
			curl_setopt($ch, CURLOPT_URL, $personlink);
			curl_setopt($ch, CURLOPT_POST, 0);
			$request_output = curl_exec($ch);
			if ($request_output == null) return -6;
			$xmlDoc = new DOMDocument($request_output);
			$xmlDoc->loadHTML($request_output);
			$oseba = array();
			$oseba['name'] = DOMinnerHTML($xmlDoc->getElementsByTagName("h1")[0]);
			foreach ($xmlDoc->getElementsByTagName('a') as $ej) {
				if (endsWith($ej->getAttribute("id"), "hlMapAddress")) {
					$addresstext = $ej;
				}
			}				
			$oseba['address'] = explode('<br>', str_replace("\n", null, str_replace("\r", null, str_replace("  ", null, DOMinnerHTML($addresstext)))));
			$oseba['nr'] = array();
			foreach($xmlDoc->getElementsByTagName("div") as $sn) {
				if($sn->getAttribute("class") == "nr") $oseba['nr'][] = str_replace(" ", null, strip_tags_content(DOMinnerHTML($sn)));
			}
			$oseba['nr'] = array_unique($oseba['nr']);
			// $oseba['gps'] = get_string_between($ej->getAttribute("href"), 'javascript:openMap(',",&#39;".$oseba['name']);
			$oseba['gps']['y'] = get_string_between($request_output, '&amp;Y=', '&amp;n=');
			$oseba['gps']['x'] = get_string_between($request_output, '/zemljevid/X=', '&amp;Y=');
				return $oseba;
		}
		public function posljisms($telefonska = "064176345", $prefix = "(poslal itisclient) ", $telo = "itisclient") { // kajsplohiscemo-bool=[0-osebe]/1-podjetja regija-string=[GORENJSKA]/...
			$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->mainpage);
			$request_init_output = curl_exec($ch);
			$xmlDoc = new DOMDocument($request_init_output);
			$xmlDoc->loadHTML( $request_init_output );
			$searchNode = $xmlDoc->getElementsByTagName("form")[0]->getElementsByTagName( "input" );
			foreach( $searchNode as $sn ) {
				if($sn->getAttribute("name") != 'ctl00$CPH_bodyMain$SendSMS$Sender1$btnSend'
				&& $sn->getAttribute("name") != 'ctl00$CPH_bodyMain$SendSMS$Sender1$hfMessage'
				&& $sn->getAttribute("name") != 'ctl00$CPH_bodyMain$SendSMS$Sender1$tbNumber'
				&& $sn->getAttribute("name") != 'ctl00$CPH_bodyMain$SendSMS$Sender1$tbMessage'
				&& $sn->getAttribute("name") != 'ctl00$search$btnSearchAlt'
				&& $sn->getAttribute("name") != 'ctl00$search$btnSearch') {
					$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->mainpageaction);
			$postbody='__EVENTARGUMENT=&__EVENTTARGET=&'.$postvars.urlencode('ctl00$CPH_bodyMain$SendSMS$Sender1$btnSend').'='.urlencode("Pošlji").'&'.
				urlencode('ctl00$CPH_bodyMain$SendSMS$Sender1$hfMessage').'='.urlencode($prefix).'&'.
				urlencode('ctl00$CPH_bodyMain$SendSMS$Sender1$tbNumber').'='.urlencode($telefonska).'&'.
				urlencode('ctl00$CPH_bodyMain$SendSMS$Sender1$tbMessage').'='.urlencode($telo).
				'&ctl00%24CPH_bodyMain%24SendSMS%24Sender1%24hfSponsorMessage=a&hiddenInputToUpdateATBuffer_CommonToolkitScripts=1'; // (-;
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postbody);															// THE ANCIENT CODE!
			$request_output = curl_exec($ch);									// https://www.reddit.com/r/ProgrammerHumor/comments/9xat04/the_ancient_code/
			if(curl_getinfo(CURLINFO_RESPONSE_CODE) == 200) {
			return true;
			} else {
				return false;
			}
		}
	}
?>