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