diff options
author | Aaron Parecki <aaron@parecki.com> | 2014-05-24 14:41:21 -0700 |
---|---|---|
committer | Aaron Parecki <aaron@parecki.com> | 2014-05-24 14:41:21 -0700 |
commit | 3f82ec2f757c62c25a31b461e0a0cddc14886117 (patch) | |
tree | 8eb85c7f356df87f2bf477a54c7ab521002492a1 /lib/helpers.php |
Working app! Copied signin logic from OwnYourGram. New "post" interface for writing a simple text post. Also supports browser geolocation.
Diffstat (limited to 'lib/helpers.php')
-rw-r--r-- | lib/helpers.php | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/lib/helpers.php b/lib/helpers.php new file mode 100644 index 0000000..7748c4e --- /dev/null +++ b/lib/helpers.php @@ -0,0 +1,106 @@ +<?php + +ORM::configure('mysql:host=' . Config::$dbHost . ';dbname=' . Config::$dbName); +ORM::configure('username', Config::$dbUsername); +ORM::configure('password', Config::$dbPassword); + +function render($page, $data) { + global $app; + return $app->render('layout.php', array_merge($data, array('page' => $page))); +}; + +function partial($template, $data=array(), $debug=false) { + global $app; + + if($debug) { + $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions); + echo '<pre>' . $tpl->fetch($template . '.php') . '</pre>'; + return ''; + } + + ob_start(); + $tpl = new Savant3(\Slim\Extras\Views\Savant::$savantOptions); + foreach($data as $k=>$v) { + $tpl->{$k} = $v; + } + $tpl->display($template . '.php'); + return ob_get_clean(); +} + +function session($key) { + if(array_key_exists($key, $_SESSION)) + return $_SESSION[$key]; + else + return null; +} + +function k($a, $k, $default=null) { + if(is_array($k)) { + $result = true; + foreach($k as $key) { + $result = $result && array_key_exists($key, $a); + } + return $result; + } else { + if(is_array($a) && array_key_exists($k, $a) && $a[$k]) + return $a[$k]; + elseif(is_object($a) && property_exists($a, $k) && $a->$k) + return $a->$k; + else + return $default; + } +} + +function get_timezone($lat, $lng) { + try { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, 'http://timezone-api.geoloqi.com/timezone/'.$lat.'/'.$lng); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + $response = curl_exec($ch); + $tz = @json_decode($response); + if($tz) + return new DateTimeZone($tz->timezone); + } catch(Exception $e) { + return null; + } + return null; +} + +function micropub_post($endpoint, $params, $access_token) { + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $endpoint); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'Authorization: Bearer ' . $access_token + )); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array_merge(array( + 'h' => 'entry' + ), $params))); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HEADER, true); + $response = curl_exec($ch); + $error = curl_error($ch); + return array( + 'response' => $response, + 'error' => $error, + 'curlinfo' => curl_getinfo($ch) + ); +} + +function static_map($latitude, $longitude, $height=180, $width=700, $zoom=14) { + 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; +} + +function relative_time($date) { + static $rel; + if(!isset($rel)) { + $config = array( + 'language' => '\RelativeTime\Languages\English', + 'separator' => ', ', + 'suffix' => true, + 'truncate' => 1, + ); + $rel = new \RelativeTime\RelativeTime($config); + } + return $rel->timeAgo($date); +} |