Example #1
0
 /**
  * Import an image from the filesystem into NextGen
  *
  * @synopsis --filename=<absolute-path> --gallery=<gallery-id>
  */
 function import_image($args, $assoc_args)
 {
     $mapper = C_Gallery_Mapper::get_instance();
     $storage = C_Gallery_Storage::get_instance();
     if ($gallery = $mapper->find($assoc_args['gallery'], TRUE)) {
         $file_data = @file_get_contents($assoc_args['filename']);
         $file_name = M_I18n::mb_basename($assoc_args['filename']);
         if (empty($file_data)) {
             WP_CLI::error('Could not load file');
         }
         $image = $storage->upload_base64_image($gallery, $file_data, $file_name);
         $image_id = $image->{$image->id_field};
         if (!$image) {
             WP_CLI::error('Could not import image');
         } else {
             WP_CLI::success("Imported image with id #{$image_id}");
         }
     } else {
         WP_CLI::error("Gallery not found (with id #{$assoc_args['gallery']}");
     }
 }
Example #2
0
 /**
  * Add images to database
  *
  * @class nggAdmin
  * @param int $galleryID
  * @param array $imageslist
  * @return array $image_ids Id's which are sucessful added
  */
 static function add_Images($galleryID, $imageslist)
 {
     global $wpdb, $ngg;
     $image_ids = array();
     if (is_array($imageslist)) {
         foreach ($imageslist as $picture) {
             // filter function to rename/change/modify image before
             $picture = apply_filters('ngg_pre_add_new_image', $picture, $galleryID);
             // strip off the extension of the filename
             $path_parts = M_I18n::mb_pathinfo($picture);
             $alttext = !isset($path_parts['filename']) ? substr($path_parts['basename'], 0, strpos($path_parts['basename'], '.')) : $path_parts['filename'];
             // save it to the database
             $pic_id = nggdb::add_image($galleryID, $picture, '', $alttext);
             if (C_NextGen_Settings::get_instance()->imgBackup && !empty($pic_id)) {
                 $storage = C_Gallery_Storage::get_instance();
                 $storage->backup_image($pic_id);
             }
             if (!empty($pic_id)) {
                 $image_ids[] = $pic_id;
             }
             // add the metadata
             nggAdmin::import_MetaData($pic_id);
             // auto rotate
             nggAdmin::rotate_image($pic_id);
             // Autoresize image if required
             if ($ngg->options['imgAutoResize']) {
                 $imagetmp = nggdb::find_image($pic_id);
                 $sizetmp = @getimagesize($imagetmp->imagePath);
                 $widthtmp = $ngg->options['imgWidth'];
                 $heighttmp = $ngg->options['imgHeight'];
                 if ($sizetmp[0] > $widthtmp && $widthtmp || $sizetmp[1] > $heighttmp && $heighttmp) {
                     nggAdmin::resize_image($pic_id);
                 }
             }
             // action hook for post process after the image is added to the database
             $image = array('id' => $pic_id, 'filename' => $picture, 'galleryID' => $galleryID);
             do_action('ngg_added_new_image', $image);
         }
     }
     // is_array
     // delete dirsize after adding new images
     delete_transient('dirsize_cache');
     do_action('ngg_after_new_images_added', $galleryID, $image_ids);
     return $image_ids;
 }
 public function import_media_library_action()
 {
     $retval = array();
     $created_gallery = FALSE;
     $gallery_id = intval($this->param('gallery_id'));
     $gallery_name = urldecode($this->param('gallery_name'));
     $gallery_mapper = C_Gallery_Mapper::get_instance();
     $image_mapper = C_Image_Mapper::get_instance();
     $attachment_ids = $this->param('attachment_ids');
     if ($this->validate_ajax_request('nextgen_upload_image', TRUE)) {
         if (empty($attachment_ids) || !is_array($attachment_ids)) {
             $retval['error'] = __('An unexpected error occured.', 'nggallery');
         }
         if (empty($retval['error']) && $gallery_id == 0) {
             if (strlen($gallery_name) > 0) {
                 $gallery = $gallery_mapper->create(array('title' => $gallery_name));
                 if (!$gallery->save()) {
                     $retval['error'] = $gallery->get_errors();
                 } else {
                     $created_gallery = TRUE;
                     $gallery_id = $gallery->id();
                 }
             } else {
                 $retval['error'] = __('No gallery name specified', 'nggallery');
             }
         }
         if (empty($retval['error'])) {
             $retval['gallery_id'] = $gallery_id;
             $storage = C_Gallery_Storage::get_instance();
             foreach ($attachment_ids as $id) {
                 try {
                     $abspath = get_attached_file($id);
                     $file_data = @file_get_contents($abspath);
                     $file_name = M_I18n::mb_basename($abspath);
                     $attachment = get_post($id);
                     if (empty($file_data)) {
                         $retval['error'] = __('Image generation failed', 'nggallery');
                         break;
                     }
                     $image = $storage->upload_base64_image($gallery_id, $file_data, $file_name);
                     if ($image) {
                         // Potentially import metadata from WordPress
                         $image = $image_mapper->find($image->id());
                         if (!empty($attachment->post_excerpt)) {
                             $image->alttext = $attachment->post_excerpt;
                         }
                         if (!empty($attachment->post_content)) {
                             $image->description = $attachment->post_content;
                         }
                         $image = apply_filters('ngg_medialibrary_imported_image', $image, $attachment);
                         $image_mapper->save($image);
                     } else {
                         $retval['error'] = __('Image generation failed', 'nggallery');
                         break;
                     }
                     $retval['image_ids'][] = $image->{$image->id_field};
                 } catch (E_NggErrorException $ex) {
                     $retval['error'] = $ex->getMessage();
                     if ($created_gallery) {
                         $gallery_mapper->destroy($gallery_id);
                     }
                 } catch (Exception $ex) {
                     $retval['error'] = __('An unexpected error occured.', 'nggallery');
                     $retval['error_details'] = $ex->getMessage();
                 }
             }
         }
     } else {
         $retval['error'] = __('No permissions to upload images. Try refreshing the page or ensuring that your user account has sufficient roles/privileges.', 'nggallery');
     }
     if (!empty($retval['error'])) {
         return $retval;
     } else {
         $retval['gallery_name'] = esc_html($gallery_name);
     }
     return $retval;
 }
 public function get_params_from_name($name, $is_only_size_name = false)
 {
     $prefix_list = $this->object->_get_name_prefix_list();
     $id_prefix = $prefix_list['id'];
     $size_prefix = $prefix_list['size'];
     $flags_prefix = $prefix_list['flags'];
     $max_value_length = $prefix_list['max_value_length'];
     $size_name = null;
     $id_name = null;
     $params = array();
     if (!$is_only_size_name) {
         $extension = M_I18n::mb_pathinfo($name, PATHINFO_EXTENSION);
         if ($extension != null) {
             $extension = '.' . $extension;
         }
         $name = M_I18n::mb_basename($name, $extension);
     }
     $size_index = strrpos($name, $size_prefix);
     if ($size_index > 0 || $size_index === 0) {
         // check if name contains dynamic size/params info by looking for prefix
         $size_name = substr($name, $size_index);
     }
     if (!$is_only_size_name) {
         // name should contain the image id, search for prefix
         $id_index = strrpos($name, $id_prefix);
         if ($id_index > 0 || $id_index === 0) {
             if ($size_index > 0 && $size_index > $id_index) {
                 $id_name = substr($name, $id_index, $size_index - $id_index);
             } else {
                 $id_name = substr($name, $id_index);
             }
         }
     }
     // Double check we got a correct dynamic size/params string
     if (substr($size_name, 0, strlen($size_prefix)) == $size_prefix) {
         $flags = $prefix_list['flag'];
         // get the length of the flag id (the key in the $flags array) in the string (how many characters to consume)
         $flag_id_len = $prefix_list['flag_len'];
         $params_str = substr($size_name, strlen($size_prefix));
         $params_parts = explode('-', $params_str);
         // $param_part is a single param, separated by '-'
         foreach ($params_parts as $param_part) {
             // Parse WxHxQ - Q=quality
             $param_size = explode('x', $param_part);
             $param_size_count = count($param_size);
             if (substr($param_part, 0, strlen($flags_prefix)) == $flags_prefix) {
                 /* Set flags, using $flags keys as prefixes */
                 // move string pointer up (after the main flags prefix)
                 $param_flags = substr($param_part, strlen($flags_prefix));
                 $param_flags_len = strlen($param_flags);
                 $flags_todo = $flags;
                 while (true) {
                     // ensure we don't run into an infinite loop ;)
                     if (count($flags_todo) == 0 || strlen($param_flags) == 0) {
                         break;
                     }
                     // get the flag prefix (a key in the $flags array) using flag id length
                     $flag_prefix = substr($param_flags, 0, $flag_id_len);
                     // move string pointer up (after the single flag prefix)
                     $param_flags = substr($param_flags, $flag_id_len);
                     // get the length of the flag value in the string (how many characters to consume)
                     // flag value length is stored in a single hexadecimal character next to the flag prefix
                     $flag_value_len = min(hexdec(substr($param_flags, 0, 1)), min($max_value_length, strlen($param_flags) - 1));
                     // get the flag value
                     $flag_value = substr($param_flags, 1, $flag_value_len);
                     // move string pointer up (after the entire flag)
                     $param_flags = substr($param_flags, $flag_value_len + 1);
                     // make sure the flag is supported
                     if (isset($flags[$flag_prefix])) {
                         $flag_name = $flags[$flag_prefix];
                         if (is_numeric($flag_value)) {
                             // convert numerical flags to integers
                             $flag_value = intval($flag_value);
                         }
                         $params[$flag_name] = $flag_value;
                         if (isset($flags_todo[$flag_prefix])) {
                             unset($flags_todo[$flag_prefix]);
                         }
                     } else {
                     }
                 }
             } else {
                 if ($param_size_count == 2 || $param_size_count == 3) {
                     // Set W H Q
                     $params['width'] = intval($param_size[0]);
                     $params['height'] = intval($param_size[1]);
                     if (isset($param_size[2]) && intval($param_size[2]) > 0) {
                         $params['quality'] = intval($param_size[2]);
                     }
                 }
             }
         }
     }
     // Double check we got a correct id string
     if (substr($id_name, 0, strlen($id_prefix)) == $id_prefix) {
         // move string pointer up (after the prefix)
         $id_name = substr($id_name, strlen($id_prefix));
         // get the length of the image id in the string (how many characters to consume)
         $id_len = min(hexdec(substr($id_name, 0, 1)), min($max_value_length, strlen($id_name) - 1));
         // get the id based on old position and id length
         $image_id = intval(substr($id_name, 1, $id_len));
         if ($image_id > 0) {
             $params['image'] = $image_id;
         }
     }
     return $this->object->_get_params_sanitized($params);
 }
 /**
  * Copies (or moves) images into another gallery
  *
  * @param array $images
  * @param int|object $gallery
  * @param boolean $db optionally only copy the image files
  * @param boolean $move move the image instead of copying
  * @return mixed NULL on failure, array|image-ids on success
  */
 public function copy_images($images, $gallery, $db = TRUE, $move = FALSE)
 {
     $new_image_pids = array();
     // the return value
     // legacy requires passing just a numeric ID
     if (is_numeric($gallery)) {
         $gallery = $this->object->_gallery_mapper->find($gallery);
     }
     // move_images() is a wrapper to this function so we implement both features here
     $func = $move ? 'rename' : 'copy';
     // legacy allows for arrays of just the ID
     if (!is_array($images)) {
         $images = array($images);
     }
     // Ensure we have a valid gallery
     $gallery_id = $this->object->_get_gallery_id($gallery);
     if (!$gallery_id) {
         return array();
     }
     $image_key = $this->object->_image_mapper->get_primary_key_column();
     $gallery_abspath = $this->object->get_gallery_abspath($gallery);
     // Check for folder permission
     if (!is_dir($gallery_abspath) && !wp_mkdir_p($gallery_abspath)) {
         echo sprintf(__('Unable to create directory %s.', 'nggallery'), esc_html($gallery_abspath));
         return $new_image_pids;
     }
     if (!is_writable($gallery_abspath)) {
         echo sprintf(__('Unable to write to directory %s. Is this directory writable by the server?', 'nggallery'), esc_html($gallery_abspath));
         return $new_image_pids;
     }
     foreach ($images as $image) {
         if ($this->object->is_current_user_over_quota()) {
             throw new E_NoSpaceAvailableException(__('Sorry, you have used your space allocation. Please delete some files to upload more files.', 'nggallery'));
         }
         // again legacy requires that it be able to pass just a numeric ID
         if (is_numeric($image)) {
             $image = $this->object->_image_mapper->find($image);
         }
         $old_pid = $image->{$image_key};
         // update the DB if requested
         $new_image = clone $image;
         $new_pid = $old_pid;
         if ($db) {
             unset($new_image->extras_post_id);
             $new_image->galleryid = $gallery_id;
             if (!$move) {
                 $new_image->image_slug = nggdb::get_unique_slug(sanitize_title_with_dashes($image->alttext), 'image');
                 unset($new_image->{$image_key});
             }
             $new_pid = $this->object->_image_mapper->save($new_image);
         }
         if (!$new_pid) {
             echo sprintf(__('Failed to copy database row for picture %s', 'nggallery'), $old_pid) . '<br />';
             continue;
         }
         // Copy each image size
         foreach ($this->object->get_image_sizes() as $size) {
             // if backups are off there's no backup file to copy
             if (!C_NextGen_Settings::get_instance()->imgBackup && $size == 'backup') {
                 continue;
             }
             $orig_path = $this->object->get_image_abspath($image, $size, TRUE);
             if (!$orig_path || !@file_exists($orig_path)) {
                 echo sprintf(__('Failed to get image path for %s', 'nggallery'), esc_html(M_I18n::mb_basename($orig_path))) . '<br/>';
                 continue;
             }
             $new_path = $this->object->get_image_abspath($new_image, $size, FALSE);
             // Prevent duplicate filenames: check if the filename exists and begin appending '-#'
             if (!ini_get('safe_mode') && @file_exists($new_path)) {
                 // prevent get_image_abspath() from using the thumbnail filename in metadata
                 unset($new_image->meta_data['thumbnail']['filename']);
                 $file_exists = TRUE;
                 $i = 0;
                 do {
                     $i++;
                     $parts = explode('.', $image->filename);
                     $extension = array_pop($parts);
                     $tmp_filename = implode('.', $parts) . '-' . $i . '.' . $extension;
                     $new_image->filename = $tmp_filename;
                     $tmp_path = $this->object->get_image_abspath($new_image, $size, FALSE);
                     if (!@file_exists($tmp_path)) {
                         $file_exists = FALSE;
                         $new_path = $tmp_path;
                         if ($db) {
                             $this->object->_image_mapper->save($new_image);
                         }
                     }
                 } while ($file_exists == TRUE);
             }
             // Copy files
             if (!@$func($orig_path, $new_path)) {
                 echo sprintf(__('Failed to copy image %1$s to %2$s', 'nggallery'), esc_html($orig_path), esc_html($new_path)) . '<br/>';
                 continue;
             }
             // disabling: this is a bit too verbose
             // if (!empty($tmp_path))
             //     echo sprintf(__('Image %1$s (%2$s) copied as image %3$s (%4$s) &raquo; The file already existed in the destination gallery.', 'nggallery'), $old_pid, esc_html($orig_path), $new_pid, esc_html($new_path)) . '<br />';
             // else
             //     echo sprintf(__('Image %1$s (%2$s) copied as image %3$s (%4$s)', 'nggallery'), $old_pid, esc_html($orig_path), $new_pid, esc_html($new_path)) . '<br />';
             // Copy tags
             if ($db) {
                 $tags = wp_get_object_terms($old_pid, 'ngg_tag', 'fields=ids');
                 $tags = array_map('intval', $tags);
                 wp_set_object_terms($new_pid, $tags, 'ngg_tag', true);
             }
         }
         $new_image_pids[] = $new_pid;
     }
     $title = '<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $gallery_id . '" >';
     $title .= $gallery->title;
     $title .= '</a>';
     echo '<hr/>' . sprintf(__('Copied %1$s picture(s) to gallery %2$s .', 'nggallery'), count($new_image_pids), $title);
     return $new_image_pids;
 }
Example #6
0
 function add_media_to_collection($mediaId, $collectionId)
 {
     global $wplr;
     // Upload the file to the gallery
     $gallery_id = $wplr->get_meta('nextgen_gallery_id', $collectionId);
     $abspath = get_attached_file($mediaId);
     $file_data = file_get_contents($abspath);
     $file_name = M_I18n::mb_basename($abspath);
     $attachment = get_post($mediaId);
     $storage = C_Gallery_Storage::get_instance();
     $image = $storage->upload_base64_image($gallery_id, $file_data, $file_name);
     $wplr->set_meta('nextgen_collection_' . $collectionId . '_image_id', $mediaId, $image->id());
     // Import metadata from WordPress
     $image_mapper = C_Image_Mapper::get_instance();
     $image = $image_mapper->find($image->id());
     if (!empty($attachment->post_excerpt)) {
         $image->alttext = $attachment->post_excerpt;
     }
     if (!empty($attachment->post_content)) {
         $image->description = $attachment->post_content;
     }
     $image_mapper->save($image);
 }
Example #7
0
 /**
  * nggPostThumbnail::ajax_set_post_thumbnail()
  * 
  * @return void
  */
 function ajax_set_post_thumbnail()
 {
     global $post_ID;
     // check for correct capability
     if (!is_user_logged_in()) {
         die('-1');
     }
     // get the post id as global variable, otherwise the ajax_nonce failed later
     $post_ID = intval($_POST['post_id']);
     if (!current_user_can('edit_post', $post_ID)) {
         die('-1');
     }
     $thumbnail_id = intval($_POST['thumbnail_id']);
     // delete the image
     if ($thumbnail_id == '-1') {
         delete_post_meta($post_ID, '_thumbnail_id');
         die('0');
     }
     if ($thumbnail_id != null) {
         $imap = C_Image_Mapper::get_instance();
         $storage = C_Gallery_Storage::get_instance();
         $image = $imap->find($thumbnail_id);
         // for NGG we look for the image id
         if ($image) {
             $image_id = $thumbnail_id;
             $args = array('post_type' => 'attachment', 'meta_key' => '_ngg_image_id', 'meta_compare' => '==', 'meta_value' => $image_id);
             $upload_dir = wp_upload_dir();
             $basedir = $upload_dir['basedir'];
             $thumbs_dir = implode(DIRECTORY_SEPARATOR, array($basedir, 'ngg_featured'));
             $gallery_abspath = $storage->get_gallery_abspath($image->galleryid);
             $image_abspath = $storage->get_full_abspath($image);
             $target_path = null;
             $posts = get_posts($args);
             $attachment_id = null;
             if ($posts != null) {
                 $attachment_id = $posts[0]->ID;
             } else {
                 $url = $storage->get_full_url($image);
                 $target_relpath = null;
                 $target_basename = M_I18n::mb_basename($image_abspath);
                 if (strpos($image_abspath, $gallery_abspath) === 0) {
                     $target_relpath = substr($image_abspath, strlen($gallery_abspath));
                 } else {
                     if ($image->galleryid) {
                         $target_relpath = path_join(strval($image->galleryid), $target_basename);
                     } else {
                         $target_relpath = $target_basename;
                     }
                 }
                 $target_relpath = trim($target_relpath, '\\/');
                 $target_path = path_join($thumbs_dir, $target_relpath);
                 $max_count = 100;
                 $count = 0;
                 while (file_exists($target_path) && $count <= $max_count) {
                     $count++;
                     $pathinfo = M_I18n::mb_pathinfo($target_path);
                     $dirname = $pathinfo['dirname'];
                     $filename = $pathinfo['filename'];
                     $extension = $pathinfo['extension'];
                     $rand = mt_rand(1, 9999);
                     $basename = $filename . '_' . sprintf('%04d', $rand) . '.' . $extension;
                     $target_path = path_join($dirname, $basename);
                 }
                 if (file_exists($target_path)) {
                     // XXX handle very rare case in which $max_count wasn't enough?
                 }
                 $target_dir = dirname($target_path);
                 wp_mkdir_p($target_dir);
                 if (@copy($image_abspath, $target_path)) {
                     $size = @getimagesize($target_path);
                     $image_type = $size ? $size['mime'] : 'image/jpeg';
                     $title = sanitize_file_name($image->alttext);
                     $caption = sanitize_file_name($image->description);
                     $attachment = array('post_title' => $title, 'post_content' => $caption, 'post_status' => 'attachment', 'post_parent' => 0, 'post_mime_type' => $image_type, 'guid' => $url);
                     // Save the data
                     $attachment_id = wp_insert_attachment($attachment, $target_path);
                     if ($attachment_id) {
                         wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $target_path));
                         update_post_meta($attachment_id, '_ngg_image_id', $image_id);
                     }
                 }
             }
             if ($attachment_id) {
                 //$attachment = get_post($attachment_id);
                 //$attachment_meta = wp_get_attachment_metadata($attachment_id);
                 $attachment_file = get_attached_file($attachment_id);
                 $target_path = $attachment_file;
                 if (filemtime($image_abspath) > filemtime($target_path)) {
                     if (@copy($image_abspath, $target_path)) {
                         wp_update_attachment_metadata($attachment_id, wp_generate_attachment_metadata($attachment_id, $target_path));
                     }
                 }
                 die(strval($attachment_id));
             }
         }
     }
     die('0');
 }