root/trunk/wifidog-auth/wifidog/classes/MainUI.php @ 1013

Revision 1013, 19.9 KB (checked in by benoitg, 7 years ago)

* dump_initial_data_postgres.sh: Add the
content_available_display_pages table to the dump, can someone
re-generate the initial data from a working install
* Remove the ?> tags from all the classes.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php
2
3
4/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5
6// +-------------------------------------------------------------------+
7// | WiFiDog Authentication Server                                     |
8// | =============================                                     |
9// |                                                                   |
10// | The WiFiDog Authentication Server is part of the WiFiDog captive  |
11// | portal suite.                                                     |
12// +-------------------------------------------------------------------+
13// | PHP version 5 required.                                           |
14// +-------------------------------------------------------------------+
15// | Homepage:     http://www.wifidog.org/                             |
16// | Source Forge: http://sourceforge.net/projects/wifidog/            |
17// +-------------------------------------------------------------------+
18// | This program is free software; you can redistribute it and/or     |
19// | modify it under the terms of the GNU General Public License as    |
20// | published by the Free Software Foundation; either version 2 of    |
21// | the License, or (at your option) any later version.               |
22// |                                                                   |
23// | This program is distributed in the hope that it will be useful,   |
24// | but WITHOUT ANY WARRANTY; without even the implied warranty of    |
25// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
26// | GNU General Public License for more details.                      |
27// |                                                                   |
28// | You should have received a copy of the GNU General Public License |
29// | along with this program; if not, contact:                         |
30// |                                                                   |
31// | Free Software Foundation           Voice:  +1-617-542-5942        |
32// | 59 Temple Place - Suite 330        Fax:    +1-617-542-2652        |
33// | Boston, MA  02111-1307,  USA       gnu@gnu.org                    |
34// |                                                                   |
35// +-------------------------------------------------------------------+
36
37/**
38 * @package    WiFiDogAuthServer
39 * @author     Benoit Gregoire <bock@step.polymtl.ca>
40 * @copyright  2005-2006 Benoit Gregoire, Technologies Coeus inc.
41 * @version    Subversion $Id$
42 * @link       http://www.wifidog.org/
43 */
44
45/**
46 * @internal We put a call to validate_schema() here so it systematically called
47 * from any UI page, but not from any machine readable pages
48 */
49require_once ('include/schema_validate.php');
50validate_schema();
51
52/**
53 * If the database doesn't get cleaned up by a cron job, we'll do now
54 */
55if (CONF_USE_CRON_FOR_DB_CLEANUP == false) {
56    garbage_collect();
57}
58
59/**
60 * Load required file
61 */
62require_once ('include/common_interface.php');
63
64/**
65 * Style contains functions managing headers, footers, stylesheet, etc.
66 *
67 * @package    WiFiDogAuthServer
68 * @author     Benoit Gregoire <bock@step.polymtl.ca>
69 * @copyright  2005-2006 Benoit Gregoire, Technologies Coeus inc.
70 */
71class MainUI {
72    /**
73     * Available structural display areas where content can be placed
74     *
75     * @var array
76     * @access private
77     */
78    private $available_display_areas;
79
80    /**
81    * Content to be displayed the page
82    *
83    * @var array
84    * @access private
85    */
86    private $contentArray;
87
88    /**
89     * Object for Smarty class
90     *
91     * @var object
92     * @access private
93     */
94    private $smarty;
95
96    /**
97     * Title of HTML page
98     *
99     * @var string
100     * @access private
101     */
102    private $title;
103    /**
104     * Additional class of the <body> of the HTML page
105     */
106    private $page_name;
107
108    /**
109     * Headers of HTML page
110     *
111     * @var private
112     * @access private
113     */
114    private $html_headers;
115
116    /**
117     * Defines if tool section of HTML page is enabled or not
118     *
119     * @var bool
120     * @access private
121     */
122    private $tool_section_enabled = true;
123
124    /**
125     * Scripts for the footer
126     *
127     * @var array
128     * @access private
129     */
130    private $footer_scripts = array ();
131
132    /**
133     * Contructor
134     *
135     * @return void
136     *
137     * @access public
138     */
139    public function __construct() {
140        global $db;
141        // Init Smarty
142        $this->smarty = new SmartyWifidog();
143
144        // Set default title
145        $this->title = Network :: getCurrentNetwork()->getName().' '._("authentication server");
146        // Init the content array
147        $current_content_sql = "SELECT display_area FROM content_available_display_areas\n";
148        $rows = array ();
149        $db->execSql($current_content_sql, $rows, false);
150        foreach ($rows as $row) {
151            $this->contentArray[$row['display_area']] = '';
152        }
153    }
154
155    /**
156     * Add content to a structural area of the page
157     *
158     * @param string $display_area Structural area where content is to be
159     * placed.  Must be one of the display aread defined in the
160     * content_available_display_areas table
161     *
162     * @param string $content HTML content to be added to the area
163     *
164     * @return void
165     */
166    public function appendContent($display_area, $content) {
167        if (!isset ($this->contentArray[$display_area])) {
168            throw new exception(sprintf(_('%s is not a valid structural display area'), $display_area));
169        }
170        $this->contentArray[$display_area] .= $content;
171    }
172
173    /**
174     * Check if the tool section is enabled
175     *
176     * @return bool True or false
177     *
178     * @access public
179     */
180    public function isToolSectionEnabled() {
181        return $this->tool_section_enabled;
182    }
183
184    /**
185     * Check if the tool section is enabled
186     *
187     * @return bool True or false
188     *
189     * @access public
190     */
191    public function setToolSectionEnabled($status) {
192        $this->tool_section_enabled = $status;
193    }
194
195    /**
196     * Set the title of the HTML page
197     *
198     * @param string $title_string Title of the HTML page
199     *
200     * @return void
201     *
202     * @access public
203     */
204    public function setTitle($title_string) {
205        $this->title = $title_string;
206    }
207
208    /**
209     * Set the class name of the <body> of the resulting page.
210     *
211     * @param string $page_name_string The page name of the resulting page.  Must have no spaces.  ex:  portal, login, userprofile, etc.)
212     *
213     * @return void
214     *
215     * @access public
216     */
217    public function setPageName($page_name_string) {
218        $this->page_name = $page_name_string;
219    }
220
221    /**
222    * Add content at the very end of the <body>.
223    *
224    * This is NOT meant to add footers or other display content, it is meant
225    * to add <script></script> tag pairs that have to be executed only once
226    * the page is loaded.
227    *
228    * @param string $script A piece of script surrounded by
229    *                       <script></script> tags.
230    *
231    * @return void
232    *
233    * @access public
234    */
235    public function addFooterScript($script) {
236        $this->footer_scripts[] = $script;
237    }
238
239    /**
240     * Set the HTML page headers
241     *
242     * @param string $headers_string HTML page headers
243     *
244     * @return void
245     *
246     * @access public
247     */
248    public function setHtmlHeader($headers_string) {
249        $this->html_headers = $headers_string;
250    }
251
252    /**
253     * Set the section to be displayed in the tool pane
254     *
255     * @param string $section Section to be displayed:
256     *                          + ADMIN for administration tool pane
257     *
258     * @return string HTML code of tool pane
259     *
260     * @access public
261     */
262    public function setToolSection($section) {
263        // Init ALL smarty SWITCH values
264        $this->smarty->assign('sectionADMIN', false);
265
266        switch ($section) {
267            case "ADMIN" :
268                // Set section of Smarty template
269                $this->smarty->assign('sectionADMIN', true);
270
271                // Get information about user
272                $_currentUser = User :: getCurrentUser();
273
274                if ($_currentUser && $_currentUser->isNobody()) {
275                    // The user has no permission to access the administrative functions
276                    $_html = _("You do not have permissions to access any administration functions.");
277                } else {
278                    // Init values
279                    $_sqlAdditionalWhere = "";
280
281                    // Init ALL smarty values
282                    $this->smarty->assign('isSuperAdmin', false);
283                    $this->smarty->assign('isOwner', false);
284                    $this->smarty->assign('formAction', "");
285                    $this->smarty->assign('nodeUI', "");
286                    $this->smarty->assign('networkUI', "");
287
288                    // Define user security levels for the template
289                    $this->smarty->assign('isSuperAdmin', $_currentUser && $_currentUser->isSuperAdmin());
290                    $this->smarty->assign('isOwner', $_currentUser && $_currentUser->isOwner());
291
292                    /*
293                     * If the user is super admin OR owner of at least one node
294                     * show the node menu
295                     */
296                    if ($_currentUser && ($_currentUser->isSuperAdmin() || $_currentUser->isOwner())) {
297                        // Assign the action URL for the form
298                        $this->smarty->assign('formAction', GENERIC_OBJECT_ADMIN_ABS_HREF);
299
300                        /*
301                         * If current user is a owner the SQL query must be changed
302                         * to return his nodes only
303                         */
304                        if (!$_currentUser->isSuperAdmin()) {
305                            $_sqlAdditionalWhere = "AND node_id IN (SELECT node_id from node_stakeholders WHERE is_owner = true AND user_id='".$_currentUser->getId()."')";
306                        }
307
308                        // Provide node select control to the template
309                        $this->smarty->assign('nodeUI', Node :: getSelectNodeUI('object_id', $_sqlAdditionalWhere));
310                    }
311
312                    // If the user is network admin show the network menu
313                    if ($_currentUser && $_currentUser->isSuperAdmin()) {
314                        // Provide network select control to the template
315                        $this->smarty->assign('networkUI', Network :: getSelectNetworkUI('object_id'));
316                    }
317
318                    // Compile HTML code
319                    $_html = $this->smarty->fetch("templates/classes/MainUI_ToolSection.tpl");
320                }
321                break;
322
323            default :
324                $_html = _("Unknown section:").$section;
325                break;
326        }
327
328        $this->appendContent('left_area_middle', $_html);
329    }
330
331    /**
332     * Get the content to be displayed in the tool pane
333     *
334     * @return string HTML markup
335     *
336     * @access private
337     */
338    private function getToolContent() {
339            // Define globals
340    global $session;
341        global $AVAIL_LOCALE_ARRAY;
342
343        // Init values
344        $_html = "";
345        $_gwId = null;
346        $_gwAddress = null;
347        $_gwPort = null;
348        $_selected = "";
349        $_languageChooser = array ();
350
351        // Init ALL smarty SWITCH values
352        $this->smarty->assign('sectionSTART', false);
353        $this->smarty->assign('sectionLOGIN', false);
354
355                // Set section of Smarty template
356                $this->smarty->assign('sectionSTART', true);
357
358                // Get information about user
359                $_currentUser = User :: getCurrentUser();
360
361                // Init ALL smarty values
362                $this->smarty->assign('networkHomepageURL', "");
363                $this->smarty->assign('networkName', "");
364                $this->smarty->assign('isValidUser', false);
365                $this->smarty->assign('username', "");
366                $this->smarty->assign('logoutParameters', "");
367                $this->smarty->assign('loginParameters', "");
368                $this->smarty->assign('formAction', "");
369                $this->smarty->assign('toolContent', "");
370                $this->smarty->assign('accountInformation', "");
371                $this->smarty->assign('techSupportInformation', "");
372
373                // Provide Smarty with information about the network
374                $this->smarty->assign('networkHomepageURL', Network :: getCurrentNetwork()->getHomepageURL());
375                $this->smarty->assign('networkName', Network :: getCurrentNetwork()->getName());
376
377                /*
378                 * Provide Smarty information about the user's login/logout status
379                 */
380
381                if ($_currentUser != null) {
382                    // User is logged in
383                    $this->smarty->assign('isValidUser', true);
384
385                    // Set username for Smarty
386                    $this->smarty->assign('username', $_currentUser->getUsername());
387
388                    // Detect gateway information
389                    $_gwId = $session->get(SESS_GW_ID_VAR);
390                    $_gwAddress = $session->get(SESS_GW_ADDRESS_VAR);
391                    $_gwPort = $session->get(SESS_GW_PORT_VAR);
392
393                    // If gateway information could be detected tell them Smarty
394                    if ($_gwId && $_gwAddress && $_gwPort) {
395                        $this->smarty->assign('logoutParameters', "&amp;gw_id=".$_gwId."&amp;gw_address=".$_gwAddress."&amp;gw_port=".$_gwPort);
396                    }
397                } else {
398                    // Detect gateway information
399                    $_gwId = !empty ($_REQUEST['gw_id']) ? $_REQUEST['gw_id'] : $session->get(SESS_GW_ID_VAR);
400                    $_gwAddress = !empty ($_REQUEST['gw_address']) ? $_REQUEST['gw_address'] : $session->get(SESS_GW_ADDRESS_VAR);
401                    $_gwPort = !empty ($_REQUEST['gw_port']) ? $_REQUEST['gw_port'] : $session->get(SESS_GW_PORT_VAR);
402
403                    // If gateway information could be detected tell them Smarty
404                    if (!empty ($_gwId) && !empty ($_gwAddress) && !empty ($_gwPort)) {
405                        $this->smarty->assign('loginParameters', "?gw_id=".$_gwId."&amp;gw_address=".$_gwAddress."&amp;gw_port=".$_gwPort);
406                    }
407                }
408
409                /*
410                 * Provide Smarty information for the language chooser
411                 */
412
413                // Assign the action URL for the form
414                $this->smarty->assign('formAction', $_SERVER['REQUEST_URI']);
415
416                foreach ($AVAIL_LOCALE_ARRAY as $_langIds => $_langNames) {
417                    if (Locale :: getCurrentLocale()->getId() == $_langIds) {
418                        $_selected = ' selected="selected"';
419                    } else {
420                        $_selected = "";
421                    }
422
423                    $_languageChooser[] = '<option label="'.$_langNames.'" value="'.$_langIds.'"'.$_selected.'>'.$_langNames.'</option>';
424                }
425
426                // Provide Smarty all available languages
427                $this->smarty->assign('languageChooser', $_languageChooser);
428
429                /*
430                 * Provide Smarty information for the language chooser
431                 */
432                 
433                // Provide information
434                $this->smarty->assign('accountInformation', sprintf(_("Accounts on %s are and will stay completely free."), Network :: getCurrentNetwork()->getName()));
435                $this->smarty->assign('techSupportInformation', sprintf(_("Please inform us of any problem or service interruption at: %s"), '<a href="mailto:'.Network :: getCurrentNetwork()->getTechSupportEmail().'">'.Network :: getCurrentNetwork()->getTechSupportEmail().'</a>'));
436
437                // Compile HTML code
438                $_html = $this->smarty->fetch("templates/classes/MainUI_ToolContent.tpl");
439 
440        return $_html;
441    }
442
443    /**
444     * Display the main page
445     *
446     * @return void
447     *
448     * @access public
449     * @internal Uses a few request parameters to displaty debug information.
450     * If $_REQUEST['debug_request'] is present, it will print out the
451     * $_REQUEST array at the top of the page.
452     */
453    public function display() {
454        // Init values
455        $_stylesheetFile = "";
456
457        // Init ALL smarty values
458        $this->smarty->assign('htmlHeaders', "");
459        $this->smarty->assign('title', "");
460        $this->smarty->assign('stylesheetURL', "");
461        $this->smarty->assign('stylesheetParsedFile', "");
462        $this->smarty->assign('isSuperAdmin', false);
463        $this->smarty->assign('isOwner', false);
464        $this->smarty->assign('debugRequested', false);
465        $this->smarty->assign('debugOutput', "");
466        $this->smarty->assign('footerScripts', array ());
467
468        // Add HTML headers
469        $this->smarty->assign('htmlHeaders', $this->html_headers);
470
471        // Asign title
472        $this->smarty->assign('title', $this->title);
473
474        // Asign CSS class for body
475        $this->smarty->assign('page_name', $this->page_name);
476
477        // Asign path to CSS stylesheet
478        $this->smarty->assign('stylesheetURL', COMMON_CONTENT_URL.STYLESHEET_NAME);
479
480        /*
481         * Include stylesheet to be parsed by Smarty
482         */
483        if (is_file(NODE_CONTENT_PHP_RELATIVE_PATH.STYLESHEET_NAME)) {
484            $_stylesheetFile = NODE_CONTENT_SMARTY_PATH.STYLESHEET_NAME;
485        } else {
486            $_stylesheetFile = DEFAULT_CONTENT_SMARTY_PATH.STYLESHEET_NAME;
487        }
488
489        // Asign path to CSS stylesheet to be parsed by Smarty
490        $this->smarty->assign('stylesheetParsedFile', $_stylesheetFile);
491
492        /*
493         * Allow super admin to display debug output if requested by using
494         * $_REQUEST['debug_request']
495         */
496
497        // Get information about user
498        $_currentUser = User :: getCurrentUser();
499
500        // Define user security levels for the template
501        $this->smarty->assign('isSuperAdmin', $_currentUser && $_currentUser->isSuperAdmin());
502        $this->smarty->assign('isOwner', $_currentUser && $_currentUser->isOwner());
503
504        if (isset ($_REQUEST['debug_request']) && ($_currentUser && $_currentUser->isSuperAdmin())) {
505            // Tell Smarty everything it needs to know
506            $this->smarty->assign('debugRequested', true);
507            $this->smarty->assign('debugOutput', print_r($_REQUEST, true));
508        }
509
510        /*
511         * Build tool pane if it has been enabled
512         */
513        if ($this->isToolSectionEnabled()) {
514            $this->appendContent('left_area_top', $this->getToolContent());
515        }
516
517        // Provide the content array to Smarty
518        $this->smarty->assign('contentArray', $this->contentArray);
519
520        // Provide footer scripts to Smarty
521        $this->smarty->assign('footerScripts', $this->footer_scripts);
522
523        // Compile HTML code and output it
524        $this->smarty->display("templates/classes/MainUI_Display.tpl");
525    }
526
527    /**
528     * Display a generic error message
529     *
530     * @param string $errmsg                  The error message to be displayed
531     * @param bool   $show_tech_support_email Defines wether to show the link of
532     *                                        the tech-support
533     *
534     * @return void
535     *
536     * @access public
537     */
538    function displayError($errmsg, $show_tech_support_email = true) {
539            // Init ALL smarty values
540    $this->smarty->assign("error", "");
541        $this->smarty->assign("show_tech_support_email", false);
542        $this->smarty->assign("tech_support_email", "");
543
544        // Define needed error content
545        $this->smarty->assign("error", $errmsg);
546
547        if ($show_tech_support_email) {
548            $this->smarty->assign("show_tech_support_email", true);
549            $this->smarty->assign("tech_support_email", Network :: getCurrentNetwork()->getTechSupportEmail());
550        }
551
552        /*
553         * Output the error message
554         */
555        $_html = $this->smarty->fetch("templates/sites/error.tpl");
556
557        $this->appendContent('page_header', $_html);
558        $this->display();
559    }
560
561}
562
563/*
564 * Local variables:
565 * tab-width: 4
566 * c-basic-offset: 4
567 * c-hanging-comment-ender-p: nil
568 * End:
569 */
Note: See TracBrowser for help on using the browser.