Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* DataAccess.class.php */
- class DataAccess {
- private static $instance = NULL;
- private $db = NULL;
- private $perf = NULL;
- private $executeTime = NULL;
- private $numExecutes = NULL;
- private $result_sets = NULL;
- private $configuration_data = NULL;
- private $errors = NULL;
- private $queries = NULL;
- public $geoTables = NULL;
- private $db_info = NULL;
- private $blacklist_columns = NULL;
- private $already_filtered = NULL;
- private $old_config_columns = NULL;
- private $messages = NULL;
- private $messages_pages = NULL;
- private $language_id = NULL;
- private $category_in_stmt = NULL;
- private $subcategory_array = array();
- private $num_new_ads = NULL;
- private $preloaded_num_new_ads = NULL;
- private $allowedTags = false;
- private $ipIsChecked = NULL;
- private $_sqlWhereClauses = array();
- const ADODB_DEBUG = 0;
- public static function getInstance() {
- if (!(isset(self::$instance) && is_object(self::$instance))) {
- $c = "DataAccess";
- self::$instance = new $c();
- }
- return self::$instance;
- }
- private function __construct() {
- $this->results = array();
- $this->current_result_index = 0;
- $this->geoTables = new geoTables();
- }
- private function __clone() {
- throw new Exception("No cloning around!");
- }
- private function _blacklistColumns() {
- $ent_blacklist = array();
- $prem_blacklist = array("days_can_upgrade", "send_admin_end_email", "admin_email_edit", "admin_email_bcc", "num_free_pics", "sponsored_by_code", "registration_splash_code", "place_an_ad_splash_code", "invoice_date");
- $basic_blacklist = array("days_can_upgrade", "send_admin_end_email", "admin_email_edit", "admin_email_bcc", "num_free_pics", "geodesic_email_do" . __FILE__ . "s", "sponsored_by_code", "registration_splash_code", "place_an_ad_splash_code", "invoice_date");
- $other_blacklist = array("days_can_upgrade", "send_admin_end_email", "admin_email_edit", "admin_email_bcc", "num_free_pics", "geodesic_email_do" . __FILE__ . "s", "sponsored_by_code", "registration_splash_code", "place_an_ad_splash_code", "invoice_date", "force_default_settings" => array("all_ads_are_free" => 1));
- if (isset($this->blacklist_columns) && is_array($this->blacklist_columns)) {
- return $this->blacklist_columns;
- }
- if (defined("DISCOVERED") && geoPC::is_ent()) {
- $this->blacklist_columns = $ent_blacklist;
- } else if (defined("DISCOVERED") && geoPC::is_premier()) {
- $this->blacklist_columns = $prem_blacklist;
- } else if (defined("DISCOVERED") && geoPC::is_basic()) {
- $this->blacklist_columns = $basic_blacklist;
- } else if (defined("DISCOVERED")) {
- $this->blacklist_columns = $other_blacklist;
- } else {
- return $other_blacklist;
- }
- $start_str = rand(34, 19486) * 23919;
- $start_str = sha1($start_str . "Sheesh need so123@#F32jd231ek2@e me extra stuff" . time());
- $secret = sha1("sk\$3kWEFjdsKEQWekfJ3#2r@#Rl'sdjf\"\"LDfi" . $start_str . "Some more junk...DFSEfsdf3@#fj");
- $answer = geoPC::redirect_check($secret, $start_str);
- if ($answer !== sha1($secret . base64_encode($start_str) . "yaDaY32Adf sf \"df\" ><dfjK")) {
- exit("File Version Mismatch.");
- }
- return $this->blacklist_columns;
- }
- private function _checkSql($sql) {
- $sql = strtolower($sql);
- $blacklist = $this->_blacklistColumns();
- foreach($blacklist as $index => $column) {
- if ($index !== "force_default_settings" && strpos($sql, $column) !== false) {
- $this->errors.= "Query contained invalid column: " . $column;
- return false;
- }
- }
- return true;
- }
- private function _filterSettings($force_rerun = false) {
- if (!$force_rerun && isset($this->already_filtered['finished'])) {
- return $this->already_filtered;
- }
- $this->already_filtered = $this->configuration_data;
- $blacklist = $this->_blacklistColumns();
- foreach($blacklist as $key => $setting) {
- if ($key != "force_default_settings" && key_exists($setting, $this->already_filtered)) {
- unset($this->already_filtered[$setting]);
- } else if ($key == "force_default_settings" && is_array($setting)) {
- foreach($setting as $name => $value) {
- $this->already_filtered[$name] = $value;
- }
- }
- }
- if (defined("DISCOVERED")) {
- $this->already_filtered['finished'] = true;
- }
- return $this->already_filtered;
- }
- private function _checkResults($results) {
- return true;
- }
- private function init() {
- if (!$this->IsConnected()) {
- require (GEO_BASE_DIR . "config.default.php");
- $this->db_info = array("db_host" => $db_host, "db_name" => $database);
- try {
- $this->db = ADONewConnection($db_type);
- if (isset($persistent_connections) && $persistent_connections) {
- if (!$this->db->PConnect($db_host, $db_username, $db_password, $database)) {
- echo "Could not connect to database.";
- exit();
- }
- } else if (!$this->db->Connect($db_host, $db_username, $db_password, $database)) {
- echo "Could not connect to database.";
- exit();
- }
- }
- catch(exception $e) {
- echo "Could not connect to database.";
- exit();
- }
- if (defined("IAMDEVELOPER") && self::ADODB_DEBUG) {
- session_start();
- $this->db->LogSQL();
- $this->perf = & NewPerfMonitor($this->db);
- }
- if (isset($strict_mode) && $strict_mode) {
- $this->Execute("SET SESSION sql_mode=''");
- }
- if (isset($force_db_connection_charset) && strlen(trim($force_db_connection_charset))) {
- $this->db->Execute("SET NAMES '{$force_db_connection_charset}'");
- }
- $this->db->SetFetchMode(ADODB_FETCH_ASSOC);
- }
- }
- public function IsConnected() {
- if (isset($this->db) && is_object($this->db) && $this->db->IsConnected()) {
- return true;
- }
- return false;
- }
- public function Affected_Rows() {
- $this->init();
- return $this->db->Affected_Rows();
- }
- public function Execute($sql, $inputs = null) {
- $this->init();
- if (!$this->_checkSql($sql)) {
- return false;
- }
- trigger_error("DEBUG STATS_EXTRA: Using Execute wrapper! Query: " . $sql);
- $sqlI = "Execute: {$sql}";
- if (!isset($this->queries[$sqlI])) {
- $this->queries[$sqlI]['count'] = 1;
- } else {
- ++$this->queries[$sqlI]['count'];
- }
- $start = $this->microtime_float();
- try {
- if (is_array($inputs)) {
- $results = $this->db->Execute($sql, $inputs);
- } else {
- $results = $this->db->Execute($sql);
- }
- $execution_time = $this->microtime_float() - $start;
- $this->executeTime+= $execution_time;
- $this->numExecutes++;
- $this->queries[$sqlI]['time'][] = $execution_time;
- if ($results === false) {
- trigger_error("ERROR SQL: Sql Query: " . $sql . " Error Reported: " . $this->db->ErrorMsg());
- return false;
- }
- }
- catch(Exception $e) {
- trigger_error("ERROR SQL STATS: Sql Query: " . $sql . " Error Caught: " . print_r($e, 1));
- return false;
- }
- return $results;
- }
- public function ErrorMsg() {
- if (isset($this->errors) && strlen($this->errors)) {
- return $this->errors;
- }
- $this->init();
- return $this->db->ErrorMsg();
- }
- public function qstr($var_1, $var_2) {
- $this->init();
- return $this->db->qstr($var_1, $var_2);
- }
- public function Prepare($sql) {
- $this->init();
- if (!$this->_checkSql($sql)) {
- return false;
- }
- $sqlI = "Prepare: {$sql}";
- if (!isset($this->queries[$sqlI])) {
- $this->queries[$sqlI]['count'] = 1;
- } else {
- ++$this->queries[$sqlI]['count'];
- }
- $start = $this->microtime_float();
- $statement = $this->db->Prepare($sql);
- $execution_time = $this->microtime_float() - $start;
- $this->executeTime+= $execution_time;
- $this->numExecutes++;
- $this->queries[$sqlI]['time'][] = $execution_time;
- if ($statement === false) {
- trigger_error("ERROR SQL: Error Running db->Prepare(), query: " . $sql . " Error Message: " . $this->db->ErrorMsg());
- return false;
- }
- return $statement;
- }
- public function GetArray($sql) {
- $this->init();
- if (!$this->_checkSql($sql)) {
- return false;
- }
- $sqlI = "GetArray: {$sql}";
- if (!isset($this->queries[$sqlI])) {
- $this->queries[$sqlI]['count'] = 1;
- } else {
- ++$this->queries[$sqlI]['count'];
- }
- $start = $this->microtime_float();
- try {
- $result = $this->db->GetArray($sql);
- $execution_time = $this->microtime_float() - $start;
- $this->executeTime+= $execution_time;
- $this->numExecutes++;
- $this->queries[$sqlI]['time'][] = $execution_time;
- if ($result === false) {
- trigger_error("ERROR SQL: Error Running db->GetArray(), query: " . $sql . " Error Message: " . $this->db->ErrorMsg());
- return false;
- }
- return $result;
- }
- catch(Exception $e) {
- trigger_error("ERROR SQL STATS: Sql Query: " . $sql . " Error Caught: " . print_r($e, 1));
- return false;
- }
- }
- public function GetOne($sql, $inputarr = false) {
- $this->init();
- if (!$this->_checkSql($sql)) {
- return false;
- }
- $sqlI = "GetOne: {$sql}";
- if (!isset($this->queries[$sqlI])) {
- $this->queries[$sqlI]['count'] = 1;
- } else {
- ++$this->queries[$sqlI]['count'];
- }
- $start = $this->microtime_float();
- try {
- $result = $this->db->GetOne($sql, $inputarr);
- $execution_time = $this->microtime_float() - $start;
- $this->executeTime+= $execution_time;
- $this->numExecutes++;
- $this->queries[$sqlI]['time'][] = $execution_time;
- }
- catch(Exception $e) {
- trigger_error("ERROR SQL STATS: Error Running db->GetOne(), query: " . $sql . " Error Message: " . $this->db->ErrorMsg());
- return false;
- }
- return $result;
- }
- public function SetFetchMode($value) {
- $this->init();
- try {
- return $this->db->SetFetchMode($value);
- }
- catch(Exception $e) {
- trigger_error("ERROR SQL STATS: Error Caught: " . print_r($e, 1));
- return false;
- }
- }
- public function Insert_Id() {
- $this->init();
- return $this->db->Insert_Id();
- }
- public function GetRow($sql, $data = null) {
- $this->init();
- if (!$this->_checkSql($sql)) {
- return false;
- }
- $sqlI = "GetRow: {$sql}";
- if (!isset($this->queries[$sqlI])) {
- $this->queries[$sqlI]['count'] = 1;
- } else {
- ++$this->queries[$sqlI]['count'];
- }
- $start = $this->microtime_float();
- try {
- $result = $this->db->GetRow($sql, $data);
- $execution_time = $this->microtime_float() - $start;
- $this->executeTime+= $execution_time;
- $this->numExecutes++;
- $this->queries[$sqlI]['time'][] = $execution_time;
- }
- catch(Exception $e) {
- trigger_error("ERROR SQL STATS: Error Caught: " . $this->ErrorMsg());
- return false;
- }
- return $result;
- }
- public function GetCol($var1, $var2 = null) {
- $this->init();
- if (!$this->_checkSql($var1)) {
- return false;
- }
- $sqlI = "GetCol: {$var1}";
- if (!isset($this->queries[$sqlI])) {
- $this->queries[$sqlI]['count'] = 1;
- } else {
- ++$this->queries[$sqlI]['count'];
- }
- $start = $this->microtime_float();
- try {
- $result = $this->db->GetCol($var1, $var2);
- $execution_time = $this->microtime_float() - $start;
- $this->executeTime+= $execution_time;
- $this->numExecutes++;
- $this->queries[$sqlI]['time'][] = $execution_time;
- }
- catch(Exception $e) {
- trigger_error("ERROR SQL: Query: " . $var1 . " Error Caught: " . $this->ErrorMsg());
- }
- return $result;
- }
- public function GetAssoc($sql, $data = null) {
- $this->init();
- if (!$this->_checkSql($sql)) {
- return false;
- }
- $sqlI = "GetAssoc: {$sql}";
- if (!isset($this->queries[$sqlI])) {
- $this->queries[$sqlI]['count'] = 1;
- } else {
- ++$this->queries[$sqlI]['count'];
- }
- $start = $this->microtime_float();
- $result = $this->db->GetAssoc($sql, $data);
- $execution_time = $this->microtime_float() - $start;
- $this->executeTime+= $execution_time;
- $this->numExecutes++;
- $this->queries[$sqlI]['time'][] = $execution_time;
- return $result;
- }
- public function GetAll($sql, $data = null) {
- $this->init();
- if (!$this->_checkSql($sql)) {
- return false;
- }
- $sqlI = "GetAll: {$sql}";
- if (!isset($this->queries[$sqlI])) {
- $this->queries[$sqlI]['count'] = 1;
- } else {
- ++$this->queries[$sqlI]['count'];
- }
- $start = $this->microtime_float();
- $result = $this->db->GetAll($sql, $data);
- $execution_time = $this->microtime_float() - $start;
- $this->executeTime+= $execution_time;
- $this->numExecutes++;
- $this->queries[$sqlI]['time'][] = $execution_time;
- if ($result === false) {
- trigger_error("ERROR SQL: in GetAll, sql: " . $sql . " : Error msg: " . $this->ErrorMsg());
- }
- return $result;
- }
- public function MetaError() {
- $this->init();
- return $this->db->MetaError();
- }
- public function Close() {
- if ($this->isConnected()) {
- $this->db->Close();
- $this->db = false;
- }
- }
- public function getStats() {
- $stats = "Num Queries: " . $this->numExecutes . " Time spent on queries: " . $this->executeTime . " sec.<br />" . "\n";
- $stats.= "Query Stats:" . "\n";
- $stats.= "<table border=\"1\"><thead><tr><th>Time(s) each query took</th><th>Query</th><th># times executed</th></thead><tbody>\n";
- foreach($this->queries as $query => $q_stat) {
- $totalT = 0;
- if (2 < count($q_stat['time'])) {
- foreach($q_stat['time'] as $t) {
- $totalT+= $t;
- }
- $q_stat['time'][] = "<br /><strong>Total</strong>:{$totalT}";
- }
- $stats.= "<tr><td>" . implode(", ", $q_stat['time']) . "</td><td>{$query}</td><td>{$q_stat['count']}</td></tr>\n";
- }
- $stats.= "</tbody></table>\n";
- if (defined("IAMDEVELOPER") && self::ADODB_DEBUG) {
- $this->perf->UI($pollsecs = 5);
- }
- return $stats;
- }
- public function get_site_settings($return_table = false) {
- if (isset($this->configuration_data)) {
- if ($return_table) {
- return $this->_filterSettings();
- }
- return true;
- }
- $use_cache = geoCache::get("cache_setting");
- if ($use_cache) {
- $cacheSettings = geoCacheSetting::getinstance();
- $config_data = $cacheSettings->process("configuration_data");
- }
- if (!$use_cache || $config_data === false) {
- $sql = "SELECT * FROM " . geoTables::site_configuration_table;
- $result = $this->Execute($sql);
- if ($result === false) {
- return false;
- }
- $this->configuration_data = $result->FetchRow();
- if ($use_cache) {
- $cacheSettings->update("configuration_data", $this->configuration_data);
- }
- } else {
- $this->configuration_data = $config_data;
- }
- if ($use_cache) {
- $site_settings = $cacheSettings->process("site_settings");
- }
- if (!$use_cache || $site_settings === false) {
- $sql = "SELECT `setting`, `value` FROM " . geoTables::site_settings_table;
- $rows = $this->GetAll($sql);
- if (false === $rows) {
- trigger_error("Unable to query the site_settings_table. " . $this->ErrorMsg());
- trigger_error("FLUSH MESSAGES");
- exit();
- }
- if ($use_cache) {
- $cacheAdd = array();
- }
- foreach($rows as $row) {
- $this->configuration_data[$row['setting']] = $row['value'];
- if ($use_cache) {
- $cacheAdd[$row['setting']] = $row['value'];
- }
- }
- if ($use_cache) {
- $cacheSettings->update("site_settings", $cacheAdd);
- }
- } else {
- foreach($site_settings as $key => $val) {
- $this->configuration_data[$key] = $val;
- }
- }
- if ($return_table) {
- return $this->_filterSettings();
- }
- return true;
- }
- private function init_old_config_columns() {
- if (!is_array($this->old_config_columns)) {
- $this->old_config_columns = array();
- $sql = "SHOW COLUMNS FROM " . geoTables::site_configuration_table;
- $result = $this->Execute($sql);
- if (!$result) {
- return false;
- }
- while ($row = $result->FetchRow()) {
- $this->old_config_columns[] = $row['Field'];
- }
- }
- }
- public function set_site_setting($setting, $value, $use_long = false) {
- if (in_array($setting, $this->_blacklistColumns())) {
- return false;
- }
- $preset = $this->_blacklistColumns();
- if (isset($preset['force_default_settings']) && array_key_exists($setting, $preset['force_default_settings'])) {
- $value = $preset['force_default_settings'][$setting];
- }
- if (isset($this->configuration_data[$setting]) && $this->configuration_data[$setting] === $value) {
- return true;
- }
- if ($value === false || $value === null) {
- $sql = "DELETE FROM " . geoTables::site_settings_table . " WHERE `setting` = ? LIMIT 1";
- $result = $this->Execute($sql, array($setting));
- if (!$result) {
- return false;
- }
- if ($use_long) {
- $sql = "DELETE FROM " . geoTables::site_settings_long_table . " WHERE `setting` = ? LIMIT 1";
- $result = $this->Execute($sql, array($setting));
- if (!$result) {
- trigger_error("ERROR SQL: Error deleting long setting " . $setting . " - Query: " . $sql . " ERROR: " . $this->db->ErrorMsg());
- return false;
- }
- }
- $this->configuration_data[$setting] = false;
- $this->init_old_config_columns();
- if (in_array($setting, $this->old_config_columns)) {
- $sql = "UPDATE " . geoTables::site_configuration_table . " SET `" . $setting . "` = 0 LIMIT 1";
- $result = $this->Execute($sql);
- if (!$result) {
- return false;
- }
- geoCacheSetting::expire("configuration_data");
- }
- geoCacheSetting::expire("site_settings");
- if ($use_long) {
- geoCacheSetting::expire("site_settings_long_" . $setting);
- }
- $this->_filterSettings(true);
- return true;
- }
- if (strlen($value) < 255 || !$use_long) {
- $table_to_use = geoTables::site_settings_table;
- $table_to_delete_from = geoTables::site_settings_long_table;
- } else {
- $table_to_use = geoTables::site_settings_long_table;
- $table_to_delete_from = geoTables::site_settings_table;
- }
- trigger_error("DEBUG STATS_EXTRA: DataAccess::set_site_setting() - Setting " . $setting . " to " . $value . " in table " . $table_to_use);
- $sql = "REPLACE INTO " . $table_to_use . " SET `setting` = ?, `value` = ?";
- $result = $this->Execute($sql, array($setting, $value));
- if (!$result) {
- trigger_error("ERROR STATS_EXTRA SQL: DataAccess::set_site_setting() - Setting " . $setting . " query failed! Setting not set!");
- return false;
- }
- if ($use_long) {
- trigger_error("DEBUG STATS_EXTRA: DataAccess::set_site_setting() - use_long so deleting " . $setting . " from other table " . $table_to_delete_from);
- $sql = "DELETE FROM " . $table_to_delete_from . " WHERE `setting` = ? LIMIT 1";
- $result = $this->Execute($sql, array($setting));
- if (!$result) {
- return false;
- }
- }
- $this->configuration_data[$setting] = $value;
- geoCacheSetting::expire("site_settings");
- if ($use_long) {
- geoCacheSetting::expire("site_settings_long_" . $setting);
- }
- $this->_filterSettings(true);
- return true;
- }
- public function get_site_setting($setting, $check_long = false) {
- if (in_array($setting, $this->_blacklistColumns())) {
- return false;
- }
- $preset = $this->_blacklistColumns();
- if (isset($preset['force_default_settings']) && array_key_exists($setting, $preset['force_default_settings'])) {
- return $preset['force_default_settings'][$setting];
- }
- $this->get_site_settings();
- if (isset($this->configuration_data[$setting])) {
- return $this->configuration_data[$setting];
- }
- if (!$check_long) {
- return false;
- }
- if (strlen($setting) == 0) {
- return false;
- }
- $cacheSettings = geoCacheSetting::getinstance();
- $longCache = $cacheSettings->process("site_settings_long_" . $setting);
- if ($longCache === false) {
- try {
- $sql = "SELECT `setting`,`value` FROM " . geoTables::site_settings_long_table . " WHERE `setting`=?";
- $results = $this->Execute($sql, array($setting));
- if (!$results) {
- return false;
- }
- if ($results->NumRows() == 0) {
- $this->configuration_data[$setting] = false;
- return false;
- }
- $row = $results->FetchRow();
- if (!$row) {
- $this->configuration_data[$setting] = false;
- return false;
- }
- $cacheSettings->update("site_settings_long_" . $setting, array($setting => $row['value']));
- return $row['value'];
- }
- catch(Exception $e) {
- return false;
- }
- } else if (isset($longCache[$setting])) {
- return $longCache[$setting];
- } else {
- }
- return false;
- }
- public function get_text($return_text = false, $current_page_id = 0) {
- $this->init();
- if (!$current_page_id) {
- if ($return_text) {
- return $this->messages;
- }
- return false;
- }
- $in_array = array();
- $in_text = array();
- $args_list = is_array($current_page_id) ? $current_page_id : array($current_page_id);
- $cache_on = geoCache::get("cache_text");
- if ($cache_on) {
- $textCache = geoCacheText::getinstance();
- }
- $language_id = $this->getLanguage();
- $use_cache = true;
- foreach($args_list as $page_id) {
- if (!isset($this->messages_pages[$page_id])) {
- if ($cache_on) {
- $cacheTextArray = $textCache->process($language_id, $page_id);
- }
- if ($cache_on) {
- trigger_error("DEBUG STATS: cache process: \$language_id = " . $language_id . " page_id = " . $page_id);
- }
- if (!$cache_on || $cacheTextArray === false) {
- $use_cache = false;
- $in_array[] = $page_id;
- $in_text[] = "?";
- } else {
- if (!is_array($this->messages)) {
- $this->messages = array();
- }
- if (!is_array($cacheTextArray)) {
- }
- $keys = array_keys($cacheTextArray);
- foreach($keys as $key) {
- $this->messages[$key] = $cacheTextArray[$key];
- }
- }
- $this->messages_pages[$page_id] = 1;
- }
- }
- unset($page_id);
- if (count($in_array) == 0) {
- if ($return_text) {
- return $this->messages;
- }
- return true;
- }
- if (!$cache_on || !$use_cache) {
- $where = "";
- if (count($in_array) == 1) {
- $where = "page_id = ?";
- $page_id = $in_array[0];
- } else {
- $where = "page_id in ( " . implode(", ", $in_text) . " )";
- }
- $sql = "SELECT `text_id`,`text`, `page_id` from " . geoTables::pages_text_languages_table . " WHERE {$where} AND `language_id` = '{$this->getLanguage() }'";
- if (!is_array($this->messages)) {
- $this->messages = array();
- }
- $result = $this->GetAll($sql, $in_array);
- if ($result === false) {
- trigger_error("ERROR SQL: sql error, sql: " . $sql . " Error message: " . $this->ErrorMsg());
- return false;
- }
- if ($cache_on) {
- $cacheArray = array();
- }
- foreach($result as $show) {
- $message = geoString::fromdb($show['text']);
- $message = geoTemplate::parseexternaltags($message);
- $this->messages[$show['text_id']] = $message;
- if ($cache_on) {
- $cacheArray[$show['page_id']][$show['text_id']] = $message;
- }
- }
- if ($cache_on && isset($page_id) && $page_id && isset($cacheArray[$page_id])) {
- trigger_error("DEBUG STATS: Updating: \$language_id: " . $language_id . " page_id = " . $page_id);
- $textCache->update($language_id, $page_id, $cacheArray[$page_id]);
- } else if ($cache_on) {
- foreach($cacheArray as $page_id => $page_text) {
- trigger_error("DEBUG STATS: Updating: \$language_id: " . $language_id . " page_id = " . $page_id . " \$page_text = " . htmlspecialchars($page_text));
- $textCache->update($language_id, $page_id, $page_text);
- }
- }
- }
- if ($return_text) {
- return $this->messages;
- }
- return true;
- }
- public function getLanguage($from_db = false) {
- if (isset($this->language_id)) {
- return $this->language_id;
- }
- if ($from_db) {
- $sql = "SELECT language_id FROM geodesic_pages_languages where default_language = 1";
- $result = $this->Execute($sql);
- if ($result && $result->RecordCount() == 1) {
- $row = $result->FetchRow();
- $this->language_id = $row['language_id'];
- return $this->language_id;
- }
- return 1;
- }
- $session = geoSession::getinstance();
- return $session->getLanguage();
- }
- public function get_sql_in_statement($category_id) {
- if ($category_id == 0) {
- return false;
- }
- if (!isset($this->category_in_stmt[$category_id])) {
- $sql = "SELECT in_statement FROM " . geoTables::categories_table . " WHERE category_id = ?";
- $result = $this->Execute($sql, array($category_id));
- if ($result === false) {
- return false;
- }
- if ($result->RecordCount() == 1) {
- $show_in_statement = $result->FetchRow();
- $current_in_statement = $show_in_statement['in_statement'];
- if (0 < strlen(trim($current_in_statement))) {
- $this->category_in_stmt[$category_id] = $current_in_statement;
- } else {
- $this->get_sql_in_array($category_id);
- if (0 < count($this->subcategory_array)) {
- $this->category_in_stmt[$category_id] = "in (" . implode(", ", $this->subcategory_array) . ")";
- $sql = "update " . geoTables::categories_table . " set in_statement = ? WHERE category_id = ?";
- $result = $this->Execute($sql, array($this->category_in_stmt[$category_id], $category_id));
- } else {
- return false;
- }
- }
- }
- }
- return $this->category_in_stmt[$category_id];
- }
- public function get_sql_in_array($category_id) {
- $count = 0;
- trigger_error("ERROR STATS: This should not be run!! Category in_statement not set, failsafe to calculate in_statement being used (so site will be running slowly). Category ID:" . $category_id);
- $sql = "select category_id from " . geoTables::categories_table . " where parent_id = ?";
- $result = $this->Execute($sql, array($category_id));
- if ($result === false) {
- return false;
- }
- while (0 < $result->RecordCount() && ($show_category = $result->FetchRow())) {
- $this->get_sql_in_array($show_category['category_id']);
- }
- array_push($this->subcategory_array, $category_id);
- return true;
- }
- public function num_new_ads_in_category($category_id, $ends_limit, $placed_limit) {
- if (!isset($this->preloaded_num_new_ads)) {
- $this->preload_num_new_ads($ends_limit, $placed_limit);
- }
- if (!isset($this->num_new_ads[$category_id]) && !isset($this->preloaded_num_new_ads)) {
- $in_statement = $this->get_sql_in_statement($category_id);
- $sql = "select count(id) as count from " . geoTables::classifieds_table . "\twhere\n\t\t\t\tlive = ? and\n\t\t\t\tends > ? and\n\t\t\t\tdate > ? and\n\t\t\t\tcategory ?";
- $new_ad_result = $this->Execute($sql, array(1, $ends_limit, $placed_limit, $in_statement));
- if ($new_ad_result === false) {
- return false;
- }
- if ($new_ad_result->RecordCount() == 1) {
- $show_count = $new_ad_result->FetchRow();
- $this->num_new_ads[$category_id] = $show_count['count'];
- }
- } else if (isset($this->preloaded_num_new_ads) && !isset($this->num_new_ads[$category_id])) {
- $this->num_new_ads[$category_id] = 0;
- }
- return $this->num_new_ads[$category_id];
- }
- public function preload_num_new_ads($ends_limit, $placed_limit) {
- if (!isset($this->preloaded_num_new_ads)) {
- $this->preloaded_num_new_ads = true;
- $filter_stmt = $tables = "";
- $filter_data = array();
- if ($this->isBrowsingWhereClause()) {
- $filters = $this->getBrowsingWhereClause();
- $filter_stmt = " AND " . $filters['sql'];
- $filter_data = $filters['data'];
- foreach($filters['tables'] as $t) {
- $tables.= ", " . $t;
- }
- }
- $sql = "SELECT DISTINCT `category` FROM " . geoTables::classifieds_table . "{$tables} WHERE\n\t\t\t\t\t`live` = ? AND\n\t\t\t\t\t`ends` > ? AND\n\t\t\t\t\t`date` > ?{$filter_stmt}";
- trigger_error("DEBUG STATS: Starting preload_num_new_ads.");
- $query_data = array(1, $ends_limit, $placed_limit);
- foreach($filter_data as $d) {
- $query_data[] = $d;
- }
- $result = $this->Execute($sql, $query_data);
- if ($result === false) {
- $this->preloaded_num_new_ads = false;
- return false;
- }
- $sql = "SELECT `parent_id` FROM " . geoTables::categories_table . " WHERE `category_id` = ?";
- $stmt = $this->db->Prepare($sql);
- while ($row = $result->FetchRow()) {
- if (isset($this->num_new_ads[$row['category']]) && 0 < $this->num_new_ads[$row['category']]) {
- continue;
- }
- $parent_result = $this->Execute($stmt, array($row['category']));
- if (!$parent_result) {
- trigger_error("ERROR SQL: Sql: " . $sql . " Error: " . $this->ErrorMsg());
- return false;
- }
- $first_row = $parent_result->FetchRow();
- $parent_id = $first_row['parent_id'];
- $this->num_new_ads[$row['category']] = 1;
- while (0 < $parent_id) {
- if (isset($this->num_new_ads[$parent_id]) && 0 < $this->num_new_ads[$parent_id]) {
- break;
- }
- $this->num_new_ads[$parent_id] = 1;
- $parent_result = $this->Execute($stmt, array($parent_id));
- if (!$parent_result) {
- trigger_error("ERROR SQL: Sql: " . $sql . " Error: " . $this->ErrorMsg());
- return false;
- }
- $this_row = $parent_result->FetchRow();
- $parent_id = $this_row['parent_id'];
- }
- }
- trigger_error("DEBUG STATS STATS: Finished preload_num_new_ads.");
- }
- }
- private static function _modFiles() {
- return array("module_display_category_navigation_1.php", "module_display_category_navigation_2.php", "module_display_category_navigation_3.php", "module_display_category_level_navigation_1.php", "module_display_subcategory_navigation_1.php", "module_display_subcategory_navigation_2.php", "module_display_subcategory_navigation_3.php", "module_display_subcategory_navigation_4.php", "module_display_subcategory_navigation_5.php", "module_display_subcategory_navigation_6.php", "module_display_subcategory_navigation_7.php", "module_featured_ads_pic_1_level_2.php", "module_featured_ads_pic_1_level_3.php", "module_featured_ads_pic_1_level_4.php", "module_featured_ads_pic_1_level_5.php", "module_featured_ads_pic_2_level_2.php", "module_featured_ads_pic_2_level_3.php", "module_featured_ads_pic_2_level_4.php", "module_featured_ads_pic_2_level_5.php", "module_featured_ads_1_level_2.php", "module_featured_ads_1_level_3.php", "module_featured_ads_1_level_4.php", "module_featured_ads_1_level_5.php", "module_featured_ads_2_level_2.php", "module_featured_ads_2_level_3.php", "module_featured_ads_2_level_4.php", "module_featured_ads_2_level_5.php", "module_featured_ads2.php", "module_featured_ads3.php", "module_featured_ads4.php", "module_featured_ads5.php", "module_newest_ads2.php", "module_featured_ads_pic_2.php", "module_featured_ads_pic_3.php", "module_featured_ads_from_category_1.php", "module_featured_ads_from_category_2.php");
- }
- public function replaceTag($tag, $page, $depth) {
- if (25 < $depth) {
- return "";
- }
- $addon = geoAddon::getinstance();
- trigger_error("DEBUG STATS: DataAccess::replaceTag(" . $tag . ") - top");
- if (!geoPC::is_ent()) {
- $arr = self::_modfiles();
- }
- $db = $this;
- $view = geoView::getinstance();
- $language_id = $this->getLanguage();
- $cat_id = $page->site_category;
- $logged_in = geoSession::getinstance()->getUserID();
- $pageCache = geoCachePage::getinstance();
- $settingsCache = geoCacheSetting::getinstance();
- $filter_query_data = null;
- $filter_sql = $filter_tables = "";
- if ($this->isBrowsingWhereClause()) {
- $filters = $this->getBrowsingWhereClause();
- $filter_sql.= " AND " . $filters['sql'];
- $filter_query_data = $filters['data'];
- foreach($filters['tables'] as $t) {
- $filter_tables.= ", " . $t;
- }
- }
- if ($this->allowedTags === false) {
- $allowedTags = $settingsCache->process("module_tag_list");
- if ($allowedTags === false) {
- $sql = "SELECT `module_replace_tag` FROM `geodesic_pages` WHERE `module` = 1";
- $tagResult = $this->Execute($sql);
- $allowedTags = array();
- if (!$tagResult) {
- trigger_error("ERROR SQL: sql:" . $sql . " Error:" . $this->ErrorMsg());
- return "";
- }
- while ($row = $tagResult->FetchRow()) {
- if (!in_array($row['module_replace_tag'], $allowedTags) && 0 < strlen($row['module_replace_tag'])) {
- $allowedTags[] = $row['module_replace_tag'];
- }
- }
- $settingsCache->update("module_tag_list", $allowedTags);
- }
- $this->allowedTags = $allowedTags;
- }
- if (!in_array($tag, $this->allowedTags)) {
- trigger_error("DEBUG STATS: DataAccess::replaceTag(" . $tag . ") - not allowed tag");
- return "";
- }
- $cacheResult = $pageCache->process($tag, $language_id, $cat_id, $logged_in, true);
- if ($cacheResult === false) {
- $remember_body = $page->body;
- $page->body = "";
- if (geoCache::get("cache_module")) {
- $moduleCache = geoCacheModule::getinstance();
- $show_module = $moduleCache->process($tag);
- } else {
- $show_module = false;
- }
- if (!$show_module) {
- $sql = "SELECT * FROM `geodesic_pages` WHERE `module_replace_tag` = ?";
- $result = $this->Execute($sql, array($tag));
- if (!$result) {
- trigger_error("ERROR SQL: sql:" . $sql . " Error: " . $this->ErrorMsg());
- return false;
- }
- $show_module = $result->FetchRow();
- if (geoCache::get("cache_module")) {
- $moduleCache->update($tag, $show_module);
- }
- }
- if (isset($show_module['module_file_name']) && file_exists(MODULES_DIR . $show_module['module_file_name']) && (geoPC::is_ent() || !in_array($show_module['module_file_name'], $arr))) {
- $page->messages = $this->get_text(true, $show_module['page_id']);
- $page->language_id = $language_id;
- require (MODULES_DIR . $show_module['module_file_name']);
- $geo_inc_files = $view->geo_inc_files;
- $cacheResult = $page->body;
- unset($geo_inc_files);
- unset($tpl);
- unset($vars);
- }
- $page->body = $remember_body;
- }
- return $cacheResult;
- }
- public function sendMail($to, $subject, $content, $from = 0, $replyTo = 0, $charset = 0, $type = 0) {
- geoEmail::sendmail($to, $subject, $content, $from, $replyTo, $charset, $type);
- return true;
- }
- public function getDbInfo() {
- $this->init();
- return $this->db_info;
- }
- public function microtime_float() {
- list($usec, $sec) = explode(" ", microtime());
- $sec = (float)($sec + $usec);
- return ((float)($sec * 1000000));
- }
- public function checkBannedIp() {
- if (defined("IN_ADMIN") || isset($this->ipIsBanned) && $this->ipIsChecked) {
- return true;
- }
- $ip_to_check = $_SERVER['REMOTE_ADDR'];
- $sql = "SELECT `ip` FROM " . $this->geoTables->ip_ban_table;
- $this->init();
- $ip_result = $this->db->Execute($sql);
- if (!$ip_result) {
- trigger_error("ERROR SQL: SQL query failed for retrieving banned ips. Query:" . $sql . " Error: " . $this->ErrorMsg());
- return false;
- }
- if (0 < $ip_result->RecordCount()) {
- $ban_me = false;
- while (!$ban_me && ($ip_banned = $ip_result->FetchRow())) {
- $ip_banned = str_replace(".", "\\.", $ip_banned['ip']);
- $ip_banned = "/^" . str_replace("*", "[0-9.]*", $ip_banned) . "\$/";
- if (preg_match($ip_banned, $ip_to_check) == 1) {
- $ban_me = true;
- }
- }
- if (isset($_GET['check_ban_ip']) && $_GET['check_ban_ip']) {
- exit($ip_to_check . " - IP BANNED? " . ($ban_me ? "YES, this IP will only see front page of site." : "NO, this ip not banned."));
- }
- if ($ban_me) {
- $_REQUEST = array();
- $_POST = array();
- $_GET = array();
- $_COOKIE = array();
- if (!defined("IS_ROBOT")) {
- define("IS_ROBOT", 1);
- }
- }
- }
- $this->ipIsChecked = true;
- return true;
- }
- public function addBrowsingWhereClause($name, $sql, $data = null, $additionalTable = null) {
- if (strlen(trim($name)) == 0) {
- return false;
- }
- if (strlen(trim($sql)) == 0) {
- unset($this->_sqlWhereClauses[$name]);
- } else {
- if ($data !== null && !is_array($data)) {
- $data = array($data);
- }
- $this->_sqlWhereClauses[$name] = array("sql" => trim($sql), "data" => $data, "tables" => $additionalTable);
- }
- }
- public function getBrowsingWhereClause() {
- if (!is_array($this->_sqlWhereClauses) || count($this->_sqlWhereClauses) == 0) {
- if (!is_array($this->_sqlWhereClauses)) {
- trigger_error("ERROR SQL: \$this->_sqlWhereClauses is not an array, how did this happen???");
- }
- return "";
- }
- $data = $sql_parts = $tables = array();
- foreach($this->_sqlWhereClauses as $name => $info) {
- if ($info['data'] !== null) {
- foreach($info['data'] as $d) {
- $data[] = $d;
- }
- }
- if ($info['tables'] !== null && !in_array(trim($info['tables']), $tables)) {
- $tables[] = $info['tables'];
- }
- $sql_parts[] = $info['sql'];
- }
- return array("sql" => implode(" AND ", $sql_parts), "data" => $data, "tables" => $tables);
- }
- public function isBrowsingWhereClause() {
- return 0 < count($this->_sqlWhereClauses);
- }
- public function tableExists($tableName) {
- $this->init();
- $result = $this->db->Execute("show tables");
- while ($row = $result->FetchRow()) {
- if (!in_array($tableName, $row)) {
- continue;
- }
- return true;
- }
- return false;
- }
- }
- require_once (CLASSES_DIR . PHP5_DIR . "config.database_tables.php");
- ?>
Add Comment
Please, Sign In to add comment