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.

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