johnburn

Decoded for: [email protected]

May 30th, 2011
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 44.21 KB | None | 0 0
  1. <?php
  2.  
  3. /* DataAccess.class.php */
  4. class DataAccess {
  5.     private static $instance = NULL;
  6.     private $db = NULL;
  7.     private $perf = NULL;
  8.     private $executeTime = NULL;
  9.     private $numExecutes = NULL;
  10.     private $result_sets = NULL;
  11.     private $configuration_data = NULL;
  12.     private $errors = NULL;
  13.     private $queries = NULL;
  14.     public $geoTables = NULL;
  15.     private $db_info = NULL;
  16.     private $blacklist_columns = NULL;
  17.     private $already_filtered = NULL;
  18.     private $old_config_columns = NULL;
  19.     private $messages = NULL;
  20.     private $messages_pages = NULL;
  21.     private $language_id = NULL;
  22.     private $category_in_stmt = NULL;
  23.     private $subcategory_array = array();
  24.     private $num_new_ads = NULL;
  25.     private $preloaded_num_new_ads = NULL;
  26.     private $allowedTags = false;
  27.     private $ipIsChecked = NULL;
  28.     private $_sqlWhereClauses = array();
  29.     const ADODB_DEBUG = 0;
  30.     public static function getInstance() {
  31.         if (!(isset(self::$instance) && is_object(self::$instance))) {
  32.             $c = "DataAccess";
  33.             self::$instance = new $c();
  34.         }
  35.         return self::$instance;
  36.     }
  37.     private function __construct() {
  38.         $this->results = array();
  39.         $this->current_result_index = 0;
  40.         $this->geoTables = new geoTables();
  41.     }
  42.     private function __clone() {
  43.         throw new Exception("No cloning around!");
  44.     }
  45.     private function _blacklistColumns() {
  46.         $ent_blacklist = array();
  47.         $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");
  48.         $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");
  49.         $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));
  50.         if (isset($this->blacklist_columns) && is_array($this->blacklist_columns)) {
  51.             return $this->blacklist_columns;
  52.         }
  53.         if (defined("DISCOVERED") && geoPC::is_ent()) {
  54.             $this->blacklist_columns = $ent_blacklist;
  55.         } else if (defined("DISCOVERED") && geoPC::is_premier()) {
  56.             $this->blacklist_columns = $prem_blacklist;
  57.         } else if (defined("DISCOVERED") && geoPC::is_basic()) {
  58.             $this->blacklist_columns = $basic_blacklist;
  59.         } else if (defined("DISCOVERED")) {
  60.             $this->blacklist_columns = $other_blacklist;
  61.         } else {
  62.             return $other_blacklist;
  63.         }
  64.         $start_str = rand(34, 19486) * 23919;
  65.         $start_str = sha1($start_str . "Sheesh need so123@#F32jd231ek2@e me extra stuff" . time());
  66.         $secret = sha1("sk\$3kWEFjdsKEQWekfJ3#2r@#Rl'sdjf\"\"LDfi" . $start_str . "Some more junk...DFSEfsdf3@#fj");
  67.         $answer = geoPC::redirect_check($secret, $start_str);
  68.         if ($answer !== sha1($secret . base64_encode($start_str) . "yaDaY32Adf sf \"df\" ><dfjK")) {
  69.             exit("File Version Mismatch.");
  70.         }
  71.         return $this->blacklist_columns;
  72.     }
  73.     private function _checkSql($sql) {
  74.         $sql = strtolower($sql);
  75.         $blacklist = $this->_blacklistColumns();
  76.         foreach($blacklist as $index => $column) {
  77.             if ($index !== "force_default_settings" && strpos($sql, $column) !== false) {
  78.                 $this->errors.= "Query contained invalid column: " . $column;
  79.                 return false;
  80.             }
  81.         }
  82.         return true;
  83.     }
  84.     private function _filterSettings($force_rerun = false) {
  85.         if (!$force_rerun && isset($this->already_filtered['finished'])) {
  86.             return $this->already_filtered;
  87.         }
  88.         $this->already_filtered = $this->configuration_data;
  89.         $blacklist = $this->_blacklistColumns();
  90.         foreach($blacklist as $key => $setting) {
  91.             if ($key != "force_default_settings" && key_exists($setting, $this->already_filtered)) {
  92.                 unset($this->already_filtered[$setting]);
  93.             } else if ($key == "force_default_settings" && is_array($setting)) {
  94.                 foreach($setting as $name => $value) {
  95.                     $this->already_filtered[$name] = $value;
  96.                 }
  97.             }
  98.         }
  99.         if (defined("DISCOVERED")) {
  100.             $this->already_filtered['finished'] = true;
  101.         }
  102.         return $this->already_filtered;
  103.     }
  104.     private function _checkResults($results) {
  105.         return true;
  106.     }
  107.     private function init() {
  108.         if (!$this->IsConnected()) {
  109.             require (GEO_BASE_DIR . "config.default.php");
  110.             $this->db_info = array("db_host" => $db_host, "db_name" => $database);
  111.             try {
  112.                 $this->db = ADONewConnection($db_type);
  113.                 if (isset($persistent_connections) && $persistent_connections) {
  114.                     if (!$this->db->PConnect($db_host, $db_username, $db_password, $database)) {
  115.                         echo "Could not connect to database.";
  116.                         exit();
  117.                     }
  118.                 } else if (!$this->db->Connect($db_host, $db_username, $db_password, $database)) {
  119.                     echo "Could not connect to database.";
  120.                     exit();
  121.                 }
  122.             }
  123.             catch(exception $e) {
  124.                 echo "Could not connect to database.";
  125.                 exit();
  126.             }
  127.             if (defined("IAMDEVELOPER") && self::ADODB_DEBUG) {
  128.                 session_start();
  129.                 $this->db->LogSQL();
  130.                 $this->perf = & NewPerfMonitor($this->db);
  131.             }
  132.             if (isset($strict_mode) && $strict_mode) {
  133.                 $this->Execute("SET SESSION sql_mode=''");
  134.             }
  135.             if (isset($force_db_connection_charset) && strlen(trim($force_db_connection_charset))) {
  136.                 $this->db->Execute("SET NAMES '{$force_db_connection_charset}'");
  137.             }
  138.             $this->db->SetFetchMode(ADODB_FETCH_ASSOC);
  139.         }
  140.     }
  141.     public function IsConnected() {
  142.         if (isset($this->db) && is_object($this->db) && $this->db->IsConnected()) {
  143.             return true;
  144.         }
  145.         return false;
  146.     }
  147.     public function Affected_Rows() {
  148.         $this->init();
  149.         return $this->db->Affected_Rows();
  150.     }
  151.     public function Execute($sql, $inputs = null) {
  152.         $this->init();
  153.         if (!$this->_checkSql($sql)) {
  154.             return false;
  155.         }
  156.         trigger_error("DEBUG STATS_EXTRA: Using Execute wrapper!  Query: " . $sql);
  157.         $sqlI = "Execute: {$sql}";
  158.         if (!isset($this->queries[$sqlI])) {
  159.             $this->queries[$sqlI]['count'] = 1;
  160.         } else {
  161.             ++$this->queries[$sqlI]['count'];
  162.         }
  163.         $start = $this->microtime_float();
  164.         try {
  165.             if (is_array($inputs)) {
  166.                 $results = $this->db->Execute($sql, $inputs);
  167.             } else {
  168.                 $results = $this->db->Execute($sql);
  169.             }
  170.             $execution_time = $this->microtime_float() - $start;
  171.             $this->executeTime+= $execution_time;
  172.             $this->numExecutes++;
  173.             $this->queries[$sqlI]['time'][] = $execution_time;
  174.             if ($results === false) {
  175.                 trigger_error("ERROR SQL: Sql Query: " . $sql . " Error Reported: " . $this->db->ErrorMsg());
  176.                 return false;
  177.             }
  178.         }
  179.         catch(Exception $e) {
  180.             trigger_error("ERROR SQL STATS: Sql Query: " . $sql . " Error Caught: " . print_r($e, 1));
  181.             return false;
  182.         }
  183.         return $results;
  184.     }
  185.     public function ErrorMsg() {
  186.         if (isset($this->errors) && strlen($this->errors)) {
  187.             return $this->errors;
  188.         }
  189.         $this->init();
  190.         return $this->db->ErrorMsg();
  191.     }
  192.     public function qstr($var_1, $var_2) {
  193.         $this->init();
  194.         return $this->db->qstr($var_1, $var_2);
  195.     }
  196.     public function Prepare($sql) {
  197.         $this->init();
  198.         if (!$this->_checkSql($sql)) {
  199.             return false;
  200.         }
  201.         $sqlI = "Prepare: {$sql}";
  202.         if (!isset($this->queries[$sqlI])) {
  203.             $this->queries[$sqlI]['count'] = 1;
  204.         } else {
  205.             ++$this->queries[$sqlI]['count'];
  206.         }
  207.         $start = $this->microtime_float();
  208.         $statement = $this->db->Prepare($sql);
  209.         $execution_time = $this->microtime_float() - $start;
  210.         $this->executeTime+= $execution_time;
  211.         $this->numExecutes++;
  212.         $this->queries[$sqlI]['time'][] = $execution_time;
  213.         if ($statement === false) {
  214.             trigger_error("ERROR SQL: Error Running db->Prepare(), query: " . $sql . " Error Message: " . $this->db->ErrorMsg());
  215.             return false;
  216.         }
  217.         return $statement;
  218.     }
  219.     public function GetArray($sql) {
  220.         $this->init();
  221.         if (!$this->_checkSql($sql)) {
  222.             return false;
  223.         }
  224.         $sqlI = "GetArray: {$sql}";
  225.         if (!isset($this->queries[$sqlI])) {
  226.             $this->queries[$sqlI]['count'] = 1;
  227.         } else {
  228.             ++$this->queries[$sqlI]['count'];
  229.         }
  230.         $start = $this->microtime_float();
  231.         try {
  232.             $result = $this->db->GetArray($sql);
  233.             $execution_time = $this->microtime_float() - $start;
  234.             $this->executeTime+= $execution_time;
  235.             $this->numExecutes++;
  236.             $this->queries[$sqlI]['time'][] = $execution_time;
  237.             if ($result === false) {
  238.                 trigger_error("ERROR SQL: Error Running db->GetArray(), query: " . $sql . " Error Message: " . $this->db->ErrorMsg());
  239.                 return false;
  240.             }
  241.             return $result;
  242.         }
  243.         catch(Exception $e) {
  244.             trigger_error("ERROR SQL STATS: Sql Query: " . $sql . " Error Caught: " . print_r($e, 1));
  245.             return false;
  246.         }
  247.     }
  248.     public function GetOne($sql, $inputarr = false) {
  249.         $this->init();
  250.         if (!$this->_checkSql($sql)) {
  251.             return false;
  252.         }
  253.         $sqlI = "GetOne: {$sql}";
  254.         if (!isset($this->queries[$sqlI])) {
  255.             $this->queries[$sqlI]['count'] = 1;
  256.         } else {
  257.             ++$this->queries[$sqlI]['count'];
  258.         }
  259.         $start = $this->microtime_float();
  260.         try {
  261.             $result = $this->db->GetOne($sql, $inputarr);
  262.             $execution_time = $this->microtime_float() - $start;
  263.             $this->executeTime+= $execution_time;
  264.             $this->numExecutes++;
  265.             $this->queries[$sqlI]['time'][] = $execution_time;
  266.         }
  267.         catch(Exception $e) {
  268.             trigger_error("ERROR SQL STATS: Error Running db->GetOne(), query: " . $sql . " Error Message: " . $this->db->ErrorMsg());
  269.             return false;
  270.         }
  271.         return $result;
  272.     }
  273.     public function SetFetchMode($value) {
  274.         $this->init();
  275.         try {
  276.             return $this->db->SetFetchMode($value);
  277.         }
  278.         catch(Exception $e) {
  279.             trigger_error("ERROR SQL STATS: Error Caught: " . print_r($e, 1));
  280.             return false;
  281.         }
  282.     }
  283.     public function Insert_Id() {
  284.         $this->init();
  285.         return $this->db->Insert_Id();
  286.     }
  287.     public function GetRow($sql, $data = null) {
  288.         $this->init();
  289.         if (!$this->_checkSql($sql)) {
  290.             return false;
  291.         }
  292.         $sqlI = "GetRow: {$sql}";
  293.         if (!isset($this->queries[$sqlI])) {
  294.             $this->queries[$sqlI]['count'] = 1;
  295.         } else {
  296.             ++$this->queries[$sqlI]['count'];
  297.         }
  298.         $start = $this->microtime_float();
  299.         try {
  300.             $result = $this->db->GetRow($sql, $data);
  301.             $execution_time = $this->microtime_float() - $start;
  302.             $this->executeTime+= $execution_time;
  303.             $this->numExecutes++;
  304.             $this->queries[$sqlI]['time'][] = $execution_time;
  305.         }
  306.         catch(Exception $e) {
  307.             trigger_error("ERROR SQL STATS: Error Caught: " . $this->ErrorMsg());
  308.             return false;
  309.         }
  310.         return $result;
  311.     }
  312.     public function GetCol($var1, $var2 = null) {
  313.         $this->init();
  314.         if (!$this->_checkSql($var1)) {
  315.             return false;
  316.         }
  317.         $sqlI = "GetCol: {$var1}";
  318.         if (!isset($this->queries[$sqlI])) {
  319.             $this->queries[$sqlI]['count'] = 1;
  320.         } else {
  321.             ++$this->queries[$sqlI]['count'];
  322.         }
  323.         $start = $this->microtime_float();
  324.         try {
  325.             $result = $this->db->GetCol($var1, $var2);
  326.             $execution_time = $this->microtime_float() - $start;
  327.             $this->executeTime+= $execution_time;
  328.             $this->numExecutes++;
  329.             $this->queries[$sqlI]['time'][] = $execution_time;
  330.         }
  331.         catch(Exception $e) {
  332.             trigger_error("ERROR SQL: Query: " . $var1 . " Error Caught: " . $this->ErrorMsg());
  333.         }
  334.         return $result;
  335.     }
  336.     public function GetAssoc($sql, $data = null) {
  337.         $this->init();
  338.         if (!$this->_checkSql($sql)) {
  339.             return false;
  340.         }
  341.         $sqlI = "GetAssoc: {$sql}";
  342.         if (!isset($this->queries[$sqlI])) {
  343.             $this->queries[$sqlI]['count'] = 1;
  344.         } else {
  345.             ++$this->queries[$sqlI]['count'];
  346.         }
  347.         $start = $this->microtime_float();
  348.         $result = $this->db->GetAssoc($sql, $data);
  349.         $execution_time = $this->microtime_float() - $start;
  350.         $this->executeTime+= $execution_time;
  351.         $this->numExecutes++;
  352.         $this->queries[$sqlI]['time'][] = $execution_time;
  353.         return $result;
  354.     }
  355.     public function GetAll($sql, $data = null) {
  356.         $this->init();
  357.         if (!$this->_checkSql($sql)) {
  358.             return false;
  359.         }
  360.         $sqlI = "GetAll: {$sql}";
  361.         if (!isset($this->queries[$sqlI])) {
  362.             $this->queries[$sqlI]['count'] = 1;
  363.         } else {
  364.             ++$this->queries[$sqlI]['count'];
  365.         }
  366.         $start = $this->microtime_float();
  367.         $result = $this->db->GetAll($sql, $data);
  368.         $execution_time = $this->microtime_float() - $start;
  369.         $this->executeTime+= $execution_time;
  370.         $this->numExecutes++;
  371.         $this->queries[$sqlI]['time'][] = $execution_time;
  372.         if ($result === false) {
  373.             trigger_error("ERROR SQL: in GetAll, sql: " . $sql . " : Error msg: " . $this->ErrorMsg());
  374.         }
  375.         return $result;
  376.     }
  377.     public function MetaError() {
  378.         $this->init();
  379.         return $this->db->MetaError();
  380.     }
  381.     public function Close() {
  382.         if ($this->isConnected()) {
  383.             $this->db->Close();
  384.             $this->db = false;
  385.         }
  386.     }
  387.     public function getStats() {
  388.         $stats = "Num Queries: " . $this->numExecutes . " Time spent on queries: " . $this->executeTime . " sec.<br />" . "\n";
  389.         $stats.= "Query Stats:" . "\n";
  390.         $stats.= "<table border=\"1\"><thead><tr><th>Time(s) each query took</th><th>Query</th><th># times executed</th></thead><tbody>\n";
  391.         foreach($this->queries as $query => $q_stat) {
  392.             $totalT = 0;
  393.             if (2 < count($q_stat['time'])) {
  394.                 foreach($q_stat['time'] as $t) {
  395.                     $totalT+= $t;
  396.                 }
  397.                 $q_stat['time'][] = "<br /><strong>Total</strong>:{$totalT}";
  398.             }
  399.             $stats.= "<tr><td>" . implode(", ", $q_stat['time']) . "</td><td>{$query}</td><td>{$q_stat['count']}</td></tr>\n";
  400.         }
  401.         $stats.= "</tbody></table>\n";
  402.         if (defined("IAMDEVELOPER") && self::ADODB_DEBUG) {
  403.             $this->perf->UI($pollsecs = 5);
  404.         }
  405.         return $stats;
  406.     }
  407.     public function get_site_settings($return_table = false) {
  408.         if (isset($this->configuration_data)) {
  409.             if ($return_table) {
  410.                 return $this->_filterSettings();
  411.             }
  412.             return true;
  413.         }
  414.         $use_cache = geoCache::get("cache_setting");
  415.         if ($use_cache) {
  416.             $cacheSettings = geoCacheSetting::getinstance();
  417.             $config_data = $cacheSettings->process("configuration_data");
  418.         }
  419.         if (!$use_cache || $config_data === false) {
  420.             $sql = "SELECT * FROM " . geoTables::site_configuration_table;
  421.             $result = $this->Execute($sql);
  422.             if ($result === false) {
  423.                 return false;
  424.             }
  425.             $this->configuration_data = $result->FetchRow();
  426.             if ($use_cache) {
  427.                 $cacheSettings->update("configuration_data", $this->configuration_data);
  428.             }
  429.         } else {
  430.             $this->configuration_data = $config_data;
  431.         }
  432.         if ($use_cache) {
  433.             $site_settings = $cacheSettings->process("site_settings");
  434.         }
  435.         if (!$use_cache || $site_settings === false) {
  436.             $sql = "SELECT `setting`, `value` FROM " . geoTables::site_settings_table;
  437.             $rows = $this->GetAll($sql);
  438.             if (false === $rows) {
  439.                 trigger_error("Unable to query the site_settings_table. " . $this->ErrorMsg());
  440.                 trigger_error("FLUSH MESSAGES");
  441.                 exit();
  442.             }
  443.             if ($use_cache) {
  444.                 $cacheAdd = array();
  445.             }
  446.             foreach($rows as $row) {
  447.                 $this->configuration_data[$row['setting']] = $row['value'];
  448.                 if ($use_cache) {
  449.                     $cacheAdd[$row['setting']] = $row['value'];
  450.                 }
  451.             }
  452.             if ($use_cache) {
  453.                 $cacheSettings->update("site_settings", $cacheAdd);
  454.             }
  455.         } else {
  456.             foreach($site_settings as $key => $val) {
  457.                 $this->configuration_data[$key] = $val;
  458.             }
  459.         }
  460.         if ($return_table) {
  461.             return $this->_filterSettings();
  462.         }
  463.         return true;
  464.     }
  465.     private function init_old_config_columns() {
  466.         if (!is_array($this->old_config_columns)) {
  467.             $this->old_config_columns = array();
  468.             $sql = "SHOW COLUMNS FROM " . geoTables::site_configuration_table;
  469.             $result = $this->Execute($sql);
  470.             if (!$result) {
  471.                 return false;
  472.             }
  473.             while ($row = $result->FetchRow()) {
  474.                 $this->old_config_columns[] = $row['Field'];
  475.             }
  476.         }
  477.     }
  478.     public function set_site_setting($setting, $value, $use_long = false) {
  479.         if (in_array($setting, $this->_blacklistColumns())) {
  480.             return false;
  481.         }
  482.         $preset = $this->_blacklistColumns();
  483.         if (isset($preset['force_default_settings']) && array_key_exists($setting, $preset['force_default_settings'])) {
  484.             $value = $preset['force_default_settings'][$setting];
  485.         }
  486.         if (isset($this->configuration_data[$setting]) && $this->configuration_data[$setting] === $value) {
  487.             return true;
  488.         }
  489.         if ($value === false || $value === null) {
  490.             $sql = "DELETE FROM " . geoTables::site_settings_table . " WHERE `setting` = ? LIMIT 1";
  491.             $result = $this->Execute($sql, array($setting));
  492.             if (!$result) {
  493.                 return false;
  494.             }
  495.             if ($use_long) {
  496.                 $sql = "DELETE FROM " . geoTables::site_settings_long_table . " WHERE `setting` = ? LIMIT 1";
  497.                 $result = $this->Execute($sql, array($setting));
  498.                 if (!$result) {
  499.                     trigger_error("ERROR SQL: Error deleting long setting " . $setting . " - Query: " . $sql . " ERROR: " . $this->db->ErrorMsg());
  500.                     return false;
  501.                 }
  502.             }
  503.             $this->configuration_data[$setting] = false;
  504.             $this->init_old_config_columns();
  505.             if (in_array($setting, $this->old_config_columns)) {
  506.                 $sql = "UPDATE " . geoTables::site_configuration_table . " SET `" . $setting . "` = 0 LIMIT 1";
  507.                 $result = $this->Execute($sql);
  508.                 if (!$result) {
  509.                     return false;
  510.                 }
  511.                 geoCacheSetting::expire("configuration_data");
  512.             }
  513.             geoCacheSetting::expire("site_settings");
  514.             if ($use_long) {
  515.                 geoCacheSetting::expire("site_settings_long_" . $setting);
  516.             }
  517.             $this->_filterSettings(true);
  518.             return true;
  519.         }
  520.         if (strlen($value) < 255 || !$use_long) {
  521.             $table_to_use = geoTables::site_settings_table;
  522.             $table_to_delete_from = geoTables::site_settings_long_table;
  523.         } else {
  524.             $table_to_use = geoTables::site_settings_long_table;
  525.             $table_to_delete_from = geoTables::site_settings_table;
  526.         }
  527.         trigger_error("DEBUG STATS_EXTRA: DataAccess::set_site_setting() - Setting " . $setting . " to " . $value . " in table " . $table_to_use);
  528.         $sql = "REPLACE INTO " . $table_to_use . " SET `setting` = ?, `value` = ?";
  529.         $result = $this->Execute($sql, array($setting, $value));
  530.         if (!$result) {
  531.             trigger_error("ERROR STATS_EXTRA SQL: DataAccess::set_site_setting() - Setting " . $setting . " query failed!  Setting not set!");
  532.             return false;
  533.         }
  534.         if ($use_long) {
  535.             trigger_error("DEBUG STATS_EXTRA: DataAccess::set_site_setting() - use_long so deleting " . $setting . " from other table " . $table_to_delete_from);
  536.             $sql = "DELETE FROM " . $table_to_delete_from . " WHERE `setting` = ? LIMIT 1";
  537.             $result = $this->Execute($sql, array($setting));
  538.             if (!$result) {
  539.                 return false;
  540.             }
  541.         }
  542.         $this->configuration_data[$setting] = $value;
  543.         geoCacheSetting::expire("site_settings");
  544.         if ($use_long) {
  545.             geoCacheSetting::expire("site_settings_long_" . $setting);
  546.         }
  547.         $this->_filterSettings(true);
  548.         return true;
  549.     }
  550.     public function get_site_setting($setting, $check_long = false) {
  551.         if (in_array($setting, $this->_blacklistColumns())) {
  552.             return false;
  553.         }
  554.         $preset = $this->_blacklistColumns();
  555.         if (isset($preset['force_default_settings']) && array_key_exists($setting, $preset['force_default_settings'])) {
  556.             return $preset['force_default_settings'][$setting];
  557.         }
  558.         $this->get_site_settings();
  559.         if (isset($this->configuration_data[$setting])) {
  560.             return $this->configuration_data[$setting];
  561.         }
  562.         if (!$check_long) {
  563.             return false;
  564.         }
  565.         if (strlen($setting) == 0) {
  566.             return false;
  567.         }
  568.         $cacheSettings = geoCacheSetting::getinstance();
  569.         $longCache = $cacheSettings->process("site_settings_long_" . $setting);
  570.         if ($longCache === false) {
  571.             try {
  572.                 $sql = "SELECT `setting`,`value` FROM " . geoTables::site_settings_long_table . " WHERE `setting`=?";
  573.                 $results = $this->Execute($sql, array($setting));
  574.                 if (!$results) {
  575.                     return false;
  576.                 }
  577.                 if ($results->NumRows() == 0) {
  578.                     $this->configuration_data[$setting] = false;
  579.                     return false;
  580.                 }
  581.                 $row = $results->FetchRow();
  582.                 if (!$row) {
  583.                     $this->configuration_data[$setting] = false;
  584.                     return false;
  585.                 }
  586.                 $cacheSettings->update("site_settings_long_" . $setting, array($setting => $row['value']));
  587.                 return $row['value'];
  588.             }
  589.             catch(Exception $e) {
  590.                 return false;
  591.             }
  592.         } else if (isset($longCache[$setting])) {
  593.             return $longCache[$setting];
  594.         } else {
  595.         }
  596.         return false;
  597.     }
  598.     public function get_text($return_text = false, $current_page_id = 0) {
  599.         $this->init();
  600.         if (!$current_page_id) {
  601.             if ($return_text) {
  602.                 return $this->messages;
  603.             }
  604.             return false;
  605.         }
  606.         $in_array = array();
  607.         $in_text = array();
  608.         $args_list = is_array($current_page_id) ? $current_page_id : array($current_page_id);
  609.         $cache_on = geoCache::get("cache_text");
  610.         if ($cache_on) {
  611.             $textCache = geoCacheText::getinstance();
  612.         }
  613.         $language_id = $this->getLanguage();
  614.         $use_cache = true;
  615.         foreach($args_list as $page_id) {
  616.             if (!isset($this->messages_pages[$page_id])) {
  617.                 if ($cache_on) {
  618.                     $cacheTextArray = $textCache->process($language_id, $page_id);
  619.                 }
  620.                 if ($cache_on) {
  621.                     trigger_error("DEBUG STATS: cache process: \$language_id = " . $language_id . " page_id = " . $page_id);
  622.                 }
  623.                 if (!$cache_on || $cacheTextArray === false) {
  624.                     $use_cache = false;
  625.                     $in_array[] = $page_id;
  626.                     $in_text[] = "?";
  627.                 } else {
  628.                     if (!is_array($this->messages)) {
  629.                         $this->messages = array();
  630.                     }
  631.                     if (!is_array($cacheTextArray)) {
  632.                     }
  633.                     $keys = array_keys($cacheTextArray);
  634.                     foreach($keys as $key) {
  635.                         $this->messages[$key] = $cacheTextArray[$key];
  636.                     }
  637.                 }
  638.                 $this->messages_pages[$page_id] = 1;
  639.             }
  640.         }
  641.         unset($page_id);
  642.         if (count($in_array) == 0) {
  643.             if ($return_text) {
  644.                 return $this->messages;
  645.             }
  646.             return true;
  647.         }
  648.         if (!$cache_on || !$use_cache) {
  649.             $where = "";
  650.             if (count($in_array) == 1) {
  651.                 $where = "page_id = ?";
  652.                 $page_id = $in_array[0];
  653.             } else {
  654.                 $where = "page_id in ( " . implode(", ", $in_text) . " )";
  655.             }
  656.             $sql = "SELECT `text_id`,`text`, `page_id` from " . geoTables::pages_text_languages_table . " WHERE {$where} AND `language_id` = '{$this->getLanguage() }'";
  657.             if (!is_array($this->messages)) {
  658.                 $this->messages = array();
  659.             }
  660.             $result = $this->GetAll($sql, $in_array);
  661.             if ($result === false) {
  662.                 trigger_error("ERROR SQL: sql error, sql: " . $sql . " Error message: " . $this->ErrorMsg());
  663.                 return false;
  664.             }
  665.             if ($cache_on) {
  666.                 $cacheArray = array();
  667.             }
  668.             foreach($result as $show) {
  669.                 $message = geoString::fromdb($show['text']);
  670.                 $message = geoTemplate::parseexternaltags($message);
  671.                 $this->messages[$show['text_id']] = $message;
  672.                 if ($cache_on) {
  673.                     $cacheArray[$show['page_id']][$show['text_id']] = $message;
  674.                 }
  675.             }
  676.             if ($cache_on && isset($page_id) && $page_id && isset($cacheArray[$page_id])) {
  677.                 trigger_error("DEBUG STATS: Updating: \$language_id: " . $language_id . " page_id = " . $page_id);
  678.                 $textCache->update($language_id, $page_id, $cacheArray[$page_id]);
  679.             } else if ($cache_on) {
  680.                 foreach($cacheArray as $page_id => $page_text) {
  681.                     trigger_error("DEBUG STATS: Updating: \$language_id: " . $language_id . " page_id = " . $page_id . " \$page_text = " . htmlspecialchars($page_text));
  682.                     $textCache->update($language_id, $page_id, $page_text);
  683.                 }
  684.             }
  685.         }
  686.         if ($return_text) {
  687.             return $this->messages;
  688.         }
  689.         return true;
  690.     }
  691.     public function getLanguage($from_db = false) {
  692.         if (isset($this->language_id)) {
  693.             return $this->language_id;
  694.         }
  695.         if ($from_db) {
  696.             $sql = "SELECT language_id FROM geodesic_pages_languages where default_language = 1";
  697.             $result = $this->Execute($sql);
  698.             if ($result && $result->RecordCount() == 1) {
  699.                 $row = $result->FetchRow();
  700.                 $this->language_id = $row['language_id'];
  701.                 return $this->language_id;
  702.             }
  703.             return 1;
  704.         }
  705.         $session = geoSession::getinstance();
  706.         return $session->getLanguage();
  707.     }
  708.     public function get_sql_in_statement($category_id) {
  709.         if ($category_id == 0) {
  710.             return false;
  711.         }
  712.         if (!isset($this->category_in_stmt[$category_id])) {
  713.             $sql = "SELECT in_statement FROM " . geoTables::categories_table . " WHERE category_id = ?";
  714.             $result = $this->Execute($sql, array($category_id));
  715.             if ($result === false) {
  716.                 return false;
  717.             }
  718.             if ($result->RecordCount() == 1) {
  719.                 $show_in_statement = $result->FetchRow();
  720.                 $current_in_statement = $show_in_statement['in_statement'];
  721.                 if (0 < strlen(trim($current_in_statement))) {
  722.                     $this->category_in_stmt[$category_id] = $current_in_statement;
  723.                 } else {
  724.                     $this->get_sql_in_array($category_id);
  725.                     if (0 < count($this->subcategory_array)) {
  726.                         $this->category_in_stmt[$category_id] = "in (" . implode(", ", $this->subcategory_array) . ")";
  727.                         $sql = "update " . geoTables::categories_table . " set in_statement = ? WHERE category_id = ?";
  728.                         $result = $this->Execute($sql, array($this->category_in_stmt[$category_id], $category_id));
  729.                     } else {
  730.                         return false;
  731.                     }
  732.                 }
  733.             }
  734.         }
  735.         return $this->category_in_stmt[$category_id];
  736.     }
  737.     public function get_sql_in_array($category_id) {
  738.         $count = 0;
  739.         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);
  740.         $sql = "select category_id from " . geoTables::categories_table . " where parent_id = ?";
  741.         $result = $this->Execute($sql, array($category_id));
  742.         if ($result === false) {
  743.             return false;
  744.         }
  745.         while (0 < $result->RecordCount() && ($show_category = $result->FetchRow())) {
  746.             $this->get_sql_in_array($show_category['category_id']);
  747.         }
  748.         array_push($this->subcategory_array, $category_id);
  749.         return true;
  750.     }
  751.     public function num_new_ads_in_category($category_id, $ends_limit, $placed_limit) {
  752.         if (!isset($this->preloaded_num_new_ads)) {
  753.             $this->preload_num_new_ads($ends_limit, $placed_limit);
  754.         }
  755.         if (!isset($this->num_new_ads[$category_id]) && !isset($this->preloaded_num_new_ads)) {
  756.             $in_statement = $this->get_sql_in_statement($category_id);
  757.             $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 ?";
  758.             $new_ad_result = $this->Execute($sql, array(1, $ends_limit, $placed_limit, $in_statement));
  759.             if ($new_ad_result === false) {
  760.                 return false;
  761.             }
  762.             if ($new_ad_result->RecordCount() == 1) {
  763.                 $show_count = $new_ad_result->FetchRow();
  764.                 $this->num_new_ads[$category_id] = $show_count['count'];
  765.             }
  766.         } else if (isset($this->preloaded_num_new_ads) && !isset($this->num_new_ads[$category_id])) {
  767.             $this->num_new_ads[$category_id] = 0;
  768.         }
  769.         return $this->num_new_ads[$category_id];
  770.     }
  771.     public function preload_num_new_ads($ends_limit, $placed_limit) {
  772.         if (!isset($this->preloaded_num_new_ads)) {
  773.             $this->preloaded_num_new_ads = true;
  774.             $filter_stmt = $tables = "";
  775.             $filter_data = array();
  776.             if ($this->isBrowsingWhereClause()) {
  777.                 $filters = $this->getBrowsingWhereClause();
  778.                 $filter_stmt = " AND " . $filters['sql'];
  779.                 $filter_data = $filters['data'];
  780.                 foreach($filters['tables'] as $t) {
  781.                     $tables.= ", " . $t;
  782.                 }
  783.             }
  784.             $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}";
  785.             trigger_error("DEBUG STATS: Starting preload_num_new_ads.");
  786.             $query_data = array(1, $ends_limit, $placed_limit);
  787.             foreach($filter_data as $d) {
  788.                 $query_data[] = $d;
  789.             }
  790.             $result = $this->Execute($sql, $query_data);
  791.             if ($result === false) {
  792.                 $this->preloaded_num_new_ads = false;
  793.                 return false;
  794.             }
  795.             $sql = "SELECT `parent_id` FROM " . geoTables::categories_table . " WHERE `category_id` = ?";
  796.             $stmt = $this->db->Prepare($sql);
  797.             while ($row = $result->FetchRow()) {
  798.                 if (isset($this->num_new_ads[$row['category']]) && 0 < $this->num_new_ads[$row['category']]) {
  799.                     continue;
  800.                 }
  801.                 $parent_result = $this->Execute($stmt, array($row['category']));
  802.                 if (!$parent_result) {
  803.                     trigger_error("ERROR SQL: Sql: " . $sql . " Error: " . $this->ErrorMsg());
  804.                     return false;
  805.                 }
  806.                 $first_row = $parent_result->FetchRow();
  807.                 $parent_id = $first_row['parent_id'];
  808.                 $this->num_new_ads[$row['category']] = 1;
  809.                 while (0 < $parent_id) {
  810.                     if (isset($this->num_new_ads[$parent_id]) && 0 < $this->num_new_ads[$parent_id]) {
  811.                         break;
  812.                     }
  813.                     $this->num_new_ads[$parent_id] = 1;
  814.                     $parent_result = $this->Execute($stmt, array($parent_id));
  815.                     if (!$parent_result) {
  816.                         trigger_error("ERROR SQL: Sql: " . $sql . " Error: " . $this->ErrorMsg());
  817.                         return false;
  818.                     }
  819.                     $this_row = $parent_result->FetchRow();
  820.                     $parent_id = $this_row['parent_id'];
  821.                 }
  822.             }
  823.             trigger_error("DEBUG STATS STATS: Finished preload_num_new_ads.");
  824.         }
  825.     }
  826.     private static function _modFiles() {
  827.         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");
  828.     }
  829.     public function replaceTag($tag, $page, $depth) {
  830.         if (25 < $depth) {
  831.             return "";
  832.         }
  833.         $addon = geoAddon::getinstance();
  834.         trigger_error("DEBUG STATS: DataAccess::replaceTag(" . $tag . ") - top");
  835.         if (!geoPC::is_ent()) {
  836.             $arr = self::_modfiles();
  837.         }
  838.         $db = $this;
  839.         $view = geoView::getinstance();
  840.         $language_id = $this->getLanguage();
  841.         $cat_id = $page->site_category;
  842.         $logged_in = geoSession::getinstance()->getUserID();
  843.         $pageCache = geoCachePage::getinstance();
  844.         $settingsCache = geoCacheSetting::getinstance();
  845.         $filter_query_data = null;
  846.         $filter_sql = $filter_tables = "";
  847.         if ($this->isBrowsingWhereClause()) {
  848.             $filters = $this->getBrowsingWhereClause();
  849.             $filter_sql.= " AND " . $filters['sql'];
  850.             $filter_query_data = $filters['data'];
  851.             foreach($filters['tables'] as $t) {
  852.                 $filter_tables.= ", " . $t;
  853.             }
  854.         }
  855.         if ($this->allowedTags === false) {
  856.             $allowedTags = $settingsCache->process("module_tag_list");
  857.             if ($allowedTags === false) {
  858.                 $sql = "SELECT `module_replace_tag` FROM `geodesic_pages` WHERE `module` = 1";
  859.                 $tagResult = $this->Execute($sql);
  860.                 $allowedTags = array();
  861.                 if (!$tagResult) {
  862.                     trigger_error("ERROR SQL: sql:" . $sql . " Error:" . $this->ErrorMsg());
  863.                     return "";
  864.                 }
  865.                 while ($row = $tagResult->FetchRow()) {
  866.                     if (!in_array($row['module_replace_tag'], $allowedTags) && 0 < strlen($row['module_replace_tag'])) {
  867.                         $allowedTags[] = $row['module_replace_tag'];
  868.                     }
  869.                 }
  870.                 $settingsCache->update("module_tag_list", $allowedTags);
  871.             }
  872.             $this->allowedTags = $allowedTags;
  873.         }
  874.         if (!in_array($tag, $this->allowedTags)) {
  875.             trigger_error("DEBUG STATS: DataAccess::replaceTag(" . $tag . ") - not allowed tag");
  876.             return "";
  877.         }
  878.         $cacheResult = $pageCache->process($tag, $language_id, $cat_id, $logged_in, true);
  879.         if ($cacheResult === false) {
  880.             $remember_body = $page->body;
  881.             $page->body = "";
  882.             if (geoCache::get("cache_module")) {
  883.                 $moduleCache = geoCacheModule::getinstance();
  884.                 $show_module = $moduleCache->process($tag);
  885.             } else {
  886.                 $show_module = false;
  887.             }
  888.             if (!$show_module) {
  889.                 $sql = "SELECT * FROM `geodesic_pages` WHERE `module_replace_tag` = ?";
  890.                 $result = $this->Execute($sql, array($tag));
  891.                 if (!$result) {
  892.                     trigger_error("ERROR SQL: sql:" . $sql . " Error: " . $this->ErrorMsg());
  893.                     return false;
  894.                 }
  895.                 $show_module = $result->FetchRow();
  896.                 if (geoCache::get("cache_module")) {
  897.                     $moduleCache->update($tag, $show_module);
  898.                 }
  899.             }
  900.             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))) {
  901.                 $page->messages = $this->get_text(true, $show_module['page_id']);
  902.                 $page->language_id = $language_id;
  903.                 require (MODULES_DIR . $show_module['module_file_name']);
  904.                 $geo_inc_files = $view->geo_inc_files;
  905.                 $cacheResult = $page->body;
  906.                 unset($geo_inc_files);
  907.                 unset($tpl);
  908.                 unset($vars);
  909.             }
  910.             $page->body = $remember_body;
  911.         }
  912.         return $cacheResult;
  913.     }
  914.     public function sendMail($to, $subject, $content, $from = 0, $replyTo = 0, $charset = 0, $type = 0) {
  915.         geoEmail::sendmail($to, $subject, $content, $from, $replyTo, $charset, $type);
  916.         return true;
  917.     }
  918.     public function getDbInfo() {
  919.         $this->init();
  920.         return $this->db_info;
  921.     }
  922.     public function microtime_float() {
  923.         list($usec, $sec) = explode(" ", microtime());
  924.         $sec = (float)($sec + $usec);
  925.         return ((float)($sec * 1000000));
  926.     }
  927.     public function checkBannedIp() {
  928.         if (defined("IN_ADMIN") || isset($this->ipIsBanned) && $this->ipIsChecked) {
  929.             return true;
  930.         }
  931.         $ip_to_check = $_SERVER['REMOTE_ADDR'];
  932.         $sql = "SELECT `ip` FROM " . $this->geoTables->ip_ban_table;
  933.         $this->init();
  934.         $ip_result = $this->db->Execute($sql);
  935.         if (!$ip_result) {
  936.             trigger_error("ERROR SQL: SQL query failed for retrieving banned ips.  Query:" . $sql . " Error: " . $this->ErrorMsg());
  937.             return false;
  938.         }
  939.         if (0 < $ip_result->RecordCount()) {
  940.             $ban_me = false;
  941.             while (!$ban_me && ($ip_banned = $ip_result->FetchRow())) {
  942.                 $ip_banned = str_replace(".", "\\.", $ip_banned['ip']);
  943.                 $ip_banned = "/^" . str_replace("*", "[0-9.]*", $ip_banned) . "\$/";
  944.                 if (preg_match($ip_banned, $ip_to_check) == 1) {
  945.                     $ban_me = true;
  946.                 }
  947.             }
  948.             if (isset($_GET['check_ban_ip']) && $_GET['check_ban_ip']) {
  949.                 exit($ip_to_check . " - IP BANNED? " . ($ban_me ? "YES, this IP will only see front page of site." : "NO, this ip not banned."));
  950.             }
  951.             if ($ban_me) {
  952.                 $_REQUEST = array();
  953.                 $_POST = array();
  954.                 $_GET = array();
  955.                 $_COOKIE = array();
  956.                 if (!defined("IS_ROBOT")) {
  957.                     define("IS_ROBOT", 1);
  958.                 }
  959.             }
  960.         }
  961.         $this->ipIsChecked = true;
  962.         return true;
  963.     }
  964.     public function addBrowsingWhereClause($name, $sql, $data = null, $additionalTable = null) {
  965.         if (strlen(trim($name)) == 0) {
  966.             return false;
  967.         }
  968.         if (strlen(trim($sql)) == 0) {
  969.             unset($this->_sqlWhereClauses[$name]);
  970.         } else {
  971.             if ($data !== null && !is_array($data)) {
  972.                 $data = array($data);
  973.             }
  974.             $this->_sqlWhereClauses[$name] = array("sql" => trim($sql), "data" => $data, "tables" => $additionalTable);
  975.         }
  976.     }
  977.     public function getBrowsingWhereClause() {
  978.         if (!is_array($this->_sqlWhereClauses) || count($this->_sqlWhereClauses) == 0) {
  979.             if (!is_array($this->_sqlWhereClauses)) {
  980.                 trigger_error("ERROR SQL: \$this->_sqlWhereClauses is not an array, how did this happen???");
  981.             }
  982.             return "";
  983.         }
  984.         $data = $sql_parts = $tables = array();
  985.         foreach($this->_sqlWhereClauses as $name => $info) {
  986.             if ($info['data'] !== null) {
  987.                 foreach($info['data'] as $d) {
  988.                     $data[] = $d;
  989.                 }
  990.             }
  991.             if ($info['tables'] !== null && !in_array(trim($info['tables']), $tables)) {
  992.                 $tables[] = $info['tables'];
  993.             }
  994.             $sql_parts[] = $info['sql'];
  995.         }
  996.         return array("sql" => implode(" AND ", $sql_parts), "data" => $data, "tables" => $tables);
  997.     }
  998.     public function isBrowsingWhereClause() {
  999.         return 0 < count($this->_sqlWhereClauses);
  1000.     }
  1001.     public function tableExists($tableName) {
  1002.         $this->init();
  1003.         $result = $this->db->Execute("show tables");
  1004.         while ($row = $result->FetchRow()) {
  1005.             if (!in_array($tableName, $row)) {
  1006.                 continue;
  1007.             }
  1008.             return true;
  1009.         }
  1010.         return false;
  1011.     }
  1012. }
  1013. require_once (CLASSES_DIR . PHP5_DIR . "config.database_tables.php");
  1014. ?>
Add Comment
Please, Sign In to add comment