You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.9 KiB

  1. <?php
  2. namespace p3k;
  3. function redis() {
  4. static $client = false;
  5. if(!$client)
  6. $client = new Predis\Client(Config::$redis);
  7. return $client;
  8. }
  9. function bs()
  10. {
  11. static $pheanstalk;
  12. if(!isset($pheanstalk))
  13. $pheanstalk = new Pheanstalk\Pheanstalk(Config::$beanstalkServer, Config::$beanstalkPort);
  14. return $pheanstalk;
  15. }
  16. function initdb() {
  17. ORM::configure('mysql:host=' . Config::$db['host'] . ';dbname=' . Config::$db['database']);
  18. ORM::configure('username', Config::$db['username']);
  19. ORM::configure('password', Config::$db['password']);
  20. }
  21. function e($text) {
  22. return htmlspecialchars($text);
  23. }
  24. function k($a, $k, $default=null) {
  25. if(is_array($k)) {
  26. $result = true;
  27. foreach($k as $key) {
  28. $result = $result && array_key_exists($key, $a);
  29. }
  30. return $result;
  31. } else {
  32. if(is_array($a) && array_key_exists($k, $a))
  33. return $a[$k];
  34. elseif(is_object($a) && property_exists($a, $k))
  35. return $a->$k;
  36. else
  37. return $default;
  38. }
  39. }
  40. function random_string($len) {
  41. $charset='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  42. $str = '';
  43. $c = strlen($charset)-1;
  44. for($i=0; $i<$len; $i++) {
  45. $str .= $charset[mt_rand(0, $c)];
  46. }
  47. return $str;
  48. }
  49. // Returns true if $needle is the end of the $haystack
  50. function str_ends_with($haystack, $needle) {
  51. if($needle == '' || $haystack == '') return false;
  52. return strpos(strrev($haystack), strrev($needle)) === 0;
  53. }
  54. // Sets up the session.
  55. // If create is true, the session will be created even if there is no cookie yet.
  56. // If create is false, the session will only be set up in PHP if they already have a session cookie.
  57. function session_setup($create=false, $lifetime=2592000) {
  58. if($create || isset($_COOKIE[session_name()])) {
  59. session_set_cookie_params($lifetime);
  60. session_start();
  61. }
  62. }
  63. function session($key) {
  64. if(array_key_exists($key, $_SESSION))
  65. return $_SESSION[$key];
  66. else
  67. return null;
  68. }
  69. function flash($key) {
  70. if(isset($_SESSION) && isset($_SESSION[$key])) {
  71. $value = $_SESSION[$key];
  72. unset($_SESSION[$key]);
  73. return $value;
  74. }
  75. }
  76. function http_header_case($str) {
  77. $str = str_replace('-', ' ', $str);
  78. $str = ucwords($str);
  79. $str = str_replace(' ', '-', $str);
  80. return $str;
  81. }
  82. function html_to_dom_document($html) {
  83. // Parse the source body as HTML
  84. $doc = new DOMDocument();
  85. libxml_use_internal_errors(true); # suppress parse errors and warnings
  86. $body = mb_convert_encoding($html, 'HTML-ENTITIES', mb_detect_encoding($html));
  87. @$doc->loadHTML($body, LIBXML_NOWARNING|LIBXML_NOERROR);
  88. libxml_clear_errors();
  89. return $doc;
  90. }
  91. function xml_to_dom_document($xml) {
  92. // Parse the source body as XML
  93. $doc = new DOMDocument();
  94. libxml_use_internal_errors(true); # suppress parse errors and warnings
  95. // $body = mb_convert_encoding($xml, 'HTML-ENTITIES', mb_detect_encoding($xml));
  96. $body = $xml;
  97. $doc->loadXML($body);
  98. libxml_clear_errors();
  99. return $doc;
  100. }
  101. // Reads the exif rotation data and actually rotates the photo.
  102. // Only does anything if the exif library is loaded, otherwise is a noop.
  103. function correct_photo_rotation($filename) {
  104. if(class_exists('IMagick')) {
  105. try {
  106. $image = new IMagick($filename);
  107. $orientation = $image->getImageOrientation();
  108. switch($orientation) {
  109. case IMagick::ORIENTATION_BOTTOMRIGHT:
  110. $image->rotateImage(new ImagickPixel('#00000000'), 180);
  111. break;
  112. case IMagick::ORIENTATION_RIGHTTOP:
  113. $image->rotateImage(new ImagickPixel('#00000000'), 90);
  114. break;
  115. case IMagick::ORIENTATION_LEFTBOTTOM:
  116. $image->rotateImage(new ImagickPixel('#00000000'), -90);
  117. break;
  118. }
  119. $image->setImageOrientation(IMagick::ORIENTATION_TOPLEFT);
  120. $image->writeImage($filename);
  121. } catch(Exception $e){}
  122. }
  123. }
  124. /**
  125. * Converts base 10 to base 60.
  126. * http://tantek.pbworks.com/NewBase60
  127. * @param int $n
  128. * @return string
  129. */
  130. function b10to60($n)
  131. {
  132. $s = "";
  133. $m = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz";
  134. if ($n==0)
  135. return 0;
  136. while ($n>0)
  137. {
  138. $d = $n % 60;
  139. $s = $m[$d] . $s;
  140. $n = ($n-$d)/60;
  141. }
  142. return $s;
  143. }
  144. /**
  145. * Converts base 60 to base 10, with error checking
  146. * http://tantek.pbworks.com/NewBase60
  147. * @param string $s
  148. * @return int
  149. */
  150. function b60to10($s)
  151. {
  152. $n = 0;
  153. for($i = 0; $i < strlen($s); $i++) // iterate from first to last char of $s
  154. {
  155. $c = ord($s[$i]); // put current ASCII of char into $c
  156. if ($c>=48 && $c<=57) { $c=$c-48; }
  157. else if ($c>=65 && $c<=72) { $c-=55; }
  158. else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1
  159. else if ($c>=74 && $c<=78) { $c-=56; }
  160. else if ($c==79) { $c=0; } // error correct typo capital O to 0
  161. else if ($c>=80 && $c<=90) { $c-=57; }
  162. else if ($c==95) { $c=34; } // underscore
  163. else if ($c>=97 && $c<=107) { $c-=62; }
  164. else if ($c>=109 && $c<=122) { $c-=63; }
  165. else { $c = 0; } // treat all other noise as 0
  166. $n = (60 * $n) + $c;
  167. }
  168. return $n;
  169. }