function find_files($folder, $args = array()) { $folder = untrailingslashit($folder); $defaults = array('pattern' => '', 'levels' => 100, 'relative' => false); $r = wp_parse_args($args, $defaults); extract($r, EXTR_SKIP); //Now for recursive calls, clear relative, we'll handle it, and decrease the levels. unset($r['relative']); --$r['levels']; if (!$levels) { return array(); } if (!is_readable($folder)) { return false; } if (true === $relative) { $relative = $folder; } $files = array(); if ($dir = @opendir($folder)) { while (($file = readdir($dir)) !== false) { if (in_array($file, array('.', '..'))) { continue; } if (is_dir($folder . '/' . $file)) { $files2 = DD32::find_files($folder . '/' . $file, $r); if ($files2) { $files = array_merge($files, $files2); } else { if (empty($pattern) || preg_match('|^' . str_replace('\\*', '\\w+', preg_quote($pattern)) . '$|i', $file)) { $files[] = $folder . '/' . $file . '/'; } } } else { if (empty($pattern) || preg_match('|^' . str_replace('\\*', '\\w+', preg_quote($pattern)) . '$|i', $file)) { $files[] = $folder . '/' . $file; } } } } @closedir($dir); if (!empty($relative)) { $relative = trailingslashit($relative); foreach ($files as $key => $file) { $files[$key] = preg_replace('!^' . preg_quote($relative) . '!', '', $file); } } return $files; }
function main_content() { global $pagenow; $post_id = isset($_REQUEST['post_id']) ? intval($_REQUEST['post_id']) : 0; $import_to_gallery = !(isset($_POST['no-gallery']) && 'on' == $_POST['no-gallery']); if ('upload.php' == $pagenow) { $url = admin_url('upload.php?page=add-from-server'); } else { $url = admin_url('media-upload.php?tab=server'); } if ($post_id) { $url = add_query_arg('post_id', $post_id, $url); } $cwd = trailingslashit(get_option('frmsvr_last_folder', WP_CONTENT_DIR)); if (isset($_REQUEST['directory'])) { $cwd .= stripslashes(urldecode($_REQUEST['directory'])); } if (isset($_REQUEST['adirectory'])) { $cwd = stripslashes(urldecode($_REQUEST['adirectory'])); } $cwd = preg_replace('![^/]+/\\.\\./!', '', $cwd); $cwd = preg_replace('!//!', '/', $cwd); if (!is_readable($cwd) && get_option('frmsvr_last_folder')) { $cwd = get_option('frmsvr_last_folder'); } if (!is_readable($cwd)) { $cwd = WP_CONTENT_DIR; } $cwd = str_replace('\\', '/', $cwd); $cwd = untrailingslashit($cwd); update_option('frmsvr_last_folder', $cwd); $files = DD32::find_files($cwd, array('levels' => 1)); $parts = explode('/', rtrim($cwd, '/')); $dir = $cwd; $dirparts = ''; for ($i = count($parts) - 1; $i >= 0; $i--) { $piece = $parts[$i]; $adir = implode('/', array_slice($parts, 0, $i + 1)); $durl = clean_url(add_query_arg(array('adirectory' => $adir), $url)); $dirparts = "<a href='{$durl}'>{$piece}</a>/ {$dirparts}"; $dir = dirname($dir); } unset($dir, $piece, $adir, $durl); ?> <div class="frmsvr_wrap"> <p><?php printf(__('<strong>Current Directory:</strong> <span id="cwd">%s</span>', 'add-from-server'), $dirparts); ?> </p> <?php $quickjumps = array(); $quickjumps[] = array(__('WordPress Root', 'add-from-server'), ABSPATH); if (($uploads = wp_upload_dir()) && false === $uploads['error']) { $quickjumps[] = array(__('Uploads Folder', 'add-from-server'), $uploads['path']); } $quickjumps = apply_filters('frmsvr_quickjumps', $quickjumps); if (!empty($quickjumps)) { echo '<p>'; _e('<strong>Quick Jump:</strong> ', 'add-from-server'); $pieces = array(); foreach ($quickjumps as $jump) { list($text, $adir) = $jump; $adir = str_replace('\\', '/', $adir); $durl = add_query_arg(array('adirectory' => addslashes($adir)), $url); $pieces[] = "<a href='{$durl}'>{$text}</a>"; } echo implode(' | ', $pieces); echo '</p>'; } ?> <form method="post" action="<?php echo $url; ?> "> <?php if ('media-upload.php' == $GLOBALS['pagenow']) { ?> <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'), clean_url(admin_url('media-upload.php?type=image&tab=library&post_id=' . $post_id))); ?> </p> <?php } ?> <table class="widefat"> <thead> <tr> <th class="check-column"><input type='checkbox' /></th> <th><?php _e('File', 'add-from-server'); ?> </th> </tr> </thead> <tbody> <tr> <td> </td> <!-- <td class='check-column'><input type='checkbox' id='file-<?php echo $sanname; ?> ' name='files[]' value='<?php echo attribute_escape($file); ?> ' /></td> --> <td><a href="<?php echo add_query_arg(array('directory' => '../'), $url); ?> " title="<?php echo attribute_escape(dirname($cwd)); ?> "><?php _e('Parent Folder', 'add-from-server'); ?> </a></td> </tr> <?php $directories = array(); foreach ((array) $files as $key => $file) { if ('/' == substr($file, -1)) { $directories[] = $file; unset($files[$key]); } } sort($directories); sort($files); foreach ((array) $directories as $file) { $filename = preg_replace('!^' . preg_quote($cwd) . '!', '', $file); $filename = ltrim($filename, '/'); $folder_url = add_query_arg(array('directory' => $filename), $url); ?> <tr> <td> </td> <!-- <td class='check-column'><input type='checkbox' id='file-<?php echo $sanname; ?> ' name='files[]' value='<?php echo attribute_escape($file); ?> ' /></td> --> <td><a href="<?php echo $folder_url; ?> "><?php echo $filename; ?> </a></td> </tr> <?php } foreach ((array) $files as $file) { $filename = preg_replace('!^' . preg_quote($cwd) . '!', '', $file); $filename = ltrim($filename, '/'); $sanname = preg_replace('![^a-zA-Z0-9]!', '', $filename); ?> <tr> <td class='check-column'><input type='checkbox' id='file-<?php echo $sanname; ?> ' name='files[]' value='<?php echo attribute_escape($filename); ?> ' /></td> <td><label for='file-<?php echo $sanname; ?> '><?php echo $filename; ?> </label></td> </tr> <?php } ?> </tbody> <tfoot> <tr> <th class="check-column"><input type='checkbox' /></th> <th><?php _e('File', 'add-from-server'); ?> </th> </tr> </tfoot> </table> <p class="ml-submit"> <input type="hidden" name="cwd" value="<?php echo attribute_escape($cwd); ?> " /> <?php if ($post_id != 0) { ?> <input type="checkbox" name="no-gallery" id="no-gallery" <?php if (!$import_to_gallery) { echo 'checked="checked"'; } ?> /> <label for="no-gallery"><?php _e('Do not add selected files to current post Gallery', 'add-from-server'); ?> </label><br /> <?php } ?> <input type="submit" class="button savebutton" name="import" value="<?php echo attribute_escape(__('Import', 'add-from-server')); ?> " /> </p> </form> </div> <?php }