PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/classes/utils.class.php

https://bitbucket.org/seyfer/safesmoking.ru
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
  1. <?php
  2. class utils {
  3. static function order_array_num($array, $key, $order = "ASC") {
  4. $tmp = array();
  5. foreach ($array as $akey => $array2) {
  6. $tmp[$akey] = $array2[$key];
  7. }
  8. if ($order == "DESC") {
  9. arsort($tmp, SORT_STRING);
  10. } else {
  11. asort($tmp, SORT_STRING);
  12. }
  13. $tmp2 = array();
  14. foreach ($tmp as $key => $value) {
  15. $tmp2[$key] = $array[$key];
  16. }
  17. return $tmp2;
  18. }
  19. static function sksort(&$array, $subkey="id", $sort_descending=false, $keep_keys_in_sub = false) {
  20. $temp_array = $array;
  21. foreach ($temp_array as $key => &$value) {
  22. $sort = array();
  23. foreach ($value as $index => $val) {
  24. $sort[$index] = $val[$subkey];
  25. }
  26. asort($sort);
  27. $keys = array_keys($sort);
  28. $newValue = array();
  29. foreach ($keys as $index) {
  30. if ($keep_keys_in_sub)
  31. $newValue[$index] = $value[$index];
  32. else
  33. $newValue[] = $value[$index];
  34. }
  35. if ($sort_descending)
  36. $value = array_reverse($newValue, $keep_keys_in_sub);
  37. else
  38. $value = $newValue;
  39. }
  40. $array = $temp_array;
  41. }
  42. static function array_sort_2($array, $sort_field) {
  43. $tmp_array = array();
  44. for ($i = 0; $i <= sizeof($array) - 1; $i++)
  45. $tmp_array[] = $array[$i][$sort_field];
  46. sort($tmp_array);
  47. reset($tmp_array);
  48. $end_array = array();
  49. for ($i = 0; $i <= sizeof($tmp_array) - 1; $i++) {
  50. for ($j = 0; $j <= sizeof($array) - 1; $j++) {
  51. if ($array[$j][$sort_field] == 'founded')
  52. continue;
  53. if ($tmp_array[$i] == $array[$j][$sort_field]) {
  54. $end_array[$i] = $array[$j];
  55. $array[$j][$sort_field] = 'founded';
  56. break;
  57. }
  58. }
  59. }
  60. return $end_array;
  61. }
  62. static function fs_get_file_with_perfix($file, $perfix) {
  63. $file = str_replace('./', '', $file);
  64. $f_arr = explode('/', $file);
  65. $p = $f_arr[0];
  66. for ($i = 1; $i < sizeof($f_arr) - 1; $i++)
  67. $p .= '/' . $f_arr[$i];
  68. $p .= '/';
  69. $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]);
  70. //if (!file_exists($p)) return $file;
  71. return $p;
  72. }
  73. static function fs_get_name_of_file($file_name) {
  74. $file_path = explode('/', $file_name);
  75. $file_name = $file_path[sizeof($file_path) - 1];
  76. return substr($file_name, 0, strpos($file_name, '.'));
  77. }
  78. static function fs_get_extension_of_file($file_name) {
  79. $file_path = explode('/', $file_name);
  80. $file_name = $file_path[sizeof($file_path) - 1];
  81. return substr($file_name, strpos($file_name, '.') + 1);
  82. }
  83. /**
  84. * ??????? ???????? ?????? ??????? ? ????????? ? ????.
  85. * @param string $src ???????? ????????.
  86. * @param string $dest ??? ????? ?? ?????.
  87. * @param int $width ??????.
  88. * @param int $height ??????.
  89. * @param int $quality ??????? ??????????.
  90. * @return bool;
  91. */
  92. static function img_resize($src, $dest, $width, $height, $quality=100) {
  93. if (!file_exists($src))
  94. return false;
  95. $size = getimagesize($src);
  96. if ($size === false)
  97. return false;
  98. $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
  99. $icfunc = "imagecreatefrom" . $format;
  100. if (!function_exists($icfunc))
  101. return false;
  102. if ($width > $size[0] && $height > $size[1])
  103. return true;
  104. $x_ratio = $width / $size[0];
  105. $y_ratio = $height / $size[1];
  106. $ratio = $height ? min($x_ratio, $y_ratio) : $x_ratio;
  107. $use_x_ratio = ($x_ratio == $ratio);
  108. $new_width = $use_x_ratio ? $width : ceil($size[0] * $ratio);
  109. $new_height = !$use_x_ratio ? $height : ceil($size[1] * $ratio);
  110. $new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2);
  111. $new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);
  112. $isrc = $icfunc($src);
  113. $idest = imagecreatetruecolor($new_width, $new_height);
  114. imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]);
  115. if ($dest)
  116. imagejpeg($idest, $dest, $quality);
  117. else
  118. imagejpeg($idest);
  119. imagedestroy($isrc);
  120. unset($isrc);
  121. imagedestroy($idest);
  122. unset($idest);
  123. return true;
  124. }
  125. /**
  126. * ??????? ???????? ?? ?????? ???????? ????????????? ? ????????? ??? ? ????
  127. * @param string $src ???????? ????????.
  128. * @param string $out ??? ????? ?? ?????.
  129. * @param int $quality ??????? ??????????.
  130. * @return bool;
  131. */
  132. static function img_crop($src, $out, $quality=100) {
  133. if (!file_exists($src))
  134. return false;
  135. $size = getimagesize($src);
  136. if ($size === false)
  137. return false;
  138. $format = strtolower(substr($size['mime'], strpos($size['mime'], '/') + 1));
  139. $icfunc = "imagecreatefrom" . $format;
  140. if (!function_exists($icfunc))
  141. return false;
  142. $isrc = $icfunc($src);
  143. $width = 150;
  144. $height = 150;
  145. $fromX = ceil($size[0] / 2) - ceil($width / 2);
  146. $fromY = ceil($size[1] / 2) - ceil($height / 2);
  147. $toX = ceil($size[0] / 2) + ceil($width / 2);
  148. $toY = ceil($size[1] / 2) + ceil($height / 2);
  149. $idest = imagecreatetruecolor($width, $height);
  150. imagecopy($idest, $isrc, 0, 0, $fromX, $fromY, $toX, $toY);
  151. imagejpeg($idest, $out, 100);
  152. ImageDestroy($idest);
  153. ImageDestroy($isrc);
  154. return true;
  155. }
  156. static function drop_tag($array, $key, $tag) {
  157. $s = sizeof($array);
  158. for ($i = 0; $i < $s; $i++) {
  159. if (!isset($array[$i][$key]) || empty($array[$i][$key]))
  160. continue;
  161. $array[$i][$key] = str_replace("<$tag>", "", $array[$i][$key]);
  162. $array[$i][$key] = str_replace("</$tag>", "", $array[$i][$key]);
  163. }
  164. return $array;
  165. }
  166. static function get_name_of_module($mod) {
  167. $xml = simplexml_load_file('lib/modules/' . $mod . '/xml/config.xml');
  168. return (string) $xml->module->title;
  169. }
  170. static function translit($str) {
  171. $tr = array(
  172. "?" => "A", "?" => "B", "?" => "V", "?" => "G",
  173. "?" => "D", "?" => "E", "?" => "J", "?" => "Z", "?" => "I",
  174. "?" => "Y", "?" => "K", "?" => "L", "?" => "M", "?" => "N",
  175. "?" => "O", "?" => "P", "?" => "R", "?" => "S", "?" => "T",
  176. "?" => "U", "?" => "F", "?" => "H", "?" => "TS", "?" => "CH",
  177. "?" => "SH", "?" => "SCH", "?" => "", "?" => "YI", "?" => "",
  178. "?" => "E", "?" => "YU", "?" => "YA", "?" => "a", "?" => "b",
  179. "?" => "v", "?" => "g", "?" => "d", "?" => "e", "?" => "j",
  180. "?" => "z", "?" => "i", "?" => "y", "?" => "k", "?" => "l",
  181. "?" => "m", "?" => "n", "?" => "o", "?" => "p", "?" => "r",
  182. "?" => "s", "?" => "t", "?" => "u", "?" => "f", "?" => "h",
  183. "?" => "ts", "?" => "ch", "?" => "sh", "?" => "sch", "?" => "y",
  184. "?" => "yi", "?" => "", "?" => "e", "?" => "yu", "?" => "ya"
  185. );
  186. return strtr($str, $tr);
  187. }
  188. static function is_capital_char($char){
  189. $chars = array("Q","W","E","R","T","Y","U","I","O","P","A","S","D","F"
  190. ,"G","H","J","K","L","Z","X","C","V","B","N","M","?","?"
  191. ,"?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?","?"
  192. ,"?","?","?","?","?","?","?","?","?");
  193. $s = sizeof($chars);
  194. for ($i=0;$i<$s;$i++){
  195. if ($char == $chars[$i]) return true;
  196. }
  197. return false;
  198. }
  199. static public function mailto($title,$txt,$to){
  200. $header = "From: \"Shiva CMS\" <cms@" . str_replace(array("http:\\", "www.", '\\'), "", url) . ">\n";
  201. $header .= "Content-type: text/plain; charset=\"utf-8\"";
  202. return mail($to, $title, $txt, $header);
  203. }
  204. }
  205. ?>