PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Quản lý website tuyển dụng việc làm PHP/webcaoca/public_html/vlv/libraries/joomla/filesystem/file.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 385 lines | 203 code | 38 blank | 144 comment | 54 complexity | bef527d69fdbb2caa791fa0b76680137 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: file.php 14401 2010-01-26 14:10:00Z louis $
  4. * @package Joomla.Framework
  5. * @subpackage FileSystem
  6. * @copyright Copyright (C) 2005 - 2010 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. JError::raiseWarning(21, 'JFile::move: ' . JText::_('Cannot find, read or write file') . ": '$src'");
  177. return false;
  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 (is_uploaded_file($src) && $ftp->store($src, $dest))
  302. {
  303. $ret = true;
  304. unlink($src);
  305. } else {
  306. JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
  307. }
  308. } else {
  309. if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) { // Short circuit to prevent file permission errors
  310. if (JPath::setPermissions($dest)) {
  311. $ret = true;
  312. } else {
  313. JError::raiseWarning(21, JText::_('WARNFS_ERR01'));
  314. }
  315. } else {
  316. JError::raiseWarning(21, JText::_('WARNFS_ERR02'));
  317. }
  318. }
  319. return $ret;
  320. }
  321. /**
  322. * Wrapper for the standard file_exists function
  323. *
  324. * @param string $file File path
  325. * @return boolean True if path is a file
  326. * @since 1.5
  327. */
  328. function exists($file)
  329. {
  330. return is_file(JPath::clean($file));
  331. }
  332. /**
  333. * Returns the name, sans any path
  334. *
  335. * param string $file File path
  336. * @return string filename
  337. * @since 1.5
  338. */
  339. function getName($file) {
  340. $slash = strrpos($file, DS);
  341. if ($slash !== false) {
  342. return substr($file, $slash + 1);
  343. } else {
  344. return $file;
  345. }
  346. }
  347. }