PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/filesystem/file.php

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