PageRenderTime 928ms CodeModel.GetById 77ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/filesystem/file.php

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 380 lines | 197 code | 38 blank | 145 comment | 51 complexity | dda4baa8b5f0e7dd2131ec999aa51c3f MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: file.php 9764 2007-12-30 07:48:11Z ircmaxell $
  4. * @package Joomla.Framework
  5. * @subpackage FileSystem
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. // Check to ensure this file is within the rest of the framework
  15. defined('JPATH_BASE') or die();
  16. jimport('joomla.filesystem.path');
  17. /**
  18. * A File handling class
  19. *
  20. * @static
  21. * @author Louis Landry <louis.landry@joomla.org>
  22. * @package Joomla.Framework
  23. * @subpackage FileSystem
  24. * @since 1.5
  25. */
  26. class JFile
  27. {
  28. /**
  29. * Gets the extension of a file name
  30. *
  31. * @param string $file The file name
  32. * @return string The file extension
  33. * @since 1.5
  34. */
  35. function getExt($file) {
  36. $dot = strrpos($file, '.') + 1;
  37. return substr($file, $dot);
  38. }
  39. /**
  40. * Strips the last extension off a file name
  41. *
  42. * @param string $file The file name
  43. * @return string The file name without the extension
  44. * @since 1.5
  45. */
  46. function stripExt($file) {
  47. return preg_replace('#\.[^.]*$#', '', $file);
  48. }
  49. /**
  50. * Makes file name safe to use
  51. *
  52. * @param string $file The name of the file [not full path]
  53. * @return string The sanitised string
  54. * @since 1.5
  55. */
  56. function makeSafe($file) {
  57. $regex = array('#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#', '#^\.#');
  58. return preg_replace($regex, '', $file);
  59. }
  60. /**
  61. * Copies a file
  62. *
  63. * @param string $src The path to the source file
  64. * @param string $dest The path to the destination file
  65. * @param string $path An optional base path to prefix to the file names
  66. * @return boolean True on success
  67. * @since 1.5
  68. */
  69. function copy($src, $dest, $path = null)
  70. {
  71. // Initialize variables
  72. jimport('joomla.client.helper');
  73. $FTPOptions = JClientHelper::getCredentials('ftp');
  74. // Prepend a base path if it exists
  75. if ($path) {
  76. $src = JPath::clean($path.DS.$src);
  77. $dest = JPath::clean($path.DS.$dest);
  78. }
  79. //Check src path
  80. if (!is_readable($src)) {
  81. JError::raiseWarning(21, 'JFile::copy: '.JText::_('Cannot find or read file' . ": '$src'"));
  82. return false;
  83. }
  84. if ($FTPOptions['enabled'] == 1) {
  85. // Connect the FTP client
  86. jimport('joomla.client.ftp');
  87. $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
  88. // If the parent folder doesn't exist we must create it
  89. if (!file_exists(dirname($dest))) {
  90. jimport('joomla.filesystem.folder');
  91. JFolder::create(dirname($dest));
  92. }
  93. //Translate the destination path for the FTP account
  94. $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
  95. if (!$ftp->store($src, $dest)) {
  96. // FTP connector throws an error
  97. return false;
  98. }
  99. $ret = true;
  100. } else {
  101. if (!@ copy($src, $dest)) {
  102. JError::raiseWarning(21, JText::_('Copy failed'));
  103. return false;
  104. }
  105. $ret = true;
  106. }
  107. return $ret;
  108. }
  109. /**
  110. * Delete a file or array of files
  111. *
  112. * @param mixed $file The file name or an array of file names
  113. * @return boolean True on success
  114. * @since 1.5
  115. */
  116. function delete($file)
  117. {
  118. // Initialize variables
  119. jimport('joomla.client.helper');
  120. $FTPOptions = JClientHelper::getCredentials('ftp');
  121. if (is_array($file)) {
  122. $files = $file;
  123. } else {
  124. $files[] = $file;
  125. }
  126. // Do NOT use ftp if it is not enabled
  127. if ($FTPOptions['enabled'] == 1)
  128. {
  129. // Connect the FTP client
  130. jimport('joomla.client.ftp');
  131. $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
  132. }
  133. foreach ($files as $file)
  134. {
  135. $file = JPath::clean($file);
  136. // Try making the file writeable first. If it's read-only, it can't be deleted
  137. // on Windows, even if the parent folder is writeable
  138. @chmod($file, 0777);
  139. // In case of restricted permissions we zap it one way or the other
  140. // as long as the owner is either the webserver or the ftp
  141. if (@unlink($file)) {
  142. // Do nothing
  143. } elseif ($FTPOptions['enabled'] == 1) {
  144. $file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
  145. if (!$ftp->delete($file)) {
  146. // FTP connector throws an error
  147. return false;
  148. }
  149. } else {
  150. $filename = basename($file);
  151. JError::raiseWarning('SOME_ERROR_CODE', JText::_('Delete failed') . ": '$filename'");
  152. return false;
  153. }
  154. }
  155. return true;
  156. }
  157. /**
  158. * Moves a file
  159. *
  160. * @param string $src The path to the source file
  161. * @param string $dest The path to the destination file
  162. * @param string $path An optional base path to prefix to the file names
  163. * @return boolean True on success
  164. * @since 1.5
  165. */
  166. function move($src, $dest, $path = '')
  167. {
  168. // Initialize variables
  169. jimport('joomla.client.helper');
  170. $FTPOptions = JClientHelper::getCredentials('ftp');
  171. if ($path) {
  172. $src = JPath::clean($path.DS.$src);
  173. $dest = JPath::clean($path.DS.$dest);
  174. }
  175. //Check src path
  176. if (!is_readable($src) && !is_writable($src)) {
  177. return JText::_('Cannot find source file');
  178. }
  179. if ($FTPOptions['enabled'] == 1) {
  180. // Connect the FTP client
  181. jimport('joomla.client.ftp');
  182. $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
  183. //Translate path for the FTP account
  184. $src = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $src), '/');
  185. $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
  186. // Use FTP rename to simulate move
  187. if (!$ftp->rename($src, $dest)) {
  188. JError::raiseWarning(21, JText::_('Rename failed'));
  189. return false;
  190. }
  191. } else {
  192. if (!@ rename($src, $dest)) {
  193. JError::raiseWarning(21, JText::_('Rename failed'));
  194. return false;
  195. }
  196. }
  197. return true;
  198. }
  199. /**
  200. * Read the contents of a file
  201. *
  202. * @param string $filename The full file path
  203. * @param boolean $incpath Use include path
  204. * @param int $amount Amount of file to read
  205. * @param int $chunksize Size of chunks to read
  206. * @param int $offset Offset of the file
  207. * @return mixed Returns file contents or boolean False if failed
  208. * @since 1.5
  209. */
  210. function read($filename, $incpath = false, $amount = 0, $chunksize = 8192, $offset = 0)
  211. {
  212. // Initialize variables
  213. $data = null;
  214. if($amount && $chunksize > $amount) { $chunksize = $amount; }
  215. if (false === $fh = fopen($filename, 'rb', $incpath)) {
  216. JError::raiseWarning(21, 'JFile::read: '.JText::_('Unable to open file') . ": '$filename'");
  217. return false;
  218. }
  219. clearstatcache();
  220. if($offset) fseek($fh, $offset);
  221. if ($fsize = @ filesize($filename)) {
  222. if($amount && $fsize > $amount) {
  223. $data = fread($fh, $amount);
  224. } else {
  225. $data = fread($fh, $fsize);
  226. }
  227. } else {
  228. $data = '';
  229. $x = 0;
  230. // While its:
  231. // 1: Not the end of the file AND
  232. // 2a: No Max Amount set OR
  233. // 2b: The length of the data is less than the max amount we want
  234. while (!feof($fh) && (!$amount || strlen($data) < $amount)) {
  235. $data .= fread($fh, $chunksize);
  236. }
  237. }
  238. fclose($fh);
  239. return $data;
  240. }
  241. /**
  242. * Write contents to a file
  243. *
  244. * @param string $file The full file path
  245. * @param string $buffer The buffer to write
  246. * @return boolean True on success
  247. * @since 1.5
  248. */
  249. function write($file, $buffer)
  250. {
  251. // Initialize variables
  252. jimport('joomla.client.helper');
  253. $FTPOptions = JClientHelper::getCredentials('ftp');
  254. // If the destination directory doesn't exist we need to create it
  255. if (!file_exists(dirname($file))) {
  256. jimport('joomla.filesystem.folder');
  257. JFolder::create(dirname($file));
  258. }
  259. if ($FTPOptions['enabled'] == 1) {
  260. // Connect the FTP client
  261. jimport('joomla.client.ftp');
  262. $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
  263. // Translate path for the FTP account and use FTP write buffer to file
  264. $file = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $file), '/');
  265. $ret = $ftp->write($file, $buffer);
  266. } else {
  267. $file = JPath::clean($file);
  268. $ret = file_put_contents($file, $buffer);
  269. }
  270. return $ret;
  271. }
  272. /**
  273. * Moves an uploaded file to a destination folder
  274. *
  275. * @param string $src The name of the php (temporary) uploaded file
  276. * @param string $dest The path (including filename) to move the uploaded file to
  277. * @return boolean True on success
  278. * @since 1.5
  279. */
  280. function upload($src, $dest)
  281. {
  282. // Initialize variables
  283. jimport('joomla.client.helper');
  284. $FTPOptions = JClientHelper::getCredentials('ftp');
  285. $ret = false;
  286. // Ensure that the path is valid and clean
  287. $dest = JPath::clean($dest);
  288. // Create the destination directory if it does not exist
  289. $baseDir = dirname($dest);
  290. if (!file_exists($baseDir)) {
  291. jimport('joomla.filesystem.folder');
  292. JFolder::create($baseDir);
  293. }
  294. if ($FTPOptions['enabled'] == 1) {
  295. // Connect the FTP client
  296. jimport('joomla.client.ftp');
  297. $ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);
  298. //Translate path for the FTP account
  299. $dest = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dest), '/');
  300. // Copy the file to the destination directory
  301. if ($ftp->store($src, $dest)) {
  302. $ftp->chmod($dest, 0777);
  303. $ret = true;
  304. } else {
  305. JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
  306. }
  307. } else {
  308. if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) { // Short circuit to prevent file permission errors
  309. if (JPath::setPermissions($dest)) {
  310. $ret = true;
  311. } else {
  312. JError::raiseWarning(21, JText::_('WARNFS_ERR01'));
  313. }
  314. } else {
  315. JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
  316. }
  317. }
  318. return $ret;
  319. }
  320. /**
  321. * Wrapper for the standard file_exists function
  322. *
  323. * @param string $file File path
  324. * @return boolean True if path is a file
  325. * @since 1.5
  326. */
  327. function exists($file)
  328. {
  329. return is_file(JPath::clean($file));
  330. }
  331. /**
  332. * Returns the name, sans any path
  333. *
  334. * param string $file File path
  335. * @return string filename
  336. * @since 1.5
  337. */
  338. function getName($file) {
  339. $slash = strrpos($file, DS) + 1;
  340. return substr($file, $slash);
  341. }
  342. }