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