]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/TemplatePlugin.php
Merge branch 'master' of gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / plugins / TemplatePlugin.php
1 <?php
2 /**
3  * Plugin to render old skool templates
4  *
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
9  *
10  * @category  Plugin
11  * @package   StatusNet
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/
16  */
17
18 if (!defined('STATUSNET')) {
19     exit(1);
20 }
21
22 define('TEMPLATEPLUGIN_VERSION', '0.1');
23
24 class TemplatePlugin extends Plugin {
25
26   var $blocks = array();
27
28   function __construct() {
29     parent::__construct();
30   }
31
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',
38     ));
39   }
40
41   // <%styles%>
42   // <%scripts%>
43   // <%search%>
44   // <%feeds%>
45   // <%description%>
46   // <%head%>
47   function onStartShowHead( &$act ) {
48     $this->clear_xmlWriter($act);
49     $act->extraHead();
50     $this->blocks['head'] = $act->xw->flush();
51     $act->showStylesheets();
52     $this->blocks['styles'] = $act->xw->flush();
53     $act->showScripts();
54     $this->blocks['scripts'] = $act->xw->flush();
55     $act->showFeeds();
56     $this->blocks['feeds'] = $act->xw->flush();
57     $act->showOpenSearch();
58     $this->blocks['search'] = $act->xw->flush();
59     $act->showDescription();
60     $this->blocks['description'] = $act->xw->flush();
61     return false;
62   }
63
64   // <%bodytext%>
65   function onStartShowContentBlock( &$act ) {
66     $this->clear_xmlWriter($act);
67     return true;
68   }
69   function onEndShowContentBlock( &$act ) {
70     $this->blocks['bodytext'] = $act->xw->flush();
71   }
72
73   // <%localnav%>
74   function onStartShowLocalNavBlock( &$act ) {
75     $this->clear_xmlWriter($act);
76     return true;
77   }
78   function onEndShowLocalNavBlock( &$act ) {
79     $this->blocks['localnav'] = $act->xw->flush();
80   }
81
82   // <%export%>
83   function onStartShowExportData( &$act ) {
84     $this->clear_xmlWriter($act);
85     return true;
86   }
87   function onEndShowExportData( &$act ) {
88     $this->blocks['export'] = $act->xw->flush();
89   }
90
91   // <%subscriptions%>
92   // <%subscribers%>
93   // <%groups%>
94   // <%statistics%>
95   // <%cloud%>
96   // <%groupmembers%>
97   // <%groupstatistics%>
98   // <%groupcloud%>
99   // <%popular%>
100   // <%groupsbyposts%>
101   // <%featuredusers%>
102   // <%groupsbymembers%>
103   function onStartShowSections( &$act ) {
104     global $action;
105     $this->clear_xmlWriter($act);
106     switch ($action) {
107       case "showstream":
108         $act->showSubscriptions();
109         $this->blocks['subscriptions'] = $act->xw->flush();
110         $act->showSubscribers();
111         $this->blocks['subscribers'] = $act->xw->flush();
112         $act->showGroups();
113         $this->blocks['groups'] = $act->xw->flush();
114         $act->showStatistics();
115         $this->blocks['statistics'] = $act->xw->flush();
116         $cloud = new PersonalTagCloudSection($act, $act->user);
117         $cloud->show();
118         $this->blocks['cloud'] = $act->xw->flush();
119         break;
120       case "showgroup":
121         $act->showMembers();
122         $this->blocks['groupmembers'] = $act->xw->flush();
123         $act->showStatistics();
124         $this->blocks['groupstatistics'] = $act->xw->flush();
125         $cloud = new GroupTagCloudSection($act, $act->group);
126         $cloud->show();
127         $this->blocks['groupcloud'] = $act->xw->flush();
128         break;
129       case "public":
130         $pop = new PopularNoticeSection($act);
131         $pop->show();
132         $this->blocks['popular'] = $act->xw->flush();
133         $gbp = new GroupsByPostsSection($act);
134         $gbp->show();
135         $this->blocks['groupsbyposts'] = $act->xw->flush();
136         $feat = new FeaturedUsersSection($act);
137         $feat->show();
138         $this->blocks['featuredusers'] = $act->xw->flush();
139         break;
140       case "groups":
141         $gbp = new GroupsByPostsSection($act);
142         $gbp->show();
143         $this->blocks['groupsbyposts'] = $act->xw->flush();
144         $gbm = new GroupsByMembersSection($act);
145         $gbm->show();
146         $this->blocks['groupsbymembers'] = $act->xw->flush();
147         break;
148     }
149     return false;
150   }
151
152   // <%logo%>
153   // <%nav%>
154   // <%notice%>
155   // <%noticeform%>
156   function onStartShowHeader( &$act ) {
157     $this->clear_xmlWriter($act);
158     $act->showLogo();
159     $this->blocks['logo'] = $act->xw->flush();
160     $act->showPrimaryNav();
161     $this->blocks['nav'] = $act->xw->flush();
162     $act->showSiteNotice();
163     $this->blocks['notice'] = $act->xw->flush();
164     if (common_logged_in()) {
165         $act->showNoticeForm();
166     } else {
167         $act->showAnonymousMessage();
168     }
169     $this->blocks['noticeform'] = $act->xw->flush();
170     return false;
171   }
172
173   // <%secondarynav%>
174   // <%licenses%>
175   function onStartShowFooter( &$act ) {
176     $this->clear_xmlWriter($act);
177     $act->showSecondaryNav();
178     $this->blocks['secondarynav'] = $act->xw->flush();
179     $act->showLicenses();
180     $this->blocks['licenses'] = $act->xw->flush();
181     return false;
182   }
183
184   // capture the EndHTML event
185   // and include the template
186   function onEndEndHTML($act) {
187
188     global $action, $tags;
189
190     // set the action and title values
191     $vars = array(
192       'action'=>$action,
193       'title'=>$act->title(). " - ". common_config('site', 'name')
194     );
195
196     // use the PHP template
197     // unless statusnet config:
198     //   $config['template']['mode'] = 'html';
199     if (!(common_config('template', 'mode') == 'html')) {
200       $tpl_file = $this->templateFolder() . '/index.php';
201       $tags = array_merge($vars,$this->blocks);
202       include $tpl_file;
203       return;
204     }
205
206     $tpl_file = $this->templateFolder() . '/index.html';
207
208     // read the static template
209     $output = file_get_contents( $tpl_file );
210
211     $tags = array();
212
213     // get a list of the <%tags%> in the template
214     $pattern='/<%([a-z]+)%>/';
215
216     if ( 1 <= preg_match_all( $pattern, $output, $found ))
217       $tags[] = $found;
218
219     // for each found tag, set its value from the rendered blocks
220     foreach( $tags[0][1] as $pos=>$tag ) {
221       if (isset($this->blocks[$tag]))
222         $vars[$tag] = $this->blocks[$tag];
223
224       // didn't find a block for the tag
225       elseif (!isset($vars[$tag]))
226         $vars[$tag] = '';
227     }
228
229     // replace the tags in the template
230     foreach( $vars as $key=>$val )
231       $output = str_replace( '<%'.$key.'%>', $val, $output );
232
233     echo $output;
234
235     return true;
236
237   }
238   function templateFolder() {
239     return 'tpl';
240   }
241
242   // catching the StartShowHTML event to halt the rendering
243   function onStartShowHTML( &$act ) {
244     $this->clear_xmlWriter($act);
245     return true;
246   }
247
248   // clear the xmlWriter
249   function clear_xmlWriter( &$act ) {
250     $act->xw->openMemory();
251     $act->xw->setIndent(true);
252   }
253
254 }
255
256 /**
257  * Action for updating the template remotely
258  *
259  * "template/update" -- a POST method that requires a single
260  * parameter "template", containing the new template code
261  *
262  * @category Plugin
263  * @package  StatusNet
264  * @author   Brian Hendrickson <brian@megapump.com>
265  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
266  * @link     http://megapump.com/
267  *
268  */
269
270 class TemplateAction extends Action
271 {
272
273   function prepare($args) {
274     parent::prepare($args);
275     return true;
276   }
277
278   function handle($args) {
279
280     parent::handle($args);
281
282     if (!isset($_SERVER['PHP_AUTH_USER'])) {
283
284       // not authenticated, show login form
285       header('WWW-Authenticate: Basic realm="StatusNet API"');
286
287       // cancelled the browser login form
288       $this->clientError(_('Authentication error!'), $code = 401);
289
290     } else {
291
292       $nick = $_SERVER['PHP_AUTH_USER'];
293       $pass = $_SERVER['PHP_AUTH_PW'];
294
295       // check username and password
296       $user = common_check_user($nick,$pass);
297
298       if ($user) {
299
300         // verify that user is admin
301         if (!($user->id == 1))
302           $this->clientError(_('Only User #1 can update the template.'), $code = 401);
303
304         // open the old template
305         $tpl_file = $this->templateFolder() . '/index.html';
306         $fp = fopen( $tpl_file, 'w+' );
307
308         // overwrite with the new template
309         fwrite($fp, $this->arg('template'));
310         fclose($fp);
311
312         header('HTTP/1.1 200 OK');
313         header('Content-type: text/plain');
314         print "Template Updated!";
315
316       } else {
317
318         // bad username and password
319         $this->clientError(_('Authentication error!'), $code = 401);
320
321       }
322
323     }
324   }
325     function onPluginVersion(&$versions)
326     {
327         $versions[] = array('name' => 'Template',
328                             'version' => TEMPLATEPLUGIN_VERSION,
329                             'author' => 'Brian Hendrickson',
330                             'homepage' => 'http://status.net/wiki/Plugin:Template',
331                             'rawdescription' =>
332                             _m('Use an HTML template for Web output.'));
333         return true;
334     }
335
336 }
337
338 /**
339  * Function for retrieving a statusnet display section
340  *
341  * requires one parameter, the name of the section
342  * section names are listed in the comments of the TemplatePlugin class
343  *
344  * @category Plugin
345  * @package  StatusNet
346  * @author   Brian Hendrickson <brian@megapump.com>
347  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
348  * @link     http://megapump.com/
349  *
350  */
351
352 function section($tagname) {
353   global $tags;
354   if (isset($tags[$tagname]))
355     return $tags[$tagname];
356 }
357