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.

216 lines
5.9 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. return $app->render('layout.php', array_merge($data, array('page' => $page)));
  81. };
  82. function partial($template, $data=array(), $debug=false) {
  83. global $app;
  84. if($debug) {
  85. $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions);
  86. echo '<pre>' . $tpl->fetch($template . '.php') . '</pre>';
  87. return '';
  88. }
  89. ob_start();
  90. $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions);
  91. foreach($data as $k=>$v) {
  92. $tpl->{$k} = $v;
  93. }
  94. $tpl->display($template . '.php');
  95. return ob_get_clean();
  96. }
  97. function json_response(&$app, $response) {
  98. $app->response()['Content-Type'] = 'application/json';
  99. $app->response()->body(json_encode($response));
  100. }
  101. function session($key) {
  102. if(array_key_exists($key, $_SESSION))
  103. return $_SESSION[$key];
  104. else
  105. return null;
  106. }
  107. function k($a, $k, $default=null) {
  108. if(is_array($k)) {
  109. $result = true;
  110. foreach($k as $key) {
  111. $result = $result && array_key_exists($key, $a);
  112. }
  113. return $result;
  114. } else {
  115. if(is_array($a) && array_key_exists($k, $a) && $a[$k])
  116. return $a[$k];
  117. elseif(is_object($a) && property_exists($a, $k) && $a->$k)
  118. return $a->$k;
  119. else
  120. return $default;
  121. }
  122. }
  123. function get_timezone($lat, $lng) {
  124. try {
  125. $ch = curl_init();
  126. curl_setopt($ch, CURLOPT_URL, 'http://timezone-api.geoloqi.com/timezone/'.$lat.'/'.$lng);
  127. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  128. $response = curl_exec($ch);
  129. $tz = @json_decode($response);
  130. if($tz)
  131. return new DateTimeZone($tz->timezone);
  132. } catch(Exception $e) {
  133. return null;
  134. }
  135. return null;
  136. }
  137. function micropub_post($endpoint, $params, $access_token, $h='entry') {
  138. $ch = curl_init();
  139. curl_setopt($ch, CURLOPT_URL, $endpoint);
  140. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  141. 'Authorization: Bearer ' . $access_token
  142. ));
  143. curl_setopt($ch, CURLOPT_POST, true);
  144. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array_merge(array(
  145. 'h' => $h
  146. ), $params)));
  147. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  148. curl_setopt($ch, CURLOPT_HEADER, true);
  149. $response = curl_exec($ch);
  150. $error = curl_error($ch);
  151. return array(
  152. 'response' => $response,
  153. 'error' => $error,
  154. 'curlinfo' => curl_getinfo($ch),
  155. 'status' => curl_getinfo($ch, CURLINFO_HTTP_CODE)
  156. );
  157. }
  158. function relative_time($date) {
  159. static $rel;
  160. if(!isset($rel)) {
  161. $config = array(
  162. 'language' => '\RelativeTime\Languages\English',
  163. 'separator' => ', ',
  164. 'suffix' => true,
  165. 'truncate' => 1,
  166. );
  167. $rel = new \RelativeTime\RelativeTime($config);
  168. }
  169. return $rel->timeAgo($date);
  170. }
  171. function bs()
  172. {
  173. static $pheanstalk;
  174. if(!isset($pheanstalk)) {
  175. $pheanstalk = new Pheanstalk\Pheanstalk(Config::$beanstalkServer, Config::$beanstalkPort);
  176. }
  177. return $pheanstalk;
  178. }
  179. function pluralize($word, $num) {
  180. if($num == 1) {
  181. return $word;
  182. } else {
  183. return $word . 's';
  184. }
  185. }