/lib/classes/utils.class.php
PHP | 230 lines | 189 code | 24 blank | 17 comment | 29 complexity | d8049667198f9dba5453237c8a297632 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
- <?php
- class utils {
- static function order_array_num($array, $key, $order = "ASC") {
- $tmp = array();
- foreach ($array as $akey => $array2) {
- $tmp[$akey] = $array2[$key];
- }
- if ($order == "DESC") {
- arsort($tmp, SORT_STRING);
- } else {
- asort($tmp, SORT_STRING);
- }
- $tmp2 = array();
- foreach ($tmp as $key => $value) {
- $tmp2[$key] = $array[$key];
- }
- return $tmp2;
- }
- static function sksort(&$array, $subkey="id", $sort_descending=false, $keep_keys_in_sub = false) {
- $temp_array = $array;
- foreach ($temp_array as $key => &$value) {
- $sort = array();
- foreach ($value as $index => $val) {
- $sort[$index] = $val[$subkey];
- }
- asort($sort);
- $keys = array_keys($sort);
- $newValue = array();
- foreach ($keys as $index) {
- if ($keep_keys_in_sub)
- $newValue[$index] = $value[$index];
- else
- $newValue[] = $value[$index];
- }
- if ($sort_descending)
- $value = array_reverse($newValue, $keep_keys_in_sub);
- else
- $value = $newValue;
- }
- $array = $temp_array;
- }
- static function array_sort_2($array, $sort_field) {
- $tmp_array = array();
- for ($i = 0; $i <= sizeof($array) - 1; $i++)
- $tmp_array[] = $array[$i][$sort_field];
- sort($tmp_array);
- reset($tmp_array);
- $end_array = array();
- for ($i = 0; $i <= sizeof($tmp_array) - 1; $i++) {
- for ($j = 0; $j <= sizeof($array) - 1; $j++) {
- if ($array[$j][$sort_field] == 'founded')
- continue;
- if ($tmp_array[$i] == $array[$j][$sort_field]) {
- $end_array[$i] = $array[$j];
- $array[$j][$sort_field] = 'founded';
- break;
- }
- }
- }
- return $end_array;
- }
- static function fs_get_file_with_perfix($file, $perfix) {
- $file = str_replace('./', '', $file);
- $f_arr = explode('/', $file);
- $p = $f_arr[0];
- for ($i = 1; $i < sizeof($f_arr) - 1; $i++)
- $p .= '/' . $f_arr[$i];
- $p .= '/';
- $p .= utils::fs_get_name_of_file($f_arr[sizeof($f_arr) - 1]) . '_' . $perfix . '.' . utils::fs_get_extension_of_file($f_arr[sizeof($f_arr) - 1]);
- //if (!file_exists($p)) return $file;
- return $p;
- }
- static function fs_get_name_of_file($file_name) {
- $file_path = explode('/', $file_name);
- $file_name = $file_path[sizeof($file_path) - 1];
- return substr($file_name, 0, strpos($file_name, '.'));
- }
- static function fs_get_extension_of_file($file_name) {
- $file_path = explode('/', $file_name);
- $file_name = $file_path[sizeof($file_path) - 1];
- return substr($file_name, strpos($file_name, '.') + 1);
- }
- /**
- * ??????? ???????? ?????? ??????? ? ????????? ? ????.
- * @param string $src ???????? ????????.
- * @param string $dest ??? ????? ?? ?????.
- * @param int $width ??????.
- * @param int $height ??????.
- * @param int $quality ??????? ??????????.
- * @return bool;
- */
- static function img_resize($src, $dest, $width, $height, $quality=100) {
- if (!file_exists($src))
- return false;
- $size = getimagesize($src);
- if ($size === false)
- return false;
- $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
- $icfunc = "imagecreatefrom" . $format;
- if (!function_exists($icfunc))
- return false;
- if ($width > $size[0] && $height > $size[1])
- return true;
- $x_ratio = $width / $size[0];
- $y_ratio = $height / $size[1];
- $ratio = $height ? min($x_ratio, $y_ratio) : $x_ratio;
- $use_x_ratio = ($x_ratio == $ratio);
- $new_width = $use_x_ratio ? $width : ceil($size[0] * $ratio);
- $new_height = !$use_x_ratio ? $height : ceil($size[1] * $ratio);
- $new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2);
- $new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);
- $isrc = $icfunc($src);
- $idest = imagecreatetruecolor($new_width, $new_height);
- imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
- if ($dest)
- imagejpeg($idest, $dest, $quality);
- else
- imagejpeg($idest);
- imagedestroy($isrc);
- unset($isrc);
- imagedestroy($idest);
- unset($idest);
- return true;
- }
- /**
- * ??????? ???????? ?? ?????? ???????? ????????????? ? ????????? ??? ? ????
- * @param string $src ???????? ????????.
- * @param string $out ??? ????? ?? ?????.
- * @param int $quality ??????? ??????????.
- * @return bool;
- */
- static function img_crop($src, $out, $quality=100) {
- if (!file_exists($src))
- return false;
- $size = getimagesize($src);
- if ($size === false)
- return false;
- $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
- $icfunc = "imagecreatefrom" . $format;
- if (!function_exists($icfunc))
- return false;
- $isrc = $icfunc($src);
- $width = 150;
- $height = 150;
- $fromX = ceil($size[0] / 2) - ceil($width / 2);
- $fromY = ceil($size[1] / 2) - ceil($height / 2);
- $toX = ceil($size[0] / 2) + ceil($width / 2);
- $toY = ceil($size[1] / 2) + ceil($height / 2);
- $idest = imagecreatetruecolor($width, $height);
- imagecopy($idest, $isrc, 0, 0, $fromX, $fromY, $toX, $toY);
- imagejpeg($idest, $out, 100);
- ImageDestroy($idest);
- ImageDestroy($isrc);
- return true;
- }
- static function drop_tag($array, $key, $tag) {
- $s = sizeof($array);
- for ($i = 0; $i < $s; $i++) {
- if (!isset($array[$i][$key]) || empty($array[$i][$key]))
- continue;
- $array[$i][$key] = str_replace("<$tag>", "", $array[$i][$key]);
- $array[$i][$key] = str_replace("</$tag>", "", $array[$i][$key]);
- }
- return $array;
- }
- static function get_name_of_module($mod) {
- $xml = simplexml_load_file('lib/modules/' . $mod . '/xml/config.xml');
- return (string) $xml->module->title;
- }
- static function translit($str) {
- $tr = array(
- "?" => "A", "?" => "B", "?" => "V", "?" => "G",
- "?" => "D", "?" => "E", "?" => "J", "?" => "Z", "?" => "I",
- "?" => "Y", "?" => "K", "?" => "L", "?" => "M", "?" => "N",
- "?" => "O", "?" => "P", "?" => "R", "?" => "S", "?" => "T",
- "?" => "U", "?" => "F", "?" => "H", "?" => "TS", "?" => "CH",
- "?" => "SH", "?" => "SCH", "?" => "", "?" => "YI", "?" => "",
- "?" => "E", "?" => "YU", "?" => "YA", "?" => "a", "?" => "b",
- "?" => "v", "?" => "g", "?" => "d", "?" => "e", "?" => "j",
- "?" => "z", "?" => "i", "?" => "y", "?" => "k", "?" => "l",
- "?" => "m", "?" => "n", "?" => "o", "?" => "p", "?" => "r",
- "?" => "s", "?" => "t", "?" => "u", "?" => "f", "?" => "h",
- "?" => "ts", "?" => "ch", "?" => "sh", "?" => "sch", "?" => "y",
- "?" => "yi", "?" => "", "?" => "e", "?" => "yu", "?" => "ya"
- );
- return strtr($str, $tr);
- }
- static function is_capital_char($char){
- $chars = array("Q","W","E","R","T","Y","U","I","O","P","A","S","D","F"
- ,"G","H","J","K","L","Z","X","C","V","B","N","M","?","?"
- ,"?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?"
- ,"?","?","?","?","?","?","?","?","?");
- $s = sizeof($chars);
- for ($i=0;$i<$s;$i++){
- if ($char == $chars[$i]) return true;
- }
- return false;
- }
- static public function mailto($title,$txt,$to){
- $header = "From: \"Shiva CMS\" <cms@" . str_replace(array("http:\\", "www.", '\\'), "", url) . ">\n";
- $header .= "Content-type: text/plain; charset=\"utf-8\"";
- return mail($to, $title, $txt, $header);
- }
-
- }
- ?>