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

  1. <?php
  2. // Default timezone
  3. date_default_timezone_set('UTC');
  4. ORM::configure('mysql:host=' . Config::$dbHost . ';dbname=' . Config::$dbName);
  5. ORM::configure('username', Config::$dbUsername);
  6. ORM::configure('password', Config::$dbPassword);
  7. function friendly_url($url) {
  8. return preg_replace(['/https?:\/\//','/\/$/'],'',$url);
  9. }
  10. function friendly_date($date_string, $tz_offset) {
  11. $date = new DateTime($date_string);
  12. if($tz_offset > 0)
  13. $date->add(new DateInterval('PT'.$tz_offset.'S'));
  14. elseif($tz_offset < 0)
  15. $date->sub(new DateInterval('PT'.abs($tz_offset).'S'));
  16. $tz = ($tz_offset < 0 ? '-' : '+') . sprintf('%02d:%02d', abs($tz_offset/60/60), ($tz_offset/60)%60);
  17. return $date->format('F j, Y g:ia') . ' ' . $tz;
  18. }
  19. function build_url($parsed_url) {
  20. $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
  21. $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
  22. $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
  23. $user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
  24. $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
  25. $pass = ($user || $pass) ? "$pass@" : '';
  26. $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
  27. $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
  28. $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
  29. return "$scheme$user$pass$host$port$path$query$fragment";
  30. }
  31. // Input: Any URL or string like "aaronparecki.com"
  32. // Output: Normlized URL (default to http if no scheme, force "/" path)
  33. // or return false if not a valid URL
  34. function normalize_url($url) {
  35. $me = parse_url($url);
  36. if(array_key_exists('path', $me) && $me['path'] == '')
  37. return false;
  38. // parse_url returns just "path" for naked domains
  39. if(count($me) == 1 && array_key_exists('path', $me)) {
  40. $me['host'] = $me['path'];
  41. unset($me['path']);
  42. }
  43. if(!array_key_exists('scheme', $me))
  44. $me['scheme'] = 'http';
  45. if(!array_key_exists('path', $me))
  46. $me['path'] = '/';
  47. // Invalid scheme
  48. if(!in_array($me['scheme'], array('http','https')))
  49. return false;
  50. return build_url($me);
  51. }
  52. // Checks if a string is a valid URL that we recognize as a PuSH topic or callback.
  53. // Must be http or https
  54. // If there is no path, '/' is assumed
  55. function is_valid_push_url($url) {
  56. $url = parse_url($url);
  57. if(!array_key_exists('scheme', $url))
  58. return false;
  59. if(!in_array($url['scheme'], ['http','https']))
  60. return false;
  61. if(!array_key_exists('path', $url))
  62. $url['path'] = '/';
  63. return build_url($url);
  64. }
  65. function render($page, $data) {
  66. global $app;
  67. return $app->render('layout.php', array_merge($data, array('page' => $page)));
  68. };
  69. function partial($template, $data=array(), $debug=false) {
  70. global $app;
  71. if($debug) {
  72. $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions);
  73. echo '<pre>' . $tpl->fetch($template . '.php') . '</pre>';
  74. return '';
  75. }
  76. ob_start();
  77. $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions);
  78. foreach($data as $k=>$v) {
  79. $tpl->{$k} = $v;
  80. }
  81. $tpl->display($template . '.php');
  82. return ob_get_clean();
  83. }
  84. function json_response(&$app, $response) {
  85. $app->response()['Content-Type'] = 'application/json';
  86. $app->response()->body(json_encode($response));
  87. }
  88. function session($key) {
  89. if(array_key_exists($key, $_SESSION))
  90. return $_SESSION[$key];
  91. else
  92. return null;
  93. }
  94. function k($a, $k, $default=null) {
  95. if(is_array($k)) {
  96. $result = true;
  97. foreach($k as $key) {
  98. $result = $result && array_key_exists($key, $a);
  99. }
  100. return $result;
  101. } else {
  102. if(is_array($a) && array_key_exists($k, $a) && $a[$k])
  103. return $a[$k];
  104. elseif(is_object($a) && property_exists($a, $k) && $a->$k)
  105. return $a->$k;
  106. else
  107. return $default;
  108. }
  109. }
  110. function get_timezone($lat, $lng) {
  111. try {
  112. $ch = curl_init();
  113. curl_setopt($ch, CURLOPT_URL, 'http://timezone-api.geoloqi.com/timezone/'.$lat.'/'.$lng);
  114. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  115. $response = curl_exec($ch);
  116. $tz = @json_decode($response);
  117. if($tz)
  118. return new DateTimeZone($tz->timezone);
  119. } catch(Exception $e) {
  120. return null;
  121. }
  122. return null;
  123. }
  124. function micropub_post($endpoint, $params, $access_token, $h='entry') {
  125. $ch = curl_init();
  126. curl_setopt($ch, CURLOPT_URL, $endpoint);
  127. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  128. 'Authorization: Bearer ' . $access_token
  129. ));
  130. curl_setopt($ch, CURLOPT_POST, true);
  131. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array_merge(array(
  132. 'h' => $h
  133. ), $params)));
  134. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  135. curl_setopt($ch, CURLOPT_HEADER, true);
  136. $response = curl_exec($ch);
  137. $error = curl_error($ch);
  138. return array(
  139. 'response' => $response,
  140. 'error' => $error,
  141. 'curlinfo' => curl_getinfo($ch),
  142. 'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE)
  143. );
  144. }
  145. function relative_time($date) {
  146. static $rel;
  147. if(!isset($rel)) {
  148. $config = array(
  149. 'language' => '\RelativeTime\Languages\English',
  150. 'separator' => ', ',
  151. 'suffix' => true,
  152. 'truncate' => 1,
  153. );
  154. $rel = new \RelativeTime\RelativeTime($config);
  155. }
  156. return $rel->timeAgo($date);
  157. }
  158. function bs()
  159. {
  160. static $pheanstalk;
  161. if(!isset($pheanstalk)) {
  162. $pheanstalk = new Pheanstalk\Pheanstalk(Config::$beanstalkServer, Config::$beanstalkPort);
  163. }
  164. return $pheanstalk;
  165. }
  166. function pluralize($word, $num) {
  167. if($num == 1) {
  168. return $word;
  169. } else {
  170. return $word . 's';
  171. }
  172. }