Inheritance: extends CakeException
 public function __construct($htmlName, $code = 0, \Exception $pex = null)
 {
     parent::__construct($htmlName, $code, $pex);
     if (SecurityConcernException::NOT_UPLOADED === $code) {
         $this->message = 'The file was not uploaded through POST';
     } else {
         if (SecurityConcernException::UNKNOWN_CODE <= $code) {
             $code -= SecurityConcernException::UNKNOWN_CODE;
             $this->message = "The file had an unknown PHP upload error code: {$code}";
         }
     }
 }
 public function __construct($htmlName, $code = 0, \Exception $pex = null)
 {
     parent::__construct($htmlName, $code, $pex);
     switch ($code) {
         case UPLOAD_ERR_NO_TMP_DIR:
             $this->message = 'No temporary folder in which to hold the upload';
             break;
         case UPLOAD_ERR_CANT_WRITE:
             $this->message = 'Failed to write upload to temporary location';
             break;
         case UPLOAD_ERR_EXTENSION:
             $this->message = 'An extension blocked the upload';
             break;
         case ServerProblemException::MOVE_UPLOAD_FAILED:
             $this->message = 'Unable to move upload';
             break;
     }
 }
function validateAndSave($file)
{
    $result = array();
    $path = $file['name'];
    $ext = pathinfo($path, PATHINFO_EXTENSION);
    $file['name'] = $_POST['profile-code'] . '.' . strtolower($ext);
    if ($file['error'] !== UPLOAD_ERR_OK) {
        // file uploading errors: http://php.net/manual/en/features.file-upload.errors.php
        $exception = new UploadException($file['error']);
        $access = date("[Y/m/d H:i:s]");
        if ($file['error'] === UPLOAD_ERR_INI_SIZE || $file['error'] === UPLOAD_ERR_FORM_SIZE) {
            $result['status'] = 'ERR';
            $result['message'] = 'Please choose a smaller file!';
            error_log("{$access} UPLOAD_ERR({$file['error']}): {$exception->getMessage()}");
        } else {
            if ($file['error'] === UPLOAD_ERR_EXTENSION) {
                $result['status'] = 'ERR';
                $result['message'] = 'Invalid file format!';
                error_log("{$access} UPLOAD_ERR({$file['error']}): {$exception->{$message}}");
            } else {
                throw $exception;
            }
        }
    } else {
        if (!preg_match('/^image\\//', $file['type']) || !preg_match('/\\.(jpe?g|gif|png)$/i', $file['name']) || getimagesize($file['tmp_name']) === FALSE) {
            //then there is an error
            $result['status'] = 'ERR';
            $result['message'] = 'Invalid file format!';
        } else {
            if ($file['size'] > 1100000) {
                // 1Mb
                //if size is larger than what we expect
                $result['status'] = 'ERR';
                $result['message'] = 'Please choose a smaller file!';
            } else {
                if ($file['error'] != 0 || !is_uploaded_file($file['tmp_name'])) {
                    //if there is an unknown error or temporary uploaded file is not what we thought it was
                    $result['status'] = 'ERR';
                    $result['message'] = 'Unspecified error!';
                } else {
                    $upload_dir = PROFILE_PICTURE_UPLOAD_DIR;
                    $small_picture_dir = $upload_dir . DIRECTORY_SEPARATOR . PICTURE_SMALL_DIR;
                    $medium_picture_dir = $upload_dir . DIRECTORY_SEPARATOR . PICTURE_MEDIUM_DIR;
                    $large_picture_dir = $upload_dir . DIRECTORY_SEPARATOR . PICTURE_LARGE_DIR;
                    makeDirs($upload_dir);
                    makeDirs($small_picture_dir);
                    makeDirs($medium_picture_dir);
                    makeDirs($large_picture_dir);
                    //save file inside current directory using a safer version of its name
                    $filename = preg_replace('/[^\\w\\.\\- ]/', '', $file['name']);
                    $filename_jpg = preg_replace('/\\.(.+)$/', '', $filename) . '.jpg';
                    $save_path = $upload_dir . DIRECTORY_SEPARATOR . $filename;
                    //thumbnail name is like filename-thumb.jpg
                    $thumb_path = $upload_dir . DIRECTORY_SEPARATOR . preg_replace('/\\.(.+)$/', '', $filename) . '-cropped.jpg';
                    $small_picture_path = $small_picture_dir . DIRECTORY_SEPARATOR . $filename_jpg;
                    $medium_picture_path = $medium_picture_dir . DIRECTORY_SEPARATOR . $filename_jpg;
                    $large_picture_path = $large_picture_dir . DIRECTORY_SEPARATOR . $filename_jpg;
                    if (!deleteFile($small_picture_path . '.deleted') or !deleteFile($medium_picture_path . '.deleted') or !deleteFile($large_picture_path . '.deleted') or !move_uploaded_file($file['tmp_name'], $save_path) or !crop($save_path, $thumb_path, $_POST['x'], $_POST['y'], $_POST['w'], $_POST['h']) or !resize($thumb_path, $small_picture_path, PICTURE_SMALL_SIZE) or !resize($thumb_path, $medium_picture_path, PICTURE_MEDIUM_SIZE) or !resize($thumb_path, $large_picture_path, PICTURE_LARGE_SIZE)) {
                        $result['status'] = 'ERR';
                        $result['message'] = 'Unable to save file!';
                    } else {
                        //everything seems OK
                        $result['status'] = 'OK';
                        $result['message'] = 'Avatar changed successfully!';
                        //include new thumbnails `url` in our result and send to browser
                        $result['url'] = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . '/' . $large_picture_path;
                    }
                }
            }
        }
    }
    return $result;
}
Example #4
0
 public function __construct()
 {
     $_['title'] = kgettext('Uploads.');
     $_['text'] = kgettext('Unknown upload type.');
     $_['image'] = Config::DIR_PATH . '/img/exceptions/default.png';
     $this->message_data = $_;
     parent::__construct($_['text']);
 }