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.

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