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.

218 lines
6.2 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 = substr(json_encode($r), 0, 1024);
  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. $post = preg_replace('/%5B[0-9]+%5D/', '%5B%5D', $post);
  89. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  90. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  91. curl_setopt($ch, CURLOPT_HEADER, true);
  92. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  93. $response = curl_exec($ch);
  94. $error = curl_error($ch);
  95. $sent_headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
  96. $request = $sent_headers . $post;
  97. return array(
  98. 'request' => $request,
  99. 'response' => $response,
  100. 'error' => $error,
  101. 'curlinfo' => curl_getinfo($ch)
  102. );
  103. }
  104. function micropub_get($endpoint, $params, $access_token) {
  105. $ch = curl_init();
  106. curl_setopt($ch, CURLOPT_URL, $endpoint . '?' . http_build_query($params));
  107. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  108. 'Authorization: Bearer ' . $access_token
  109. ));
  110. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  111. $response = curl_exec($ch);
  112. $data = array();
  113. if($response) {
  114. parse_str($response, $data);
  115. }
  116. $error = curl_error($ch);
  117. return array(
  118. 'response' => $response,
  119. 'data' => $data,
  120. 'error' => $error,
  121. 'curlinfo' => curl_getinfo($ch)
  122. );
  123. }
  124. function get_syndication_targets(&$user) {
  125. $targets = array();
  126. $r = micropub_get($user->micropub_endpoint, array('q'=>'syndicate-to'), $user->micropub_access_token);
  127. if($r['data'] && array_key_exists('syndicate-to', $r['data'])) {
  128. if(is_array($r['data']['syndicate-to'])) {
  129. $targetURLs = $r['data']['syndicate-to'];
  130. } elseif(is_string($r['data']['syndicate-to'])) {
  131. // support comma separated as a fallback
  132. $targetURLs = preg_split('/, ?/', $r['data']['syndicate-to']);
  133. } else {
  134. $targetURLs = array();
  135. }
  136. foreach($targetURLs as $t) {
  137. // If the syndication target doesn't have a scheme, add http
  138. if(!preg_match('/^http/', $t))
  139. $t2 = 'http://' . $t;
  140. else
  141. $t2 = $t;
  142. // Parse the target expecting it to be a URL
  143. $url = parse_url($t2);
  144. // If there's a host, and the host contains a . then we can assume there's a favicon
  145. // parse_url will parse strings like http://twitter into an array with a host of twitter, which is not resolvable
  146. if(array_key_exists('host', $url) && strpos($url['host'], '.') !== false) {
  147. $targets[] = array(
  148. 'target' => $t,
  149. 'favicon' => 'http://' . $url['host'] . '/favicon.ico'
  150. );
  151. } else {
  152. $targets[] = array(
  153. 'target' => $t,
  154. 'favicon' => false
  155. );
  156. }
  157. }
  158. }
  159. if(count($targets)) {
  160. $user->syndication_targets = json_encode($targets);
  161. $user->save();
  162. }
  163. return array(
  164. 'targets' => $targets,
  165. 'response' => $r
  166. );
  167. }
  168. function static_map($latitude, $longitude, $height=180, $width=700, $zoom=14) {
  169. 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;
  170. }
  171. function relative_time($date) {
  172. static $rel;
  173. if(!isset($rel)) {
  174. $config = array(
  175. 'language' => '\RelativeTime\Languages\English',
  176. 'separator' => ', ',
  177. 'suffix' => true,
  178. 'truncate' => 1,
  179. );
  180. $rel = new \RelativeTime\RelativeTime($config);
  181. }
  182. return $rel->timeAgo($date);
  183. }
  184. function instagram_client() {
  185. return new Andreyco\Instagram\Client(array(
  186. 'apiKey' => Config::$instagramClientID,
  187. 'apiSecret' => Config::$instagramClientSecret,
  188. 'apiCallback' => Config::$base_url . 'auth/instagram/callback',
  189. 'scope' => array('basic','likes'),
  190. ));
  191. }