PageRenderTime 157ms CodeModel.GetById 51ms app.highlight 25ms RepoModel.GetById 75ms app.codeStats 0ms

/plugins/fvLinks/includes/fvLinks.class.php

https://github.com/RadicalLinux/faceBot
PHP | 420 lines | 399 code | 16 blank | 5 comment | 36 complexity | cfbd1d495f118de4b0fe64ad54966595 MD5 | raw file
  1<?php
  2class fvLinks
  3{
  4	var $userId, $settings, $_fsManagerDBM;
  5
  6	private function _fsManager_checkDB()
  7	{
  8		if(!empty($this->error))
  9		{
 10			AddLog2($this->error);
 11			return;
 12		}
 13		$q = @$this->_fsManagerDBM->query('SELECT * FROM settings LIMIT 1');
 14		if($q === false)
 15		{
 16			AddLog2('Creating Settings Table');
 17			$fvSQL = 'CREATE TABLE
 18						settings (
 19						settings_name CHAR(250) PRIMARY KEY,
 20						settings_value TEXT
 21			)';
 22			$this->_fsManagerDBM->queryExec($fvSQL);
 23			$fvSQL = "INSERT INTO settings(settings_name,settings_value) values('userid','" . $this->userId . "')";
 24			$this->_fsManagerDBM->queryExec($fvSQL);
 25		}
 26		$q = @$this->_fsManagerDBM->query('SELECT * FROM rewardlinks LIMIT 1');
 27		if($q === false)
 28		{
 29			AddLog2('Creating Reward Links Table');
 30			$fvSQL = 'CREATE TABLE
 31						rewardlinks (
 32						rewardlinks_link CHAR(250) PRIMARY KEY,
 33						rewardlinks_timestamp NUMERIC DEFAULT 0
 34			)';
 35			$this->_fsManagerDBM->queryExec($fvSQL);
 36			$fvSQL = "INSERT INTO settings(settings_name,settings_value) values('userid','" . $this->userId . "')";
 37			$this->_fsManagerDBM->queryExec($fvSQL);
 38		}
 39		$q = @$this->_fsManagerDBM->query('SELECT * FROM knownlinks LIMIT 1');
 40		if($q === false)
 41		{
 42			AddLog2('Creating Known Links Table');
 43			$fvSQL = 'CREATE TABLE
 44						knownlinks (
 45						knownlinks_name CHAR(250) PRIMARY KEY,
 46						knownlinks_post TEXT
 47			)';
 48			$this->_fsManagerDBM->queryExec($fvSQL);
 49		}
 50		$q = @$this->_fsManagerDBM->query('SELECT * FROM locales LIMIT 1');
 51		if($q === false)
 52		{
 53			AddLog2('Creating facebook Post Table');
 54			$fvSQL = 'CREATE TABLE
 55						locales (
 56						locales_key CHAR(250) PRIMARY KEY,
 57						locales_value TEXT
 58			)';
 59			$this->_fsManagerDBM->queryExec($fvSQL);
 60		}
 61	}
 62
 63	//Function fvLinks class initializer
 64	function fvLinks($inittype = '')
 65	{
 66		$this->userId = $_SESSION['userId'];
 67		$this->error = '';
 68		$this->haveWorld = true;
 69		$this->flashRevision = $_SESSION['flashRevision'];
 70		if(!is_numeric($this->userId))
 71		{
 72			$this->error = "Farmville Bot Not Initialized/User Unknown";
 73			return;
 74		}
 75
 76		//Open Databases
 77		$this->_fsManagerDBM = new SQLiteDatabase(fvLinks_Path . PluginF(fvLinks_Main));
 78		$this->_fsManager_checkDB();
 79		if(!$this->_fsManagerDBM)
 80		{
 81			$this->error = 'fvLinks - Database Error';
 82			return;
 83		}
 84
 85		//Get Settings
 86		$this->settings = $this->fsGetSettings();
 87		if($inittype == 'formload')
 88		{
 89			if(empty($this->settings))
 90			{
 91				$this->error = 'Please allow fvLinks to run a cycle';
 92			}
 93			return;
 94		}
 95		$this->_fsUpdateSettings();
 96		$this->_fsExpireLinks();
 97		//$this->_fsGetLocales();
 98		$this->GetALinks();
 99	}
100
101	function fsGetSettings()
102	{
103		$fvSQL = 'SELECT * FROM settings';
104		$q = @$this->_fsManagerDBM->query($fvSQL);
105		if($q !== false)
106		{
107			$results = $q->fetchAll(SQLITE_ASSOC);
108			foreach($results as $item)
109			{
110				$newresults[$item['settings_name']] = $item['settings_value'];
111			}
112			return $newresults;
113		}
114		return false;
115	}
116
117	function fsMakeTime($ctime)
118	{
119		$days = $ctime / 86400;
120		if ($days > 1)
121		{
122			$days = intval($days);
123			$ctime = $ctime - ($days * 86400);
124			$timestr = $days . ' days ';
125		}
126		$hours = $ctime / 3600;
127		if ($hours > 1)
128		{
129			$hours = intval($hours);
130			$ctime = $ctime - ($hours * 3600);
131			@$timestr .= $hours . ' hrs ';
132		}
133		$minutes = $ctime / 60;
134		if ($minutes > 1)
135		{
136			$minutes = intval($minutes);
137			@$timestr .= $minutes . ' min';
138		}
139		return $timestr;
140	}
141
142	function GetLinkTypes($fbid = '')
143	{
144
145		if (is_numeric($fbid)) {
146			$vSQL = "SELECT content FROM rewardstore WHERE userid='" . $fbid . "' AND storetype='rewards'";
147			$rlinks = unserialize(@$_SESSION['vRewardStoreDB']->querySingle($vSQL));
148		} else {
149			$rlinks = unserialize(fBGetRewardStore('rewards'));
150		}
151		//echo nl2br(print_r($rlinks,true));
152		foreach ($rlinks as $url)
153		foreach ($url as $key=>$link)
154		{
155			if (strlen($link['rewardLink']) == 1) continue;
156			if (isset($newurl[$link['rewardLink']]) && empty($newurl[$link['rewardLink']]['rewardItem']) && empty($newurl[$link['rewardLink']]['rewardCode'])) {
157				@$newurl[$link['rewardLink']] = array('rewardItem' => $link['rewardItem'], 'rewardCode' => $link['rewardCode'], 'timestamp' => $link['timestamp']);
158			} elseif (!isset($newurl[$link['rewardLink']])) {
159				@$newurl[$link['rewardLink']] = array('rewardItem' => $link['rewardItem'], 'rewardCode' => $link['rewardCode'], 'timestamp' => $link['timestamp']);
160			} else { unset($url[$key]); }
161		}
162		foreach ($newurl as $key=>$furl)
163		{
164			preg_match_all('/frType=(.*)&key=/sim', $key, $cat);
165			$rtypes[$cat[1][0]][] = array('rewardLink' => $key, 'rewardItem' => $furl['rewardItem'], 'rewardCode' => $furl['rewardCode'], 'timestamp' => $furl['timestamp']);
166		}
167		ksort($rtypes);
168		foreach (array_keys($rtypes) as $cat)
169		{
170			if (!empty($cat)) {
171				$fvSQL = "INSERT OR REPLACE INTO knownlinks(knownlinks_name,knownlinks_post) values('" . $cat . "','0')";
172				$this->_fsManagerDBM->queryExec($fvSQL);
173			}
174		}
175		return $rtypes;
176	}
177
178	function GetALinkTypes()
179	{
180		$rlinks = unserialize(fBGetDataStore('ralinks'));
181		return $rlinks;
182	}
183
184	function GetALinks()
185	{
186		$rlinks = unserialize(fBGetDataStore('ralinks'));
187		$i = 0;
188		$links = array();
189		foreach ($rlinks as $lkey=>$linfo)
190		{
191			$Md5string = MD5($linfo['animalType'] . "farmville1is4the3best4game4ever" . $linfo['id'] . "8415");
192			$links[$i]['url'] = 'http://apps.facebook.com/onthefarm/lonelycow.php?owner_id=' . $_SESSION['userId'] . '&lonely_cow_id=' . $linfo['id'] . '&animalType=' . $linfo['animalType'] . '&lonely_cow_sig=' . $Md5string . '&_fb_noscript=1';
193			$links[$i]['helpers'] = count(array_keys($linfo['helpers']));
194			$links[$i]['animal'] = $linfo['animalType'];
195			$links[$i]['time'] = $linfo['timeRequested'];
196			$i++;
197		}
198		return $links;
199	}
200
201
202	function fsDoSettings($post)
203	{
204		$fvSQL = "INSERT OR REPLACE INTO settings(settings_name,settings_value) values('sharelinks','" . serialize($post) . "')";
205		$this->_fsManagerDBM->queryExec($fvSQL);
206
207	}
208
209	private function _fsUpdateSettings()
210	{
211		$fvSQL = "INSERT OR REPLACE INTO settings(settings_name,settings_value) values('flashRevision','" . $_SESSION['flashRevision'] . "')";
212		$this->_fsManagerDBM->queryExec($fvSQL);
213
214	}
215
216	private function _fsGetLocales()
217	{
218		if ($this->settings['flashRevision'] != $this->settings['unitversion'])
219		{
220			$doc1 = file_get_contents('./farmville-xml/' . $this->flashRevision . '_flashLocaleXML.xml');
221			if (!$doc1) AddLog2('Unable to get flashLocaleXml.xml');
222			if(!$doc1)
223			{
224				return;
225			}
226			AddLog2('fvLinks is updating facebook post text');
227			$xmlDoc = new DOMDocument();
228			$xmlDoc->loadXML($doc1);
229			$items = $xmlDoc->getElementsByTagName("bundleLine");
230			$this->_fsManagerDBM->queryExec('BEGIN;');
231			foreach($items as $item)
232			{
233				$k = $item->getAttribute('key');
234				$vT = $item->getElementsByTagName('value');
235				$v = $vT->item(0)->nodeValue;
236				$v = str_replace("'", "''", $v);
237				$fvSQL = "INSERT OR REPLACE INTO locales (locales_key, locales_value) values('" . trim($k) . "', '" . trim($v) . "');";
238				$this->_fsManagerDBM->queryExec($fvSQL,$error);
239				if (!empty($error)) { AddLog2($error); }
240			}
241			$this->_fsManagerDBM->queryExec('COMMIT;');
242			AddLog2('fvLinks has finished updating facebook post text');
243			$fvSQL = "INSERT OR REPLACE INTO settings(settings_name,settings_value) values('unitversion','". $this->flashRevision . "')";
244			$this->_fsManagerDBM->queryExec($fvSQL);
245			unset($xmlDoc);
246		}
247
248	}
249
250	function GetKnownTypes()
251	{
252		$fvSQL = "SELECT * FROM knownlinks ORDER BY UPPER(knownlinks_name)";
253		$q = $this->_fsManagerDBM->query($fvSQL);
254		$results = $q->fetchAll(SQLITE_ASSOC);
255		return $results;
256	}
257
258	function fvClearLinks()
259	{
260		$uSQL = "DELETE FROM rewardstore WHERE userid='" . $_SESSION['userId'] . "' AND storetype='rewards'";
261		$_SESSION['vRewardStoreDB']->exec($uSQL);
262	}
263
264	function DoWork()
265	{
266
267		$grabs = unserialize($this->settings['sharelinks']);
268		if (empty($grabs)) return;
269		$gbcount = array_sum(unserialize(fBGetDataStore('ingiftbox')));
270		$cbcount = array_sum(unserialize(fBGetDataStore('inconbox')));
271		$count = 0;
272		foreach ($grabs as $fbid=>$info)
273		{
274			if (@$info['share'] != 1)  continue;
275			$links = $this->GetLinkTypes($fbid);
276			foreach ($links as $cat=>$link)
277			{
278				if (@$info[$cat] == 1) continue;
279				foreach ($link as $nlink)
280				{
281					if (($gbcount + $cbcount) + $count >= 500) { AddLog2('fvLinks: GiftBox/Consumable Box Full'); return; }
282					$key = str_replace('&key={*key*}', '', $nlink['rewardLink']);
283					if ($this->fvCheckLink($key) === true) continue;
284					$fvSQL = "INSERT OR REPLACE INTO rewardlinks(rewardlinks_link, rewardlinks_timestamp) values('$key','". time() . "')";
285					$this->_fsManagerDBM->queryExec($fvSQL);
286					$answer = $this->_fvLinksGet('http://apps.facebook.com/onthefarm/' . $key);
287					$count++;
288					preg_match_all('/<h3>(.*)<\/h3>/sim', $answer, $text);
289					if (!empty($answer) && empty($text[1][0])) { AddLog2('fvLinks: Unknown Gift Response'); continue; }
290					if ($text[1][0] == "Hey there, farmer! You've claimed all the rewards you can from your friends today.  Try again tomorrow!") break;
291					if (!empty($answer)) AddLog2($text[1][0]);
292					sleep(3);
293				}
294
295			}
296		}
297	}
298
299	function fvCheckLink($link = '')
300	{
301		$fvSQL = "SELECT * FROM rewardlinks WHERE rewardlinks_link='" . $link . "'";
302		$q = $this->_fsManagerDBM->query($fvSQL);
303		$results = $q->fetchSingle();
304		return (@$results === false ? false : true);
305	}
306
307	private function _fvLinksGet($url = '')
308	{
309		$ch = curl_init();
310		curl_setopt($ch, CURLOPT_URL, $url);
311		curl_setopt($ch, CURLOPT_HEADER, 0);
312		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
313		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
314		curl_setopt($ch, CURLOPT_TIMEOUT, 45);
315		curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SearchToolbar 1.1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322)');
316		curl_setopt($ch, CURLOPT_COOKIE, $this->_fvLinksGetCookie());
317		curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
318		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Language: en-US', 'Pragma: no-cache'));
319		if ($_SESSION['use_proxy']) {
320			curl_setopt($ch, CURLOPT_PROXY, trim($_SESSION['proxy_settings'][0]));
321			curl_setopt($ch, CURLOPT_PROXYPORT, intval($_SESSION['proxy_settings'][1]));
322			if (isset($_SESSION['proxy_settings'][2]) && isset($_SESSION['proxy_settings'][3])) { // is set proxy user and password
323				$authorization = trim($_SESSION['proxy_settings'][2]) . ':' . trim($_SESSION['proxy_settings'][3]);
324				curl_setopt($ch, CURLOPT_PROXYUSERPWD, $authorization);
325				curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
326			}
327		}
328		$response = curl_exec($ch);
329		$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
330		if($httpCode == 404) {
331			AddLog2("fvLinks: Error 404/Page Not Found");
332			return;
333		}
334		if($httpCode == 500) {
335			AddLog2("fvLinks: Error 500/Internal Server Error");
336			return;
337		}
338		if(empty($response)) {
339			AddLog2("fvLinks: Empty Response Returned");
340			return;
341		}
342		curl_close ($ch);
343		return $response;
344	}
345
346	private function _fvLinksGetCookie()
347	{
348		$newcookiestr = trim(file_get_contents($_SESSION['base_path'] .  $_SESSION['userId'] . '_cookie.txt'));
349		return $newcookiestr;
350	}
351
352	private function _fsExpireLinks()
353	{
354		$rlinks = unserialize(fBGetRewardStore('rewards'));
355		//echo nl2br(print_r($rlinks,true));
356		foreach ($rlinks as $url)
357		foreach ($url as $key=>$link)
358		{
359			if (strlen($link['rewardLink']) == 1) continue;
360			if (isset($newurl[$link['rewardLink']]) && empty($newurl[$link['rewardLink']]['rewardItem']) && empty($newurl[$link['rewardLink']]['rewardCode'])) {
361				@$newurl[$link['rewardLink']] = array('rewardLink' => $link['rewardLink'], 'rewardItem' => $link['rewardItem'], 'rewardCode' => $link['rewardCode'], 'timestamp' => $link['timestamp']);
362			} elseif (!isset($newurl[$link['rewardLink']])) {
363				@$newurl[$link['rewardLink']] = array('rewardLink' => $link['rewardLink'], 'rewardItem' => $link['rewardItem'], 'rewardCode' => $link['rewardCode'], 'timestamp' => $link['timestamp']);
364			} else { unset($link[$key]); }
365		}
366		$mytime = time() - (3600 * 24);
367		if (isset($newurl)) {
368			foreach ($newurl as $tmp)
369			{
370				if ($tmp['timestamp'] >= $mytime) $final[0][] = $tmp;
371			}
372			$uSQL = "INSERT OR REPLACE INTO rewardstore(userid, storetype, content) values('" . $_SESSION['userId'] . "',
373				'rewards', '" . serialize($final) . "')";
374			$_SESSION['vRewardStoreDB']->exec($uSQL);
375		}
376		$fvSQL = "DELETE FROM rewardlinks WHERE rewardlinks_timestamp <= '" . $mytime . "'";
377		$this->_fsManagerDBM->queryExec($fvSQL);
378	}
379
380	function fsExport()
381	{
382		$linkTypes = $this->GetLinkTypes();
383		$text = '';
384		foreach ($linkTypes as $cat=>$link)
385		{
386			$text .= '----------' . $cat . "----------\r\n";
387			foreach ($link as $nlink)
388			{
389				$key = str_replace('&key={*key*}', '', $nlink['rewardLink']);
390				$time = date("m/d g:i a", $nlink['timestamp']);
391				$url = 'http://apps.facebook.com/onthefarm/' . $key;
392				if (isset($nlink['rewardCode'])) {
393					$name = Units_GetRealnameByCode($nlink['rewardCode']);
394					$name = empty($name) ? Units_GetRealnameByName($nlink['rewardCode']) : $name;
395					$item = $name;
396				}
397				if (isset($nlink['rewardItem'])) {
398					if (empty($nlink['rewardItem'])) {
399						$item = '--Unknown--';
400					} else 	{
401						$name = Units_GetRealnameByName($nlink['rewardItem']);
402						$name = empty($name) ? Units_GetRealnameByCode($nlink['rewardItem']) : $name;
403						$item = $name;
404					}
405				}
406				$text .= $time . "\t" . $item . "\t" . $url . "\r\n";
407			}
408			$text .= "\r\n";
409		}
410		$display_file =  $_SESSION['base_path'] . "plugins\\fvLinks\\" . $_SESSION['userId'] . '_links.txt';
411		if (is_file($display_file)) unlink($display_file);
412		$f = fopen($display_file, "w+");
413		fwrite($f, $text);
414		fclose($f);
415		$runcmd = "notepad.exe $display_file";
416		$WshShell = new COM("WScript.Shell");
417		$WshShell->Run($runcmd, 5, false);
418	}
419}
420?>