$function"; $caller[] = $function; } unset($caller[count($caller) - 1]); return $caller; } /** * Trims the text to given number of words. * * @param string $text text to trim * @param int $words_count words to trim to * @return string trimmed text */ static function trim_to_words($text, $words_count = 10) { if ($words_count > 0) { $words = explode(' ', $text, $words_count + 1); if (count($words) > $words_count) { $words = array_slice($words, 0, $words_count); $text = implode(' ', $words)."..."; } } return $text; } /** * Adds zeroes to set length. * * @param string $text original number * @param int $len max number of zeroes * @return string prefilled text */ static function prefill_zeros($text, $len) { $count = strlen($text); $zeros = ""; for ($i = 0; $i < $len - $count; $i++) $zeros.= "0"; return $zeros.$text; } /** * Splits string into array on size. * * @param string $string string to split * @param int $chunk lenght of each element * @return array split string */ static function split_by_length($string, $chunk = 1) { $result = array(); $strlnght = strlen($string); $x = 0; while($x < ($strlnght / $chunk)){ $result[] = substr($string, ($x * $chunk), $chunk); $x++; } return $result; } /** * Finds image url from text. * * @param string $text text to search * @return string image url */ static function get_image_from_text($text) { $imageurl = ""; preg_match('/<\s*img [^\>]*src\s*=\s*[\""\']?([^\""\'>]*)/i', $text, $matches); $imageurl = $matches[1]; return $imageurl; } /** * Counts files in a folder. * * @param string $path folder path * @return int number of files in the folder */ static function get_folder_files_count($path) { if (!file_exists($path)) return 0; if (is_file($path)) return filesize($path); $ret = 0; foreach(glob($path."/*") as $fn) $ret++; return $ret; } /** * Gets folder size. * * @param string $path folder path * @return int folder size */ static function get_folder_size($path) { if (!file_exists($path)) return 0; if (is_file($path)) return filesize($path); $ret = 0; foreach(glob($path."/*") as $fn) $ret += gdFunctionsGDSR::get_folder_size($fn); return $ret; } /** * Scans the folder and returns all the files and folder in it. * * @param string $path path of the folder to scan * @return array list of files and folders in the folder */ static function scan_dir($path) { if (function_exists("scandir")) { return scandir($path); } else { $dh = opendir($path); $files = array(); while (false !== ($filename = readdir($dh))) { $files[] = $filename; } closedir($dh); return $files; } } /** * Gets the folder permissions as a string. * * @param string $path path to file or folder * @return string file permissions */ static function file_permission($path) { return substr(sprintf('%o', fileperms($path)), -4); } /** * Scans folder and adds all folders to array. * * @param string $path folder path * @return array founded folders in array */ static function get_folders($path) { $folders = gdFunctionsGDSR::scan_dir($path); $import = array(); foreach ($folders as $folder) { if (substr($folder, 0, 1) != ".") { if (is_dir($path.$folder."/")) $import[] = $folder; } } return $import; } /** * Adds all new settings array elements and remove obsolete ones. * * @param array $old old settings * @param array $new new settings * @return array upgraded array */ static function upgrade_settings($old, $new) { foreach ($new as $key => $value) { if (!isset($old[$key])) $old[$key] = $value; } $unset = Array(); foreach ($old as $key => $value) { if (!isset($new[$key])) $unset[] = $key; } foreach ($unset as $key) { unset($old[$key]); } return $old; } /** * Adds missing default parameters into parameters array. * * @param array $defaults default parameters * @param array $attributes input parameters * @return array result */ static function prefill_attributes($defaults, $attributes) { $attributes = (array)$attributes; $result = array(); foreach($defaults as $name => $default) { if (array_key_exists($name, $attributes)) $result[$name] = $attributes[$name]; else $result[$name] = $default; } return $result; } /** * Formats byte based size into readable string * * @param int $size size in bytes * @return string formated string */ static function size_format($size) { if (strlen($size) <= 9 && strlen($size) >= 7) { $size = number_format($size / 1048576,1); return "$size MB"; } else if (strlen($size) >= 10) { $size = number_format($size / 1073741824,1); return "$size GB"; } else if (strlen($size) <= 6 && strlen($size) >= 4) { $size = number_format($size / 1024,1); return "$size KB"; } else return "$size B"; } /** * Recalcuates size from weight based string. * * @param string $size input string with k/m/g/t ending * @return int resulting size */ static function recalculate_size($size) { switch (strtolower(substr($size, -1))) { case "k": return $size * 1024; break; case "m": return $size * 1024 * 1024; break; case "g": return $size * 1024 * 1024 * 1024; break; case "t": return $size * 1024 * 1024 * 1024 * 1024; break; } return $size; } /** * Draw a pager for wordpress query loop. * * @global object $wp_query wordpress query object * @param string $query page element for url query * @param string $sign what sign to add before page part * @param bool $div draw div around the pager */ static function loop_pager($query = "pg", $sign = "?", $div = true) { global $wp_query; $numposts = $wp_query->found_posts; $max_page = $wp_query->max_num_pages; if ($max_page > 1) { $page = intval(get_query_var('paged')); if ($page == 0) $page++; $url = remove_query_arg($query); if ($div) echo '