예제 #1
0
파일: Id.php 프로젝트: pdyn/datatype
 /**
  * Constructor.
  *
  * @param int $val An ID value.
  */
 public function __construct($val)
 {
     if (\pdyn\datatype\Id::validate($val) !== true) {
         throw new \Exception('Bad ID Received', 406);
     }
     $this->val = (int) $val;
 }
예제 #2
0
파일: UserSession.php 프로젝트: pdyn/user
 /**
  * Get the user object of the user that is logged in with this session.
  *
  * @return \pdyn\user\User The user object that is logged in.
  */
 public function get_user()
 {
     $this->set_userid(UserInterface::GUESTID);
     if (!empty($_SESSION['user']) && \pdyn\datatype\Id::validate($_SESSION['user'], false) === true) {
         // Active session.
         $this->set_userid($_SESSION['user']);
     } else {
         // Check persistant login cookie.
         $pcookienane = $this->get_persistent_login_cookie_name();
         if (!empty($_COOKIE[$pcookienane])) {
             $sess_id = $_COOKIE[$pcookienane];
             $conditions = ['sess_id' => $sess_id, 'sess_invalid' => 0, 'sess_persistent' => 1];
             $session = $this->DB->get_record('online_users', $conditions);
             if (!empty($session) && !empty($session['user_id']) && \pdyn\datatype\Id::validate($session['user_id'], false) === true) {
                 $this->login($session['user_id'], false);
             }
         }
     }
     return $this->constructuser($this->userid);
 }
예제 #3
0
파일: User.php 프로젝트: pdyn/user
 /**
  * Populate the class.
  *
  * Validates the set ID, if invalid, switches the class to the guest user, and reloads the object.
  *
  * @param array $info Array of information to populate into the object.
  * @return bool Success/Failure.
  */
 protected function populate(array $info = array())
 {
     if (!empty($this->id) && \pdyn\datatype\Id::validate($this->id, true) !== true) {
         $this->id = static::GUESTID;
         $this->reload();
         return false;
     }
     return parent::populate($info);
 }
예제 #4
0
파일: IdTest.php 프로젝트: pdyn/datatype
 /**
  * Test validate id.
  *
  * @dataProvider dataprovider_validate
  */
 public function test_validate($test, $expected)
 {
     $this->assertEquals($expected, \pdyn\datatype\Id::validate($test));
 }
예제 #5
0
 /**
  * Assign values to class properties.
  *
  * @param array $info An array of data to assign.
  * @return bool Success/Failure.
  */
 protected function populate(array $info = array())
 {
     if (empty($info)) {
         $this->post_populate();
         return false;
     }
     // Load datatype information into static var cache.
     $obj_datatypes = static::get_obj_datatypes($this->DB);
     $info = array_intersect_key($info, $obj_datatypes);
     foreach ($info as $key => $val) {
         switch ($obj_datatypes[$key]) {
             case 'email':
                 if (\pdyn\datatype\Validator::email($val)) {
                     $this->{$key} = (string) $val;
                 }
                 break;
             case 'timestamp':
                 if ((int) $val > 0) {
                     $this->{$key} = (int) $val;
                 }
                 break;
             case 'str':
             case 'text':
             case 'longtext':
                 $this->{$key} = (string) $val;
                 break;
             case 'filename':
                 $val = trim($val);
                 if (!empty($val)) {
                     $this->{$key} = \pdyn\filesystem\FilesystemUtils::sanitize_filename($val);
                 }
                 break;
             case 'int':
             case 'bigint':
                 $this->{$key} = (int) $val;
                 break;
             case 'float':
                 $this->{$key} = (double) $val;
                 break;
             case 'id':
                 $this->{$key} = (int) $val;
                 break;
             case 'bool':
                 $this->{$key} = (bool) $val;
                 break;
             case 'user_id':
                 if (\pdyn\datatype\Id::validate($val) === true) {
                     $this->{$key} = (int) $val;
                 }
                 break;
             case 'url':
                 if (\pdyn\datatype\Url::validate($val) === true) {
                     $this->{$key} = (string) $val;
                 }
                 break;
             case 'mime':
                 if (\pdyn\datatype\Validator::mime($val) === true) {
                     $this->{$key} = (string) $val;
                 }
                 break;
             default:
                 if (is_array($obj_datatypes[$key])) {
                     if (in_array($val, $obj_datatypes[$key], true)) {
                         $this->{$key} = $val;
                     }
                 } else {
                     $this->{$key} = $val;
                 }
         }
     }
     $this->post_populate();
     return true;
 }