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.

222 lines
6.3 KiB

  1. <?php
  2. if(Config::$dbType == 'sqlite') {
  3. ORM::configure('sqlite:' . Config::$dbFilePath);
  4. } else {
  5. ORM::configure('mysql:host=' . Config::$dbHost . ';dbname=' . Config::$dbName);
  6. ORM::configure('username', Config::$dbUsername);
  7. ORM::configure('password', Config::$dbPassword);
  8. }
  9. function render($page, $data) {
  10. global $app;
  11. return $app->render('layout.php', array_merge($data, array('page' => $page)));
  12. };
  13. function partial($template, $data=array(), $debug=false) {
  14. global $app;
  15. if($debug) {
  16. $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions);
  17. echo '<pre>' . $tpl->fetch($template . '.php') . '</pre>';
  18. return '';
  19. }
  20. ob_start();
  21. $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions);
  22. foreach($data as $k=>$v) {
  23. $tpl->{$k} = $v;
  24. }
  25. $tpl->display($template . '.php');
  26. return ob_get_clean();
  27. }
  28. function js_bookmarklet($partial, $context) {
  29. return str_replace('+','%20',urlencode(str_replace(array("\n"),array(''),partial($partial, $context))));
  30. }
  31. function session($key) {
  32. if(array_key_exists($key, $_SESSION))
  33. return $_SESSION[$key];
  34. else
  35. return null;
  36. }
  37. function k($a, $k, $default=null) {
  38. if(is_array($k)) {
  39. $result = true;
  40. foreach($k as $key) {
  41. $result = $result && array_key_exists($key, $a);
  42. }
  43. return $result;
  44. } else {
  45. if(is_array($a) && array_key_exists($k, $a) && $a[$k])
  46. return $a[$k];
  47. elseif(is_object($a) && property_exists($a, $k) && $a->$k)
  48. return $a->$k;
  49. else
  50. return $default;
  51. }
  52. }
  53. function get_timezone($lat, $lng) {
  54. try {
  55. $ch = curl_init();
  56. curl_setopt($ch, CURLOPT_URL, 'http://timezone-api.geoloqi.com/timezone/'.$lat.'/'.$lng);
  57. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  58. $response = curl_exec($ch);
  59. $tz = @json_decode($response);
  60. if($tz)
  61. return new DateTimeZone($tz->timezone);
  62. } catch(Exception $e) {
  63. return null;
  64. }
  65. return null;
  66. }
  67. function micropub_post_for_user(&$user, $params) {
  68. // Now send to the micropub endpoint
  69. $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token);
  70. $user->last_micropub_response = substr(json_encode($r), 0, 1024);
  71. $user->last_micropub_response_date = date('Y-m-d H:i:s');
  72. // Check the response and look for a "Location" header containing the URL
  73. if($r['response'] && preg_match('/Location: (.+)/', $r['response'], $match)) {
  74. $r['location'] = trim($match[1]);
  75. $user->micropub_success = 1;
  76. } else {
  77. $r['location'] = false;
  78. }
  79. $user->save();
  80. return $r;
  81. }
  82. function micropub_post($endpoint, $params, $access_token) {
  83. $ch = curl_init();
  84. curl_setopt($ch, CURLOPT_URL, $endpoint);
  85. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  86. 'Authorization: Bearer ' . $access_token
  87. ));
  88. curl_setopt($ch, CURLOPT_POST, true);
  89. $post = http_build_query(array_merge(array(
  90. 'h' => 'entry'
  91. ), $params));
  92. $post = preg_replace('/%5B[0-9]+%5D/', '%5B%5D', $post); // change [0] to []
  93. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  94. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  95. curl_setopt($ch, CURLOPT_HEADER, true);
  96. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  97. $response = curl_exec($ch);
  98. $error = curl_error($ch);
  99. $sent_headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
  100. $request = $sent_headers . $post;
  101. return array(
  102. 'request' => $request,
  103. 'response' => $response,
  104. 'error' => $error,
  105. 'curlinfo' => curl_getinfo($ch)
  106. );
  107. }
  108. function micropub_get($endpoint, $params, $access_token) {
  109. $ch = curl_init();
  110. curl_setopt($ch, CURLOPT_URL, $endpoint . '?' . http_build_query($params));
  111. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  112. 'Authorization: Bearer ' . $access_token
  113. ));
  114. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  115. $response = curl_exec($ch);
  116. $data = array();
  117. if($response) {
  118. parse_str($response, $data);
  119. }
  120. $error = curl_error($ch);
  121. return array(
  122. 'response' => $response,
  123. 'data' => $data,
  124. 'error' => $error,
  125. 'curlinfo' => curl_getinfo($ch)
  126. );
  127. }
  128. function get_syndication_targets(&$user) {
  129. $targets = array();
  130. $r = micropub_get($user->micropub_endpoint, array('q'=>'syndicate-to'), $user->micropub_access_token);
  131. if($r['data'] && array_key_exists('syndicate-to', $r['data'])) {
  132. if(is_array($r['data']['syndicate-to'])) {
  133. $targetURLs = $r['data']['syndicate-to'];
  134. } elseif(is_string($r['data']['syndicate-to'])) {
  135. // support comma separated as a fallback
  136. $targetURLs = preg_split('/, ?/', $r['data']['syndicate-to']);
  137. } else {
  138. $targetURLs = array();
  139. }
  140. foreach($targetURLs as $t) {
  141. // If the syndication target doesn't have a scheme, add http
  142. if(!preg_match('/^http/', $t))
  143. $t2 = 'http://' . $t;
  144. else
  145. $t2 = $t;
  146. // Parse the target expecting it to be a URL
  147. $url = parse_url($t2);
  148. // If there's a host, and the host contains a . then we can assume there's a favicon
  149. // parse_url will parse strings like http://twitter into an array with a host of twitter, which is not resolvable
  150. if(array_key_exists('host', $url) && strpos($url['host'], '.') !== false) {
  151. $targets[] = array(
  152. 'target' => $t,
  153. 'favicon' => 'http://' . $url['host'] . '/favicon.ico'
  154. );
  155. } else {
  156. $targets[] = array(
  157. 'target' => $t,
  158. 'favicon' => false
  159. );
  160. }
  161. }
  162. }
  163. if(count($targets)) {
  164. $user->syndication_targets = json_encode($targets);
  165. $user->save();
  166. }
  167. return array(
  168. 'targets' => $targets,
  169. 'response' => $r
  170. );
  171. }
  172. function static_map($latitude, $longitude, $height=180, $width=700, $zoom=14) {
  173. 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;
  174. }
  175. function relative_time($date) {
  176. static $rel;
  177. if(!isset($rel)) {
  178. $config = array(
  179. 'language' => '\RelativeTime\Languages\English',
  180. 'separator' => ', ',
  181. 'suffix' => true,
  182. 'truncate' => 1,
  183. );
  184. $rel = new \RelativeTime\RelativeTime($config);
  185. }
  186. return $rel->timeAgo($date);
  187. }
  188. function instagram_client() {
  189. return new Andreyco\Instagram\Client(array(
  190. 'apiKey' => Config::$instagramClientID,
  191. 'apiSecret' => Config::$instagramClientSecret,
  192. 'apiCallback' => Config::$base_url . 'auth/instagram/callback',
  193. 'scope' => array('basic','likes'),
  194. ));
  195. }