/** * metaWeblog.newMediaObject image upload * wp.uploadFile * * Supplied image is encoded into the struct as bits * * @see http://www.xmlrpc.com/metaWeblogApi#metaweblognewmediaobject * @see http://codex.wordpress.org/XML-RPC_wp#wp.uploadFile * * @param xmlrpcmsg XML-RPC Message * 0 blogid (string): Unique identifier of the blog the post will be added to. * Currently ignored in b2evo, in favor of the category. * 1 username (string): Login for a Blogger user who has permission to edit the given * post (either the user who originally created it or an admin of the blog). * 2 password (string): Password for said username. * 3 struct (struct) * - name : filename * - type : mimetype * - bits : base64 encoded file * @return xmlrpcresp XML-RPC Response */ function _wp_mw_newmediaobject($m) { global $Settings, $Plugins, $force_upload_forbiddenext; // CHECK LOGIN: /** * @var User */ if (!($current_User =& xmlrpcs_login($m, 1, 2))) { // Login failed, return (last) error: return xmlrpcs_resperror(); } // GET BLOG: /** * @var Blog */ if (!($Blog =& xmlrpcs_get_Blog($m, 0))) { // Login failed, return (last) error: return xmlrpcs_resperror(); } // CHECK PERMISSION: if (!$current_User->check_perm('files', 'add', false, $Blog->ID)) { // Permission denied return xmlrpcs_resperror(3); // User error 3 } logIO('Permission granted.'); if (!$Settings->get('upload_enabled')) { return xmlrpcs_resperror(2, 'Object upload not allowed'); } $xcontent = $m->getParam(3); // Get the main data - and decode it properly for the image - sorry, binary object logIO('Decoding content...'); $contentstruct = xmlrpc_decode_recurse($xcontent); $data = $contentstruct['bits']; $file_mimetype = isset($contentstruct['type']) ? $contentstruct['type'] : '(none)'; logIO('Received MIME type: ' . $file_mimetype); $overwrite = false; if (isset($contentstruct['overwrite'])) { $overwrite = (bool) $contentstruct['overwrite']; } logIO('Overwrite if exists: ' . ($overwrite ? 'yes' : 'no')); load_funcs('files/model/_file.funcs.php'); $filesize = evo_bytes($data); if (($maxfilesize = $Settings->get('upload_maxkb') * 1024) && $filesize > $maxfilesize) { return xmlrpcs_resperror(4, sprintf(T_('The file is too large: %s but the maximum allowed is %s.'), bytesreadable($filesize, false), bytesreadable($maxfilesize, false))); } logIO('File size is OK: ' . bytesreadable($filesize, false)); $FileRootCache =& get_FileRootCache(); $fm_FileRoot =& $FileRootCache->get_by_type_and_ID('collection', $Blog->ID, true); if (!$fm_FileRoot) { // fileRoot not found: return xmlrpcs_resperror(14, 'File root not found'); } $rf_filepath = $contentstruct['name']; logIO('Received filepath: ' . $rf_filepath); // Split into path + name: $filepath_parts = explode('/', $rf_filepath); $filename = array_pop($filepath_parts); logIO('Original file name: ' . $filename); // Validate and sanitize filename if ($error_filename = process_filename($filename, true)) { return xmlrpcs_resperror(5, $error_filename); } logIO('Sanitized file name: ' . $filename); // Check valid path parts: $rds_subpath = ''; foreach ($filepath_parts as $filepath_part) { if (empty($filepath_part) || $filepath_part == '.') { // self ref not useful continue; } if ($error = validate_dirname($filepath_part)) { // invalid relative path: logIO($error); return xmlrpcs_resperror(6, $error); } $rds_subpath .= $filepath_part . '/'; } logIO('Subpath: ' . $rds_subpath); // Create temporary file and insert contents into it. $tmpfile_name = tempnam(sys_get_temp_dir(), 'fmupload'); if ($tmpfile_name) { if (save_to_file($data, $tmpfile_name, 'wb')) { $image_info = @getimagesize($tmpfile_name); } else { return xmlrpcs_resperror(13, 'Error while writing to temp file.'); } } if (!empty($image_info)) { // This is an image file, let's check mimetype and correct extension if ($image_info['mime'] != $file_mimetype) { // Invalid file type $FiletypeCache =& get_FiletypeCache(); // Get correct file type based on mime type $correct_Filetype = $FiletypeCache->get_by_mimetype($image_info['mime'], false, false); $file_mimetype = $image_info['mime']; // Check if file type is known by us, and if it is allowed for upload. // If we don't know this file type or if it isn't allowed we don't change the extension! The current extension is allowed for sure. if ($correct_Filetype && $correct_Filetype->is_allowed()) { // A FileType with the given mime type exists in database and it is an allowed file type for current User // The "correct" extension is a plausible one, proceed... $correct_extension = array_shift($correct_Filetype->get_extensions()); $path_info = pathinfo($filename); $current_extension = $path_info['extension']; // change file extension to the correct extension, but only if the correct extension is not restricted, this is an extra security check! if (strtolower($current_extension) != strtolower($correct_extension) && !in_array($correct_extension, $force_upload_forbiddenext)) { // change the file extension to the correct extension $old_filename = $filename; $filename = $path_info['filename'] . '.' . $correct_extension; } } } } // Get File object for requested target location: $FileCache =& get_FileCache(); $newFile =& $FileCache->get_by_root_and_path($fm_FileRoot->type, $fm_FileRoot->in_type_ID, trailing_slash($rds_subpath) . $filename, true); if ($newFile->exists()) { if ($overwrite && $newFile->unlink()) { // OK, file deleted // Delete thumb caches from old location: logIO('Old file deleted'); $newFile->rm_cache(); } else { return xmlrpcs_resperror(8, sprintf(T_('The file «%s» already exists.'), $filename)); } } // Trigger plugin event if ($Plugins->trigger_event_first_false('AfterFileUpload', array('File' => &$newFile, 'name' => &$filename, 'type' => &$file_mimetype, 'tmp_name' => &$tmpfile_name, 'size' => &$filesize))) { // Plugin returned 'false'. // Abort upload for this file: @unlink($tmpfile_name); return xmlrpcs_resperror(16, 'File upload aborted by a plugin.'); } if (!mkdir_r($newFile->get_dir())) { // Dir didn't already exist and could not be created return xmlrpcs_resperror(9, 'Error creating sub directories: ' . $newFile->get_rdfs_rel_path()); } if (!@rename($tmpfile_name, $newFile->get_full_path())) { return xmlrpcs_resperror(13, 'Error while writing to file.'); } // chmod the file $newFile->chmod(); // Initializes file properties (type, size, perms...) $newFile->load_properties(); // Load meta data AND MAKE SURE IT IS CREATED IN DB: $newFile->meta == 'unknown'; $newFile->load_meta(true); // Resize and rotate logIO('Running file post-processing (resize and rotate)...'); prepare_uploaded_files(array($newFile)); logIO('Done'); $url = $newFile->get_url(); logIO('URL of new file: ' . $url); $struct = new xmlrpcval(array('file' => new xmlrpcval($filename, 'string'), 'url' => new xmlrpcval($url, 'string'), 'type' => new xmlrpcval($file_mimetype, 'string')), 'struct'); logIO('OK.'); return new xmlrpcresp($struct); }
* @author fplanque: Francois PLANQUE. * @author mbruneau: Marc BRUNEAU / PROGIDISTRI * * @version $Id: _filetype_list.view.php 3328 2013-03-26 11:44:11Z yura $ */ if (!defined('EVO_MAIN_INIT')) { die('Please, do not access this page directly.'); } global $rsc_url, $dispatcher; global $Session; // Create result set: $SQL = new SQL(); $SQL->SELECT('*'); $SQL->FROM('T_filetypes'); $Results = new Results($SQL->get(), 'ftyp_'); $Results->Cache =& get_FiletypeCache(); $Results->title = T_('File types list'); $Results->cols[] = array('th' => T_('Icon'), 'th_class' => 'shrinkwrap', 'td_class' => 'shrinkwrap', 'td' => '% {Obj}->get_icon() %'); if ($current_User->check_perm('options', 'edit', false)) { // We have permission to modify: $Results->cols[] = array('th' => T_('Extensions'), 'order' => 'ftyp_extensions', 'td' => '<strong><a href="' . $dispatcher . '?ctrl=filetypes&ftyp_ID=$ftyp_ID$&action=edit" title="' . T_('Edit this file type...') . '">$ftyp_extensions$</a></strong>'); } else { // View only: $Results->cols[] = array('th' => T_('Extensions'), 'order' => 'ftyp_extensions', 'td' => '<strong>$ftyp_extensions$</strong>'); } $Results->cols[] = array('th' => T_('Name'), 'order' => 'ftyp_name', 'td' => '$ftyp_name$'); $Results->cols[] = array('th' => T_('Mime type'), 'order' => 'ftyp_mimetype', 'td' => '$ftyp_mimetype$'); $Results->cols[] = array('th' => T_('View type'), 'order' => 'ftyp_viewtype', 'td' => '$ftyp_viewtype$'); /** * Display the permissions for the type file */
* * {@internal Below is a list of authors who have contributed to design/coding of this file: }} * @author fplanque: Francois PLANQUE. * @author mbruneau: Marc BRUNEAU / PROGIDISTRI * * @version $Id: file_types.ctrl.php 3328 2013-03-26 11:44:11Z yura $ */ if (!defined('EVO_MAIN_INIT')) { die('Please, do not access this page directly.'); } // Check minimum permission: $current_User->check_perm('options', 'view', true); param('action', 'string'); if (param('ftyp_ID', 'integer', '', true)) { // Load file type: $FiletypeCache =& get_FiletypeCache(); if (($edited_Filetype =& $FiletypeCache->get_by_ID($ftyp_ID, false)) === false) { // We could not find the file type to edit: unset($edited_Filetype); forget_param('ftyp_ID'); $Messages->add(sprintf(T_('Requested «%s» object does not exist any longer.'), T_('File type')), 'error'); $action = 'nil'; } } if (isset($edited_Filetype) && $edited_Filetype !== false) { // We are editing a division: $AdminUI->append_to_titlearea('«<a href="' . regenerate_url('action', 'action=edit') . '">' . $edited_Filetype->dget('name') . '</a>»'); } if ($demo_mode && !empty($action)) { $Messages->add('You cannot make any edits on this screen while in demo mode.', 'error'); $action = '';
/** * Copy file from source path to destination path (Used on import) * * @param string Path of source file * @param string FileRoot id string * @param string the upload dir relative path in the FileRoot * @param boolean Shall we check files add permission for current_User? * @return mixed NULL if import was impossible to complete for some reason (wrong fileroot ID, insufficient user permission, etc.) * file ID of new inserted file in DB */ function copy_file($file_path, $root_ID, $path, $check_perms = true) { global $current_User; $FileRootCache =& get_FileRootCache(); $fm_FileRoot =& $FileRootCache->get_by_ID($root_ID, true); if (!$fm_FileRoot) { // fileRoot not found: return NULL; } if ($check_perms && (!isset($current_User) || $current_User->check_perm('files', 'add', false, $fm_FileRoot))) { // Permission check required but current User has no permission to upload: return NULL; } // Let's get into requested list dir... $non_canonical_list_path = $fm_FileRoot->ads_path . $path; // Dereference any /../ just to make sure, and CHECK if directory exists: $ads_list_path = get_canonical_path($non_canonical_list_path); // check if the upload dir exists if (!is_dir($ads_list_path)) { // Create path mkdir_r($ads_list_path); } // Get file name from full path: $newName = basename($file_path); // validate file name if ($error_filename = process_filename($newName, true)) { // Not a valid file name or not an allowed extension: // Abort import for this file: return NULL; } // Check if the imported file type is an image, and if is an image then try to fix the file extension based on mime type // If the mime type is a known mime type and user has right to import files with this kind of file type, // this part of code will check if the file extension is the same as admin defined for this file type, and will fix it if it isn't the same // Note: it will also change the jpeg extensions to jpg. // this image_info variable will be used again to get file thumb $image_info = getimagesize($file_path); if ($image_info) { // This is an image, validate mimetype vs. extension: $image_mimetype = $image_info['mime']; $FiletypeCache =& get_FiletypeCache(); // Get correct file type based on mime type $correct_Filetype = $FiletypeCache->get_by_mimetype($image_mimetype, false, false); // Check if file type is known by us, and if it is allowed for upload. // If we don't know this file type or if it isn't allowed we don't change the extension! The current extension is allowed for sure. if ($correct_Filetype && $correct_Filetype->is_allowed()) { // A FileType with the given mime type exists in database and it is an allowed file type for current User // The "correct" extension is a plausible one, proceed... $correct_extension = array_shift($correct_Filetype->get_extensions()); $path_info = pathinfo($newName); $current_extension = $path_info['extension']; // change file extension to the correct extension, but only if the correct extension is not restricted, this is an extra security check! if (strtolower($current_extension) != strtolower($correct_extension) && !in_array($correct_extension, $force_upload_forbiddenext)) { // change the file extension to the correct extension $old_name = $newName; $newName = $path_info['filename'] . '.' . $correct_extension; } } } // Get File object for requested target location: $oldName = strtolower($newName); list($newFile, $oldFile_thumb) = check_file_exists($fm_FileRoot, $path, $newName, $image_info); $newName = $newFile->get('name'); if (!copy($file_path, $newFile->get_full_path())) { // Abort import for this file: return NULL; } // change to default chmod settings $newFile->chmod(NULL); // Refreshes file properties (type, size, perms...) $newFile->load_properties(); // Store File object into DB: if ($newFile->dbsave()) { // Success return $newFile->ID; } else { // Failure return NULL; } }
/** * Detect file type by extension and update 'file_type' in DB */ function set_file_type() { if (!empty($this->type)) { // Don't detect file type if File type is already defined return; } // AUDIO: $file_extension = $this->get_ext(); if (!empty($file_extension)) { // Set audio file type by extension: // Load all audio file types in cache $FiletypeCache =& get_FiletypeCache(); $FiletypeCache->load_where('ftyp_mimetype LIKE "audio/%"'); if (count($FiletypeCache->cache)) { foreach ($FiletypeCache->cache as $Filetype) { if (preg_match('#^audio/#', $Filetype->mimetype) && in_array($file_extension, $Filetype->get_extensions())) { // This is audio file $this->update_file_type('audio'); return; } } } } // IMAGE: // File type is still not defined, Try to detect image if ($this->get_image_size() !== false) { // This is image file $this->update_file_type('image'); return; } // OTHER: // File type is still not detected, Use this default $this->update_file_type('other'); return; }
/** * Get the File's Filetype object (or NULL). * * @return Filetype The Filetype object or NULL */ function &get_Filetype() { if (!isset($this->Filetype)) { // Create the filetype with the extension of the file if the extension exist in database if ($ext = $this->get_ext()) { // The file has an extension, load filetype object $FiletypeCache =& get_FiletypeCache(); $this->Filetype =& $FiletypeCache->get_by_extension(strtolower($ext), false); } if (!$this->Filetype) { // remember as being retrieved. $this->Filetype = false; } } $r = $this->Filetype ? $this->Filetype : NULL; return $r; }