private function gc() { $bin = loadBalance($bins = 50); switch ($bin) { case 1: //delete regular sessions inactive for just a little over an hour $where = array('last_request' => array('<', time() - 40000), 'remember' => 0); break; case 2: //delete remembered/regular sessions inactive for one month $where = array('last_request' => array('<', time() - 2592000)); break; default: if ($bin >= 40) { //timeout inactive not remembered sessions dbTable::get('session')->updateRowsWhere(array('timedout' => 1, 'logged_in' => 0), array('last_request' => array('<', time() - 20000), 'remember' => 0)); } } if (isset($where)) { dbTable::get('session')->deleteRowsWhere($where); } }
private function connect() { //this function connects to both the read(if needed) and write server //when we add read servers we need to build out no connect write if no write is necessary //its simple because all writes go through one if (!isset(self::$write_connection)) { self::$write_connection = false; for ($i = 0; $i < 3; $i++) { $wcon = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); if (mysqli_connect_errno()) { if (defined('DEBUG_VERBOSE')) { if (DEBUG_VERBOSE) { echo "Db write host error:" . mysqli_connect_error() . "<br/>"; } } sleep(1); } else { self::$write_connection = $wcon; break; } } if (!self::$write_connection) { throw new Exception(SITE_SERVER_URL . ' is temporarily unavailable'); } } self::_checkForReadHosts(); if (!isset(self::$read_connection)) { if (isset(self::$read_hosts)) { //flag read host as failed so we dont try it agian self::$read_connection = false; //localize read host var $db_read_hosts = self::$read_hosts; //total number of read hosts $count = count($db_read_hosts); //the index in the readhosts array of the server to try first $start = $i = loadBalance($count); //function defined in modules/api/lib/general.php //loop breaking flag so we dont try to connect forever $break = 0; while (true) { $db_read_host = $db_read_hosts[$i]; $con = @new mysqli($db_read_host, DB_USER, DB_PASS, DB_NAME); //setup to attempt to connect to all hosts twice before giving up if (mysqli_connect_errno()) { $i++; if ($i == $count) { $i = 0; } elseif ($i == $start) { $break++; if ($break == 2) { if (defined('DEBUG_VERBOSE')) { if (DEBUG_VERBOSE) { echo "DB read host error:" . mysqli_connect_error() . "<br/>"; } } break; } else { sleep(1); } } } else { self::$read_host = $db_read_host; self::$read_connection = $con; break; } } if (!self::$read_connection) { //i have no read hosts i want to run all queries on the master self::$read_connection = self::$write_connection; } } else { //i have no read hosts i want to run all queries on the master self::$read_connection = self::$write_connection; } } }
private function save($code) { $session_id = sessionCookie::$session['session_id']; $captcha = DBTable::get('captcha'); $rows = $captcha->loadRowsWhere(array('session_id' => $session_id)); if (!count($rows)) { $new = $captcha->loadNewRow(); $new->session_id = $session_id; } else { $new = $rows[0]; } $new->code = $code; $new->created = time(); $new->save(); $gc = loadBalance(10); if ($gc >= 8) { $halfhour = time() - 60 * 30; $captcha->deleteRowsWhere(array('created' => array('<', $halfhour))); } }