Added full source code

This commit is contained in:
Emmanuel BENOîT 2016-01-10 11:01:49 +01:00
commit 33f8586698
1377 changed files with 123808 additions and 0 deletions
scripts/game/main/actions

View file

@ -0,0 +1,150 @@
<?
/** This action allows an user to join a game.
*
* \parameter $user User ID of the user who wants to register
* \parameter $gId Game ID of the game to join
* \parameter $dryRun Indicates whether to register or to check whether the
* game can be joined by the user
* \parameter $planet Name of the player's first planet
* \parameter $player Name of the player if he had played the game before
*
* \returns either an integer indicating an internal error or an array
* containing the data related to the request.
*
* The following integer values indicate errors:
* 0 Game not found
* 1 User already registered
* 2 Internal error while registering
*
* Two forms of arrays can be returned. If the call was a dry run or if
* an error happened, it will contain the following data:
*
* game Game ID of the game to register to
* gName Title of the game to register to
* planet Selected planet name
* planetError An integer indicating an eventual error (see
* below)
* player Selected player name
* playerError An integer indicating an eventual error (see
* below)
* returning A boolean indicating whether the user has
* played the selected game before
*
* The error codes for the names are:
*
* 0 Game not found
* 1 Name too long
* 2 Invalid characters
* 3 Heading/trailing spaces
* 4 Multiple spaces
* 5 Name too short
* 6 Name in use
*
* If the registration was successful, the following array is returned:
*
* registered ID of the game the user registered to
*
*/
class main_joinGame {
function main_joinGame($main) {
$this->main = $main;
$this->lib = $this->main->getLib();
}
function run($user, $gId, $dryRun = true, $planet = null, $player = null) {
// Get the game and its main library
$game = config::getGame($gId);
if (is_null($game)) {
return array('error' => 0);
}
$lib = $game->getLib();
// Check the game's status
$status = $game->status();
if ($status != 'RUNNING' && $status != 'READY' && $status != 'ENDING') {
return array('error' => 0);
}
// Check if the game is available and can be joined
if (! $lib->call('canJoin')) {
return array('error' => 0);
}
// Does the user play the game already?
if ((int)$user < 1 || $lib->call('doesUserPlay', $user)) {
return array('error' => 1);
}
// Has the current user played that game in the past?
$returning = $lib->call("hasPlayed", $user);
// If the user submitted the form, handle it
if ($dryRun) {
// Display a blank form
return array(
"game" => $gId,
"gName" => $game->text,
"desc" => $game->descriptions[getLanguage()],
"planet" => "",
"planetError" => 0,
"player" => "",
"playerError" => 0,
"returning" => $returning
);
}
// Check the specified planet and player names
$pErr = $lib->call('checkPlanetName', $planet);
if ($returning) {
$pnErr = $this->checkName($player);
} else {
$pnErr = 0;
}
if (!($pErr || $pnErr)) {
// Try to register to this game
$res = $lib->call('register', $user, $planet, $returning ? $player : null);
switch ($res) :
case 1: return array('error' => 2);
case 2: $pnErr = 6; break;
case 3: $pErr = 6; break;
endswitch;
}
if ($pErr || $pnErr) {
return array(
"game" => $gId,
"gName" => $game->text,
"planet" => $planet,
"planetError" => $pErr,
"player" => $player,
"playerError" => $pnErr,
"returning" => $returning
);
}
return array("registered" => $gId);
}
function checkName($n) {
if (strlen($n) > 15) {
return array('error' => 1);
} elseif (preg_match('/[^A-Za-z0-9_\.\-\+@\/'."'".' ]/', $n)) {
return array('error' => 2);
} elseif (preg_match('/^\s/', $n) || preg_match('/\s$/', $n)) {
return array('error' => 3);
} elseif (preg_match('/\s\s+/', $n)) {
return array('error' => 4);
} elseif (strlen($n) < 2) {
return array('error' => 5);
}
return 0;
}
}
?>

View file

@ -0,0 +1,139 @@
<?
/** This action allows an user to get a new password; the user first enters
* his username and his email address; once this step is completed, and if
* the mail address matches the one in the database for the account, a
* confirmation code is generated and an email is sent to the address. The
* user is then required to enter that code, at which time the password is
* replaced with a random password and a second email is sent.
*/
class main_lostPassword {
function main_lostPassword($main) {
$this->main = $main;
$this->db = $this->main->db;
$this->lib = $this->main->getLib();
$this->accounts = $this->main->getLib("main/account");
}
function run($userName, $mailAddress, $confirmationCode) {
if (is_null($userName)) {
return false;
}
$r = $this->checkAccount($userName, $mailAddress, $confirmationCode);
if (is_null($r)) {
return array(
"error" => is_null($confirmationCode) ? 1 : 3,
"name" => $userName,
"mail" => $mailAddress,
"code" => $confirmationCode
);
}
list($accountId, $userName, $realCode) = $r;
// No confirmation code
if (is_null($confirmationCode)) {
return $this->checkNewForm($accountId, $userName, $mailAddress, $realCode);
}
return $this->generatePassword($accountId, $userName, $mailAddress);
}
function checkAccount($userName, $mailAddress, $confirmationCode) {
if (!($this->lib->call('isValidName', $userName) && $this->lib->call('isValidAddress', $mailAddress))) {
return null;
}
if (!(is_null($confirmation) || preg_match('/^[A-Fa-f0-9]{32,32}$/', $confirmationCode))) {
return null;
}
$qs = "SELECT id,name,pw_conf FROM account WHERE status NOT IN ('NEW','KICKED') "
. "AND name='" . addslashes($userName) . "' AND email='" . addslashes($mailAddress) . "'";
if (!is_null($confirmationCode)) {
$qs .= " AND pw_conf='$confirmationCode'";
}
$q = $this->db->query($qs);
if (!($q && dbCount($q) == 1)) {
logText("main::actions::lostPassword() failed to authenticate account '$userName'", LOG_WARNING);
return null;
}
return dbFetchArray($q);
}
function checkNewForm($id, $name, $mail, $code) {
$rc = 4;
if (is_null($code)) {
// No code created yet; generate one and send the mail
$code = $this->createConfirmationCode($id, $name, $mail);
if (is_null($code)) {
$rc = 2;
}
}
return array(
"error" => $rc,
"name" => $name,
"mail" => $mail,
"code" => null
);
}
function createConfirmationCode($id, $name, $mail) {
$conf = substr(md5(uniqid(rand())), 0, 16);
$this->db->query("UPDATE account SET pw_conf='$conf' WHERE id='$id'");
$lang = $this->accounts->call('getLanguage', $id);
$rv = $this->lib->call('sendMail', "mail-change-pass-conf.$lang.txt", $mail, array(
"USER" => $name,
"CODE" => $conf
));
if (!$rv) {
$this->db->end(true); $this->db->start();
return null;
}
return $conf;
}
function generatePassword($id, $name, $mail) {
$newPass = "";
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-+/*_@=#~&!";
for ($i=0;$i<16;$i++) {
do {
$nc = $chars{rand(0,strlen($chars)-1)};
} while (strstr($newPass, $nc) !== false);
$newPass .= $nc;
}
$this->db->query("UPDATE account SET pw_conf=NULL,password='$newPass' WHERE id=$id");
$lang = $this->accounts->call('getLanguage', $id);
$rv = $this->lib->call('sendMail', "mail-change-pass.$lang.txt", $mail, array(
"USER" => $name,
"PASS" => $newPass
));
if (!$rv) {
$this->db->end(true); $this->db->start();
return array(
"error" => 5,
"name" => $name,
"mail" => $mail,
"code" => null
);
}
return true;
}
}
?>