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.6 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 js_bookmarklet($partial, $context) {
  25. return str_replace('+','%20',urlencode(str_replace(array("\n"),array(''),partial($partial, $context))));
  26. }
  27. function session($key) {
  28. if(array_key_exists($key, $_SESSION))
  29. return $_SESSION[$key];
  30. else
  31. return null;
  32. }
  33. function k($a, $k, $default=null) {
  34. if(is_array($k)) {
  35. $result = true;
  36. foreach($k as $key) {
  37. $result = $result && array_key_exists($key, $a);
  38. }
  39. return $result;
  40. } else {
  41. if(is_array($a) && array_key_exists($k, $a) && $a[$k])
  42. return $a[$k];
  43. elseif(is_object($a) && property_exists($a, $k) && $a->$k)
  44. return $a->$k;
  45. else
  46. return $default;
  47. }
  48. }
  49. function parse_geo_uri($uri) {
  50. if(preg_match('/geo:([\-\+]?[0-9\.]+),([\-\+]?[0-9\.]+)/', $uri, $match)) {
  51. return array(
  52. 'latitude' => (double)$match[1],
  53. 'longitude' => (double)$match[2],
  54. );
  55. } else {
  56. return false;
  57. }
  58. }
  59. function get_timezone($lat, $lng) {
  60. try {
  61. $ch = curl_init();
  62. curl_setopt($ch, CURLOPT_URL, 'http://timezone-api.geoloqi.com/timezone/'.$lat.'/'.$lng);
  63. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  64. $response = curl_exec($ch);
  65. $tz = @json_decode($response);
  66. if($tz)
  67. return new DateTimeZone($tz->timezone);
  68. } catch(Exception $e) {
  69. return null;
  70. }
  71. return null;
  72. }
  73. function micropub_post($endpoint, $params, $access_token) {
  74. $ch = curl_init();
  75. curl_setopt($ch, CURLOPT_URL, $endpoint);
  76. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  77. 'Authorization: Bearer ' . $access_token
  78. ));
  79. curl_setopt($ch, CURLOPT_POST, true);
  80. $post = http_build_query(array_merge(array(
  81. 'h' => 'entry'
  82. ), $params));
  83. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  84. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  85. curl_setopt($ch, CURLOPT_HEADER, true);
  86. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  87. $response = curl_exec($ch);
  88. $error = curl_error($ch);
  89. $sent_headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
  90. $request = $sent_headers . $post;
  91. return array(
  92. 'request' => $request,
  93. 'response' => $response,
  94. 'error' => $error,
  95. 'curlinfo' => curl_getinfo($ch)
  96. );
  97. }
  98. function micropub_get($endpoint, $params, $access_token) {
  99. $ch = curl_init();
  100. curl_setopt($ch, CURLOPT_URL, $endpoint . '?' . http_build_query($params));
  101. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  102. 'Authorization: Bearer ' . $access_token
  103. ));
  104. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  105. $response = curl_exec($ch);
  106. $data = array();
  107. if($response) {
  108. parse_str($response, $data);
  109. }
  110. $error = curl_error($ch);
  111. return array(
  112. 'response' => $response,
  113. 'data' => $data,
  114. 'error' => $error,
  115. 'curlinfo' => curl_getinfo($ch)
  116. );
  117. }
  118. function get_syndication_targets(&$user) {
  119. $targets = array();
  120. $r = micropub_get($user->micropub_endpoint, array('q'=>'syndicate-to'), $user->micropub_access_token);
  121. if($r['data'] && array_key_exists('syndicate-to', $r['data'])) {
  122. $targetURLs = preg_split('/, ?/', $r['data']['syndicate-to']);
  123. foreach($targetURLs as $t) {
  124. // If the syndication target doesn't have a scheme, add http
  125. if(!preg_match('/^http/', $t))
  126. $tmp = 'http://' . $t;
  127. // Parse the target expecting it to be a URL
  128. $url = parse_url($tmp);
  129. // If there's a host, and the host contains a . then we can assume there's a favicon
  130. // parse_url will parse strings like http://twitter into an array with a host of twitter, which is not resolvable
  131. if(array_key_exists('host', $url) && strpos($url['host'], '.') !== false) {
  132. $targets[] = array(
  133. 'target' => $t,
  134. 'favicon' => 'http://' . $url['host'] . '/favicon.ico'
  135. );
  136. } else {
  137. $targets[] = array(
  138. 'target' => $t,
  139. 'favicon' => false
  140. );
  141. }
  142. }
  143. }
  144. if(count($targets)) {
  145. $user->syndication_targets = json_encode($targets);
  146. $user->save();
  147. }
  148. return array(
  149. 'targets' => $targets,
  150. 'response' => $r
  151. );
  152. }
  153. function static_map($latitude, $longitude, $height=180, $width=700, $zoom=14) {
  154. 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;
  155. }
  156. function relative_time($date) {
  157. static $rel;
  158. if(!isset($rel)) {
  159. $config = array(
  160. 'language' => '\RelativeTime\Languages\English',
  161. 'separator' => ', ',
  162. 'suffix' => true,
  163. 'truncate' => 1,
  164. );
  165. $rel = new \RelativeTime\RelativeTime($config);
  166. }
  167. return $rel->timeAgo($date);
  168. }
  169. function caffeine_options() {
  170. return array(
  171. 'Coffee',
  172. 'Americano',
  173. 'Latte',
  174. 'Cappuccino',
  175. 'Espresso',
  176. 'Iced Coffee',
  177. 'Iced Americano',
  178. 'Iced Latte',
  179. 'Black Tea',
  180. 'Tea'
  181. );
  182. }
  183. function alcohol_options() {
  184. return array(
  185. 'Beer',
  186. 'Cocktail',
  187. 'Mimosa',
  188. 'Champagne',
  189. 'Wine',
  190. 'Sake',
  191. 'Cider'
  192. );
  193. }