summaryrefslogblamecommitdiffstats
path: root/main.php
blob: e453cae47ee3749a4c3c137006737725d3bc4ff8 (plain) (tree)




























































                                                                                                                              
                                     






                                  
                                                 
                                                         
                                                                                          

                                                                                          
                                                               

                                                                                
                                                                          






















































                                                                                                                                                                                                                                                    
                                                                                                          







                                                                                                                                  













                                                                                                                                              
                         






                                                                                        






                                                                                       




                                                                                                                                                      





                                                                              




















                                                                                                                                                  







                                                                                                                
                                          















                                                                                                                              
                                              












                                                                                                       














                                                                                                                 

                                                  
                                              




















                                                                                                             

         
#!/usr/bin/env php
<?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 value from argument not supported
 -6 error in fetched data
  0 bad
  1 good
*/
	class oldredditClient {
		private $username;
		private $password;
		public $version = array(0, 0, 1);
		private $programname = "oldredditclient";
		private $adminemail = "a"."n"."t"."o"."n"."@"."s"."i"."j"."a"."n"."ec.eu";
		private $programdomain = 'oldredditclient.sijanec.eu';
		private $cookiedir; // set at runtime, ker je get_curerent_user, v login()
		private $redditurl = "https://old.reddit.com/";
		private $loginurl = "https://old.reddit.com/api/login/";
		private $setflairurl = "https://old.reddit.com/api/selectflair";
		private $modflairurl = "https://old.reddit.com/api/flair";
		public $modhash = "";
		private $loggedin = false;
		public $ch = false;
		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;
		}
		public 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;
				}
			}
			if(curl_getinfo($this->ch)) {
				return 2;
			}
			$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->loginurl.$this->username);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, 'op=login-main&user='.$this->username.'&passwd='.$this->password.'&api_type=json');
			$login_output = curl_exec($ch);
			if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
				return false;
			}
			$this->modhash = json_decode($login_output, true)["json"]["data"]["modhash"];
			$this->ch = $ch;
			return true;
		}
		public function setflair($sub = "Slovenia", $flair = null, $magic = null, $post = false) {
			if(!curl_getinfo($this->ch)) {
				if (!$this->login()) {
					return -2;
				}
			}
			$ch = $this->ch;
			if($flair == null) {
				$dnevi = array("drekek", "ponedeljek", "torek", "sreda", "cetrtek", "petek", "sobota", "nedelja");
				$wiki = file_get_contents("https://en.wikipedia.org/wiki/Template:2019%E2%80%9320_coronavirus_pandemic_data");
				$xmlDoc = new DOMDocument($wiki);
				$xmlDoc->loadHTML( $wiki );
				$searchNode = $xmlDoc->getElementById( "thetable" )->getElementsByTagName("tr");
				foreach( $searchNode as $sn ) {
					if (strpos(DOMinnerHTML($sn), 'Slovenia') !== false) {
						$ok = trim(str_replace(",", "", DOMinnerHTML($sn->getElementsByTagName("td")[0])));
						$um = trim(str_replace(",", "", DOMinnerHTML($sn->getElementsByTagName("td")[1])));
						$oz = trim(str_replace(",", "", DOMinnerHTML($sn->getElementsByTagName("td")[2])));
						echo 'true';
						break;
					}
				}
				$flair = $dnevi[date("N")].", ".date("j.n.Y H:i")." | okuž.: ".$ok." umr.: ".$um." ozd.: ".$oz;
			}
			if($magic == null) {
				if($post) {
					$magic = "8eff03aa-f62b-11e6-86f4-0e0e0c95c44a";
				} else {
					$magic = "bc411828-bfa2-11e5-ba67-0e414077483d";
				}
			}
			curl_setopt($ch, CURLOPT_URL, $this->setflairurl);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'X-Modhash: '.$this->modhash,
				'X-ContactBotDeveloper: '."sijanecantonluka@gmail.com",
				"X-UselessBot: true"
			));
			if($post) {
				curl_setopt($ch, CURLOPT_POSTFIELDS, 'link='.$post.'&r='.$sub.'&flair_template_id='.$magic.'&text='.$flair);
			} else {
				curl_setopt($ch, CURLOPT_POSTFIELDS, 'name='.$this->username.'&r='.$sub.'&flair_template_id='.$magic.'&text='.$flair);
			}
			$request_output = curl_exec($ch);
			if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
				return false;
			}
			return true;
		}
		public function modflair($name, $text = "Preverjen Bežigrajčan!", $css_class = "preverjen-bezigrajcan-flair", $r = "bezigrad") {
			if(!curl_getinfo($this->ch)) {
				if (!$this->login()) {
					return -2;
				}
			}
			$ch = $this->ch;
			curl_setopt($ch, CURLOPT_URL, $this->modflairurl);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'X-Modhash: '.$this->modhash,
				'X-ContactBotDeveloper: '."sijanecantonluka@gmail.com",
				"X-UselessBot: true"
			));
			curl_setopt($ch, CURLOPT_POSTFIELDS, 'name='.$name.'&r='.$r.'&css_class='.$css_class.'&text='.$text);
			$request_output = curl_exec($ch);
			if(curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
				return false;
			}
			return true;
		}
		public function fetch($what = "mentions", $value = "jd2fm5", $limit = 100) {
			if(!curl_getinfo($this->ch)) {
				if (!$this->login()) {
					return -2;
				}
			}
			if ($limit>100) {
				echo "\n[oldredditclient] WARN get(): multipage not implemented (limit>100)!\n";
				return -4;
			}
			$ch = $this->ch;
			switch($what) {
				case "mentions":
					curl_setopt($ch, CURLOPT_URL, $this->redditurl."message/mentions.json?limit=".$limit);
					curl_setopt($ch, CURLOPT_POST, 0);
					curl_setopt($ch, CURLOPT_HTTPHEADER, array(
						'X-Modhash: '.$this->modhash,
						'X-ContactBotDeveloper: '.$this->adminemail
					));
					$response = curl_exec($ch);
					if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
						return false;
					}
					$arr = json_decode($response, true, 512);
					return $arr;
					break;
				case "post":
					curl_setopt($ch, CURLOPT_URL, $this->redditurl.$value.".json");
					curl_setopt($ch, CURLOPT_POST, 0);
					curl_setopt($ch, CURLOPT_HTTPHEADER, array(
						'X-Modhash: '.$this->modhash,
						'X-ContactBotDeveloper: '.$this->adminemail
					));
					$response = curl_exec($ch);
					if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
						return false;
					}
					$arr = json_decode($response, true, 512);
					return $arr;
					break; // unreachable code amirite
				case "comments": // value=r/Slovenia || value=u/first
					curl_setopt($ch, CURLOPT_URL, $this->redditurl.$value."/comments/.json");
					curl_setopt($ch, CURLOPT_POST, 0);
					curl_setopt($ch, CURLOPT_HTTPHEADER, array(
						"X-Modhash: ".$this->modhash,
						"X-ContactBotDeveloper: ".$this->adminemail
					));
					$response = curl_exec($ch);
					if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200) {
						return false;
					}
					$arr = json_decode($response, true, 512);
					return $arr;
					break; // te breake delam nagonsko
				default:
					return -5;
					break;
			}
		}
		public function comment($text = "oldredditClient", $in_reply_to = "jd2fm5") {
			if (!curl_getinfo($this->ch)) {
				if (!$this->login()) {
					return -2;
				}
			}
			$ch = $this->ch;
			curl_setopt($ch, CURLOPT_URL, $this->redditurl."api/comment");
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'X-Modhash: '.$this->modhash,
				'X-ContactBotDeveloper: '.$this->adminemail
			));
			curl_setopt($ch, CURLOPT_POSTFIELDS, "thing_id=$in_reply_to&text=".urlencode($text));
			$response = curl_exec($ch);
			if (curl_getinfo($ch, CURLINFO_RESPONSE_CODE) != 200)
				return false;
			return true;
		}
	}
?>