function tweets($username, $params = array()) { $defaults = array('limit' => 10, 'cache' => true, 'refresh' => 60 * 20); // add the username to the defaults array $defaults['username'] = $username; $options = array_merge($defaults, $params); // check the cache dir $cacheDir = c::get('root.cache') . '/tweets'; dir::make($cacheDir); // disable the cache if adding the cache dir failed if (!is_dir($cacheDir) || !is_writable($cacheDir)) { $options['cache'] = false; } // sanitize the limit if ($options['limit'] > 200) { $options['limit'] = 200; } // generate a unique cache ID $cacheID = 'tweets/tweets.' . md5($options['username']) . '.' . $options['limit'] . '.php'; if ($options['cache']) { $cache = cache::modified($cacheID) < time() - $options['refresh'] ? false : cache::get($cacheID); } else { $cache = false; } if (!empty($cache)) { return $cache; } $url = 'http://api.twitter.com/1/statuses/user_timeline.json?screen_name=' . $options['username'] . '&count=' . $options['limit']; $json = @file_get_contents($url); $data = str::parse($json); if (!$data) { return false; } $result = array(); foreach ($data as $tweet) { $user = $tweet['user']; $result[] = new tweet(array('url' => 'http://twitter.com/' . $options['username'] . '/status/' . $tweet['id_str'], 'text' => $tweet['text'], 'date' => strtotime($tweet['created_at']), 'source' => $tweet['source'], 'user' => new obj(array('name' => $user['name'], 'bio' => $user['description'], 'username' => $user['screen_name'], 'url' => 'http://twitter.com/' . $user['screen_name'], 'image' => 'http://twitter.com/api/users/profile_image/' . $user['screen_name'], 'following' => $user['friends_count'], 'followers' => $user['followers_count'])))); } $result = new obj($result); if ($options['cache']) { cache::set($cacheID, $result); } return $result; }
function load() { // initiate the site and make pages and page // globally available $pages = $this->pages; $page = $this->pages->active(); // check for ssl if (c::get('ssl')) { // if there's no https in the url if (!server::get('https')) { go(str_replace('http://', 'https://', $page->url())); } } // check for a misconfigured subfolder install if ($page->isErrorPage()) { // get the subfolder in which the site is running $subfolder = ltrim(dirname(server::get('script_name')), '/'); // if it is running in a subfolder and it does not match the config // send an error with some explanations how to fix that if (!empty($subfolder) && c::get('subfolder') != $subfolder) { // this main url $url = 'http://' . server::get('http_host') . '/' . $subfolder; require_once c::get('root.kirby') . '/modals/subfolder.php'; exit; } } // redirect file urls (file:image.jpg) if ($this->uri->param('file')) { // get the local file $file = $page->files()->find($this->uri->param('file')); if ($file) { go($file->url()); } } // redirect /home to / if ($this->uri->path() == c::get('home')) { go(url()); } // redirect tinyurls if ($this->uri->path(1) == c::get('tinyurl.folder') && c::get('tinyurl.enabled')) { $hash = $this->uri->path(2); if (!empty($hash)) { $resolved = $this->pages->findByHash($hash)->first(); // redirect to the original page if ($resolved) { go(url($resolved->uri)); } } } // set the global template vars tpl::set('site', $this); tpl::set('pages', $pages); tpl::set('page', $page); $cacheID = $this->uri->toCacheID() . '.php'; $cacheModified = time(); $cacheData = null; if ($this->htmlCacheEnabled) { // check if the cache is disabled for some reason $this->htmlCacheEnabled = $page->isErrorPage() || in_array($page->uri(), c::get('cache.ignore', array())) ? false : true; // check for the last modified date of the cache file $cacheModified = cache::modified($cacheID); // check if the files have been modified // since the last html cache file has been written if ($this->htmlCacheEnabled && $cacheModified >= $this->modified) { $cacheData = cache::get($cacheID, true); } } if (empty($cacheData)) { // load the main template $html = tpl::load($page->template(), false, true); if ($this->htmlCacheEnabled) { cache::set($cacheID, (string) $html, true); } } else { $html = $cacheData; } die($html); }
function load() { // initiate the site and make pages and page // globally available $pages = $this->pages; $page = $this->pages->active(); // check for ssl if (c::get('ssl')) { // if there's no https in the url if (!server::get('https')) { go(str_replace('http://', 'https://', $page->url())); } } // check for index.php in rewritten urls and rewrite them if (c::get('rewrite') && preg_match('!index.php\\/!i', $this->uri->original)) { go($page->url()); } // check for a misconfigured subfolder install if ($page->isErrorPage()) { // if you want to store subfolders in the homefolder for blog articles i.e. and you // want urls like http://yourdomain.com/article-title you can set // RedirectMatch 301 ^/home/(.*)$ /$1 in your htaccess file and those // next lines will take care of delivering the right pages. $uri = c::get('home') . '/' . $this->uri->path(); if ($redirected = $this->pages()->find($uri)) { if ($redirected->uri() == $uri) { $page = $redirected; $this->pages->active = $page; $this->uri = new uri($uri); } } // try to rewrite broken translated urls // this will only work for default uris if (c::get('lang.support')) { $path = $this->uri->path->toArray(); $obj = $pages; $found = false; foreach ($path as $p) { // first try to find the page by uid $next = $obj->{'_' . $p}; if (!$next) { // go through each translation for each child page // and try to find the url_key or uid there foreach ($obj as $child) { foreach (c::get('lang.available') as $lang) { $c = $child->content($lang); // redirect to the url if a translated url has been found if ($c && $c->url_key() == $p && !$child->isErrorPage()) { $next = $child; } } } if (!$next) { break; } } $found = $next; $obj = $next->children(); } if ($found && !$found->isErrorPage()) { go($found->url()); } } } // redirect file urls (file:image.jpg) if ($this->uri->param('file')) { // get the local file $file = $page->files()->find($this->uri->param('file')); if ($file) { go($file->url()); } } // redirect /home to / if ($this->uri->path() == c::get('home')) { go(url()); } // redirect tinyurls if ($this->uri->path(1) == c::get('tinyurl.folder') && c::get('tinyurl.enabled')) { $hash = $this->uri->path(2); if (!empty($hash)) { $resolved = $this->pages->findByHash($hash)->first(); // redirect to the original page if ($resolved) { go(url($resolved->uri)); } } } // set the global template vars tpl::set('site', $this); tpl::set('pages', $pages); tpl::set('page', $page); $cacheID = $this->htmlCacheID(); $cacheModified = time(); $cacheData = null; if ($this->htmlCacheEnabled) { // check if the cache is disabled for some reason $this->htmlCacheEnabled = $page->isErrorPage() || in_array($page->uri(), c::get('cache.ignore', array())) ? false : true; // check for the last modified date of the cache file $cacheModified = cache::modified($cacheID); // check if the files have been modified // since the last html cache file has been written if ($this->htmlCacheEnabled && $cacheModified >= $this->modified) { $cacheData = cache::get($cacheID, true); } } // send a 404 header if this is the error page if ($page->isErrorPage() && c::get('404.header')) { header("HTTP/1.0 404 Not Found"); } if (empty($cacheData)) { // load the main template $html = tpl::load($page->template(), array(), true); if ($this->htmlCacheEnabled) { cache::set($cacheID, (string) $html, true); } } else { $html = $cacheData; } die($html); }
/** * Constructor. Loads the data from the Instagram API. * @param string The access token. * @param integer The number of shots that will be loaded. * @param boolean Chache enabled. * @param integer How many seconds until the cache expires. * @param string The user-id of the user or 'self' for your own account. */ function __construct($_token = '', $_count = 10, $_cache = true, $_cache_expire = 3600, $_user = '******') { // Init $this->images = array(); $this->user = new stdClass(); // Check if a token is provided if (trim($_token) != '') { // Construct the API url… // http://instagr.am/developer/endpoints/users/ $url = "https://api.instagram.com/v1/users/{$_user}/media/recent/?access_token={$_token}&count={$_count}"; // Create cache directory if it doesn't exist yet if ($_cache) { dir::make(c::get('root.cache') . '/instagram'); } $images_cache_id = 'instagram/images.' . md5($_token) . '.' . $_count . '.php'; $images_cache_data = false; // Try to fetch data from cache if ($_cache) { $images_cache_data = cache::modified($images_cache_id) < time() - $_cache_expire ? false : cache::get($images_cache_id); } // Load data from the API if the cache expired or the cache is empty if (empty($images_cache_data)) { $data = $this->fetch_data($url); $photos = json_decode($data); // Set new data for the cache if ($_cache) { cache::set($images_cache_id, $photos); } } else { $photos = $images_cache_data; } // Process the images for ($i = 0; $i < $_count; $i++) { if (isset($photos->data[$i]) && count($photos->data) > 0) { // Get the user's data from the first image if ($i == 0) { $this->user->username = $photos->data[$i]->user->username; $this->user->full_name = $photos->data[$i]->user->full_name; $this->user->picture = $photos->data[$i]->user->profile_picture; } // create a new object for each image $obj = new stdClass(); $obj->link = $photos->data[$i]->link; $obj->comments = @$photos->data[$i]->comments->count; $obj->likes = @$photos->data[$i]->likes->count; $obj->created = $photos->data[$i]->created_time; $obj->thumb = @$photos->data[$i]->images->thumbnail->url; $obj->url = @$photos->data[$i]->images->standard_resolution->url; $obj->image_lowres = @$photos->data[$i]->images->low_resolution->url; $obj->filter = $photos->data[$i]->filter; $obj->location = @$photos->data[$i]->location->name; $obj->latitude = @$photos->data[$i]->location->latitude; $obj->longitude = @$photos->data[$i]->location->longitude; $obj->tags = array(); // attach the new object to the array $this->images[$i] = $obj; // Process tags for ($j = 0; $j < count($photos->data[$i]->tags); $j++) { $this->images[$i]->tags[$j] = $photos->data[$i]->tags[$j]; } } } } else { throw new Exception('$_token MUST be set!'); } }
/** * Constructor. Loads the data from Dribbble when the object is constructed. * @param string $_username The username of the player. * @param integer $_number_of_shots The number of shots that will be loaded. * @param boolean $_fetch_likes If the likes of the user should be fetched in a second call. * @param integer If <code>$_fetch_likes</code> is <code>true</code>, then how many likes should be fetched. * @param boolean $cache Enable/disable caching. Cache is enabled by default * @param integer $refresh Seconds before the cache will be refreshed. Default is in hour (3600 seconds) */ function __construct($_username = "******", $_number_of_shots = 3, $_fetch_likes = false, $_number_of_likes = 3, $cache = true, $refresh = 3600) { // Init $this->username = $_username; $this->shots = array(); $this->likes = array(); $this->player = null; // Build URLs $base_url = "http://api.dribbble.com/players/" . $this->username; $shots_url = $base_url . "/shots"; $likes_url = $base_url . "/likes"; // create the cache directory if not there yet if ($cache) { dir::make(c::get('root.cache') . '/dribbble'); } // Process the data if ($_number_of_shots > 0) { // define a cache id $shots_cache_id = 'dribbble/shots.' . md5($this->username) . '.' . $_number_of_shots . '.php'; $shots_cache_data = false; // try to fetch the data from cache if ($cache) { $shots_cache_data = cache::modified($shots_cache_id) < time() - $refresh ? false : cache::get($shots_cache_id); } // if there's no data in the cache, load shots from the Dribbble API if (empty($shots_cache_data)) { $all_shots = $this->fetch_data($shots_url); $all_shots = json_decode($all_shots); $all_shots = $all_shots->shots; if ($cache) { cache::set($shots_cache_id, $all_shots); } } else { $all_shots = $shots_cache_data; } // Only proceed if there is at least one shot. // If there's no shot, then player data can't be extracted from this API call // and must be extracted via /players/:id/ (maybe I'll implement that later) if (count($all_shots) > 0) { // Load shots data for ($i = 0; $i < $_number_of_shots; $i++) { if (!is_null($all_shots[$i])) { $this->shots[$i]->id = $all_shots[$i]->id; $this->shots[$i]->title = $all_shots[$i]->title; $this->shots[$i]->url = $all_shots[$i]->url; $this->shots[$i]->short_url = $all_shots[$i]->short_url; $this->shots[$i]->image = $all_shots[$i]->image_url; $this->shots[$i]->likes = $all_shots[$i]->likes_count; $this->shots[$i]->views = $all_shots[$i]->views_count; $this->shots[$i]->rebounds = $all_shots[$i]->rebounds_count; $this->shots[$i]->comments = $all_shots[$i]->comments_count; $this->shots[$i]->created = $all_shots[$i]->created_at; } } // Process player data $this->player->id = $all_shots[0]->player->id; $this->player->name = $all_shots[0]->player->name; $this->player->username = $all_shots[0]->player->username; $this->player->url = $all_shots[0]->player->url; $this->player->avatar_url = $all_shots[0]->player->avatar_url; $this->player->twitter = $all_shots[0]->player->twitter_screen_name; $this->player->location = $all_shots[0]->player->location; $this->player->followers = $all_shots[0]->player->followers_count; $this->player->following = $all_shots[0]->player->following_count; $this->player->likes = $all_shots[0]->player->likes_count; } } // Fetch all likes of the user (needs another API call). // If you only want to fetch the likes, not the shots, then set <code>$_number_of_shots</code> to <code>0</code>. if ($_fetch_likes && $_number_of_likes > 0) { // define a cache id $likes_cache_id = 'dribbble/likes.' . md5($this->username) . '.' . $_number_of_likes . '.php'; $likes_cache_data = false; // try to fetch the data from cache if ($cache) { $likes_cache_data = cache::modified($likes_cache_id) < time() - $refresh ? false : cache::get($likes_cache_id); } // if there's no data in the cache, load likes from the Dribbble API if (empty($likes_cache_data)) { $all_likes = $this->fetch_data($likes_url); $all_likes = json_decode($all_likes); $all_likes = $all_likes->shots; if ($cache) { cache::set($likes_cache_id, $all_likes); } } else { $all_likes = $likes_cache_data; } // Process likes for ($i = 0; $i < $_number_of_likes; $i++) { if (!is_null($all_likes[$i])) { $this->likes[$i]->id = $all_likes[$i]->id; $this->likes[$i]->title = $all_likes[$i]->title; $this->likes[$i]->url = $all_likes[$i]->url; $this->likes[$i]->short_url = $all_likes[$i]->short_url; $this->likes[$i]->image = $all_likes[$i]->image_url; $this->likes[$i]->likes = $all_likes[$i]->likes_count; $this->likes[$i]->views = $all_likes[$i]->views_count; $this->likes[$i]->rebounds = $all_likes[$i]->rebounds_count; $this->likes[$i]->comments = $all_likes[$i]->comments_count; $this->likes[$i]->created = $all_likes[$i]->created_at; // Process the user the like belongs to $this->likes[$i]->player->id = $all_likes[$i]->player->id; $this->likes[$i]->player->name = $all_likes[$i]->player->name; $this->likes[$i]->player->username = $all_likes[$i]->player->username; $this->likes[$i]->player->url = $all_likes[$i]->player->url; $this->likes[$i]->player->avatar_url = $all_likes[$i]->player->avatar_url; $this->likes[$i]->player->twitter = $all_likes[$i]->player->twitter_screen_name; $this->likes[$i]->player->location = $all_likes[$i]->player->location; $this->likes[$i]->player->followers = $all_likes[$i]->player->followers_count; $this->likes[$i]->player->following = $all_likes[$i]->player->following_count; $this->likes[$i]->player->likes = $all_likes[$i]->player->likes_count; } } } }
function flickrbadge($params = array()) { $defaults = array('key' => false, 'username' => false, 'limit' => 10, 'format' => 'square', 'cache' => true, 'refresh' => 60 * 60 * 2); $options = array_merge($defaults, $params); // check the cache dir $cacheDir = c::get('root.cache') . '/flickrbadge'; dir::make($cacheDir); // disable the cache if adding the cache dir failed if (!is_dir($cacheDir) || !is_writable($cacheDir)) { $options['cache'] = false; } if (!$options['key']) { return false; } if (!$options['username']) { return false; } $cacheID = 'flickrbadge/data.' . md5(serialize($options)) . '.php'; if ($options['cache']) { $cache = cache::modified($cacheID) < time() - $options['refresh'] ? false : cache::get($cacheID); } else { $cache = false; } if (!empty($cache)) { return $cache; } $flickr = new phpFlickr($options['key']); $userCacheID = 'flickrbadge/user.' . md5($options['username']) . '.php'; $userCache = $options['cache'] ? cache::get($userCacheID) : false; $user = false; $url = false; if (!empty($userCache)) { $user = a::get($userCache, 'user'); $url = a::get($userCache, 'url'); } if (!$user || !$url) { $user = $flickr->people_findByUsername($options['username']); $url = $flickr->urls_getUserPhotos($user['id']); if ($options['cache']) { cache::set($userCacheID, array('user' => $user, 'url' => $url)); } } $photos = $flickr->people_getPublicPhotos($user['id'], NULL, NULL, $options['limit']); $result = array(); foreach ($photos['photos']['photo'] as $photo) { $photoCacheID = 'flickrbadge/photo.' . $photo['id'] . '.php'; $info = $options['cache'] ? cache::get($photoCacheID) : false; if (empty($info)) { $info = $flickr->photos_getInfo($photo['id']); if ($options['cache']) { cache::set($photoCacheID, $info); } } $info = a::get($info, 'photo', array()); $dates = a::get($info, 'dates', array()); $tags = array(); foreach ((array) $info['tags']['tag'] as $t) { if (!empty($t['raw']) && !$t['machine_tag']) { $tags[] = $t['raw']; } } $result[] = new obj(array('url' => $url . $photo['id'], 'title' => a::get($info, 'title', $photo['title']), 'description' => @$info['description'], 'src' => $flickr->buildPhotoURL($photo, $options['format']), 'taken' => isset($dates['taken']) ? strtotime($dates['taken']) : false, 'posted' => isset($dates['posted']) ? $dates['posted'] : false, 'lastupdate' => isset($dates['lastupdate']) ? $dates['lastupdate'] : false, 'views' => a::get($info, 'views', 0), 'comments' => a::get($info, 'comments', 0), 'tags' => $tags)); } $result = new obj($result); if ($options['cache']) { cache::set($cacheID, $result); } return $result; }
static function expired($file, $time = false) { return cache::modified($file) < time() - $time ? true : false; }
function tweets($username, $params = array()) { $defaults = array('limit' => 10, 'cache' => true, 'hiderep' => false, 'refresh' => 60 * 20); // add the username to the defaults array $defaults['username'] = $username; $options = array_merge($defaults, $params); // check the cache dir $cacheDir = c::get('root.cache') . '/tweets'; dir::make($cacheDir); // disable the cache if adding the cache dir failed if (!is_dir($cacheDir) || !is_writable($cacheDir)) { $options['cache'] = false; } // sanitize the limit if ($options['limit'] > 200) { $options['limit'] = 200; } // generate a unique cache ID $cacheID = 'tweets/tweets.' . md5($options['username']) . '.' . $options['limit'] . '.php'; if ($options['cache']) { $cache = cache::modified($cacheID) < time() - $options['refresh'] ? false : cache::get($cacheID); } else { $cache = false; } if (!empty($cache)) { return $cache; } // Encode the key and secret from the Twitter config. $twitterKey = urlencode(c::get('twitter.key')); $twitterSecret = urlencode(c::get('twitter.secret')); // combine and base64 encode the key and secret with a colon seperator $twitterCode = base64_encode($twitterKey . ':' . $twitterSecret); // obtain a bearer token from the api, by building a request //url to use $url = 'https://api.twitter.com/oauth2/token'; //create header $header = array('http' => array('method' => "POST", 'header' => "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n" . "Authorization: Basic " . $twitterCode . "\r\n", 'content' => "grant_type=client_credentials")); //send the request $context = stream_context_create($header); $bearer = file_get_contents($url, false, $context); // decode the json response $bearer = json_decode($bearer); // send the rquest for tweets $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $options['username'] . '&count=' . $options['limit'] . '&include_rts=true' . '&exclude_replies=' . $options['hiderep']; $header = array('http' => array('method' => "GET", 'header' => "Authorization: Bearer " . $bearer->access_token . "\r\n")); $context = stream_context_create($header); $json = file_get_contents($url, false, $context); $data = json_decode($json); if (!$data) { return false; } $result = array(); foreach ($data as $tweet) { $user = $tweet->user; $result[] = new tweet(array('url' => 'http://twitter.com/' . $options['username'] . '/status/' . $tweet->id_str, 'text' => $tweet->text, 'date' => strtotime($tweet->created_at), 'source' => $tweet->source, 'user' => new obj(array('name' => $user->name, 'bio' => $user->description, 'username' => $user->screen_name, 'url' => 'http://twitter.com/' . $user->screen_name, 'image' => 'http://twitter.com/api/users/profile_image/' . $user->screen_name, 'following' => $user->friends_count, 'followers' => $user->followers_count)))); } $result = new obj($result); if ($options['cache']) { cache::set($cacheID, $result); } return $result; }