예제 #1
0
 /**
  * Delete a folder.
  *
  * @param   string  $path  The path to the folder to delete.
  *
  * @return  boolean  True on success.
  *
  * @since   1.0
  * @throws  FilesystemException
  * @throws  \UnexpectedValueException
  */
 public static function delete($path)
 {
     @set_time_limit(ini_get('max_execution_time'));
     // Sanity check
     if (!$path) {
         // Bad programmer! Bad Bad programmer!
         throw new FilesystemException(__METHOD__ . ': You can not delete a base directory.');
     }
     try {
         // Check to make sure the path valid and clean
         $path = Path::clean($path);
     } catch (\UnexpectedValueException $e) {
         throw $e;
     }
     // Is this really a folder?
     if (!is_dir($path)) {
         throw new \UnexpectedValueException(sprintf('%1$s: Path is not a folder. Path: %2$s', __METHOD__, $path));
     }
     // Remove all the files in folder if they exist; disable all filtering
     $files = self::files($path, '.', false, true, array(), array());
     if (!empty($files)) {
         if (File::delete($files) !== true) {
             // File::delete throws an error
             return false;
         }
     }
     // Remove sub-folders of folder; disable all filtering
     $folders = self::folders($path, '.', false, true, array(), array());
     foreach ($folders as $folder) {
         if (is_link($folder)) {
             // Don't descend into linked directories, just delete the link.
             if (File::delete($folder) !== true) {
                 // File::delete throws an error
                 return false;
             }
         } elseif (self::delete($folder) !== true) {
             // Folder::delete throws an error
             return false;
         }
     }
     // In case of restricted permissions we zap it one way or the other
     // as long as the owner is either the webserver or the ftp.
     if (@rmdir($path)) {
         return true;
     } else {
         throw new FilesystemException(sprintf('%1$s: Could not delete folder. Path: %2$s', __METHOD__, $path));
     }
 }
예제 #2
0
 public function deleteOldAvatar($item_id, $item_type)
 {
     $avatar = $this->getAvatar($item_id, $item_type);
     if ($avatar) {
         echo JPATH_SITE . '/src/Cobalt/media/avatars/' . $avatar;
         File::delete(JPATH_SITE . '/src/Cobalt/media/avatars/' . $avatar);
     }
 }
예제 #3
0
 /**
  * Test write method
  *
  * @return void
  *
  * @covers        Joomla\Filesystem\File::write
  * @since         1.0
  */
 public function testWrite()
 {
     $name = 'tempFile';
     $path = __DIR__;
     $data = 'Lorem ipsum dolor sit amet';
     // Create a file on pre existing path.
     $this->assertThat(File::write($path . '/' . $name, $data), $this->isTrue(), 'Line:' . __LINE__ . ' File should be written successfully.');
     // Create a file on pre existing path by using streams.
     $this->assertThat(File::write($path . '/' . $name, $data, true), $this->isTrue(), 'Line:' . __LINE__ . ' File should be written successfully.');
     // Create a file on non-existing path.
     $this->assertThat(File::write($path . '/TempFolder/' . $name, $data), $this->isTrue(), 'Line:' . __LINE__ . ' File should be written successfully.');
     // Removes file and folder.
     File::delete($path . '/' . $name);
     Folder::delete($path . '/TempFolder');
 }
 /**
  * Delete a folder.
  *
  * @param   string  $path  The path to the folder to delete.
  *
  * @return  boolean  True on success.
  *
  * @since   11.1
  */
 public static function delete($path)
 {
     @set_time_limit(ini_get('max_execution_time'));
     // Sanity check
     if (!$path) {
         // Bad programmer! Bad Bad programmer!
         Log::add(__METHOD__ . ': ' . Text::_('JLIB_FILESYSTEM_ERROR_DELETE_BASE_DIRECTORY'), Log::WARNING, 'jerror');
         return false;
     }
     $FTPOptions = ClientHelper::getCredentials('ftp');
     // Check to make sure the path valid and clean
     $path = Path::clean($path);
     // Is this really a folder?
     if (!is_dir($path)) {
         Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_PATH_IS_NOT_A_FOLDER', $path), Log::WARNING, 'jerror');
         return false;
     }
     // Remove all the files in folder if they exist; disable all filtering
     $files = self::files($path, '.', false, true, array(), array());
     if (!empty($files)) {
         jimport('joomla.filesystem.file');
         if (File::delete($files) !== true) {
             // File::delete throws an error
             return false;
         }
     }
     // Remove sub-folders of folder; disable all filtering
     $folders = self::folders($path, '.', false, true, array(), array());
     foreach ($folders as $folder) {
         if (is_link($folder)) {
             // Don't descend into linked directories, just delete the link.
             jimport('joomla.filesystem.file');
             if (File::delete($folder) !== true) {
                 // File::delete throws an error
                 return false;
             }
         } elseif (self::delete($folder) !== true) {
             // JFolder::delete throws an error
             return false;
         }
     }
     if ($FTPOptions['enabled'] == 1) {
         // Connect the FTP client
         $ftp = Ftp::getInstance($FTPOptions['host'], $FTPOptions['port'], array(), $FTPOptions['user'], $FTPOptions['pass']);
     }
     // In case of restricted permissions we zap it one way or the other
     // as long as the owner is either the webserver or the ftp.
     if (@rmdir($path)) {
         $ret = true;
     } elseif ($FTPOptions['enabled'] == 1) {
         // Translate path and delete
         $path = Path::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/');
         // FTP connector throws an error
         $ret = $ftp->delete($path);
     } else {
         Log::add(Text::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_DELETE', $path), Log::WARNING, 'jerror');
         $ret = false;
     }
     return $ret;
 }
 /**
  * Method to determine if script owns the path.
  *
  * @param   string  $path  Path to check ownership.
  *
  * @return  boolean  True if the php script owns the path passed.
  *
  * @since   11.1
  */
 public static function isOwner($path)
 {
     jimport('joomla.filesystem.file');
     $tmp = md5(mt_rand());
     $ssp = ini_get('session.save_path');
     $jtp = JPATH_SITE . '/tmp';
     // Try to find a writable directory
     $dir = is_writable('/tmp') ? '/tmp' : false;
     $dir = !$dir && is_writable($ssp) ? $ssp : false;
     $dir = !$dir && is_writable($jtp) ? $jtp : false;
     if ($dir) {
         $test = $dir . '/' . $tmp;
         // Create the test file
         $blank = '';
         File::write($test, $blank, false);
         // Test ownership
         $return = fileowner($test) == fileowner($path);
         // Delete the test file
         File::delete($test);
         return $return;
     }
     return false;
 }
 /**
  * Apply the patches
  *
  * @throw  RuntimeException
  *
  * @return integer the number of files patched
  */
 public function apply()
 {
     foreach ($this->patches as $patch) {
         // Separate the input into lines
         $lines = self::splitLines($patch['udiff']);
         // Loop for each header
         while (self::findHeader($lines, $src, $dst)) {
             $done = false;
             if ($patch['strip'] === null) {
                 $src = $patch['root'] . preg_replace('#^([^/]*/)*#', '', $src);
                 $dst = $patch['root'] . preg_replace('#^([^/]*/)*#', '', $dst);
             } else {
                 $src = $patch['root'] . preg_replace('#^([^/]*/){' . (int) $patch['strip'] . '}#', '', $src);
                 $dst = $patch['root'] . preg_replace('#^([^/]*/){' . (int) $patch['strip'] . '}#', '', $dst);
             }
             // Loop for each hunk of differences
             while (self::findHunk($lines, $src_line, $src_size, $dst_line, $dst_size)) {
                 $done = true;
                 // Apply the hunk of differences
                 $this->applyHunk($lines, $src, $dst, $src_line, $src_size, $dst_line, $dst_size);
             }
             // If no modifications were found, throw an exception
             if (!$done) {
                 throw new RuntimeException('Invalid Diff');
             }
         }
     }
     // Initialize the counter
     $done = 0;
     // Patch each destination file
     foreach ($this->destinations as $file => $content) {
         if (File::write($file, implode("\n", $content))) {
             if (isset($this->sources[$file])) {
                 $this->sources[$file] = $content;
             }
             $done++;
         }
     }
     // Remove each removed file
     foreach ($this->removals as $file) {
         if (File::delete($file)) {
             if (isset($this->sources[$file])) {
                 unset($this->sources[$file]);
             }
             $done++;
         }
     }
     // Clear the destinations cache
     $this->destinations = array();
     // Clear the removals
     $this->removals = array();
     // Clear the patches
     $this->patches = array();
     return $done;
 }