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.

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