Advertisement
Guest User

NGG public uploader functions

a guest
Jan 20th, 2012
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.66 KB | None | 0 0
  1. // Get NextGEN Gallery Functions
  2. require_once (NGGALLERY_ABSPATH."/admin/functions.php");
  3.  
  4. class UploaderNggAdmin extends nggAdmin
  5. {
  6.     // Public Variables
  7.     public $arrImageIds = false;
  8.     public $strGalleryPath = false;
  9.     public $arrImageNames = false;
  10.     public $strFileName = false;
  11.     public $blnRedirectPage = false;
  12.     public $arrThumbReturn = false;
  13.     public $arrEXIF = false;
  14.     public $arrErrorMsg = array();
  15.     public $arrErrorMsg_widg = array();
  16.  
  17.     function upload_images() {        
  18.         global $wpdb;
  19.         // Image Array
  20.         $imageslist = array();
  21.         // Get Gallery ID
  22.         $galleryID = (int) $_POST['galleryselect'];
  23.         if ($galleryID == 0) {
  24.             if(get_option('npu_default_gallery')) {
  25.                 $galleryID = get_option('npu_default_gallery');
  26.             } else {
  27.                 nggGallery::show_error(__('No gallery selected.','nggallery'));
  28.                 return;
  29.             }
  30.         }
  31.         // Get Gallery Path
  32.         $gallerypath = $wpdb->get_var("SELECT path FROM $wpdb->nggallery WHERE gid = '$galleryID' ");      
  33.         if (!$gallerypath){
  34.             nggGallery::show_error(__('Failure in database, no gallery path set.','nggallery'));
  35.             return;
  36.         }
  37.         // Read Image List
  38.         $dirlist = $this->scandir(WINABSPATH.$gallerypath);
  39.         foreach ($_FILES as $key => $value) {
  40.             if ($_FILES[$key]['error'] == 0) {
  41.                 $temp_file = $_FILES[$key]['tmp_name'];
  42.                 $filepart = pathinfo ( strtolower($_FILES[$key]['name']) );
  43.                 // Required Until PHP 5.2.0
  44.                 $filepart['filename'] = substr($filepart["basename"],0 ,strlen($filepart["basename"]) - (strlen($filepart["extension"]) + 1) );
  45.                 // Random hash generation added by [http://www.linus-neumann.de/2011/04/19/ngg_pu_patch]
  46.                     $randPool = '0123456789abcdefghijklmnopqrstuvwxyz';
  47.                     for($i = 0; $i<20; $i++)
  48.                         $entropy .= $randPool[mt_rand(0,strlen($randPool)-1)];
  49.                 $filename = sanitize_title($filepart['filename']) . '-' . sha1(md5($entropy)) . '.' . $filepart['extension'];
  50.                 // Allowed Extensions
  51.                 $ext = array('jpeg', 'jpg', 'png', 'gif');         
  52.                 if ( !in_array($filepart['extension'], $ext) || !@getimagesize($temp_file) ){
  53.                     nggGallery::show_error('<strong>'.$_FILES[$key]['name'].' </strong>'.__('is not a valid file.','nggallery'));
  54.                     continue;
  55.                 }
  56.                 // Check If File Exists
  57.                 $i = 0;
  58.                 while (in_array($filename,$dirlist)) {
  59.                     $filename = sanitize_title($filepart['filename']) . '_' . $i++ . '.' .$filepart['extension'];
  60.                 }
  61.                 $dest_file = WINABSPATH . $gallerypath . '/' . $filename;
  62.                 // Check Folder Permissions
  63.                 if (!is_writeable(WINABSPATH.$gallerypath)) {
  64.                     $message = sprintf(__('Unable to write to directory %s. Is this directory writable by the server?', 'nggallery'), WINABSPATH.$gallerypath);
  65.                     nggGallery::show_error($message);
  66.                     return;
  67.                 }
  68.                 // Save Temporary File
  69.                 if (!@move_uploaded_file($_FILES[$key]['tmp_name'], $dest_file)){
  70.                     nggGallery::show_error(__('Error, the file could not moved to : ','nggallery').$dest_file);
  71.                     $this->check_safemode(WINABSPATH.$gallerypath);
  72.                     continue;
  73.                 }
  74.                 if (!$this->chmod ($dest_file)) {
  75.                     nggGallery::show_error(__('Error, the file permissions could not set.','nggallery'));
  76.                     continue;
  77.                 }
  78.                 // Add to Image and Dir List
  79.                 $imageslist[] = $filename;
  80.                 $dirlist[] = $filename;
  81.             }
  82.         }
  83.         if (count($imageslist) > 0) {      
  84.             if (get_option('npu_exclude_select') == "Disabled") {
  85.                 $npu_exclude_id = 0;
  86.             } else {
  87.                 $npu_exclude_id = 1;
  88.             }
  89.             // Add Images to Database
  90.             $image_ids = $this->add_Images($galleryID, $imageslist);
  91.             $this->arrThumbReturn = array();
  92.             foreach ($image_ids as $pid) {
  93.                 $wpdb->query("UPDATE $wpdb->nggpictures SET exclude = '$npu_exclude_id' WHERE pid = '$pid'");
  94.                 $this->arrThumbReturn[] = $this->create_thumbnail($pid);
  95.             }
  96.             $this->arrImageIds = array();
  97.             $this->arrImageIds = $image_ids;
  98.             $this->arrImageNames =array();
  99.             $this->arrImageNames = $imageslist;
  100.             $this->strGalleryPath = $gallerypath;
  101.         }
  102.         return;
  103.     } // End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement