id, $rightid)); if(!$res || $res['count'] == 0){ return $json_result ? \SYSTEM\LOG\JsonResult::fail() : false;} return $json_result ? \SYSTEM\LOG\JsonResult::ok() : true; } /** * Change logged in users Password * * @param string $old_password_sha1 Users old hashed Password * @param string $new_password_sha1 Users new hashed Password * @return json Returns json with status true or false */ public static function change_password($old_password_sha1,$new_password_sha1){ if(!\SYSTEM\SECURITY\security::isLoggedIn()){ throw new \SYSTEM\LOG\ERROR("You need to be logged in to change your Password!");} $row = \SYSTEM\SQL\SYS_SECURITY_LOGIN_USER_SHA1::Q1(array(\SYSTEM\SECURITY\security::getUser()->username, $old_password_sha1)); if(!$row){ throw new \SYSTEM\LOG\ERROR("No such User Password combination.");} return \SYSTEM\SQL\SYS_SECURITY_UPDATE_PW::QI(array($new_password_sha1, $row['id'])) ? \SYSTEM\LOG\JsonResult::ok() : \SYSTEM\LOG\JsonResult::fail(); } /** * Change logged in users Email. * * This will facilitate the @see \SYSTEM\TOKEN\token utility to generate * a token and send it to the logged in users email using php mailinc function. * * This function can only be invoked if the user is logged in and uses the * function on himself. * * This function will fail if the Email of the user is unconfirmed. You can * only change the email of a confirmed account. * * @param string $new_email New Email for the logged in User * @param string $post_script Function to be executed AFTER clicking the EMail Link, BEFORE updating the EMail * @param string $post_script_data Additional Data for the Postscript * @return bool Returns true or false */ public static function change_email($new_email,$post_script=null,$post_script_data=null) { if(!\SYSTEM\SECURITY\security::isLoggedIn()){ throw new \SYSTEM\LOG\ERROR("You need to be logged in to change your EMail!");} //find all userdata $vars = \SYSTEM\SQL\SYS_SECURITY_USER_INFO::Q1(array(\SYSTEM\SECURITY\security::getUser()->username)); if(!$vars || $vars['email_confirmed'] !== 1){ throw new \SYSTEM\LOG\ERROR("Username not found or Email unconfirmed.");} $old_email = $vars['email']; $data = array('user' => $vars['id'],'email' => $new_email); if($post_script){ $data['post_script_data'] = $post_script_data;} //generate pw & token $vars['email'] = $new_email; $vars['token'] = \SYSTEM\TOKEN\token::request('\SYSTEM\TOKEN\token_change_email',$data,$post_script); $vars['base_url'] = \SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_BASEURL); $vars['newline'] = "\r\n"; //mail $to = $old_email; $subject= \SYSTEM\PAGE\replace::replace(\SYSTEM\PAGE\text::get('mail_change_email_subject'), $vars); $message= \SYSTEM\PAGE\replace::replace(\SYSTEM\PAGE\text::get('mail_change_email'), $vars); $header = 'From: '. \SYSTEM\PAGE\text::get('mail_change_email_from')."\r\n" . 'Reply-To: '.\SYSTEM\PAGE\text::get('mail_change_email_replyto'); return \mail($to, $subject, $message, $header) ? \SYSTEM\LOG\JsonResult::ok() : \SYSTEM\LOG\JsonResult::fail(); } /** * Reset given users Password. * * This will facilitate the @see \SYSTEM\TOKEN\token utility to generate * a token and send it to the users email using php mailinc function. * A new password is generated on invoke and sent with the email. * After confirming the token the new password given in the email is valid. * * @param string Username subject to Password reset * @param string $post_script Function to be executed AFTER clicking the EMail Link, BEFORE updating the Password * @param string $post_script_data Additional Data for the Postscript * @return bool Returns true or false */ public static function reset_password($username,$post_script=null,$post_script_data=null) { if(!\SYSTEM\SECURITY\security::isLoggedIn()){ throw new \SYSTEM\LOG\ERROR("You need to be logged in to reset your Password!");} //find all userdata $vars = \SYSTEM\SQL\SYS_SECURITY_USER_INFO::Q1(array($username)); if(!$vars){ throw new \SYSTEM\LOG\ERROR("Username not found.");} //generate pw & token $vars['pw'] = substr(sha1(time().rand(0, 4000)), 1,10); $data = array('user' => $vars['id'],'pw_sha1' => sha1($vars['pw'])); if($post_script){ $data['post_script_data'] = $post_script_data;} $vars['token'] = \SYSTEM\TOKEN\token::request('\SYSTEM\TOKEN\token_reset_password',$data,$post_script); $vars['base_url'] = \SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_BASEURL); $vars['newline'] = "\r\n"; //mail $to = $vars['email']; $subject= \SYSTEM\PAGE\replace::replace(\SYSTEM\PAGE\text::get('mail_reset_password_subject'), $vars); $message= \SYSTEM\PAGE\replace::replace(\SYSTEM\PAGE\text::get('mail_reset_password'), $vars); $header = 'From: '. \SYSTEM\PAGE\text::get('mail_reset_password_from')."\r\n" . 'Reply-To: '.\SYSTEM\PAGE\text::get('mail_reset_password_replyto'); return \mail($to, $subject, $message, $header) ? \SYSTEM\LOG\JsonResult::ok() : \SYSTEM\LOG\JsonResult::fail(); } /** * Request an Confirm-Email for logged in User. * * This will facilitate the @see \SYSTEM\TOKEN\token utility to generate * a token and send it to the users email using php mailinc function. * * This function can only be invoked if the user is logged in and uses the * function on himself. * * @param string $post_script Function to be executed AFTER clicking the EMail Link, BEFORE updating the Confirmation Status * @param string $post_script_data Additional Data for the Postscript * @return bool Returns true or false */ public static function confirm_email($post_script=null,$post_script_data=null) { if(!\SYSTEM\SECURITY\security::isLoggedIn()){ throw new ERROR("You need to be logged in to confirm your EMail!");} return self::confirm_email_admin(\SYSTEM\SECURITY\security::getUser()->username, $post_script, $post_script_data); } /** * Request an Confirm-Email for an User. * * This will facilitate the @see \SYSTEM\TOKEN\token utility to generate * a token and send it to the users email using php mailinc function.. * * @param string Username of the Account * @param string $post_script Function to be executed AFTER clicking the EMail Link, BEFORE updating the Confirmation Status * @param string $post_script_data Additional Data for the Postscript * @return bool Returns true or false */ public static function confirm_email_admin($user, $post_script=null,$post_script_data=null) { //find all userdata $vars = \SYSTEM\SQL\SYS_SECURITY_USER_INFO::Q1(array($user)); if(!$vars || $vars['email_confirmed'] == 1){ throw new \SYSTEM\LOG\ERROR("Username not found or already confirmed.");} $data = array('user' => $vars['id']); if($post_script){ $data['post_script_data'] = $post_script_data;} //generate token $vars['token'] = \SYSTEM\TOKEN\token::request('\SYSTEM\TOKEN\token_confirm_email',$data,$post_script); $vars['base_url'] = \SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_BASEURL); $vars['newline'] = "\r\n"; //mail $to = $vars['email']; $subject= \SYSTEM\PAGE\replace::replace(\SYSTEM\PAGE\text::get('mail_confirm_email_subject'), $vars); $message= \SYSTEM\PAGE\replace::replace(\SYSTEM\PAGE\text::get('mail_confirm_email'), $vars); $header = 'From: '. \SYSTEM\PAGE\text::get('mail_confirm_email_from')."\r\n" . 'Reply-To: '.\SYSTEM\PAGE\text::get('mail_confirm_email_replyto'); return \mail($to, $subject, $message, $header) ? \SYSTEM\LOG\JsonResult::ok() : \SYSTEM\LOG\JsonResult::fail(); } /** * Confirm a token sent using @see \SYSTEM\TOKEN\token utility * (email confirm, email change, password reset) * * @param string $token Token given in eg an email. * @param bool $json_result Return data as JSON or Array * @return bool Returns json with status true or false or a bool */ public static function confirm($token) { return \SYSTEM\TOKEN\token::confirm($token) ? \SYSTEM\TOKEN\token::text_success($token) : \SYSTEM\TOKEN\token::text_fail($token);} /** * Get Userinfo stored in the current Session. * * @return User Returns User object or NULL */ public static function getUser(){ if(!self::isLoggedIn(false)){ return NULL;} return $_SESSION[\SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_BASEURL)]; } /** * End the current Session and logout the User * * @param bool $json_result Return data as JSON or Array * @return mixed Returns Json status true or true */ public static function logout($json_result = false){ self::startSession(); session_destroy(); return $json_result ? \SYSTEM\LOG\JsonResult::ok() : true;} /** * Save a key=>value into the current session(not preserved) * * @param string $key key for the given value * @param mixed $value Value for the given Key to be saved * @return null Returns null. */ public static function save($key,$value){ self::startSession(); $_SESSION['values'][$key] = $value;} /** * Save the value of a key from the current session(not preserved) * * @param string $key key to be queried * @return mixed Returns Value or null. */ public static function load($key){ self::startSession(); if(!isset($_SESSION['values'][$key])){ return NULL;} return $_SESSION['values'][$key];} /** * Check if the current session is a logged in user * * @param bool $json_result Return data as JSON or Array * @return mixed Returns json with status true or false or a bool. */ public static function isLoggedIn($json_result = false){ self::startSession(); return (isset($_SESSION[\SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_BASEURL)]) && $_SESSION[\SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_BASEURL)] instanceof User) ? ($json_result ? \SYSTEM\LOG\JsonResult::ok() : true) : ($json_result ? \SYSTEM\LOG\JsonResult::fail() : false);} /** * State the Session for the current request * * @return null Returns null. */ protected static function startSession(){ if(!isset($_SESSION) && !headers_sent()){ \session_start();} //respect locale from db if not set(right place here?) if( isset($_SESSION[\SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_BASEURL)]) && $_SESSION[\SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_BASEURL)] instanceof User){ $_SESSION['values'][\SYSTEM\locale::SESSION_KEY] = $_SESSION[\SYSTEM\CONFIG\config::get(\SYSTEM\CONFIG\config_ids::SYS_CONFIG_PATH_BASEURL)]->locale;} } }