tools.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /* ------------------------------------------------------------------------------------
  3. * Goosle - A meta search engine for private and fast internet fun.
  4. *
  5. * COPYRIGHT NOTICE
  6. * Copyright 2023-2024 Arnan de Gans. All Rights Reserved.
  7. *
  8. * COPYRIGHT NOTICES AND ALL THE COMMENTS SHOULD REMAIN INTACT.
  9. * By using this code you agree to indemnify Arnan de Gans from any
  10. * liability that might arise from its use.
  11. ------------------------------------------------------------------------------------ */
  12. /*--------------------------------------
  13. // Verify the hash, or not, and let people in, or not
  14. --------------------------------------*/
  15. function verify_hash($opts, $auth) {
  16. if(($opts->hash_auth == "on" && strtolower($opts->hash) === strtolower($auth)) || $opts->hash_auth == "off") return true;
  17. return false;
  18. }
  19. /*--------------------------------------
  20. // Load pages into a DOM
  21. --------------------------------------*/
  22. function get_xpath($response) {
  23. if(!$response)
  24. return null;
  25. $htmlDom = new DOMDocument;
  26. @$htmlDom->loadHTML($response);
  27. $xpath = new DOMXPath($htmlDom);
  28. return $xpath;
  29. }
  30. /*--------------------------------------
  31. // Strip all extras from an url
  32. --------------------------------------*/
  33. function get_base_url($url) {
  34. $url = parse_url($url);
  35. return $url['scheme'] . "://" . $url['host'] . "/";
  36. }
  37. /*--------------------------------------
  38. // Format search result urls
  39. --------------------------------------*/
  40. function get_formatted_url($url) {
  41. $url = parse_url($url);
  42. return $url['scheme'] . "://" . $url['host'] . str_replace('/', ' &rsaquo; ', str_replace('%20', ' ', rtrim($url['path'], '/')));
  43. }
  44. /*--------------------------------------
  45. // APCu Caching
  46. --------------------------------------*/
  47. function has_cached_results($url, $hash) {
  48. if(function_exists("apcu_exists")) {
  49. return apcu_exists("$hash:$url");
  50. }
  51. return false;
  52. }
  53. function store_cached_results($url, $hash, $results, $ttl = 0) {
  54. if(function_exists("apcu_store") && !empty($results)) {
  55. return apcu_store("$hash:$url", $results, $ttl);
  56. }
  57. }
  58. function fetch_cached_results($url, $hash) {
  59. if(function_exists("apcu_fetch")) {
  60. return apcu_fetch("$hash:$url");
  61. }
  62. return array();
  63. }
  64. /*--------------------------------------
  65. // Sanitize variables
  66. --------------------------------------*/
  67. function sanitize($thing) {
  68. switch(gettype($thing)) {
  69. case 'string':
  70. $thing = stripslashes(strip_tags(trim($thing)));
  71. break;
  72. case 'boolean':
  73. $thing = ($thing === FALSE) ? 0 : 1;
  74. break;
  75. default:
  76. $thing = ($thing === NULL) ? 'NULL' : strip_tags(trim($thing));
  77. break;
  78. }
  79. return $thing;
  80. }
  81. /*--------------------------------------
  82. // Human readable file sizes
  83. --------------------------------------*/
  84. function human_filesize($bytes, $dec = 2) {
  85. $size = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
  86. $factor = floor((strlen($bytes) - 1) / 3);
  87. return sprintf("%.{$dec}f ", $bytes / pow(1024, $factor)) . @$size[$factor];
  88. }
  89. /*--------------------------------------
  90. // Generate random strings for passwords
  91. --------------------------------------*/
  92. function string_generator() {
  93. $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  94. $password = array();
  95. $length = strlen($characters) - 1;
  96. for ($i = 0; $i < 24; $i++) {
  97. $n = rand(0, $length);
  98. $password[] = $characters[$n];
  99. }
  100. array_splice($password, 6, 0, '-');
  101. array_splice($password, 13, 0, '-');
  102. array_splice($password, 20, 0, '-');
  103. return implode($password);
  104. }
  105. ?>