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.

214 lines
6.1 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_for_user(&$user, $params) {
  64. // Now send to the micropub endpoint
  65. $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token);
  66. $user->last_micropub_response = json_encode($r);
  67. $user->last_micropub_response_date = date('Y-m-d H:i:s');
  68. // Check the response and look for a "Location" header containing the URL
  69. if($r['response'] && preg_match('/Location: (.+)/', $r['response'], $match)) {
  70. $r['location'] = trim($match[1]);
  71. $user->micropub_success = 1;
  72. } else {
  73. $r['location'] = false;
  74. }
  75. $user->save();
  76. return $r;
  77. }
  78. function micropub_post($endpoint, $params, $access_token) {
  79. $ch = curl_init();
  80. curl_setopt($ch, CURLOPT_URL, $endpoint);
  81. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  82. 'Authorization: Bearer ' . $access_token
  83. ));
  84. curl_setopt($ch, CURLOPT_POST, true);
  85. $post = http_build_query(array_merge(array(
  86. 'h' => 'entry'
  87. ), $params));
  88. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  89. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  90. curl_setopt($ch, CURLOPT_HEADER, true);
  91. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  92. $response = curl_exec($ch);
  93. $error = curl_error($ch);
  94. $sent_headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
  95. $request = $sent_headers . $post;
  96. return array(
  97. 'request' => $request,
  98. 'response' => $response,
  99. 'error' => $error,
  100. 'curlinfo' => curl_getinfo($ch)
  101. );
  102. }
  103. function micropub_get($endpoint, $params, $access_token) {
  104. $ch = curl_init();
  105. curl_setopt($ch, CURLOPT_URL, $endpoint . '?' . http_build_query($params));
  106. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  107. 'Authorization: Bearer ' . $access_token
  108. ));
  109. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  110. $response = curl_exec($ch);
  111. $data = array();
  112. if($response) {
  113. parse_str($response, $data);
  114. }
  115. $error = curl_error($ch);
  116. return array(
  117. 'response' => $response,
  118. 'data' => $data,
  119. 'error' => $error,
  120. 'curlinfo' => curl_getinfo($ch)
  121. );
  122. }
  123. function get_syndication_targets(&$user) {
  124. $targets = array();
  125. $r = micropub_get($user->micropub_endpoint, array('q'=>'syndicate-to'), $user->micropub_access_token);
  126. if($r['data'] && array_key_exists('syndicate-to', $r['data'])) {
  127. if(is_array($r['data']['syndicate-to'])) {
  128. $targetURLs = $r['data']['syndicate-to'];
  129. } elseif(is_string($r['data']['syndicate-to'])) {
  130. $targetURLs = preg_split('/, ?/', $r['data']['syndicate-to']);
  131. } else {
  132. $targetURLs = array();
  133. }
  134. foreach($targetURLs as $t) {
  135. // If the syndication target doesn't have a scheme, add http
  136. if(!preg_match('/^http/', $t))
  137. $t = 'http://' . $t;
  138. // Parse the target expecting it to be a URL
  139. $url = parse_url($t);
  140. // If there's a host, and the host contains a . then we can assume there's a favicon
  141. // parse_url will parse strings like http://twitter into an array with a host of twitter, which is not resolvable
  142. if(array_key_exists('host', $url) && strpos($url['host'], '.') !== false) {
  143. $targets[] = array(
  144. 'target' => $t,
  145. 'favicon' => 'http://' . $url['host'] . '/favicon.ico'
  146. );
  147. } else {
  148. $targets[] = array(
  149. 'target' => $t,
  150. 'favicon' => false
  151. );
  152. }
  153. }
  154. }
  155. if(count($targets)) {
  156. $user->syndication_targets = json_encode($targets);
  157. $user->save();
  158. }
  159. return array(
  160. 'targets' => $targets,
  161. 'response' => $r
  162. );
  163. }
  164. function static_map($latitude, $longitude, $height=180, $width=700, $zoom=14) {
  165. 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;
  166. }
  167. function relative_time($date) {
  168. static $rel;
  169. if(!isset($rel)) {
  170. $config = array(
  171. 'language' => '\RelativeTime\Languages\English',
  172. 'separator' => ', ',
  173. 'suffix' => true,
  174. 'truncate' => 1,
  175. );
  176. $rel = new \RelativeTime\RelativeTime($config);
  177. }
  178. return $rel->timeAgo($date);
  179. }
  180. function instagram_client() {
  181. return new Andreyco\Instagram\Client(array(
  182. 'apiKey' => Config::$instagramClientID,
  183. 'apiSecret' => Config::$instagramClientSecret,
  184. 'apiCallback' => Config::$base_url . 'auth/instagram/callback',
  185. 'scope' => array('basic','likes'),
  186. ));
  187. }