Esempio n. 1
0
 /**
  * Constructor
  */
 function __construct($file_id, Assets_ee_source $source, $prefetched_row = null)
 {
     parent::__construct($file_id, $source, $prefetched_row);
     $cache =& $this->EE->session->cache['assets'];
     $upload_filedir_id = $this->folder_row->filedir_id;
     if (!isset($cache['filedir_prefs'][$upload_filedir_id])) {
         $cache['filedir_prefs'][$upload_filedir_id] = $this->source->get_filedir($upload_filedir_id);
     }
     if (empty($cache['filedir_prefs'][$upload_filedir_id])) {
         throw new Exception(lang('exception_error'));
     }
     $this->filedir = $cache['filedir_prefs'][$upload_filedir_id];
     $this->subpath = $this->folder_row->full_path . $this->row_field('file_name');
     $this->path = $this->filedir->name . '/' . $this->subpath;
     $this->server_path = str_replace('//', '/', Assets_ee_source::resolve_server_path($this->filedir->server_path) . $this->subpath);
 }
 /**
  * Migrate data according to a scenario
  * @param $scenario
  */
 private function _migrate_data($scenario)
 {
     $this->EE = get_instance();
     $db = $this->EE->db;
     $this->EE->load->library('assets_lib');
     clearstatcache();
     switch ($scenario) {
         case '<2 -> 2.0':
             require_once PATH_THIRD . 'assets/sources/ee/source.ee.php';
             $filedirs = array();
             $folder_list = array();
             // load upload preferences and store them in table for Assets
             $rows = $db->get('upload_prefs')->result();
             foreach ($rows as $filedir) {
                 $filedirs[$filedir->id] = Assets_ee_source::apply_filedir_overrides($filedir);
             }
             // load physical folder structure
             foreach ($filedirs as $id => $filedir) {
                 $filedir->server_path = Assets_ee_source::resolve_server_path($filedir->server_path);
                 $folder_list[$id][] = $filedir->server_path;
                 $this->_load_folder_structure($filedir->server_path, $folder_list[$id]);
             }
             // store the folder structure in database
             $subfolders = array();
             foreach ($folder_list as $filedir_id => $folders) {
                 $filedir = $filedirs[$filedir_id];
                 foreach ($folders as $folder) {
                     $subpath = substr($folder, strlen($filedir->server_path));
                     if (empty($subpath)) {
                         $folder_name = $filedir->name;
                         $parent_id = NULL;
                     } else {
                         $path_parts = explode('/', $subpath);
                         $folder_name = array_pop($path_parts);
                         $parent_key = $filedir_id . ':' . rtrim(join('/', $path_parts), '/');
                         $parent_id = isset($subfolders[$parent_key]) ? $subfolders[$parent_key] : 0;
                     }
                     // in case false was returned earlier
                     $subpath = $subpath ? rtrim($subpath, '/') . '/' : '';
                     $folder_entry = array('source_type' => 'ee', 'filedir_id' => $filedir_id, 'folder_name' => $folder_name, 'full_path' => $subpath);
                     if (!is_null($parent_id)) {
                         $folder_entry['parent_id'] = $parent_id;
                     }
                     $this->EE->db->insert('assets_folders', $folder_entry);
                     $subfolders[$filedir_id . ':' . rtrim($subpath, '/')] = $this->EE->db->insert_id();
                 }
             }
             // bring up the list of existing assets and update the entries
             $rows = $db->get('assets_files')->result();
             $pattern = '/\\{filedir_(?P<filedir_id>[0-9]+)\\}(?P<path>.*)/';
             foreach ($rows as $asset) {
                 $asset->connector = 'ee';
                 if (preg_match($pattern, $asset->file_name, $matches)) {
                     if (isset($filedirs[$matches['filedir_id']])) {
                         $filedir = $filedirs[$matches['filedir_id']];
                         $full_path = str_replace('{filedir_' . $filedir->id . '}', $filedir->server_path, $asset->file_name);
                         $subpath = substr($full_path, strlen($filedir->server_path));
                         $path_parts = explode('/', $subpath);
                         $file = array_pop($path_parts);
                         $subpath = join('/', $path_parts);
                         $folder_key = $matches['filedir_id'] . ':' . $subpath;
                         if (isset($subfolders[$folder_key])) {
                             $folder_id = $subfolders[$folder_key];
                         } else {
                             $folder_id = 0;
                         }
                         $kind = Assets_helper::get_kind($full_path);
                         $data = array('source_type' => 'ee', 'filedir_id' => $filedir->id, 'folder_id' => $folder_id, 'file_name' => $file, 'kind' => $kind);
                         if (file_exists($full_path)) {
                             $data['size'] = filesize($full_path);
                             $data['date_modified'] = filemtime($full_path);
                             if ($kind == 'image') {
                                 list($width, $height) = getimagesize($full_path);
                                 $data['width'] = $width;
                                 $data['height'] = $height;
                             }
                         }
                         $this->EE->db->update('assets_files', $data, array('file_id' => $asset->file_id));
                         $this->EE->assets_lib->update_file_search_keywords($asset->file_id);
                     }
                 }
             }
             // celebrate
             break;
         case '2.0b1 -> 2.0b2':
             // get S3 credentials if any
             $query = $this->EE->db->select('settings')->where('name', 'assets')->get('fieldtypes');
             $settings = unserialize(base64_decode($query->row('settings')));
             $settings = array_merge(array('license_key' => '', 's3_access_key_id' => '', 's3_secret_access_key' => ''), $settings);
             //if we have s3 settings, let's convert the "folder_prefs" way to "sources" way
             if (!empty($settings['s3_access_key_id']) && !empty($settings['s3_secret_access_key'])) {
                 $old_sources = $this->EE->db->get('assets_sources')->result();
                 foreach ($old_sources as $source) {
                     $previous_settings = json_decode($source->settings);
                     $new_settings = (object) array('access_key_id' => $settings['s3_access_key_id'], 'secret_access_key' => $settings['s3_secret_access_key'], 'bucket' => $previous_settings->name, 'url_prefix' => $previous_settings->url_prefix, 'location' => $previous_settings->location);
                     $data = array('name' => $previous_settings->name, 'settings' => Assets_helper::get_json($new_settings));
                     $this->EE->db->update('assets_sources', $data, array('source_id' => $source->source_id));
                 }
             }
             // modify folder data and also keep a list of who's who
             $folders = $this->EE->db->get('assets_folders')->result();
             $folder_sources = array();
             foreach ($folders as $row) {
                 if ($row->source_type == 'ee') {
                     $row->filedir_id = $row->source_id;
                     $row->source_id = NULL;
                     $this->EE->db->update('assets_folders', $row, array('folder_id' => $row->folder_id));
                     $folder_sources[$row->folder_id] = $row->filedir_id;
                 } else {
                     $folder_sources[$row->folder_id] = $row->source_id;
                 }
             }
             // add some data for file entries and we're done!
             $files = $this->EE->db->get('assets_files')->result();
             foreach ($files as $row) {
                 if ($row->source_type == 'ee' && isset($folder_sources[$row->folder_id])) {
                     $row->source_id = NULL;
                     $row->filedir_id = $folder_sources[$row->folder_id];
                     $this->EE->db->update('assets_files', $row, array('file_id' => $row->file_id));
                 } else {
                     if (isset($folder_sources[$row->folder_id])) {
                         $row->source_id = $folder_sources[$row->folder_id];
                         $row->filedir_id = NULL;
                         $this->EE->db->update('assets_files', $row, array('file_id' => $row->file_id));
                     }
                 }
             }
             // party!
             break;
     }
 }
Esempio n. 3
0
 /**
  * Create EE thumbnails.
  *
  * @param $image_path
  * @param $upload_folder_id
  * @return mixed
  */
 public static function create_ee_thumbnails($image_path, $upload_folder_id)
 {
     if (!class_exists('Assets_ee_source')) {
         require_once PATH_THIRD . 'assets/sources/ee/source.ee.php';
     }
     $preferences = self::_get_EE()->filemanager->fetch_upload_dir_prefs($upload_folder_id);
     $preferences['file_name'] = pathinfo($image_path, PATHINFO_BASENAME);
     $preferences['server_path'] = Assets_ee_source::resolve_server_path(Assets_helper::normalize_path($preferences['server_path']));
     // Trick Filemanager into creating the thumbnail where WE need it
     $preferences['server_path'] .= str_replace($preferences['server_path'], '', str_replace(pathinfo($image_path, PATHINFO_BASENAME), '', $image_path));
     // On Windows machines CI's Image_lib gets all sorts of confused, so have to make sure our paths use the DIRECTORY_SEPARATOR separator
     if (DIRECTORY_SEPARATOR === "\\") {
         $preferences['server_path'] = str_replace('/', '\\', $preferences['server_path']);
         $image_path = str_replace('/', '\\', $image_path);
     }
     return self::_get_EE()->filemanager->create_thumb($image_path, $preferences);
 }
 /**
  * Return an array of all available sources
  * @return array
  */
 public function get_all_sources()
 {
     require_once PATH_THIRD . 'assets/sources/ee/source.ee.php';
     $ee_filedirs = Assets_ee_source::get_all_filedirs();
     $other_sources = $this->EE->db->order_by('name')->get('assets_sources')->result();
     $output = array();
     foreach ($ee_filedirs as $row) {
         $output[$row->name . '_ee_' . $row->site_id] = (object) array('type' => 'ee', 'id' => $row->id, 'name' => $row->name, 'site_id' => $row->site_id);
     }
     foreach ($other_sources as $row) {
         $output[$row->name . '_' . $row->name] = (object) array('type' => $row->source_type, 'id' => $row->source_id, 'name' => $row->name);
     }
     ksort($output);
     return $output;
 }