Example #1
0
 /**
  * Check the Quota under WPMU. Only needed for this case
  * 
  * @class nggWPMU
  * @return bool $result
  */
 function check_quota()
 {
     if (get_site_option('upload_space_check_disabled')) {
         return false;
     }
     if (is_multisite() && nggWPMU::wpmu_enable_function('wpmuQuotaCheck')) {
         if ($error = upload_is_user_over_quota(false)) {
             nggGallery::show_error(__('Sorry, you have used your space allocation. Please delete some files to upload more files.', 'nggallery'));
             return true;
         }
     }
     return false;
 }
Example #2
0
 /**
  * Copy images to another gallery
  * 
  * @class nggAdmin
  * @param array|int $pic_ids ID's of the images
  * @param int $dest_gid destination gallery
  * @return void
  */
 function copy_images($pic_ids, $dest_gid)
 {
     require_once NGGALLERY_ABSPATH . '/lib/meta.php';
     $errors = $messages = '';
     if (!is_array($pic_ids)) {
         $pic_ids = array($pic_ids);
     }
     // Get destination gallery
     $destination = nggdb::find_gallery($dest_gid);
     if ($destination == null) {
         nggGallery::show_error(__('The destination gallery does not exist', 'nggallery'));
         return;
     }
     // Check for folder permission
     if (!is_writeable(WINABSPATH . $destination->path)) {
         $message = sprintf(__('Unable to write to directory %s. Is this directory writable by the server?', 'nggallery'), WINABSPATH . $destination->path);
         nggGallery::show_error($message);
         return;
     }
     // Get pictures
     $images = nggdb::find_images_in_list($pic_ids);
     $destination_path = WINABSPATH . $destination->path;
     foreach ($images as $image) {
         // WPMU action
         if (nggWPMU::check_quota()) {
             return;
         }
         $i = 0;
         $tmp_prefix = '';
         $destination_file_name = $image->filename;
         while (file_exists($destination_path . '/' . $destination_file_name)) {
             $tmp_prefix = 'copy_' . $i++ . '_';
             $destination_file_name = $tmp_prefix . $image->filename;
         }
         $destination_file_path = $destination_path . '/' . $destination_file_name;
         $destination_thumb_file_path = $destination_path . '/' . $image->thumbFolder . $image->thumbPrefix . $destination_file_name;
         // Copy files
         if (!@copy($image->imagePath, $destination_file_path)) {
             $errors .= sprintf(__('Failed to copy image %1$s to %2$s', 'nggallery'), $image->filename, $destination_file_path) . '<br />';
             continue;
         }
         // Copy backup file, if possible
         @copy($image->imagePath . '_backup', $destination_file_path . '_backup');
         // Copy the thumbnail if possible
         @copy($image->thumbPath, $destination_thumb_file_path);
         // Create new database entry for the image
         $new_pid = nggdb::insert_image($destination->gid, $destination_file_name, $image->alttext, $image->description, $image->exclude);
         if (!isset($new_pid)) {
             $errors .= sprintf(__('Failed to copy database row for picture %s', 'nggallery'), $image->pid) . '<br />';
             continue;
         }
         // Copy tags
         nggTags::copy_tags($image->pid, $new_pid);
         // Copy meta information
         $meta = new nggMeta($image->pid);
         nggdb::update_image_meta($new_pid, $meta->image->meta_data);
         if ($tmp_prefix != '') {
             $messages .= sprintf(__('Image %1$s (%2$s) copied as image %3$s (%4$s) &raquo; The file already existed in the destination gallery.', 'nggallery'), $image->pid, $image->filename, $new_pid, $destination_file_name) . '<br />';
         } else {
             $messages .= sprintf(__('Image %1$s (%2$s) copied as image %3$s (%4$s)', 'nggallery'), $image->pid, $image->filename, $new_pid, $destination_file_name) . '<br />';
         }
     }
     // Finish by showing errors or success
     if ($errors == '') {
         $link = '<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $destination->gid . '" >' . $destination->title . '</a>';
         $messages .= '<hr />' . sprintf(__('Copied %1$s picture(s) to gallery: %2$s .', 'nggallery'), count($images), $link);
     }
     if ($messages != '') {
         nggGallery::show_message($messages);
     }
     if ($errors != '') {
         nggGallery::show_error($errors);
     }
     return;
 }
 /**
  * Uploads base64 file to a gallery
  * @param int|stdClass|C_Gallery $gallery
  * @param $data base64-encoded string of data representing the image
  * @param type $filename specifies the name of the file
  * @return C_Image
  */
 function upload_base64_image($gallery, $data, $filename = FALSE)
 {
     $settings = C_NextGen_Settings::get_instance();
     $memory_limit = intval(ini_get('memory_limit'));
     if ($memory_limit < 256) {
         @ini_set('memory_limit', '256M');
     }
     $retval = NULL;
     if ($gallery_id = $this->object->_get_gallery_id($gallery)) {
         // Ensure that there is capacity available
         require_once ABSPATH . 'wp-admin/includes/ms.php';
         if (is_multisite() && nggWPMU::wpmu_enable_function('wpmuQuotaCheck')) {
             if (upload_is_user_over_quota(FALSE)) {
                 throw new E_NoSpaceAvailableException();
             }
         }
         // Get path information. The use of get_upload_abspath() might
         // not be the best for some drivers. For example, if using the
         // WordPress Media Library for uploading, then the wp_upload_bits()
         // function should perhaps be used
         $upload_dir = $this->object->get_upload_abspath($gallery);
         // Perhaps a filename was given instead of base64 data?
         if ($data[0] == '/' && @file_exists($data)) {
             if (!$filename) {
                 $filename = basename($data);
             }
             $data = file_get_contents($data);
         }
         // Determine filenames
         $filename = $filename ? sanitize_title_with_dashes($filename) : uniqid('nextgen-gallery');
         if (preg_match("/\\-(png|jpg|gif|jpeg)\$/i", $filename, $match)) {
             $filename = str_replace($match[0], '.' . $match[1], $filename);
         }
         $abs_filename = path_join($upload_dir, $filename);
         // Create the database record
         $factory = $this->object->get_registry()->get_utility('I_Component_Factory');
         $retval = $image = $factory->create('image');
         $image->alttext = sanitize_title_with_dashes(basename($filename, '.' . pathinfo($filename, PATHINFO_EXTENSION)));
         $image->galleryid = $this->object->_get_gallery_id($gallery);
         $image->filename = $filename;
         $image_key = $this->object->_image_mapper->get_primary_key_column();
         // If we can't write to the directory, then there's no point in continuing
         if (!@file_exists($upload_dir)) {
             @wp_mkdir_p($upload_dir);
         }
         if (!is_writable($upload_dir)) {
             throw new E_InsufficientWriteAccessException(FALSE, $upload_dir, FALSE);
         }
         // Save the image
         if ($image_id = $this->object->_image_mapper->save($image)) {
             try {
                 // Try writing the image
                 if (!@file_exists($upload_dir)) {
                     wp_mkdir_p($upload_dir);
                 }
                 $fp = fopen($abs_filename, 'w');
                 fwrite($fp, $data);
                 fclose($fp);
                 if ($settings->imgBackup) {
                     $this->object->backup_image($image);
                 }
                 if ($settings->imgAutoResize) {
                     $this->object->generate_image_clone($abs_filename, $abs_filename, $this->object->get_image_size_params($image_id, 'full'));
                 }
                 // Ensure that fullsize dimensions are added to metadata array
                 $dimensions = getimagesize($abs_filename);
                 $full_meta = array('width' => $dimensions[0], 'height' => $dimensions[1]);
                 if (!isset($image->meta_data) or is_string($image->meta_data) && strlen($image->meta_data) == 0) {
                     $image->meta_data = array();
                 }
                 $image->meta_data = array_merge($image->meta_data, $full_meta);
                 $image->meta_data['full'] = $full_meta;
                 // Generate a thumbnail for the image
                 $this->object->generate_thumbnail($image);
                 // Set gallery preview image if missing
                 $this->object->get_registry()->get_utility('I_Gallery_Mapper')->set_preview_image($gallery, $image_id, TRUE);
                 // Notify other plugins that an image has been added
                 do_action('ngg_added_new_image', $image);
                 // delete dirsize after adding new images
                 delete_transient('dirsize_cache');
                 // Seems redundant to above hook. Maintaining for legacy purposes
                 do_action('ngg_after_new_images_added', $gallery_id, array($image->{$image_key}));
             } catch (Exception $ex) {
                 throw new E_InsufficientWriteAccessException(FALSE, $abs_filename, FALSE, $ex);
             }
         } else {
             throw new E_InvalidEntityException();
         }
     } else {
         throw new E_EntityNotFoundException();
     }
     @ini_set('memory_limit', $memory_limit . 'M');
     return $retval;
 }
Example #4
0
 /**
  * Upload function will be called via the Flash uploader
  *
  * @class nggAdmin
  * @param integer $galleryID
  * @return string $result
  */
 static function swfupload_image($galleryID = 0)
 {
     global $nggdb;
     if ($galleryID == 0) {
         return __('No gallery selected !', 'nggallery');
     }
     // WPMU action
     if (nggWPMU::check_quota()) {
         return '0';
     }
     // Check the upload
     if (!isset($_FILES['Filedata']) || !is_uploaded_file($_FILES['Filedata']['tmp_name']) || $_FILES['Filedata']['error'] != 0) {
         return __('Invalid upload. Error Code : ', 'nggallery') . $_FILES['Filedata']['error'];
     }
     // get the filename and extension
     $temp_file = $_FILES['Filedata']['tmp_name'];
     $filepart = nggGallery::fileinfo($_FILES['Filedata']['name']);
     $filename = $filepart['basename'];
     // check for allowed extension
     $ext = apply_filters('ngg_allowed_file_types', array('jpeg', 'jpg', 'png', 'gif'));
     if (!in_array(strtolower($filepart['extension']), $ext)) {
         return esc_html($_FILES[$key]['name']) . __('is no valid image file!', 'nggallery');
     }
     // get the path to the gallery
     $gallery = $nggdb->find_gallery((int) $galleryID);
     if (empty($gallery->path)) {
         @unlink($temp_file);
         return __('Failure in database, no gallery path set !', 'nggallery');
     }
     // read list of images
     $imageslist = nggAdmin::scandir(ABSPATH . $gallery->path);
     // check if this filename already exist
     $i = 0;
     while (in_array($filename, $imageslist)) {
         $filename = $filepart['filename'] . '_' . $i++ . '.' . $filepart['extension'];
     }
     $dest_file = ABSPATH . $gallery->path . '/' . $filename;
     // save temp file to gallery
     if (!@move_uploaded_file($_FILES["Filedata"]['tmp_name'], $dest_file)) {
         nggAdmin::check_safemode(ABSPATH . $gallery->path);
         return __('Error, the file could not be moved to : ', 'nggallery') . esc_html($dest_file);
     }
     if (!nggAdmin::chmod($dest_file)) {
         return __('Error, the file permissions could not be set', 'nggallery');
     }
     return '0';
 }