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.

106 lines
2.8 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 session($key) {
  25. if(array_key_exists($key, $_SESSION))
  26. return $_SESSION[$key];
  27. else
  28. return null;
  29. }
  30. function k($a, $k, $default=null) {
  31. if(is_array($k)) {
  32. $result = true;
  33. foreach($k as $key) {
  34. $result = $result && array_key_exists($key, $a);
  35. }
  36. return $result;
  37. } else {
  38. if(is_array($a) && array_key_exists($k, $a) && $a[$k])
  39. return $a[$k];
  40. elseif(is_object($a) && property_exists($a, $k) && $a->$k)
  41. return $a->$k;
  42. else
  43. return $default;
  44. }
  45. }
  46. function get_timezone($lat, $lng) {
  47. try {
  48. $ch = curl_init();
  49. curl_setopt($ch, CURLOPT_URL, 'http://timezone-api.geoloqi.com/timezone/'.$lat.'/'.$lng);
  50. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  51. $response = curl_exec($ch);
  52. $tz = @json_decode($response);
  53. if($tz)
  54. return new DateTimeZone($tz->timezone);
  55. } catch(Exception $e) {
  56. return null;
  57. }
  58. return null;
  59. }
  60. function micropub_post($endpoint, $params, $access_token) {
  61. $ch = curl_init();
  62. curl_setopt($ch, CURLOPT_URL, $endpoint);
  63. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  64. 'Authorization: Bearer ' . $access_token
  65. ));
  66. curl_setopt($ch, CURLOPT_POST, true);
  67. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array_merge(array(
  68. 'h' => 'entry'
  69. ), $params)));
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  71. curl_setopt($ch, CURLOPT_HEADER, true);
  72. $response = curl_exec($ch);
  73. $error = curl_error($ch);
  74. return array(
  75. 'response' => $response,
  76. 'error' => $error,
  77. 'curlinfo' => curl_getinfo($ch)
  78. );
  79. }
  80. function static_map($latitude, $longitude, $height=180, $width=700, $zoom=14) {
  81. 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;
  82. }
  83. function relative_time($date) {
  84. static $rel;
  85. if(!isset($rel)) {
  86. $config = array(
  87. 'language' => '\RelativeTime\Languages\English',
  88. 'separator' => ', ',
  89. 'suffix' => true,
  90. 'truncate' => 1,
  91. );
  92. $rel = new \RelativeTime\RelativeTime($config);
  93. }
  94. return $rel->timeAgo($date);
  95. }