3 include_once('../config.php');
4 include_once('service_connection.php');
6 // ----------------------------------------------------------------------------------------
8 // ----------------------------------------------------------------------------------------
12 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
14 return substr($chars, rand(0, strlen($chars)-1), 1).substr($chars, rand(0, strlen($chars)-1), 1);
17 // $reason contains the reason why the check failed or success
18 // return true if the check is ok
19 function checkUserValidity ($login, $password, $clientApplication, $cp, &$id, &$reason, &$priv, &$extended)
21 global $DBHost, $DBUserName, $DBPassword, $DBName, $AcceptUnknownUser;
23 $link = mysql_connect($DBHost, $DBUserName, $DBPassword) or die ("Can't connect to database host:$DBHost user:$DBUserName");
24 mysql_select_db ($DBName) or die ("Can't access to the table dbname:$DBName");
25 $query = "SELECT * FROM user where Login='$login'";
26 $result = mysql_query ($query) or die ("Can't execute the query: ".$query);
28 if (mysql_num_rows ($result) == 0)
30 if ($AcceptUnknownUser)
34 // Create a crypted user/pass.
35 $password = crypt($password, createSalt());
38 // login doesn't exist, create it
39 $query = "INSERT INTO user (Login, Password) VALUES ('$login', '$password')";
40 $result = mysql_query ($query) or die ("Can't execute the query: ".$query);
42 // get the user to have his UId
43 $query = "SELECT * FROM user WHERE Login='$login'";
44 $result = mysql_query ($query) or die ("Can't execute the query: ".$query);
46 if (mysql_num_rows ($result) == 1)
48 $reason = "Login '".$login."' was created because it was not found in database (error code 50)";
49 $row = mysql_fetch_array ($result);
51 $priv = $row["Privilege"];
52 $extended = $row["ExtendedPrivilege"];
54 // add the default permission
55 $query = "INSERT INTO permission (UId,ClientApplication) VALUES ('$id', 'snowballs')";
56 $result = mysql_query ($query) or die ("Can't execute the query: ".$query);
62 $reason = "Can't fetch the login '".$login."' (error code 51)";
68 $reason = "Unknown login '".$login."' (error code 52)";
74 $row = mysql_fetch_array ($result);
75 $salt = substr($row["Password"],0,2);
76 if (($cp && $row["Password"] == $password) ||
(!$cp && $row["Password"] == crypt($password, $salt)))
78 // check if the user can use this application
80 $query = "SELECT * FROM permission WHERE UId='".$row["UId"]."' AND ClientApplication='$clientApplication'";
81 $result = mysql_query ($query) or die ("Can't execute the query: ".$query);
82 if (mysql_num_rows ($result) == 0)
85 $reason = "You can't use the client application '$clientApplication' (error code 53)";
90 // check if the user not already online
92 if ($row["State"] != "Offline")
94 $reason = "$login is already online and ";
95 // ask the LS to remove the client
96 if (disconnectClient ($row["ShardId"], $row["UId"], $tempres))
98 $reason = $reason."was just disconnected. Now you can retry the identification (error code 54)";
100 $query = "update shard set NbPlayers=NbPlayers-1 where ShardId=".$row["ShardId"];
101 $result = mysql_query ($query) or die ("Can't execute the query: '$query' errno:".mysql_errno().": ".mysql_error());
103 $query = "update user set ShardId=-1, State='Offline' where UId=".$row["UId"];
104 $result = mysql_query ($query) or die ("Can't execute the query: '$query' errno:".mysql_errno().": ".mysql_error());
108 $reason = $reason."can't be disconnected: $tempres (error code 55)";
115 $priv = $row["Privilege"];
116 $extended = $row["ExtendedPrivilege"];
123 $reason = "Bad password (error code 56)";
131 function checkShardAccess($id, $clientApplication, $shardId)
134 global $DBHost, $DBUserName, $DBPassword, $DBName;
136 $link = mysql_connect($DBHost, $DBUserName, $DBPassword) or die ("0:Can't connect to database host:$DBHost user:$DBUserName");
137 mysql_select_db ($DBName) or die ("0:Can't access to the table dbname:$DBName");
139 $query = "SELECT * FROM permission WHERE UId='".$id."' AND ClientApplication='".$clientApplication."' AND (ShardId='".$shardId."' OR ShardId='-1')";;
140 $result = mysql_query ($query) or die ("0:Can't execute the query: ".$query);
142 if (mysql_num_rows ($result) > 0)
148 die("0:Invalid shard access");
151 function displayAvailableShards($id, $clientApplication, $multiplePatchers)
154 global $DBHost, $DBUserName, $DBPassword, $DBName;
156 $link = mysql_connect($DBHost, $DBUserName, $DBPassword) or die ("0:Can't connect to database host:$DBHost user:$DBUserName");
157 mysql_select_db ($DBName) or die ("0:Can't access to the table dbname:$DBName");
159 $query = "SELECT * FROM user WHERE UId='".$id."'";
160 $result = mysql_query ($query) or die ("0:Can't execute the query: ".$query);
163 $uData = mysql_fetch_array($result);
165 if (strstr($uData['Privilege'], ':DEV:'))
167 else if (strlen($uData['Privilege']) > 0)
172 $query = "SELECT * FROM shard WHERE ClientApplication='".$clientApplication."'";
173 $result = mysql_query ($query) or die ("0:Can't execute the query: ".$query);
177 if (mysql_num_rows ($result) > 0)
179 //echo "<h1>Please, select a shard:</h1>\n";
180 while($row = mysql_fetch_array($result))
182 $query2 = "SELECT * FROM permission WHERE UId='".$id."' AND ClientApplication='".$clientApplication."' AND ShardId='".$row["ShardId"]."'";
183 $result2 = mysql_query ($query2) or die ("Can't execute the query: ".$query2);
185 $online = $row["Online"];
194 $uOnline = ($priv == 'dev' ?
1 : 2);
197 $uOnline = (($priv == 'dev' ||
$priv == 'gm') ?
1 : 2);
204 // only display the shard if the user have the good application name AND access to this shard with the permission table
205 if (mysql_num_rows ($result2) > 0 && $row["ProgramName"] == $programName)
208 $res = $res.$row["Version"]."|";
209 $res = $res.$uOnline."|";
210 $res = $res.$row["ShardId"]."|";
211 $res = $res.$row["Name"]."|";
212 $res = $res."999999|";
213 $res = $res.$row["WsAddr"]."|";
214 $res = $res.$row["PatchURL"];
215 if (strlen($row["DynPatchURL"]) > 0 && $multiplePatchers)
216 $res = $res."|".$row["DynPatchURL"];
228 function askSalt($login)
231 global $DBHost, $DBUserName, $DBPassword, $DBName, $AcceptUnknownUser;
233 $link = mysql_connect($DBHost, $DBUserName, $DBPassword) or die ("0:Can't connect to database host:$DBHost user:$DBUserName");
234 mysql_select_db ($DBName) or die ("0:Can't access to the table dbname:$DBName");
236 $query = "SELECT Password FROM user WHERE Login='$login'";
237 $result = mysql_query ($query) or die ("0:Can't execute the query: ".$query);
239 if (mysql_num_rows ($result) != 1)
241 if ($AcceptUnknownUser)
243 $salt = createSalt();
247 die ("0:Unknown login $login (error code 64)");
252 $res_array = mysql_fetch_array($result);
253 $salt = substr($res_array['Password'], 0, 2);
260 // --------------------------------------------------------------------------------------
262 // --------------------------------------------------------------------------------------
264 if ($_GET["cmd"] == "ask")
266 askSalt($_GET["login"]);
270 // check cp is set (force bool)
271 $cp = ($_GET["cp"] == "1");
273 if (!checkUserValidity($_GET["login"], $_GET["password"], $_GET["clientApplication"], $cp, $id, $reason, $priv, $extended))
279 if ($_GET["cmd"] == "login")
281 checkShardAccess($id, $_GET["clientApplication"], $_GET["shardid"]);
283 // user selected a shard, try to add the user to the shard
285 if (askClientConnection($_GET["shardid"], $id, $_GET["login"], $priv, $extended, $res, $patchURLS))
287 // access granted, send cookie and addr
290 // LS sent patching URLS? Add them at the end of the string
291 if (strlen($patchURLS) > 0)
295 // OBSOLETE: emergency patch URI already sent at displayAvailableShards - no need to add it
296 // There is a default patching address? Add it at the end of the patching URLS
297 $query = "SELECT PatchURL FROM shard WHERE ShardId='$shardid'";
298 $result = mysql_query($query);
299 if ($result && ($array=mysql_fetch_array($result)))
301 $patchURL = $array['PatchURL'];
302 if (strlen($patchURL) > 0)
304 echo (strlen($patchURLS) > 0 ? '|' : ' ').$patchURL;
311 // access denied, display why
317 // user logged, display the available shard
318 displayAvailableShards ($id, $_GET["clientApplication"], $cp);