This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ if ( ! function_exists('akv_create_upload_folder') ) : /** * Creates a subdirectory within the WordPress uploads folder. * * @param string $name New subdirectory name. * @return void */ function akv_create_upload_folder( $name ) { $uploads = wp_upload_dir(); $dir = trailingslashit($uploads['basedir']) . $name; wp_mkdir_p($dir); } endif; if ( ! function_exists('akv_max_upload') ) : /** * Returns the system max filesize for uploads. * This depends on the directives upload_max_filesize, post_max_size and memory_limit. The lowest of them is the limit. * * @return int Max system filesize upload (in bytes). */ function akv_max_upload() { $file = akv_return_bytes(ini_get('upload_max_filesize')); $post = akv_return_bytes(ini_get('post_max_size')); $mem = akv_return_bytes(ini_get('memory_limit')); return min($file, $post, $mem); } endif; if ( ! function_exists('akv_return_bytes') ) : /** * Converts a PHP config value to bytes. * * @param $string PHP config value, as readed with ini_get() * @return int Value in bytes. */ function akv_return_bytes( $value ) { $val = trim($value); $unit = strtoupper($val[strlen($val)-1]); switch ( $unit ) { case 'G': // GigaBytes $val *= 1024; case 'M': // MegaBytes $val *= 1024; case 'K': // KiloBytes $val *= 1024; } return $val; } endif; if ( ! function_exists('akv_return_units') ) : /** * Formats a value in bytes to the bigger unit. (Gb, Mb, Kb). * * @param int $value Value to convert. * @return string Formated value. */ function akv_return_units( $value, $max = 'G' ) { $max = strtoupper($max); $unit = 'bytes'; if ( $value >= 1024 ) { $value /= 1024; $unit = 'Kb'; } if ( $value >= 1024 && ( 'M' == $max || 'G' == $max ) ){ $value /= 1024; $unit = 'Mb'; } if ( $value >= 1024 && ( 'G' == $max) ) { $value /= 1024; $unit = 'Gb'; } $val = intval($value) . ' ' . $unit; return $val; } endif; if ( !function_exists('akv_dir_content') ) : /** * Creates and returns an ordered list for files in a given directori. * The function recurses into all directory tree. * * @param string $directory Directori where search for files. Absolute path. * @param array $args Options array to select wich files to return. Options: * - tree: recurses into subdirectories. 0 = No, 1 (default) = Yes * - extensions: array or comma delimited list of wanted extensions (default all extensions). * - with_ext: Return filename with extension. 0 = No, 1 (default) = Yes * @param boolean $withexst We want returned filenames with extension. Defaults true. * @return array List of files found. */ function akv_dir_content($directory, $args='') { $defaults = array( 'tree' => 1, // recurses into subdirectories (0 = No, 1 = Yes) 'extensions' => '', // array or comma delimited list of wanted extensions 'with_ext' => 1); // Return filename with extension (0 = No, 1 = Yes) $options = wp_parse_args($args, $defaults); if ( ! empty($options['extensions']) && ! is_array($options['extensions']) ) { $options['extensions'] = explode(',', $options['extensions']); } $tree = array(); // Directory could be empty. $handle = opendir($directory); while ( $filename = readdir($handle) ) { if ( substr($filename, 0, 1) != '.' ) { // No hidden files. if ( is_dir($directory .'/'. $filename) && $options['tree'] ) { $tree[$filename] = akv_dir_content($directory .'/'. $filename, $options); } else { $fileinfo = pathinfo($directory .'/'. $filename); if ( empty($options['extensions']) || in_array($fileinfo['extension'], $options['extensions']) ) { if ( ! $options['with_ext'] ) { $filename = substr($filename, 0, (strlen($fileinfo['extension']) + 1) * -1); } $tree[] = $filename; } } } } closedir($handle); asort($tree); return $tree; } endif;