PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/fB_Utils.php

https://github.com/RadicalLinux/faceBot
PHP | 396 lines | 347 code | 21 blank | 28 comment | 25 complexity | bb3b16add2a5fb4bfa58c3303d061944 MD5 | raw file
  1. <?php
  2. // ------------------------------------------------------------------------------
  3. // load_array
  4. // ------------------------------------------------------------------------------
  5. function load_array($filename) {
  6. return @unserialize(file_get_contents($_SESSION['this_plugin']['folder'] . '/' . PluginF($filename)));
  7. }
  8. // ------------------------------------------------------------------------------
  9. // save_array
  10. // ------------------------------------------------------------------------------
  11. function save_array($array, $filename) {
  12. file_put_contents($_SESSION['this_plugin']['folder'] . '/' . PluginF($filename),serialize($array));
  13. }
  14. // ------------------------------------------------------------------------------
  15. // AddLog add string to main log
  16. // @params string $str Text
  17. // ------------------------------------------------------------------------------
  18. function AddLog($str) {
  19. global $res_str;
  20. $res_str .= $str . "\r\n";
  21. }
  22. // ------------------------------------------------------------------------------
  23. // AddLog2 add string to advanced log
  24. // @params string $str Text
  25. // ------------------------------------------------------------------------------
  26. function AddLog2($str) {
  27. @file_put_contents($_SESSION['base_path'] . LogF("log2.txt"),@date("H:i:s")." $str\r\n",FILE_APPEND);
  28. }
  29. // ------------------------------------------------------------------------------
  30. // F creates a full file name
  31. // @param string $filename Short file name
  32. // @return string Full file name (UserID + '_' + Short name)
  33. // ------------------------------------------------------------------------------
  34. function F($filename) {
  35. $folder = "FBID_" . $_SESSION['userId'];
  36. if (!is_dir($_SESSION['base_path'] . $folder)) {
  37. mkdir($_SESSION['base_path'] . $folder);
  38. }
  39. return $folder . '/' . $filename;
  40. }
  41. // ------------------------------------------------------------------------------
  42. // PluginF creates a full file name (original F())
  43. // @param string $filename Short file name
  44. // @return string Full file name (UserID + '_' + Short name)
  45. // ------------------------------------------------------------------------------
  46. function PluginF($filename) {
  47. return $_SESSION['userId'] . '_' . $filename;
  48. }
  49. function LogF($filename) {
  50. return $_SESSION['userId'] . '_' . $filename;
  51. }
  52. // *********************************************************************
  53. // we save lots of things now so created this to cleaning up the code *
  54. // *********************************************************************
  55. function save_botarray ($array, $filename) {
  56. file_put_contents($filename,serialize($array));
  57. if ($filename="world.txt") return;
  58. $filename = substr($filename, strpos($filename,'/') + 1);
  59. $filename = str_replace('.txt', '', $filename);
  60. $newarray = serialize($array);
  61. $cleanedarray = str_replace("'", "''", $newarray);
  62. $uSQL = "INSERT OR REPLACE INTO datastore(userid, storetype, content) values('" . $_SESSION['userId'] . "',
  63. '$filename', '" . $cleanedarray . "')";
  64. $_SESSION['vDataStoreDB']->exec($uSQL);
  65. }
  66. // ------------------------------------------------------------------------------
  67. // EchoData returns data in the main application
  68. // @param string $data data
  69. // ------------------------------------------------------------------------------
  70. function EchoData($data) {
  71. }
  72. // ------------------------------------------------------------------------------
  73. // GetFarmserver returns farmville server name
  74. // @return string Server name
  75. // ------------------------------------------------------------------------------
  76. function GetFarmserver() {
  77. $flashVars = parse_flashvars();
  78. $res = str_replace('/', '', $flashVars['app_url']);
  79. $res = str_replace('http:', '', $res);
  80. unset($flashVars);
  81. return $res;
  82. }
  83. // ------------------------------------------------------------------------------
  84. // GetFarmUrl returns farmville URL
  85. // @return string URL
  86. // ------------------------------------------------------------------------------
  87. function GetFarmUrl() {
  88. $flashVars = parse_flashvars();
  89. $res = $flashVars['app_url'] . 'flashservices/gateway.php';
  90. unset($flashVars);
  91. return $res;
  92. }
  93. // ------------------------------------------------------------------------------
  94. // GetObjects gets a list of objects on the farm
  95. // @param string $className Class name ('Plot', 'Animal', 'Tree' etc.)
  96. // @return array List of objects
  97. // ------------------------------------------------------------------------------
  98. function GetObjects($className = '') {
  99. $objectsstr = fBGetDataStore('objects');
  100. $objects = unserialize($objectsstr);
  101. if ($className) {
  102. $resobjects = array();
  103. foreach ($objects as $object)
  104. if ($object['className'] == $className)
  105. $resobjects[] = $object;
  106. return $resobjects;
  107. } else {
  108. return $objects; //return all objects
  109. }
  110. }
  111. // ------------------------------------------------------------------------------
  112. // GetObjects gets a list of objects on the farm
  113. // @param string $name name
  114. // @return array List of objects
  115. // ------------------------------------------------------------------------------
  116. function GetObjectsByName($name = '') {
  117. $objectsstr = fBGetDataStore('objects');
  118. $objects = unserialize($objectsstr);
  119. if ($name) {
  120. $resobjects = array();
  121. foreach ($objects as $object)
  122. if ($object['itemName'] == $name)
  123. $resobjects[] = $object;
  124. return $resobjects;
  125. } else {
  126. return $objects; //return all objects
  127. }
  128. }
  129. // ------------------------------------------------------------------------------
  130. // GetPlotName compiles plot name
  131. // @param array $plot
  132. // @return string Plot name
  133. // ------------------------------------------------------------------------------
  134. function DebugLog($info = '')
  135. {
  136. return;
  137. }
  138. function GetPlotName($plot) {
  139. return $plot['position']['x'] . '-' . $plot['position']['y'];
  140. }
  141. // ------------------------------------------------------------------------------
  142. // GetNeighbors gets a list of neighbors
  143. // @return array List of neighbors
  144. // ------------------------------------------------------------------------------
  145. function GetNeighbors() {
  146. $neighbors = unserialize(fBGetDataStore('neighbors'));
  147. return $neighbors;
  148. }
  149. //--------------------------
  150. // CreateDefaultSettings()
  151. //
  152. // Create an default settings array and pass to SaveSettings()
  153. //--------------------------------------------------------------
  154. function CreateDefaultSettings() {
  155. $dset = array();
  156. $dset['version'] = FB_SETTINGS_VER;
  157. $dset['e_gzip'] = 1;
  158. $dset['farm_server'] = 0;
  159. $dset['bot_speed'] = 8;
  160. $dset['not_plugin'] = '';
  161. $dset['lonlyanimals'] = 1;
  162. $dset['acceptneighborhelp'] = 1;
  163. SaveSettings($dset);
  164. return $dset;
  165. }
  166. //--------------------------------------------------------------
  167. // SaveSettings(array)
  168. //
  169. // Save the supplied settings array into FBID_settings.txt
  170. //--------------------------------------------------------------
  171. function SaveSettings($settings) {
  172. $set2 = array();
  173. foreach ($settings as $key => $sopt)
  174. {
  175. if (count($settings[$key]) > 1)
  176. {
  177. $multi = '';
  178. foreach ($settings[$key] as $name => $check)
  179. $multi .= "$name:$check:";
  180. $multi = substr($multi, 0, -1); //rip the last : off
  181. $set2[] = "$key:LIST:$multi";
  182. }
  183. else
  184. {
  185. $set2[] = "$key:$sopt";
  186. }
  187. }
  188. file_put_contents($_SESSION['base_path'] . F('settings.txt'),implode(';', $set2));
  189. }
  190. //--------------------------------------------------------------
  191. // LoadSavedSettings()
  192. //
  193. // Read FBID_settings.txt if exists, call CreateDefaultSettings if not or if version is incorrect
  194. //--------------------------------------------------------------
  195. function LoadSavedSettings() {
  196. $px_Setopts = array();
  197. if (file_exists($_SESSION['base_path'] . F('settings.txt'))) {
  198. $set_read_list = @explode(';', trim(file_get_contents($_SESSION['base_path'] . F('settings.txt'))));
  199. foreach ($set_read_list as $setting_option) {
  200. $set_name = @explode(':', $setting_option);
  201. if (count($set_name) > 2) { //we have a settings 'list'
  202. $liststart = explode(':', $setting_option,3);
  203. $listopt = explode(':', $liststart[2]);
  204. $tired = count($listopt);
  205. for ($i=0; $i < $tired; $i=$i+2) {
  206. $tired2 = $i+1;
  207. $px_Setopts[$liststart[0]][$listopt[$i]] = $listopt[$tired2];
  208. }
  209. } else {
  210. $px_Setopts[$set_name[0]] = $set_name[1];
  211. }
  212. }
  213. if($px_Setopts['version']<>FB_SETTINGS_VER) {
  214. $px_Setopts['version'] = FB_SETTINGS_VER;
  215. @unlink('sqlite_check.txt');
  216. }
  217. } else {
  218. $px_Setopts = CreateDefaultSettings();
  219. }
  220. return $px_Setopts;
  221. }
  222. function parse_neighbors() {
  223. $temp = file_get_contents($_SESSION['userId'] . '_flashInfo.txt');
  224. preg_match('/var g_friendData = \[([^]]*)\]/sim', $temp, $friend);
  225. unset($temp);
  226. if (!isset($friend[1])) return;
  227. preg_match_all('/\{([^}]*)\}/sim', $friend[1], $friend2);
  228. unset($friend);
  229. foreach($friend2[1] as $f)
  230. {
  231. preg_match_all('/"([^"]*)":"([^"]*)"/im', $f, $fr);
  232. $newarray[] = array_combine($fr[1], $fr[2]);
  233. }
  234. unset($friend2, $fr);
  235. $uSQL = '';
  236. foreach ($newarray as $friends)
  237. {
  238. if ($friends['is_app_user'] != 1) continue;
  239. $friends['pic_square'] = str_replace('\\/', '\\', $friends['pic_square']);
  240. $friends['name'] = str_replace("'", "''", $friends['name']);
  241. $friends['name'] = preg_replace('/\\\u([0-9a-z]{4})/', '&#x$1;', $friends['name']);
  242. $uSQL .= "INSERT OR REPLACE INTO neighbors(neighborid, fullname, profilepic) values('" . $friends['uid'] . "',
  243. '" . $friends['name'] . "', '" . $friends['pic_square'] . "');";
  244. }
  245. $_SESSION['vDataStoreDB']->exec($uSQL);
  246. unset($uSQL, $newarray);
  247. return;
  248. }
  249. function parse_user()
  250. {
  251. $temp = file_get_contents($_SESSION['userId'] . '_flashInfo.txt');
  252. preg_match('/var g_userInfo = \{([^}]*)\}/sim', $temp, $user);
  253. unset($temp);
  254. preg_match_all('/"([^"]*)":"([^"]*)"/im', $user[1], $fr);
  255. $newarray = array_combine($fr[1], $fr[2]);
  256. $newarray['name'] = str_replace("'", "''", $newarray['name']);
  257. $newarray['name'] = preg_replace('/\\\u([0-9a-z]{4})/', '&#x$1;', $newarray['name']);
  258. unset($user, $fr);
  259. $uSQL = 'INSERT OR REPLACE INTO userids(userid, username) values("' . $_SESSION['userId'] . '", "' . $newarray['name'] . '");';
  260. $_SESSION['vDataStoreDB']->exec($uSQL);
  261. unset($uSQL);
  262. }
  263. function parse_flashvars()
  264. {
  265. $temp = file_get_contents($_SESSION['userId'] . '_flashInfo.txt');
  266. preg_match('/var flashVars = \{([^}]*)\}/sim', $temp, $flash);
  267. unset($temp);
  268. preg_match_all('/"([^"]*)":"([^"]*)"/im', $flash[1], $fr);
  269. $newarray = array_combine($fr[1], $fr[2]);
  270. $newarray['game_config_url'] = str_replace('\\/', '/', $newarray['game_config_url']);
  271. $newarray['items_url'] = str_replace('\\/', '/', $newarray['items_url']);
  272. $newarray['swfLocation'] = str_replace('\\/', '/', $newarray['swfLocation']);
  273. $newarray['localization_url'] = str_replace('\\/', '/', $newarray['localization_url']);
  274. $newarray['app_url'] = str_replace('\\/', '/', $newarray['app_url']);
  275. $newarray['social_quest_url'] = str_replace('\\/', '/', $newarray['social_quest_url']);
  276. $newarray['asset_url'] = str_replace('\\/', '/', $newarray['asset_url']);
  277. $newarray['xml_url'] = str_replace('\\/', '/', $newarray['xml_url']);
  278. return $newarray;
  279. }
  280. function objectsIntoArray($arrObjData, $arrSkipIndices = array())
  281. {
  282. $arrData = array();
  283. // if input is object, convert into array
  284. if (is_object($arrObjData)) {
  285. $arrObjData = get_object_vars($arrObjData);
  286. }
  287. if (is_array($arrObjData)) {
  288. foreach ($arrObjData as $index => $value) {
  289. if (is_object($value) || is_array($value)) {
  290. $value = objectsIntoArray($value, $arrSkipIndices); // recursive call
  291. }
  292. if (in_array($index, $arrSkipIndices)) {
  293. continue;
  294. }
  295. $arrData[$index] = $value;
  296. }
  297. }
  298. return $arrData;
  299. }
  300. class xml2array {
  301. function xml2array($xml) {
  302. if (is_string($xml)) {
  303. $this->dom = new DOMDocument;
  304. $this->dom->loadXml($xml);
  305. }
  306. return FALSE;
  307. }
  308. function _process($node) {
  309. $occurance = array();
  310. if (is_array($node->childNodes)|| is_object($node->childNodes))
  311. foreach($node->childNodes as $child) {
  312. $occurance[$child->nodeName]++;
  313. }
  314. if($node->nodeType == XML_TEXT_NODE) {
  315. $result = html_entity_decode(htmlentities($node->nodeValue, ENT_COMPAT, 'UTF-8'),
  316. ENT_COMPAT,'ISO-8859-15');
  317. }
  318. else {
  319. if($node->hasChildNodes()){
  320. $children = $node->childNodes;
  321. for($i=0; $i<$children->length; $i++) {
  322. $child = $children->item($i);
  323. if($child->nodeName != '#text') {
  324. if($occurance[$child->nodeName] > 1) {
  325. $result[$child->nodeName][] = $this->_process($child);
  326. }
  327. else {
  328. $result[$child->nodeName] = $this->_process($child);
  329. }
  330. }
  331. else if ($child->nodeName == '#text') {
  332. $text = $this->_process($child);
  333. if (trim($text) != '') {
  334. $result['value'] = $this->_process($child);
  335. }
  336. }
  337. }
  338. }
  339. if($node->hasAttributes()) {
  340. $attributes = $node->attributes;
  341. if(!is_null($attributes)) {
  342. foreach ($attributes as $key => $attr) {
  343. $result[$attr->name] = $attr->value;
  344. }
  345. }
  346. }
  347. }
  348. return $result;
  349. }
  350. function getResult() {
  351. return $this->_process($this->dom);
  352. }
  353. }