function __construct($uri = false) { // set the defaults $this->path = new uriPath(); $this->params = new uriParams(); $this->query = new uriQuery(str::parse(server::get('query_string'), 'query')); $this->extension = false; $this->raw = $this->raw($uri); $this->url = url(ltrim($this->raw, '/')); // crawl the uri and get all elements $this->crawl(); }
function parse($result, $format = 'plain') { switch ($format) { case 'xml': case 'json': case 'php': $result = str::parse($result, $format); return is_array($result) ? $result : false; break; default: return $result; break; } }
public static function latest_post() { $cache = kirby()->cache()->get('facebook.latest.post'); if (!is_null($cache)) { return response::json($cache); } $response = remote::get(url::build(array('scheme' => static::$graph_scheme, 'host' => static::$graph_host, 'fragments' => array(c::get('facebook.id'), 'posts'), 'query' => array('access_token' => c::get('facebook.accesstoken'), 'limit' => 1)))); if ($response->content) { // parse json string $data = str::parse($response->content)['data'][0]; $return = array('text' => $data['message'], 'picture' => $data['picture'], 'timestamp' => strtotime($data['updated_time']), 'time_formatted' => strftime('%d. %B %G', strtotime($data['updated_time'])), 'link' => $data['link']); // set cache for 10 minutes kirby()->cache()->set('facebook.latest.post', $return, 10); } else { return false; } return $return; }
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; }
/** * Reads the file content and parses it * * @param string $format * @return mixed */ public function read($format = null) { return str::parse($this->content(), $format); }
/** * Reads the content of a file * * @param string $file The path for the file * @param mixed $parse if set to true, parse the result with the passed method. See: "str::parse()" for more info about available methods. * @return mixed */ static function read($file, $parse = false) { $content = @file_get_contents($file); return $parse ? str::parse($content, $parse) : $content; }
function testParse() { $this->assertEqual(str::parse('{"test":"cool","super":"genious"}'), array('test' => 'cool', 'super' => 'genious')); $this->assertEqual(str::parse('<xml><entries><cool>nice</cool></entries></xml>', 'xml'), array('entries' => array('cool' => 'nice'))); }