File indexing completed on 2024-05-12 17:26:17

0001 <?php
0002 
0003 /*
0004  *   GFX 4
0005  * 
0006  *   support: happy.snizzo@gmail.com
0007  *   website: http://trt-gfx.googlecode.com
0008  *   credits: Claudio Desideri
0009  *   
0010  *   This software is released under the MIT License.
0011  *   http://opensource.org/licenses/mit-license.php
0012  */ 
0013 
0014 class OCSUser{
0015   
0016   //ocs mandatory user attributes
0017   private static  $login;
0018   private static  $password;
0019   private static  $firstname;
0020   private static  $lastname;
0021   private static  $email;
0022   
0023   private static  $logged = false; //some kind of log
0024   private static  $id;
0025   
0026   private static  $persons;
0027   
0028   public static function server_load(){
0029     //storing root object
0030     OCSUser::$persons = new EModel("ocs_person");
0031   }
0032   
0033   
0034   public static function version()
0035   {
0036     return '1.6';
0037   }
0038   /*
0039    * getters/setters
0040    */
0041   public static  function id(){
0042     return OCSUser::$id;
0043   }
0044   
0045   public static function login(){
0046     return OCSUser::$login;
0047   }
0048   
0049   public static  function is_logged(){
0050     return OCSUser::$logged;
0051   }
0052 
0053   public static function client_login() {
0054 
0055     $login = EHeaderDataParser::secure_post("login");
0056     $password = EHeaderDataParser::secure_post("password");
0057     OCSUser::$login= $login;
0058 
0059     if($login==false && $password==false) {
0060       $login = EHeaderDataParser::get_cookie("login");
0061       $password = EHeaderDataParser::get_cookie("password");
0062       OCSUser::$login= $login;
0063     }
0064     $postdata = array(
0065       "login" => $login,
0066       "password" => $password
0067     );
0068     $client = new OCSClient(EConfig::$data["ocs"]["host"]);
0069     $check = $client->post("v1/person/check",$postdata);
0070 
0071     if($check["ocs"]["meta"]["statuscode"]=="100"){
0072       OCSUser::$logged=true;
0073       EHeaderDataParser::set_cookie("login", $login);
0074       EHeaderDataParser::set_cookie("password", $password);
0075     }
0076 
0077   }
0078 
0079   public static function client_logout() {
0080     $login = EHeaderDataParser::get_cookie("login");
0081     $password = EHeaderDataParser::get_cookie("password");
0082     EHeaderDataParser::del_cookie("login");
0083     EHeaderDataParser::del_cookie("password");
0084   }
0085 
0086   public static function get_login(){
0087     return OCSUser::$login;
0088   }
0089   
0090   
0091   /*
0092    * attempt an authentication trough nickname:password
0093    * and populates object data if successfull
0094    */
0095   public static  function server_checklogin($login,$passwd){
0096     //autoload if necessary
0097     if(is_null(OCSUser::$persons)){ OCSUser::server_load(); }
0098     //checklogin
0099     $r = OCSUser::$persons->count("login", "login='$login' and password='$passwd'");
0100     if($r==0){
0101       OCSUser::$logged = false;
0102       return false;
0103     } else {
0104       OCSUser::$logged = true;
0105       
0106       $data = OCSUser::$persons->find("*","where login='$login' and password='$passwd'");
0107       OCSUser::$id = $data[0]["id"];
0108       OCSUser::$login = $data[0]["login"];
0109       OCSUser::$firstname = $data[0]["firstname"];
0110       OCSUser::$lastname = $data[0]["lastname"];
0111       OCSUser::$email = $data[0]["email"];
0112       
0113       return $login;
0114     }
0115   }
0116   
0117   /*
0118    * Some utils functions regarding users
0119    */
0120   
0121   public static  function server_exists($user){
0122     //autoload if necessary
0123     if(is_null(OCSUser::$persons)){ OCSUser::server_load(); }
0124     
0125     $user = EDatabase::safe($user);
0126     $r = OCSUser::$persons->is_there("login","login='$user'");
0127     return $r;
0128   }
0129   
0130   public static  function server_get_user_info($username=""){
0131     //autoload if necessary
0132     if(is_null(OCSUser::$persons)){ OCSUser::server_load(); }
0133     
0134     if($username==OCSUser::$login){
0135       $user_info["id"] = OCSUser::$id;
0136       $user_info["login"] = OCSUser::$login;
0137       $user_info["firstname"] = OCSUser::$firstname;
0138       $user_info["lastname"] = OCSUser::$lastname;
0139       $user_info["email"] = OCSUser::$email;
0140       
0141       return $user_info;
0142     } else {
0143       $user_info = OCSUser::$persons->find("*","where login='$username' limit 1");
0144       return $user_info;
0145     }
0146   }
0147   
0148   public static  function server_register($login,$passwd,$firstname,$lastname,$email){
0149     //autoload if necessary
0150     if(is_null(OCSUser::$persons)){ OCSUser::server_load(); }
0151     
0152     $login = EDatabase::safe($login);
0153     $passwd = EDatabase::safe($passwd);
0154     $firstname = EDatabase::safe($firstname);
0155     $lastname = EDatabase::safe($lastname);
0156     $email = EDatabase::safe($email);
0157     
0158     EDatabase::q("INSERT INTO ocs_person (login,password,firstname,lastname,email) VALUES ('$login','$passwd','$firstname','$lastname','$email')");
0159   }
0160   
0161   /*
0162    * TODO: utils function which are semantically in the wrong place. Inspect and fix.
0163    */
0164   //TODO: ask for more infos about password validation
0165   public static  function isvalidpassword($pass){
0166     if(strlen($pass)>=8){
0167       return true;
0168     } else {
0169       return false;
0170     }
0171   }
0172   
0173   public static  function isloginname($login){
0174     if(preg_match("([A-Za-z0-9]*)",$login)){
0175       return true;
0176     } else {
0177       return false;
0178     }
0179   }
0180   
0181   public static  function server_countusersbyemail($email){
0182     //autoload if necessary
0183     if(is_null(OCSUser::$persons)){ OCSUser::server_load(); }
0184     
0185     $email = EDatabase::safe($email);
0186     $r = OCSUser::$persons->count("login", "email='$email'");
0187     return $r;
0188   }
0189   
0190   /*
0191    * Obscure magic string told me by the elders.
0192    * If modified, I don't guarantee you'll be safe anymore.
0193    */
0194   public static  function isvalidemail($email){
0195     if(preg_match("/^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i", $email)){
0196       return true;
0197     } else {
0198       return false;
0199     }
0200   }
0201 }
0202 
0203 ?>