Exemple #1
0
 /**
  * Constructor.
  *
  * @param string $stored_filename The stored name of the file.
  * @param string $orig_filename The original name of the file.
  */
 public function __construct($stored_filename, $orig_filename = '')
 {
     if (\pdyn\datatype\Validator::filename($stored_filename) !== true) {
         throw new \Exception('Invalid filename received', 400);
     }
     if (!empty($orig_filename) && !is_scalar($orig_filename)) {
         throw new \Exception('Uploaded file orig filename must be scalar or empty', 400);
     }
     $this->stored_filename = \pdyn\filesystem\FilesystemUtils::sanitize_filename($stored_filename);
     $this->orig_filename = \pdyn\filesystem\FilesystemUtils::sanitize_filename($orig_filename);
 }
Exemple #2
0
 /**
  * Test validate boolint.
  */
 public function test_boolint()
 {
     $valid = array(0, 1);
     foreach ($valid as $val) {
         $this->assertTrue(\pdyn\datatype\Validator::boolint($val));
     }
     $invalid = array(true, false, null, array(), new \stdClass(), 1.2, '1.0', 'test', -1, '-1', '1', '0');
     foreach ($invalid as $val) {
         $this->assertFalse(\pdyn\datatype\Validator::boolint($val));
     }
 }
Exemple #3
0
 /**
  * Validate datatype.
  *
  * @param mixed $input Value to validate.
  * @return bool Value is valid or not.
  */
 public static function validate($input)
 {
     return \pdyn\datatype\Validator::intlike($input) && (int) $input >= 0;
 }
Exemple #4
0
 /**
  * Set the number of tiems per page.
  *
  * @param int $perpage The number of items per page to set.
  */
 public function set_items_per_page($perpage)
 {
     $this->perpage = !empty($perpage) && \pdyn\datatype\Validator::intlike($perpage) ? (int) $perpage : 20;
 }
Exemple #5
0
 /**
  * Create a thumbnail for the image based on a maximum x and maximum y value.
  *
  * @param int $maxx Maximum x dimension.
  * @param int $maxy Maximum y dimension.
  * @return bool Success/Failure.
  */
 public function thumbnail($maxx, $maxy)
 {
     if ($maxx !== null && \pdyn\datatype\Validator::intlike($maxx) !== true) {
         throw new \Exception('Invalid maxx parameter in \\pdyn\\image\\Image', static::ERR_BAD_INPUT);
     }
     if ($maxy !== null && \pdyn\datatype\Validator::intlike($maxy) !== true) {
         throw new \Exception('Invalid maxy parameter in \\pdyn\\image\\Image', static::ERR_BAD_INPUT);
     }
     // Fix orientation.
     $imageinfo = $this->get_info();
     if ($imageinfo['orientation'] !== 1) {
         $this->transform_for_exif_orientation($imageinfo['orientation']);
     }
     $this->bounded_resize((int) $maxx, (int) $maxy);
     return true;
 }
Exemple #6
0
 /**
  * Insert data into the object's database table.
  *
  * @param array $info Array of object data, with database column names as keys.
  * @return int|false The auto-generated record ID or false if failure.
  */
 protected function db_insert($info)
 {
     if (empty($info['timecreated']) || \pdyn\datatype\Validator::timestamp($info['timecreated']) !== true) {
         $info['timecreated'] = time();
         $this->timecreated = $info['timecreated'];
     }
     if (empty($info['timeupdated']) || \pdyn\datatype\Validator::timestamp($info['timeupdated']) !== true) {
         $info['timeupdated'] = time();
         $this->timeupdated = $info['timeupdated'];
     }
     return parent::db_insert($info);
 }
Exemple #7
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;
 }