| 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 | require_once BASEPATH.'include/common.php'; |
|---|
| 25 | |
|---|
| 26 | /** Abstract a Node. A Node is an actual physical transmitter. */ |
|---|
| 27 | class Node |
|---|
| 28 | { |
|---|
| 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 | } //End class |
|---|
| 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 | /** Return all the nodes |
|---|
| 161 | */ |
|---|
| 162 | static function getAllNodes () |
|---|
| 163 | { |
|---|
| 164 | global $db; |
|---|
| 165 | |
|---|
| 166 | $db->ExecSql ("SELECT * FROM nodes", $nodes, false); |
|---|
| 167 | |
|---|
| 168 | if ($nodes == null) |
|---|
| 169 | throw new Exception(_("No nodes could not be found in the database")); |
|---|
| 170 | |
|---|
| 171 | return $nodes; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | static function getAllNodesOrdered ($order_by) |
|---|
| 175 | { |
|---|
| 176 | global $db; |
|---|
| 177 | |
|---|
| 178 | $db->ExecSql ("SELECT * FROM nodes ORDER BY $order_by", $nodes, false); |
|---|
| 179 | |
|---|
| 180 | if ($nodes == null) |
|---|
| 181 | throw new Exception(_("No nodes could not be found in the database")); |
|---|
| 182 | |
|---|
| 183 | return $nodes; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | static function getAllNodesWithStatus () |
|---|
| 187 | { |
|---|
| 188 | global $db; |
|---|
| 189 | |
|---|
| 190 | $db-> |
|---|
| 191 | ExecSql |
|---|
| 192 | ("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", |
|---|
| 193 | $nodes, false); |
|---|
| 194 | |
|---|
| 195 | if ($nodes == null) |
|---|
| 196 | throw new Exception(_("No nodes could not be found in the database")); |
|---|
| 197 | |
|---|
| 198 | return $nodes; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | static function getAllDeploymentStatus () |
|---|
| 202 | { |
|---|
| 203 | global $db; |
|---|
| 204 | |
|---|
| 205 | $db->ExecSql ("SELECT * FROM node_deployment_status", $statuses, false); |
|---|
| 206 | if ($statuses == null) |
|---|
| 207 | throw new Exception(_("No deployment statues could be found in the database")); |
|---|
| 208 | |
|---|
| 209 | $statuses_array = array (); |
|---|
| 210 | foreach ($statuses as $status) |
|---|
| 211 | array_push ($statuses_array, $status['node_deployment_status']); |
|---|
| 212 | |
|---|
| 213 | return $statuses_array; |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | function getOnlineUsers () |
|---|
| 217 | { |
|---|
| 218 | global $db; |
|---|
| 219 | |
|---|
| 220 | $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->mId}'", $users, false); |
|---|
| 221 | return $users; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | function getOwners() { |
|---|
| 225 | global $db; |
|---|
| 226 | |
|---|
| 227 | $db->ExecSql("SELECT user_id FROM node_owners WHERE node_id='{$this->mId}'", $owners, false); |
|---|
| 228 | return $owners; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | function addOwner($user_id) { |
|---|
| 232 | /* TODO: VALIDER les champs de donnees node_id et user_id */ |
|---|
| 233 | |
|---|
| 234 | global $db; |
|---|
| 235 | if (!$db->ExecSqlUpdate("INSERT INTO node_owners (node_id, user_id) VALUES ('{$this->mId}','{$user_id}')", false)) |
|---|
| 236 | throw new Exception(_('Could not add owner')); |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | function removeOwner($user_id) { |
|---|
| 240 | global $db; |
|---|
| 241 | if (!$db->ExecSqlUpdate("DELETE FROM node_owners WHERE node_id='{$this->mId}' AND user_id='{$user_id}'", false)) |
|---|
| 242 | throw new Exception(_('Could not remove owner')); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | function nodeExists($id) { |
|---|
| 246 | global $db; |
|---|
| 247 | $id_str = $db->EscapeString($id); |
|---|
| 248 | $sql = "SELECT * FROM nodes WHERE node_id='{$id_str}'"; |
|---|
| 249 | $db->ExecSqlUniqueRes($sql, $row, false); |
|---|
| 250 | return $row; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | public static function getAllOnlineUsers() { |
|---|
| 254 | global $db; |
|---|
| 255 | $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); |
|---|
| 256 | return $online_users; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | } // End class |
|---|
| 260 | |
|---|
| 261 | ?> |
|---|