PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/public/wp-content/plugins/w3-total-cache/inc/functions/file.php

https://bitbucket.org/millien/illien.ch-wordpress
PHP | 333 lines | 251 code | 32 blank | 50 comment | 30 complexity | d5baffa8bef81e004f18131ea7ed67a9 MD5 | raw file
  1. <?php
  2. /**
  3. * Recursive creates directory
  4. *
  5. * @param string $path
  6. * @param integer $mask
  7. * @param string $curr_path
  8. * @return boolean
  9. */
  10. function w3_mkdir($path, $mask = 0777, $curr_path = '') {
  11. $path = w3_realpath($path);
  12. $path = trim($path, '/');
  13. $dirs = explode('/', $path);
  14. foreach ($dirs as $dir) {
  15. if ($dir == '') {
  16. return false;
  17. }
  18. $curr_path .= ($curr_path == '' ? '' : '/') . $dir;
  19. if (!@is_dir($curr_path)) {
  20. if (!@mkdir($curr_path, $mask)) {
  21. return false;
  22. }
  23. }
  24. }
  25. return true;
  26. }
  27. /**
  28. * Recursive creates directory from some directory
  29. * Does not try to create directory before from
  30. *
  31. * @param string $path
  32. * @param string $from_path
  33. * @param integer $mask
  34. * @return boolean
  35. */
  36. function w3_mkdir_from($path, $from_path = '', $mask = 0777) {
  37. $path = w3_realpath($path);
  38. $from_path = w3_realpath($from_path);
  39. if (substr($path, 0, strlen($from_path)) != $from_path)
  40. return false;
  41. $path = substr($path, strlen($from_path));
  42. $path = trim($path, '/');
  43. $dirs = explode('/', $path);
  44. $curr_path = $from_path;
  45. foreach ($dirs as $dir) {
  46. if ($dir == '') {
  47. return false;
  48. }
  49. $curr_path .= ($curr_path == '' ? '' : '/') . $dir;
  50. if (!@is_dir($curr_path)) {
  51. if (!@mkdir($curr_path, $mask)) {
  52. return false;
  53. }
  54. }
  55. }
  56. return true;
  57. }
  58. /**
  59. * Recursive remove dir
  60. *
  61. * @param string $path
  62. * @param array $exclude
  63. * @param bool $remove
  64. * @return void
  65. */
  66. function w3_rmdir($path, $exclude = array(), $remove = true) {
  67. $dir = @opendir($path);
  68. if ($dir) {
  69. while (($entry = @readdir($dir)) !== false) {
  70. if ($entry == '.' || $entry == '..') {
  71. continue;
  72. }
  73. foreach ($exclude as $mask) {
  74. if (fnmatch($mask, basename($entry))) {
  75. continue 2;
  76. }
  77. }
  78. $full_path = $path . DIRECTORY_SEPARATOR . $entry;
  79. if (@is_dir($full_path)) {
  80. w3_rmdir($full_path, $exclude);
  81. } else {
  82. @unlink($full_path);
  83. }
  84. }
  85. @closedir($dir);
  86. if ($remove) {
  87. @rmdir($path);
  88. }
  89. }
  90. }
  91. /**
  92. * Recursive empty dir
  93. *
  94. * @param string $path
  95. * @param array $exclude
  96. * @return void
  97. */
  98. function w3_emptydir($path, $exclude = array()) {
  99. w3_rmdir($path, $exclude, false);
  100. }
  101. /**
  102. * Check if file is write-able
  103. *
  104. * @param string $file
  105. * @return boolean
  106. */
  107. function w3_is_writable($file) {
  108. $exists = file_exists($file);
  109. $fp = @fopen($file, 'a');
  110. if ($fp) {
  111. fclose($fp);
  112. if (!$exists) {
  113. @unlink($file);
  114. }
  115. return true;
  116. }
  117. return false;
  118. }
  119. /**
  120. * Cehck if dir is write-able
  121. *
  122. * @param string $dir
  123. * @return boolean
  124. */
  125. function w3_is_writable_dir($dir) {
  126. $file = $dir . '/' . uniqid(mt_rand()) . '.tmp';
  127. return w3_is_writable($file);
  128. }
  129. /**
  130. * Returns dirname of path
  131. *
  132. * @param string $path
  133. * @return string
  134. */
  135. function w3_dirname($path) {
  136. $dirname = dirname($path);
  137. if ($dirname == '.' || $dirname == '/' || $dirname == '\\') {
  138. $dirname = '';
  139. }
  140. return $dirname;
  141. }
  142. function w3_make_relative_path($filename, $base_dir) {
  143. $filename = w3_realpath($filename);
  144. $base_dir = w3_realpath($base_dir);
  145. $filename_parts = explode('/', trim($filename, '/'));
  146. $base_dir_parts = explode('/', trim($base_dir, '/'));
  147. // count number of equal path parts
  148. for ($equal_number = 0;;$equal_number++) {
  149. if ($equal_number >= count($filename_parts) ||
  150. $equal_number >= count($base_dir_parts))
  151. break;
  152. if ($filename_parts[$equal_number] != $base_dir_parts[$equal_number])
  153. break;
  154. }
  155. $relative_dir = str_repeat('../', count($base_dir_parts) - $equal_number);
  156. $relative_dir .= implode('/', array_slice($filename_parts, $equal_number));
  157. return $relative_dir;
  158. }
  159. /**
  160. * Returns open basedirs
  161. *
  162. * @return array
  163. */
  164. function w3_get_open_basedirs() {
  165. $open_basedir_ini = ini_get('open_basedir');
  166. $open_basedirs = (W3TC_WIN ? preg_split('~[;,]~', $open_basedir_ini) : explode(':', $open_basedir_ini));
  167. $result = array();
  168. foreach ($open_basedirs as $open_basedir) {
  169. $open_basedir = trim($open_basedir);
  170. if (!empty($open_basedir) && $open_basedir != '') {
  171. $result[] = w3_realpath($open_basedir);
  172. }
  173. }
  174. return $result;
  175. }
  176. /**
  177. * Checks if path is restricted by open_basedir
  178. *
  179. * @param string $path
  180. * @return boolean
  181. */
  182. function w3_check_open_basedir($path) {
  183. $path = w3_realpath($path);
  184. $open_basedirs = w3_get_open_basedirs();
  185. if (!count($open_basedirs)) {
  186. return true;
  187. }
  188. foreach ($open_basedirs as $open_basedir) {
  189. if (strstr($path, $open_basedir) !== false) {
  190. return true;
  191. }
  192. }
  193. return false;
  194. }
  195. function w3_get_file_permissions($file) {
  196. if (function_exists('fileperms') && $fileperms = @fileperms($file)) {
  197. $fileperms = 0777 & $fileperms;
  198. } else {
  199. clearstatcache();
  200. $stat=@stat($file);
  201. if ($stat)
  202. $fileperms = 0777 & $stat['mode'];
  203. else
  204. $fileperms = 0;
  205. }
  206. return $fileperms;
  207. }
  208. function w3_get_file_owner($file = '') {
  209. $fileowner = $filegroup = 'unknown';
  210. if ($file) {
  211. if (function_exists('fileowner') && function_exists('fileowner')) {
  212. $fileowner = @fileowner($file);
  213. $filegroup = @filegroup($file);
  214. if (function_exists('posix_getpwuid') && function_exists('posix_getgrgid')) {
  215. $fileowner = @posix_getpwuid($fileowner);
  216. $fileowner = $fileowner['name'];
  217. $filegroup = @posix_getgrgid($filegroup);
  218. $filegroup = $filegroup['name'];
  219. }
  220. }
  221. } else {
  222. if (function_exists('getmyuid') && function_exists('getmygid')) {
  223. $fileowner = @getmyuid();
  224. $filegroup = @getmygid();
  225. if (function_exists('posix_getpwuid') && function_exists('posix_getgrgid')) {
  226. $fileowner = @posix_getpwuid($fileowner);
  227. $fileowner = $fileowner['name'];
  228. $filegroup = @posix_getgrgid($filegroup);
  229. $filegroup = $filegroup['name'];
  230. }
  231. }
  232. }
  233. return $fileowner . ':' . $filegroup;
  234. }
  235. /**
  236. * Atomically writes file inside W3TC_CACHE_DIR dir
  237. * @param $filename
  238. * @param $content
  239. * @throws Exception
  240. * @return void
  241. **/
  242. function w3_file_put_contents_atomic($filename, $content) {
  243. if (!is_dir(W3TC_CACHE_TMP_DIR) || !is_writable(W3TC_CACHE_TMP_DIR)) {
  244. w3_mkdir_from(W3TC_CACHE_TMP_DIR, W3TC_CACHE_DIR);
  245. if (!is_dir(W3TC_CACHE_TMP_DIR) || !is_writable(W3TC_CACHE_TMP_DIR)) {
  246. throw new Exception('Can\'t create folder <strong>' .
  247. W3TC_CACHE_TMP_DIR . '</strong>');
  248. }
  249. }
  250. $temp = tempnam(W3TC_CACHE_TMP_DIR, 'temp');
  251. try {
  252. if (!($f = @fopen($temp, 'wb'))) {
  253. if (file_exists($temp))
  254. @unlink($temp);
  255. throw new Exception('Can\'t write to temporary file <strong>' .
  256. $temp . '</strong>');
  257. }
  258. fwrite($f, $content);
  259. fclose($f);
  260. if (!@rename($temp, $filename)) {
  261. @unlink($filename);
  262. if (!@rename($temp, $filename)) {
  263. w3_mkdir_from(dirname($filename), W3TC_CACHE_DIR);
  264. if (!@rename($temp, $filename)) {
  265. throw new Exception('Can\'t write to file <strong>' .
  266. $filename . '</strong>');
  267. }
  268. }
  269. }
  270. $chmod = 0644;
  271. if (defined('FS_CHMOD_FILE'))
  272. $chmod = FS_CHMOD_FILE;
  273. @chmod($filename, $chmod);
  274. } catch (Exception $ex) {
  275. if (file_exists($temp))
  276. @unlink($temp);
  277. throw $ex;
  278. }
  279. }