root/trunk/wifidog-auth/wifidog/classes/Server.php @ 1273

Revision 1273, 11.2 KB (checked in by benoitg, 6 years ago)
  • DependenciesList::hook_menu(): Fix menu path conflict with Server::hook_menu();
  • Server::getAdminUI(): Add output to help debug timezone problems.
Line 
1<?php
2
3/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5// +-------------------------------------------------------------------+
6// | WiFiDog Authentication Server                                     |
7// | =============================                                     |
8// |                                                                   |
9// | The WiFiDog Authentication Server is part of the WiFiDog captive  |
10// | portal suite.                                                     |
11// +-------------------------------------------------------------------+
12// | PHP version 5 required.                                           |
13// +-------------------------------------------------------------------+
14// | Homepage:     http://www.wifidog.org/                             |
15// | Source Forge: http://sourceforge.net/projects/wifidog/            |
16// +-------------------------------------------------------------------+
17// | This program is free software; you can redistribute it and/or     |
18// | modify it under the terms of the GNU General Public License as    |
19// | published by the Free Software Foundation; either version 2 of    |
20// | the License, or (at your option) any later version.               |
21// |                                                                   |
22// | This program is distributed in the hope that it will be useful,   |
23// | but WITHOUT ANY WARRANTY; without even the implied warranty of    |
24// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the     |
25// | GNU General Public License for more details.                      |
26// |                                                                   |
27// | You should have received a copy of the GNU General Public License |
28// | along with this program; if not, contact:                         |
29// |                                                                   |
30// | Free Software Foundation           Voice:  +1-617-542-5942        |
31// | 59 Temple Place - Suite 330        Fax:    +1-617-542-2652        |
32// | Boston, MA  02111-1307,  USA       gnu@gnu.org                    |
33// |                                                                   |
34// +-------------------------------------------------------------------+
35
36/**
37 * @package    WiFiDogAuthServer
38 * @author     Max Horvath <max.horvath@maxspot.de>
39 * @author     Benoit Grégoire <bock@step.polymtl.ca>
40 * @copyright  2007 Benoit Grégoire, Technologies Coeus inc.
41 * @version    Subversion $Id: $
42 * @link       http://www.wifidog.org/
43 */
44
45/**
46 * Load required classes
47 */
48require_once('classes/GenericObject.php');
49require_once('classes/Security.php');
50define('SERVER_ID', 'SERVER_ID');
51/**
52 * Administration interface for configuring Server wide settings for the server WiFiDog is running on
53 *
54 * @package    WiFiDogAuthServer
55 * @author     Benoit Grégoire <bock@step.polymtl.ca>
56 * @copyright  2007 Benoit Grégoire, Technologies Coeus inc.
57 */
58class Server extends GenericDataObject
59{
60    /** Object cache for the object factory (getObject())*/
61    private static $instanceArray = array();
62
63    /**
64     * Constructor
65     *
66     * @param string $p_server_id Id of the server
67
68     */
69    private function __construct($p_server_id)
70    {
71         
72        $db = AbstractDb::getObject();
73
74        // Init values
75        $row = null;
76
77        $serverId = $db->escapeString($p_server_id);
78        $sql = "SELECT * FROM server WHERE server_id='$serverId'";
79        $db->execSqlUniqueRes($sql, $row, false);
80
81        if ($row == null) {
82            throw new Exception("The server with id $serverId could not be found in the database!");
83        }
84
85        $this->_row = $row;
86
87        $this->_id = $db->escapeString($row['server_id']);
88    }
89
90    public function __toString() {
91        return _("Main server object");
92    }
93    /** Get an instance of the object
94     *
95     * @param string $id The object id
96     *
97     * @return object The object, or null if there was an error (an exception
98     *                is also thrown)
99     *
100     * @see GenericObject
101
102     */
103    public static function &getObject($id)
104    {
105        if(!isset(self::$instanceArray[$id]))
106        {
107            self::$instanceArray[$id] = new self($id);
108        }
109        return self::$instanceArray[$id];
110    }
111
112    /**
113     * Get the server object (there is only one)
114     *
115     * @return object A VirtualHost object, NEVER returns null
116     *
117     * @static
118     * @access public
119     */
120    public static function &getServer()
121    {
122        return self::getObject(SERVER_ID);
123    }
124    /**
125     * Create a new Content object in the database
126     *
127     * @param string $server_id The server id of the new server. If absent,
128     *                           will be assigned a guid.
129     *
130     * @return mixed The newly created object, or null if there was an error
131     *
132     * @see GenericObject
133     *
134     * @static
135     * @access public
136     */
137    public static function createNewObject($server_id = null)
138    {
139         
140        $db = AbstractDb::getObject();
141
142        if (empty($server_id)) {
143            $server_id = get_guid();
144        }
145
146        $server_id = $db->escapeString($server_id);
147
148        $sql = "INSERT INTO servers (server_id) VALUES ('$server_id')";
149
150        if (!$db->execSqlUpdate($sql, false)) {
151            throw new Exception(_('Unable to insert the new server in the database!'));
152        }
153
154        $_object = self::getObject($server_id);
155
156        return $_object;
157
158    }
159
160    /** Get an interface to create a new object.
161     * @return html markup
162     */
163    public static function getCreateNewObjectUI()
164    {
165        throw new Exception ("Unsupported method");
166    }
167
168    /** Process the new object interface.
169     *  Will       return the new object if the user has the credentials
170     * necessary (Else an exception is thrown) and and the form was fully
171     * filled (Else the object returns null).
172     * @return the node object or null if no new node was created.
173     */
174    static function processCreateNewObjectUI()
175    {
176        throw new Exception ("Unsupported method");
177    }
178
179
180
181    /**
182     * Set as the default server
183     *
184     * @param VirtualHost $vhost
185     *
186     * @return bool True on success, false on failure
187     */
188    public function setDefaultVirtualHost(VirtualHost $vhost)
189    {
190         
191        $db = AbstractDb::getObject();
192        $vhostIdStr = $db->escapeString($vhost->getId());
193        // Init values
194        $_retVal = false;
195
196        if ($vhostIdStr != VirtualHost::getDefaultVirtualHost()->getId()) {
197            $sql  = "UPDATE server SET default_virtual_host = '$vhostIdStr';\n";
198            $_retVal = $db->execSqlUpdate($sql, false);
199            $this->refresh();
200        }
201
202        return $_retVal;
203    }
204    /**
205     * Retrieves the servers's creation date
206     *
207     * @return string Creation date of server
208     *
209     * @access public
210     */
211    public function getCreationDate()
212    {
213        return $this->_row['creation_date'];
214    }
215
216    /**
217     * Retreives the admin interface of this object
218     *
219     * @return string The HTML fragment for this interface
220     */
221    public function getAdminUI()
222    {
223        Security::requirePermission(Permission::P('SERVER_PERM_EDIT_SERVER_CONFIG'), $this);
224        // Init values
225        $db = AbstractDb::getObject();
226        $html = '';
227
228        $html .= "<fieldset class='admin_container ".get_class($this)."'>\n";
229        $html .= "<legend>"._("Server management")."</legend>\n";
230        $html .= "<ul class='admin_element_list'>\n";
231        // server_id
232        /*$value = htmlspecialchars($this->getId(), ENT_QUOTES);
233
234        $html .= "<li class='admin_element_item_container'>\n";
235        $html .= "<div class='admin_element_label'>" . _("Server ID") . ":</div>\n";
236        $html .= "<div class='admin_element_data'>\n";
237        $html .= $value;
238        $html .= "</div>\n";
239        $html .= "</li>\n";*/
240
241        // creation_date
242        $name = "server_" . $this->getId() . "_creation_date";
243        $value = htmlspecialchars($this->getCreationDate(), ENT_QUOTES);
244
245        $html .= "<li class='admin_element_item_container'>\n";
246        $html .= "<div class='admin_element_label'>" . _("Creation date") . ":</div>\n";
247        $html .= "<div class='admin_element_data'>\n";
248        $html .= $value."\n";
249        $html .= "</div>\n";
250        $html .= "</li>\n";
251
252        //timezone check
253
254        $html .= "<li class='admin_element_item_container'>\n";
255        $html .= "<div class='admin_element_label'>" . _("Timezone check:  The following must be in the same timezone") . ":</div>\n";
256        $html .= "<div class='admin_element_data'>\n";
257        $html .= "<p>";
258        $db->execSqlUniqueRes("SHOW timezone", $row, false);
259        $html .= " ".sprintf(_("Timezone from postgresql: %s"), $row['TimeZone'])."</p>"; // Version < 5.0.0
260        $date_default_timezone_get = 'date_default_timezone_get';
261        is_callable($date_default_timezone_get)?$phpTimezone = date_default_timezone_get():$phpTimezone = "Requires PHP 5.1 to tell";
262        $html .= " ".sprintf(_("Timezone from PHP: %s"), $phpTimezone)."</p>"; // Version < 5.0.0
263         
264        $html .= "</div>\n";
265        $html .= "</li>\n";
266
267
268
269        /*
270         * Access rights
271         */
272        if (true) {
273            require_once('classes/Stakeholder.php');
274            $html_access_rights = Stakeholder::getAssignStakeholdersUI($this);
275            $html .= InterfaceElements::generateAdminSectionContainer("access_rights", _("Access rights"), $html_access_rights);
276        }
277
278        $html .= "</ul>\n";
279        $html .= "</fieldset>\n";
280        return $html;
281    }
282
283    /**
284     * Process admin interface of this object
285     *
286     * @return void
287     */
288    public function processAdminUI()
289    {
290        require_once('classes/User.php');
291        Security::requirePermission(Permission::P('SERVER_PERM_EDIT_SERVER_CONFIG'), $this);
292        // Access rights
293        require_once('classes/Stakeholder.php');
294        Stakeholder::processAssignStakeholdersUI($this, $errMsg);
295        if(!empty($errMsg)) {
296            echo $errMsg;
297        }
298    }
299
300    /**
301     * Delete this Object form the it's storage mechanism
302     *
303     * @param string &$errmsg Returns an explanation of the error on failure
304     *
305     * @return bool True on success, false on failure or access denied
306     */
307    public function delete(&$errmsg)
308    {
309        $errmsg = _('The server can never be deleted!');
310        return false;
311    }
312    /** Menu hook function */
313    static public function hookMenu() {
314        $items = array();
315        $server = self::getServer();
316        if(Security::hasPermission(Permission::P('SERVER_PERM_EDIT_SERVER_CONFIG'), $server))
317        {
318            $items[] = array('path' => 'server/admin',
319            'title' => _("Server access control"),
320            'url' => BASE_URL_PATH."admin/generic_object_admin.php?object_class=Server&action=edit&object_id=".SERVER_ID.""
321                );
322        }
323        $items[] = array('path' => 'server',
324        'title' => _('Server administration'),
325        'type' => MENU_ITEM_GROUPING);
326
327        return $items;
328    }
329    /**
330     * Reloads the object from the database
331     *
332     * Should normally be called after a set operation
333     *
334     * @return void     */
335    protected function refresh()
336    {
337        $this->__construct($this->_id);
338    }
339
340}
341
342/*
343 * Local variables:
344 * tab-width: 4
345 * c-basic-offset: 4
346 * c-hanging-comment-ender-p: nil
347 * End:
348 */
Note: See TracBrowser for help on using the browser.