root/trunk/wifidog-auth/wifidog/classes/Node.php @ 684

Revision 684, 29.9 KB (checked in by fproulx, 8 years ago)

2005-08-11 Francois Proulx <francois.proulx@…>

  • Refactored the Admin UI.
  • I need to write the upload form and rewrite statistics
  • 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 * This program is free software; you can redistribute it and/or    *
4 * modify it under the terms of the GNU General Public License as   *
5 * published by the Free Software Foundation; either version 2 of   *
6 * the License, or (at your option) any later version.              *
7 *                                                                  *
8 * This program is distributed in the hope that it will be useful,  *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
11 * GNU General Public License for more details.                     *
12 *                                                                  *
13 * You should have received a copy of the GNU General Public License*
14 * along with this program; if not, contact:                        *
15 *                                                                  *
16 * Free Software Foundation           Voice:  +1-617-542-5942       *
17 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       *
18 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   *
19 *                                                                  *
20 \********************************************************************/
21/**@file Node.php
22 * @author Copyright (C) 2005 Benoit Gr�goire <bock@step.polymtl.ca>
23 */
24 
25require_once BASEPATH.'include/common.php';
26require_once 'Content/ContentGroup.php';
27require_once BASEPATH.'classes/User.php';
28
29/** Abstract a Node.  A Node is an actual physical transmitter. */
30class Node implements GenericObject
31{
32        private $mRow;
33        private $id;
34        private static $current_node_id = null;
35
36        /** Instantiate a node object
37         * @param $id The id of the requested node
38         * @return a Node object, or null if there was an error
39         */
40        static function getObject($id)
41        {
42                $object = null;
43                $object = new self($id);
44                return $object;
45        }
46
47        /** Get the current node for which the portal is displayed or to which a user is physically connected.
48         * @param $real_node_only true or false.  If true, the real physical node where the user is connected is returned, and the node set by setCurrentNode is ignored.
49         * @return a Node object, or null if it can't be found.
50         */
51        static function getCurrentNode($real_node_only = false)
52        {
53                $object = null;
54                if (self :: $current_node_id != null && $real_node_only == false)
55                {
56                        $object = new self(self :: $current_node_id);
57                }
58                else
59                {
60                        $object = self::getCurrentRealNode();
61                }
62                return $object;
63        }
64
65        /** Set the current node where the user is to be considered connected to.  (For portal and content display purpuses, among other.
66         * @param $node Node.  The new current node.
67         * @return true  */
68        static function setCurrentNode(Node $node)
69        {
70                self :: $current_node_id = $node->GetId();
71                return true;
72        }
73
74        /** Get the current node to which a user is physically connected, if any.  This is done by an IP adress lookup against the last reported IP adress of the node
75         * @param        * @return a Node object, or null if it can't be found.
76         */
77        public static function getCurrentRealNode()
78        {
79                global $db;
80                $retval = null;
81                $sql = "SELECT node_id, last_heartbeat_ip from nodes WHERE last_heartbeat_ip='$_SERVER[REMOTE_ADDR]'";
82                $db->ExecSql($sql, $node_rows, false);
83                $num_match = count($node_rows);
84                if ($num_match == 0)
85                {
86
87                        // User is not physically connected to a node
88                        $retval = null;
89                }
90                else
91                        if ($num_match = 1)
92                        {
93                                // Only a single node matches, the user is presumed to be there
94                                $retval = new self($node_rows[0]['node_id']);
95                        }
96                        else
97                        {
98                                /* We have more than one node matching the IP (the nodes are behind the same NAT).
99                                 * We will try to discriminate by finding which node the user last authenticated against.
100                                 * If the IP matches, we can be pretty certain the user is there.
101                                 */
102                                $retval = null;
103                                $current_user = User :: getCurrentUser();
104                                if ($current_user != null)
105                                {
106                                        $current_user_id = $current_user->getId();
107                                        $_SERVER['REMOTE_ADDR'];
108                                        $sql = "SELECT node_id, last_heartbeat_ip from connections NATURAL JOIN nodes WHERE user_id='$current_user_id' ORDER BY last_updated DESC ";
109                                        $db->ExecSql($sql, $node_rows, false);
110                                        $node_row = $node_rows[0];
111                                        if($node_row!=null && $node_row['last_heartbeat_ip']==$_SERVER['REMOTE_ADDR'])
112                                        {
113                                                $retval = new self($node_row['node_id']);
114                                        }
115                                }
116                        }
117        return $retval;
118        }
119
120        public function delete(& $errmsg)
121        {
122                $retval = false;
123                $user = User :: getCurrentUser();
124                if ($this->isOwner($user) || $user->isSuperAdmin())
125                {
126                        $errmsg = _('Access denied!');
127                }
128               
129                global $db;
130                $id = $db->EscapeString($this->getId());
131                if (!$db->ExecSqlUpdate("DELETE FROM nodes WHERE node_id='{$id}'", false))
132                {
133                        $errmsg = _('Could not delete node!');
134                }
135                else
136                {
137                        $retval = true;
138                }
139               
140                return $retval;
141        }
142
143        /** Create a new Node in the database
144         * @deprecated version - 18-Apr-2005
145         * @param $id The id to be given to the new node
146         * @return the newly created Node object, or null if there was an error
147         */
148        static function createNewObject()
149        {
150                global $db;
151
152                $node_id = $db->EscapeString(get_guid());
153                $name = $db->EscapeString('New node');
154
155                $sql = "INSERT INTO nodes (node_id, name) VALUES ('$node_id','$name')";
156
157                if (!$db->ExecSqlUpdate($sql, false))
158                {
159                        throw new Exception(_('Unable to insert new node into database!'));
160                }
161                $object = new self($node_id);
162                return $object;
163        }
164
165        /** Create a new Node in the database
166         * @deprecated version - 18-Apr-2005
167         * @param $id The id to be given to the new node
168         * @return the newly created Node object, or null if there was an error
169         */
170        static function createNode($node_id, $name, $rss_url = "", $home_page_url = "", $description = "", $map_url = "", $street_address = "", $public_phone_number = "", $public_email = "", $mass_transit_info = "", $node_deployment_status = "IN_PLANNING")
171        {
172                global $db;
173
174                $node_id = $db->EscapeString($node_id);
175                $name = $db->EscapeString($name);
176                $rss_url = $db->EscapeString($rss_url);
177                $home_page_url = $db->EscapeString($home_page_url);
178                $description = $db->EscapeString($description);
179                $map_url = $db->EscapeString($map_url);
180                $street_address = $db->EscapeString($street_address);
181                $public_phone_number = $db->EscapeString($public_phone_number);
182                $public_email = $db->EscapeString($public_email);
183                $mass_transit_info = $db->EscapeString($mass_transit_info);
184                $node_deployment_status = $db->EscapeString($node_deployment_status);
185
186                if (Node :: nodeExists($node_id))
187                        throw new Exception(_('This node already exists.'));
188
189                $sql = "INSERT INTO nodes (node_id, name, rss_url, creation_date, home_page_url, description, map_url, street_address, public_phone_number, public_email, mass_transit_info, node_deployment_status) VALUES ('$node_id','$name','$rss_url',NOW(),'$home_page_url','$description','$map_url','$street_address','$public_phone_number','$public_email','$mass_transit_info','$node_deployment_status')";
190
191                if (!$db->ExecSqlUpdate($sql, false))
192                {
193                        throw new Exception(_('Unable to insert new node into database!'));
194                }
195                $object = new self($node_id);
196                return $object;
197        }
198
199        /** Get an interface to pick a node.
200        * @param $user_prefix A identifier provided by the programmer to recognise it's generated html form
201        * @param $sql_additional_where Addidional where conditions to restrict the candidate objects
202        * @return html markup
203        */
204        public static function getSelectNodeUI($user_prefix, $sql_additional_where = null)
205        {
206                global $db;
207                $html = '';
208                $name = "{$user_prefix}";
209                $html .= "Node: \n";
210                $sql = "SELECT node_id, name from nodes WHERE 1=1 $sql_additional_where ORDER BY node_id";
211                $db->ExecSql($sql, $node_rows, false);
212                if ($node_rows != null)
213                {
214                        $i = 0;
215                        foreach ($node_rows as $node_row)
216                        {
217                                $tab[$i][0] = $node_row['node_id'];
218                                $tab[$i][1] = $node_row['node_id'].": ".$node_row['name'];
219                                $i ++;
220                        }
221                        $html .= FormSelectGenerator :: generateFromArray($tab, null, $name, null, false);
222                }
223                return $html;
224        }
225
226        /** Get the selected Network object.
227         * @param $user_prefix A identifier provided by the programmer to recognise it's generated form
228         * @return the node object
229         */
230        static function processSelectNodeUI($user_prefix)
231        {
232                $object = null;
233                $name = "{$user_prefix}";
234                return new self($_REQUEST[$name]);
235        }
236       
237        /** Get an interface to select the deployment status
238        * @param $user_prefix A identifier provided by the programmer to recognise it's generated html form
239        * @return html markup
240        */
241        public function getSelectDeploymentStatus($user_prefix)
242        {
243                global $db;
244                $html = '';
245                $name = "{$user_prefix}";
246                $status_list = self::getAllDeploymentStatus();
247                if ($status_list != null)
248                {
249                        $tab = array();
250                        foreach ($status_list as $status)
251                                $tab[] = array($status, $status);
252                        $html .= FormSelectGenerator :: generateFromArray($tab, $this->getDeploymentStatus(), $name, null, false);
253                }
254                return $html;
255        }
256
257        /** Get the selected deployment status
258         * @param $user_prefix A identifier provided by the programmer to recognise it's generated form
259         * @return the deployment status
260         */
261        public function processSelectDeploymentStatus($user_prefix)
262        {
263                $object = null;
264                $name = "{$user_prefix}";
265                return $_REQUEST[$name];
266        }
267
268        /** @param $node_id The id of the node */
269        function __construct($node_id)
270        {
271                global $db;
272
273                $node_id_str = $db->EscapeString($node_id);
274                $sql = "SELECT * FROM nodes WHERE node_id='$node_id_str'";
275                $db->ExecSqlUniqueRes($sql, $row, false);
276                if ($row == null)
277                {
278                        throw new Exception(_("The id $node_id_str could not be found in the database"));
279                }
280                $this->mRow = $row;
281                $this->mDb = &$db;
282                $this->id = $row['node_id'];
283        }
284       
285        function getID()
286        {
287                return $this->mRow['node_id'];
288        }
289       
290        /** Return the name of the node
291         */
292        function getName()
293        {
294                return $this->mRow['name'];
295        }
296       
297        function setName($name)
298        {
299                $name = $this->mDb->EscapeString($name);
300                $this->mDb->ExecSqlUpdate("UPDATE nodes SET name = '{$name}' WHERE node_id = '{$this->getId()}'");
301                $this->refresh();
302        }
303
304        function getRssUrl()
305        {
306                return $this->mRow['rss_url'];
307        }
308       
309        function setRssUrl($url)
310        {
311                $url = $this->mDb->EscapeString($url);
312                $this->mDb->ExecSqlUpdate("UPDATE nodes SET rss_url = '{$url}' WHERE node_id = '{$this->getId()}'");
313                $this->refresh();
314        }
315
316        function getHomePageURL()
317        {
318                return $this->mRow['home_page_url'];
319        }
320       
321        function setHomePageUrl($url)
322        {
323                $url = $this->mDb->EscapeString($url);
324                $this->mDb->ExecSqlUpdate("UPDATE nodes SET home_page_url = '{$url}' WHERE node_id = '{$this->getId()}'");
325                $this->refresh();
326        }
327
328        function getDescription()
329        {
330                return $this->mRow['description'];
331        }
332       
333        function setDescription($description)
334        {
335                $description = $this->mDb->EscapeString($description);
336                $this->mDb->ExecSqlUpdate("UPDATE nodes SET description = '{$description}' WHERE node_id = '{$this->getId()}'");
337                $this->refresh();
338        }
339
340        function getMapURL()
341        {
342                return $this->mRow['map_url'];
343        }
344       
345        function setMapURL($url)
346        {
347                $url = $this->mDb->EscapeString($url);
348                $this->mDb->ExecSqlUpdate("UPDATE nodes SET map_url = '{$url}' WHERE node_id = '{$this->getId()}'");
349                $this->refresh();
350        }
351
352        function getAddress()
353        {
354                return $this->mRow['street_address'];
355        }
356       
357        function setAddress($address)
358        {
359                $address = $this->mDb->EscapeString($address);
360                $this->mDb->ExecSqlUpdate("UPDATE nodes SET street_address = '{$address}' WHERE node_id = '{$this->getId()}'");
361                $this->refresh();
362        }
363
364        function getTelephone()
365        {
366                return $this->mRow['public_phone_number'];
367        }
368       
369        function setTelephone($phone)
370        {
371                $phone = $this->mDb->EscapeString($phone);
372                $this->mDb->ExecSqlUpdate("UPDATE nodes SET public_phone_number = '{$phone}' WHERE node_id = '{$this->getId()}'");
373                $this->refresh();
374        }
375
376        function getTransitInfo()
377        {
378                return $this->mRow['mass_transit_info'];
379        }
380       
381        function setTransitInfo($transit_info)
382        {
383                $transit_info = $this->mDb->EscapeString($transit_info);
384                $this->mDb->ExecSqlUpdate("UPDATE nodes SET mass_transit_info = '{$transit_info}' WHERE node_id = '{$this->getId()}'");
385                $this->refresh();
386        }
387
388        function getEmail()
389        {
390                return $this->mRow['public_email'];
391        }
392       
393        function setEmail($email)
394        {
395                $email = $this->mDb->EscapeString($email);
396                $this->mDb->ExecSqlUpdate("UPDATE nodes SET public_email = '{$email}' WHERE node_id = '{$this->getId()}'");
397                $this->refresh();
398        }
399
400        function getDeploymentStatus()
401        {
402                return $this->mRow['node_deployment_status'];
403        }
404       
405        function setDeploymentStatus($status)
406        {
407                $status = $this->mDb->EscapeString($status);
408                $this->mDb->ExecSqlUpdate("UPDATE nodes SET node_deployment_status = '{$status}' WHERE node_id = '{$this->getId()}'");
409                $this->refresh();
410        }
411
412        /** Retreives the admin interface of this object.
413         * @return The HTML fragment for this interface */
414        public function getAdminUI()
415        {
416                //TODO: Most of this code will be moved to Hotspot class when the abtraction will be completed
417               
418                $html = '';
419                $html .= "<div class='admin_container'>\n";
420                $html .= "<div class='admin_class'>Node (".get_class($this)." instance)</div>\n";
421                $html .= "<h3>"._("Edit a hotspot")."</h3>\n";
422               
423                // Information about the node
424                $html .= "<div class='admin_section_container'>\n";
425                $html .= "<div class='admin_section_title'>"._("Information about the node:")."</div>\n";
426               
427                // Node ID
428                $html .= "<div class='admin_section_container'>\n";
429                $html .= "<div class='admin_section_title'>"._("ID")." : </div>\n";
430                $html .= "<div class='admin_section_data'>\n";
431                $name = "node_".$this->getId()."_id";
432                $value = htmlspecialchars($this->getId(), ENT_QUOTES);
433                $html .= "<input type='text' readonly='' size='10' value='$value' name='$name'>\n";
434                $html .= "</div>\n";
435                $html .= "</div>\n";
436
437                // Name
438                $html .= "<div class='admin_section_container'>\n";
439                $html .= "<div class='admin_section_title'>"._("Name")." : </div>\n";
440                $html .= "<div class='admin_section_data'>\n";
441                $name = "node_".$this->getId()."_name";
442                $value = htmlspecialchars($this->getName(), ENT_QUOTES);
443                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
444                $html .= "</div>\n";
445                $html .= "</div>\n";
446               
447                // RSS URL
448                $html .= "<div class='admin_section_container'>\n";
449                $html .= "<div class='admin_section_title'>"._("RSS URL")." : </div>\n";
450                $html .= "<div class='admin_section_data'>\n";
451                $name = "node_".$this->getId()."_rss_url";
452                $value = htmlspecialchars($this->getRSSUrl(), ENT_QUOTES);
453                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
454                $html .= "</div>\n";
455                $html .= "</div>\n";
456               
457                // Homepage URL
458                $html .= "<div class='admin_section_container'>\n";
459                $html .= "<div class='admin_section_title'>"._("Homepage URL")." : </div>\n";
460                $html .= "<div class='admin_section_data'>\n";
461                $name = "node_".$this->getId()."_homepage_url";
462                $value = htmlspecialchars($this->getHomePageURL(), ENT_QUOTES);
463                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
464                $html .= "</div>\n";
465                $html .= "</div>\n";
466               
467                // Description
468                $html .= "<div class='admin_section_container'>\n";
469                $html .= "<div class='admin_section_title'>"._("Description")." : </div>\n";
470                $html .= "<div class='admin_section_data'>\n";
471                $name = "node_".$this->getId()."_description";
472                $value = htmlspecialchars($this->getDescription(), ENT_QUOTES);
473                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
474                $html .= "</div>\n";
475                $html .= "</div>\n";
476               
477                // Map URL
478                $html .= "<div class='admin_section_container'>\n";
479                $html .= "<div class='admin_section_title'>"._("Map URL")." : </div>\n";
480                $html .= "<div class='admin_section_data'>\n";
481                $name = "node_".$this->getId()."_map_url";
482                $value = htmlspecialchars($this->getMapURL(), ENT_QUOTES);
483                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
484                $html .= "</div>\n";
485                $html .= "</div>\n";
486               
487                // Street address
488                $html .= "<div class='admin_section_container'>\n";
489                $html .= "<div class='admin_section_title'>"._("Street address")." : </div>\n";
490                $html .= "<div class='admin_section_data'>\n";
491                $name = "node_".$this->getId()."_street_address";
492                $value = htmlspecialchars($this->getAddress(), ENT_QUOTES);
493                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
494                $html .= "</div>\n";
495                $html .= "</div>\n";
496               
497                // Public phone #
498                $html .= "<div class='admin_section_container'>\n";
499                $html .= "<div class='admin_section_title'>"._("Public phone number")." : </div>\n";
500                $html .= "<div class='admin_section_data'>\n";
501                $name = "node_".$this->getId()."_public_phone";
502                $value = htmlspecialchars($this->getTelephone(), ENT_QUOTES);
503                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
504                $html .= "</div>\n";
505                $html .= "</div>\n";
506               
507                // Public mail
508                $html .= "<div class='admin_section_container'>\n";
509                $html .= "<div class='admin_section_title'>"._("Public email")." : </div>\n";
510                $html .= "<div class='admin_section_data'>\n";
511                $name = "node_".$this->getId()."_public_email";
512                $value = htmlspecialchars($this->getEmail(), ENT_QUOTES);
513                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
514                $html .= "</div>\n";
515                $html .= "</div>\n";
516               
517                // Mass transit info
518                $html .= "<div class='admin_section_container'>\n";
519                $html .= "<div class='admin_section_title'>"._("Mass transit info")." : </div>\n";
520                $html .= "<div class='admin_section_data'>\n";
521                $name = "node_".$this->getId()."_mass_transit_info";
522                $value = htmlspecialchars($this->getTransitInfo(), ENT_QUOTES);
523                $html .= "<input type='text' size ='50' value='$value' name='$name'>\n";
524                $html .= "</div>\n";
525                $html .= "</div>\n";
526               
527                // Deployment status
528                $html .= "<div class='admin_section_container'>\n";
529                $html .= "<div class='admin_section_title'>"._("Node deployment status")." : </div>\n";
530                $html .= "<div class='admin_section_data'>\n";
531                $name = "node_".$this->getId()."_deployment_status";           
532                $html .= self::getSelectDeploymentStatus($name);
533                $html .= "</div>\n";
534                $html .= "</div>\n";
535               
536                // End of information section
537                $html .= "</div>\n";
538               
539                // Owners management
540                $html .= "<div class='admin_section_container'>\n";
541                $html .= "<div class='admin_section_title'>"._("Node owners")." : </div>\n";
542                $html .= "<ul class='admin_section_list'>\n";
543                foreach ($this->getOwners() as $owner)
544                {
545                        $html .= "<li class='admin_section_list_item'>\n";
546                        $html .= "<div class='admin_section_data'>\n";
547                        $html .= "{$owner->getUsername()}";
548                        $html .= "</div>\n";
549                        $html .= "<div class='admin_section_tools'>\n";
550                        $name = "node_{$this->getId()}_owner_{$owner->GetId()}_remove";
551                        $html .= "<input type='submit' name='$name' value='"._("Remove owner")."'>";
552                        $html .= "</div>\n";
553                        $html .= "</li>\n";
554                }
555                $html .= "<li class='admin_section_list_item'>\n";
556                $name = "node_{$this->getId()}_new_owner";
557                $html .= User :: getSelectUserUI($name);
558                $name = "node_{$this->getId()}_new_owner_submit";
559                $html .= "<input type='submit' name='$name' value='"._("Add owner")."'>";
560                $html .= "</li>\n";
561                $html .= "</ul>\n";
562                $html .= "</div>\n";
563                $html .= "</div>\n";
564               
565                // Display stats
566                $html .= "<div class='admin_section_container'>\n";
567                $html .= "<div class='admin_section_title'>"._("Statistics:")."</div>\n";
568                $html .= "<div class='admin_section_data'>\n";
569                $name = "node_".$this->id."_get_stats";
570                $html .= "<input type='submit' name='$name' value='"._("Get access statistics")."'>";
571                $html .= "</div>\n";
572                $html .= "</div>\n";
573               
574                $html .= "<div class='admin_section_container'>\n";
575                $html .= "<div class='admin_section_title'>"._("Node content:")."</div>\n";
576
577                $html .= "<ul class='admin_section_list'>\n";
578                foreach ($this->getAllContent() as $content)
579                {
580                        $html .= "<li class='admin_section_list_item'>\n";
581                        $html .= "<div class='admin_section_data'>\n";
582                        $html .= $content->getListUI();
583                        $html .= "</div>\n";
584                        $html .= "<div class='admin_section_tools'>\n";
585                        $name = "node_".$this->id."_content_".$content->GetId()."_erase";
586                        $html .= "<input type='submit' name='$name' value='"._("Remove")."'>";
587                        $html .= "</div>\n";
588                        $html .= "</li>\n";
589                }
590                $html .= "<li class='admin_section_list_item'>\n";
591                $name = "node_{$this->id}_new_content";
592                $html .= Content :: getSelectContentUI($name, "AND content_id NOT IN (SELECT content_id FROM node_has_content WHERE node_id='$this->id')");
593                $name = "node_{$this->id}_new_content_submit";
594                $html .= "<input type='submit' name='$name' value='"._("Add")."'>";
595                $html .= "</li>\n";
596                $html .= "</ul>\n";
597                $html .= "</div>\n";
598                $html .= "</div>\n";
599                return $html;
600        }
601
602        /** Process admin interface of this object.
603        */
604        public function processAdminUI()
605        {
606                $user = User::getCurrentUser();
607                if (!$this->isOwner($user) && !$user->isSuperAdmin())
608                {
609                        throw new Exception(_('Access denied!'));
610                }
611               
612                // Information about the node
613               
614                // Name
615                $name = "node_".$this->getId()."_name";
616                $this->setName($_REQUEST[$name]);
617               
618                // RSS URL
619                $name = "node_".$this->getId()."_rss_url";
620                $this->setRssUrl($_REQUEST[$name]);
621                       
622                // Homepage URL
623                $name = "node_".$this->getId()."_homepage_url";
624                $this->setHomePageUrl($_REQUEST[$name]);
625               
626                // Description
627                $name = "node_".$this->getId()."_description";
628                $this->setDescription($_REQUEST[$name]);
629               
630                // Map URL
631                $name = "node_".$this->getId()."_map_url";
632                $this->setMapUrl($_REQUEST[$name]);
633               
634                // Street address
635                $name = "node_".$this->getId()."_street_address";
636                $this->setAddress($_REQUEST[$name]);
637               
638                // Public phone #
639                $name = "node_".$this->getId()."_public_phone";
640                $this->setTelephone($_REQUEST[$name]);
641               
642                // Public mail
643                $name = "node_".$this->getId()."_public_email";
644                $this->setEmail($_REQUEST[$name]);
645               
646                // Mass transit info
647                $name = "node_".$this->getId()."_mass_transit_info";
648                $this->setTransitInfo($_REQUEST[$name]);
649               
650                // Deployment status
651                $name = "node_".$this->getId()."_deployment_status";
652                $this->setDeploymentStatus(self::processSelectDeploymentStatus($name));
653
654                // Statistics
655                $name = "node_{$this->id}_get_stats";
656                if (!empty ($_REQUEST[$name]))
657                        header("Location: hotspot_log.php?node_id=".urlencode($this->getId()));
658                       
659                // Owners processing
660                // Rebuild user id, and delete if it was selected
661                foreach ($this->getOwners() as $owner)
662                {
663                        $name = "node_{$this->getId()}_owner_{$owner->GetId()}_remove";
664                        if (!empty ($_REQUEST[$name]))
665                        {
666                                if($this->isOwner($owner))
667                                        $this->removeOwner($owner);
668                                else
669                                        echo _("Invalid user!");
670                        }
671                }
672
673                $name = "node_{$this->getId()}_new_owner_submit";
674                if (!empty ($_REQUEST[$name]))
675                {
676                        $name = "node_{$this->getId()}_new_owner";
677                        $owner = User :: processSelectUserUI($name);
678            if($owner)
679            {
680                if($this->isOwner($owner))
681                        echo _("The user is already an owner of this node.");
682                else
683                        $this->addOwner($owner);
684            }
685                }
686               
687                // Content processing
688                // Rebuild content id and deleting if it was selected for deletion )
689                foreach ($this->getAllContent() as $content)
690                {
691                        $name = "node_".$this->id."_content_".$content->GetId()."_erase";
692                        if (!empty ($_REQUEST[$name]))
693                        {
694                                $this->removeContent($content);
695                        }
696                }
697
698                $name = "node_{$this->id}_new_content_submit";
699                if (!empty ($_REQUEST[$name]))
700                {
701                        $name = "node_{$this->id}_new_content";
702                        $content = Content :: processSelectContentUI($name);
703            if($content)
704                $this->addContent($content);
705                }
706        }
707   
708    // Redirect to this node's portal page
709    public function getUserUI()
710    {
711        header("Location: ".BASE_SSL_PATH."portal/?gw_id=".$this->getId());
712    }
713
714        /** Add content to this node */
715        public function addContent(Content $content)
716        {
717                global $db;
718                $content_id = $db->EscapeString($content->getId());
719                $sql = "INSERT INTO node_has_content (node_id, content_id) VALUES ('$this->id','$content_id')";
720                $db->ExecSqlUpdate($sql, false);
721        }
722
723        /** Remove content from this node */
724        public function removeContent(Content $content)
725        {
726                global $db;
727                $content_id = $db->EscapeString($content->getId());
728                $sql = "DELETE FROM node_has_content WHERE node_id='$this->id' AND content_id='$content_id'";
729                $db->ExecSqlUpdate($sql, false);
730        }
731
732        /**Get an array of all Content linked to this node
733     * @param boolean $exclude_subscribed_content
734        * @param User $subscriber The User object used to discriminate the content
735    * @return an array of Content or an empty arrray */
736    function getAllContent($exclude_subscribed_content = false, $subscriber = null)
737        {
738                global $db;
739                $retval = array ();
740        // Get all network, but exclude user subscribed content if asked
741        if ($exclude_subscribed_content == true && $subscriber)
742            $sql = "SELECT content_id FROM node_has_content WHERE node_id='$this->id' AND content_id NOT IN (SELECT content_id FROM user_has_content WHERE user_id = '{$subscriber->getId()}') ORDER BY subscribe_timestamp";
743        else
744            $sql = "SELECT content_id FROM node_has_content WHERE node_id='$this->id' ORDER BY subscribe_timestamp";
745                $db->ExecSql($sql, $content_rows, false);
746       
747                if ($content_rows != null)
748                {
749                        foreach ($content_rows as $content_row)
750                        {
751                                $retval[] = Content :: getObject($content_row['content_id']);
752                        }
753                }
754                return $retval;
755        }
756   
757    /** Get an array of all artistic and locative Content for this hotspot
758    * @return an array of Content or an empty arrray */
759    function getAllLocativeArtisticContent()
760    {
761        global $db;
762        $retval = array ();
763        $sql = "SELECT * FROM content_group JOIN content ON (content.content_id = content_group.content_group_id) JOIN node_has_content ON (node_has_content.content_id = content_group.content_group_id AND node_has_content.node_id = '{$this->getId()}') WHERE is_persistent = true AND is_artistic_content = true AND is_locative_content = true";
764        $db->ExecSql($sql, $content_rows, false);
765        if ($content_rows != null)
766        {
767            foreach ($content_rows as $content_row)
768            {
769                // Create a content group object and grab only those that have content for the current Node
770                $content_group = Content::getObject($content_row['content_group_id']);
771                if($content_group->getDisplayNumElements() >= 1)
772                {
773                        if($content_group->isDisplayableAt($this))
774                        {
775                                                // Disable logging and allow content to expand ( if possible )
776                                                $content_group->setExpandStatus(true);
777                                                $content_group->setLoggingStatus(false);
778                                                $retval[] = $content_group;
779                        }
780                }
781            }
782        }
783        return $retval;
784    }
785
786        /** Return all the nodes
787         */
788        static function getAllNodes()
789        {
790                global $db;
791
792                $db->ExecSql("SELECT * FROM nodes", $nodes, false);
793
794                if ($nodes == null)
795                        throw new Exception(_("No nodes could not be found in the database"));
796
797                return $nodes;
798        }
799
800        static function getAllNodesOrdered($order_by)
801        {
802                global $db;
803
804                $db->ExecSql("SELECT * FROM nodes ORDER BY $order_by", $nodes, false);
805
806                if ($nodes == null)
807                        throw new Exception(_("No nodes could not be found in the database"));
808
809                return $nodes;
810        }
811
812        static function getAllNodesWithStatus()
813        {
814                global $db;
815
816                $db->ExecSql("SELECT node_id, name, last_heartbeat_user_agent, (NOW()-last_heartbeat_timestamp) AS since_last_heartbeat, last_heartbeat_ip, CASE WHEN ((NOW()-last_heartbeat_timestamp) < interval '5 minutes') THEN true ELSE false END AS online, creation_date, node_deployment_status FROM nodes ORDER BY node_id", $nodes, false);
817
818                if ($nodes == null)
819                        throw new Exception(_("No nodes could not be found in the database"));
820
821                return $nodes;
822        }
823
824        static function getAllDeploymentStatus()
825        {
826                global $db;
827
828                $db->ExecSql("SELECT * FROM node_deployment_status", $statuses, false);
829                if ($statuses == null)
830                        throw new Exception(_("No deployment statues  could be found in the database"));
831
832                $statuses_array = array ();
833                foreach ($statuses as $status)
834                        array_push($statuses_array, $status['node_deployment_status']);
835
836                return $statuses_array;
837        }
838
839    /** The list of users online at this node
840     * @return An array of User object, or en empty array */
841        function getOnlineUsers()
842        {
843                global $db;
844                $retval=array();
845                $db->ExecSql("SELECT users.user_id FROM users,connections WHERE connections.token_status='".TOKEN_INUSE."' AND users.user_id=connections.user_id AND connections.node_id='{$this->id}'", $users, false);
846                if($users != null)
847                {
848                        foreach ($users as $user_row)
849                        {
850                                $retval[] = User::getObject($user_row['user_id']);
851                        }
852                }
853                return $retval;
854        }
855
856        function getOwners()
857        {               
858                global $db;
859                $retval = array();
860                $db->ExecSql("SELECT user_id FROM node_owners WHERE node_id='{$this->id}'", $owners, false);
861                if($owners != null)
862                {
863                        foreach ($owners as $owner_row)
864                        {
865                                $retval[] = User::getObject($owner_row['user_id']);
866                        }
867                }
868                return $retval;
869        }
870
871        function addOwner($user)
872        {
873                global $db;
874                if (!$db->ExecSqlUpdate("INSERT INTO node_owners (node_id, user_id) VALUES ('{$this->getId()}','{$user->getId()}')", false))
875                        throw new Exception(_('Could not add owner'));
876        }
877
878        function removeOwner($user)
879        {
880                global $db;
881                if (!$db->ExecSqlUpdate("DELETE FROM node_owners WHERE node_id='{$this->getId()}' AND user_id='{$user->getId()}'", false))
882                        throw new Exception(_('Could not remove owner'));
883        }
884
885        /** Is the user an owner of the Node?
886         * @return true our false*/
887        function isOwner(User $user)
888        {
889                global $db;
890                if ($user != null)
891                {
892                        $user_id = $user->getId();
893                        $retval = false;
894                        $db->ExecSqlUniqueRes("SELECT * FROM node_owners WHERE node_id='{$this->id}' AND user_id='{$user_id}'", $row, false);
895                        if ($row != null)
896                        {
897                                $retval = true;
898                        }
899                }
900                return $retval;
901        }
902
903        function nodeExists($id)
904        {
905                global $db;
906                $id_str = $db->EscapeString($id);
907                $sql = "SELECT * FROM nodes WHERE node_id='{$id_str}'";
908                $db->ExecSqlUniqueRes($sql, $row, false);
909                return $row;
910        }
911
912    /** Warning, the semantics of this function will change *
913     * @deprecated version - 2005-04-29 USE getOnlineUsers instead
914     * */
915        public static function getAllOnlineUsers()
916        {
917                global $db;
918                $db->ExecSql("SELECT * FROM connections,users,nodes WHERE token_status='".TOKEN_INUSE."' AND users.user_id=connections.user_id AND nodes.node_id=connections.node_id ORDER BY timestamp_in DESC", $online_users);
919                return $online_users;
920        }
921       
922        /** Reloads the object from the database.  Should normally be called after a set operation */
923        protected function refresh()
924        {
925                $this->__construct($this->id);
926        }
927
928} // End class
929?>
Note: See TracBrowser for help on using the browser.