function testStrings() { $this->assertEquals(Jalali::DateString($this->Timestamp), "1391-11-15"); $this->assertEquals(Jalali::TimeString($this->Timestamp), "01:39:26"); $this->assertEquals(new Jalali($this->Timestamp), "1391-11-15 01:39:26"); $this->assertEquals(new Jalali(), new Jalali(jf::time())); }
function Insert() { if (jf::$RunMode->IsCLI()) { return false; } $res = jf::SQL("INSERT INTO {$this->TablePrefix()}stats (UserID,SessionID,Timestamp,Page,Query,IP,Host,Protocol,UserAgent) VALUES\n\t\t\t(?,?,?,?,?,?,?,?,?)", jf::CurrentUser() ?: 0, jf::$Session->SessionID(), jf::time(), HttpRequest::URI(), HttpRequest::QueryString(), HttpRequest::IP(), HttpRequest::Host(), HttpRequest::Protocol(), HttpRequest::UserAgent()); return $res; }
function testGetTime() { $profiler = new \jf\Profiler(); $profileTime = (int) $profiler->GetTime(false); $jfTime = jf::time(); $this->assertTrue($profileTime == $jfTime or $profileTime == $jfTime - 1); $profileTime = $profiler->GetTime(false); $profileTime *= 1000000; $time = $profiler->GetTime(true); $this->assertGreaterThan($profileTime - $time, 100); }
function Start() { $this->Username = jf::$XUser->Username(); $Logged = false; if (isset($_COOKIE["jframework_rememberme"])) { $rememberMeToken = $_COOKIE["jframework_rememberme"]; $userID = jf::LoadGeneralSetting("rememberme_" . $rememberMeToken); if ($userID > 0) { $Result = jf::$XUser->ForceLogin($userID); $Logged = true; } } if (isset($_POST["Username"])) { $Username = $_POST['Username']; $Password = $_POST['Password']; $loginResult = jf::$XUser->Login($Username, $Password); if ($loginResult == false) { $UserID = jf::$XUser->UserID($Username); $res = jf::$XUser->LastError; if ($res == \jf\ExtendedUserErrors::Inactive) { $ErrorString = "Your account is not activated."; } elseif ($res == \jf\ExtendedUserErrors::InvalidCredentials or $res == \jf\ExtendedUserErrors::NotFound) { $ErrorString = "Invalid Credentials."; } elseif ($res == \jf\ExtendedUserErrors::Locked) { $ErrorString = "Your account is locked. Try again in " . floor(jf::$XUser->LockTime($Username) / 60) . " minute(s)."; } elseif ($res == \jf\ExtendedUserErrors::PasswordExpired) { $Link = "./reset?user={$UserID}"; $ErrorString = "Your password is expired. You should <a href='{$Link}'>change your password</a>."; } elseif ($res == \jf\ExtendedUserErrors::TemporaryValidPassword) { $Link = "./reset?user={$UserID}&temp={$Password}"; $ErrorString = "This is a temporary password. You should <a href='{$Link}'>reset your password</a> now."; } $Logged = false; $this->Error = $ErrorString; } else { $Logged = true; if (isset($_POST['Remember'])) { $timeout = 60 * 60 * 24 * 30; $rememberMeToken = jf::$Security->RandomToken(); jf::SaveGeneralSetting("rememberme_" . $rememberMeToken, jf::CurrentUser(), $timeout); setcookie('jframework_rememberme', $rememberMeToken, jf::time() + $timeout); } } } if ($Logged == true) { if (isset($_GET['return'])) { $this->Redirect($_GET['return']); } $this->Success = true; } return $this->Present(); }
/** * Delete expired settings with a probability * @param boolean $force run the sweep 100% */ function _Sweep($force = false) { if (!$force) { if (rand(0, 1000) / 1000.0 > 0.1) { return; } } //percentage of SweepRatio, don't always do this when called if (!isset($this->PreparedSweepStatement[$this->dbIndex()]) or $this->PreparedSweepStatement[$this->dbIndex()] === null) { $this->PreparedSweepStatement[$this->dbIndex()] = jf::db()->prepare("DELETE FROM {$this->TablePrefix()}options WHERE Expiration<=?"); } $this->PreparedSweepStatement[$this->dbIndex()]->execute(jf::time()); }
/** * Assigns a role to a user * * @param integer|string $Role id or path or title * @param integer $UserID * ID * optional, UserID or the current user would be used (use 0 for * guest) * @return inserted or existing */ function Assign($Role, $UserID = null) { if ($UserID === null) { $UserID = jf::CurrentUser(); } if (is_int($Role)) { $RoleID = $Role; } else { if (substr($Role, 0, 1) == "/") { $RoleID = jf::$RBAC->Roles->PathID($Role); } else { $RoleID = jf::$RBAC->Roles->TitleID($Role); } } $res = jf::SQL("INSERT INTO {$this->TablePrefix()}rbac_userroles\n\t\t(UserID,RoleID,AssignmentDate)\n\t\tVALUES (?,?,?)\n\t\t", $UserID, $RoleID, jf::time()); return $res >= 1; }
function __construct($Timestamp = null) { if ($Timestamp === null) { $Timestamp = jf::time(); } $this->Timestamp = $Timestamp; }
static function Log($Subject, $Content, $Severity = 0) { if (jf::$App) { return jf::SQL("INSERT INTO " . jf::TablePrefix() . "logs (Subject,Data,Severity,UserID,SessionID,Timestamp) \n\t\t" . "VALUES (?,?,?,?,?,?)", $Subject, $Content, $Severity, jf::CurrentUser(), jf::$Session->SessionID(), jf::time()); } }
/** * Assigns a role to a permission (or vice-versa) * * @param integer $Role * @param integer $Permission * @return boolean inserted or existing */ function Assign($Role, $Permission) { return jf::SQL("INSERT INTO {$this->TablePrefix()}rbac_rolepermissions\n\t\t\t(RoleID,PermissionID,AssignmentDate)\n\t\t\tVALUES (?,?,?)", $Role, $Permission, jf::time()) == 1; }
/** * @depends testLoadSession */ function testSaveSessionTimeOut() { $this->assertTrue(jf::SaveSessionSetting("some_name", "some_value", jf\Timeout::DAY)); $this->movetime(jf\Timeout::DAY + 1); jf::$Settings->_Sweep(true); $this->assertNull(jf::LoadSessionSetting("some_name")); $this->assertTrue(jf::SaveSessionSetting("some_name2", "some_value", 1)); $this->movetime(jf\Timeout::YEAR * 10); $this->assertNotNull(jf::LoadSessionSetting("some_name2", 1)); $this->movetime(0); $this->movetime(jf\Timeout::NEVER - jf::time()); $this->assertEquals(jf::time(), 2147483647); jf::$Settings->_Sweep(true); $this->assertNull(jf::LoadSessionSetting("some_name2", 1)); }
/** * Destroys current session, removing all session variables and parameters */ function DestroySession() { jf::SQL("DELETE FROM {$this->TablePrefix()}session WHERE SessionID=?", $this->SessionID()); if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', jf::time() - 42000, '/'); } $this->SetCurrentUser(null); $_SESSION = array(); session_regenerate_id(true); }
/** * Logs a user in only by user ID without needing valid credentials. Intended for system use only. * This is the core login function, it is called everytime a user is trying to log in * @param integer $UserID * @return boolean|null false if user not found, null on multiple login reject */ function ForceLogin($UserID) { /** * 4 possiblilities * Session not logged in, UserID not logged in * Roll and login * Session logged in, UserID not logged in * Roll and change session to UserID * Session not logged in, UserID logged in * Roll and change session to UserID * Session logged in, UserID logged in, * Roll and change session to UserID * */ if (!jf::$Session->IsLoggedIn() && !$this->IsLoggedIn($UserID)) { jf::$Session->RollSession(); $r = jf::SQL("UPDATE {$this->TablePrefix()}session SET UserID=?,LoginDate=?,LastAccess=?,AccessCount=? WHERE SessionID=?", $UserID, jf::time(), jf::time(), 1, jf::$Session->SessionID()); if ($r > 0) { jf::$Session->SetCurrentUser($UserID); } return $r > 0; } else { if (self::$MultipleLoginPolicy == MultipleLogin::Reject) { if ($this->IsLoggedIn($UserID)) { //already logged in return null; } else { jf::$Session->RollSession(); $r = jf::SQL("UPDATE {$this->TablePrefix()}session SET UserID=?,LoginDate=?,LastAccess=?,AccessCount=? WHERE SessionID=?", $UserID, jf::time(), jf::time(), 1, jf::$Session->SessionID()); } } elseif (self::$MultipleLoginPolicy == MultipleLogin::Overwrite) { $this->LogoutAll($UserID); $r = jf::SQL("UPDATE {$this->TablePrefix()}session SET UserID=?,LoginDate=?,LastAccess=?,AccessCount=? WHERE SessionID=?", $UserID, jf::time(), jf::time(), 1, jf::$Session->SessionID()); } elseif (self::$MultipleLoginPolicy == MultipleLogin::Allowed) { jf::$Session->RollSession(); $r = jf::SQL("UPDATE {$this->TablePrefix()}session SET UserID=?,LoginDate=?,LastAccess=?,AccessCount=? WHERE SessionID=?", $UserID, jf::time(), jf::time(), 1, jf::$Session->SessionID()); if ($r == 0) { //same user $r = 1; } } else { throw new \Exception("Unknown multiple login policy."); } if ($r > 0) { jf::$Session->SetCurrentUser($UserID); } return $r > 0; } }
/** * Initiate an extended user by setting initial times * @param integer $UserID * @return array user info */ function InitUser($UserID) { jf::SQL("UPDATE {$this->TablePrefix()}xuser SET CreateTimestamp=?,PasswordChangeTimestamp=?", jf::time(), jf::time() + self::$PasswordLifeTime); return $this->UserInfo($UserID); }