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

View file

@ -0,0 +1,87 @@
<?php
class main_account_library {
var $index = array(
'createAccount',
'getAccounts',
'getKickList',
'getLanguage',
'getQuitCountdown',
'getUserName',
'isLeech',
'log',
'requestKick',
'setQuitCountdown',
'terminate'
);
function main_account_library($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function isOnline($uid) {
$q = $this->db->query("SELECT COUNT(*) FROM account WHERE id=$uid AND last_login IS NOT NULL AND (last_logout IS NULL OR last_logout<last_login)");
list($online) = dbFetchArray($q);
return ($online == 1);
}
function lastOnline($userID) {
$q = $this->db->query("SELECT last_login, last_logout FROM account WHERE id = $userID");
list($login, $logout) = dbFetchArray($q);
if (is_null($logout) || $logout < $login) {
return 0;
}
return $logout;
}
function cancelQuitCountdown($uid) {
$this->db->query("UPDATE account SET quit_ts=NULL,reason=NULL WHERE id=$uid");
}
function isAdmin($uid) {
$q = $this->db->query("SELECT admin FROM account WHERE id=$uid");
if ($q && dbCount($q) == 1) {
list($a) = dbFetchArray($q);
} else {
$a = 'f';
}
return ($a == 't');
}
function getUser($name) {
$i = (int) $name;
if ((string)$i == (string)$name) {
$qs = "id=$name";
} else {
$qs = "LOWER(name)=LOWER('" . addslashes($name) . "')";
}
$q = $this->db->query("SELECT * FROM account WHERE $qs");
if (!($q && dbCount($q) == 1)) {
return null;
}
return dbFetchHash($q);
}
function getKickRequest($kid) {
$q = $this->db->query("SELECT * FROM adm_kick WHERE id=$kid");
if (!($q && dbCount($q) == 1)) {
return null;
}
return dbFetchHash($q);
}
function kickRequestHandled($kid, $admin, $accepted) {
$this->db->query("UPDATE adm_kick SET examined_by=$admin,status='" . ($accepted ? "Y" : "N") . "' WHERE id=$kid");
}
}
?>

View file

@ -0,0 +1,58 @@
<?php
class main_account_createAccount {
function main_account_createAccount($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($user, $password, $email, $lang, $pName) {
$conf = substr(md5(uniqid(rand())), 0, 16);
$asu = addslashes($user);
$id = $this->db->query(
"INSERT INTO account(name,email,password,status,conf_code) "
. "VALUES('$asu','$email','".addslashes($password)."','NEW','$conf')"
);
if (!$id) {
return false;
}
$this->db->query(
"INSERT INTO pass_change (account, old_pass, new_pass) "
. "VALUES($id, '', '" . addslashes($password) . "')"
);
$this->db->query("INSERT INTO credits (account) VALUES ($id)");
$this->db->query("INSERT INTO user_preferences VALUES('language','main',$id,'$lang');");
$this->db->query(
"INSERT INTO account_log(tracking,account,ip_addr,action) VALUES("
. tracking::$dbId . ",$id,'".$_SERVER['REMOTE_ADDR']."','CREATE')"
);
// Insert the planet in the registration queue for the default game
$game = config::getDefaultGame();
$this->db->query(
"INSERT INTO reg_queue (account, game) "
. "VALUES ($id, '{$game->name}')"
);
$game->getLib()->call('preRegister', $id, $pName);
$main = $this->lib->game->getLib('main');
$rv = $main->call('sendMail', "mail-reg.$lang.txt", $email, array(
"USER" => $user,
"PASS" => $password,
"CCODE" => $conf
));
// If sending the mail failed, rollback the transaction
if (!$rv) {
$this->db->end(true); $this->db->begin();
}
return $rv;
}
}
?>

View file

@ -0,0 +1,27 @@
<?php
class main_account_getAccounts {
function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run() {
$q = $this->db->query("SELECT COUNT(*) FROM account WHERE status IN ('STD','VAC')");
if (!($q && dbCount($q))) {
return null;
}
list($total) = dbFetchArray($q);
$q = $this->db->query("SELECT COUNT(*) FROM account WHERE status IN ('STD','VAC') AND last_login IS NOT NULL AND (last_logout IS NULL OR last_logout<last_login)");
if (!($q && dbCount($q))) {
return null;
}
list($online) = dbFetchArray($q);
return array($total, $online);
}
}
?>

View file

@ -0,0 +1,24 @@
<?php
class main_account_getKickList {
function main_account_getKickList($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run() {
$lists = array(array(),array(),array());
$types = array("P" => 0, "Y" => 1, "N" => 2);
$q = $this->db->query("SELECT * FROM adm_kick");
while ($r = dbFetchHash($q)) {
array_push($lists[$types[$r['status']]], $r);
}
return $lists;
}
}
?>

View file

@ -0,0 +1,22 @@
<?php
class main_account_getLanguage {
function main_account_getLanguage($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($id) {
$ql = $this->db->query("SELECT value FROM user_preferences WHERE id='language' AND version='main' AND account=$id");
if ($ql && dbCount($ql)) {
list($l) = dbFetchArray($ql);
} else {
$l = 'en';
}
return $l;
}
}
?>

View file

@ -0,0 +1,22 @@
<?php
class main_account_getQuitCountdown {
function main_account_getQuitCountdown($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($uid) {
$q = $this->db->query("SELECT quit_ts FROM account WHERE id=$uid");
if ($q && dbCount($q) == 1) {
list($quit) = dbFetchArray($q);
} else {
$quit = null;
}
return $quit;
}
}
?>

View file

@ -0,0 +1,25 @@
<?php
class main_account_getUserName {
var $userNames = array(); // FIXME: can be cached
function main_account_getUserName($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($uid) {
if (!is_null($this->userNames[$uid])) {
return $this->userNames[$uid];
}
$q = $this->db->query("SELECT name FROM account WHERE id=$uid");
if (!($q && dbCount($q))) {
return null;
}
list($this->userNames[$uid]) = dbFetchArray($q);
return $this->userNames[$uid];
}
}
?>

View file

@ -0,0 +1,32 @@
<?php
class main_account_isLeech {
function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($id) {
$q = $this->db->query("SELECT UNIX_TIMESTAMP(NOW())-donated FROM pp_history ORDER BY donated ASC LIMIT 1");
if (!($q && dbCount($q))) {
return true;
}
list($interval) = dbFetchArray($q);
if ($interval == 0) {
return true;
}
$days = $interval / 86400;
$q = $this->db->query("SELECT SUM(amount) FROM pp_history WHERE account=$id");
if (!($q && dbCount($q))) {
return false;
}
list($sum) = dbFetchArray($q);
$sum = is_null($sum) ? 0 : $sum;
return ($sum / $days <= 0.05);
}
}
?>

View file

@ -0,0 +1,63 @@
<?php
/** This function adds an entry to the account_log table. The $uid parameter
* indicates the user's database identifier; the $what parameter indicates
* the action that warrants the log entry. $what can take the following
* values:
*
* [Ii] User has logged in
* [Oo] User has logged out
* [Cc] The account has just been created
* [Vv] User has validated his account using his confirmation code
* [Qq] User has quit the game
* VS User has entered vacation mode
* VE User has left vacation mode
*/
class main_account_log {
function main_account_log($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($uid, $what) {
if (class_exists('tracking') && !is_null(tracking::$dbId)) {
$track = tracking::$dbId;
} else {
$track = 'NULL';
}
if (gettype($_SERVER['REMOTE_ADDR']) == 'NULL') {
$addr = 'AUTO';
} else {
$addr = addslashes($_SERVER['REMOTE_ADDR']);
}
switch ($what) :
case 'I': case 'i':
$w = 'IN'; break;
case 'O': case 'o':
$w = 'OUT'; break;
case 'C': case 'c':
$w = 'CREATE'; break;
case 'V': case 'v':
$w = 'CONF'; break;
case 'Q': case 'q':
$w = 'QUIT'; break;
case 'Q': case 'q':
$w = 'QUIT'; break;
case 'VS': case 'vs':
$w = 'VSTART'; break;
case 'VE': case 've':
$w = 'VEND'; break;
default:
return;
endswitch;
$this->db->query("INSERT INTO account_log(tracking,account,ip_addr,action) VALUES ($track,$uid,'$addr','$w')");
}
}
?>

View file

@ -0,0 +1,24 @@
<?php
class main_account_requestKick {
function main_account_requestKick($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($admin, $account, $reason) {
$q = $this->db->query("SELECT * FROM adm_kick WHERE to_kick=$account AND status<>'N'");
if (!$q || dbCount($q)) {
return false;
}
$id = $this->db->query("INSERT INTO adm_kick (to_kick,requested_by,requested_at,reason) "
. "VALUES ($account,$admin," . time() . ",'" . addslashes($reason) . "')");
return !!$id;
}
}
?>

View file

@ -0,0 +1,23 @@
<?php
class main_account_setQuitCountdown {
function main_account_setQuitCountdown($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($uid, $reason) {
$reason = trim(preg_replace('/\s+/', ' ', $reason));
if ($reason == '') {
$rqs = "";
} else {
$rqs = ",reason='" . addslashes($reason) . "'";
}
$this->db->query("UPDATE account SET quit_ts=UNIX_TIMESTAMP(NOW())$rqs WHERE id=$uid AND status='STD'");
}
}
?>

View file

@ -0,0 +1,32 @@
<?php
class main_account_terminate {
function main_account_terminate($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($uid, $status, $reason = null) {
foreach (config::getGames() as $game) {
if ($game->name == 'main' || $game->status() == 'FINISHED') {
continue;
}
$lib = $game->getLib();
$pid = $lib->call('doesUserPlay', $uid);
if (is_null($pid)) {
continue;
}
$lib->call('leaveGame', $pid, $status);
}
$qs = is_null($reason) ? "" : (",reason='" . addslashes($reason) . "'");
$this->db->query("UPDATE account SET status='$status',quit_ts=NULL,vac_start=NULL$qs WHERE id=$uid");
$this->lib->call('log', $uid, 'q');
}
}
?>

View file

@ -0,0 +1,238 @@
<?php
class actions_main {
var $versions = null;
var $userNames = array();
var $index = array(
'joinGame',
'lostPassword'
);
function actions_main($game) {
$this->game = $game;
$this->main = $this->game->getLib('main');
$this->accounts = $this->game->getLib('main/account');
$this->vacation = $this->game->getLib('main/vacation');
$this->forums = $this->game->getLib('main/forums');
}
function getUserName($uid) {
return $this->accounts->call('getUserName', $uid);
}
function createAccount($u, $p, $e, $l) {
return $this->accounts->call('createAccount', $u, $p, $e, $l);
}
function isGameRunning($version) {
return $this->main->call('isGameRunning', $version);
}
function getTick($version, $name, $lang = null) {
$g = config::getGame($version);
$lib = $g->getLib('main');
return $lib->call('getTick', $name, $lang);
}
function getTicks($version, $lang) {
$g = config::getGame($version);
$lib = $g->getLib('main');
return $lib->call('getTicks', $lang);
}
function getRankingType($version, $identifier) {
$g = config::getGame($version);
$rnk = $g->getLib('main/rankings');
return $rnk->call('getType', $identifier);
}
function getRankingTypes($version) {
$g = config::getGame($version);
$rnk = $g->getLib('main/rankings');
return $rnk->call('getTypes');
}
function getRankingText($id, $lang) {
$rnk = $this->game->getLib('main/rankings');
return $rnk->call('getText', $id, $lang);
}
function updateRankings($id, $data) {
$rnk = $this->game->getLib('main/rankings');
$rnk->call('update', $id, $data);
}
function getRankings($type, $top = null) {
$rnk = $this->game->getLib('main/rankings');
return $rnk->call('getAll', $type, $top);
}
function getRanking($type, $id) {
$rnk = $this->game->getLib('main/rankings');
return $rnk->call('get', $type, $id);
}
function appendRanking($type,$id) {
$rnk = $this->game->getLib('main/rankings');
return $rnk->call('append', $type, $id);
}
function deleteRanking($type,$id) {
$rnk = $this->game->getLib('main/rankings');
return $rnk->call('delete', $type, $id);
}
function getForumCategories() {
return $this->forums->call('getCategories');
}
function getVersionCategory($ver) {
return $this->forums->call('getVersionCategory', $ver);
}
function getForumCategory($c) {
return $this->forums->call('getCategory', $c);
}
function getForums($c) {
return $this->forums->call('getForums', $c);
}
function getForum($f) {
return $this->forums->call('get', $f);
}
function getTopics($f, $first, $count) {
return $this->forums->call('getTopics', $f, $first, $count);
}
function newTopic($a, $fid, $sub, $txt, $ec, $es, $st) {
return $this->forums->call('newTopic', $a, $fid, $sub, $txt, $ec, $es, $st);
}
function postReply($a, $post, $sub, $txt, $ec, $es) {
return $this->forums->call('reply', $a, $post, $sub, $txt, $ec, $es);
}
function postEdit($a, $pid, $sub, $txt, $ec, $es) {
return $this->forums->call('edit', $a, $pid, $sub, $txt, $ec, $es);
}
function forumSubstitute($text, $ec, $es) {
return $this->forums->call('substitute', $text, $ec, $es);
}
function forumSignature($u) {
return $this->forums->call('signature', $u);
}
function getTopic($tid) {
return $this->forums->call('getTopic', $tid);
}
function getPosts($tid, $thr, $o, $cnt, $fst) {
return $this->forums->call('getPosts', $tid, $thr, $o, $cnt, $fst);
}
function getPost($pid) {
return $this->forums->call('getPost', $pid);
}
function isTopicRead($topic, $player) {
return $this->forums->call('isRead', $topic, $player);
}
function markAsRead($topic, $player) {
return $this->forums->call('markRead', $topic, $player);
}
function markAsUnread($topic, $player) {
return $this->forums->call('markUnread', $topic, $player);
}
function getReadTopics($fid, $uid) {
return $this->forums->call('getRead', $fid, $uid);
}
function updateForumLast($forum) {
return $this->forums->call('updateLast', $forum);
}
function deleteTopic($forum, $topic) {
return $this->forums->call('deleteTopic', $forum, $topic);
}
function switchTopicSticky($forum, $topic) {
return $this->forums->call('switchSticky', $forum, $topic);
}
function moveTopic($forum, $topic, $dest, $user) {
return $this->forums->call('move', $forum, $topic, $dest, $user);
}
function deleteSinglePost($postId) {
return $this->forums->call('deletePost', $postId);
}
function markForumAsRead($fid, $uid) {
return $this->forums->call('markForumRead', $fid, $uid);
}
function getAdministrator($uid) {
return $this->forums->call('getAdministrator', $uid);
}
function getModerator($uid) {
return $this->forums->call('getModerator', $uid);
}
function getAccounts() {
return $this->accounts->call('getAccounts');
}
function isOnline($uid) {
return $this->accounts->call('isOnline', $uid);
}
function setQuitCountdown($uid, $reason) {
$this->accounts->call('setQuitCountdown', $uid, $reason);
}
function cancelQuitCountdown($uid) {
$this->accounts->call('cancelQuitCountdown', $uid);
}
//--------------------------------------------------------------------------------------------------------------------------------
// VACATION MODE MANAGEMENT
//--------------------------------------------------------------------------------------------------------------------------------
function isOnVacation($uid) {
return $this->vacation->call('isOnVacation', $uid);
}
function canSetVacation($uid) {
return $this->vacation->call('canSet', $uid);
}
function setVacationStart($uid) {
return $this->vacation->call('setStart', $uid);
}
function resetVacationStart($uid) {
return $this->vacation->call('resetStart', $uid);
}
function startVacation($uid) {
return $this->vacation->call('start', $uid);
}
function leaveVacation($uid) {
return $this->vacation->call('leave', $uid);
}
}
?>

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;
}
}
?>

View file

@ -0,0 +1,73 @@
<?php
class main_forums_library {
var $index = array(
'deletePost',
'deleteTopic',
'edit',
'get',
'getAdministrator',
'getCategories',
'getCategory',
'getForums',
'getModerator',
'getPost',
'getPosts',
'getTopic',
'getTopics',
'move',
'newTopic',
'reply',
'signature',
'substitute',
'updateLast',
);
function main_forums_library($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function getVersionCategory($ver) {
$q = $this->db->query("SELECT id,description FROM f_category WHERE title='!$ver!'");
return dbFetchHash($q);
}
function isRead($topic, $player) {
$q = $this->db->query("SELECT * FROM f_read WHERE topic=$topic AND reader=$player");
return $q && dbCount($q);
}
function markRead($topic, $player) {
if ($this->isRead($topic,$player)) {
return false;
}
$this->db->query("DELETE FROM f_read WHERE topic=$topic AND reader=$player");
$this->db->query("INSERT INTO f_read(topic,reader)VALUES($topic,$player)");
return true;
}
function markUnread($topic, $player) {
$this->db->query("DELETE FROM f_read WHERE topic=$topic AND reader<>$player");
}
// Get the amount of unread topics in a forum
function getRead($fid, $uid) {
$q = $this->db->query("SELECT COUNT(*) FROM f_read r,f_topic t WHERE t.id=r.topic AND t.forum=$fid AND r.reader=$uid AND t.deleted IS NULL");
list($nr) = dbFetchArray($q);
return $nr;
}
function switchSticky($forum, $topic) {
$this->db->query("UPDATE f_topic SET sticky=NOT sticky WHERE id=$topic AND forum=$forum AND deleted IS NULL");
}
function markForumRead($fid, $uid) {
$q = $this->db->query("SELECT id FROM f_topic WHERE forum=$fid AND deleted IS NULL");
while ($r = dbFetchArray($q)) {
$this->markRead($r[0], $uid);
}
}
}
?>

View file

@ -0,0 +1,27 @@
<?php
class main_forums_deletePost {
function main_forums_deletePost($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($postId) {
$q = $this->db->query("SELECT forum,topic,reply_to FROM f_post WHERE id=$postId AND deleted IS NULL");
if (!($q && dbCount($q))) {
return;
}
list($fid,$tid,$rtid) = dbFetchArray($q);
$this->db->query("UPDATE f_post SET reply_to=$rtid WHERE reply_to=$postId");
$this->db->query("UPDATE f_forum SET posts=posts-1 WHERE id=$fid");
$this->db->query("UPDATE f_post SET deleted=" . time() . " WHERE id=$postId");
$q = $this->db->query("SELECT id FROM f_post WHERE topic=$tid AND deleted IS NULL ORDER BY moment DESC LIMIT 1");
list($lastid) = dbFetchArray($q);
$this->db->query("UPDATE f_topic SET last_post=$lastid WHERE id=$tid");
$this->lib->call('updateLast', $fid);
}
}
?>

View file

@ -0,0 +1,26 @@
<?php
class main_forums_deleteTopic {
function main_forums_deleteTopic($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($forum, $topic) {
$q = $this->db->query("SELECT COUNT(*) FROM f_post WHERE forum=$forum AND topic=$topic AND deleted IS NULL");
if (!($q && dbCount($q))) {
return;
}
list($np) = dbFetchArray($q);
$tm = time();
$this->db->query("UPDATE f_post SET deleted=$tm WHERE topic=$topic AND forum=$forum");
$this->db->query("UPDATE f_topic SET deleted=$tm WHERE id=$topic");
$this->db->query("UPDATE f_forum SET posts=posts-$np,topics=topics-1 WHERE id=$forum");
$this->db->query("DELETE FROM f_read WHERE topic=$topic");
$this->lib->call('updateLast', $forum);
}
}
?>

View file

@ -0,0 +1,22 @@
<?php
class main_forums_edit {
function main_forums_edit($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($a, $pid, $sub, $txt, $ec, $es) {
$q = $this->db->query("SELECT topic FROM f_post WHERE id=$pid AND deleted IS NULL");
list($tid) = dbFetchArray($q);
$this->lib->call('markUnread', $tid,$a);
$tm = time();
$qs = "UPDATE f_post SET edited=$tm,edited_by=$a,title='".addslashes($sub)."',contents='"
.addslashes($txt)."',enable_code=".dbBool($ec).",enable_smileys="
. dbBool($es) . " WHERE id=$pid AND deleted IS NULL";
return !is_null($this->db->query($qs));
}
}
?>

View file

@ -0,0 +1,28 @@
<?php
class main_forums_get {
function main_forums_get($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($f) {
$q = $this->db->query(
"SELECT f.id AS id,f.title AS title,f.description AS description,"
. "f.user_post AS user_post,f.topics AS ntopics,"
. "c.id AS pid,c.title AS ptitle "
. "FROM f_forum f,f_category c "
. "WHERE f.id=$f AND c.id=f.category"
);
if (!$q||dbCount($q)!=1) {
return null;
}
$a = dbFetchHash($q);
$f['user_post'] = ($f['user_post'] == 't');
return $a;
}
}
?>

View file

@ -0,0 +1,26 @@
<?php
class main_forums_getAdministrator {
function main_forums_getAdministrator($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($uid) {
$q = $this->db->query("SELECT category FROM f_admin WHERE \"user\"=$uid");
$a = array();
while ($r = dbFetchArray($q)) {
if (is_null($r[0])) {
$q = $this->db->query("SELECT id FROM f_category");
$a = array();
} else {
array_push($a, $r[0]);
}
}
return $a;
}
}
?>

View file

@ -0,0 +1,23 @@
<?php
class main_forums_getCategories {
function main_forums_getCategories($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run() {
$q = $this->db->query("SELECT id,title,description FROM f_category WHERE title NOT ILIKE '!%!' ORDER BY corder ASC");
$a = array();
if ($q) {
while ($rs = dbFetchHash($q)) {
array_push($a, $rs);
}
}
return $a;
}
}
?>

View file

@ -0,0 +1,20 @@
<?php
class main_forums_getCategory {
function main_forums_getCategory($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($c) {
$q = $this->db->query("SELECT id,title,description FROM f_category WHERE id=$c");
if (!$q||dbCount($q)!=1) {
return null;
}
return dbFetchHash($q);
}
}
?>

View file

@ -0,0 +1,36 @@
<?php
class main_forums_getForums {
function main_forums_getForums($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($c) {
$q = $this->db->query("SELECT * FROM f_forum WHERE category=$c ORDER BY forder ASC");
$a = array();
if (!$q) {
return $a;
}
while ($rs = dbFetchHash($q)) {
if ($rs['last_post'] != "") {
$q2 = $this->db->query(
"SELECT u.name AS author,p.moment AS moment "
. "FROM f_post p,account u "
. "WHERE p.id=".$rs['last_post']." AND u.id=p.author"
);
$rs['last'] = dbFetchHash($q2);
} else {
$rs['last'] = null;
}
$rs['user_post'] = ($rs['user_post'] == 't');
$rs['admin_only'] = ($rs['admin_only'] == 't');
array_push($a, $rs);
}
return $a;
}
}
?>

View file

@ -0,0 +1,21 @@
<?php
class main_forums_getModerator {
function main_forums_getModerator($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($uid) {
$q = $this->db->query("SELECT forum FROM f_moderator WHERE \"user\"=$uid");
$a = array();
while ($r = dbFetchArray($q)) {
array_push($a, $r[0]);
}
return $a;
}
}
?>

View file

@ -0,0 +1,49 @@
<?php
class main_forums_getPost {
function main_forums_getPost($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->accounts = $this->lib->game->getLib("main/account");
}
function run($pid) {
// Get post data
$q = $this->db->query(
"SELECT p.id AS id,p.title AS title,"
. "t.id AS tid,p2.title AS tname,"
. "f.id AS fid,f.title AS fname,"
. "c.id AS cid,c.title AS cname,"
. "p.author AS uid,u.name AS author,p.reply_to AS reply_to,"
. "p.moment AS moment,p.title AS title,"
. "p.contents AS contents,p.enable_code AS ec,"
. "p.enable_smileys AS es,p.edited AS edited,"
. "p.edited_by AS edited_by "
. "FROM f_topic t,f_post p,f_post p2,f_forum f,f_category c,account u "
. "WHERE p.id=$pid AND t.id=p.topic AND p2.id=t.first_post "
. "AND f.id=p.forum AND c.id=f.category AND u.id=p.author "
. "AND p.deleted IS NULL"
);
if (!$q || dbCount($q) != 1) {
return null;
}
$rv = dbFetchHash($q);
$rv['html'] = $this->lib->call('substitute',
$rv['contents'], $rv['ec'], $rv['es']
);
$rv['html'] .= $this->lib->call('signature', $rv['uid']);
if (!is_null($rv['edited_by'])) {
$rv['edited_by'] = $this->accounts->call('getUserName', $rv['edited_by']);
}
if (preg_match('/^!.*!$/', $rv['cname'])) {
$game = config::getGame(preg_replace('/!/', '', $rv['cname']));
$rv['cname'] = $all[$game->game['site_path']][1];
}
return $rv;
}
}
?>

View file

@ -0,0 +1,117 @@
<?php
class main_forums_getPosts {
function main_forums_getPosts($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->accounts = $this->lib->game->getLib("main/account");
}
function run($tid, $thr, $o, $cnt, $fst) {
$os = $o?"DESC":"ASC";
$posts = array();
if ($thr) {
// Read list of IDs
$q = $this->db->query(
"SELECT id,reply_to FROM f_post WHERE topic=$tid AND deleted IS NULL ORDER BY moment $os"
);
$ids = array();
while ($qr = dbFetchArray($q)) {
array_push($ids, $qr);
}
// Get first post
if ($o) {
$mp = array_pop($ids);
} else {
$mp = array_shift($ids);
}
// Initialize IDs and depths
$sids = array($mp[0]);
$dpth = array(0);
// Create lists
$ist = array($mp[0]);
$cd = 0;
while (count($ids)) {
$od = $cd;
for ($i=0;$i<count($ids)&&$od==$cd;$i++) {
if ($ids[$i][1] != $ist[$cd]) {
continue;
}
array_push($ist, $ids[$i][0]);
array_push($sids, $ids[$i][0]);
array_splice($ids, $i, 1);
array_push($dpth, $cd);
$cd++;
}
if ($cd == $od) {
$cd--;
array_pop($ist);
}
}
$rsids = array_splice($sids, $fst, $cnt);
$q = $this->db->query(
"SELECT p.id AS id,p.author AS uid,u.name AS author,"
. "p.moment AS moment,p.title AS title,"
. "p.contents AS contents,p.enable_code AS ec,"
. "p.enable_smileys AS es,p.edited AS edited,"
. "p.edited_by "
. "FROM f_post p,account u "
. "WHERE p.id IN (".join(',',$rsids).") AND u.id=p.author "
. "AND p.deleted IS NULL"
);
while ($qr = dbFetchHash($q)) {
$qr['mine'] = ($qr['uid'] == $_SESSION['userid']);
$qr['contents'] = $this->lib->call('substitute',
$qr['contents'], $qr['ec'], $qr['es']
);
$qr['contents'] .= $this->lib->call('signature', $qr['uid']);
$i = array_search($qr['id'], $rsids);
$qr['depth'] = $dpth[$i+$fst];
if ($qr['depth'] > 19) {
$qr['depth'] = 19;
}
if (!is_null($qr['edited_by'])) {
$qr['edited_by'] = $this->accounts->call('getUserName', $qr['edited_by']);
}
$posts[$i] = $qr;
$i++;
}
} else {
$q = $this->db->query(
"SELECT p.id AS id,p.author AS uid,u.name AS author,"
. "p.moment AS moment,p.title AS title,"
. "p.contents AS contents,p.enable_code AS ec,"
. "p.enable_smileys AS es,p.edited AS edited,"
. "p.edited_by AS edited_by "
. "FROM f_post p,account u "
. "WHERE p.topic=$tid AND u.id=p.author "
. "AND p.deleted IS NULL "
. "ORDER BY p.moment $os "
. "LIMIT $cnt OFFSET $fst"
);
while ($qr = dbFetchHash($q)) {
$qr['mine'] = ($qr['uid'] == $_SESSION['userid']);
$qr['contents'] = $this->lib->call('substitute',
$qr['contents'], $qr['ec'], $qr['es']
);
$qr['contents'] .= $this->lib->call('signature', $qr['uid']);
$qr['depth'] = 0;
if (!is_null($qr['edited_by'])) {
$qr['edited_by'] = $this->accounts->call('getUserName', $qr['edited_by']);
}
array_push($posts, $qr);
}
}
return $posts;
}
}
?>

View file

@ -0,0 +1,36 @@
<?php
class main_forums_getTopic {
function main_forums_getTopic($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($tid) {
// Get main topic data
$q = $this->db->query(
"SELECT t.id AS id,p.title AS title,"
. "p.id AS fpid,t.last_post AS lpid,"
. "f.id AS fid,f.title AS fname,"
. "c.id AS cid,c.title AS cname "
. "FROM f_topic t,f_post p,f_forum f,f_category c "
. "WHERE t.id=$tid AND p.id=t.first_post "
. "AND f.id=t.forum AND c.id=f.category "
. "AND t.deleted IS NULL"
);
if (!$q || dbCount($q) != 1) {
return null;
}
$rv = dbFetchHash($q);
// Get post count
$q = $this->db->query("SELECT COUNT(*) FROM f_post WHERE topic=$tid AND deleted IS NULL");
list($rv["nitems"]) = dbFetchArray($q);
return $rv;
}
}
?>

View file

@ -0,0 +1,33 @@
<?php
class main_forums_getTopics {
function main_forums_getTopics($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($f, $first, $count) {
$q = $this->db->query(
"SELECT t.id AS id,p.title AS title,p.moment AS moment,"
. "u.name AS author,p2.moment AS last_moment,"
. "u2.name AS last_author,t.sticky AS sticky "
. "FROM f_topic t,f_post p,account u,f_post p2,account u2 "
. "WHERE t.forum=$f AND p.id=t.first_post AND u.id=p.author "
. "AND p2.id=t.last_post AND u2.id=p2.author "
. "AND t.deleted IS NULL "
. "ORDER BY sticky DESC,last_moment DESC LIMIT $count OFFSET $first"
);
$a = array();
while ($q && $rs = dbFetchHash($q)) {
$q2 = $this->db->query("SELECT COUNT(*) - 1 FROM f_post WHERE topic={$rs["id"]} AND deleted IS NULL");
list($rs['posts']) = dbFetchArray($q2);
$rs['sticky'] = ($rs['sticky'] == 't');
array_push($a, $rs);
}
return $a;
}
}
?>

View file

@ -0,0 +1,29 @@
<?php
class main_forums_move {
function main_forums_move($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($forum, $topic, $dest, $user) {
$this->db->query("SELECT * FROM f_forum WHERE id IN ($forum,$dest) FOR UPDATE");
$q = $this->db->query("SELECT COUNT(*) FROM f_post WHERE forum=$forum AND topic=$topic AND deleted IS NULL");
if (!($q && dbCount($q))) {
return;
}
list($np) = dbFetchArray($q);
$this->db->query("UPDATE f_post SET forum=$dest WHERE topic=$topic AND forum=$forum");
$this->db->query("UPDATE f_topic SET forum=$dest WHERE id=$topic AND forum=$forum");
$this->db->query("UPDATE f_forum SET posts=posts-$np,topics=topics-1 WHERE id=$forum");
$this->db->query("UPDATE f_forum SET posts=posts+$np,topics=topics+1 WHERE id=$dest");
$this->lib->call('markUnread', $topic, $user);
$this->lib->call('updateLast', $forum);
$this->lib->call('updateLast', $dest);
}
}
?>

View file

@ -0,0 +1,41 @@
<?php
class main_forums_newTopic {
function main_forums_newTopic($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($a, $fid, $sub, $txt, $ec, $es, $st) {
$tm = time();
$qs = "INSERT INTO f_post(forum,author,moment,title,contents,enable_code,enable_smileys) VALUES ("
. "$fid,$a,$tm,'".addslashes($sub)."','".addslashes($txt)."',"
. dbBool($ec) . "," . dbBool($es) . ")";
if (!$this->db->query($qs)) {
return false;
}
$q = $this->db->query("SELECT id FROM f_post WHERE forum=$fid AND topic IS NULL AND author=$a AND moment=$tm ORDER BY id DESC LIMIT 1");
if (!$q || dbCount($q) != 1) {
return false;
}
list($pid) = dbFetchArray($q);
$this->db->query("INSERT INTO f_topic(forum,first_post,last_post,sticky) VALUES($fid,$pid,$pid,"
. dbBool($st) . ")");
$q = $this->db->query("SELECT id FROM f_topic WHERE forum=$fid AND first_post=$pid");
if (!$q || dbCount($q) != 1) {
return false;
}
list($tid) = dbFetchArray($q);
$this->db->query("UPDATE f_post SET topic=$tid WHERE id=$pid");
$this->db->query("UPDATE f_forum SET topics=topics+1,posts=posts+1,last_post=$pid WHERE id=$fid");
$this->lib->call('markRead', $tid, $a);
return $pid;
}
}
?>

View file

@ -0,0 +1,33 @@
<?php
class main_forums_reply {
function main_forums_reply($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($a, $post, $sub, $txt, $ec, $es) {
$tm = time();
$fid = $post['fid']; $tid = $post['tid']; $pid = $post['id'];
$qs = "INSERT INTO f_post(forum,topic,reply_to,author,moment,title,contents,enable_code,enable_smileys) VALUES ("
. "$fid,$tid,$pid,$a,$tm,'".addslashes($sub)."','".addslashes($txt)."',"
. dbBool($ec) . "," . dbBool($es) . ")";
if (!$this->db->query($qs)) {
return false;
}
$q = $this->db->query("SELECT id FROM f_post WHERE topic=$tid AND reply_to=$pid AND author=$a AND moment=$tm ORDER BY id DESC LIMIT 1");
if (!$q || dbCount($q) != 1) {
return false;
}
list($pid) = dbFetchArray($q);
$this->db->query("UPDATE f_topic SET last_post=$pid WHERE id=$tid");
$this->db->query("UPDATE f_forum SET posts=posts+1,last_post=$pid WHERE id=$fid");
$this->lib->call('markUnread', $tid,$a);
return $pid;
}
}
?>

View file

@ -0,0 +1,39 @@
<?php
class main_forums_signature {
var $signatures = array();
function main_forums_signature($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($u) {
if (is_array($this->signatures)) {
if ($this->signatures[$u] != "")
return $this->signatures[$u];
} else {
$this->signatures = array();
}
$q = $this->db->query(
"SELECT id,value FROM user_preferences "
. "WHERE account IN (0,$u) "
. "AND id IN ('forums_sig','forum_code','smileys') "
. "AND version='main' "
. "ORDER BY account"
);
$p = array();
while ($r = dbFetchArray($q)) {
$p[$r[0]] = $r[1];
}
if ($p["forums_sig"] == "") {
$s = "";
} else {
$s = "<div class='fsig'><hr/>" . $this->lib->call('substitute', $p['forums_sig'], $p['forum_code'] ? 't' : 'f', $p['smileys'] ? 't' : 'f') . "</div>";
}
return ($this->signatures[$u] = $s);
}
}
?>

View file

@ -0,0 +1,60 @@
<?php
class main_forums_substitute {
var $code = null;
var $smiley = null;
function main_forums_substitute($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($text, $ec, $es) {
$src = array('/\\n/','/\\r/'); $dst = array("<br/>",'');
$text = utf8entities($text, ENT_NOQUOTES);
$ec = ($ec == 't');
$es = ($es == 't');
if ($ec) {
if (is_array($this->code)) {
foreach ($this->code as $s => $d) {
array_push($src, '/'.$s.'/i');
array_push($dst, $d);
}
} else {
$this->codes = array();
$q = $this->db->query("SELECT * FROM f_code");
while ($r = dbFetchArray($q)) {
$this->code[$r[0]] = $r[1];
array_push($src, '/'.$r[0].'/i');
array_push($dst, $r[1]);
}
}
}
if ($es) {
if (is_array($this->smiley)) {
foreach ($this->smiley as $s => $d) {
array_push($src, '/'.$s.'/i');
array_push($dst, $d);
}
} else {
$this->smiley = array();
$q = $this->db->query("SELECT * FROM f_smiley");
while ($r = dbFetchArray($q)) {
$fn = getStatic("main/pics/smiles/icon_".$r[1].".gif");
if (is_null($fn)) {
continue;
}
$code = "<img src='$fn' alt='[S]' />";
$this->smiley[$r[0]] = $code;
array_push($src, '/'.$r[0].'/i');
array_push($dst, $code);
}
}
}
return preg_replace($src, $dst, $text);
}
}
?>

View file

@ -0,0 +1,21 @@
<?php
class main_forums_updateLast {
function main_forums_updateLast($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($forum) {
$q = $this->db->query("SELECT id FROM f_post WHERE forum=$forum AND deleted IS NULL ORDER BY moment DESC LIMIT 1");
if (!($q && dbCount($q))) {
return;
}
list($id) = dbFetchArray($q);
$this->db->query("UPDATE f_forum SET last_post=$id WHERE id=$forum");
}
}
?>

View file

@ -0,0 +1,37 @@
<?php
class main_library {
var $index = array(
'getTick',
'getTicks',
'isGameRunning',
'preJoin',
'requestGenPlanets',
'sendMail'
);
function main_library($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function isValidAddress($mAddr) {
return preg_match(
'/^[A-Za-z0-9_\.\-\+]+@([A-Za-z0-9_\.\-\+]+)+\.[A-Za-z]{2,6}/',
$mAddr
);
}
function isValidName($uName) {
$test = array('/[^A-Za-z0-9_\.\-\+@\/'."'".' ]/', '/\s\s+/', '/^\s/', '/\s$/');
$r = true;
for ($i=0;$r&&$i<count($test);$i++) {
$r &= !preg_match($test[$i], $uName);
}
return $r;
}
}
?>

View file

@ -0,0 +1,32 @@
<?php
class main_getTick {
function main_getTick($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($name, $lang = null) {
$tInst = $this->lib->game->ticks[$name];
if (is_null($tInst)) {
return null;
}
$tick = array(
"game" => $tInst->definition->public,
"first" => $tInst->first,
"last" => $tInst->last,
"interval" => $tInst->interval
);
if (!is_null($lang)) {
$tick['name'] = $tInst->definition->getName($lang);
$tick['description'] = $tInst->definition->getDescription($lang);
}
return $tick;
}
}
?>

View file

@ -0,0 +1,31 @@
<?php
class main_getTicks {
function main_getTicks($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($lang = null) {
$ticks = array();
foreach ($this->lib->game->ticks as $script => $tInst) {
$tick = array(
"script" => $script,
"game" => $tInst->definition->public,
"first" => $tInst->first,
"last" => $tInst->last,
"interval" => $tInst->interval
);
if (!is_null($lang)) {
$tick['name'] = $tInst->definition->getName($lang);
$tick['description'] = $tInst->definition->getDescription($lang);
}
array_push($ticks, $tick);
}
return $ticks;
}
}
?>

View file

@ -0,0 +1,31 @@
<?php
class main_isGameRunning {
function main_isGameRunning($lib) {
$this->lib = $lib;
}
function run($game) {
$g = config::getGame($game);
if (!$g) {
return false;
}
$lib = $g->getLib();
if ($lib->call('isFinished')) {
return false;
}
$now = time();
foreach ($g->ticks as $td => $tick) {
if ($tick->first <= $now && $tick->definition->public) {
return true;
}
}
return false;
}
}
?>

View file

@ -0,0 +1,49 @@
<?php
//-----------------------------------------------------------------------
// LegacyWorlds Beta 5
// Game libraries
//
// main/library/preJoin.inc
//
// This function lets a player who had registered into the game
//
// Copyright(C) 2004-2008, DeepClone Development
//-----------------------------------------------------------------------
class main_preJoin {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
public function run($account) {
// Get the game's ID
$q = $this->db->query("SELECT game FROM reg_queue WHERE account = $account");
if (!($q && dbCount($q))) {
$this->db->end(false);
return null;
}
list($gameID) = dbFetchArray($q);
// Get the game library on call its preJoin() function
$game = config::getGame($gameID);
if (!$game) {
$this->db->end(false);
return null;
}
if (!$game->getLib()->call('preJoin', $account)) {
return null;
}
// Delete the queue entry
$this->db->query("DELETE FROM reg_queue WHERE account = $account");
return $gameID;
}
}
?>

View file

@ -0,0 +1,29 @@
<?php
class main_requestGenPlanets {
function main_requestGenPlanets($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($first, $amount) {
$dir = config::getParam('pgenreq');
$game = $this->lib->game->name;
if (!is_dir($dir)) {
$base = dirname($dir);
if (!(is_dir($base) && is_writable($base) && @mkdir($dir))) {
logText("main::requestGenPlanets($game,$first,$amount): unable to create directory '$dir'", LOG_WARNING);
return false;
}
}
$r = @touch("$dir/req-$game-$first-$amount");
if (!$r) {
logText("main::requestGenPlanets($game,$first,$amount): unable to create file 'req-$game-$first-$amount'", LOG_WARNING);
}
return $r;
}
}
?>

View file

@ -0,0 +1,48 @@
<?php
class main_sendMail {
function main_sendMail($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($file, $to, $vars) {
if (! config::$main['sendmails']) {
return true;
}
// Read the file
$mail = @file_get_contents("{$this->lib->game->dir}/mail/$file");
if (is_bool($mail)) {
logText("Mail '$file' to $to failed: file not found", LOG_WARNING);
return false;
}
// Generate the substitution arrays from the variables
$vals = array_values($vars);
$subst = array();
foreach (array_keys($vars) as $name) {
array_push($subst, "/_{$name}_/");
}
$mail = preg_replace($subst, $vals, $mail);
$tmp = explode("\n", $mail);
$subject = array_shift($tmp);
$mail = join("\n", $tmp);
$header = "From: webmaster@legacyworlds.com\r\n"
. "Reply-To: webmaster@legacyworlds.com\r\n"
. "X-Mailer: LegacyWorlds\r\n"
. "Mime-Version: 1.0\r\n"
. "Content-Type: text/plain; charset=iso-8859-1";
if (!mail($to, $subject, $mail, $header)) {
logText("Mail '$file' to $to failed: unable to send", LOG_WARNING);
return false;
}
return true;
}
}
?>

View file

@ -0,0 +1,212 @@
<?php
class main_links_library {
var $index = array();
function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function getCategories() {
$q = $this->db->query("SELECT * FROM lk_category ORDER BY position");
if (!$q) {
return array();
}
$res = array();
while ($r = dbFetchHash($q)) {
array_push($res, $r);
}
return $res;
}
function getCategory($id) {
$q = $this->db->query("SELECT * FROM lk_category WHERE id=$id");
if (!($q && count($q))) {
return null;
}
return dbFetchHash($q);
}
function checkCategoryName($name, $id) {
$qs = is_null($id) ? "" : " AND id<>$id";
$q = $this->db->query("SELECT * FROM lk_category WHERE title='" . addslashes($name) . "'$qs");
return (dbCount($q) == 0);
}
function createCategory($title, $description, $after = null) {
$q = $this->db->query("SELECT id,position FROM lk_category ORDER BY position DESC FOR UPDATE");
if (is_null($after)) {
while ($r = dbFetchArray($q)) {
$this->db->query("UPDATE lk_category SET position=position+1 WHERE id={$r[0]}");
}
$pos = 0;
} else {
$act = false;
while ($r = dbFetchArray($q)) {
if ($act) {
$this->db->query("UPDATE lk_category SET position=position+1 WHERE id={$r[0]}");
} else {
$act = ($r[0] == $after);
if ($act) {
$pos = $r[1];
}
}
}
}
$id = $this->db->query("INSERT INTO lk_category(position,title" . (is_null($description) ? "" : ",description")
. ") VALUES ($pos,'" . addslashes($title) . "'" . (is_null($description) ? "" : (",'" . addslashes($description) . "'"))
. ")");
return $id;
}
function changeCategory($id, $title, $description) {
$this->db->query("UPDATE lk_category SET title='" . addslashes($title) . "',description="
. ($description == "" ? "NULL" : ("'" . addslashes($description) . "'"))
. " WHERE id=$id");
}
function deleteCategory($id) {
$this->db->query("SELECT * FROM lk_category FOR UPDATE");
$q = $this->db->query("SELECT position FROM lk_category WHERE id=$id");
if (!($q && count($q))) {
return;
}
list($pos) = dbFetchArray($q);
$this->db->query("DELETE FROM lk_category WHERE id=$id");
$this->db->query("UPDATE lk_category SET position=position-1 WHERE position>$pos");
}
function moveCategory($id, $up) {
$this->db->query("SELECT * FROM lk_category FOR UPDATE");
$q = $this->db->query("SELECT MAX(position)+1 FROM lk_category");
list($mPos) = dbFetchArray($q);
$q = $this->db->query("SELECT position FROM lk_category WHERE id=$id");
list($pos) = dbFetchArray($q);
$this->db->query("UPDATE lk_category SET position=$mPos WHERE id=$id");
if ($up) {
$this->db->query("UPDATE lk_category SET position=position+1 WHERE position=" . ($pos-1));
$this->db->query("UPDATE lk_category SET position=".($pos-1)." WHERE id=$id");
} else {
$this->db->query("UPDATE lk_category SET position=position-1 WHERE position=" . ($pos+1));
$this->db->query("UPDATE lk_category SET position=".($pos+1)." WHERE id=$id");
}
}
function getLinks($category) {
$q = $this->db->query("SELECT * FROM lk_link WHERE category=$category ORDER BY title");
if (!$q) {
return array();
}
$res = array();
while ($r = dbFetchHash($q)) {
array_push($res, $r);
}
return $res;
}
function getLink($id) {
$q = $this->db->query("SELECT * FROM lk_link WHERE id=$id");
if (!($q && count($q))) {
return null;
}
return dbFetchHash($q);
}
function checkLink($url, $id = null) {
$qs = is_null($id) ? "" : " AND id<>$id";
$q = $this->db->query("SELECT * FROM lk_link WHERE url='".addslashes($url) . "'$qs");
return (dbCount($q) == 0);
}
function addLink($category, $title, $description, $url) {
return $this->db->query("INSERT INTO lk_link(category,url,title" . (is_null($description) ? "" : ",description")
. ") VALUES ($category,'" . addslashes($url) . "','" . addslashes($title) . "'"
. (is_null($description) ? "" : (",'" . addslashes($description) . "'")) . ")");
}
function changeLink($id, $title, $url, $description) {
$this->db->query("UPDATE lk_link SET title='" . addslashes($title) . "',description="
. ($description == "" ? "NULL" : ("'" . addslashes($description) . "'"))
. ",url='" . addslashes($url) . "' WHERE id=$id");
}
function deleteLink($id) {
$this->db->query("DELETE FROM lk_link WHERE id=$id");
}
function getBrokenReports() {
$q = $this->db->query("SELECT * FROM lk_broken");
if (!$q) {
return array();
}
$res = array();
while ($r = dbFetchHash($q)) {
array_push($res, $r);
}
return $res;
}
function reportBroken($link, $account) {
$this->db->query("INSERT INTO lk_broken VALUES($link, $account)");
}
function deleteReports($link) {
$this->db->query("DELETE FROM lk_broken WHERE link=$link");
}
function getSubmissions() {
$q = $this->db->query("SELECT * FROM lk_submitted");
if (!$q) {
return array();
}
$res = array();
while ($r = dbFetchHash($q)) {
array_push($res, $r);
}
return $res;
}
function getSubmission($url, $account) {
$q = $this->db->query("SELECT * FROM lk_submitted WHERE url='" . addslashes($url)
. "' AND submitted_by=$account");
if (!($q && dbCount($q))) {
return null;
}
return dbFetchHash($q);
}
function checkSubmission($url, $account) {
$q = $this->db->query("SELECT * FROM lk_submitted WHERE url='" . addslashes($url)
. "' AND submitted_by=$account");
return (dbCount($q) == 0);
}
function submitLink($url, $title, $description, $account) {
$this->db->query("INSERT INTO lk_submitted VALUES('" . addslashes($url) . "',$account,'" . addslashes($title)
. "'," . (is_null($description) ? "NULL" : ("'" . addslashes($description) . "'")) . ")");
}
function deleteSubmissions($url) {
$this->db->query("DELETE FROM lk_submitted WHERE url='" . addslashes($url) . "'");
}
}
?>

View file

@ -0,0 +1,11 @@
Legacy Worlds > Lost password
Dear _USER_,
You have requested your password on the Legacy Worlds web site to be changed. You will need to enter an authentication code before the site actually changes your password to a new, random password and sends you another mail containing the new password.
The confirmation code is _CODE_
If you didn't request this, you can safely ignore this message.
Best regards,
The Legacy Worlds staff

View file

@ -0,0 +1,11 @@
Legacy Worlds > Password changed
Dear _USER_,
You requested your password to be changed on the Legacy Worlds web site. This request has been accepted after you entered your confirmation code.
Your new password is: _PASS_
You should change it just after you log on to Legacy Worlds.
Best regards,
The Legacy Worlds staff

View file

@ -0,0 +1,13 @@
Legacy Worlds account disabled
Dear _USER_,
This email is being sent to inform you that your account on Legacy Worlds has
been disabled. It had been inactive for 28 days.
You can reactivate your account by logging in to the Legacy Worlds web site
using your usual user name and password.
We hope to see you again very soon on Legacy Worlds.
Best regards,
The Legacy Worlds staff

View file

@ -0,0 +1,13 @@
Legacy Worlds account closed
Dear _USER_,
This email is being sent to confirm that your account has been closed as
you requested.
You can reactivate your account by logging in to the Legacy Worlds web site
using your usual user name and password.
We hope to see you again sometime on Legacy Worlds.
Best regards,
The Legacy Worlds staff

View file

@ -0,0 +1,17 @@
Legacy Worlds registration
Dear _USER_,
Thank you for registering at Legacy Worlds!
Before we activate your account, there's just one more step to complete your registration.
You have to connect to the site using the username and password you chose, then validate your account using the confirmation code below.
Please note that this confirmation code is NOT your password, and you will only need to use it once.
Your Username is: _USER_
Your Password is: _PASS_
Your Confirmation Code is: _CCODE_
If you are still having problems signing up, please contact a member of our support staff at webmaster@legacyworlds.com
Thanks!
The Legacy Worlds staff

View file

@ -0,0 +1,14 @@
Legacy Worlds registration
Dear _USER_,
Thank you for coming back to Legacy Worlds!
There's one step you must complete before your account is re-activated.
You have to connect to the site again using your username and password, then re-validate your account using the confirmation code below.
Your Confirmation Code is: _CCODE_
If you encouter problems during the process, please contact a member of our support staff at staff@legacyworlds.com
Thanks!
The Legacy Worlds staff

View file

@ -0,0 +1,11 @@
Your account on Legacy Worlds
Dear _USER_,
This email is being sent to warn you that your account on Legacy Worlds has
been inactive for three weeks. Unless you connect in the coming week, it will
be closed and your current games will be lost.
We hope to see you again on Legacy Worlds very soon.
Best regards,
The Legacy Worlds staff

View file

@ -0,0 +1,66 @@
<?php
class main_manual_library {
var $index = array(
'getFirstPage',
'getNavLinks',
'getPageId',
'getSectionsIn',
'getStructure',
'readXMLFile',
'search',
'updateSections'
);
function main_manual_library($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->version = $this->lib->game->version->id;
}
function getPage($pageId) {
$section = $this->readSectionRecord($pageId);
$tree = $this->lib->call('getSectionsIn', $pageId, 0);
$this->readSubSections($pageId, $section, $tree);
return $section;
}
function getSectionId($lang, $name) {
$q = $this->db->query("SELECT id FROM man_section WHERE version='{$this->version}' AND lang='$lang' AND name='" . addslashes($name) . "'");
if (!($q && dbCount($q) == 1)) {
return null;
}
list($secId) = dbFetchArray($q);
return $secId;
}
function readSectionRecord($sectionId) {
$q = dbQuery("SELECT name,title,contents,link_to FROM man_section WHERE id=$sectionId");
if (!($q && dbCount($q) == 1)) {
return null;
}
list($name,$title,$contents,$linkto) = dbFetchArray($q);
return array(
"id" => $sectionId,
"name" => $name,
"title" => $title,
"contents" => $contents,
"linkto" => $linkto,
"subsections" => array()
);
}
function readSubSections($sectionId, &$section, &$subList) {
foreach ($subList as $secId => $sData) {
$nSec = $this->readSectionRecord($secId);
$this->readSubSections($secId, $nSec, $subList[$secId]['subs']);
array_push($section['subsections'], $nSec);
}
}
}
?>

View file

@ -0,0 +1,30 @@
<?php
class main_manual_getFirstPage {
function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->version = $this->lib->mainClass->version;
}
function run($lang) {
// Get the manual's root node identifier
$q = $this->db->query("SELECT id FROM man_section WHERE version='{$this->version}' AND lang='$lang' AND in_section IS NULL");
if (!($q && dbCount($q) == 1)) {
return null;
}
list($rootId) = dbFetchArray($q);
// Get the root node's first page
$q = $this->db->query("SELECT id FROM man_section WHERE in_section=$rootId AND after_section=$rootId AND is_page");
if (!($q && dbCount($q) == 1)) {
return null;
}
list($fPage) = dbFetchArray($q);
return $fPage;
}
}
?>

View file

@ -0,0 +1,67 @@
<?php
class main_manual_getNavLinks {
function main_manual_getNavLinks($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($pageId) {
// Get the version, language and parent section for the page
$q = $this->db->query("SELECT version,lang,in_section,after_section FROM man_section WHERE id=$pageId");
if (!($q && dbCount($q) == 1)) {
return null;
}
list($version,$lang,$parent,$after) = dbFetchArray($q);
// Get the manual's root node identifier
$q = $this->db->query("SELECT id FROM man_section WHERE version='$version' AND lang='$lang' AND in_section IS NULL");
if (!($q && dbCount($q) == 1)) {
return null;
}
list($rootId) = dbFetchArray($q);
// Get the root node's first page
$q = $this->db->query("SELECT id FROM man_section WHERE in_section=$rootId AND after_section=$rootId AND is_page");
if (!($q && dbCount($q) == 1)) {
return null;
}
list($fPage) = dbFetchArray($q);
// Get identifiers
$rv = array($pageId == $fPage ? null : $fPage);
$rv[1] = ($parent == $rootId) ? (is_null($rv[0]) ? null : $rv[0]) : $parent;
$rv[2] = ($after == $rootId) ? null : $after;
$q = $this->db->query("SELECT id FROM man_section WHERE is_page AND after_section=$pageId");
if ($q && dbCount($q) == 1) {
list($rv[3]) = dbFetchArray($q);
} else {
$rv[3] = null;
}
// Get names
$nnl = array();
for ($i=0;$i<4;$i++) {
if (!is_null($rv[$i])) {
array_push($nnl, $rv[$i]);
}
}
if (count($nnl)) {
$q = $this->db->query("SELECT id,name FROM man_section WHERE id IN (" . join(',', array_unique($nnl)) . ")");
$l = array();
while ($r = dbFetchArray($q)) {
$l[$r[0]] = $r[1];
}
for ($i=0;$i<4;$i++) {
if (!is_null($rv[$i])) {
$rv[$i] = $l[$rv[$i]];
}
}
}
return $rv;
}
}
?>

View file

@ -0,0 +1,31 @@
<?php
class main_manual_getPageId {
function main_manual_getPageId($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($secId) {
// Find the section itself
$q = $this->db->query("SELECT is_page,in_section FROM man_section WHERE id=$secId");
if (!($q && dbCount($q) == 1)) {
return null;
}
list($isPage, $parentId) = dbFetchArray($q);
// Find the page in which the section is
while ($isPage != 't') {
$q = $this->db->query("SELECT id,is_page,in_section FROM man_section WHERE id=$parentId");
if (!($q && dbCount($q) == 1)) {
return null;
}
list($secId, $isPage, $parentId) = dbFetchArray($q);
}
return $secId;
}
}
?>

View file

@ -0,0 +1,35 @@
<?php
class main_manual_getSectionsIn {
function main_manual_getSectionsIn($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($rootId, $type = 1) {
$qs = ($type > 0) ? (" AND is_page" . ($type == 2 ? " AND in_menu" : "")) : " AND NOT is_page";
$q = $this->db->query("SELECT id,name,title,after_section FROM man_section WHERE in_section=$rootId$qs");
$sl = array();
$as = array();
while ($r = dbFetchArray($q)) {
$sl[$r[0]] = array(
"name" => $r[1],
"title" => $r[2]
);
$as[$r[3]] = $r[0];
}
$cid = $rootId;
$nl = array();
while (!is_null($as[$cid])) {
$cid = $as[$cid];
$nl[$cid] = $sl[$cid];
$nl[$cid]['subs'] = $this->run($cid, $type);
}
return $nl;
}
}
?>

View file

@ -0,0 +1,24 @@
<?php
class main_manual_getStructure {
function main_manual_getStructure($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->version = $this->lib->mainClass->version;
}
function run($lang) {
// Get the manual's root node identifier
$q = $this->db->query("SELECT id FROM man_section WHERE version='{$this->version}' AND lang='$lang' AND in_section IS NULL");
if (!($q && dbCount($q) == 1)) {
return array();
}
list($rootId) = dbFetchArray($q);
// List sections
return $this->lib->call('getSectionsIn', $rootId, 2);
}
}
?>

View file

@ -0,0 +1,158 @@
<?php
class main_manual_readXMLFile {
function main_manual_readXMLFile($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($path) {
logText("manual/readXMLFile: extracting XML file '$path'", LOG_DEBUG);
// Get the section's name from the path
$section = basename($path, ".lwdoc");
// Open the file
$file = @file_get_contents($path);
if ($file === FALSE) {
logText("manual/readXMLFile: failed to read file '$path'", LOG_DEBUG);
return 1;
}
$file = utf8_encode($file);
// Parse it
$doc = new DOMDocument();
if (!$doc->loadXML($file)) {
logText("manual/readXMLFile: failed to parse file '$path'", LOG_DEBUG);
return 2;
}
$root = $doc->documentElement;
$node = $root->firstChild;
$version = $title = $lang = "";
// Extract the version, language and title
for ($i=0;$i<3;$i++) {
while ($node && $node->nodeType != XML_ELEMENT_NODE) {
$node = $node->nextSibling;
}
$name = $node->nodeName;
$val = $node->textContent;
if ($name == "version") {
$version = $val;
} elseif ($name == "language") {
$language = $val;
} elseif ($name == "title") {
$title = $val;
} else {
logText("manual/readXMLFile('$path'): unexpected tag '$name'", LOG_DEBUG);
return 3;
}
$node = $node->nextSibling;
}
if (!$node || $version == "" || $language == "" || $title == "") {
logText("manual/readXMLFile('$path'): incomplete file header", LOG_DEBUG);
return 4;
}
// Generate the main section
$sections = array(
$section => array(
"language" => $language,
"version" => $version,
"in_section" => null,
"is_page" => true,
"in_menu" => ($root->getAttribute('hide') !== "1"),
"title" => $title,
"contents" => "",
"subs" => array()
)
);
// Read the sections
$stack = array();
while ($node) {
$cs = empty($stack) ? $section : $stack[count($stack)-1][1];
if ($node->nodeType == XML_ELEMENT_NODE && $node->nodeName == "section") {
$nFile = $node->getAttribute('file');
if ($nFile != '') {
// This subsection must be read from another file
if ($nFile{0} != "/")
$nFile = dirname($path) . "/$nFile";
$a = $this->run($nFile);
if (!is_array($a)) {
logText("manual/readXMLFile('$path'): error $a in sub-file '$nFile'", LOG_DEBUG);
return $a;
}
$id = basename($nFile, '.lwdoc');
$a[$id]['in_section'] = $cs;
$sections = array_merge($sections, $a);
array_push($sections[$cs]['subs'], $id);
$node = $node->nextSibling;
} else {
// Inline subsection
$id = $node->getAttribute('id');
array_push($sections[$cs]['subs'], $id);
$sections[$id] = array(
"language" => $language,
"version" => $version,
"in_section" => $cs,
"is_page" => false,
"title" => $node->getAttribute('title'),
"linkto" => $node->getAttribute('linkto'),
"contents" => $this->getEmbeddedXHTML($node),
"subs" => array()
);
array_push($stack, array($node, $id));
$node = $node->firstChild;
}
} else {
$node = $node->nextSibling;
if (!empty($stack)) {
do {
if (!$node) {
$x = array_pop($stack);
$node = $x[0]->nextSibling;
}
} while (!$node && !empty($stack));
}
}
}
return $sections;
}
function getEmbeddedXHTML($node) {
if (!$node->hasChildNodes()) {
return $node->textContent;
}
$st = "";
for ($i=0;$i<$node->childNodes->length;$i++) {
$cnode = $node->childNodes->item($i);
if ($cnode->nodeType == XML_TEXT_NODE) {
$st .= $cnode->nodeValue;
} elseif ($cnode->nodeType == XML_ELEMENT_NODE && $cnode->nodeName != 'section') {
$st .= "<" . $cnode->nodeName;
if ($attribnodes = $cnode->attributes) {
$st .= " ";
foreach ($attribnodes as $anode) {
$st .= $anode->nodeName . "='" . $anode->nodeValue . "'";
}
}
$nodeText = $this->getEmbeddedXHTML($cnode);
if (empty($nodeText) && !$attribnodes) {
$st .= " />"; // unary
} else {
$st .= ">" . $nodeText . "</" . $cnode->nodeName . ">";
}
}
}
return $st;
}
}
?>

View file

@ -0,0 +1,229 @@
<?php
class main_manual_search {
function main_manual_search($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($text, $lang) {
$version = "'{$this->lib->mainClass->version}'";
$text = preg_replace('/\s+/', ' ', trim($text));
// Read the banned words list
$banList = array();
$q = $this->db->query("SELECT word FROM man_index_ban WHERE lang='$lang'");
while ($r = dbFetchArray($q)) {
array_push($banList, $r[0]);
}
// Generate the three lists: required, excluded and normal words
$required = $excluded = $normal = array();
$tl = explode(' ', $text);
foreach ($tl as $word) {
if ($word{0} == '+' || $word{0} == '-') {
$qual = $word{0};
$word = substr($word, 1);
} else {
$qual = '';
}
$word = preg_replace('/["\s\.,;:!\(\)<>&]+/', ' ', $word);
$rwl = explode(' ', $word);
foreach ($rwl as $rword) {
$rword = strtolower($rword);
if (in_array($rword, $banList)) {
continue;
}
$rword = addslashes($rword);
if ($qual == '') {
array_push($normal, $rword);
} elseif ($qual == '+') {
array_push($required, $rword);
} else {
array_push($excluded, $rword);
}
}
}
$required = array_unique($required);
$excluded = array_unique($excluded);
$normal = array_unique($normal);
// Read the index for each list
$words = array($required, $normal, $excluded);
$lists = array(array(), array(), array());
for ($i=0;$i<count($words);$i++) {
if (empty($words[$i])) {
continue;
}
$q = $this->db->query("SELECT i.word,i.wcount,i.section"
. " FROM man_index i,man_section s "
. "WHERE i.lang='$lang' AND s.id=i.section AND s.version=$version "
. "AND word in ('" . join("','", $words[$i]) . "')");
while ($r = dbFetchArray($q)) {
if (!is_array($lists[$i][$r[0]])) {
$lists[$i][$r[0]] = array();
}
$lists[$i][$r[0]][$r[2]] = $r[1];
}
}
if (count($words[0])) {
// Look for sections that have all required words
$secReq = array();
foreach ($required as $word) {
if (!is_array($lists[0][$word])) {
continue;
}
foreach (array_keys($lists[0][$word]) as $s) {
$secReq[$s] ++;
}
}
$secOk = array();
foreach ($secReq as $s => $n) {
if ($n == count($words[0]) && !in_array($s, $secOk)) {
array_push($secOk, $s);
}
}
} else {
// Look for sections that have these words
$secOk = array();
if (count($words[1])) {
foreach ($lists[1] as $word => $sections) {
foreach (array_keys($sections) as $section) {
if (!in_array($section, $secOk)) {
array_push($secOk, $section);
}
}
}
}
}
// Remove sections that have excluded words
if (count($words[2])) {
// Generate a list of banned sections
$bannedSections = array();
foreach ($lists[2] as $word => $sections) {
foreach ($sections as $s => $c) {
if (!in_array($s, $bannedSections)) {
array_push($bannedSections, $s);
}
}
}
// Remove banned sections from the list
$newList = array();
foreach ($secOk as $s) {
if (!in_array($s, $bannedSections)) {
array_push($newList, $s);
}
}
$secOk = $newList;
}
/*
* For each section in the generated list, find out:
* - how many occurences of each required or normal word it
* has,
* - the maximum amount of occurences a section has of each
* required or normal word.
*/
// Initialise the word list
$sWords = array_unique(array_merge($words[0], $words[1]));
$mWords = array();
foreach ($sWords as $word) {
$mWords[$word] = 0;
}
// Generate the section list and update maximum counts
$wPerSec = array();
foreach ($secOk as $section) {
$wPerSec[$section] = array();
foreach ($sWords as $word) {
$count = $lists[is_null($lists[0][$word]) ? 1 : 0][$word][$section];
if (!is_null($count)) {
$wPerSec[$section][$word] = $count;
if ($count > $mWords[$word]) {
$mWords[$word] = $count;
}
} else {
$wPerSec[$section][$word] = 0;
}
}
}
/* Now we assign a "distance" from the optimal value to
* each section by using each word as a dimension; the closer
* a section to the optimal value, the higher it will be in
* the displayed results.
*/
// Eliminate words that are not on any section
$nWords = array();
foreach ($sWords as $word) {
if ($mWords[$word] > 0) {
array_push($nWords, $word);
}
}
// Compute the maximum distance
$sum = 0;
foreach ($nWords as $word) {
$d = $mWords[$word];
$sum += $d * $d;
}
$dMax = sqrt($sum);
// Compute the revelance of each section
$dSec = array();
foreach ($secOk as $section) {
$sum = 0;
foreach ($nWords as $word) {
$d = $mWords[$word] - $wPerSec[$section][$word];
$sum += $d * $d;
}
$dSec[$section] = 1 - (sqrt($sum) / $dMax);
}
// Find out the page for each section
$pages = array();
$nSecs = array();
foreach ($secOk as $section) {
$pageId = $this->lib->call('getPageId', $section);
if (is_null($pageId)) {
continue;
}
$pages[$pageId] += $dSec[$section];
$nSecs[$pageId] += 1;
}
// Sort the pages according to their points
$pts = array();
foreach (array_keys($pages) as $p) {
$pp = sprintf("%.2f", $pages[$p] / $nSecs[$pageId]);
if (!is_array($pts[$pp])) {
$pts[$pp] = array();
}
array_push($pts[$pp], $p);
}
$k = array_keys($pts);
sort($k);
// Return the results
$results = array();
foreach (array_reverse($k) as $points) {
foreach ($pts[$points] as $p) {
array_push($results, $p);
}
}
return $results;
}
}
?>

View file

@ -0,0 +1,107 @@
<?php
class main_manual_updateSections {
function main_manual_updateSections($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($sections) {
$versions = array();
foreach ($sections as $sName => $sData) {
if (is_null($versions[$sData['version']])) {
$versions[$sData['version']] = "'" . addslashes($sData['version']) . "'";
}
$this->db->query("DELETE FROM man_section WHERE name='" . addslashes($sName)
. "' AND version=" . $versions[$sData['version']]
. " AND lang='" . addslashes($sData['language']) . "'");
}
// Insert the data itself
$now = time();
foreach ($sections as $sName => $sData) {
if (is_null($versions[$sData['version']])) {
continue;
}
$this->db->query("INSERT INTO man_section (version,lang,name,last_update,is_page,in_menu,title,contents) VALUES("
. $versions[$sData['version']] . ",'" . addslashes($sData['language']) . "','"
. addslashes($sName) . "',$now," . dbBool($sData['is_page']) . ","
. dbBool($sData['in_menu']) . ",'" . addslashes($sData['title'])
. "','" . addslashes($sData['contents']) . "')");
$q = $this->db->query("SELECT id FROM man_section WHERE name='" . addslashes($sName)
. "' AND version=" . $versions[$sData['version']]
. " AND lang='" . addslashes($sData['language']) . "'");
list($sections[$sName]['dbid']) = dbFetchArray($q);
}
// Create links between data
foreach ($sections as $sName => $sData) {
if (count($sData['subs'])) {
// In / After section
foreach ($sData['subs'] as $i => $sn) {
$ri = $i - 1;
while ($ri >= 0) {
$sn2 = $sections[$sData['subs'][$ri]];
if ($sn2['is_page'] == $sections[$sn]['is_page']) {
$previous = $sn2['dbid'];
break;
}
$ri --;
}
if ($ri == -1) {
$previous = $sData['dbid'];
}
$this->db->query("UPDATE man_section SET in_section={$sData['dbid']},after_section=$previous WHERE id={$sections[$sn]['dbid']}");
}
}
if ($sData['linkto'] != '') {
if (is_array($sections[$sData['linkto']])) {
$this->db->query("UPDATE man_section SET link_to={$sections[$sData['linkto']]['dbid']} WHERE id={$sData['dbid']}");
} else {
$q = $this->db->query("SELECT id FROM man_section WHERE name='" . addslashes($sData['linkto']) . "' AND version="
. $versions[$sData['version']] . " AND lang='" . addslashes($sData['language']) . "'");
if ($q && dbCount($q) == 1) {
list($toid) = dbFetchArray($q);
$this->db->query("UPDATE man_section SET link_to=$toid WHERE id={$sData['dbid']}");
}
}
}
}
// Update indices
$banwords = array();
foreach ($sections as $sName => $sData) {
if (!is_array($banwords[$sData['language']])) {
$ban = array();
$q = $this->db->query("SELECT word FROM man_index_ban WHERE lang='" . addslashes($sData['language']) . "'");
while ($r = dbFetchArray($q)) {
array_push($ban, $r[0]);
}
$banwords[$sData['lang']] = $ban;
}
$text = preg_replace('/<[^>]+>/', '', $sData['title']) . " ";
$text .= preg_replace('/<[^>]+>/', '', $sData['contents']);
$text = preg_replace('/["\s\.,;:!\(\)<>&]+/', ' ', $text);
$tl = explode(' ', $text);
$rtl = array();
foreach ($tl as $word) {
$word = strtolower($word);
if ($word != '' && !in_array($word, $banwords[$sData['lang']])) {
$rtl[$word] ++;
}
}
foreach ($rtl as $word => $count) {
$this->db->query("INSERT INTO man_index(word,wcount,lang,section) VALUES ('" . addslashes($word)
. "',$count,'" . addslashes($sData['language']) . "',{$sData['dbid']})");
}
}
}
}
?>

View file

@ -0,0 +1,147 @@
<?php
class main_paypal_library {
var $index = array();
function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function newTicket($account) {
$this->db->query("DELETE FROM pp_ticket WHERE UNIX_TIMESTAMP(NOW())-created>2592000");
do {
$v = md5(uniqid(rand()));
$q = "SELECT * FROM pp_ticket WHERE md5_id='$v'";
$qr = $this->db->query($q);
} while ($qr && dbCount($qr));
if (is_null($qr)) {
return null;
}
$this->db->query("INSERT INTO pp_ticket(account,md5_id) VALUES($account,'$v')");
return $v;
}
function getUserContributions($account) {
$q = $this->db->query("SELECT SUM(amount) FROM pp_history WHERE account=$account");
if (!($q && dbCount($q))) {
return 0;
}
list($amount) = dbFetchArray($q);
return $amount;
}
function getTotalContributions() {
$q = $this->db->query("SELECT SUM(amount) FROM pp_history");
if (!($q && dbCount($q))) {
return 0;
}
list($amount) = dbFetchArray($q);
return $amount;
}
function getMonthContributions() {
$q = $this->db->query("SELECT SUM(amount) FROM pp_history WHERE donated>UNIX_TIMESTAMP(NOW())-2592000");
if (!($q && dbCount($q))) {
return 0;
}
list($amount) = dbFetchArray($q);
return $amount;
}
function getUserHistory($account) {
$q = $this->db->query("SELECT donated,amount FROM pp_history WHERE account=$account ORDER BY donated DESC");
if (!($q && dbCount($q))) {
return array();
}
$res = array();
while ($r = dbFetchArray($q)) {
array_push($res, array(
"time" => strftime("%Y-%m-%d %H:%M:%S", $r[0]),
"amount" => number_format($r[1])
));
}
return $res;
}
function checkIPN() {
// Generate the verification request
$ra = array();
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
array_push($ra, "$key=$value");
}
$req = join('&', $ra);
// Connect to the PayPal server
$curl = curl_init("http://www.paypal.com/cgi-bin/webscr?cmd=_notify%2dvalidate&$req");
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$rv = curl_exec($curl);
logText("PAYPAL: CURL request returned '$rv'", LOG_DEBUG);
if (preg_match("/VERIFIED/", $rv)) {
$handled = true;
$ok = true;
} elseif (preg_match("/INVALID/", $rv)) {
$handled = true;
$ok = false;
} else {
logText("PAYPAL: remote server didn't return VERIFIED or INVALID while checking IPN!", LOG_ERR);
logText("PAYPAL: item number was '{$_POST['item_number']}'", LOG_INFO);
return false;
}
if (!$ok) {
logText("PAYPAL: remote server returned INVALID while checking IPN!", LOG_ERR);
logText("PAYPAL: item number was '{$_POST['item_number']}'", LOG_INFO);
}
return $ok;
}
function logIPN($input) {
$fields = array(
"receiver_email", "item_number", "payment_status", "pending_reason", "payment_date", "mc_gross",
"mc_fee", "tax", "mc_currency", "txn_id", "txn_type", "payer_email", "payer_status", "payment_type",
"verify_sign", "referrer_id"
);
$q1 = "INSERT INTO pp_ipn(received";
$q2 = ") VALUES (" . time();
foreach ($fields as $f) {
$q1 .= ",$f";
$q2 .= ",'" . addslashes($input[$f]) . "'";
}
return $this->db->query("$q1$q2)");
}
function addDonation($ticket, $amount) {
$q = $this->db->query("SELECT account FROM pp_ticket WHERE md5_id='" . addslashes($ticket) . "'");
if (!($q || dbCount($q))) {
logText("PAYPAL: couldn't find account number for ticket $ticket", LOG_ERR);
return;
}
list($account) = dbFetchArray($q);
$this->db->query("DELETE FROM pp_ticket WHERE md5_id='" . addslashes($ticket) . "'");
$this->db->query("INSERT INTO pp_history VALUES($account," . time() . ",$amount)");
$this->db->query("UPDATE credits SET credits_obtained = credits_obtained + " . round(10000 * $amount) . " WHERE account = $account");
}
}
?>

View file

@ -0,0 +1,52 @@
<?php
class main_rankings_library {
var $index = array(
'append',
'delete',
'getAll',
'update'
);
function main_rankings_library($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function getType($identifier) {
$version = $this->lib->game->version->id;
$name = $this->lib->game->name;
$q = $this->db->query("SELECT g.id FROM ranking_def d, ranking_game g "
. "WHERE d.version='$version' AND d.name='$identifier' AND g.ranking=d.id AND g.game='$name'");
list($id) = dbFetchArray($q);
return $id;
}
function getTypes() {
$game = $this->lib->game;
$version = $game->version;
$q = $this->db->query("SELECT g.id,d.name FROM ranking_def d, ranking_game g "
. "WHERE d.version='{$version->id}' AND g.game='{$game->name}' AND g.ranking=d.id");
$rs = array();
while ($r = dbFetchArray($q)) {
$rs[$r[1]] = $r[0];
}
return $rs;
}
function getText($id, $lang) {
$q = $this->db->query("SELECT t.name AS name,t.description AS description "
. "FROM ranking_text t, ranking_game g "
. "WHERE g.id=$id AND t.ranking=g.ranking AND t.lang='$lang'");
return dbFetchHash($q);
}
function get($type, $id) {
$q = $this->db->query("SELECT points,ranking FROM ranking WHERE r_type=$type AND LOWER(id)='".addslashes(strtolower($id))."' ORDER BY ranking,id");
return dbFetchHash($q);
}
}
?>

View file

@ -0,0 +1,30 @@
<?php
class main_rankings_append {
function main_rankings_append($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($type, $id, $additional = null) {
$q = $this->db->query("SELECT ranking FROM ranking WHERE r_type=$type AND points=0");
if ($q && dbCount($q)) {
list($rk) = dbFetchArray($q);
} else {
$q = $this->db->query("SELECT MAX(ranking) FROM ranking WHERE r_type=$type");
list($mrk) = dbFetchArray($q);
if (is_null($mrk)) {
$rk = 1;
} else {
$q = $this->db->query("SELECT COUNT(*) FROM ranking WHERE r_type=$type AND ranking=$mrk");
list($nrk) = dbFetchArray($q);
$rk = $mrk + $nrk;
}
}
$this->db->query("INSERT INTO ranking(r_type,id,additional,points,ranking) VALUES("
. "$type,'" . addslashes($id) . "'," . (is_null($additional) ? "NULL" : ("'" . addslashes($additional) . "'")) . ",0,$rk)");
}
}
?>

View file

@ -0,0 +1,20 @@
<?php
class main_rankings_delete {
function main_rankings_delete($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($type,$id) {
$q = $this->db->query("SELECT ranking FROM ranking WHERE r_type=$type AND id='".addslashes($id)."'");
list($rk) = dbFetchArray($q);
if (!is_null($rk)) {
$this->db->query("DELETE FROM ranking WHERE r_type=$type AND id='".addslashes($id)."'");
$this->db->query("UPDATE ranking SET ranking=ranking-1 WHERE r_type=$type AND ranking>$rk");
}
}
}
?>

View file

@ -0,0 +1,27 @@
<?php
class main_rankings_getAll {
function main_rankings_getAll($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($type, $top = null) {
if (is_null($top)) {
$qs = "";
} else {
$qs = "AND ranking<=$top";
}
$rs = array();
$q = $this->db->query("SELECT id,points,ranking FROM ranking WHERE r_type=$type $qs ORDER BY ranking ASC,id ASC");
while ($r = dbFetchHash($q)) {
array_push($rs, $r);
}
return $rs;
}
}
?>

View file

@ -0,0 +1,39 @@
<?php
class main_rankings_update {
function main_rankings_update($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($id, $data) {
// $data = array($points => array($name));
$plist = array_keys($data);
sort($plist);
$plist = array_reverse($plist);
$a = array(); $cr = 1;
for ($i=0;$i<count($plist);$i++) {
$s = 0;
foreach ($data[$plist[$i]] as $pn) {
array_push($a, array(
'name' => $pn,
'points' => $plist[$i],
'ranking' => $cr
));
$s ++;
}
$cr += $s;
}
$this->db->query("DELETE FROM ranking WHERE r_type=$id");
foreach ($a as $entry) {
$this->db->query("INSERT INTO ranking(r_type,id,additional,points,ranking) VALUES("
. "$id, '" . addslashes($entry['name']) . "',NULL,".$entry['points'].",".$entry['ranking'].")");
}
}
}
?>

View file

@ -0,0 +1,150 @@
<?php
//---------------------------------------------------------------
// "Day tick": adds vacation credits to registered players who
// have less than 240 credits (2 months), deletes old logs,
// warn inactive players or delete their accounts
// Executed once per day at 07:00 ST
//---------------------------------------------------------------
class main_ticks_day_library {
function main_ticks_day_library($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->main = $this->lib->game->getLib();
$this->accounts = $this->lib->game->getLib("main/account");
}
function runTick() {
// Delete logs and update vacation credits
$this->db->safeTransaction(array($this, "updateStatus"));
// Manage inactive / leaving accounts
$this->db->safeTransaction(array($this, "terminateAccounts"));
// Send mails if needed
foreach ($this->mails as $mail) {
if ($this->main->call('sendMail', $mail[0], $mail[1], $mail[2])) {
l::info($mail[3]);
} else {
l::warn("Failed to send mail {$mail[0]} to {$mail[1]}");
}
}
}
public function updateStatus() {
// Delete old logs
$this->db->query(
"DELETE FROM account_log "
. "WHERE UNIX_TIMESTAMP(NOW())-t>3600*24*60 AND action IN ('IN','OUT')"
);
// Add vacation credits
$this->db->query(
"UPDATE account SET vac_credits=vac_credits + 1 "
. "WHERE status='STD' AND vac_credits<240 AND vac_start IS NULL"
);
}
public function terminateAccounts() {
$this->mails = array();
// Send mails to accounts that have been inactive for 21 days
$this->warnInactive();
// Close really inactive accounts
$this->closeInactive();
// Close accounts on request
$this->closeQuitters();
// Delete accounts that haven't been confirmed yet
$this->deleteUnconfirmed();
}
private function sendMail($file, $addr, $data, $log) {
array_push($this->mails, array($file, $addr, $data, $log));
}
private function warnInactive() {
// Get accounts that have been inactive for 21 days
$q = $this->db->query(
"SELECT id, name, email FROM account "
. "WHERE NOT admin AND status='STD' "
. "AND last_logout IS NOT NULL AND last_logout > last_login "
. "AND (UNIX_TIMESTAMP(NOW()) - last_logout BETWEEN 24*3600*21 AND 24 * 3600 * 22)"
);
while ($r = dbFetchArray($q)) {
list($id, $name, $addr) = $r;
// Get the user's language
$l = $this->accounts->call('getLanguage', $id);
// Send the warning e-mail
$this->sendMail("mail-warn-inactive.$l.txt", $addr, array("USER" => $name),
"main/day: inactivity warning mail sent to player $name");
}
}
private function closeInactive() {
// Disable accounts that have been inactive for 28 days
$q = $this->db->query(
"SELECT id, name, email FROM account "
. "WHERE NOT admin AND status='STD' AND last_logout IS NOT NULL "
. "AND UNIX_TIMESTAMP(NOW()) - last_logout >= 24 * 3600 * 28"
);
while ($r = dbFetchArray($q)) {
list($uid, $name, $addr) = $r;
$this->closeAccount($uid, $name, $addr, 'INAC', 'kick-inactive');
}
}
private function closeQuitters() {
// Close accounts for players who requested to quit
$q = $this->db->query(
"SELECT id, name, email FROM account "
. "WHERE (status='STD' OR status='VAC') AND quit_ts IS NOT NULL "
. "AND UNIX_TIMESTAMP(NOW()) > quit_ts + 86400"
);
while ($r = dbFetchArray($q)) {
list($uid, $name, $addr) = $r;
$this->closeAccount($uid, $name, $addr, 'QUIT', 'quit');
}
}
private function closeAccount($uid, $name, $addr, $reason, $mail) {
// Get the user's language
$l = $this->accounts->call('getLanguage', $uid);
// Send the termination e-mail
$this->sendMail("mail-$mail.$l.txt", $addr, array("USER" => $name),
"main/day: mail sent to player $name ($reason)");
// Terminate the account
$this->accounts->call('terminate', $uid, $reason);
}
private function deleteUnconfirmed() {
// Silently delete accounts that have the 'NEW' status and have been created more than 6 days ago
$q = $this->db->query(
"SELECT a.id FROM account a,account_log l "
. "WHERE a.status='NEW' AND l.account=a.id AND l.action='CREATE' "
. "AND UNIX_TIMESTAMP(NOW()) - l.t > 6 * 24 * 3600"
);
$nd = array();
while ($r = dbFetchArray($q)) {
array_push($nd, $r[0]);
}
if (count($nd)) {
$this->db->query("DELETE FROM account WHERE id IN (" . join(',', $nd) . ")");
}
}
}
?>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,12 @@
<?php
// Empty tick, to be run every 5 seconds. Should make the scheduler
// react faster if there are no ticks.
class main_ticks_mark_library {
public function __construct($lib) { }
public function runTick() { }
}
?>

View file

@ -0,0 +1,68 @@
<?php
//---------------------------------------------------------------
// "Session tick": removes outdated sessions and tracking data
// Executed once per minute, starting from 7:00:40 ST
//---------------------------------------------------------------
class main_ticks_session_library {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->accounts = $this->lib->game->getLib("main/account");
}
public function runTick() {
$this->db->safeTransaction(array($this, 'removeTracking'));
$this->db->safeTransaction(array($this, 'removeSessions'));
}
public function removeTracking() {
// Removes old tracking data (> 3 months or > 1 day and last used within a minute of creation)
$this->db->query(
"UPDATE web_tracking SET stored_data = '' "
. "WHERE UNIX_TIMESTAMP(NOW()) - last_used > 7776000"
);
$this->db->query(
"DELETE FROM web_tracking "
. "WHERE (UNIX_TIMESTAMP(NOW()) - last_used > 86400 AND last_used - created < 60)"
);
}
public function removeSessions() {
// Removes old sessions
$q = $this->db->query(
"SELECT s.id, t.id, a.id FROM web_session s, web_tracking t, account a "
. "WHERE UNIX_TIMESTAMP(NOW()) - s.last_used > 3600 "
. "AND a.id = s.account "
. "AND t.id = s.tracking "
. "FOR UPDATE OF s, t, a"
);
if (! dbCount($q)) {
return;
}
$accounts = array();
while ($r = dbFetchArray($q)) {
list($sid, $tid, $uid) = $r;
if (is_null($uid)) {
continue;
}
$this->accounts->call('log', $uid, 'o');
array_push($accounts, $uid);
l::info("Player #$uid logged out automatically");
}
$this->db->query(
"UPDATE account SET last_logout = UNIX_TIMESTAMP(NOW()) "
. "WHERE id IN (" . join(',', $accounts) . ")"
);
$this->db->query("DELETE FROM web_session WHERE UNIX_TIMESTAMP(NOW()) - last_used > 3600");
}
}
?>

View file

@ -0,0 +1,62 @@
<?php
//--------------------------------------------------------------------
// "Vacation tick": decrements credits for players in vacation mode
// and, should they reach 0, cancel vacation mode. Check for
// accounts which must enter vacation mode as well.
// Executed four times a day
//--------------------------------------------------------------------
class main_ticks_vacation_library {
public function __construct($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->main = $this->lib->game->getLib();
$this->vacation = $this->lib->game->getLib("main/vacation");
}
public function runTick() {
$this->db->safeTransaction(array($this, "decreaseCredits"));
$this->db->safeTransaction(array($this, "enterVacation"));
}
public function decreaseCredits() {
// Decrease credits for players in vacation mode
$q = $this->db->query(
"SELECT id FROM account WHERE status = 'VAC' AND vac_credits = 0 FOR UPDATE"
);
if ($q && dbCount($q)) {
while ($r = dbFetchArray($q)) {
list($uid) = $r;
$this->vacation->call('leave', $uid);
}
}
$this->db->query("UPDATE account SET vac_credits = vac_credits - 1 WHERE status = 'VAC'");
}
public function enterVacation() {
// Look for players who should enter vacation mode
$q = $this->db->query(
"SELECT id FROM account "
. "WHERE status='STD' AND vac_start IS NOT NULL AND UNIX_TIMESTAMP(NOW()) > vac_start "
. "FOR UPDATE"
);
if (! dbCount($q)) {
return;
}
while ($r = dbFetchArray($q)) {
list($uid) = $r;
$this->vacation->call('start', $uid);
}
$this->db->query(
"UPDATE account SET status = 'VAC', vac_start = NULL $vsWhere"
. "WHERE status='STD' AND vac_start IS NOT NULL "
. "AND UNIX_TIMESTAMP(NOW()) > vac_start"
);
}
}
?>

View file

@ -0,0 +1,45 @@
<?php
class main_vacation_library {
var $index = array(
'canSet',
'start',
'leave'
);
function main_vacation_library($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function isOnVacation($uid) {
$q = $this->db->query("SELECT COUNT(*) FROM account WHERE id=$uid AND status='VAC'");
list($onVac) = dbFetchArray($q);
return ($onVac == 1);
}
function getStatus($uid) {
$q = $this->db->query("SELECT status,vac_start,vac_credits FROM account WHERE id=$uid");
if ($q && dbCount($q) == 1) {
return dbFetchHash($q);
}
return null;
}
function setStart($uid) {
$vs = time() + 86400;
$this->db->query("UPDATE account SET vac_start=$vs WHERE id=$uid AND status='STD'");
logText("Account $uid requested vacation mode");
}
function resetStart($uid) {
$this->db->query("UPDATE account SET vac_start=NULL WHERE id=$uid AND status='STD'");
logText("Account $uid cancelled request for vacation mode");
}
}
?>

View file

@ -0,0 +1,31 @@
<?php
class main_vacation_canSet {
function main_vacation_canSet($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
}
function run($uid) {
$q = $this->db->query("SELECT t FROM account_log WHERE account=$uid AND action='VEND'");
if (!$q) {
return false;
} elseif (dbCount($q) > 0) {
list($last) = dbFetchArray($q);
if (!(time() - $last > 604800 /* 7 * 24 * 3600 */ )) {
return false;
}
}
$q = $this->db->query("SELECT vac_credits,quit_ts FROM account WHERE id=$uid");
if (!($q && dbCount($q) == 1)) {
return false;
}
list($cred,$quit) = dbFetchArray($q);
return is_null($quit) && ($cred > 0);
}
}
?>

View file

@ -0,0 +1,31 @@
<?php
class main_vacation_leave {
function main_vacation_leave($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->accounts =& $this->lib->game->getLib('main/account');
}
function run($uid) {
foreach (config::getGames() as $game) {
if ($game->name == 'main') {
continue;
}
$lib = $game->getLib();
$pid = $lib->call('doesUserPlay', $uid);
if (!is_null($pid)) {
$lib->call('leaveVacation', $pid);
}
}
$this->accounts->call('log', $uid, 've');
$this->db->query("UPDATE account SET status='STD' WHERE id=$uid");
logText("Account $uid has left vacation mode");
}
}
?>

View file

@ -0,0 +1,31 @@
<?php
class main_vacation_start {
function main_vacation_start($lib) {
$this->lib = $lib;
$this->db = $this->lib->game->db;
$this->accounts = $this->lib->game->getLib('main/account');
}
function run($uid) {
foreach (config::getGames() as $game) {
if ($game->name == 'main') {
continue;
}
$lib = $game->getLib();
$pid = $lib->call('doesUserPlay', $uid);
if (!is_null($pid)) {
$lib->call('startVacation', $pid);
}
}
$this->db->query("UPDATE account SET status='VAC',vac_start=NULL,vac_credits=vac_credits-1 WHERE id=$uid");
$this->accounts->call('log', $uid, 'vs');
logText("Account $uid has entered vacation mode");
}
}
?>