3 * Plugin to render old skool templates
5 * Captures rendered parts from the output buffer, passes them through a template file: tpl/index.html
6 * Adds an API method at index.php/template/update which lets you overwrite the template file
7 * Requires username/password and a single POST parameter called "template"
8 * The method is disabled unless the user is #1, the first user of the system
12 * @author Brian Hendrickson <brian@megapump.com>
13 * @copyright 2009 Megapump, Inc.
14 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
15 * @link http://megapump.com/
18 if (!defined('LACONICA')) {
22 define('TEMPLATEPLUGIN_VERSION', '0.1');
24 class TemplatePlugin extends Plugin {
26 var $blocks = array();
28 function __construct() {
29 parent::__construct();
32 // capture the RouterInitialized event
33 // and connect a new API method
34 // for updating the template
35 function onRouterInitialized( &$m ) {
36 $m->connect( 'template/update', array(
37 'action' => 'template',
48 function onStartShowHead( &$act ) {
49 $this->clear_xmlWriter($act);
51 $this->blocks['head'] = $act->xw->flush();
52 $act->showStylesheets();
53 $this->blocks['styles'] = $act->xw->flush();
55 $this->blocks['scripts'] = $act->xw->flush();
57 $this->blocks['feeds'] = $act->xw->flush();
58 $act->showOpenSearch();
59 $this->blocks['search'] = $act->xw->flush();
60 $act->showDescription();
61 $this->blocks['description'] = $act->xw->flush();
66 function onStartShowContentBlock( &$act ) {
67 $this->clear_xmlWriter($act);
70 function onEndShowContentBlock( &$act ) {
71 $this->blocks['bodytext'] = $act->xw->flush();
75 function onStartShowLocalNavBlock( &$act ) {
76 $this->clear_xmlWriter($act);
79 function onEndShowLocalNavBlock( &$act ) {
80 $this->blocks['localnav'] = $act->xw->flush();
84 function onStartShowExportData( &$act ) {
85 $this->clear_xmlWriter($act);
88 function onEndShowExportData( &$act ) {
89 $this->blocks['export'] = $act->xw->flush();
98 // <%groupstatistics%>
103 // <%groupsbymembers%>
104 function onStartShowSections( &$act ) {
106 $this->clear_xmlWriter($act);
109 $act->showSubscriptions();
110 $this->blocks['subscriptions'] = $act->xw->flush();
111 $act->showSubscribers();
112 $this->blocks['subscribers'] = $act->xw->flush();
114 $this->blocks['groups'] = $act->xw->flush();
115 $act->showStatistics();
116 $this->blocks['statistics'] = $act->xw->flush();
117 $cloud = new PersonalTagCloudSection($act, $act->user);
119 $this->blocks['cloud'] = $act->xw->flush();
123 $this->blocks['groupmembers'] = $act->xw->flush();
124 $act->showStatistics();
125 $this->blocks['groupstatistics'] = $act->xw->flush();
126 $cloud = new GroupTagCloudSection($act, $act->group);
128 $this->blocks['groupcloud'] = $act->xw->flush();
131 $pop = new PopularNoticeSection($act);
133 $this->blocks['popular'] = $act->xw->flush();
134 $gbp = new GroupsByPostsSection($act);
136 $this->blocks['groupsbyposts'] = $act->xw->flush();
137 $feat = new FeaturedUsersSection($act);
139 $this->blocks['featuredusers'] = $act->xw->flush();
142 $gbp = new GroupsByPostsSection($act);
144 $this->blocks['groupsbyposts'] = $act->xw->flush();
145 $gbm = new GroupsByMembersSection($act);
147 $this->blocks['groupsbymembers'] = $act->xw->flush();
157 function onStartShowHeader( &$act ) {
158 $this->clear_xmlWriter($act);
160 $this->blocks['logo'] = $act->xw->flush();
161 $act->showPrimaryNav();
162 $this->blocks['nav'] = $act->xw->flush();
163 $act->showSiteNotice();
164 $this->blocks['notice'] = $act->xw->flush();
165 if (common_logged_in()) {
166 $act->showNoticeForm();
168 $act->showAnonymousMessage();
170 $this->blocks['noticeform'] = $act->xw->flush();
176 function onStartShowFooter( &$act ) {
177 $this->clear_xmlWriter($act);
178 $act->showSecondaryNav();
179 $this->blocks['secondarynav'] = $act->xw->flush();
180 $act->showLicenses();
181 $this->blocks['licenses'] = $act->xw->flush();
185 // capture the EndHTML event
186 // and include the template
187 function onEndEndHTML($act) {
189 global $action, $tags;
191 // set the action and title values
194 'title'=>$act->title(). " - ". common_config('site', 'name')
197 // use the PHP template
198 // unless laconica config:
199 // $config['template']['mode'] = 'html';
200 if (!(common_config('template', 'mode') == 'html')) {
201 $tpl_file = $this->templateFolder() . '/index.php';
202 $tags = array_merge($vars,$this->blocks);
207 $tpl_file = $this->templateFolder() . '/index.html';
209 // read the static template
210 $output = file_get_contents( $tpl_file );
214 // get a list of the <%tags%> in the template
215 $pattern='/<%([a-z]+)%>/';
217 if ( 1 <= preg_match_all( $pattern, $output, $found ))
220 // for each found tag, set its value from the rendered blocks
221 foreach( $tags[0][1] as $pos=>$tag ) {
222 if (isset($this->blocks[$tag]))
223 $vars[$tag] = $this->blocks[$tag];
225 // didn't find a block for the tag
226 elseif (!isset($vars[$tag]))
230 // replace the tags in the template
231 foreach( $vars as $key=>$val )
232 $output = str_replace( '<%'.$key.'%>', $val, $output );
239 function templateFolder() {
243 // catching the StartShowHTML event to halt the rendering
244 function onStartShowHTML( &$act ) {
245 $this->clear_xmlWriter($act);
249 // clear the xmlWriter
250 function clear_xmlWriter( &$act ) {
251 $act->xw->openMemory();
252 $act->xw->setIndent(true);
258 * Action for updating the template remotely
260 * "template/update" -- a POST method that requires a single
261 * parameter "template", containing the new template code
265 * @author Brian Hendrickson <brian@megapump.com>
266 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
267 * @link http://megapump.com/
271 class TemplateAction extends Action
274 function prepare($args) {
275 parent::prepare($args);
279 function handle($args) {
281 parent::handle($args);
283 if (!isset($_SERVER['PHP_AUTH_USER'])) {
285 // not authenticated, show login form
286 header('WWW-Authenticate: Basic realm="Laconica API"');
288 // cancelled the browser login form
289 $this->clientError(_('Authentication error!'), $code = 401);
293 $nick = $_SERVER['PHP_AUTH_USER'];
294 $pass = $_SERVER['PHP_AUTH_PW'];
296 // check username and password
297 $user = common_check_user($nick,$pass);
301 // verify that user is admin
302 if (!($user->id == 1))
303 $this->clientError(_('only User #1 can update the template'), $code = 401);
305 // open the old template
306 $tpl_file = $this->templateFolder() . '/index.html';
307 $fp = fopen( $tpl_file, 'w+' );
309 // overwrite with the new template
310 fwrite($fp, $this->arg('template'));
313 header('HTTP/1.1 200 OK');
314 header('Content-type: text/plain');
315 print "Template Updated!";
319 // bad username and password
320 $this->clientError(_('Authentication error!'), $code = 401);
329 * Function for retrieving a laconica display section
331 * requires one parameter, the name of the section
332 * section names are listed in the comments of the TemplatePlugin class
336 * @author Brian Hendrickson <brian@megapump.com>
337 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
338 * @link http://megapump.com/
342 function section($tagname) {
344 if (isset($tags[$tagname]))
345 return $tags[$tagname];