escape() public method

Escape the filenames, any non-word characters will be replaced by an underscore.
public escape ( string $filename ) : string
$filename string the orginal filename
return string the escaped safe filename
Beispiel #1
0
 /**
  * Copy a file from source to destination. If unique == true, then if
  * the destination exists, it will be renamed by appending an increamenting 
  * counting number.
  * @param string $source where the file is from, full path to the files required
  * @param string $destination_file name of the new file, just the filename
  * @param string $destination_dir where the files, just the destination dir,
  * e.g., /www/html/gallery/
  * @param boolean $unique create unique destination file if true.
  * @return string the new copied filename, else error if anything goes bad.
  */
 function copyFile($source, $destination_dir, $destination_file, $unique = true)
 {
     if (!(file_exists($source) && is_file($source))) {
         return FILE_ERROR_NO_SOURCE;
     }
     $destination_dir = Files::fixPath($destination_dir);
     if (!is_dir($destination_dir)) {
         return FILE_ERROR_DST_DIR_FAILED;
     }
     $filename = Files::escape($destination_file);
     if ($unique) {
         $dotIndex = strrpos($destination_file, '.');
         $ext = '';
         if (is_int($dotIndex)) {
             $ext = substr($destination_file, $dotIndex);
             $base = substr($destination_file, 0, $dotIndex);
         }
         $counter = 0;
         while (is_file($destination_dir . $filename)) {
             $counter++;
             $filename = $base . '_' . $counter . $ext;
         }
     }
     if (!copy($source, $destination_dir . $filename)) {
         return FILE_ERROR_COPY_FAILED;
     }
     //verify that it copied, new file must exists
     if (is_file($destination_dir . $filename)) {
         return $filename;
     } else {
         return FILE_ERROR_COPY_FAILED;
     }
 }
 /**
  * Create new directories.
  * If in safe_mode, nothing happens.
  * @return boolean true if created, false otherwise.
  */
 function processNewDir()
 {
     if ($this->config['safe_mode'] == true) {
         return false;
     }
     if (isset($_GET['newDir']) && isset($_GET['dir'])) {
         $newDir = rawurldecode($_GET['newDir']);
         $dir = rawurldecode($_GET['dir']);
         $path = Files::makePath($this->getImagesDir(), $dir);
         $fullpath = Files::makePath($path, Files::escape($newDir));
         if (is_dir($fullpath)) {
             return false;
         }
         return Files::createFolder($fullpath);
     }
 }
 /**
  * Get a unique filename. If the file exists, the filename
  * base is appended with an increasing integer.
  * @param string $relative the relative filename to the base_dir
  * @return string a unique filename in the current path
  */
 function getUniqueFilename($relative)
 {
     $fullpath = $this->manager->getFullPath($relative);
     $pathinfo = pathinfo($fullpath);
     $path = Files::fixPath($pathinfo['dirname']);
     $file = Files::escape($pathinfo['basename']);
     $filename = $file;
     $dotIndex = strrpos($file, '.');
     $ext = '';
     if (is_int($dotIndex)) {
         $ext = substr($file, $dotIndex);
         $base = substr($file, 0, $dotIndex);
     }
     $counter = 0;
     while (is_file($path . $filename)) {
         $counter++;
         $filename = $base . '_' . $counter . $ext;
     }
     return $filename;
 }
 /**
  * Renames files if certain GET variables are set
  * @return bool
  */
 function processRenames()
 {
     if (!empty($_GET['rename']) && !empty($_GET['renameTo'])) {
         // new file name (without path and extension)
         $newName = Files::escape(rawurldecode($_GET['renameTo']));
         $newName = str_replace('.', '', $newName);
         // path to file (from base images directory)
         $oldName = rawurldecode($_GET['rename']);
         // strip parent dir ("..") to avoid escaping from base directiory
         $oldName = preg_replace('#\\.\\.#', '', $oldName);
         if (is_dir($oldPath = Files::makeFile($this->getImagesDir(), $_GET['dir'] . $oldName))) {
             $newPath = Files::makeFile($this->getImagesDir(), $_GET['dir'] . $newName);
             return Files::rename($oldPath, $newPath);
         } else {
             // path to old file
             $oldPath = Files::makeFile($this->getImagesDir(), $oldName);
             $ret = Files::renameFile($oldPath, $newName);
             if ($ret === true) {
                 // delete old thumbnail
                 Files::delFile($this->getThumbname($oldPath));
             }
         }
         return $ret;
     }
     return null;
 }
 /**
  * Create new directories.
  * If in safe_mode, nothing happens.
  * @return boolean true if created, false otherwise.
  */
 function processNewDir()
 {
     if ($this->config['safe_mode']) {
         return false;
     }
     if (isset($_GET['newDir']) && isset($_GET['dir'])) {
         $newDir = rawurldecode($_GET['newDir']);
         $dir = rawurldecode($_GET['dir']);
         $path = Files::makePath($this->getBaseDir(), $dir);
         $fullpath = Files::makePath($path, Files::escape($newDir));
         if (is_dir($fullpath)) {
             return false;
         } else {
             //adding to the DB
             // now the create_unexisting_directory will create the folder
             //$result = Files::createFolder($fullpath);
             global $_course;
             if (isset($_course) && !empty($_course) && isset($_course['code'])) {
                 //@todo make this str to functions
                 $base_dir = substr($path, 0, strpos($path, '/document/') + 9);
                 //
                 $new_dir = substr($fullpath, strlen($base_dir), -1);
                 //
                 create_unexisting_directory($_course, api_get_user_id(), api_get_session_id(), 0, 0, $base_dir, $new_dir, $newDir);
                 $doc_id = DocumentManager::get_document_id($_course, $new_dir);
                 api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', api_get_user_id(), null, null, null, null, api_get_session_id());
             } else {
                 return Files::createFolder($fullpath);
             }
             return true;
         }
     }
 }
Beispiel #6
0
 /**
  * Create new directories.
  * If in safe_mode, nothing happens.
  * @return boolean true if created, false otherwise.
  */
 function processNewDir()
 {
     if ($this->config['safe_mode'] == true) {
         return false;
     }
     if (isset($_GET['newDir']) && isset($_GET['dir'])) {
         $newDir = rawurldecode($_GET['newDir']);
         $dir = rawurldecode($_GET['dir']);
         $path = Files::makePath($this->getBaseDir(), $dir);
         $fullpath = Files::makePath($path, Files::escape($newDir));
         if (is_dir($fullpath)) {
             return false;
         }
         return Files::createFolder($fullpath, $this->config['modx']['folder_permissions']);
         //modified for MODx
     }
 }