function Dwoo_Plugin_rss(Dwoo_Core $dwoo, $feed, $assign = 'rss_items', $limit = 5, $cacheTime = 60) { $cacheKey = 'rss:' . $feed; if (false === ($rss = Cache::fetch($cacheKey))) { $rss = new RssReader(); $rss->load($feed); Cache::store($cacheKey, $rss, $cacheTime); } if ($limit) { $dwoo->assignInScope(array_slice($rss->getItems(), 0, $limit), $assign); } else { $dwoo->assignInScope($rss->getItems(), $assign); } return ''; }
/** * Reads a file * <pre> * * file : path or URI of the file to read (however reading from another website is not recommended for performance reasons) * * assign : if set, the file will be saved in this variable instead of being output * </pre> * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from the use of this software. * * @author Jordi Boggiano <*****@*****.**> * @copyright Copyright (c) 2008, Jordi Boggiano * @license http://dwoo.org/LICENSE Modified BSD License * @link http://dwoo.org/ * @version 1.1.0 * @date 2009-07-18 * @package Dwoo */ function Dwoo_Plugin_fetch(Dwoo_Core $dwoo, $file, $assign = null) { if ($file === '') { return; } if ($policy = $dwoo->getSecurityPolicy()) { while (true) { if (preg_match('{^([a-z]+?)://}i', $file)) { return $dwoo->triggerError('The security policy prevents you to read files from external sources.', E_USER_WARNING); } $file = realpath($file); $dirs = $policy->getAllowedDirectories(); foreach ($dirs as $dir => $dummy) { if (strpos($file, $dir) === 0) { break 2; } } return $dwoo->triggerError('The security policy prevents you to read <em>' . $file . '</em>', E_USER_WARNING); } } $file = str_replace(array("\t", "\n", "\r"), array('\\t', '\\n', '\\r'), $file); $out = file_get_contents($file); if ($assign === null) { return $out; } $dwoo->assignInScope($out, $assign); }
/** * Inserts another template into the current one * <pre> * * file : the resource name of the template * * cache_time : cache length in seconds * * cache_id : cache identifier for the included template * * compile_id : compilation identifier for the included template * * data : data to feed into the included template, it can be any array and will default to $_root (the current data) * * assign : if set, the output of the included template will be saved in this variable instead of being output * * rest : any additional parameter/value provided will be added to the data array * </pre> * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from the use of this software. * * @author Jordi Boggiano <*****@*****.**> * @copyright Copyright (c) 2008, Jordi Boggiano * @license http://dwoo.org/LICENSE Modified BSD License * @link http://dwoo.org/ * @version 1.1.0 * @date 2009-07-18 * @package Dwoo */ function Dwoo_Plugin_include(Dwoo_Core $dwoo, $file, $cache_time = null, $cache_id = null, $compile_id = null, $data = '_root', $assign = null, array $rest = array()) { if ($file === '') { return; } if (preg_match('#^([a-z]{2,}):(.*)$#i', $file, $m)) { // resource:identifier given, extract them $resource = $m[1]; $identifier = $m[2]; } else { // get the current template's resource $resource = $dwoo->getTemplate()->getResourceName(); $identifier = $file; } try { $include = $dwoo->templateFactory($resource, $identifier, $cache_time, $cache_id, $compile_id); } catch (Dwoo_Security_Exception $e) { return $dwoo->triggerError('Include : Security restriction : ' . $e->getMessage(), E_USER_WARNING); } catch (Dwoo_Exception $e) { return $dwoo->triggerError('Include : ' . $e->getMessage(), E_USER_WARNING); } if ($include === null) { return $dwoo->triggerError('Include : Resource "' . $resource . ':' . $identifier . '" not found.', E_USER_WARNING); } elseif ($include === false) { return $dwoo->triggerError('Include : Resource "' . $resource . '" does not support includes.', E_USER_WARNING); } if (is_string($data)) { $vars = $dwoo->readVar($data); } else { $vars = $data; } if (count($rest)) { $vars = $rest + $vars; } $clone = clone $dwoo; $out = $clone->get($include, $vars); if ($assign !== null) { $dwoo->assignInScope($out, $assign); } foreach ($clone->getReturnValues() as $name => $value) { $dwoo->assignInScope($value, $name); } if ($assign === null) { return $out; } }
function Dwoo_Plugin_twitter(Dwoo_Core $dwoo, $username = false, $query = false, $assign = 'tweets', $count = 5, $cacheTime = 60) { if ($query) { $cacheKey = sprintf('twitter?query=%s', urlencode($query)); $feedURL = sprintf('http://search.twitter.com/search.json?q=%s', urlencode($query)); $twitterData = Twitter::getFeedData($feedURL, $cacheKey, $cacheTime); $results = $twitterData['results']; } elseif ($username) { $cacheKey = sprintf('twitter?username=%s', urlencode($username)); $feedURL = sprintf('http://api.twitter.com/1/statuses/user_timeline/%s.json', urlencode($username)); $twitterData = Twitter::getFeedData($feedURL, $cacheKey, $cacheTime); $results = $twitterData; } if (!empty($results) && is_array($results)) { $dwoo->assignInScope(array_slice($results, 0, $count), $assign); } else { $dwoo->assignInScope(array(), $assign); } return ''; }
/** * Evaluates the given string as if it was a template * * Although this plugin is kind of optimized and will * not recompile your string each time, it is still not * a good practice to use it. If you want to have templates * stored in a database or something you should probably use * the Dwoo_Template_String class or make another class that * extends it * <pre> * * var : the string to use as a template * * assign : if set, the output of the template will be saved in this variable instead of being output * </pre> * This software is provided 'as-is', without any express or implied warranty. * In no event will the authors be held liable for any damages arising from the use of this software. * * @author Jordi Boggiano <*****@*****.**> * @copyright Copyright (c) 2008, Jordi Boggiano * @license http://dwoo.org/LICENSE Modified BSD License * @link http://dwoo.org/ * @version 1.0.0 * @date 2008-10-23 * @package Dwoo */ function Dwoo_Plugin_eval(Dwoo_Core $dwoo, $var, $assign = null) { if ($var == '') { return; } $tpl = new Dwoo_Template_String($var); $clone = clone $dwoo; $out = $clone->get($tpl, $dwoo->readVar('_parent')); if ($assign !== null) { $dwoo->assignInScope($out, $assign); } else { return $out; } }