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

/www/wp-content/plugins/ithemes-exchange/lib/classes/it-file-utility.php

https://github.com/ArzuA/gitwordpress
PHP | 883 lines | 490 code | 179 blank | 214 comment | 141 complexity | a00d668b6ad870562cdc2e64d2b9c135 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. Written by Chris Jean for iThemes.com
  4. Version 2.4.2
  5. Version History
  6. 2.0.0 - 2011-02-22
  7. Complete rewrite
  8. 2.0.1 - 2011-05-16 - Chris Jean
  9. Added require_existing arg to get_writable_file
  10. 2.1.0 - 2011-08-04 - Chris Jean
  11. Added an argument to upload_file to prevent the file from being added to the media library
  12. Added require_existing arg to get_writable_directory to keep in line with API for get_writable_file
  13. Added create_favicon
  14. 2.2.0 - 2011-10-06 - Chris Jean
  15. Moved compatibility functions to new files in the compat directory
  16. Moved the image functions to the new ITImageUtility class
  17. Added back-compat functions that call the new ITImageUtility functions
  18. Removed the create_writable_file_old function
  19. 2.3.0 - 2011-12-05 - Chris Jean
  20. Added an auto_ssl arg to the get_url_from_file function
  21. 2.3.1 - 2012-02-13 - Chris Jean
  22. Improved url creation to work with servers with odd ABSPATH configurations
  23. 2.4.0 - 2012-07-26 - Chris Jean
  24. Changed get_url_from_file and get_file_from_url to be compat functions
  25. that call the new functions found in ITUtility
  26. 2.4.1 - 2013-05-21 - Chris Jean
  27. Added "public static" in front of function declarations to satisfy strict requirements.
  28. 2.4.2 - 2013-08-19 - Chris Jean
  29. Loading wp-admin/includes/image.php in the add_to_media_library in order to gain access to the wp_read_image_metadata function.
  30. */
  31. if ( !class_exists( 'ITFileUtility' ) ) {
  32. class ITFileUtility {
  33. public static function file_uploaded( $file_id ) {
  34. if ( ! empty( $_FILES[$file_id] ) && ( '4' != $_FILES[$file_id]['error'] ) )
  35. return true;
  36. return false;
  37. }
  38. public static function add_to_media_library( $file, $args = array() ) {
  39. if ( is_bool( $args ) )
  40. $args = array( 'move_to_uploads' => false );
  41. $default_args = array(
  42. 'move_to_uploads' => true,
  43. 'url' => null,
  44. 'type' => null,
  45. 'name' => null,
  46. 'title' => null,
  47. 'content' => null,
  48. 'attachment_id' => null,
  49. );
  50. $args = array_merge( $default_args, $args );
  51. if ( is_null( $args['name'] ) )
  52. $args['name'] = basename( $file );
  53. if ( is_null( $args['type'] ) ) {
  54. $wp_filetype = wp_check_filetype_and_ext( $file, $file );
  55. if ( false !== $wp_filetype['proper_filename'] )
  56. $args['name'] = $wp_filetype['proper_filename'];
  57. if ( false !== $wp_filetype['type'] )
  58. $args['type'] = $wp_filetype['type'];
  59. else
  60. $args['type'] = '';
  61. }
  62. if ( true === $args['move_to_uploads'] ) {
  63. $uploads = wp_upload_dir();
  64. if ( ! is_array( $uploads ) || ( false !== $uploads['error'] ) )
  65. return false;
  66. $filename = wp_unique_filename( $uploads['path'], $args['name'] );
  67. $new_file = "{$uploads['path']}/$filename";
  68. if ( false === @copy( $file, $new_file ) )
  69. return false;
  70. $stat = stat( dirname( $new_file ));
  71. $perms = $stat['mode'] & 0000666;
  72. @chmod( $new_file, $perms );
  73. $args['url'] = "{$uploads['url']}/$filename";
  74. $file = $new_file;
  75. if ( is_multisite() )
  76. delete_transient( 'dirsize_cache' );
  77. }
  78. if ( is_null( $args['url'] ) )
  79. $args['url'] = ITFileUtility::get_url_from_file( $file );
  80. if ( is_null( $args['title'] ) )
  81. $title = preg_replace( '/\.[^.]+$/', '', basename( $file ) );
  82. if ( is_null( $args['content'] ) )
  83. $args['content'] = '';
  84. require_once( ABSPATH . '/wp-admin/includes/image.php' );
  85. if ( false !== ( $image_meta = @wp_read_image_metadata( $file ) ) ) {
  86. if ( '' !== trim( $image_meta['title'] ) )
  87. $args['title'] = $image_meta['title'];
  88. if ( '' !== trim( $image_meta['caption'] ) )
  89. $args['content'] = $image_meta['caption'];
  90. }
  91. $attachment = array(
  92. 'post_mime_type' => $args['type'],
  93. 'guid' => $args['url'],
  94. 'post_title' => $args['title'],
  95. 'post_content' => $args['content'],
  96. );
  97. $id = wp_insert_attachment( $attachment, $file );
  98. if ( !is_wp_error( $id ) )
  99. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  100. $data = array(
  101. 'id' => $id,
  102. 'file' => $file,
  103. 'url' => $args['url'],
  104. 'type' => $args['type'],
  105. 'title' => $args['title'],
  106. 'caption' => $args['content'],
  107. );
  108. return $data;
  109. }
  110. public static function upload_file( $file_id, $add_to_media_library = true ) {
  111. $overrides = array( 'test_form' => false );
  112. $file = wp_handle_upload( $_FILES[$file_id], $overrides );
  113. if ( isset( $file['error'] ) )
  114. return new WP_Error( 'upload_error', $file['error'] );
  115. $args = array(
  116. 'move_to_uploads' => false,
  117. 'url' => $file['url'],
  118. 'type' => $file['type'],
  119. );
  120. if ( $add_to_media_library )
  121. return ITFileUtility::add_to_media_library( $file['file'], $args );
  122. else
  123. return $file;
  124. }
  125. public static function get_image_dimensions( $file ) {
  126. it_classes_load( 'it-image-utility.php' );
  127. return ITImageUtility::get_image_dimensions( $file );
  128. }
  129. public static function resize_image( $file, $max_w = 0, $max_h = 0, $crop = true, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
  130. it_classes_load( 'it-image-utility.php' );
  131. return ITImageUtility::resize_image( $file, $max_w, $max_h, $crop, $suffix, $dest_path, $jpeg_quality );
  132. }
  133. public static function get_url_from_file( $file, $auto_ssl = true ) {
  134. return ITUtility::get_url_from_file( $file, $auto_ssl );
  135. }
  136. public static function get_file_from_url( $url ) {
  137. return ITUtility::get_file_from_url( $url );
  138. }
  139. public static function get_mime_type( $file ) {
  140. if ( preg_match( '|^https?://|', $file ) )
  141. $file = get_file_from_url( $file );
  142. return mime_content_type( $file );
  143. }
  144. public static function get_file_attachment( $id ) {
  145. if ( wp_attachment_is_image( $id ) ) {
  146. $post = get_post( $id );
  147. $file = array();
  148. $file['ID'] = $id;
  149. $file['file'] = get_attached_file( $id );
  150. $file['url'] = wp_get_attachment_url( $id );
  151. $file['title'] = $post->post_title;
  152. $file['name'] = basename( get_attached_file( $id ) );
  153. return $file;
  154. }
  155. return false;
  156. }
  157. public static function delete_file_attachment( $id ) {
  158. if ( wp_attachment_is_image( $id ) ) {
  159. $file = get_attached_file( $id );
  160. $info = pathinfo( $file );
  161. $ext = $info['extension'];
  162. $name = basename( $file, ".$ext" );
  163. if ( $dir = opendir( dirname( $file ) ) ) {
  164. while ( false !== ( $filename = readdir( $dir ) ) ) {
  165. if ( preg_match( "/^$name-resized-image-\d+x\d+\.$ext$/", $filename ) )
  166. unlink( dirname( $file ) . '/' . $filename );
  167. elseif ( "$name-coalesced-file.$ext" === $filename )
  168. unlink( dirname( $file ) . '/' . $filename );
  169. }
  170. closedir( $dir );
  171. }
  172. unlink( $file );
  173. return true;
  174. }
  175. return false;
  176. }
  177. public static function is_animated_gif( $file ) {
  178. it_classes_load( 'it-image-utility.php' );
  179. return ITImageUtility::is_animated_gif( $file );
  180. }
  181. public static function write( $path, $content, $args = array() ) {
  182. if ( is_bool( $args ) )
  183. $args = array( 'append' => $args );
  184. else if ( is_int( $args ) )
  185. $args = array( 'permissions' => $args );
  186. else if ( ! is_array( $args ) )
  187. $args = array();
  188. $default_args = array(
  189. 'append' => false,
  190. 'permissions' => 0644,
  191. );
  192. $args = array_merge( $default_args, $args );
  193. $mode = ( false === $args['append'] ) ? 'w' : 'a';
  194. if ( ! is_dir( dirname( $path ) ) ) {
  195. ITFileUtility::mkdir( dirname( $path ) );
  196. if ( ! is_dir( dirname( $path ) ) )
  197. return false;
  198. }
  199. $created = ! is_file( $path );
  200. if ( false === ( $handle = fopen( $path, $mode ) ) )
  201. return false;
  202. $result = fwrite( $handle, $content );
  203. fclose( $handle );
  204. if ( false === $result )
  205. return false;
  206. if ( ( true === $created ) && is_int( $args['append'] ) )
  207. @chmod( $path, $args['append'] );
  208. return true;
  209. }
  210. public static function is_file_writable( $path ) {
  211. return ITFileUtility::write( $path, '', array( 'append' => true ) );
  212. }
  213. public static function locate_file( $args ) {
  214. if ( is_string( $args ) )
  215. $args = array( 'glob_pattern' => $args );
  216. else if ( ! is_array( $args ) )
  217. $args = array();
  218. $default_args = array(
  219. 'glob_pattern' => null,
  220. 'regex_pattern' => null, // The regex_pattern can refine results from glob
  221. 'default_search_paths' => array( 'uploads_basedir', 'uploads_path', 'wp-content', 'abspath' ),
  222. 'custom_search_paths' => array(),
  223. 'locate_all_matches' => true,
  224. 'type' => 'all', // all, file, dir
  225. );
  226. $args = array_merge( $default_args, $args );
  227. if ( is_null( $args['glob_pattern'] ) )
  228. return new WP_Error( 'locate_file_no_name', 'The call to ITFileUtility::locate_file is missing the glob_pattern attribute' );
  229. $uploads = wp_upload_dir();
  230. if ( ! is_array( $uploads ) || ( false !== $uploads['error'] ) )
  231. $uploads = array( 'basedir' => false, 'path' => false );
  232. $default_search_paths = array(
  233. 'uploads_basedir' => $uploads['basedir'],
  234. 'uploads_path' => $uploads['path'],
  235. 'wp-content' => WP_CONTENT_DIR,
  236. 'abspath' => ABSPATH,
  237. );
  238. $search_paths = array_merge( (array) $args['custom_search_paths'], (array) $args['default_search_paths'] );
  239. $results = array();
  240. foreach ( (array) $search_paths as $search_path ) {
  241. if ( isset( $default_search_paths[$search_path] ) ) {
  242. if ( false === $default_search_paths[$search_path] )
  243. continue;
  244. $search_path = $default_search_paths[$search_path];
  245. }
  246. if ( is_dir( $search_path ) ) {
  247. $files = glob( "$search_path/{$args['glob_pattern']}" );
  248. foreach ( (array) $files as $file ) {
  249. if ( ! is_null( $args['regex_pattern'] ) && ! preg_match( $args['regex_pattern'], $file ) )
  250. continue;
  251. if ( ( 'dir' === $args['type'] ) && ! is_dir( $file ) )
  252. continue;
  253. if ( ( 'file' === $args['type'] ) && ! is_file( $file ) )
  254. continue;
  255. $results[] = $file;
  256. }
  257. if ( ! empty( $results ) && ( true !== $args['locate_all_matches'] ) ) {
  258. if ( 1 === count( $results ) )
  259. return $results[0];
  260. return $results;
  261. }
  262. }
  263. }
  264. if ( empty( $results ) )
  265. return new WP_Error( 'locate_file_failed', 'Unable to locate requested file' );
  266. if ( 1 === count ( $results ) )
  267. return $results[0];
  268. return $results;
  269. }
  270. public static function get_writable_directory( $args ) {
  271. if ( is_string( $args ) )
  272. $args = array( 'name' => $args );
  273. else if ( ! is_array( $args ) )
  274. $args = array();
  275. $default_args = array(
  276. 'name' => '',
  277. 'create_new' => false, // Indicates if a new file is needed. Often used with rename set to true.
  278. 'rename' => false, // Generates a new name if a file already exists with the specified name. Not used if create_new is false.
  279. 'require_existing' => false, // Set to true to throw an error if the file does not already exist.
  280. 'random' => false, // Generate a random name to be used
  281. 'permissions' => 0755,
  282. 'default_search_paths' => array( 'uploads_basedir', 'uploads_path', 'wp-content', 'abspath' ),
  283. 'custom_search_paths' => array(),
  284. );
  285. $args = array_merge( $default_args, $args );
  286. if ( empty( $args['name'] ) && ( true === $args['create_new'] ) && ( false === $args['random'] ) )
  287. return new WP_Error( 'get_writable_directory_no_name', 'The call to ITFileUtility::get_writable_directory is missing the name attribute' );
  288. $uploads = wp_upload_dir();
  289. if ( ! is_array( $uploads ) || ( false !== $uploads['error'] ) )
  290. $uploads = array( 'basedir' => false, 'path' => false );
  291. $default_search_paths = array(
  292. 'uploads_basedir' => $uploads['basedir'],
  293. 'uploads_path' => $uploads['path'],
  294. 'wp-content' => WP_CONTENT_DIR,
  295. 'abspath' => ABSPATH,
  296. );
  297. $search_paths = array_merge( (array) $args['custom_search_paths'], (array) $args['default_search_paths'] );
  298. $path = false;
  299. foreach ( (array) $search_paths as $search_path ) {
  300. if ( isset( $default_search_paths[$search_path] ) ) {
  301. if ( false === $default_search_paths[$search_path] )
  302. continue;
  303. $search_path = $default_search_paths[$search_path];
  304. }
  305. if ( is_dir( $search_path ) && is_writable( $search_path ) ) {
  306. $path = $search_path;
  307. break;
  308. }
  309. }
  310. if ( false === $path )
  311. return new WP_Error( 'get_writable_base_directory_failed', 'Unable to find a writable base directory' );
  312. if ( empty( $args['name'] ) && ( false === $args['random'] ) )
  313. return $path;
  314. if ( true === $args['random'] ) {
  315. $name = ( isset( $args['name'] ) ) ? $args['name'] : '';
  316. $uid = uniqid( "$name-", true );
  317. while ( is_dir( "$path/$uid" ) )
  318. $uid = uniqid( "$name-", true );
  319. $name = $uid;
  320. }
  321. else
  322. $name = $args['name'];
  323. if ( is_dir( "$path/$name" ) ) {
  324. if ( true === $args['create_new'] ) {
  325. if ( false === $args['rename'] )
  326. return new WP_Error( 'get_writable_directory_no_rename', 'Unable to create the named writable directory' );
  327. $name = ITFileUtility::get_unique_name( $path, $name );
  328. }
  329. else {
  330. if ( is_writable( "$path/$name" ) )
  331. return "$path/$name";
  332. return new WP_Error( 'get_writable_directory_cannot_write', 'Required directory exists but is not writable' );
  333. }
  334. }
  335. else if ( true === $args['require_existing'] )
  336. return new WP_Error( 'get_writable_directory_does_not_exist', 'Required writable directory does not exist' );
  337. if ( true === ITFileUtility::mkdir( "$path/$name", $args['permissions'] ) )
  338. return "$path/$name";
  339. return new WP_Error( 'get_writable_directory_failed', 'Unable to create a writable directory' );
  340. }
  341. public static function create_writable_directory( $args ) {
  342. if ( is_string( $args ) )
  343. $args = array( 'name' => $args );
  344. else if ( ! is_array( $args ) )
  345. $args = array();
  346. $default_args = array(
  347. 'create_new' => true,
  348. 'rename' => true,
  349. 'random' => false,
  350. );
  351. $args = array_merge( $default_args, $args );
  352. return ITFileUtility::get_writable_directory( $args );
  353. }
  354. public static function get_writable_file( $args, $extension = null ) {
  355. if ( is_string( $args ) )
  356. $args = array( 'name' => $args );
  357. else if ( ! is_array( $args ) )
  358. $args = array();
  359. $default_args = array(
  360. 'name' => '',
  361. 'extension' => '',
  362. 'create_new' => false, // Indicates if a new file is needed. Often used with rename set to true.
  363. 'rename' => false, // Generates a new name if a file already exists with the specified name. Not used if create_new is false.
  364. 'require_existing' => false, // Set to true to throw an error if the file does not already exist.
  365. 'permissions' => 0644,
  366. 'path_permissions' => 0755,
  367. 'default_search_paths' => array( 'uploads_basedir', 'uploads_path', 'wp-content', 'abspath' ),
  368. 'custom_search_paths' => array(),
  369. );
  370. $args = array_merge( $default_args, $args );
  371. if ( empty( $args['name'] ) )
  372. return new WP_Error( 'get_writable_file_no_name', 'The call to ITFileUtility::get_writable_file is missing the name attribute' );
  373. if ( is_null( $extension ) )
  374. $extension = $args['extension'];
  375. if ( ! empty( $extension ) && ! preg_match( '/^\./', $extension ) )
  376. $extension = ".$extension";
  377. $base_path = ITFileUtility::get_writable_directory( array( 'permissions' => $args['path_permissions'], 'default_search_paths' => $args['default_search_paths'], 'custom_search_paths' => $args['custom_search_paths'] ) );
  378. if ( is_wp_error( $base_path ) )
  379. return $base_path;
  380. $name = $args['name'];
  381. if ( preg_match( '|/|', $name ) ) {
  382. $base_path .= '/' . dirname( $name );
  383. $name = basename( $name );
  384. }
  385. $file = "$base_path/$name$extension";
  386. if ( is_file( $file ) ) {
  387. if ( true === $args['create_new'] ) {
  388. if ( false === $args['rename'] )
  389. return new WP_Error( 'get_writable_file_no_rename', 'Unable to create the named writable file' );
  390. $name = ITFileUtility::get_unique_name( $base_path, $name, $extension );
  391. $file = "$base_path/$name";
  392. }
  393. else {
  394. if ( is_writable( $file ) )
  395. return $file;
  396. return new WP_Error( 'get_writable_file_cannot_write', 'Required file exists but is not writable' );
  397. }
  398. }
  399. else if ( true === $args['require_existing'] )
  400. return new WP_Error( 'get_writable_file_does_not_exist', 'Required writable file does not exist' );
  401. if ( true === ITFileUtility::is_file_writable( $file ) ) {
  402. @chmod( $file, $args['permissions'] );
  403. return $file;
  404. }
  405. return new WP_Error( 'get_writable_file_failed', 'Unable to create a writable file' );
  406. }
  407. public static function create_writable_file( $args, $extension = null ) {
  408. if ( is_string( $args ) )
  409. $args = array( 'name' => $args );
  410. else if ( ! is_array( $args ) )
  411. $args = array();
  412. $default_args = array(
  413. 'create_new' => true,
  414. 'rename' => true,
  415. );
  416. $args = array_merge( $default_args, $args );
  417. return ITFileUtility::get_writable_file( $args, $extension );
  418. }
  419. public static function get_writable_uploads_directory( $directory ) {
  420. $uploads = wp_upload_dir();
  421. if ( ! is_array( $uploads ) || ( false !== $uploads['error'] ) )
  422. return false;
  423. $path = "{$uploads['basedir']}/$directory";
  424. if ( ! is_dir( $path ) ) {
  425. ITFileUtility::mkdir( $path );
  426. if ( ! is_dir( $path ) )
  427. return false;
  428. }
  429. if ( ! is_writable( $path ) )
  430. return false;
  431. $directory_info = array(
  432. 'path' => $path,
  433. 'url' => "{$uploads['baseurl']}/$directory",
  434. );
  435. return $directory_info;
  436. }
  437. public static function find_writable_path( $args = array(), $vars = array() ) {
  438. $default_args = array(
  439. 'private' => true,
  440. 'possible_paths' => array(),
  441. 'permissions' => 0755,
  442. );
  443. $args = array_merge( $default_args, $args );
  444. $uploads_dir_data = wp_upload_dir();
  445. $default_vars = array(
  446. 'uploads_basedir' => $uploads_dir_data['basedir'],
  447. 'uploads_path' => $uploads_dir_data['path'],
  448. );
  449. $vars = array_merge( $default_vars, $vars );
  450. foreach ( (array) $args['possible_paths'] as $path ) {
  451. foreach ( (array) $vars as $var => $val )
  452. $path = preg_replace( '/%' . preg_quote( $var, '/' ) . '%/', $val, $path );
  453. if ( ! is_dir( $path ) )
  454. ITFileUtility::mkdir( $path, $args['permissions'] );
  455. $path = realpath( $path );
  456. if ( ! empty( $path ) && is_writable( $path ) ) {
  457. $writable_dir = $path;
  458. break;
  459. }
  460. }
  461. if ( empty( $writable_dir ) || ! is_writable( $writable_dir ) ) {
  462. if ( is_writable( $uploads_dir_data['basedir'] ) )
  463. $writable_dir = $uploads_dir_data['basedir'];
  464. else if ( is_writable( $uploads_dir_data['path'] ) )
  465. $writable_dir = $uploads_dir_data['path'];
  466. else if ( is_writable( dirname( __FILE__ ) ) )
  467. $writable_dir = dirname( __FILE__ );
  468. else if ( is_writable( ABSPATH ) )
  469. $writable_dir = ABSPATH;
  470. else if ( true === $args['private'] )
  471. return new WP_Error( 'no_private_writable_path', 'Unable to find a writable path within the private space' );
  472. else
  473. $writable_dir = sys_get_temp_dir();
  474. }
  475. if ( empty( $writable_dir ) || ! is_dir( $writable_dir ) || ! is_writable( $writable_dir ) )
  476. return new WP_Error( 'no_writable_path', 'Unable to find a writable path' );
  477. $writable_dir = preg_replace( '|/+$|', '', $writable_dir );
  478. return $writable_dir;
  479. }
  480. public static function create_writable_path( $args = array() ) {
  481. if ( is_string( $args ) )
  482. $args = array( 'name' => $args );
  483. else if ( ! is_array( $args ) )
  484. $args = array();
  485. $default_args = array(
  486. 'name' => 'temp-deleteme',
  487. 'private' => true,
  488. 'possible_paths' => array(),
  489. 'permissions' => 0755,
  490. 'rename' => false,
  491. );
  492. $args = array_merge( $default_args, $args );
  493. $writable_dir = ITFileUtility::find_writable_path( array( 'private' => $args['private'], 'possible_paths' => $args['possible_paths'], 'permissions' => $args['permissions'] ) );
  494. if ( is_wp_error( $writable_dir ) )
  495. return $writable_dir;
  496. $test_dir_name = $args['name'];
  497. $path = "$writable_dir/$test_dir_name";
  498. if ( file_exists( $path ) && ( false === $args['rename'] ) ) {
  499. if ( is_writable( $path ) )
  500. return $path;
  501. else
  502. return new WP_Error( 'create_writable_path_failed', 'Requested path exists and cannot be written to' );
  503. }
  504. $count = 0;
  505. while ( is_dir( "$writable_dir/$test_dir_name" ) ) {
  506. $count++;
  507. $test_dir_name = "{$args['name']}-$count";
  508. }
  509. $path = "$writable_dir/$test_dir_name";
  510. if ( false === ITFileUtility::mkdir( $path, $args['permissions'] ) )
  511. return new WP_Error( 'create_path_failed', 'Unable to create a writable path' );
  512. if ( ! is_writable( $path ) )
  513. return new WP_Error( 'create_writable_path_failed', 'Unable to create a writable path' );
  514. return $path;
  515. }
  516. public static function get_file_listing( $path ) {
  517. if ( ! is_dir( $path ) )
  518. return false;
  519. $files = array_merge( glob( "$path/*" ), glob( "$path/.*" ) );
  520. $contents = array();
  521. foreach ( (array) $files as $file ) {
  522. if ( in_array( basename( $file ), array( '.', '..' ) ) )
  523. continue;
  524. if ( is_dir( $file ) )
  525. $contents[basename( $file )] = ITFileUtility::get_file_listing( $file );
  526. else if ( is_file( $file ) )
  527. $contents[basename( $file )] = true;
  528. }
  529. return $contents;
  530. }
  531. public static function get_flat_file_listing( $path, $recurse = false ) {
  532. if ( ! is_dir( $path ) )
  533. return false;
  534. $path = rtrim( $path, '/' );
  535. $files = array_merge( glob( "$path/*" ), glob( "$path/.*" ) );
  536. $contents = array();
  537. foreach ( (array) $files as $file ) {
  538. if ( in_array( basename( $file ), array( '.', '..' ) ) )
  539. continue;
  540. if ( is_dir( $file ) ) {
  541. $results = ITFileUtility::get_flat_file_listing( $file, true );
  542. $contents = array_merge( $contents, $results );
  543. }
  544. else if ( is_file( $file ) )
  545. $contents[] = $file;
  546. }
  547. if ( false === $recurse )
  548. $contents = str_replace( "$path/", '', $contents );
  549. return $contents;
  550. }
  551. public static function mkdir( $directory, $args = array() ) {
  552. if ( is_dir( $directory ) )
  553. return true;
  554. if ( is_file( $directory ) )
  555. return false;
  556. if ( is_int( $args ) )
  557. $args = array( 'permissions' => $args );
  558. if ( is_bool( $args ) )
  559. $args = array( 'create_index' => false );
  560. $default_args = array(
  561. 'permissions' => 0755,
  562. 'create_index' => true,
  563. );
  564. $args = array_merge( $default_args, $args );
  565. if ( ! is_dir( dirname( $directory ) ) ) {
  566. if ( false === ITFileUtility::mkdir( dirname( $directory ), $args ) )
  567. return false;
  568. }
  569. if ( false === @mkdir( $directory, $args['permissions'] ) )
  570. return false;
  571. if ( true === $args['create_index'] )
  572. ITFileUtility::write( "$directory/index.php", '<?php // Silence is golden.' );
  573. return true;
  574. }
  575. public static function copy( $source, $destination, $args = array() ) {
  576. $default_args = array(
  577. 'max_depth' => 100,
  578. 'folder_mode' => 0755,
  579. 'file_mode' => 0744,
  580. 'ignore_files' => array(),
  581. );
  582. $args = array_merge( $default_args, $args );
  583. ITFileUtility::_copy( $source, $destination, $args );
  584. }
  585. public static function _copy( $source, $destination, $args, $depth = 0 ) {
  586. if ( $depth > $args['max_depth'] )
  587. return true;
  588. if ( is_file( $source ) ) {
  589. if ( is_dir( $destination ) || preg_match( '|/$|', $destination ) ) {
  590. $destination = preg_replace( '|/+$|', '', $destination );
  591. $destination = "$destination/" . basename( $source );
  592. }
  593. if ( false === ITFileUtility::mkdir( dirname( $destination ), $args['folder_mode'] ) )
  594. return false;
  595. if ( false === @copy( $source, $destination ) )
  596. return false;
  597. @chmod( $destination, $args['file_mode'] );
  598. return true;
  599. }
  600. else if ( is_dir( $source ) || preg_match( '|/\*$|', $source ) ) {
  601. if ( preg_match( '|/\*$|', $source ) )
  602. $source = preg_replace( '|/\*$|', '', $source );
  603. else if ( preg_match( '|/$|', $destination ) )
  604. $destination = $destination . basename( $source );
  605. $destination = preg_replace( '|/$|', '', $destination );
  606. $files = array_diff( array_merge( glob( $source . '/.*' ), glob( $source . '/*' ) ), array( $source . '/.', $source . '/..' ) );
  607. if ( false === ITFileUtility::mkdir( $destination, $args['folder_mode'] ) )
  608. return false;
  609. $result = true;
  610. foreach ( (array) $files as $file ) {
  611. if ( false === ITFileUtility::_copy( $file, "$destination/", $args, $depth + 1 ) )
  612. $result = false;
  613. }
  614. return $result;
  615. }
  616. return false;
  617. }
  618. public static function delete_directory( $path ) {
  619. if ( ! is_dir( $path ) )
  620. return true;
  621. $files = array_merge( glob( "$path/*" ), glob( "$path/.*" ) );
  622. $contents = array();
  623. foreach ( (array) $files as $file ) {
  624. if ( in_array( basename( $file ), array( '.', '..' ) ) )
  625. continue;
  626. if ( is_dir( $file ) )
  627. ITFileUtility::delete_directory( $file );
  628. else if ( is_file( $file ) )
  629. @unlink( $file );
  630. }
  631. @rmdir( $path );
  632. if ( ! is_dir( $path ) )
  633. return true;
  634. return false;
  635. }
  636. public static function get_unique_name( $path, $prefix, $postfix = '' ) {
  637. $count = 0;
  638. $test_name = "$prefix$postfix";
  639. while ( file_exists( "$path/$test_name" ) ) {
  640. $count++;
  641. $test_name = "$prefix-$count$postfix";
  642. }
  643. return $test_name;
  644. }
  645. public static function create_favicon( $dir_name, $image, $sizes = false ) {
  646. it_classes_load( 'it-image-utility.php' );
  647. return ITImageUtility::create_favicon( $dir_name, $image, $sizes );
  648. }
  649. }
  650. }
  651. if ( ! function_exists( 'mime_content_type' ) )
  652. require_once( dirname( __FILE__ ) . '/compat/mime_content_type.php' );
  653. if ( ! function_exists( 'sys_get_temp_dir' ) )
  654. require_once( dirname( __FILE__ ) . '/compat/sys_get_temp_dir.php' );