public function getCity($ip) { $record = $this->fetchRow("select * from auto_pre_iptocity WHERE ip = ?", array($ip), 3600); if (!empty($record)) { $rec = json_decode($record['details'], 1); return $rec; } if (empty($record)) { $this->clearCache($this->sql, array($ip)); $details = iptocity($ip); if (empty($details) || $details['status'] != 'OK') { return false; } $geo = new Models_Geo(); $nearby = $geo->get_nearby_cities($details['lat'], $details['lon']); $rec = array(); foreach ($nearby as $v) { $rec = $v; break; } if (empty($rec)) { return false; } $d = array(); $d['ip'] = $ip; $d['details'] = json_encode($rec); $this->addDetails('auto_pre_iptocity', $d); return $rec; } return false; }
} } $firebase->update($path, $data); // updates data in Firebase break; case 'push': if (empty($_REQUEST['data'])) { throw new Exception('empty data'); } $data = $_REQUEST['data']; if (!is_array($data)) { $data = json_decode($_REQUEST['data'], 1); } if (!empty($_REQUEST['saveIP'])) { $ip = !empty($_REQUEST['ip']) ? $_REQUEST['ip'] : $_SERVER['REMOTE_ADDR']; $ipDetails = iptocity($ip); if (!empty($ipDetails['result'])) { $data['currentLocation'] = $ipDetails['result']; } } $firebase->push($path, $data); // push data to Firebase break; } $result = array('success' => 1, 'arr' => $arr); } catch (Exception $e) { $result = array('success' => 0, 'msg' => $e->getMessage()); } $txt = json_encode($result); echo $txt; exit;
public function update($tableName, $tableTag, $id, $params) { $arr = array(); $q = 'select * from ' . $tableName . ' WHERE id = ?'; $res = $this->fetchRow($q, array($id), 0); if ($res['uid'] != UID) { throw new Exception('invalid user'); } $data = $params['data']; if (!is_array($data)) { $data = json_decode($params['data'], 1); } $arr['details'] = json_encode($data); if (!empty($params['saveIP'])) { $ip = !empty($params['ip']) ? $params['ip'] : $_SERVER['REMOTE_ADDR']; $q = 'select * from users_info WHERE ip = ?'; $res = $this->fetchRow($q, array($ip), TIMEBIG); if (empty($res)) { $ipDetails = iptocity($ip); $d = array(); $d['ip'] = $ip; $d['users_info'] = json_encode($ipDetails['result']); $this->addDetails('users_info', $d); $res = $this->fetchRow($q, array($ip), TIMEBIG); } $arr['users_info_id'] = $res['users_info_id']; } $arr['title'] = isset($params['title']) ? $params['title'] : null; $arr['status'] = isset($params['status']) ? $params['status'] : 1; $arr['path'] = isset($params['path']) ? $params['path'] : null; $arr['deleted'] = isset($params['deleted']) ? $params['deleted'] : 0; $arr['admin_approved'] = isset($params['appr']) ? $params['appr'] : 0; $arr['updated'] = date('Y-m-d H:i:s'); $arr['uid'] = UID; //searchable fields $arr['i1'] = isset($params['i1']) ? $params['i1'] : null; $arr['i2'] = isset($params['i2']) ? $params['i2'] : null; $arr['d1'] = isset($params['d1']) ? $params['d1'] : null; $arr['d2'] = isset($params['d2']) ? $params['d2'] : null; $arr['vc1'] = isset($params['vc1']) ? $params['vc1'] : null; $arr['vc2'] = isset($params['vc2']) ? $params['vc2'] : null; $arr['t1'] = isset($params['t1']) ? $params['t1'] : null; $arr['t2'] = isset($params['t2']) ? $params['t2'] : null; if (!empty($params['location'])) { $q = 'select * from locations WHERE place_id = ?'; $res = $this->fetchRow($q, array($params['location']['place_id']), TIMEBIG); if (empty($res)) { $id = $this->addDetails('locations', $params['location']); $res = $this->fetchRow($q, array($params['location']['place_id']), TIMEBIG); } $arr['location_id'] = $res['location_id']; $arr['lat'] = $params['location']['latitude']; $arr['lng'] = $params['location']['longitude']; } //insert into main db $where = sprintf('id = %s', $this->qstr($id)); $check = $this->updateDetails($tableName, $arr, $where); $arr['id'] = $id; $arr['location'] = !empty($params['location']) ? $params['location'] : null; $q = 'delete from ' . $tableTag . ' WHERE id = ?'; $this->deleteDetails($q, array($id)); //insert into tags db if (!empty($params['tags'])) { $d = array(); $d['id'] = $id; $exp = explode(',', $params['tags']); $arr['tags'] = array(); if (!empty($exp)) { foreach ($exp as $v) { $d['tag'] = trim($v); if (empty($d['tag'])) { continue; } $arr['tags'][] = $d['tag']; $this->addDetails($tableTag, $d); } } } return $arr; }