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.

200 lines
5.5 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 http_build_query($params) {
  89. // PHP's built-in http_build_query function encodes arrays with numeric indexes,
  90. // like foo[0]=bar&foo[0]=baz
  91. // This function removes the numeric indexes so that it's conformant with Micropub
  92. return preg_replace('/%5B[0-9]+%5D/', '%5B%5D', \http_build_query($params));
  93. }
  94. function html_to_dom_document($html) {
  95. // Parse the source body as HTML
  96. $doc = new DOMDocument();
  97. libxml_use_internal_errors(true); # suppress parse errors and warnings
  98. $body = mb_convert_encoding($html, 'HTML-ENTITIES', mb_detect_encoding($html));
  99. @$doc->loadHTML($body, LIBXML_NOWARNING|LIBXML_NOERROR);
  100. libxml_clear_errors();
  101. return $doc;
  102. }
  103. function xml_to_dom_document($xml) {
  104. // Parse the source body as XML
  105. $doc = new DOMDocument();
  106. libxml_use_internal_errors(true); # suppress parse errors and warnings
  107. // $body = mb_convert_encoding($xml, 'HTML-ENTITIES', mb_detect_encoding($xml));
  108. $body = $xml;
  109. $doc->loadXML($body);
  110. libxml_clear_errors();
  111. return $doc;
  112. }
  113. // Reads the exif rotation data and actually rotates the photo.
  114. // Only does anything if the exif library is loaded, otherwise is a noop.
  115. function correct_photo_rotation($filename) {
  116. if(class_exists('IMagick')) {
  117. try {
  118. $image = new IMagick($filename);
  119. $orientation = $image->getImageOrientation();
  120. switch($orientation) {
  121. case IMagick::ORIENTATION_BOTTOMRIGHT:
  122. $image->rotateImage(new ImagickPixel('#00000000'), 180);
  123. break;
  124. case IMagick::ORIENTATION_RIGHTTOP:
  125. $image->rotateImage(new ImagickPixel('#00000000'), 90);
  126. break;
  127. case IMagick::ORIENTATION_LEFTBOTTOM:
  128. $image->rotateImage(new ImagickPixel('#00000000'), -90);
  129. break;
  130. }
  131. $image->setImageOrientation(IMagick::ORIENTATION_TOPLEFT);
  132. $image->writeImage($filename);
  133. } catch(Exception $e){}
  134. }
  135. }
  136. /**
  137. * Converts base 10 to base 60.
  138. * http://tantek.pbworks.com/NewBase60
  139. * @param int $n
  140. * @return string
  141. */
  142. function b10to60($n)
  143. {
  144. $s = "";
  145. $m = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz";
  146. if ($n==0)
  147. return 0;
  148. while ($n>0)
  149. {
  150. $d = $n % 60;
  151. $s = $m[$d] . $s;
  152. $n = ($n-$d)/60;
  153. }
  154. return $s;
  155. }
  156. /**
  157. * Converts base 60 to base 10, with error checking
  158. * http://tantek.pbworks.com/NewBase60
  159. * @param string $s
  160. * @return int
  161. */
  162. function b60to10($s)
  163. {
  164. $n = 0;
  165. for($i = 0; $i < strlen($s); $i++) // iterate from first to last char of $s
  166. {
  167. $c = ord($s[$i]); // put current ASCII of char into $c
  168. if ($c>=48 && $c<=57) { $c=$c-48; }
  169. else if ($c>=65 && $c<=72) { $c-=55; }
  170. else if ($c==73 || $c==108) { $c=1; } // typo capital I, lowercase l to 1
  171. else if ($c>=74 && $c<=78) { $c-=56; }
  172. else if ($c==79) { $c=0; } // error correct typo capital O to 0
  173. else if ($c>=80 && $c<=90) { $c-=57; }
  174. else if ($c==95) { $c=34; } // underscore
  175. else if ($c>=97 && $c<=107) { $c-=62; }
  176. else if ($c>=109 && $c<=122) { $c-=63; }
  177. else { $c = 0; } // treat all other noise as 0
  178. $n = (60 * $n) + $c;
  179. }
  180. return $n;
  181. }