Show
Ignore:
Timestamp:
04/19/05 09:18:11 (8 years ago)
Author:
benoitg
Message:

2005-04-18 Benoit Gr�goire <bock@…>

  • Hotspot and network content association, continue access control work.
  • hotspot_owner.php: Fix wrong assignement of user_id that prevented the script from working.
  • All files: Remove whitespace and carriage return after the ?> closing tags.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog-auth/wifidog/classes/Node.php

    r545 r553  
    11<?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   require_once BASEPATH.'include/common.php'; 
     2 
     3/********************************************************************\ 
     4 * This program is free software; you can redistribute it and/or    * 
     5 * modify it under the terms of the GNU General Public License as   * 
     6 * published by the Free Software Foundation; either version 2 of   * 
     7 * the License, or (at your option) any later version.              * 
     8 *                                                                  * 
     9 * This program is distributed in the hope that it will be useful,  * 
     10 * but WITHOUT ANY WARRANTY; without even the implied warranty of   * 
     11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    * 
     12 * GNU General Public License for more details.                     * 
     13 *                                                                  * 
     14 * You should have received a copy of the GNU General Public License* 
     15 * along with this program; if not, contact:                        * 
     16 *                                                                  * 
     17 * Free Software Foundation           Voice:  +1-617-542-5942       * 
     18 * 59 Temple Place - Suite 330        Fax:    +1-617-542-2652       * 
     19 * Boston, MA  02111-1307,  USA       gnu@gnu.org                   * 
     20 *                                                                  * 
     21 \********************************************************************/ 
     22/**@file Node.php 
     23 * @author Copyright (C) 2005 Benoit Gr�goire <bock@step.polymtl.ca> 
     24 */ 
     25require_once BASEPATH.'include/common.php'; 
    2526 
    2627/** Abstract a Node.  A Node is an actual physical transmitter. */ 
    27 class Node 
     28class Node implements GenericObject 
    2829{ 
    29   private $mRow; 
    30   private $mId; 
    31  
    32   /** Instantiate a node object  
    33    * @param $id The id of the requested node 
    34    * @return a Node object, or null if there was an error 
    35    */ 
    36   static function getNode ($id) 
    37   { 
    38     $object = null; 
    39     $object = new self ($id); 
    40     return $object; 
    41   } 
    42  
    43   static function deleteNode($node_id) { 
    44     global $db; 
    45  
    46     if (!$db->ExecSqlUpdate("DELETE FROM node_owners WHERE node_id='{$node_id}'", false)) 
    47         throw new Exception(_('Could not delete node owners!')); 
    48  
    49     if (!$db->ExecSqlUpdate("DELETE FROM nodes WHERE node_id='{$node_id}'", false)) 
    50         throw new Exception(_('Could not delete node!')); 
    51   } 
    52  
    53   /** Create a new Node in the database  
    54    * @param $id The id to be given to the new node 
    55    * @return the newly created Node object, or null if there was an error 
    56    */ 
    57   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) 
    58   { 
    59     global $db; 
    60  
    61     $node_id = $db->EscapeString ($node_id); 
    62     $name = $db->EscapeString ($name); 
    63     $rss_url = $db->EscapeString ($rss_url); 
    64     $home_page_url = $db->EscapeString ($home_page_url); 
    65     $description = $db->EscapeString ($description); 
    66     $map_url = $db->EscapeString ($map_url); 
    67     $street_address = $db->EscapeString ($street_address); 
    68     $public_phone_number = $db->EscapeString ($public_phone_number); 
    69     $public_email = $db->EscapeString ($public_email); 
    70     $mass_transit_info = $db->EscapeString ($mass_transit_info); 
    71     $node_deployment_status = $db->EscapeString ($node_deployment_status); 
    72  
    73     if (Node::nodeExists($node_id)) 
    74         throw new Exception(_('This node already exists.')); 
    75  
    76     $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')"; 
    77  
    78     if (!$db->ExecSqlUpdate ($sql, false)) { 
    79         throw new Exception(_('Unable to insert new node into database!')); 
    80     } 
    81     $object = new self ($node_id); 
    82     return $object; 
    83   } 
    84  
    85   /** @param $node_id The id of the node */ 
    86   function __construct ($node_id) 
    87   { 
    88     global $db; 
    89  
    90     $node_id_str = $db->EscapeString ($node_id); 
    91     $sql = "SELECT * FROM nodes WHERE node_id='$node_id_str'"; 
    92     $db->ExecSqlUniqueRes ($sql, $row, false); 
    93     if ($row == null) 
    94       { 
    95         throw new 
    96           Exception (_ 
    97                      ("The id $node_id_str could not be found in the database")); 
    98       } 
    99     $this->mRow = $row; 
    100     $this->mId = $row['node_id']; 
    101   }      
    102  
    103   /** Return the name of the node  
    104    */ 
    105   function getName () 
    106   { 
    107     return $this->mRow['name']; 
    108   } 
    109  
    110   function getID () 
    111   { 
    112     return $this->mRow['node_id']; 
    113   } 
    114  
    115   function getRSSURL () 
    116   { 
    117     return $this->mRow['rss_url']; 
    118   } 
    119  
    120   function getHomePageURL () 
    121   { 
    122     return $this->mRow['home_page_url']; 
    123   } 
    124  
    125   function getDescription() 
    126   { 
    127     return $this->mRow['description']; 
    128   } 
    129  
    130   function getMapURL() 
    131   { 
    132     return $this->mRow['map_url']; 
    133   } 
    134  
    135   function getAddress() 
    136   { 
    137     return $this->mRow['street_address']; 
    138   } 
    139  
    140   function getTelephone () 
    141   { 
    142     return $this->mRow['public_phone_number']; 
    143   } 
    144  
    145   function getTransitInfo () 
    146   { 
    147     return $this->mRow['mass_transit_info']; 
    148   } 
    149  
    150   function getEmail () 
    151   { 
    152     return $this->mRow['public_email']; 
    153   } 
    154  
    155   function getDeploymentStatus () 
    156   { 
    157     return $this->mRow['node_deployment_status']; 
    158   } 
    159  
    160   function setInfos($info_array) 
    161   { 
    162     global $db; 
    163  
    164     $infos_to_add = array(); 
    165     if ($info_array) { 
    166       foreach ($info_array as $column => $value) { 
    167         $value = $db->EscapeString($value); 
    168         array_push($infos_to_add, "$column='$value'"); 
    169       } 
    170       $sql = "UPDATE nodes SET "; 
    171       $sql .= implode(",", $infos_to_add); 
    172       $sql .= " WHERE node_id='{$this->mId}'"; 
    173       if (!$db->ExecSqlUpdate($sql, false)) { 
    174         throw new Exception(_('Unable to update database!')); 
    175       } 
    176     } else { 
    177       throw new Exception(_('No info to update node with!')); 
    178     } 
    179   } 
    180  
    181   /** Return all the nodes 
    182    */ 
    183   static function getAllNodes () 
    184   { 
    185     global $db; 
    186  
    187     $db->ExecSql ("SELECT * FROM nodes", $nodes, false); 
    188  
    189     if ($nodes == null) 
    190       throw new Exception(_("No nodes could not be found in the database")); 
    191  
    192     return $nodes; 
    193   } 
    194  
    195   static function getAllNodesOrdered ($order_by) 
    196   { 
    197     global $db; 
    198  
    199     $db->ExecSql ("SELECT * FROM nodes ORDER BY $order_by", $nodes, false); 
    200  
    201     if ($nodes == null) 
    202       throw new Exception(_("No nodes could not be found in the database")); 
    203  
    204     return $nodes; 
    205   } 
    206  
    207   static function getAllNodesWithStatus () 
    208   { 
    209     global $db; 
    210  
    211     $db-> 
    212       ExecSql 
    213       ("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 FROM nodes ORDER BY node_id", 
    214        $nodes, false); 
    215  
    216     if ($nodes == null) 
    217       throw new Exception(_("No nodes could not be found in the database")); 
    218  
    219     return $nodes; 
    220   } 
    221  
    222   static function getAllDeploymentStatus () 
    223   { 
    224     global $db; 
    225  
    226     $db->ExecSql ("SELECT * FROM node_deployment_status", $statuses, false); 
    227     if ($statuses == null) 
    228       throw new Exception(_("No deployment statues  could be found in the database")); 
    229  
    230     $statuses_array = array (); 
    231     foreach ($statuses as $status) 
    232       array_push ($statuses_array, $status['node_deployment_status']); 
    233  
    234     return $statuses_array; 
    235   } 
    236  
    237   function getOnlineUsers () 
    238   { 
    239     global $db; 
    240  
    241     $db->ExecSql("SELECT users.user_id, users.username, users.account_origin FROM users,connections WHERE connections.token_status='". TOKEN_INUSE. "' AND users.user_id=connections.user_id AND connections.node_id='{$this->mId}'", $users, false); 
    242     return $users; 
    243   } 
    244  
    245   function getOwners() { 
    246     global $db; 
    247  
    248     $db->ExecSql("SELECT user_id FROM node_owners WHERE node_id='{$this->mId}'", $owners, false); 
    249     return $owners; 
    250   } 
    251  
    252   function addOwner($user_id) { 
    253     /* TODO: VALIDER les champs de donnees node_id et user_id */ 
    254  
    255     global $db; 
    256     if (!$db->ExecSqlUpdate("INSERT INTO node_owners (node_id, user_id) VALUES ('{$this->mId}','{$user_id}')", false)) 
    257         throw new Exception(_('Could not add owner')); 
    258   } 
    259  
    260   function removeOwner($user_id) { 
    261     global $db; 
    262     if (!$db->ExecSqlUpdate("DELETE FROM node_owners WHERE node_id='{$this->mId}' AND user_id='{$user_id}'", false)) 
    263         throw new Exception(_('Could not remove owner')); 
    264   } 
    265  
    266   function nodeExists($id) { 
    267     global $db; 
    268     $id_str = $db->EscapeString($id); 
    269     $sql = "SELECT * FROM nodes WHERE node_id='{$id_str}'"; 
    270     $db->ExecSqlUniqueRes($sql, $row, false); 
    271     return $row; 
    272   } 
    273  
    274   function getInfoArray() { 
    275     return $this->mRow; 
    276   } 
    277  
    278   public static function getAllOnlineUsers() { 
    279     global $db; 
    280     $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); 
    281     return $online_users; 
    282   } 
    283  
    284 }                               // End class 
    285  
     30        private $mRow; 
     31        private $id; 
     32 
     33        /** Instantiate a node object  
     34         * @param $id The id of the requested node 
     35         * @return a Node object, or null if there was an error 
     36         */ 
     37        static function getObject($id) 
     38        { 
     39                $object = null; 
     40                $object = new self($id); 
     41                return $object; 
     42        } 
     43 
     44        public function delete(& $errmsg) 
     45        { 
     46                $retval=false; 
     47                $user = User::getCurrentUser(); 
     48                if($this->isOwner($user)||$user->isSuperAdmin()) 
     49                { 
     50                        $errmsg=_('Access denied!'); 
     51                } 
     52                global $db; 
     53 
     54                if (!$db->ExecSqlUpdate("DELETE FROM nodes WHERE node_id='{$this->$id}'", false)) 
     55                { 
     56                        $errmsg=_('Could not delete node!');} 
     57                        else 
     58                        { 
     59                                $retval=true; 
     60                        } 
     61return $retval; 
     62        } 
     63 
     64        /** Create a new Node in the database 
     65         * @deprecated version - 18-Apr-2005 
     66         * @param $id The id to be given to the new node 
     67         * @return the newly created Node object, or null if there was an error 
     68         */ 
     69        static function createNewObject() 
     70        { 
     71                global $db; 
     72 
     73                $node_id = $db->EscapeString(get_guid()); 
     74                $name = $db->EscapeString('New node'); 
     75 
     76                $sql = "INSERT INTO nodes (node_id, name) VALUES ('$node_id','$name')"; 
     77 
     78                if (!$db->ExecSqlUpdate($sql, false)) 
     79                { 
     80                        throw new Exception(_('Unable to insert new node into database!')); 
     81                } 
     82                $object = new self($node_id); 
     83                return $object; 
     84        } 
     85 
     86        /** Create a new Node in the database  
     87         * @deprecated version - 18-Apr-2005 
     88         * @param $id The id to be given to the new node 
     89         * @return the newly created Node object, or null if there was an error 
     90         */ 
     91        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) 
     92        { 
     93                global $db; 
     94 
     95                $node_id = $db->EscapeString($node_id); 
     96                $name = $db->EscapeString($name); 
     97                $rss_url = $db->EscapeString($rss_url); 
     98                $home_page_url = $db->EscapeString($home_page_url); 
     99                $description = $db->EscapeString($description); 
     100                $map_url = $db->EscapeString($map_url); 
     101                $street_address = $db->EscapeString($street_address); 
     102                $public_phone_number = $db->EscapeString($public_phone_number); 
     103                $public_email = $db->EscapeString($public_email); 
     104                $mass_transit_info = $db->EscapeString($mass_transit_info); 
     105                $node_deployment_status = $db->EscapeString($node_deployment_status); 
     106 
     107                if (Node :: nodeExists($node_id)) 
     108                        throw new Exception(_('This node already exists.')); 
     109 
     110                $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')"; 
     111 
     112                if (!$db->ExecSqlUpdate($sql, false)) 
     113                { 
     114                        throw new Exception(_('Unable to insert new node into database!')); 
     115                } 
     116                $object = new self($node_id); 
     117                return $object; 
     118        } 
     119 
     120        /** Get an interface to pick a node. 
     121        * @param $user_prefix A identifier provided by the programmer to recognise it's generated html form 
     122        * @param $sql_additional_where Addidional where conditions to restrict the candidate objects 
     123        * @return html markup 
     124        */ 
     125        public static function getSelectNodeUI($user_prefix, $sql_additional_where=null) 
     126        { 
     127                global $db; 
     128                $html = ''; 
     129                $name = "{$user_prefix}"; 
     130                $html .= "Node: \n"; 
     131                $sql = "SELECT node_id, name from nodes WHERE 1=1 $sql_additional_where ORDER BY node_id"; 
     132                $db->ExecSql($sql, $node_rows, false); 
     133                if ($node_rows != null) 
     134                { 
     135                        $i = 0; 
     136                        foreach ($node_rows as $node_row) 
     137                        { 
     138                                $tab[$i][0] = $node_row['node_id']; 
     139                                $tab[$i][1] = $node_row['node_id'].": ".$node_row['name']; 
     140                                $i ++; 
     141                        } 
     142                        $html .= FormSelectGenerator :: generateFromArray($tab, null, $name, null, false); 
     143                } 
     144                return $html; 
     145        } 
     146 
     147        /** Get the selected Network object. 
     148         * @param $user_prefix A identifier provided by the programmer to recognise it's generated form 
     149         * @return the Network object 
     150         */ 
     151        static function processSelectNodeUI($user_prefix) 
     152        { 
     153                $object = null; 
     154                $name = "{$user_prefix}"; 
     155                return new self($_REQUEST[$name]); 
     156        } 
     157 
     158        /** @param $node_id The id of the node */ 
     159        function __construct($node_id) 
     160        { 
     161                global $db; 
     162 
     163                $node_id_str = $db->EscapeString($node_id); 
     164                $sql = "SELECT * FROM nodes WHERE node_id='$node_id_str'"; 
     165                $db->ExecSqlUniqueRes($sql, $row, false); 
     166                if ($row == null) 
     167                { 
     168                        throw new Exception(_("The id $node_id_str could not be found in the database")); 
     169                } 
     170                $this->mRow = $row; 
     171                $this->id = $row['node_id']; 
     172        } 
     173 
     174        /** Return the name of the node  
     175         */ 
     176        function getName() 
     177        { 
     178                return $this->mRow['name']; 
     179        } 
     180 
     181        function getID() 
     182        { 
     183                return $this->mRow['node_id']; 
     184        } 
     185 
     186        function getRSSURL() 
     187        { 
     188                return $this->mRow['rss_url']; 
     189        } 
     190 
     191        function getHomePageURL() 
     192        { 
     193                return $this->mRow['home_page_url']; 
     194        } 
     195 
     196        function getDescription() 
     197        { 
     198                return $this->mRow['description']; 
     199        } 
     200 
     201        function getMapURL() 
     202        { 
     203                return $this->mRow['map_url']; 
     204        } 
     205 
     206        function getAddress() 
     207        { 
     208                return $this->mRow['street_address']; 
     209        } 
     210 
     211        function getTelephone() 
     212        { 
     213                return $this->mRow['public_phone_number']; 
     214        } 
     215 
     216        function getTransitInfo() 
     217        { 
     218                return $this->mRow['mass_transit_info']; 
     219        } 
     220 
     221        function getEmail() 
     222        { 
     223                return $this->mRow['public_email']; 
     224        } 
     225 
     226        function getDeploymentStatus() 
     227        { 
     228                return $this->mRow['node_deployment_status']; 
     229        } 
     230 
     231        function setInfos($info_array) 
     232        { 
     233                global $db; 
     234 
     235                $infos_to_add = array (); 
     236                if ($info_array) 
     237                { 
     238                        foreach ($info_array as $column => $value) 
     239                        { 
     240                                $value = $db->EscapeString($value); 
     241                                array_push($infos_to_add, "$column='$value'"); 
     242                        } 
     243                        $sql = "UPDATE nodes SET "; 
     244                        $sql .= implode(",", $infos_to_add); 
     245                        $sql .= " WHERE node_id='{$this->id}'"; 
     246                        if (!$db->ExecSqlUpdate($sql, false)) 
     247                        { 
     248                                throw new Exception(_('Unable to update database!')); 
     249                        } 
     250                } 
     251                else 
     252                { 
     253                        throw new Exception(_('No info to update node with!')); 
     254                } 
     255        } 
     256 
     257        /** Retreives the admin interface of this object. 
     258         * @return The HTML fragment for this interface */ 
     259        public function getAdminUI() 
     260        { 
     261                $html = ''; 
     262                $html .= "<div class='admin_container'>\n"; 
     263                $html .= "<div class='admin_class'>Node (".get_class($this)." instance)</div>\n"; 
     264 
     265                $html .= "<div class='admin_section_container'>\n"; 
     266                $html .= "<div class='admin_section_title'>"._("Node content:")."</div>\n"; 
     267 
     268                $html .= "<ul class='admin_section_list'>\n"; 
     269                foreach ($this->getAllContent() as $content) 
     270                { 
     271                        $html .= "<li class='admin_section_list_item'>\n"; 
     272                        $html .= "<div class='admin_section_data'>\n"; 
     273                        $html .= $content->getListUI(); 
     274                        $html .= "</div'>\n"; 
     275                        $html .= "<div class='admin_section_tools'>\n"; 
     276                        $name = "node_".$this->id."_content_".$content->GetId()."_erase"; 
     277                        $html .= "<input type='submit' name='$name' value='"._("Remove")."' onclick='submit();'>"; 
     278                        $html .= "</div>\n"; 
     279                        $html .= "</li>\n"; 
     280                } 
     281                $html .= "<li class='admin_section_list_item'>\n"; 
     282                $name = "node_{$this->id}_new_content"; 
     283                $html .= Content :: getSelectContentUI($name, "AND content_id NOT IN (SELECT content_id FROM node_has_content WHERE node_id='$this->id')"); 
     284                $name = "node_{$this->id}_new_content_submit"; 
     285                $html .= "<input type='submit' name='$name' value='"._("Add")."' onclick='submit();'>"; 
     286                $html .= "</li>\n"; 
     287                $html .= "</ul>\n"; 
     288                $html .= "</div>\n"; 
     289                $html .= "</div>\n"; 
     290                return $html; 
     291        } 
     292         
     293        /** Process admin interface of this object. 
     294        */ 
     295        public function processAdminUI() 
     296        { 
     297                if($this->isOwner($user)||$user->isSuperAdmin()) 
     298                { 
     299                        throw new Exception(_('Access denied!')); 
     300                } 
     301 
     302                foreach ($this->getAllContent() as $content) 
     303                { 
     304                        $name = "node_".$this->id."_content_".$content->GetId()."_erase"; 
     305                        if (!empty ($_REQUEST[$name])) 
     306                        { 
     307                                $this->removeContent($content); 
     308                        } 
     309                } 
     310 
     311                $name = "node_{$this->id}_new_content_submit"; 
     312                if (!empty ($_REQUEST[$name])) 
     313                { 
     314                        $name = "node_{$this->id}_new_content"; 
     315                        $content = Content :: processSelectContentUI($name); 
     316                        $this->addContent($content); 
     317                } 
     318        } 
     319         
     320/** Add content to this node */ 
     321        public function addContent(Content $content) 
     322        { 
     323                global $db; 
     324                $content_id=$db->EscapeString($content->getId()); 
     325                $sql = "INSERT INTO node_has_content (node_id, content_id) VALUES ('$this->id','$content_id')"; 
     326                $db->ExecSqlUpdate($sql, false); 
     327        } 
     328         
     329/** Remove content from this node */ 
     330        public function removeContent(Content $content) 
     331        { 
     332                global $db; 
     333                $content_id=$db->EscapeString($content->getId()); 
     334                $sql = "DELETE FROM node_has_content WHERE node_id='$this->id' AND content_id='$content_id'"; 
     335                $db->ExecSqlUpdate($sql, false); 
     336        } 
     337         
     338        /**Get an array of all Content linked to this node 
     339        * @return an array of Content or an empty arrray */ 
     340        function getAllContent() 
     341        { 
     342                global $db; 
     343                $retval = array (); 
     344                $sql = "SELECT * FROM node_has_content WHERE node_id='$this->id' ORDER BY subscribe_timestamp"; 
     345                $db->ExecSql($sql, $content_rows, false); 
     346                if ($content_rows != null) 
     347                { 
     348                        foreach ($content_rows as $content_row) 
     349                        { 
     350                                $retval[] = Content :: getObject($content_row['content_id']); 
     351                        } 
     352                } 
     353                return $retval; 
     354        } 
     355 
     356        /** Return all the nodes 
     357         */ 
     358        static function getAllNodes() 
     359        { 
     360                global $db; 
     361 
     362                $db->ExecSql("SELECT * FROM nodes", $nodes, false); 
     363 
     364                if ($nodes == null) 
     365                        throw new Exception(_("No nodes could not be found in the database")); 
     366 
     367                return $nodes; 
     368        } 
     369 
     370        static function getAllNodesOrdered($order_by) 
     371        { 
     372                global $db; 
     373 
     374                $db->ExecSql("SELECT * FROM nodes ORDER BY $order_by", $nodes, false); 
     375 
     376                if ($nodes == null) 
     377                        throw new Exception(_("No nodes could not be found in the database")); 
     378 
     379                return $nodes; 
     380        } 
     381 
     382        static function getAllNodesWithStatus() 
     383        { 
     384                global $db; 
     385 
     386                $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 FROM nodes ORDER BY node_id", $nodes, false); 
     387 
     388                if ($nodes == null) 
     389                        throw new Exception(_("No nodes could not be found in the database")); 
     390 
     391                return $nodes; 
     392        } 
     393 
     394        static function getAllDeploymentStatus() 
     395        { 
     396                global $db; 
     397 
     398                $db->ExecSql("SELECT * FROM node_deployment_status", $statuses, false); 
     399                if ($statuses == null) 
     400                        throw new Exception(_("No deployment statues  could be found in the database")); 
     401 
     402                $statuses_array = array (); 
     403                foreach ($statuses as $status) 
     404                        array_push($statuses_array, $status['node_deployment_status']); 
     405 
     406                return $statuses_array; 
     407        } 
     408 
     409        function getOnlineUsers() 
     410        { 
     411                global $db; 
     412 
     413                $db->ExecSql("SELECT users.user_id, users.username, users.account_origin FROM users,connections WHERE connections.token_status='".TOKEN_INUSE."' AND users.user_id=connections.user_id AND connections.node_id='{$this->id}'", $users, false); 
     414                return $users; 
     415        } 
     416 
     417        function getOwners() 
     418        { 
     419                global $db; 
     420 
     421                $db->ExecSql("SELECT user_id FROM node_owners WHERE node_id='{$this->id}'", $owners, false); 
     422                return $owners; 
     423        } 
     424 
     425        function addOwner($user_id) 
     426        { 
     427                /* TODO: VALIDER les champs de donnees node_id et user_id */ 
     428 
     429                global $db; 
     430                if (!$db->ExecSqlUpdate("INSERT INTO node_owners (node_id, user_id) VALUES ('{$this->id}','{$user_id}')", false)) 
     431                        throw new Exception(_('Could not add owner')); 
     432        } 
     433 
     434        function removeOwner($user_id) 
     435        { 
     436                global $db; 
     437                if (!$db->ExecSqlUpdate("DELETE FROM node_owners WHERE node_id='{$this->id}' AND user_id='{$user_id}'", false)) 
     438                        throw new Exception(_('Could not remove owner')); 
     439        } 
     440 
     441        /** Is the user an owner of the Node?  
     442         * @return true our false*/ 
     443        function isOwner(User $user) 
     444        { 
     445                global $db; 
     446                if ($user != null) 
     447                { 
     448                        $user_id = $user->getId(); 
     449                        $retval = false; 
     450                        $db->ExecSqlUniqueRes("SELECT * FROM node_owners WHERE node_id='{$this->id}' AND user_id='{$user_id}'", $row, false); 
     451                        if ($row != null) 
     452                        { 
     453                                $retval = true; 
     454                        } 
     455                } 
     456                return $retval; 
     457        } 
     458 
     459        function nodeExists($id) 
     460        { 
     461                global $db; 
     462                $id_str = $db->EscapeString($id); 
     463                $sql = "SELECT * FROM nodes WHERE node_id='{$id_str}'"; 
     464                $db->ExecSqlUniqueRes($sql, $row, false); 
     465                return $row; 
     466        } 
     467 
     468        function getInfoArray() 
     469        { 
     470                return $this->mRow; 
     471        } 
     472 
     473        public static function getAllOnlineUsers() 
     474        { 
     475                global $db; 
     476                $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); 
     477                return $online_users; 
     478        } 
     479 
     480} // End class 
    286481?>