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

/wp-content/plugins/add-from-server/class.add-from-server.php

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 678 lines | 547 code | 95 blank | 36 comment | 115 complexity | f375175db01bc91598052a4c075ffd38 MD5 | raw file
  1. <?php
  2. class add_from_server {
  3. var $version = '3.2.0.1';
  4. var $basename = '';
  5. var $folder = '';
  6. var $meets_guidelines = array(); // Internal use only.
  7. function __construct($plugin) {
  8. $this->basename = $plugin;
  9. $this->folder = dirname($plugin);
  10. //Register general hooks.
  11. add_action('init', array(&$this, 'load_translations')); // must run before admin_menu
  12. add_action('admin_init', array(&$this, 'admin_init'));
  13. add_action('admin_menu', array(&$this, 'admin_menu'));
  14. }
  15. function requires_32() {
  16. echo '<div class="error"><p>' . __('<strong>Add From Server:</strong> Sorry, This plugin requires WordPress 3.2+. Please upgrade your WordPress installation or deactivate this plugin.', 'add-from-server') . '</p></div>';
  17. }
  18. function load_translations() {
  19. //Load any translation files needed:
  20. load_plugin_textdomain('add-from-server', '', $this->folder . '/langs/');
  21. }
  22. function admin_init() {
  23. //Register our JS & CSS
  24. wp_register_style ('add-from-server', plugins_url( '/add-from-server.css', __FILE__ ), array(), $this->version);
  25. if ( ! function_exists('submit_button') ) {
  26. add_action('admin_notices', array(&$this, 'requires_32') );
  27. return;
  28. }
  29. //Enqueue JS & CSS
  30. add_action('load-media_page_add-from-server', array(&$this, 'add_styles') );
  31. add_action('media_upload_server', array(&$this, 'add_styles') );
  32. add_filter('plugin_action_links_' . $this->basename, array(&$this, 'add_configure_link'));
  33. if ( $this->user_allowed() ) {
  34. //Add actions/filters
  35. add_filter('media_upload_tabs', array(&$this, 'tabs'));
  36. add_action('media_upload_server', array(&$this, 'tab_handler'));
  37. }
  38. //Register our settings:
  39. register_setting('add_from_server', 'frmsvr_root', array(&$this, 'sanitize_option_root') );
  40. //register_setting('add-from-server', 'frmsvr_last_folder');
  41. register_setting('add_from_server', 'frmsvr_uac');
  42. register_setting('add_from_server', 'frmsvr_uac_users');
  43. register_setting('add_from_server', 'frmsvr_uac_role');
  44. }
  45. function admin_menu() {
  46. if ( ! function_exists('submit_button') )
  47. return;
  48. if ( $this->user_allowed() )
  49. add_media_page( __('Add From Server', 'add-from-server'), __('Add From Server', 'add-from-server'), 'read', 'add-from-server', array(&$this, 'menu_page') );
  50. add_options_page( __('Add From Server Settings', 'add-from-server'), __('Add From Server', 'add-from-server'), 'manage_options', 'add-from-server-settings', array(&$this, 'options_page') );
  51. }
  52. function add_configure_link($_links) {
  53. $links = array();
  54. if ( $this->user_allowed() )
  55. $links[] = '<a href="' . admin_url('upload.php?page=add-from-server') . '">' . __('Import Files', 'add-from-server') . '</a>';
  56. if ( current_user_can('manage_options') )
  57. $links[] = '<a href="' . admin_url('options-general.php?page=add-from-server-settings') . '">' . __('Options', 'add-from-server') . '</a>';
  58. return array_merge($links, $_links);
  59. }
  60. //Add a tab to the media uploader:
  61. function tabs($tabs) {
  62. if ( $this->user_allowed() )
  63. $tabs['server'] = __('Add From Server', 'add-from-server');
  64. return $tabs;
  65. }
  66. function add_styles() {
  67. //Enqueue support files.
  68. if ( 'media_upload_server' == current_filter() )
  69. wp_enqueue_style('media');
  70. wp_enqueue_style('add-from-server');
  71. }
  72. //Handle the actual page:
  73. function tab_handler(){
  74. if ( ! $this->user_allowed() )
  75. return;
  76. //Set the body ID
  77. $GLOBALS['body_id'] = 'media-upload';
  78. //Do an IFrame header
  79. iframe_header( __('Add From Server', 'add-from-server') );
  80. //Add the Media buttons
  81. media_upload_header();
  82. //Handle any imports:
  83. $this->handle_imports();
  84. //Do the content
  85. $this->main_content();
  86. //Do a footer
  87. iframe_footer();
  88. }
  89. function menu_page() {
  90. if ( ! $this->user_allowed() )
  91. return;
  92. //Handle any imports:
  93. $this->handle_imports();
  94. echo '<div class="wrap">';
  95. screen_icon('upload');
  96. echo '<h2>' . __('Add From Server', 'add-from-server') . '</h2>';
  97. //Do the content
  98. $this->main_content();
  99. echo '</div>';
  100. }
  101. function options_page() {
  102. if ( ! current_user_can('manage_options') )
  103. return;
  104. include 'class.add-from-server-settings.php';
  105. $this->settings = new add_from_server_settings(&$this);
  106. $this->settings->render();
  107. }
  108. function get_root( $context = 'use' ) {
  109. static $static_root = null;
  110. if ( $static_root )
  111. return $static_root;
  112. $root = get_option('frmsvr_root', false);
  113. if ( strpos($root, '%') !== false && 'raw' != $context ) {
  114. $user = wp_get_current_user();
  115. $root = str_replace('%username%', $user->user_login, $root);
  116. $root = str_replace('%role%', $user->roles[0], $root);
  117. }
  118. if ( false === $root ) {
  119. $file = __FILE__;
  120. if ( '/' == $file[0] )
  121. $root = '/';
  122. elseif ( preg_match('/(\w:)/i', __FILE__, $root_win_match) )
  123. $root = $root_win_match[1];
  124. }
  125. if ( strlen($root) > 1 )
  126. $root = untrailingslashit($root);
  127. $static_root = $root = strtolower( $root );
  128. return $root;
  129. }
  130. function user_allowed() {
  131. if ( ! current_user_can('upload_files') )
  132. return false;
  133. switch ( get_option('frmsvr_uac', 'allusers') ) {
  134. case 'allusers':
  135. return true;
  136. case 'role':
  137. $user = wp_get_current_user();
  138. $roles = $user->roles;
  139. $allowed_roles = get_option('frmsvr_uac_role', array());
  140. foreach ( $roles as $r ) {
  141. if ( in_array($r, $allowed_roles) )
  142. return true;
  143. }
  144. return false;
  145. case 'listusers':
  146. $user = wp_get_current_user();
  147. $allowed_users = explode("\n", get_option('frmsvr_uac_users', ''));
  148. $allowed_users = array_map('trim', $allowed_users);
  149. $allowed_users = array_filter($allowed_users);
  150. return in_array($user->user_login, $allowed_users);
  151. }
  152. return false;
  153. }
  154. function sanitize_option_root($input) {
  155. $_input = $input;
  156. if ( 'specific' == $input )
  157. $input = stripslashes($_POST['frmsvr_root-specified']);
  158. if ( !$this->validate_option_root( $input ) )
  159. $input = get_option('frmsvr_root');
  160. $input = strtolower($input);
  161. $input = str_replace('\\', '/', $input);
  162. return $input;
  163. }
  164. function validate_option_root($o) {
  165. if ( strpos($o, '%') !== false ) {
  166. // Ensure only valid placeholders are used:
  167. if ( preg_match_all('!%(.*?)%!', $o, $placeholders) ) {
  168. $valid_ph = array('username', 'role');
  169. foreach ( $placeholders[1] as $ph ) {
  170. if ( !in_array($ph, $valid_ph) ) {
  171. add_settings_error('general', 'update_failed', sprintf(__('The placeholder %s is not valid in the root path.', 'add-from-server'), '%' . $ph . '%'), 'error');
  172. return false;
  173. }
  174. }
  175. return true;
  176. }
  177. }
  178. if ( !is_dir($o) || !is_readable($o) ) {
  179. add_settings_error('general', 'update_failed', __('The root path specified could not be read.', 'add-from-server'), 'error');
  180. return false;
  181. }
  182. return true;
  183. }
  184. //Handle the imports
  185. function handle_imports() {
  186. if ( !empty($_POST['files']) && !empty($_POST['cwd']) ) {
  187. $files = array_map('stripslashes', $_POST['files']);
  188. $cwd = trailingslashit(stripslashes($_POST['cwd']));
  189. $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
  190. $import_date = isset($_REQUEST['import-date']) ? $_REQUEST['import-date'] : 'file';
  191. $import_to_gallery = isset($_POST['gallery']) && 'on' == $_POST['gallery'];
  192. if ( ! $import_to_gallery && !isset($_REQUEST['cwd']) )
  193. $import_to_gallery = true; // cwd should always be set, if it's not, and neither is gallery, this must be the first page load.
  194. if ( ! $import_to_gallery )
  195. $post_id = 0;
  196. flush();
  197. wp_ob_end_flush_all();
  198. foreach ( (array)$files as $file ) {
  199. $filename = $cwd . $file;
  200. $id = $this->handle_import_file($filename, $post_id, $import_date);
  201. if ( is_wp_error($id) ) {
  202. echo '<div class="updated error"><p>' . sprintf(__('<em>%s</em> was <strong>not</strong> imported due to an error: %s', 'add-from-server'), $file, $id->get_error_message() ) . '</p></div>';
  203. } else {
  204. //increment the gallery count
  205. if ( $import_to_gallery )
  206. echo "<script type='text/javascript'>jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);</script>";
  207. echo '<div class="updated"><p>' . sprintf(__('<em>%s</em> has been added to Media library', 'add-from-server'), $file) . '</p></div>';
  208. }
  209. flush();
  210. wp_ob_end_flush_all();
  211. }
  212. }
  213. }
  214. //Handle an individual file import.
  215. function handle_import_file($file, $post_id = 0, $import_date = 'file') {
  216. set_time_limit(120);
  217. // Initially, Base it on the -current- time.
  218. $time = current_time('mysql', 1);
  219. // Next, If it's post to base the upload off:
  220. if ( 'post' == $import_date && $post_id > 0 ) {
  221. $post = get_post($post_id);
  222. if ( $post && substr( $post->post_date_gmt, 0, 4 ) > 0 )
  223. $time = $post->post_date_gmt;
  224. } elseif ( 'file' == $import_date ) {
  225. $time = gmdate( 'Y-m-d H:i:s', @filemtime($file) );
  226. }
  227. // A writable uploads dir will pass this test. Again, there's no point overriding this one.
  228. if ( ! ( ( $uploads = wp_upload_dir($time) ) && false === $uploads['error'] ) )
  229. return new WP_Error( 'upload_error', $uploads['error']);
  230. $wp_filetype = wp_check_filetype( $file, null );
  231. extract( $wp_filetype );
  232. if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
  233. return new WP_Error('wrong_file_type', __( 'Sorry, this file type is not permitted for security reasons.' ) ); //A WP-core string..
  234. //Is the file allready in the uploads folder?
  235. if ( preg_match('|^' . preg_quote(str_replace('\\', '/', $uploads['basedir'])) . '(.*)$|i', $file, $mat) ) {
  236. $filename = basename($file);
  237. $new_file = $file;
  238. $url = $uploads['baseurl'] . $mat[1];
  239. $attachment = get_posts(array( 'post_type' => 'attachment', 'meta_key' => '_wp_attached_file', 'meta_value' => ltrim($mat[1], '/') ));
  240. if ( !empty($attachment) )
  241. return new WP_Error('file_exists', __( 'Sorry, That file already exists in the WordPress media library.' ) );
  242. //Ok, Its in the uploads folder, But NOT in WordPress's media library.
  243. if ( 'file' == $import_date ) {
  244. $time = @filemtime($file);
  245. if ( preg_match("|(\d+)/(\d+)|", $mat[1], $datemat) ) { //So lets set the date of the import to the date folder its in, IF its in a date folder.
  246. $hour = $min = $sec = 0;
  247. $day = 1;
  248. $year = $datemat[1];
  249. $month = $datemat[2];
  250. // If the files datetime is set, and it's in the same region of upload directory, set the minute details to that too, else, override it.
  251. if ( $time && date('Y-m', $time) == "$year-$month" )
  252. list($hour, $min, $sec, $day) = explode(';', date('H;i;s;j', $time) );
  253. $time = mktime($hour, $min, $sec, $month, $day, $year);
  254. }
  255. $time = gmdate( 'Y-m-d H:i:s', $time);
  256. // A new time has been found! Get the new uploads folder:
  257. // A writable uploads dir will pass this test. Again, there's no point overriding this one.
  258. if ( ! ( ( $uploads = wp_upload_dir($time) ) && false === $uploads['error'] ) )
  259. return new WP_Error( 'upload_error', $uploads['error']);
  260. $url = $uploads['baseurl'] . $mat[1];
  261. }
  262. } else {
  263. $filename = wp_unique_filename( $uploads['path'], basename($file));
  264. // copy the file to the uploads dir
  265. $new_file = $uploads['path'] . '/' . $filename;
  266. if ( false === @copy( $file, $new_file ) )
  267. return new WP_Error('upload_error', sprintf( __('The selected file could not be copied to %s.', 'add-from-server'), $uploads['path']) );
  268. // Set correct file permissions
  269. $stat = stat( dirname( $new_file ));
  270. $perms = $stat['mode'] & 0000666;
  271. @ chmod( $new_file, $perms );
  272. // Compute the URL
  273. $url = $uploads['url'] . '/' . $filename;
  274. if ( 'file' == $import_date )
  275. $time = gmdate( 'Y-m-d H:i:s', @filemtime($file));
  276. }
  277. //Apply upload filters
  278. $return = apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ) );
  279. $new_file = $return['file'];
  280. $url = $return['url'];
  281. $type = $return['type'];
  282. $title = preg_replace('!\.[^.]+$!', '', basename($file));
  283. $content = '';
  284. // use image exif/iptc data for title and caption defaults if possible
  285. if ( $image_meta = @wp_read_image_metadata($new_file) ) {
  286. if ( '' != trim($image_meta['title']) )
  287. $title = trim($image_meta['title']);
  288. if ( '' != trim($image_meta['caption']) )
  289. $content = trim($image_meta['caption']);
  290. }
  291. if ( $time ) {
  292. $post_date_gmt = $time;
  293. $post_date = $time;
  294. } else {
  295. $post_date = current_time('mysql');
  296. $post_date_gmt = current_time('mysql', 1);
  297. }
  298. // Construct the attachment array
  299. $attachment = array(
  300. 'post_mime_type' => $type,
  301. 'guid' => $url,
  302. 'post_parent' => $post_id,
  303. 'post_title' => $title,
  304. 'post_name' => $title,
  305. 'post_content' => $content,
  306. 'post_date' => $post_date,
  307. 'post_date_gmt' => $post_date_gmt
  308. );
  309. $attachment = apply_filters('afs-import_details', $attachment, $file, $post_id, $import_date);
  310. //Win32 fix:
  311. $new_file = str_replace( strtolower(str_replace('\\', '/', $uploads['basedir'])), $uploads['basedir'], $new_file);
  312. // Save the data
  313. $id = wp_insert_attachment($attachment, $new_file, $post_id);
  314. if ( !is_wp_error($id) ) {
  315. $data = wp_generate_attachment_metadata( $id, $new_file );
  316. wp_update_attachment_metadata( $id, $data );
  317. }
  318. //update_post_meta( $id, '_wp_attached_file', $uploads['subdir'] . '/' . $filename );
  319. return $id;
  320. }
  321. //Create the content for the page
  322. function main_content() {
  323. global $pagenow;
  324. $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0;
  325. $import_to_gallery = isset($_POST['gallery']) && 'on' == $_POST['gallery'];
  326. if ( ! $import_to_gallery && !isset($_REQUEST['cwd']) )
  327. $import_to_gallery = true; // cwd should always be set, if it's not, and neither is gallery, this must be the first page load.
  328. $import_date = isset($_REQUEST['import-date']) ? $_REQUEST['import-date'] : 'file';
  329. if ( 'upload.php' == $pagenow )
  330. $url = admin_url('upload.php?page=add-from-server');
  331. else
  332. $url = admin_url('media-upload.php?tab=server');
  333. if ( $post_id )
  334. $url = add_query_arg('post_id', $post_id, $url);
  335. $cwd = trailingslashit(get_option('frmsvr_last_folder', WP_CONTENT_DIR));
  336. if ( isset($_REQUEST['directory']) )
  337. $cwd .= stripslashes(urldecode($_REQUEST['directory']));
  338. if ( isset($_REQUEST['adirectory']) && empty($_REQUEST['adirectory']) )
  339. $_REQUEST['adirectory'] = '/'; //For good measure.
  340. if ( isset($_REQUEST['adirectory']) )
  341. $cwd = stripslashes(urldecode($_REQUEST['adirectory']));
  342. $cwd = preg_replace('![^/]*/\.\./!', '', $cwd);
  343. $cwd = preg_replace('!//!', '/', $cwd);
  344. if ( ! is_readable($cwd) && is_readable( $this->get_root() . '/' . ltrim($cwd, '/') ) )
  345. $cwd = $this->get_root() . '/' . ltrim($cwd, '/');
  346. if ( ! is_readable($cwd) && get_option('frmsvr_last_folder') )
  347. $cwd = get_option('frmsvr_last_folder');
  348. if ( ! is_readable($cwd) )
  349. $cwd = WP_CONTENT_DIR;
  350. if ( strpos($cwd, $this->get_root()) === false )
  351. $cwd = $this->get_root();
  352. $cwd = str_replace('\\', '/', $cwd);
  353. if ( strlen($cwd) > 1 )
  354. $cwd = untrailingslashit($cwd);
  355. if ( ! is_readable($cwd) ) {
  356. echo '<div class="error"><p>';
  357. _e('<strong>Error:</strong> This users root directory is not readable. Please have your site administrator correct the <em>Add From Server</em> root directory settings.', 'add-from-server');
  358. echo '</p></div>';
  359. return;
  360. }
  361. update_option('frmsvr_last_folder', $cwd);
  362. $files = $this->find_files($cwd, array('levels' => 1));
  363. $parts = explode('/', ltrim(str_replace($this->get_root(), '/', $cwd), '/'));
  364. if ( $parts[0] != '' )
  365. $parts = array_merge(array(''), $parts);
  366. $dir = $cwd;
  367. $dirparts = '';
  368. for ( $i = count($parts)-1; $i >= 0; $i-- ) {
  369. $piece = $parts[$i];
  370. $adir = implode('/', array_slice($parts, 0, $i+1));
  371. if ( strlen($adir) > 1 )
  372. $adir = ltrim($adir, '/');
  373. $durl = esc_url(add_query_arg(array('adirectory' => $adir ), $url));
  374. $dirparts = '<a href="' . $durl . '">' . $piece . DIRECTORY_SEPARATOR . '</a>' . $dirparts;
  375. $dir = dirname($dir);
  376. }
  377. unset($dir, $piece, $adir, $durl);
  378. ?>
  379. <div class="frmsvr_wrap">
  380. <p><?php printf(__('<strong>Current Directory:</strong> <span id="cwd">%s</span>', 'add-from-server'), $dirparts) ?></p>
  381. <?php
  382. $quickjumps = array();
  383. $quickjumps[] = array( __('WordPress Root', 'add-from-server'), ABSPATH );
  384. if ( ( $uploads = wp_upload_dir() ) && false === $uploads['error'] )
  385. $quickjumps[] = array( __('Uploads Folder', 'add-from-server'), $uploads['path']);
  386. $quickjumps[] = array( __('Content Folder', 'add-from-server'), WP_CONTENT_DIR );
  387. $quickjumps = apply_filters('frmsvr_quickjumps', $quickjumps);
  388. if ( ! empty($quickjumps) ) {
  389. $pieces = array();
  390. foreach( $quickjumps as $jump ) {
  391. list( $text, $adir ) = $jump;
  392. $adir = str_replace('\\', '/', strtolower($adir));
  393. if ( strpos($adir, $this->get_root()) === false )
  394. continue;
  395. $adir = preg_replace('!^' . preg_quote($this->get_root(), '!') . '!i', '', $adir);
  396. if ( strlen($adir) > 1 )
  397. $adir = ltrim($adir, '/');
  398. $durl = add_query_arg(array('adirectory' => addslashes($adir)), $url);
  399. $pieces[] = "<a href='$durl'>$text</a>";
  400. }
  401. if ( ! empty($pieces) ) {
  402. echo '<p>';
  403. printf( __('<strong>Quick Jump:</strong> %s', 'add-from-server'), implode(' | ', $pieces) );
  404. echo '</p>';
  405. }
  406. }
  407. ?>
  408. <form method="post" action="<?php echo $url ?>">
  409. <?php if ( 'media-upload.php' == $GLOBALS['pagenow'] && $post_id > 0 ) : ?>
  410. <p><?php printf(__('Once you have selected files to be imported, Head over to the <a href="%s">Media Library tab</a> to add them to your post.', 'add-from-server'), esc_url(admin_url('media-upload.php?type=image&tab=library&post_id=' . $post_id)) ); ?></p>
  411. <?php endif; ?>
  412. <table class="widefat">
  413. <thead>
  414. <tr>
  415. <th class="check-column"><input type='checkbox' /></th>
  416. <th><?php _e('File', 'add-from-server'); ?></th>
  417. </tr>
  418. </thead>
  419. <tbody>
  420. <?php
  421. $parent = dirname($cwd);
  422. if ( (strpos($parent, $this->get_root()) === 0) && is_readable($parent) ) :
  423. $parent = preg_replace('!^' . preg_quote($this->get_root(), '!') . '!i', '', $parent);
  424. ?>
  425. <tr>
  426. <td>&nbsp;</td>
  427. <?php /* <td class='check-column'><input type='checkbox' id='file-<?php echo $sanname; ?>' name='files[]' value='<?php echo esc_attr($file) ?>' /></td> */ ?>
  428. <td><a href="<?php echo add_query_arg(array('adirectory' => $parent), $url) ?>" title="<?php echo esc_attr(dirname($cwd)) ?>"><?php _e('Parent Folder', 'add-from-server') ?></a></td>
  429. </tr>
  430. <?php endif; ?>
  431. <?php
  432. $directories = array();
  433. foreach( (array)$files as $key => $file ) {
  434. if ( '/' == substr($file, -1) ) {
  435. $directories[] = $file;
  436. unset($files[$key]);
  437. }
  438. }
  439. sort($directories);
  440. sort($files);
  441. foreach( (array)$directories as $file ) :
  442. $filename = preg_replace('!^' . preg_quote($cwd) . '!i', '', $file);
  443. $filename = ltrim($filename, '/');
  444. $folder_url = add_query_arg(array('directory' => $filename, 'import-date' => $import_date, 'gallery' => $import_to_gallery ), $url);
  445. ?>
  446. <tr>
  447. <td>&nbsp;</td>
  448. <?php /* <td class='check-column'><input type='checkbox' id='file-<?php echo $sanname; ?>' name='files[]' value='<?php echo esc_attr($file) ?>' /></td> */ ?>
  449. <td><a href="<?php echo $folder_url ?>"><?php echo rtrim($filename, '/') . DIRECTORY_SEPARATOR ?></a></td>
  450. </tr>
  451. <?php
  452. endforeach;
  453. $names = $rejected_files = $unreadable_files = array();
  454. $unfiltered_upload = current_user_can( 'unfiltered_upload' );
  455. foreach ( (array)$files as $key => $file ) {
  456. if ( ! $unfiltered_upload ) {
  457. $wp_filetype = wp_check_filetype( $file );
  458. if ( false === $wp_filetype['type'] ) {
  459. $rejected_files[] = $file;
  460. unset($files[$key]);
  461. continue;
  462. }
  463. }
  464. if ( ! is_readable($file) ) {
  465. $unreadable_files[] = $file;
  466. unset($files[$key]);
  467. continue;
  468. }
  469. }
  470. foreach ( array( 'meets_guidelines' => $files, 'unreadable' => $unreadable_files, 'doesnt_meets_guidelines' => $rejected_files) as $key => $_files ) :
  471. $file_meets_guidelines = $unfiltered_upload || ('meets_guidelines' == $key);
  472. $unreadable = 'unreadable' == $key;
  473. foreach ( $_files as $file ) :
  474. $classes = array();
  475. if ( ! $file_meets_guidelines )
  476. $classes[] = 'doesnt-meet-guidelines';
  477. if ( $unreadable )
  478. $classes[] = 'unreadable';
  479. if ( preg_match('/\.(.+)$/i', $file, $ext_match) )
  480. $classes[] = 'filetype-' . $ext_match[1];
  481. $filename = preg_replace('!^' . preg_quote($cwd) . '!', '', $file);
  482. $filename = ltrim($filename, '/');
  483. $sanname = preg_replace('![^a-zA-Z0-9]!', '', $filename);
  484. $i = 0;
  485. while ( in_array($sanname, $names) )
  486. $sanname = preg_replace('![^a-zA-Z0-9]!', '', $filename) . '-' . ++$i;
  487. $names[] = $sanname;
  488. ?>
  489. <tr class="<?php echo esc_attr(implode(' ', $classes)); ?>" title="<?php if ( ! $file_meets_guidelines ) { _e('Sorry, this file type is not permitted for security reasons. Please see the FAQ.', 'add-from-server'); } elseif ($unreadable) { _e('Sorry, but this file is unreadable by your Webserver. Perhaps check your File Permissions?', 'add-from-server'); } ?>">
  490. <th class='check-column'><input type='checkbox' id='file-<?php echo $sanname; ?>' name='files[]' value='<?php echo esc_attr($filename) ?>' <?php disabled(!$file_meets_guidelines || $unreadable); ?> /></th>
  491. <td><label for='file-<?php echo $sanname; ?>'><?php echo $filename ?></label></td>
  492. </tr>
  493. <?php endforeach; endforeach;?>
  494. </tbody>
  495. <tfoot>
  496. <tr>
  497. <th class="check-column"><input type='checkbox' /></th>
  498. <th><?php _e('File', 'add-from-server'); ?></th>
  499. </tr>
  500. </tfoot>
  501. </table>
  502. <fieldset>
  503. <legend><?php _e('Import Options', 'add-from-server'); ?></legend>
  504. <?php if ( $post_id != 0 ) : ?>
  505. <input type="checkbox" name="gallery" id="gallery-import" <?php checked( $import_to_gallery ); ?> /> <label for="gallery-import"><?php _e('Attach imported files to this post', 'add-from-server')?></label>
  506. <br class="clear" />
  507. <?php endif; ?>
  508. <?php _e('Set the imported date to the', 'add-from-server'); ?>
  509. <input type="radio" name="import-date" id="import-time-currenttime" value="current" <?php checked('current', $import_date); ?> /> <label for="import-time-currenttime"><?php _e('Current Time', 'add-from-server'); ?></label>
  510. <input type="radio" name="import-date" id="import-time-filetime" value="file" <?php checked('file', $import_date); ?> /> <label for="import-time-filetime"><?php _e('File Time', 'add-from-server'); ?></label>
  511. <?php if ( $post_id != 0 ) : ?>
  512. <input type="radio" name="import-date" id="import-time-posttime" value="post" <?php checked('post', $import_date); ?> /> <label for="import-time-posttime"><?php _e('Post Time', 'add-from-server'); ?></label>
  513. <?php endif; ?>
  514. </fieldset>
  515. <br class="clear" />
  516. <input type="hidden" name="cwd" value="<?php echo esc_attr( $cwd ); ?>" />
  517. <?php submit_button( __('Import', 'add-from-server'), 'primary', 'import', false); ?>
  518. </form>
  519. </div>
  520. <?php
  521. }
  522. //HELPERS
  523. function find_files( $folder, $args = array() ) {
  524. if ( strlen($folder) > 1 )
  525. $folder = untrailingslashit($folder);
  526. $defaults = array( 'pattern' => '', 'levels' => 100, 'relative' => '' );
  527. $r = wp_parse_args($args, $defaults);
  528. extract($r, EXTR_SKIP);
  529. //Now for recursive calls, clear relative, we'll handle it, and decrease the levels.
  530. unset($r['relative']);
  531. --$r['levels'];
  532. if ( ! $levels )
  533. return array();
  534. if ( ! is_readable($folder) )
  535. return array();
  536. $files = array();
  537. if ( $dir = @opendir( $folder ) ) {
  538. while ( ( $file = readdir($dir) ) !== false ) {
  539. if ( in_array($file, array('.', '..') ) )
  540. continue;
  541. if ( is_dir( $folder . '/' . $file ) ) {
  542. $files2 = $this->find_files( $folder . '/' . $file, $r );
  543. if( $files2 )
  544. $files = array_merge($files, $files2 );
  545. else if ( empty($pattern) || preg_match('|^' . str_replace('\*', '\w+', preg_quote($pattern)) . '$|i', $file) )
  546. $files[] = $folder . '/' . $file . '/';
  547. } else {
  548. if ( empty($pattern) || preg_match('|^' . str_replace('\*', '\w+', preg_quote($pattern)) . '$|i', $file) )
  549. $files[] = $folder . '/' . $file;
  550. }
  551. }
  552. }
  553. @closedir( $dir );
  554. if ( ! empty($relative) ) {
  555. $relative = trailingslashit($relative);
  556. foreach ( $files as $key => $file )
  557. $files[$key] = preg_replace('!^' . preg_quote($relative) . '!', '', $file);
  558. }
  559. return $files;
  560. }
  561. }//end class
  562. ?>