/** * Delete a file or array of files * * @param mixed $file The file name or an array of file names * * @return boolean True on success * * @since 11.1 */ public static function delete($file) { // Initialise variables. jimport('joomla.client.helper'); $FTPOptions = JClientHelper::getCredentials('ftp'); if (is_array($file)) { $files = $file; } else { $files[] = $file; } // Do NOT use ftp if it is not enabled if ($FTPOptions['enabled'] == 1) { // Connect the FTP client jimport('joomla.client.ftp'); $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']); } foreach ($files as $file) { $file = JPath::clean($file); if (!is_file($file)) { continue; } // Try making the file writable first. If it's read-only, it can't be deleted // on Windows, even if the parent folder is writable @chmod($file, 0777); // 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 (@unlink($file)) { JFactory::getLogger()->debug('File deleted: ' . $file . '; ' . (!JFactory::getUser()->get('guest') ? ' By: ' . JFactory::getUser()->get('username') . '; ' : '') . 'Request: ' . JRequest::getVar('REQUEST_URI', '', 'server')); // Do nothing } elseif ($FTPOptions['enabled'] == 1) { $file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/'); if (!$ftp->delete($file)) { // FTP connector throws an error return false; } } else { $filename = basename($file); JError::raiseWarning('SOME_ERROR_CODE', JText::sprintf('JLIB_FILESYSTEM_DELETE_FAILED', $filename)); return false; } } return true; }
/** * 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! JError::raiseWarning(500, __METHOD__ . ': ' . JText::_('JLIB_FILESYSTEM_ERROR_DELETE_BASE_DIRECTORY')); return false; } // Initialise variables. $FTPOptions = JClientHelper::getCredentials('ftp'); try { // Check to make sure the path valid and clean $path = JPath::clean($path); } catch (UnexpectedValueException $e) { throw new UnexpectedValueException($e); } // Is this really a folder? if (!is_dir($path)) { JError::raiseWarning(21, JText::sprintf('JLIB_FILESYSTEM_ERROR_PATH_IS_NOT_A_FOLDER', $path)); 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 (JFile::delete($files) !== true) { // JFile::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 (JFile::delete($folder) !== true) { // JFile::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 jimport('joomla.client.ftp'); $ftp = JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $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)) { JFactory::getLogger()->debug('Path deleted: ' . $path . '; ' . (!JFactory::getUser()->get('guest') ? ' By: ' . JFactory::getUser()->get('username') . '; ' : '') . 'Request: ' . JRequest::getVar('REQUEST_URI', '', 'server')); $ret = true; } elseif ($FTPOptions['enabled'] == 1) { // Translate path and delete $path = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $path), '/'); // FTP connector throws an error $ret = $ftp->delete($path); } else { JError::raiseWarning('SOME_ERROR_CODE', JText::sprintf('JLIB_FILESYSTEM_ERROR_FOLDER_DELETE', $path)); $ret = false; } return $ret; }