/** * Set the full URL of the media folder * * @param string the full URL */ function set_media_url($url) { parent::set_param('media_url', 'string', trailing_slash($url)); }
/** * 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); }
// Check permission: $current_User->check_perm('files', 'add', true, $fileroot_ID); param('path', 'string'); param('oldfile', 'string'); param('newfile', 'string'); param('format', 'string'); $fileroot = explode('_', $fileroot_ID); $fileroot_type = $fileroot[0]; $fileroot_type_ID = empty($fileroot[1]) ? 0 : $fileroot[1]; $result = replace_old_file_with_new($fileroot_type, $fileroot_type_ID, $path, $newfile, $oldfile, false); $data = array(); if ($format == 'full_path_link') { // User link with full path to file $FileCache =& get_FileCache(); $new_File =& $FileCache->get_by_root_and_path($fileroot_type, $fileroot_type_ID, trailing_slash($path) . $newfile, true); $old_File =& $FileCache->get_by_root_and_path($fileroot_type, $fileroot_type_ID, trailing_slash($path) . $oldfile, true); $data['new'] = $new_File->get_view_link(); $data['old'] = $old_File->get_view_link(); } else { // Simple text format $data['new'] = $newfile; $data['old'] = $oldfile; } if ($result !== true) { // Send an error if it was created during the replacing $data['error'] = $result; } echo evo_json_encode($data); exit(0); case 'link_attachment': // The content for popup window to link the files to the items/comments
unset($file_contents); } else { $failedFiles[$k] = sprintf('Could not retrieve file. Error: %s (status %s). Used method: %s.', $info['error'], isset($info['status']) ? $info['status'] : '-', isset($info['used_method']) ? $info['used_method'] : '-'); } } } } // Process renaming/replacing of old versions: if (!empty($renamedFiles)) { foreach ($renamedFiles as $rKey => $rData) { $replace_old = param('Renamed_' . $rKey, 'string', null); if ($replace_old == "Yes") { // replace the old file with the new one $FileCache =& get_FileCache(); $newFile =& $FileCache->get_by_root_and_path($fm_FileRoot->type, $fm_FileRoot->in_type_ID, trailing_slash($path) . $renamedFiles[$rKey]['newName'], true); $oldFile =& $FileCache->get_by_root_and_path($fm_FileRoot->type, $fm_FileRoot->in_type_ID, trailing_slash($path) . $renamedFiles[$rKey]['oldName'], true); $new_filename = $newFile->get_name(); $old_filename = $oldFile->get_name(); $dir = $newFile->get_dir(); $oldFile->rm_cache(); $newFile->rm_cache(); $error_message = ''; // rename new uploaded file to temp file name $index = 0; $temp_filename = 'temp' . $index . '-' . $new_filename; while (file_exists($dir . $temp_filename)) { // find an unused filename $index++; $temp_filename = 'temp' . $index . '-' . $new_filename; } // @rename will overwrite a file with the same name if exists. In this case it shouldn't be a problem.
/** * Create a new directory with unique name * This creates a new directory below the given path with the given prefix and a random number * * @param string $dir base path to new directory * @param string $prefix prefix random number with this * @param integer $mode permissions to use * @return string path to created directory */ function pbm_tempdir($dir, $prefix = 'tmp', $mode = 0700) { // Add trailing slash $dir = trailing_slash($dir); do { $path = $dir . $prefix . mt_rand(); } while (!evo_mkdir($path, $mode)); return $path; }
/** * Delete any file that is older than 24 hours from the whole /cache folder (recursively) * except index.html files and hiddenfiles (starting with .) * * @static * * @return string empty string on success, error message otherwise. */ function prune_page_cache() { global $cache_path; load_funcs('tools/model/_system.funcs.php'); // check and try to repair cache folders system_check_caches(); $path = trailing_slash($cache_path); $first_error = ''; PageCache::deleteDirContent($path, $first_error); return $first_error; }
/** * Check if file exists in the target location with the given name. Used during file upload. * * @param FileRoot target file Root * @param string target path * @param string file name * @param array the new file image_info * @return array contains two elements * first elements is a new File object * second element is the existing file thumb, or empty string, if the file doesn't exists */ function check_file_exists($fm_FileRoot, $path, $newName, $image_info = NULL) { global $filename_max_length; // 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($path) . $newName, true); $num_ext = 0; $oldName = $newName; $oldFile_thumb = ""; while ($newFile->exists()) { // The file already exists in the target location! $num_ext++; $ext_pos = strrpos($newName, '.'); if ($num_ext == 1) { if ($image_info == NULL) { $image_info = getimagesize($newFile->get_full_path()); } $newName = substr_replace($newName, '-' . $num_ext . '.', $ext_pos, 1); if ($image_info) { $oldFile_thumb = $newFile->get_preview_thumb('fulltype'); } else { $oldFile_thumb = $newFile->get_size_formatted(); } } else { $replace_length = strlen('-' . ($num_ext - 1)); $newName = substr_replace($newName, '-' . $num_ext, $ext_pos - $replace_length, $replace_length); } if (strlen($newName) > $filename_max_length) { $newName = fix_filename_length($newName, strrpos($newName, '-')); if ($error_filename = process_filename($newName, true)) { // The file name is still not valid debug_die('Invalid file name has found during file exists check: ' . $error_filename); } } $newFile =& $FileCache->get_by_root_and_path($fm_FileRoot->type, $fm_FileRoot->in_type_ID, trailing_slash($path) . $newName, true); } return array($newFile, $oldFile_thumb); }
/** * Get current skin URL * @return string */ function get_url() { global $skins_url; return trailing_slash($skins_url . $this->folder); }
function parse_args() { $index_file = get_option_value('f'); $output_dir = get_option_value('d'); $output_url = get_option_value('u'); if (file_exists($output_dir)) { if (is_writable($output_dir) === false) { error("{$output_dir} is not writable."); } } else { error("output directory {$output_dir} does not exist."); } $paths = array('index_file' => $index_file, 'output_dir' => trailing_slash($output_dir), 'output_url' => trailing_slash($output_url)); return $paths; }
/** * Replace the old file with the new one * * @param string Root type: 'user', 'group' or 'collection' * @param integer ID of the user, the group or the collection the file belongs to... * @param string Subpath for this file/folder, relative the associated root, including trailing slash (if directory) * @param string Name of NEW file * @param string Name of OLD file * @param boolean TRUE to display message * @return boolean|string TRUE on success, otherwise an error message */ function replace_old_file_with_new($root_type, $root_in_type_ID, $path, $new_name, $old_name, $display_message = true) { $error_message = ''; if (empty($new_name)) { $error_message = T_('The new file name is empty!'); } elseif (empty($new_name)) { $error_message = T_('The old file name is empty!'); } if (empty($error_message)) { $FileCache =& get_FileCache(); $newFile =& $FileCache->get_by_root_and_path($root_type, $root_in_type_ID, trailing_slash($path) . $new_name, true); $oldFile =& $FileCache->get_by_root_and_path($root_type, $root_in_type_ID, trailing_slash($path) . $old_name, true); $new_filename = $newFile->get_name(); $old_filename = $oldFile->get_name(); $dir = $newFile->get_dir(); $oldFile->rm_cache(); $newFile->rm_cache(); // rename new uploaded file to temp file name $index = 0; $temp_filename = 'temp' . $index . '-' . $new_filename; while (file_exists($dir . $temp_filename)) { // find an unused filename $index++; $temp_filename = 'temp' . $index . '-' . $new_filename; } } // @rename will overwrite a file with the same name if exists. In this case it shouldn't be a problem. if (empty($error_message) && !@rename($newFile->get_full_path(), $dir . $temp_filename)) { // rename new file to temp file name failed $error_message = sprintf(T_('The new file could not be renamed to %s'), $temp_filename); } if (empty($error_message) && !@rename($oldFile->get_full_path(), $dir . $new_filename)) { // rename original file to the new file name failed $error_message = sprintf(T_('The original file could not be renamed to %s. The new file is now named %s.'), $new_filename, $temp_filename); } if (empty($error_message) && !@rename($dir . $temp_filename, $dir . $old_filename)) { // rename new file to the original file name failed $error_message = sprintf(T_('The new file could not be renamed to %s. It is now named %s.'), $old_filename, $temp_filename); } if ($display_message) { global $Messages; if (empty($error_message)) { $Messages->add(sprintf(T_('%s has been replaced with the new version!'), $old_filename), 'success'); } else { $Messages->add($error_message, 'error'); } } return empty($error_message) ? true : $error_message; }
$message .= '<label for="Yes_' . $newFile->ID . '">'; $message .= sprintf(T_("Replace the old version %s with the new version %s and keep old version as %s."), $oldFile_thumb, $newFile_thumb, $newName) . '</label><br />'; $message .= '<input type="radio" name="Renamed_' . $newFile->ID . '" value="No" id="No_' . $newFile->ID . '" checked="checked"/>'; $message .= '<label for="No_' . $newFile->ID . '">'; $message .= sprintf(T_("Don't touch the old version and keep the new version as %s."), $newName) . '</label><br />'; $message .= '</div>'; } $warning = ''; if ($Messages->count > 0) { // Some errors/info messages can be created during prepare_uploaded_files() $warning .= $Messages->display(NULL, NULL, false); } if (!empty($message)) { // There is an error message on uploading $FileCache =& get_FileCache(); $old_File =& $FileCache->get_by_root_and_path($fm_FileRoot->type, $fm_FileRoot->in_type_ID, trailing_slash($path) . $oldName, true); $message .= '<input type="hidden" name="renamedFiles[' . $newFile->ID . '][newName]" value="' . $newName . '" />' . '<input type="hidden" name="renamedFiles[' . $newFile->ID . '][oldName]" value="' . $oldName . '" />'; $message = array('text' => $message, 'status' => 'rename', 'file' => $newFile->get_preview_thumb('fulltype'), 'oldname' => $oldName, 'oldpath' => $old_File->get_root_and_rel_path()); } else { // Success uploading $message['text'] = $newFile->get_preview_thumb('fulltype'); $message['status'] = 'success'; } $message['newname'] = $newName; $message['newpath'] = $newFile->get_root_and_rel_path(); $message['warning'] = $warning; $message['path'] = rawurlencode($newFile->get_rdfp_rel_path()); if (!empty($new_Link)) { // Send also the link data if it was created $message['link_ID'] = $new_Link->ID; $message['link_url'] = $newFile->get_view_link();
function parse_args() { $args = getopt('f:d:u:'); if (is_null($args[f]) && is_null($args[d]) && is_null($args[u])) { error('Mandatory arguments: -f <index file path> -d <output directory path> -u <URL of sitemaps directory>'); } if (is_null($args[f])) { error('You must specify an index file name with the -f option.'); } if (is_null($args[d])) { error('You must specify a directory for the output file with the -d option.'); } if (is_null($args[u])) { error('You must specify a URL for the directory where the sitemaps will be kept with the -u option.'); } $index_file = $args[f]; $output_dir = $args[d]; $output_url = $args[u]; if (file_exists($output_dir)) { if (is_writable($output_dir) === false) { error("{$output_dir} is not writable."); } } else { error("output directory {$output_dir} does not exist."); } $paths = array('index_file' => $index_file, 'output_dir' => trailing_slash($output_dir), 'output_url' => trailing_slash($output_url)); return $paths; }
/** * Create a directory recursively. * * NOTE: this can be done with the "recursive" param in PHP5 * * @todo dh> simpletests for this (especially for open_basedir) * * @param string directory name * @param integer permissions * @return boolean */ function mkdir_r($dirName, $chmod = NULL) { if (is_dir($dirName)) { // already exists: return true; } if ($chmod === NULL) { global $Settings; $chmod = $Settings->get('fm_default_chmod_dir'); } /* if( version_compare(PHP_VERSION, 5, '>=') ) { return mkdir( $dirName, $chmod, true ); } */ $dirName = trailing_slash($dirName); $parts = array_reverse(explode('/', $dirName)); $loop_dir = $dirName; $create_dirs = array(); foreach ($parts as $part) { if (!strlen($part)) { continue; } // We want to create this dir: array_unshift($create_dirs, $loop_dir); $loop_dir = substr($loop_dir, 0, 0 - strlen($part) - 1); if (is_dir($loop_dir)) { // found existing dir: foreach ($create_dirs as $loop_dir) { if (!@mkdir($loop_dir, octdec($chmod))) { return false; } } return true; } } return true; }
function get_directory_files($path_type = 'relative') { global $Settings; $path = trailing_slash($this->_adfp_full_path); if ($dir = @opendir($path)) { // Scan directory and list all files $filenames = array(); while (($file = readdir($dir)) !== false) { if ($file == '.' || $file == '..' || $file == $Settings->get('evocache_foldername')) { // Invalid file continue; } // sam2kb> TODO: Do we need to process directories recursively? if (!is_dir($path . $file)) { switch ($path_type) { case 'basename': $filenames[] = $file; break; case 'relative': $filenames[] = trailing_slash($this->_rdfp_rel_path) . $file; break; case 'absolute': $filenames[] = $path . $file; break; } } } closedir($dir); if (!empty($filenames)) { return $filenames; } } return false; }
function isfile($a, $path = '', $show_errors = TRUE, $fatal = FALSE) { $path = trailing_slash($path); if (is_array($a)) { foreach ($a as $file) { if (!file_exists($path . $file)) { if ($show_errors === TRUE) { error('isfile()', '`' . $path . $file . '` did not exist', $fatal); } return FALSE; } } return TRUE; } else { if (is_string($a)) { if (!file_exists($path . $a)) { if ($show_errors === TRUE) { error('isfile()', '`' . $path . $file . '` did not exist', $fatal); } return FALSE; } return TRUE; } } if ($show_errors) { error('isfile()', 'was provided a non-string, non-array', $fatal); } return FALSE; }