root/wifidog/wifidog-auth/wifidog/ping/index.php

Revision 598, 8.3 kB (checked in by syrus, 5 months ago)

optimisation postgres par sxpert - synchro avec la version en prod

  • Property svn:executable set to *
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  * This will respond to the gateway to tell them that the gateway is still up,
38  * and also log the gateway checking in for network monitoring
39  *
40  * @package    WiFiDogAuthServer
41  * @author     Alexandre Carmel-Veilleux <acv@acv.ca>
42  * @copyright  2004-2006 Alexandre Carmel-Veilleux
43  * @version    Subversion $Id: index.php 916 2006-01-23 05:28:15Z max-horvath $
44  * @link       http://www.wifidog.org/
45  */
46
47 /**
48  * Raphael Jacquot 2009-01-15
49  * amélioration des vérifications des entrées.
50  */
51
52 /**
53  * Load required files
54  * connect to the database directly, get rid of the dumbass abstraction layer from hell
55  */
56 require_once('../config.php');
57 $conn_string = "host=".CONF_DATABASE_HOST." dbname=".CONF_DATABASE_NAME." user=".CONF_DATABASE_USER." password=".CONF_DATABASE_PASSWORD;
58 $db = @ pg_pconnect($conn_string);
59
60 if ($db == FALSE) {
61     // si on a un probleme, on sort sans rien faire
62     error_log ("Unable to connect to database");
63     exit(1);
64 }
65
66 echo "Pong";
67
68 // On recupere l'id de la gateway
69 if (array_key_exists('gw_id',$_REQUEST)) $node_id = $_REQUEST['gw_id'];
70 else $node_id="";
71
72 // On recupere la version de Wifidog utilisee
73 if (array_key_exists('HTTP_USER_AGENT',$_SERVER)) $user_agent $_SERVER['HTTP_USER_AGENT'];
74 else $user_agent="";
75
76 // On recupere le network_id
77 if (array_key_exists('network_id',$_REQUEST)) $network_id = $_REQUEST['network_id'];
78 else $network_id="";
79
80 // On recupere les coordonnees gps
81 // c'est transformé en float automatiquement par php (comme ca on est tranquille)
82 if (array_key_exists('lat', $_REQUEST)) $lat = 0.0+$_REQUEST['lat'];
83 else $lat = 0.0;
84 if (array_key_exists('lon', $_REQUEST)) $lon=0.0+$_REQUEST['lon'];
85 else $lon = 0.0;
86
87 // On recupere le node name
88 if (array_key_exists('node_name',$_REQUEST)) $node_name = trim($_REQUEST['node_name']);
89 else $node_name = "";
90
91 if($node_name == "(null)") $node_name = "";
92
93 // On verifie que le noeud existe en base
94 $q = pg_query_params($db, "select node_id, name from nodes where node_id=$1;", array($node_id));
95 $numrow = pg_num_rows($q);
96
97 // Si le noeud n'existe pas
98 if (($numrow == 0) && array_key_exists('prop',$_REQUEST)){
99     
100     /* modifications de valeurs */
101     if($network_id=="") $network_id="default-network";
102     if($node_name=="") $node_name="Node".$owner;
103
104     echo "\nLe node $node_id n existe pas\n";
105     // On verifie que le proprietaire est enregistre en base
106
107     $owner = $_REQUEST['prop'];
108     echo "Proprio: $owner\n";
109
110     $q = pg_query_params($db, "select user_id from users where username=$1;", array($owner));
111     $numrow = pg_num_rows($q);
112   
113     echo "Nbr de résultats : $numrow\n";
114
115     // Si le proprietaire est enregistre comme utilisateur
116     // normalement on ne doit en avoir qu'un seul
117     if ($numrow == 1){
118         echo "Proprio $owner existe\n";
119
120         $row = pg_fetch_assoc($q);
121         $user_id=$row["user_id"];
122       
123         // On ajoute le noeud a la base
124         echo "Rajout du node $node_id\n";
125
126         pg_query_params ($db, "insert into nodes(network_id, last_heartbeat_ip, last_heartbeat_timestamp, last_heartbeat_user_agent,".
127             "node_id, latitude, longitude, node_deployment_status, name) values ($1, $2, CURRENT_TIMESTAMP, $3, $4, $5, $6, $7, $8);",
128             array ($network_id, $_SERVER['REMOTE_ADDR'], $user_agent, $node_id, $lat, $lon, "DEPLOYED", $node_name));
129
130         // On ajoute les droits d'administration du noeud au proprietaire
131         echo "Rajout $user_id pour $node_id\n";
132         pg_query_params ($db, "insert into node_stakeholders (node_id, user_id, is_owner, is_tech_officer) values ($1, $2, 'TRUE', 'FALSE');", array($node_id, $user_id));
133         
134     } else {
135         if ($numrow==0) {
136             $errmsg = sprintf("Attempting to add node %s (%s) to network %s with owner %s failed, owner does not exist",
137                 $node_id, $node_name, $network_id, $owner);
138             error_log ($errmsg);
139         } else {
140             $errmsg = sprintf("CAN'T HAPPEN: found multiple (%d) users for user_id %s", $numrow, $user_id);
141             error_log ($errmsg);
142         }
143     }
144
145 } else {
146     // Le noeud existe deja en base, on met a jour ses informations
147
148     echo "\nMise a jour des infos.\n";
149
150     $sql = "last_heartbeat_ip='". $_SERVER['REMOTE_ADDR']. "', last_heartbeat_user_agent='$user_agent'";
151
152     if(isset($_REQUEST['network_id'])) $sql .= ",network_id = '". $_REQUEST['network_id']. "'";
153
154     if($lat!=0 && $lon!=0) $sql .= ", latitude=$lat, longitude=$lon";
155
156     // change le nom du node si ca a changé
157     $row = pg_fetch_assoc($q);
158     $errmsg = "node_id = '".$node_id."', name = '".$row['name']."', node_name = '".$node_name."'";
159     if (strcmp($row["name"], $node_name)!=0) {
160         $errmsg.=" ==> name change";
161         error_log ($errmsg);
162     }
163
164     $req = "update nodes set last_heartbeat_timestamp=CURRENT_TIMESTAMP, node_deployment_status='DEPLOYED', $sql where node_id='$node_id';";
165      pg_query($db, $req);
166
167 }
168
169 pg_close($db);
170
171 ////////////////////////////////////////////////////////////////////////
172 // HACK : Permet de forwarder le ping vers le serveur de la federation
173
174 //function postit($host, $url, $postdata) {
175 //function doResquest($host, $url){
176 //    $fp = pfsockopen ( $host, 80, &$errno, &$errstr, 60 );
177 //    if( ! $fp ) return "$errstr ($errno)<br>\n";
178 //    fputs ($ft,"GET /ping/index.php?$url HTTP/1.1\n");
179 //     fputs ($fp,"POST $url HTTP/1.1\n");
180 //    fputs ($fp,"Host: $host\n");
181 //    fputs ($fp,"User-Agent: France-Wireless Auth\n");
182 //    fputs ($fp,"Accept: */*\n");
183 //    fputs ($fp,"Content-type: application/x-www-form-urlencoded\n");
184 //    fputs ($fp,"Content-length: ".strlen($postdata)."\n\n");
185 //   fputs ($fp,"$postdata\n\n");
186 //    fputs ($fp,"\n");
187 //    $output = "";
188 //    while( !feof( $fp ) ) {
189 //        $output .= fgets( $fp, 1024);
190 //    }
191 //    fclose ( $fp);
192 //    return ($output);
193 //}
194
195 //$host = "auth.marseille-wireless.org";
196 //$url = "/ping/index.php";
197 //$url = $_SERVER['QUERY_STRING'];
198 //$postdata = "gw_id=".$_REQUEST['gw_id']."&prop=".$_REQUEST['prop'];
199 //doResquets($host, $url);
200
201 /*$gotten = postit($host, $url, $postdata);*/
202 //////////////////////////////////////////////////////////////////////
203
204
205 /*
206  * Local variables:
207  * tab-width: 4
208  * c-basic-offset: 4
209  * c-hanging-comment-ender-p: nil
210  * End:
211  */
212
213 ?>
214
Note: See TracBrowser for help on using the browser.