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.

193 lines
5.2 KiB

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