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

Revision 1018, 19.6 KB (checked in by rob_janes, 7 years ago)

* javascript for validating and navigating forms.
* help text (untranslated).
* EVENT_LOGGING enabling/disabling flag.
* use SYSTEM_PATH instead of BASE_URL_PATH and BASE_SSL_PATH where ever appropriate.
* standardize Smarty variables for Network and User info by adding assignSmartyValues($smarty, $self) to Network and User.
* change_password only allowed if logged in.
* non-superadmin can only use change_password to change their own password.
* superadmin can use change_password to change any user password by entering in their password for the old password.
* page_header div defined and positioned in MainUI

  • 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        private $shrink_left_area = false;
133
134    /**
135     * Contructor
136     *
137     * @return void
138     *
139     * @access public
140     */
141    public function __construct() {
142        global $db;
143        // Init Smarty
144        $this->smarty = new SmartyWifidog();
145
146        // Set default title
147        $this->title = Network :: getCurrentNetwork()->getName().' '._("authentication server");
148        // Init the content array
149        $current_content_sql = "SELECT display_area FROM content_available_display_areas\n";
150        $rows = array ();
151        $db->execSql($current_content_sql, $rows, false);
152        foreach ($rows as $row) {
153            $this->contentArray[$row['display_area']] = '';
154        }
155    }
156
157    /**
158     * Add content to a structural area of the page
159     *
160     * @param string $display_area Structural area where content is to be
161     * placed.  Must be one of the display aread defined in the
162     * content_available_display_areas table
163     *
164     * @param string $content HTML content to be added to the area
165     *
166     * @return void
167     */
168    public function appendContent($display_area, $content) {
169        if (!isset ($this->contentArray[$display_area])) {
170            throw new exception(sprintf(_('%s is not a valid structural display area'), $display_area));
171        }
172        $this->contentArray[$display_area] .= $content;
173    }
174
175    /**
176     * Check if the tool section is enabled
177     *
178     * @return bool True or false
179     *
180     * @access public
181     */
182    public function isToolSectionEnabled() {
183        return $this->tool_section_enabled;
184    }
185
186    /**
187     * Check if the tool section is enabled
188     *
189     * @return bool True or false
190     *
191     * @access public
192     */
193    public function setToolSectionEnabled($status) {
194        $this->tool_section_enabled = $status;
195    }
196
197    /**
198     * Set the title of the HTML page
199     *
200     * @param string $title_string Title of the HTML page
201     *
202     * @return void
203     *
204     * @access public
205     */
206    public function setTitle($title_string) {
207        $this->title = $title_string;
208    }
209
210    public function shrinkLeftArea() {
211                $this->shrink_left_area = true;
212        }
213
214    /**
215     * Set the class name of the <body> of the resulting page.
216     *
217     * @param string $page_name_string The page name of the resulting page.  Must have no spaces.  ex:  portal, login, userprofile, etc.)
218     *
219     * @return void
220     *
221     * @access public
222     */
223    public function setPageName($page_name_string) {
224        $this->page_name = $page_name_string;
225    }
226
227    /**
228    * Add content at the very end of the <body>.
229    *
230    * This is NOT meant to add footers or other display content, it is meant
231    * to add <script></script> tag pairs that have to be executed only once
232    * the page is loaded.
233    *
234    * @param string $script A piece of script surrounded by
235    *                       <script></script> tags.
236    *
237    * @return void
238    *
239    * @access public
240    */
241    public function addFooterScript($script) {
242        $this->footer_scripts[] = $script;
243    }
244
245    /**
246     * Set the HTML page headers
247     *
248     * @param string $headers_string HTML page headers
249     *
250     * @return void
251     *
252     * @access public
253     */
254    public function setHtmlHeader($headers_string) {
255        $this->html_headers = $headers_string;
256    }
257
258    /**
259     * Set the section to be displayed in the tool pane
260     *
261     * @param string $section Section to be displayed:
262     *                          + ADMIN for administration tool pane
263     *
264     * @return string HTML code of tool pane
265     *
266     * @access public
267     */
268    public function setToolSection($section) {
269        // Init ALL smarty SWITCH values
270        $this->smarty->assign('sectionADMIN', false);
271
272        switch ($section) {
273            case "ADMIN" :
274                // Set section of Smarty template
275                $this->smarty->assign('sectionADMIN', true);
276
277                // Get information about user
278                $_currentUser = User :: getCurrentUser();
279
280                if ($_currentUser && $_currentUser->isNobody()) {
281                    // The user has no permission to access the administrative functions
282                    $_html = _("You do not have permissions to access any administration functions.");
283                } else {
284                    // Init values
285                    $_sqlAdditionalWhere = "";
286
287                    // Init ALL smarty values
288                                        User::assignSmartyValues($this->smarty, $_currentUser);
289                    $this->smarty->assign('formAction', "");
290                    $this->smarty->assign('nodeUI', "");
291                    $this->smarty->assign('networkUI', "");
292
293                    /*
294                     * If the user is super admin OR owner of at least one node
295                     * show the node menu
296                     */
297                    if ($_currentUser && ($_currentUser->isSuperAdmin() || $_currentUser->isOwner())) {
298                        // Assign the action URL for the form
299                        $this->smarty->assign('formAction', GENERIC_OBJECT_ADMIN_ABS_HREF);
300
301                        /*
302                         * If current user is a owner the SQL query must be changed
303                         * to return his nodes only
304                         */
305                        if (!$_currentUser->isSuperAdmin()) {
306                            $_sqlAdditionalWhere = "AND node_id IN (SELECT node_id from node_stakeholders WHERE is_owner = true AND user_id='".$_currentUser->getId()."')";
307                        }
308
309                        // Provide node select control to the template
310                        $this->smarty->assign('nodeUI', Node :: getSelectNodeUI('object_id', $_sqlAdditionalWhere));
311                    }
312
313                    // If the user is network admin show the network menu
314                    if ($_currentUser && $_currentUser->isSuperAdmin()) {
315                        // Provide network select control to the template
316                        $this->smarty->assign('networkUI', Network :: getSelectNetworkUI('object_id'));
317                    }
318
319                    // Compile HTML code
320                    $_html = $this->smarty->fetch("templates/classes/MainUI_ToolSection.tpl");
321                }
322                break;
323
324            default :
325                $_html = _("Unknown section:").$section;
326                break;
327        }
328
329        $this->appendContent('left_area_middle', $_html);
330    }
331
332    /**
333     * Get the content to be displayed in the tool pane
334     *
335     * @return string HTML markup
336     *
337     * @access private
338     */
339    private function getToolContent() {
340            // Define globals
341    global $session;
342        global $AVAIL_LOCALE_ARRAY;
343
344        // Init values
345        $_html = "";
346        $_gwId = null;
347        $_gwAddress = null;
348        $_gwPort = null;
349        $_selected = "";
350        $_languageChooser = array ();
351
352        // Init ALL smarty SWITCH values
353        $this->smarty->assign('sectionSTART', false);
354        $this->smarty->assign('sectionLOGIN', false);
355
356                // Set section of Smarty template
357                $this->smarty->assign('sectionSTART', true);
358
359                // Get information about user
360                $_currentUser = User :: getCurrentUser();
361
362                                User::assignSmartyValues($this->smarty, $_currentUser);
363
364                $this->smarty->assign('logoutParameters', "");
365                $this->smarty->assign('loginParameters', "");
366                $this->smarty->assign('formAction', "");
367                $this->smarty->assign('toolContent', "");
368                $this->smarty->assign('accountInformation', "");
369                $this->smarty->assign('techSupportInformation', "");
370                $this->smarty->assign('shrinkLeftArea', $this->shrink_left_area);
371
372                // Provide Smarty with information about the network
373                                Network::assignSmartyValues($this->smarty);
374
375                /*
376                 * Provide Smarty information about the user's login/logout status
377                 */
378
379                if ($_currentUser != null) {
380                    // User is logged in
381
382                    // Detect gateway information
383                    $_gwId = $session->get(SESS_GW_ID_VAR);
384                    $_gwAddress = $session->get(SESS_GW_ADDRESS_VAR);
385                    $_gwPort = $session->get(SESS_GW_PORT_VAR);
386
387                    // If gateway information could be detected tell them Smarty
388                    if ($_gwId && $_gwAddress && $_gwPort) {
389                        $this->smarty->assign('logoutParameters', "&amp;gw_id=".$_gwId."&amp;gw_address=".$_gwAddress."&amp;gw_port=".$_gwPort);
390                    }
391                } else {
392                    // Detect gateway information
393                    $_gwId = !empty ($_REQUEST['gw_id']) ? $_REQUEST['gw_id'] : $session->get(SESS_GW_ID_VAR);
394                    $_gwAddress = !empty ($_REQUEST['gw_address']) ? $_REQUEST['gw_address'] : $session->get(SESS_GW_ADDRESS_VAR);
395                    $_gwPort = !empty ($_REQUEST['gw_port']) ? $_REQUEST['gw_port'] : $session->get(SESS_GW_PORT_VAR);
396
397                    // If gateway information could be detected tell them Smarty
398                    if (!empty ($_gwId) && !empty ($_gwAddress) && !empty ($_gwPort)) {
399                        $this->smarty->assign('loginParameters', "?gw_id=".$_gwId."&amp;gw_address=".$_gwAddress."&amp;gw_port=".$_gwPort);
400                    }
401                }
402
403                /*
404                 * Provide Smarty information for the language chooser
405                 */
406
407                // Assign the action URL for the form
408                $this->smarty->assign('formAction', $_SERVER['REQUEST_URI']);
409
410                foreach ($AVAIL_LOCALE_ARRAY as $_langIds => $_langNames) {
411                    if (Locale :: getCurrentLocale()->getId() == $_langIds) {
412                        $_selected = ' selected="selected"';
413                    } else {
414                        $_selected = "";
415                    }
416
417                    $_languageChooser[] = '<option label="'.$_langNames.'" value="'.$_langIds.'"'.$_selected.'>'.$_langNames.'</option>';
418                }
419
420                // Provide Smarty all available languages
421                $this->smarty->assign('languageChooser', $_languageChooser);
422
423                /*
424                 * Provide Smarty information for the language chooser
425                 */
426                 
427                // Provide information
428                $this->smarty->assign('accountInformation', sprintf(_("Accounts on %s are and will stay completely free."), Network :: getCurrentNetwork()->getName()));
429                $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>'));
430
431                // Compile HTML code
432                $_html = $this->smarty->fetch("templates/classes/MainUI_ToolContent.tpl");
433 
434        return $_html;
435    }
436
437    /**
438     * Display the main page
439     *
440     * @return void
441     *
442     * @access public
443     * @internal Uses a few request parameters to display debug information.
444     * If $_REQUEST['debug_request'] is present, it will print out the
445     * $_REQUEST array at the top of the page.
446     */
447    public function display() {
448        // Init values
449        $_stylesheetFile = "";
450
451        // Init ALL smarty values
452        $this->smarty->assign('htmlHeaders', "");
453        $this->smarty->assign('title', "");
454        $this->smarty->assign('stylesheetURL', "");
455        $this->smarty->assign('stylesheetParsedFile', "");
456        // $this->smarty->assign('isSuperAdmin', false);
457        // $this->smarty->assign('isOwner', false);
458        $this->smarty->assign('debugRequested', false);
459        $this->smarty->assign('debugOutput', "");
460        $this->smarty->assign('footerScripts', array ());
461
462        // Add HTML headers
463        $this->smarty->assign('htmlHeaders', $this->html_headers);
464
465        // Asign title
466        $this->smarty->assign('title', $this->title);
467
468        // Asign CSS class for body
469        $this->smarty->assign('page_name', $this->page_name);
470
471        // Asign path to CSS stylesheet
472        $this->smarty->assign('stylesheetURL', COMMON_CONTENT_URL.STYLESHEET_NAME);
473
474        /*
475         * Include stylesheet to be parsed by Smarty
476         */
477        if (is_file(NODE_CONTENT_PHP_RELATIVE_PATH.STYLESHEET_NAME)) {
478            $_stylesheetFile = NODE_CONTENT_SMARTY_PATH.STYLESHEET_NAME;
479        } else {
480            $_stylesheetFile = DEFAULT_CONTENT_SMARTY_PATH.STYLESHEET_NAME;
481        }
482
483        // Asign path to CSS stylesheet to be parsed by Smarty
484        $this->smarty->assign('stylesheetParsedFile', $_stylesheetFile);
485
486        /*
487         * Allow super admin to display debug output if requested by using
488         * $_REQUEST['debug_request']
489         */
490
491        // Get information about user
492                User::assignSmartyValues($this->smarty);
493
494                $this->appendContent('page_header', $this->customBanner());
495
496        /*
497         * Build tool pane if it has been enabled
498         */
499        if ($this->isToolSectionEnabled()) {
500            $this->appendContent('left_area_top', $this->getToolContent());
501        }
502
503        // Provide the content array to Smarty
504        $this->smarty->assign('contentArray', $this->contentArray);
505
506        // Provide footer scripts to Smarty
507        $this->smarty->assign('footerScripts', $this->footer_scripts);
508
509        // Compile HTML code and output it
510        $this->smarty->display("templates/classes/MainUI_Display.tpl");
511    }
512
513    /**
514     * Display a generic error message
515     *
516     * @param string $errmsg                  The error message to be displayed
517     * @param bool   $show_tech_support_email Defines wether to show the link of
518     *                                        the tech-support
519     *
520     * @return void
521     *
522     * @access public
523     */
524    function displayError($errmsg, $show_tech_support_email = true) {
525        // Init ALL smarty values
526        $this->smarty->assign("error", "");
527        $this->smarty->assign("show_tech_support_email", false);
528        $this->smarty->assign("tech_support_email", "");
529
530        // Define needed error content
531        $this->smarty->assign("error", $errmsg);
532
533        if ($show_tech_support_email) {
534            $this->smarty->assign("show_tech_support_email", true);
535            $this->smarty->assign("tech_support_email", Network :: getCurrentNetwork()->getTechSupportEmail());
536        }
537
538        /*
539         * Output the error message
540         */
541        $_html = $this->smarty->fetch("templates/sites/error.tpl");
542
543        $this->appendContent('page_header', $_html);
544        $this->display();
545    }
546
547        static public function redirect($redirect_url, $redirect_to_title=null, $timeout=60) {
548                if (!$redirect_to_title) {
549                        $network = Network :: getCurrentNetwork();
550                        $redirect_to_title = $network ? sprintf(_("%s Login"), $network->getName()) : _("Login");
551                }
552
553                header("Location: $redirect_url");
554                echo "<html>\n" .
555                        "<head><meta http-equiv='Refresh' content='$timeout; URL=$redirect_url'/></head>\n".
556                        "<body>\n" .
557                        "<noscript>\n" .
558                        "<span style='display:none;'>\n" .
559                        "<h1>" . $redirect_to_title . "</h1>\n" .
560                        sprintf(_("Click <a href='%s'>here</a> to continue"), $redirect_url) . "<br/>\n" .
561                        _("The transfer from secure login back to regular http may cause a warning.") . "\n" .
562                        "</span>\n" .
563                        "</noscript>\n" .
564                        "</body>\n" .
565                        "</html>\n"
566                        ;
567                exit;
568        }
569
570        public function customBanner() {
571                $custom_banner = '';
572
573                return $custom_banner;
574        }
575}
576
577/*
578 * Local variables:
579 * tab-width: 4
580 * c-basic-offset: 4
581 * c-hanging-comment-ender-p: nil
582 * End:
583 */
Note: See TracBrowser for help on using the browser.