/** * checks if there is a cookie we can use for log in. If cookie exists * we will log in the user * * You can run trigger events which needs to be set in session_events * in config/config.ini * * @return void */ public static function checkSystemCookie() { if (isset($_COOKIE['system_cookie'])) { // user is in session. Can only be this after first request. if (isset($_SESSION['in_session'])) { return; } if (isset($_SESSION['id'])) { // user is logged in we return return; } // get a system cookie if any $row = q::select('system_cookie')->filter('cookie_id =', @$_COOKIE['system_cookie'])->fetchSingle(); // we got a cookie that equals one found in database if (!empty($row)) { $days = self::getCookiePersistentDays(); // delete system_cookies that are out of date. $now = date::getDateNow(); $last = date::substractDaysFromTimestamp($now, $days); q::delete('system_cookie')->filter('account_id =', $row['account_id'])->condition('AND')->filter('last_login <', $last)->exec(); // on every cookie login we update the cookie id $last_login = date::getDateNow(array('hms' => true)); $new_cookie_id = random::md5(); $values = array('account_id' => $row['account_id'], 'cookie_id' => $new_cookie_id, 'last_login' => $last_login); q::delete('system_cookie')->filter('cookie_id=', @$_COOKIE['system_cookie'])->exec(); q::insert('system_cookie')->values($values)->exec(); //filter('cookie_id =' , $new_cookie_id)->condition('AND')-> //filter('last_login ='******'system_cookie', $new_cookie_id); // get account which is connected to account id $account = self::getAccount($row['account_id']); // user with account if (!empty($account)) { $_SESSION['id'] = $account['id']; $_SESSION['admin'] = $account['admin']; $_SESSION['super'] = $account['super']; $_SESSION['type'] = $account['type']; } else { // keep anon user in session $_SESSION['id'] = 0; $_SESSION['type'] = 'anon'; } } } }
/** * sets a string in cache * @param string $module * @param int $id * @param string $data * @return strin $str */ private static function setDb($module, $id, $data) { q::begin(); self::delete($module, $id); $id = self::generateId($module, $id); $values = array('id' => $id, 'unix_ts' => time()); $values['data'] = serialize($data); q::insert(self::$table)->values($values)->exec(); return q::commit(); }
/** * Sets a string in cache. Notice that the database uses transactions. * @param string $module * @param int $id * @param string $data * @return strin $str */ private static function setDb($module, $id, $data) { q::begin(); self::delete($module, $id); $md5 = self::generateId($module, $id); $values = array('id' => $md5, 'unix_ts' => time()); $values['data'] = serialize($data); $values['name'] = $module; $values['index_id'] = $id; $res = q::insert(self::$table)->values($values)->exec(); if (!$res) { q::rollback(); } return q::commit(); }
/** * checks if there is a cookie we can use for log in. If cookie exists * we will log in the user * * You can run trigger events which needs to be set in session_events * in config/config.ini * * @return void */ public static function checkSystemCookie() { if (isset($_COOKIE['system_cookie'])) { // Check against cookie from DB // User may have logged out of all devices $row = self::getSystemCookieDb(); if (empty($row)) { return; } // we got a cookie that equals one found in database $days = self::getCookiePersistentDays(); // delete system_cookies that are out of date. $now = date::getDateNow(); $last = date::substractDaysFromTimestamp($now, $days); q::delete('system_cookie')->filter('account_id =', $row['account_id'])->condition('AND')->filter('last_login <', $last)->exec(); // on every cookie login we update the cookie id $last_login = date::getDateNow(array('hms' => true)); $new_cookie_id = random::md5(); $values = array('account_id' => $row['account_id'], 'cookie_id' => $new_cookie_id, 'last_login' => $last_login); q::delete('system_cookie')->filter('cookie_id=', $_COOKIE['system_cookie'])->exec(); q::insert('system_cookie')->values($values)->exec(); // set the new cookie self::setCookie('system_cookie', $new_cookie_id); // get account which is connected to account id $account = self::getAccount($row['account_id']); // user with account if (!empty($account)) { $_SESSION['id'] = $account['id']; $_SESSION['admin'] = $account['admin']; $_SESSION['super'] = $account['super']; $_SESSION['type'] = $account['type']; } } }