PageRenderTime 68ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/joomla/filesystem/file.php

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