faceBot /fB_Utils.php

Language PHP Lines 397
MD5 Hash bb3b16add2a5fb4bfa58c3303d061944 Estimated Cost $7,045 (why?)
Repository https://github.com/RadicalLinux/faceBot.git View Raw File View Project SPDX
  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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
<?php

// ------------------------------------------------------------------------------
// load_array
// ------------------------------------------------------------------------------
function load_array($filename) {

	return @unserialize(file_get_contents($_SESSION['this_plugin']['folder'] . '/' . PluginF($filename)));
}
// ------------------------------------------------------------------------------
// save_array
// ------------------------------------------------------------------------------
function save_array($array, $filename) {
	file_put_contents($_SESSION['this_plugin']['folder'] . '/' . PluginF($filename),serialize($array));
}
// ------------------------------------------------------------------------------
// AddLog add string to main log
//  @params string $str Text
// ------------------------------------------------------------------------------
function AddLog($str) {
	global $res_str;
	$res_str .= $str . "\r\n";
}
// ------------------------------------------------------------------------------
// AddLog2 add string to advanced log
//  @params string $str Text
// ------------------------------------------------------------------------------
function AddLog2($str) {
	@file_put_contents($_SESSION['base_path'] . LogF("log2.txt"),@date("H:i:s")." $str\r\n",FILE_APPEND);
}
// ------------------------------------------------------------------------------
// F creates a full file name
//  @param string $filename Short file name
//  @return string Full file name (UserID + '_' + Short name)
// ------------------------------------------------------------------------------
function F($filename) {
	$folder = "FBID_" . $_SESSION['userId'];
	if (!is_dir($_SESSION['base_path'] . $folder)) {
		mkdir($_SESSION['base_path'] . $folder);
	}

	return $folder . '/' . $filename;
}
// ------------------------------------------------------------------------------
// PluginF creates a full file name (original F())
//  @param string $filename Short file name
//  @return string Full file name (UserID + '_' + Short name)
// ------------------------------------------------------------------------------
function PluginF($filename) {
	return $_SESSION['userId'] . '_' . $filename;
}

function LogF($filename) {
	return $_SESSION['userId'] . '_' . $filename;
}

// *********************************************************************
// we save lots of things now so created this to cleaning up the code *
// *********************************************************************
function save_botarray ($array, $filename) {
	file_put_contents($filename,serialize($array));
	if ($filename="world.txt") return;
	$filename = substr($filename, strpos($filename,'/') + 1);
	$filename = str_replace('.txt', '', $filename);
	$newarray = serialize($array);
	$cleanedarray = str_replace("'", "''", $newarray);
	$uSQL = "INSERT OR REPLACE INTO datastore(userid, storetype, content) values('" . $_SESSION['userId'] . "',
				'$filename', '" . $cleanedarray . "')";
	$_SESSION['vDataStoreDB']->exec($uSQL);
}

// ------------------------------------------------------------------------------
// EchoData returns data in the main application
//  @param string $data data
// ------------------------------------------------------------------------------
function EchoData($data) {

}

// ------------------------------------------------------------------------------
// GetFarmserver returns farmville server name
//  @return string Server name
// ------------------------------------------------------------------------------
function GetFarmserver() {

	$flashVars = parse_flashvars();
	$res = str_replace('/', '', $flashVars['app_url']);
	$res = str_replace('http:', '', $res);
	unset($flashVars);
	return $res;
}
// ------------------------------------------------------------------------------
// GetFarmUrl returns farmville URL
//  @return string URL
// ------------------------------------------------------------------------------
function GetFarmUrl() {
	$flashVars = parse_flashvars();
	$res = $flashVars['app_url'] . 'flashservices/gateway.php';
	unset($flashVars);
	return $res;
}

// ------------------------------------------------------------------------------
// GetObjects gets a list of objects on the farm
//  @param string $className Class name ('Plot', 'Animal', 'Tree' etc.)
//  @return array List of objects
// ------------------------------------------------------------------------------
function GetObjects($className = '') {
	$objectsstr = fBGetDataStore('objects');
	$objects = unserialize($objectsstr);
	if ($className) {
		$resobjects = array();
		foreach ($objects as $object)
		if ($object['className'] == $className)
		$resobjects[] = $object;
		return $resobjects;
	} else {
		return $objects; //return all objects
	}
}


// ------------------------------------------------------------------------------
// GetObjects gets a list of objects on the farm
//  @param string $name name
//  @return array List of objects
// ------------------------------------------------------------------------------
function GetObjectsByName($name = '') {
	$objectsstr = fBGetDataStore('objects');
	$objects = unserialize($objectsstr);
	if ($name) {
		$resobjects = array();
		foreach ($objects as $object)
		if ($object['itemName'] == $name)
		$resobjects[] = $object;
		return $resobjects;
	} else {
		return $objects; //return all objects
	}
}
// ------------------------------------------------------------------------------
// GetPlotName compiles plot name
//  @param array $plot
//  @return string Plot name
// ------------------------------------------------------------------------------
function DebugLog($info = '')
{
	return;
}
function GetPlotName($plot) {
	return $plot['position']['x'] . '-' . $plot['position']['y'];
}

// ------------------------------------------------------------------------------
// GetNeighbors gets a list of neighbors
//  @return array List of neighbors
// ------------------------------------------------------------------------------
function GetNeighbors() {
	$neighbors = unserialize(fBGetDataStore('neighbors'));
	return $neighbors;
}

//--------------------------
// CreateDefaultSettings()
//
// Create an default settings array and pass to SaveSettings()
//--------------------------------------------------------------


function CreateDefaultSettings() {
	$dset = array();

	$dset['version'] = FB_SETTINGS_VER;
	$dset['e_gzip'] = 1;
	$dset['farm_server'] = 0;
	$dset['bot_speed'] = 8;
	$dset['not_plugin'] = '';
	$dset['lonlyanimals'] = 1;
	$dset['acceptneighborhelp'] = 1;
	SaveSettings($dset);

	return $dset;
}


//--------------------------------------------------------------
// SaveSettings(array)
//
// Save the supplied settings array into FBID_settings.txt
//--------------------------------------------------------------

function SaveSettings($settings) {
	$set2 = array();
	foreach ($settings as $key => $sopt)
	{
		if (count($settings[$key]) > 1)
		{
			$multi = '';
			foreach ($settings[$key] as $name => $check)
			$multi .= "$name:$check:";

			$multi = substr($multi, 0, -1); //rip the last : off
			$set2[] = "$key:LIST:$multi";
		}
		else
		{
			$set2[] = "$key:$sopt";
		}
	}
	file_put_contents($_SESSION['base_path'] . F('settings.txt'),implode(';', $set2));
}

//--------------------------------------------------------------
// LoadSavedSettings()
//
// Read FBID_settings.txt if exists, call CreateDefaultSettings if not or if version is incorrect
//--------------------------------------------------------------
function LoadSavedSettings() {
	$px_Setopts = array();

	if (file_exists($_SESSION['base_path'] . F('settings.txt'))) {
		$set_read_list = @explode(';', trim(file_get_contents($_SESSION['base_path'] . F('settings.txt'))));

		foreach ($set_read_list as $setting_option) {
			$set_name = @explode(':', $setting_option);

			if (count($set_name) > 2) { //we have a settings 'list'
				$liststart = explode(':', $setting_option,3);
				$listopt = explode(':', $liststart[2]);
				$tired = count($listopt);
				for ($i=0; $i < $tired; $i=$i+2) {
					$tired2 = $i+1;
					$px_Setopts[$liststart[0]][$listopt[$i]] = $listopt[$tired2];
				}
			} else {
				$px_Setopts[$set_name[0]] = $set_name[1];
			}
		}
		if($px_Setopts['version']<>FB_SETTINGS_VER) {
			$px_Setopts['version'] = FB_SETTINGS_VER;
			@unlink('sqlite_check.txt');
		}
	} else {
		$px_Setopts = CreateDefaultSettings();
	}
	return $px_Setopts;
}

function parse_neighbors() {
	$temp = file_get_contents($_SESSION['userId'] . '_flashInfo.txt');
	preg_match('/var g_friendData = \[([^]]*)\]/sim', $temp, $friend);
	unset($temp);
	if (!isset($friend[1])) return;
	preg_match_all('/\{([^}]*)\}/sim', $friend[1], $friend2);
	unset($friend);
	foreach($friend2[1] as $f)
	{
		preg_match_all('/"([^"]*)":"([^"]*)"/im', $f, $fr);
		$newarray[] = array_combine($fr[1], $fr[2]);
	}
	unset($friend2, $fr);
	$uSQL = '';
	foreach ($newarray as $friends)
	{
		if ($friends['is_app_user'] != 1) continue;
		$friends['pic_square'] = str_replace('\\/', '\\', $friends['pic_square']);
		$friends['name'] = str_replace("'", "''", $friends['name']);
		$friends['name'] = preg_replace('/\\\u([0-9a-z]{4})/', '&#x$1;', $friends['name']);
		$uSQL .= "INSERT OR REPLACE INTO neighbors(neighborid, fullname, profilepic) values('" . $friends['uid'] . "',
				'" . $friends['name'] . "', '" . $friends['pic_square'] . "');";
	}
	$_SESSION['vDataStoreDB']->exec($uSQL);
	unset($uSQL, $newarray);
	return;
}

function parse_user()
{
	$temp = file_get_contents($_SESSION['userId'] . '_flashInfo.txt');
	preg_match('/var g_userInfo = \{([^}]*)\}/sim', $temp, $user);
	unset($temp);
	preg_match_all('/"([^"]*)":"([^"]*)"/im', $user[1], $fr);
	$newarray = array_combine($fr[1], $fr[2]);
	$newarray['name'] = str_replace("'", "''", $newarray['name']);
	$newarray['name'] = preg_replace('/\\\u([0-9a-z]{4})/', '&#x$1;', $newarray['name']);
	unset($user, $fr);
	$uSQL = 'INSERT OR REPLACE INTO userids(userid, username) values("' . $_SESSION['userId'] . '", "' . $newarray['name'] . '");';
	$_SESSION['vDataStoreDB']->exec($uSQL);
	unset($uSQL);
}

function parse_flashvars()
{
	$temp = file_get_contents($_SESSION['userId'] . '_flashInfo.txt');
	preg_match('/var flashVars = \{([^}]*)\}/sim', $temp, $flash);
	unset($temp);
	preg_match_all('/"([^"]*)":"([^"]*)"/im', $flash[1], $fr);
	$newarray = array_combine($fr[1], $fr[2]);
	$newarray['game_config_url'] = str_replace('\\/', '/', $newarray['game_config_url']);
	$newarray['items_url'] = str_replace('\\/', '/', $newarray['items_url']);
	$newarray['swfLocation'] = str_replace('\\/', '/', $newarray['swfLocation']);
	$newarray['localization_url'] = str_replace('\\/', '/', $newarray['localization_url']);
	$newarray['app_url'] = str_replace('\\/', '/', $newarray['app_url']);
	$newarray['social_quest_url'] = str_replace('\\/', '/', $newarray['social_quest_url']);
	$newarray['asset_url'] = str_replace('\\/', '/', $newarray['asset_url']);
	$newarray['xml_url'] = str_replace('\\/', '/', $newarray['xml_url']);
	return $newarray;
}


function objectsIntoArray($arrObjData, $arrSkipIndices = array())
{
	$arrData = array();

	// if input is object, convert into array
	if (is_object($arrObjData)) {
		$arrObjData = get_object_vars($arrObjData);
	}

	if (is_array($arrObjData)) {
		foreach ($arrObjData as $index => $value) {
			if (is_object($value) || is_array($value)) {
				$value = objectsIntoArray($value, $arrSkipIndices); // recursive call
			}
			if (in_array($index, $arrSkipIndices)) {
				continue;
			}
			$arrData[$index] = $value;
		}
	}
	return $arrData;
}
class xml2array {

	function xml2array($xml) {
		if (is_string($xml)) {
			$this->dom = new DOMDocument;
			$this->dom->loadXml($xml);
		}

		return FALSE;
	}

	function _process($node) {
		$occurance = array();
		if (is_array($node->childNodes)|| is_object($node->childNodes))
		foreach($node->childNodes as $child) {
			$occurance[$child->nodeName]++;
		}
		if($node->nodeType == XML_TEXT_NODE) {
			$result = html_entity_decode(htmlentities($node->nodeValue, ENT_COMPAT, 'UTF-8'),
			ENT_COMPAT,'ISO-8859-15');
		}
		else {
			if($node->hasChildNodes()){
				$children = $node->childNodes;

				for($i=0; $i<$children->length; $i++) {
					$child = $children->item($i);

					if($child->nodeName != '#text') {
						if($occurance[$child->nodeName] > 1) {
							$result[$child->nodeName][] = $this->_process($child);
						}
						else {
							$result[$child->nodeName] = $this->_process($child);
						}
					}
					else if ($child->nodeName == '#text') {
						$text = $this->_process($child);

						if (trim($text) != '') {
							$result['value'] = $this->_process($child);
						}
					}
				}
			}

			if($node->hasAttributes()) {
				$attributes = $node->attributes;

				if(!is_null($attributes)) {
					foreach ($attributes as $key => $attr) {
						$result[$attr->name] = $attr->value;
					}
				}
			}
		}

		return $result;
	}

	function getResult() {
		return $this->_process($this->dom);
	}
}
Back to Top