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.

208 lines
5.9 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. $targetURLs = preg_split('/, ?/', $r['data']['syndicate-to']);
  128. foreach($targetURLs as $t) {
  129. // If the syndication target doesn't have a scheme, add http
  130. if(!preg_match('/^http/', $t))
  131. $tmp = 'http://' . $t;
  132. // Parse the target expecting it to be a URL
  133. $url = parse_url($tmp);
  134. // If there's a host, and the host contains a . then we can assume there's a favicon
  135. // parse_url will parse strings like http://twitter into an array with a host of twitter, which is not resolvable
  136. if(array_key_exists('host', $url) && strpos($url['host'], '.') !== false) {
  137. $targets[] = array(
  138. 'target' => $t,
  139. 'favicon' => 'http://' . $url['host'] . '/favicon.ico'
  140. );
  141. } else {
  142. $targets[] = array(
  143. 'target' => $t,
  144. 'favicon' => false
  145. );
  146. }
  147. }
  148. }
  149. if(count($targets)) {
  150. $user->syndication_targets = json_encode($targets);
  151. $user->save();
  152. }
  153. return array(
  154. 'targets' => $targets,
  155. 'response' => $r
  156. );
  157. }
  158. function static_map($latitude, $longitude, $height=180, $width=700, $zoom=14) {
  159. 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;
  160. }
  161. function relative_time($date) {
  162. static $rel;
  163. if(!isset($rel)) {
  164. $config = array(
  165. 'language' => '\RelativeTime\Languages\English',
  166. 'separator' => ', ',
  167. 'suffix' => true,
  168. 'truncate' => 1,
  169. );
  170. $rel = new \RelativeTime\RelativeTime($config);
  171. }
  172. return $rel->timeAgo($date);
  173. }
  174. function instagram_client() {
  175. return new Andreyco\Instagram\Client(array(
  176. 'apiKey' => Config::$instagramClientID,
  177. 'apiSecret' => Config::$instagramClientSecret,
  178. 'apiCallback' => Config::$base_url . 'auth/instagram/callback',
  179. 'scope' => array('basic','likes'),
  180. ));
  181. }