Index: branches/5.2.x/core/units/helpers/file_helper.php =================================================================== diff -u -N -r14619 -r14681 --- branches/5.2.x/core/units/helpers/file_helper.php (.../file_helper.php) (revision 14619) +++ branches/5.2.x/core/units/helpers/file_helper.php (.../file_helper.php) (revision 14681) @@ -1,6 +1,6 @@ Application->ConfigValue($object->Prefix.'_MaxImageCount'); // file count equals to image count (temporary measure) @@ -46,8 +48,10 @@ * Saves newly uploaded images to external image table * * @param kCatDBItem $object + * @return void + * @access public */ - function SaveItemFiles(&$object) + public function SaveItemFiles(&$object) { $table_name = $this->Application->getUnitOption('#file', 'TableName'); $max_file_count = $this->Application->getUnitOption($object->Prefix, 'FileCount'); // $this->Application->ConfigValue($object->Prefix.'_MaxImageCount'); @@ -104,11 +108,13 @@ } /** - * Preserves cloned item images/files to be rewrited with original item images/files + * Preserves cloned item images/files to be rewritten with original item images/files * * @param Array $field_values + * @return void + * @access public */ - function PreserveItemFiles(&$field_values) + public function PreserveItemFiles(&$field_values) { foreach ($field_values as $field_name => $field_value) { if (!is_array($field_value)) continue; @@ -125,12 +131,14 @@ * * @param string $prefix * @param bool $is_image + * @return void + * @access public */ - function createItemFiles($prefix, $is_image = false) + public function createItemFiles($prefix, $is_image = false) { $items_info = $this->Application->GetVar($prefix); if ($items_info) { - list ($id, $fields_values) = each($items_info); + list (, $fields_values) = each($items_info); $this->createUploadFields($prefix, $fields_values, $is_image); } else { @@ -144,8 +152,10 @@ * @param string $prefix * @param Array $fields_values * @param bool $is_image + * @return void + * @access public */ - function createUploadFields($prefix, $fields_values, $is_image = false) + public function createUploadFields($prefix, $fields_values, $is_image = false) { $field_options = Array ( 'type' => 'string', @@ -238,8 +248,10 @@ * Downloads file to user * * @param string $filename + * @return void + * @access public */ - function DownloadFile($filename) + public function DownloadFile($filename) { header('Content-type: ' . kUtil::mimeContentType($filename)); header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); @@ -253,8 +265,9 @@ * * @param string $path * @return bool + * @access public */ - function CheckFolder($path) + public function CheckFolder($path) { $result = true; @@ -292,8 +305,9 @@ * @param string $source * @param string $destination * @return bool + * @access public */ - function copyFolderRecursive($source, $destination) + public function copyFolderRecursive($source, $destination) { if ( substr($source, -1) == DIRECTORY_SEPARATOR ) { $source = substr($source, 0, -1); @@ -337,8 +351,9 @@ * @param string $source * @param string $destination * @return bool + * @access public */ - function copyFolder($source, $destination) + public function copyFolder($source, $destination) { if ( substr($source, -1) == DIRECTORY_SEPARATOR ) { $source = substr($source, 0, -1); @@ -376,8 +391,9 @@ * * @param string $url * @return string + * @access public */ - function pathToUrl($url) + public function pathToUrl($url) { $url = str_replace(DIRECTORY_SEPARATOR, '/', preg_replace('/^' . preg_quote(FULL_PATH, '/') . '(.*)/', '\\1', $url, 1)); $url = implode('/', array_map('rawurlencode', explode('/', $url))); @@ -390,8 +406,9 @@ * * @param string $url * @return string + * @access public */ - function urlToPath($url) + public function urlToPath($url) { $base_url = rtrim($this->Application->BaseURL(), '/'); @@ -401,4 +418,31 @@ return str_replace('/', DIRECTORY_SEPARATOR, rawurldecode($path)); } + + /** + * Ensures, that new file will not overwrite any of previously created files with same name + * + * @param string $path + * @param string $name + * @param Array $forbidden_names + * @return string + */ + public function ensureUniqueFilename($path, $name, $forbidden_names = Array()) + { + $parts = pathinfo($name); + $ext = '.' . $parts['extension']; + $filename = mb_substr($parts['basename'], 0, -mb_strlen($ext)); + $new_name = $filename . $ext; + + while ( file_exists($path . '/' . $new_name) || in_array(rtrim($path, '/') . '/' . $new_name, $forbidden_names) ) { + if ( preg_match('/(' . preg_quote($filename, '/') . '_)([0-9]*)(' . preg_quote($ext, '/') . ')/', $new_name, $regs) ) { + $new_name = $regs[1] . ($regs[2] + 1) . $regs[3]; + } + else { + $new_name = $filename . '_1' . $ext; + } + } + + return $new_name; + } } \ No newline at end of file