Example #1
0
 function validate($data = null)
 {
     $this->errors = array();
     if (!empty($data)) {
         $data = empty($data[$this->model]) ? $data : $data[$this->model];
         foreach ($data as $dkey => $dval) {
             $this->data->{$dkey} = stripslashes($dval);
         }
         extract($data, EXTR_SKIP);
         if (empty($title)) {
             $this->errors['title'] = __('Please fill in a title', $this->plugin_name);
         }
         if (empty($description)) {
             $this->errors['description'] = __('Please fill in a description', $this->plugin_name);
         }
         if (empty($image_url)) {
             $this->errors['image_url'] = __('Please specify an image', $this->plugin_name);
         } else {
             if ($image = wp_remote_fopen($image_url)) {
                 $filename = basename($image_url);
                 $filepath = ABSPATH . 'wp-content' . DS . 'uploads' . DS . $this->plugin_name . DS;
                 $filefull = $filepath . $filename;
                 if (!file_exists($filefull)) {
                     $fh = @fopen($filefull, "w");
                     @fwrite($fh, $image);
                     @fclose($fh);
                     $name = GalleryHtmlHelper::strip_ext($filename, 'filename');
                     $ext = GalleryHtmlHelper::strip_ext($filename, 'ext');
                     $thumbfull = $filepath . $name . '-thumb.' . $ext;
                     $smallfull = $filepath . $name . '-small.' . $ext;
                     image_resize($filefull, $width = null, $height = 75, $crop = false, $append = 'thumb', $dest = null, $quality = 100);
                     image_resize($filefull, $width = 50, $height = 50, $crop = true, $append = 'small', $dest = null, $quality = 100);
                     @chmod($filefull, 0777);
                     @chmod($thumbfull, 0777);
                     @chmod($smallfull, 0777);
                 }
             }
         }
     } else {
         $this->errors[] = __('No data was posted', $this->plugin_name);
     }
     return $this->errors;
 }
 function validate($data = null)
 {
     $this->errors = array();
     if (!empty($data)) {
         $data = empty($data[$this->model]) ? $data : $data[$this->model];
         $data = stripslashes_deep($data);
         extract($data, EXTR_SKIP);
         if (empty($title)) {
             $this->errors['title'] = __('Please fill in a title', $this->plugin_name);
         }
         if (empty($showinfo)) {
             $this->data->showinfo = "both";
         }
         if (empty($type)) {
             $this->errors['type'] = __('Please select an image type', $this->plugin_name);
         } elseif ($type == "media") {
             if (!empty($media_file) && !empty($attachment_id)) {
                 $imagename = basename($media_file);
                 $this->data->image = $imagename;
                 $this->data->image_path = $media_file;
                 $this->data->image_url = $media_file;
             } else {
                 $this->errors['media_file'] = __('Choose an image', $this->plugin_name);
             }
         } elseif ($type == "file") {
             if (!empty($image_oldfile) && empty($_FILES['image_file']['name'])) {
                 $imagename = $image_oldfile;
                 $imagepath = GalleryHtmlHelper::uploads_path() . DS . $this->plugin_name . DS;
                 $imagefull = $imagepath . $imagename;
                 $this->data->image = $imagename;
                 GalleryHtmlHelper::image_path($this->data);
             } else {
                 if ($_FILES['image_file']['error'] <= 0) {
                     $imagename = $_FILES['image_file']['name'];
                     $image_name = GalleryHtmlHelper::strip_ext($imagename, "name");
                     $image_ext = GalleryHtmlHelper::strip_ext($imagename, "ext");
                     $imagename = GalleryHtmlHelper::sanitize($image_name) . '.' . $image_ext;
                     $imagepath = GalleryHtmlHelper::uploads_path() . DS . $this->plugin_name . DS;
                     $imagefull = $imagepath . $imagename;
                     $issafe = false;
                     $mimes = get_allowed_mime_types();
                     foreach ($mimes as $type => $mime) {
                         if (strpos($type, $image_ext) !== false) {
                             $issafe = true;
                         }
                     }
                     if (empty($issafe) || $issafe == false) {
                         $this->errors['image_file'] = __('This file type is not allowed for security reasons', $this->plugin_name);
                     } else {
                         if (!is_uploaded_file($_FILES['image_file']['tmp_name'])) {
                             $this->errors['image_file'] = __('The image did not upload, please try again', $this->plugin_name);
                         } elseif (!move_uploaded_file($_FILES['image_file']['tmp_name'], $imagefull)) {
                             $this->errors['image_file'] = __('Image could not be moved from TMP to "wp-content/uploads/", please check permissions', $this->plugin_name);
                         } else {
                             @chmod($imagefull, 0644);
                             $this->data->image = $imagename;
                             GalleryHtmlHelper::image_path($this->data);
                         }
                     }
                 } else {
                     switch ($_FILES['image_file']['error']) {
                         case UPLOAD_ERR_INI_SIZE:
                         case UPLOAD_ERR_FORM_SIZE:
                             $this->errors['image_file'] = __('The image file is too large', $this->plugin_name);
                             break;
                         case UPLOAD_ERR_PARTIAL:
                             $this->errors['image_file'] = __('The image was partially uploaded, please try again', $this->plugin_name);
                             break;
                         case UPLOAD_ERR_NO_FILE:
                             $this->errors['image_file'] = __('No image was chosen for uploading, please choose an image', $this->plugin_name);
                             break;
                         case UPLOAD_ERR_NO_TMP_DIR:
                             $this->errors['image_file'] = __('No TMP directory has been specified for PHP to use, please ask your hosting provider', $this->plugin_name);
                             break;
                         case UPLOAD_ERR_CANT_WRITE:
                             $this->errors['image_file'] = __('Image cannot be written to disc, please ask your hosting provider', $this->plugin_name);
                             break;
                     }
                 }
             }
         } elseif ($type == "url") {
             if (empty($image_url)) {
                 $this->errors['image_url'] = __('Please specify an image', $this->plugin_name);
             } else {
                 if ($image = wp_remote_fopen(str_replace(" ", "%20", $image_url))) {
                     $filename = basename($image_url);
                     $file_name = GalleryHtmlHelper::strip_ext($filename, "name");
                     $file_ext = GalleryHtmlHelper::strip_ext($filename, "ext");
                     $filename = GalleryHtmlHelper::sanitize($file_name) . '.' . $file_ext;
                     $filepath = GalleryHtmlHelper::uploads_path() . DS . $this->plugin_name . DS;
                     $filefull = $filepath . $filename;
                     if (!file_exists($filefull)) {
                         $fh = @fopen($filefull, "w");
                         @fwrite($fh, $image);
                         @fclose($fh);
                     }
                 }
             }
         }
     } else {
         $this->errors[] = __('No data was posted', $this->plugin_name);
     }
     return $this->errors;
 }
Example #3
0
 function validate($data = null)
 {
     $this->errors = array();
     if (!empty($data)) {
         $data = empty($data[$this->model]) ? $data : $data[$this->model];
         foreach ($data as $dkey => $dval) {
             $this->data->{$dkey} = stripslashes($dval);
         }
         extract($data, EXTR_SKIP);
         if (empty($title)) {
             $this->errors['title'] = __('Please fill in a title', $this->plugin_name);
         }
         if (empty($description)) {
             $this->errors['description'] = __('Please fill in a description', $this->plugin_name);
         }
         if (empty($type)) {
             $this->errors['type'] = __('Please select an image type', $this->plugin_name);
         } elseif ($type == "file") {
             if (!empty($image_oldfile) && empty($_FILES['image_file']['name'])) {
                 $imagename = $image_oldfile;
                 $imagepath = ABSPATH . "wp-content" . DS . "uploads" . DS . $this->plugin_name . DS;
                 $imagefull = $imagepath . $imagename;
                 $this->data->image = $imagename;
             } else {
                 if ($_FILES['image_file']['error'] <= 0) {
                     $imagename = $_FILES['image_file']['name'];
                     $imagepath = ABSPATH . 'wp-content' . DS . 'uploads' . DS . $this->plugin_name . DS;
                     $imagefull = $imagepath . $imagename;
                     if (!is_uploaded_file($_FILES['image_file']['tmp_name'])) {
                         $this->errors['image_file'] = __('The image did not upload, please try again', $this->plugin_name);
                     } elseif (!move_uploaded_file($_FILES['image_file']['tmp_name'], $imagefull)) {
                         $this->errors['image_file'] = __('Image could not be moved from TMP to "wp-content/uploads/", please check permissions', $this->plugin_name);
                     } else {
                         $this->data->image = $imagename;
                         $name = GalleryHtmlHelper::strip_ext($imagename, 'filename');
                         $ext = GalleryHtmlHelper::strip_ext($imagename, 'ext');
                         $thumbfull = $imagepath . $name . '-thumb.' . strtolower($ext);
                         $smallfull = $imagepath . $name . '-small.' . strtolower($ext);
                         image_resize($imagefull, $width = null, $height = 75, $crop = false, $append = 'thumb', $dest = null, $quality = 100);
                         image_resize($imagefull, $width = 50, $height = 50, $crop = true, $append = 'small', $dest = null, $quality = 100);
                         @chmod($imagefull, 0777);
                         @chmod($thumbfull, 0777);
                         @chmod($smallfull, 0777);
                     }
                 } else {
                     switch ($_FILES['image_file']['error']) {
                         case UPLOAD_ERR_INI_SIZE:
                         case UPLOAD_ERR_FORM_SIZE:
                             $this->errors['image_file'] = __('The image file is too large', $this->plugin_name);
                             break;
                         case UPLOAD_ERR_PARTIAL:
                             $this->errors['image_file'] = __('The image was partially uploaded, please try again', $this->plugin_name);
                             break;
                         case UPLOAD_ERR_NO_FILE:
                             $this->errors['image_file'] = __('No image was chosen for uploading, please choose an image', $this->plugin_name);
                             break;
                         case UPLOAD_ERR_NO_TMP_DIR:
                             $this->errors['image_file'] = __('No TMP directory has been specified for PHP to use, please ask your hosting provider', $this->plugin_name);
                             break;
                         case UPLOAD_ERR_CANT_WRITE:
                             $this->errors['image_file'] = __('Image cannot be written to disc, please ask your hosting provider', $this->plugin_name);
                             break;
                     }
                 }
             }
         } elseif ($type == "url") {
             if (empty($image_url)) {
                 $this->errors['image_url'] = __('Please specify an image', $this->plugin_name);
             } else {
                 if ($image = wp_remote_fopen($image_url)) {
                     $filename = basename($image_url);
                     $filepath = ABSPATH . 'wp-content' . DS . 'uploads' . DS . $this->plugin_name . DS;
                     $filefull = $filepath . $filename;
                     if (!file_exists($filefull)) {
                         $fh = @fopen($filefull, "w");
                         @fwrite($fh, $image);
                         @fclose($fh);
                         $name = GalleryHtmlHelper::strip_ext($filename, 'filename');
                         $ext = GalleryHtmlHelper::strip_ext($filename, 'ext');
                         $thumbfull = $filepath . $name . '-thumb.' . strtolower($ext);
                         $smallfull = $filepath . $name . '-small.' . strtolower($ext);
                         image_resize($filefull, $width = null, $height = 75, $crop = false, $append = 'thumb', $dest = null, $quality = 100);
                         image_resize($filefull, $width = 50, $height = 50, $crop = true, $append = 'small', $dest = null, $quality = 100);
                         @chmod($filefull, 0777);
                         @chmod($thumbfull, 0777);
                         @chmod($smallfull, 0777);
                     }
                 }
             }
         }
     } else {
         $this->errors[] = __('No data was posted', $this->plugin_name);
     }
     return $this->errors;
 }