Exemple #1
0
 /** @access private */
 function _generate_temp_name($filename, $suffix = '')
 {
     $id = sha1(uniqid(mt_rand(), true));
     list(, $extension) = get_filename_parts($filename);
     if (!empty($extension)) {
         $extension = strtolower(".{$extension}");
     }
     return WEB_TEMP . "{$id}{$suffix}{$extension}";
 }
/**
 * Gets a unique version of the given asset filename.
 *
 * Returns a version of the given filename that has been modified (if 
 * necessary) so that it does not conflict with the filename of an existing
 * asset.
 * 
 * @author Eric Naeseth <*****@*****.**>
 * @since Reason 4.0 beta 8
 * @param string $filename the filename to make unique
 * @param int $site_id the ID of the Reason minisite in which the filename must
 *        be unique
 * @param int $skip the ID of an asset to ignore in checking for a unique
 *        filename. If the caller is trying to overwrite an existing asset
 *        file, the ID of that asset should be passed as $skip.
 * @return unique $filename
 */
function reason_get_unique_asset_filename($filename, $site_id, $skip = null)
{
    $selector = new entity_selector($site_id);
    $selector->add_type(id_of("asset"));
    $assets = $selector->run_one("", "All");
    $existing_filenames = array();
    foreach ((array) $assets as $asset) {
        if (!$skip || $asset->id() != $skip) {
            $existing_filenames[] = $asset->get_value("file_name");
        }
    }
    list($base, $extension) = get_filename_parts($filename);
    if (!empty($extension)) {
        $extension = ".{$extension}";
    }
    $index = 1;
    while (in_array($filename, $existing_filenames)) {
        $index++;
        $filename = "{$base}_{$index}{$extension}";
    }
    return $filename;
}