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.

173 lines
4.8 KiB

  1. <?php
  2. ORM::configure('mysql:host=' . Config::$dbHost . ';dbname=' . Config::$dbName);
  3. ORM::configure('username', Config::$dbUsername);
  4. ORM::configure('password', Config::$dbPassword);
  5. function render($page, $data) {
  6. global $app;
  7. return $app->render('layout.php', array_merge($data, array('page' => $page)));
  8. };
  9. function partial($template, $data=array(), $debug=false) {
  10. global $app;
  11. if($debug) {
  12. $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions);
  13. echo '<pre>' . $tpl->fetch($template . '.php') . '</pre>';
  14. return '';
  15. }
  16. ob_start();
  17. $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions);
  18. foreach($data as $k=>$v) {
  19. $tpl->{$k} = $v;
  20. }
  21. $tpl->display($template . '.php');
  22. return ob_get_clean();
  23. }
  24. function session($key) {
  25. if(array_key_exists($key, $_SESSION))
  26. return $_SESSION[$key];
  27. else
  28. return null;
  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) && $a[$k])
  39. return $a[$k];
  40. elseif(is_object($a) && property_exists($a, $k) && $a->$k)
  41. return $a->$k;
  42. else
  43. return $default;
  44. }
  45. }
  46. function get_timezone($lat, $lng) {
  47. try {
  48. $ch = curl_init();
  49. curl_setopt($ch, CURLOPT_URL, 'http://timezone-api.geoloqi.com/timezone/'.$lat.'/'.$lng);
  50. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  51. $response = curl_exec($ch);
  52. $tz = @json_decode($response);
  53. if($tz)
  54. return new DateTimeZone($tz->timezone);
  55. } catch(Exception $e) {
  56. return null;
  57. }
  58. return null;
  59. }
  60. function micropub_post($endpoint, $params, $access_token) {
  61. $ch = curl_init();
  62. curl_setopt($ch, CURLOPT_URL, $endpoint);
  63. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  64. 'Authorization: Bearer ' . $access_token
  65. ));
  66. curl_setopt($ch, CURLOPT_POST, true);
  67. $post = http_build_query(array_merge(array(
  68. 'h' => 'entry'
  69. ), $params));
  70. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  71. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  72. curl_setopt($ch, CURLOPT_HEADER, true);
  73. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  74. $response = curl_exec($ch);
  75. $error = curl_error($ch);
  76. $sent_headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
  77. $request = $sent_headers . $post;
  78. return array(
  79. 'request' => $request,
  80. 'response' => $response,
  81. 'error' => $error,
  82. 'curlinfo' => curl_getinfo($ch)
  83. );
  84. }
  85. function micropub_get($endpoint, $params, $access_token) {
  86. $ch = curl_init();
  87. curl_setopt($ch, CURLOPT_URL, $endpoint . '?' . http_build_query($params));
  88. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  89. 'Authorization: Bearer ' . $access_token
  90. ));
  91. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  92. $response = curl_exec($ch);
  93. $data = array();
  94. if($response) {
  95. parse_str($response, $data);
  96. }
  97. $error = curl_error($ch);
  98. return array(
  99. 'response' => $response,
  100. 'data' => $data,
  101. 'error' => $error,
  102. 'curlinfo' => curl_getinfo($ch)
  103. );
  104. }
  105. function get_syndication_targets(&$user) {
  106. $targets = array();
  107. $r = micropub_get($user->micropub_endpoint, array('q'=>'syndicate-to'), $user->micropub_access_token);
  108. if($r['data'] && array_key_exists('syndicate-to', $r['data'])) {
  109. $targetURLs = preg_split('/, ?/', $r['data']['syndicate-to']);
  110. foreach($targetURLs as $t) {
  111. // If the syndication target doesn't have a scheme, add http
  112. if(!preg_match('/^http/', $t))
  113. $tmp = 'http://' . $t;
  114. // Parse the target expecting it to be a URL
  115. $url = parse_url($tmp);
  116. // If there's a host, and the host contains a . then we can assume there's a favicon
  117. // parse_url will parse strings like http://twitter into an array with a host of twitter, which is not resolvable
  118. if(array_key_exists('host', $url) && strpos($url['host'], '.') !== false) {
  119. $targets[] = array(
  120. 'target' => $t,
  121. 'favicon' => 'http://' . $url['host'] . '/favicon.ico'
  122. );
  123. } else {
  124. $targets[] = array(
  125. 'target' => $t,
  126. 'favicon' => false
  127. );
  128. }
  129. }
  130. }
  131. if(count($targets)) {
  132. $user->syndication_targets = json_encode($targets);
  133. $user->save();
  134. }
  135. return array(
  136. 'targets' => $targets,
  137. 'response' => $r
  138. );
  139. }
  140. function static_map($latitude, $longitude, $height=180, $width=700, $zoom=14) {
  141. return 'http://static-maps.pdx.esri.com/img.php?marker[]=lat:' . $latitude . ';lng:' . $longitude . ';icon:small-blue-cutout&basemap=gray&width=' . $width . '&height=' . $height . '&zoom=' . $zoom;
  142. }
  143. function relative_time($date) {
  144. static $rel;
  145. if(!isset($rel)) {
  146. $config = array(
  147. 'language' => '\RelativeTime\Languages\English',
  148. 'separator' => ', ',
  149. 'suffix' => true,
  150. 'truncate' => 1,
  151. );
  152. $rel = new \RelativeTime\RelativeTime($config);
  153. }
  154. return $rel->timeAgo($date);
  155. }