PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/system/functions.php

https://gitlab.com/kidaa/quantum
PHP | 293 lines | 241 code | 32 blank | 20 comment | 54 complexity | 3db48b725b67c10f655f073a95238e02 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. function magicQuotesFix() {
  3. if (ini_get('magic_quotes_gpc')) {
  4. function clean($data) {
  5. if (is_array($data)) {
  6. foreach ($data as $key => $value) {
  7. $data[clean($key)] = clean($value);
  8. }
  9. } else {
  10. $data = stripslashes($data);
  11. }
  12. return $data;
  13. }
  14. $_GET = clean($_GET);
  15. $_POST = clean($_POST);
  16. $_REQUEST = clean($_REQUEST);
  17. $_COOKIE = clean($_COOKIE);
  18. }
  19. }
  20. function compress($buffer) {
  21. //$buffer = str_replace("\r\n", "", $buffer);
  22. $buffer = preg_replace('/[\s]{2,}/', ' ', $buffer);
  23. return $buffer;
  24. }
  25. function getPermission($filename) {
  26. return substr(sprintf('%o', fileperms($filename)), -4);
  27. }
  28. function mysqlImport($mysqlDatabaseName, $mysqlUserName, $mysqlPassword, $mysqlHostName, $mysqlImportFilename) {
  29. $command = 'mysql -h' . $mysqlHostName . ' -u' . $mysqlUserName . ' -p' . $mysqlPassword . ' ' .
  30. $mysqlDatabaseName . ' < ' . $mysqlImportFilename;
  31. $output = array();
  32. exec($command, $output, $result);
  33. if ($result == 0) {
  34. return true;
  35. } else {
  36. return false;
  37. }
  38. }
  39. function mysqlExport($mysqlDatabaseName, $mysqlUserName, $mysqlPassword, $mysqlHostName, $mysqlExportPath) {
  40. $command = 'mysqldump --opt -h' . $mysqlHostName . ' -u' . $mysqlUserName . ' -p' . $mysqlPassword .
  41. ' ' . $mysqlDatabaseName . ' > ' . $mysqlExportPath;
  42. $output = array();
  43. exec($command, $output, $result);
  44. if ($result == 0) {
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. function zip($source, $destination) {
  51. if (!extension_loaded('zip') || !file_exists($source)) {
  52. return false;
  53. }
  54. $zip = new ZipArchive();
  55. if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
  56. return false;
  57. }
  58. $source = str_replace('\\', '/', realpath($source));
  59. if (is_dir($source) === true) {
  60. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source),
  61. RecursiveIteratorIterator::SELF_FIRST);
  62. foreach ($files as $file) {
  63. $file = str_replace('\\', '/', $file);
  64. // Ignore "." and ".." folders
  65. if (in_array(substr($file, strrpos($file, '/') + 1), array('.', '..')))
  66. continue;
  67. $file = realpath($file);
  68. if (is_dir($file) === true) {
  69. $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
  70. } else if (is_file($file) === true) {
  71. $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
  72. }
  73. }
  74. } else if (is_file($source) === true) {
  75. $zip->addFromString(basename($source), file_get_contents($source));
  76. }
  77. return $zip->close();
  78. }
  79. function str_replace_once($search, $replace, $text) {
  80. $pos = strpos($text, $search);
  81. return $pos !== false ? substr_replace($text, $replace, $pos, strlen($search)) : $text;
  82. }
  83. function makeRandomString($max = 9) {
  84. $i = 0;
  85. $possible_keys = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  86. $keys_length = strlen($possible_keys);
  87. $str = "";
  88. while ($i < $max) {
  89. $rand = mt_rand(1, $keys_length - 1);
  90. $str .= $possible_keys[$rand];
  91. $i++;
  92. }
  93. return $str;
  94. }
  95. function printPagination($page_count, $url, $centered = true, $backend = false, $visible_pages = 10) {
  96. // minus first, last and current number
  97. $visible_pages = $visible_pages - 3;
  98. // prevent incorrect current page number
  99. $_GET['page_n'] = intval($_GET['page_n']);
  100. $_GET['page_n'] = $_GET['page_n'] > 0 ? $_GET['page_n'] : 1;
  101. // calculating left-side numbers
  102. $left_numbers_count = floor($visible_pages / 2);
  103. // calculating right-side numbers
  104. $right_numbers_count = ceil($visible_pages / 2);
  105. // calculating left number
  106. $left_number = $_GET['page_n'] - $left_numbers_count;
  107. if ($left_number < 2) {
  108. $right_numbers_count += 2 - $left_number;
  109. $left_number = 2;
  110. }
  111. // calculating right number
  112. $right_number = $_GET['page_n'] + $right_numbers_count;
  113. if ($right_number > $page_count - 1) {
  114. $left_numbers_count += $right_number - $page_count - 1;
  115. // correcting left number
  116. $left_number = ($_GET['page_n'] - $left_numbers_count > 1) ? $_GET['page_n'] - $left_numbers_count : 2;
  117. $right_number = $page_count - 1;
  118. }
  119. // processing href
  120. $url = str_replace('page_n=' . $_GET['page_n'], '', $url);
  121. $url = isset($_GET['lang']) ? str_replace('&lang=' . $_GET['lang'], '', $url) : $url;
  122. $last_s = substr($url, -1);
  123. if (strpos($url, '?') === false) {
  124. $url .= '?';
  125. } elseif ($last_s != '?' && $last_s != '&') {
  126. $url .= '&';
  127. }
  128. // building result
  129. if ($page_count == 0) $page_count = 1;
  130. if ($backend) {
  131. // adding wrapper for bootstrap2
  132. if ($centered) $cls = ' pagination-centered'; else $cls = '';
  133. $tmp_res = '<div class="pagination' . $cls . '"><ul>';
  134. } else {
  135. // adding wrapper for bootstrap3
  136. if ($centered) $cls = ' center-block text-center'; else $cls = '';
  137. $tmp_res = '<div class="paginations ' . $cls . '"><ul class="pagination">';
  138. }
  139. // adding first item (arrow-left)
  140. $tmp_res .= '<li ';
  141. if ($_GET["page_n"] == 1) $tmp_res .= 'class="disabled"';
  142. $tmp_res .= '><a ';
  143. if ($_GET["page_n"] > 1) {
  144. if ($_GET["page_n"] == 2) {
  145. $tmp_res .= 'href="' . substr($url, 0, -1) . '"';
  146. } else {
  147. $tmp_res .= 'href="' . $url . 'page_n=' . ($_GET["page_n"] - 1) . '"';
  148. }
  149. }
  150. $tmp_res .= '>&laquo;</a></li><li ';
  151. // adding item "first page"
  152. if ($_GET["page_n"] == 1) $tmp_res .= 'class="active"';
  153. $tmp_res .= '><a ';
  154. if ($_GET["page_n"] > 1) $tmp_res .= 'href="' . substr($url, 0, -1) . '"';
  155. $tmp_res .= '>1</a></li>';
  156. // adding item ".." or "2"
  157. if ($left_number > 3) {
  158. $tmp_res .= '<li class="disable"><a>..</a></li>';
  159. } elseif ($left_number == 3) {
  160. $tmp_res .= '<li><a href="' . $url . 'page_n=2">2</a></li>';
  161. }
  162. // adding numbers
  163. for ($i = $left_number; $i <= $right_number; $i++) {
  164. $active = $i == $_GET['page_n'] ? ' class="active"' : '';
  165. $tmp_res .= '<li' . $active . '><a href="' . $url . 'page_n=' . $i . '">' . $i . '</a></li>';
  166. }
  167. // adding item ".." or "pre-last"
  168. if ($right_number < $page_count - 2) {
  169. $tmp_res .= '<li class="disable"><a>..</a></li>';
  170. } elseif ($right_number == $page_count - 2) {
  171. $tmp_res .= '<li><a href="' . $url . 'page_n=' . ($page_count - 1) . '">' . ($page_count - 1) . '</a></li>';
  172. }
  173. // adding item "last page"
  174. if ($page_count > 1) $tmp_res .= '<li><a href="' . $url . 'page_n=' . $page_count . '">' . $page_count . '</a></li>';
  175. // adding last item (arrow-right)
  176. $tmp_res .= '<li ';
  177. if ($page_count == 1 || $_GET["page_n"] == $page_count) $tmp_res .= 'class="disabled"';
  178. $tmp_res .= '><a ';
  179. if ($_GET["page_n"] < $page_count) $tmp_res .= 'href="' . $url . 'page_n=' . ($_GET["page_n"] + 1) . '"';
  180. $tmp_res .= ">&raquo;</a></li></ul></div>";
  181. return $tmp_res;
  182. }
  183. function captionToLink($str) {
  184. $str = htmlspecialchars(strip_tags(trim($str)));
  185. $str = str_replace('.', '-', $str);
  186. $str = str_replace(',', '-', $str);
  187. $str = str_replace('!', '-', $str);
  188. $str = str_replace(';', '-', $str);
  189. $str = str_replace(' ', '-', $str);
  190. $str = str_replace('"', '-', $str);
  191. $str = str_replace("'", '-', $str);
  192. $str = str_replace('&', '-', $str);
  193. $str = str_replace('<', '-', $str);
  194. $str = str_replace('>', '-', $str);
  195. $str = str_replace('/', '-', $str);
  196. $str = str_replace('?', '-', $str);
  197. $str = str_replace('+', '-', $str);
  198. $str = str_replace('=', '-', $str);
  199. $str = str_replace('^', '-', $str);
  200. $str = str_replace(':', '-', $str);
  201. $str = str_replace('`', '-', $str);
  202. $str = str_replace('%', '-', $str);
  203. $str = str_replace('$', '-', $str);
  204. $str = str_replace('#', '-', $str);
  205. $str = str_replace('*', '-', $str);
  206. $str = str_replace('(', '-', $str);
  207. $str = str_replace(')', '-', $str);
  208. $str = str_replace('[', '-', $str);
  209. $str = str_replace(']', '-', $str);
  210. $str = str_replace('{', '-', $str);
  211. $str = str_replace('}', '-', $str);
  212. $str = str_replace('\\', '-', $str);
  213. $str = str_replace('|', '-', $str);
  214. $str = str_replace('@', '-', $str);
  215. return strtolower($str);
  216. }
  217. function resizeImage($filename, $width, $height, $crop = true, $use_urldecode = true, $noimage = '/upload/images/no-image.jpg') {
  218. $root_dir = dirname(dirname(__FILE__)) . ROOT_DIR;
  219. if ($use_urldecode) {
  220. $filename = urldecode($filename);
  221. }
  222. if (!file_exists($root_dir . $filename) || !is_file($root_dir . $filename)) {
  223. if ($noimage == '') {
  224. return null;
  225. } else {
  226. $filename = $noimage;
  227. }
  228. }
  229. $info = pathinfo($filename);
  230. $extension = $info['extension'];
  231. $in_name = $crop ? '' : 'a';
  232. $old_image = $filename;
  233. $new_image = str_replace('upload/images', 'upload/cache/images', substr($filename, 0, strrpos($filename, '.')) . '-' . $width . 'x' . $height . $in_name . '.' . $extension);
  234. if (!file_exists($root_dir . $new_image) || (filemtime($root_dir . $old_image) > filemtime($root_dir . $new_image))) {
  235. $path = '';
  236. $directories = explode('/', dirname(str_replace('../', '', $new_image)));
  237. foreach ($directories as $directory) {
  238. $path = $path . '/' . $directory;
  239. if (!file_exists($root_dir . $path)) {
  240. @mkdir($root_dir . $path, 0777);
  241. }
  242. }
  243. $image = new QImage($root_dir . $old_image);
  244. $image->resize($width, $height, $crop);
  245. $image->save($root_dir . $new_image);
  246. unset($image);
  247. }
  248. return $new_image;
  249. }