예제 #1
0
 /**
  * Upload a file into the folder with the id.
  *
  * @param int    $folder_id
  * @param string $local_path if set, will simulate an upload from local path
  * @param string $file_name filename to use for simulated uploads
  *
  * @throws Exception
  * @return array|mixed
  */
 public function upload_file($folder_id, $local_path = "", $file_name = "")
 {
     try {
         $folder_row = $this->EE->assets_lib->get_folder_row_by_id($folder_id);
     } catch (Exception $error) {
         return array('error' => $error->getMessage());
     }
     $server_path = $this->get_folder_server_path($folder_row->full_path, $folder_row);
     if (!$server_path) {
         return array('error' => lang('invalid_filedir_path'));
     }
     if (empty($local_path)) {
         // upload the file and drop it in the temporary folder
         $uploader = new qqFileUploader();
         // make sure a file was uploaded
         if (!$uploader->file) {
             return array('error' => lang('no_files'));
         }
         $size = $uploader->file->getSize();
         // make sure the file isn't empty
         if (!$size) {
             return array('error' => lang('empty_file'));
         }
         $file_name = $uploader->file->getName();
         if (!Assets_helper::is_allowed_file_name($file_name)) {
             throw new Exception(lang('invalid_file_name'));
         }
         $file_path = Assets_helper::get_temp_file(pathinfo($file_name, PATHINFO_EXTENSION));
         $uploader->file->save($file_path);
     } else {
         if (!Assets_helper::is_allowed_file_name($file_name)) {
             throw new Exception(lang('invalid_file_name'));
         }
         $file_path = $local_path;
     }
     // the file is being saved in a temporary location, so that the workflow here is manageable
     // if we didn't do this, that would mean that all sources must implement their own uploader as well
     // which would have been an overkill.
     $result = $this->_do_upload_in_folder($folder_row, $file_path, $file_name);
     $return_prompt = FALSE;
     // naming conflict. create the new filename and ask user what to do
     if (isset($result['prompt'])) {
         $new_file_name = $this->get_name_replacement($folder_row, $uploader->file->getName());
         $return_prompt = $result;
         $result = $this->_do_upload_in_folder($folder_row, $file_path, $new_file_name);
     }
     if (isset($result['success'])) {
         $filename = pathinfo($result['path'], PATHINFO_BASENAME);
         $data = array('folder_id' => $folder_id, 'source_type' => $folder_row->source_type, 'source_id' => $folder_row->source_id, 'filedir_id' => $folder_row->filedir_id, 'file_name' => $filename, 'kind' => Assets_helper::get_kind($filename));
         $this->EE->db->insert('assets_files', $data);
         $file_id = $this->EE->db->insert_id();
         $file = $this->get_file($file_id);
         // For EE files, the dimensions might have changed due to filedir restrictions, so we have to use the returned path
         if ($file instanceof Assets_ee_file) {
             $this->update_file_info($file, $result['path']);
         } else {
             $this->update_file_info($file, $file_path);
         }
         @unlink($file_path);
         $this->EE->assets_lib->update_file_search_keywords($file_id);
         if (!$return_prompt) {
             return array('success' => TRUE, 'file_id' => $file_id);
         } else {
             $return_prompt['additional_info'] = $folder_id . ':' . $file_id;
             $return_prompt['new_file_id'] = $file_id;
             return $return_prompt;
         }
     } else {
         @unlink($file_path);
         return $result;
     }
 }
예제 #2
0
 /**
  * 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;
     }
 }
예제 #3
0
 /**
  * Register a file using a folder id and filename
  * @param $folder_id
  * @param $filename
  * @return mixed
  * @throws Exception
  */
 public function register_file($folder_id, $filename)
 {
     $folder_row = $this->get_folder_row_by_id($folder_id);
     $source = $this->instantiate_source_type($folder_row);
     if ($this->get_file_id_by_folder_id_and_name($folder_id, $filename)) {
         return TRUE;
     }
     $data = array('file_name' => $filename, 'source_type' => $folder_row->source_type, 'source_id' => $folder_row->source_id, 'filedir_id' => $folder_row->filedir_id, 'folder_id' => $folder_id, 'kind' => Assets_helper::get_kind($filename));
     $this->EE->db->insert('assets_files', $data);
     $file_id = $this->EE->db->insert_id();
     $file = $source->get_file($file_id);
     if ($file->exists()) {
         $file_id = $source->update_file_info($file);
         $this->update_file_search_keywords($file_id);
         return $file_id;
     } else {
         return FALSE;
     }
 }
예제 #4
0
 /**
  * Perform indexing
  * @param $session_id int
  * @param $offset
  * @return boolean
  */
 public function process_index($session_id, $offset)
 {
     $search_parameters = array('session_id' => $session_id, 'source_type' => $this->get_source_type(), 'source_id' => $this->get_source_id(), 'offset' => $offset);
     $index_entry = $this->_get_index_entry($search_parameters);
     // can't find the file. awkward. avoid eye contact and return next offset
     if (empty($index_entry)) {
         return FALSE;
     }
     $file = $index_entry->uri;
     $size = $index_entry->filesize;
     $file_indexed = FALSE;
     if ($this->_is_extension_allowed(pathinfo($file, PATHINFO_EXTENSION))) {
         $parts = explode('/', $file);
         $file_name = array_pop($parts);
         $search_full_path = join('/', $parts) . '/';
         if ($search_full_path == '/') {
             $search_full_path = '';
         }
         $folder_search = array('source_type' => $this->get_source_type(), 'source_id' => $this->get_source_id(), 'full_path' => $search_full_path);
         // check for parent by path segment in table
         $parent_row = $this->_find_folder($folder_search);
         if (empty($parent_row)) {
             return FALSE;
         }
         $folder_id = $parent_row->folder_id;
         $file_id = $this->EE->assets_lib->get_file_id_by_folder_id_and_name($folder_id, $file_name);
         // new file?
         if (empty($file_id)) {
             $data = array('folder_id' => $folder_id, 'source_type' => $this->get_source_type(), 'source_id' => $this->get_source_id(), 'file_name' => $file_name, 'kind' => Assets_helper::get_kind($file));
             $file_id = $this->_store_file($data);
             $this->EE->db->update('assets_index_data', array('record_id' => $file_id), $search_parameters);
         } else {
             $this->EE->db->update('assets_index_data', array('record_id' => $file_id), $search_parameters);
         }
         $file_indexed = $file_id;
     }
     // add image dimensions and size as well
     if ($file_indexed) {
         $settings = $this->settings();
         $this->_s3_set_creds($settings->access_key_id, $settings->secret_access_key);
         $this->s3->setEndpoint($this->get_endpoint_by_location($settings->location));
         $info = $this->s3->getObjectInfo($this->settings()->bucket, $this->_get_path_prefix() . $file);
         $data = array('size' => $size);
         if (is_array($info)) {
             $data['date_modified'] = $info['time'];
         }
         $file_row = $this->EE->assets_lib->get_file_row_by_id($file_id);
         if (!$file_row->date) {
             $data['date'] = $file_row->date_modified ? $file_row->date_modified : $data['date_modified'];
         }
         if ($file_row->kind == 'image' && $size != $file_row->size) {
             $this->_perform_image_actions($file, $file_id, $this->settings()->bucket);
         }
         $this->_update_file($data, $file_id);
     }
     return TRUE;
 }
예제 #5
0
 /**
  * Perform indexing
  * @param $session_id int
  * @param $offset
  * @return boolean
  */
 public function process_index($session_id, $offset)
 {
     $search_parameters = array('session_id' => $session_id, 'source_type' => $this->get_source_type(), 'source_id' => $this->get_source_id(), 'offset' => $offset);
     $index_entry = $this->_get_index_entry($search_parameters);
     // can't find the file. awkward. avoid eye contact and return next offset
     if (empty($index_entry)) {
         return FALSE;
     }
     $filedir = $this->settings();
     $upload_folder_path = $filedir->server_path;
     $file = $index_entry->uri;
     // get the relevant path - the part that is not shared with the upload folder
     $relevant_path = Assets_helper::normalize_path(substr($file, strlen($upload_folder_path)));
     $file_indexed = FALSE;
     if ($this->_is_extension_allowed(pathinfo($file, PATHINFO_EXTENSION))) {
         $parts = explode('/', $relevant_path);
         $file_name = array_pop($parts);
         $search_full_path = join('/', $parts) . '/';
         if ($search_full_path == '/') {
             $search_full_path = '';
         }
         $folder_search = array('source_type' => $this->get_source_type(), 'filedir_id' => $this->get_source_id(), 'full_path' => $search_full_path);
         // check for parent by path segment in table
         $parent_row = $this->_find_folder($folder_search);
         if (empty($parent_row)) {
             return FALSE;
         }
         $folder_id = $parent_row->folder_id;
         $file_id = $this->EE->assets_lib->get_file_id_by_folder_id_and_name($folder_id, $file_name);
         // new file?
         if (empty($file_id)) {
             $data = array('folder_id' => $folder_id, 'source_type' => $this->get_source_type(), 'filedir_id' => $this->get_source_id(), 'file_name' => $file_name, 'kind' => Assets_helper::get_kind($file));
             $file_id = $this->_store_file($data);
             $this->EE->db->update('assets_index_data', array('record_id' => $file_id), $search_parameters);
         } else {
             $this->EE->db->update('assets_index_data', array('record_id' => $file_id), $search_parameters);
         }
         $file_indexed = $file_id;
     }
     // add image dimensions and size as well
     if ($file_indexed) {
         $data = array('size' => filesize($file), 'date_modified' => filemtime($file));
         $file_row = $this->EE->assets_lib->get_file_row_by_id($file_id);
         if (!$file_row->date) {
             $data['date'] = $file_row->date_modified ? $file_row->date_modified : $data['date_modified'];
         }
         if (Assets_helper::get_kind($file) == 'image') {
             list($width, $height) = getimagesize($file);
             $data['width'] = $width;
             $data['height'] = $height;
             @$this->_create_thumbnails($file, $this->get_source_id());
         }
         $this->_update_file($data, $file_indexed);
     }
     return TRUE;
 }