Skip to content

Commit

Permalink
Update for Web Server (#328)
Browse files Browse the repository at this point in the history
* Update for Web Server

Added configs for webserver.
"EmblemUseWebservice" was removed from modules.

* Added support for animated gif
  • Loading branch information
Balferian authored Nov 9, 2023
1 parent 99b0c43 commit a2a068a
Show file tree
Hide file tree
Showing 32 changed files with 151 additions and 141 deletions.
8 changes: 8 additions & 0 deletions config/servers.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
'Persistent' => true,
'Timezone' => null // Possible values is as described in the comment in DbConfig.
),
// Web server configuration.
'WebDbConfig' => array(
'Hostname' => '127.0.0.1',
'Username' => 'ragnarok',
'Password' => 'ragnarok',
'Database' => 'ragnarok',
'Persistent' => true
),
// Login server configuration.
'LoginServer' => array(
'Address' => '127.0.0.1',
Expand Down
6 changes: 4 additions & 2 deletions lib/Flux.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public static function initialize($options = array())
public static function initializeServerObjects()
{
foreach (self::$serversConfig->getChildrenConfigs() as $key => $config) {
$connection = new Flux_Connection($config->getDbConfig(), $config->getLogsDbConfig());
$connection = new Flux_Connection($config->getDbConfig(), $config->getLogsDbConfig(), $config->getWebDbConfig());
$loginServer = new Flux_LoginServer($config->getLoginServer());

// LoginAthenaGroup maintains the grouping of a central login
Expand Down Expand Up @@ -378,14 +378,16 @@ public static function parseServersConfigFile($filename, $import = false)

$topConfig->setDbConfig(array(), $options);
$topConfig->setLogsDbConfig(array(), $options);
$topConfig->setWebDbConfig(array(), $options);
$topConfig->setLoginServer(array(), $options);
$topConfig->setCharMapServers(array(), $options);

$dbConfig = $topConfig->getDbConfig();
$logsDbConfig = $topConfig->getLogsDbConfig();
$webDbConfig = $topConfig->getWebDbConfig();
$loginServer = $topConfig->getLoginServer();

foreach (array($dbConfig, $logsDbConfig) as $_dbConfig) {
foreach (array($dbConfig, $logsDbConfig, $webDbConfig) as $_dbConfig) {
$_dbConfig->setHostname('localhost', $options);
$_dbConfig->setUsername('ragnarok', $options);
$_dbConfig->setPassword('ragnarok', $options);
Expand Down
9 changes: 9 additions & 0 deletions lib/Flux/Athena.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ class Flux_Athena {
*/
public $logsDatabase;

/**
* Web server database. (is not set until setConnection() is called.)
*
* @access public
* @var string
*/
public $webDatabase;

/**
* Database used for the char/map (aka everything else) SQL operations.
* This does not include log-related tasks.
Expand Down Expand Up @@ -249,6 +257,7 @@ public function setConnection(Flux_Connection $connection)
{
$this->connection = $connection;
$this->logsDatabase = $connection->logsDbConfig->getDatabase();
$this->webDatabase = $connection->webDbConfig->getDatabase();

return $connection;
}
Expand Down
63 changes: 62 additions & 1 deletion lib/Flux/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class Flux_Connection {
*/
public $logsDbConfig;

/**
* Logs database configuration object.
*
* @access public
* @var Flux_Config
*/
public $webDbConfig;

/**
* @access private
* @var PDO
Expand All @@ -38,15 +46,23 @@ class Flux_Connection {
*/
private $pdoLogs;

/**
* @access private
* @var PDO
*/
private $pdoWeb;

/**
* @param Flux_Config $dbConfig
* @param Flux_Config $logsDbConfig
* @param Flux_Config $webDbConfig
* @access public
*/
public function __construct(Flux_Config $dbConfig, Flux_Config $logsDbConfig)
public function __construct(Flux_Config $dbConfig, Flux_Config $logsDbConfig, Flux_Config $webDbConfig)
{
$this->dbConfig = $dbConfig;
$this->logsDbConfig = $logsDbConfig;
$this->webDbConfig = $webDbConfig;
}

/**
Expand Down Expand Up @@ -131,6 +147,31 @@ private function getLogsConnection()
return $this->pdoLogs;
}

/**
* Get the PDO instance for the web server database server connection.
*
* @return PDO
* @access private
*/
private function getWebConnection()
{
if (!$this->pdoWeb) {
// Establish separate connection just for the web server database.
$pdoWeb = $this->connect($this->webDbConfig);
$this->pdoWeb = $pdoWeb;

if ($encoding=$this->webDbConfig->getEncoding()) {
$sth = $this->getStatementForWeb("SET NAMES ?");
$sth->execute(array($encoding));
}
if ($timezone=$this->webDbConfig->getTimezone()) {
$sth = $this->getStatementForWeb("SET time_zone = ?");
$sth->execute(array($timezone));
}
}
return $this->pdoWeb;
}

/**
* Select database to use.
*
Expand Down Expand Up @@ -188,6 +229,26 @@ public function getStatementForLogs($statement, $options = array())
}
}

/**
* Instanciate a PDOStatement without obtaining a PDO handler before-hand.
*
* @return PDOStatement
* @access public
*/
public function getStatementForWeb($statement, $options = array())
{
$dbh = $this->getWebConnection();
$sth = $dbh->prepare($statement, $options);
@$sth->setFetchMode(PDO::FETCH_CLASS, 'Flux_DataObject', array(null, array('dbconfig' => $this->webDbConfig)));

if ($sth) {
return new Flux_Connection_Statement($sth);
}
else {
return false;
}
}

/**
*
*/
Expand Down
9 changes: 9 additions & 0 deletions lib/Flux/LoginAthenaGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ class Flux_LoginAthenaGroup {
*/
public $logsDatabase;

/**
* Web server database.
*
* @access public
* @var string
*/
public $webDatabase;

/**
* Array of Flux_Athena instances.
*
Expand All @@ -64,6 +72,7 @@ public function __construct($serverName, Flux_Connection $connection, Flux_Login
$this->loginServer = $loginServer;
$this->loginDatabase = $loginServer->config->getDatabase();
$this->logsDatabase = $connection->logsDbConfig->getDatabase();
$this->webDatabase = $connection->webDbConfig->getDatabase();

// Assign connection to LoginServer, used mainly to enable
// authentication feature.
Expand Down
9 changes: 9 additions & 0 deletions lib/Flux/LoginServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ class Flux_LoginServer extends Flux_BaseServer {
*/
public $logsDatabase;

/**
* Web server database. (is not set until setConnection() is called.)
*
* @access public
* @var string
*/
public $webDatabase;

/**
* Overridden to add custom properties.
*
Expand All @@ -52,6 +60,7 @@ public function setConnection(Flux_Connection $connection)
{
$this->connection = $connection;
$this->logsDatabase = $connection->logsDbConfig->getDatabase();
$this->webDatabase = $connection->webDbConfig->getDatabase();

return $connection;
}
Expand Down
6 changes: 0 additions & 6 deletions modules/account/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,8 @@
$athena = $session->getAthenaServer($serverName);

$sql = "SELECT ch.*, guild.name AS guild_name, ";
if(Flux::config('EmblemUseWebservice'))
$sql .= "guild_emblems.file_data as guild_emblem_len ";
else
$sql .= "guild.emblem_len AS guild_emblem_len ";
$sql .= "FROM {$athena->charMapDatabase}.`char` AS ch ";
$sql .= "LEFT OUTER JOIN {$athena->charMapDatabase}.guild ON guild.guild_id = ch.guild_id ";
if(Flux::config('EmblemUseWebservice'))
$sql .= "LEFT JOIN {$server->charMapDatabase}.`guild_emblems` ON `guild_emblems`.guild_id = guild.guild_id ";
$sql .= "WHERE ch.account_id = ? ORDER BY ch.char_num ASC";
$sth = $server->connection->getStatement($sql);
$sth->execute(array($accountID));
Expand Down
8 changes: 1 addition & 7 deletions modules/castle/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@
$castleNames = Flux::config('CastleNames')->toArray();
$ids = implode(',', array_fill(0, count($castleNames), '?'));

$sql = "SELECT castles.castle_id, castles.guild_id, guild.name AS guild_name, ";
if(Flux::config('EmblemUseWebservice'))
$sql .= "guild_emblems.file_data as emblem_len ";
else
$sql .= "guild.emblem_len ";
$sql = "SELECT castles.castle_id, castles.guild_id, guild.name AS guild_name, guild.emblem_id as emblem ";
$sql .= "FROM {$server->charMapDatabase}.guild_castle AS castles ";
$sql .= "LEFT JOIN guild ON guild.guild_id = castles.guild_id ";
if(Flux::config('EmblemUseWebservice'))
$sql .= "LEFT JOIN {$server->charMapDatabase}.`guild_emblems` ON `guild_emblems`.guild_id = castles.guild_id ";
$sql .= "WHERE castles.castle_id IN ($ids)";
$sql .= "ORDER BY castles.castle_id ASC";
$sth = $server->connection->getStatement($sql);
Expand Down
8 changes: 1 addition & 7 deletions modules/character/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
$sqlpartial .= "LEFT OUTER JOIN {$server->charMapDatabase}.`char` AS mother ON mother.char_id = ch.mother ";
$sqlpartial .= "LEFT OUTER JOIN {$server->charMapDatabase}.`char` AS father ON father.char_id = ch.father ";
$sqlpartial .= "LEFT OUTER JOIN {$server->charMapDatabase}.`char` AS child ON child.char_id = ch.child ";
if(Flux::config('EmblemUseWebservice'))
$sqlpartial .= "LEFT JOIN {$server->charMapDatabase}.`guild_emblems` ON `guild_emblems`.guild_id = ch.guild_id ";

$sqlwhere = "WHERE 1=1 ";
$sqlcount = '';
Expand Down Expand Up @@ -169,11 +167,7 @@

$col = "ch.account_id, ch.char_id, ch.name AS char_name, ch.char_num, ";
$col .= "ch.online, ch.base_level, ch.job_level, ch.class, ch.zeny, ";
$col .= "guild.guild_id, guild.name AS guild_name, ";
if(Flux::config('EmblemUseWebservice'))
$col .= "guild_emblems.file_data as guild_emblem_len, ";
else
$col .= "guild.emblem_len as guild_emblem_len, ";
$col .= "guild.guild_id, guild.name AS guild_name, guild.emblem_id as emblem, ";
$col .= "login.userid, partner.name AS partner_name, partner.char_id AS partner_id, ";
$col .= "mother.name AS mother_name, mother.char_id AS mother_id, ";
$col .= "father.name AS father_name, father.char_id AS father_id, ";
Expand Down
8 changes: 1 addition & 7 deletions modules/character/online.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

$sqlpartial = "LEFT JOIN {$server->loginDatabase}.login ON login.account_id = ch.account_id ";
$sqlpartial .= "LEFT JOIN {$server->charMapDatabase}.guild ON guild.guild_id = ch.guild_id ";
if(Flux::config('EmblemUseWebservice'))
$sqlpartial .= "LEFT JOIN {$server->charMapDatabase}.`guild_emblems` ON `guild_emblems`.guild_id = ch.guild_id ";

if (!$auth->allowedToIgnoreHiddenPref) {
$sqlpartial .= "LEFT JOIN {$server->charMapDatabase}.$charPrefsTable AS pref1 ON ";
Expand Down Expand Up @@ -100,11 +98,7 @@
$hiddenCount = (int)$sth->fetch()->total;

$col = "ch.char_id, ch.name AS char_name, ch.class AS char_class, ch.base_level, ch.job_level, ";
$col .= "guild.name AS guild_name, guild.guild_id, ch.last_map, pref2.value AS hidemap, ";
if(Flux::config('EmblemUseWebservice'))
$col .= "guild_emblems.file_data as guild_emblem_len ";
else
$col .= "guild.emblem_len as guild_emblem_len ";
$col .= "guild.name AS guild_name, guild.guild_id, ch.last_map, pref2.value AS hidemap, guild.emblem_id as emblem ";

$sql = $paginator->getSQL("SELECT $col FROM {$server->charMapDatabase}.`char` AS ch $sqlpartial");
$sth = $server->connection->getStatement($sql);
Expand Down
22 changes: 3 additions & 19 deletions modules/character/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@
$col .= "mother.name AS mother_name, mother.char_id AS mother_id, ";
$col .= "father.name AS father_name, father.char_id AS father_id, ";
$col .= "child.name AS child_name, child.char_id AS child_id, ";
$col .= "guild.guild_id, guild.name AS guild_name, ";
if(Flux::config('EmblemUseWebservice'))
$col .= "guild_emblems.file_data as guild_emblem_len, ";
else
$col .= "guild.emblem_len as guild_emblem_len, ";
$col .= "guild.guild_id, guild.name AS guild_name, guild.emblem_id AS emblem, ";
$col .= "guild_position.name AS guild_position, IFNULL(guild_position.exp_mode, 0) AS guild_tax, ";
$col .= "party.name AS party_name, party.leader_char AS party_leader_id, party_leader.name AS party_leader_name, ";

Expand Down Expand Up @@ -67,8 +63,6 @@
$sql .= "LEFT OUTER JOIN {$server->charMapDatabase}.`".$mobdb[0]."` AS pet_mob ON pet_mob.ID = pet.class ";
$sql .= "LEFT OUTER JOIN {$server->charMapDatabase}.`".$mobdb[1]."` AS pet_mob2 ON pet_mob2.ID = pet.class ";
$sql .= "LEFT OUTER JOIN {$server->charMapDatabase}.`char_reg_num` AS reg ON reg.char_id = ch.char_id AND reg.key = 'PC_DIE_COUNTER' ";
if(Flux::config('EmblemUseWebservice'))
$sql .= "LEFT OUTER JOIN {$server->charMapDatabase}.`guild_emblems` ON `guild_emblems`.guild_id = ch.guild_id ";
$sql .= "WHERE ch.char_id = ?";

$sth = $server->connection->getStatement($sql);
Expand Down Expand Up @@ -96,14 +90,9 @@

$sql = "SELECT fr.char_id, fr.name, fr.class, fr.base_level, fr.job_level, ";
$sql .= "guild.guild_id, guild.name AS guild_name, fr.online, ";
if(Flux::config('EmblemUseWebservice'))
$sql .= "guild_emblems.file_data as guild_emblem_len ";
else
$sql .= "guild.emblem_len as guild_emblem_len ";
$sql .= "guild.emblem_id AS emblem ";
$sql .= "FROM {$server->charMapDatabase}.`char` AS fr ";
$sql .= "LEFT OUTER JOIN {$server->charMapDatabase}.guild ON guild.guild_id = fr.guild_id ";
if(Flux::config('EmblemUseWebservice'))
$sql .= "LEFT JOIN {$server->charMapDatabase}.`guild_emblems` ON `guild_emblems`.guild_id = fr.guild_id ";
$sql .= "LEFT OUTER JOIN {$server->charMapDatabase}.friends ON friends.friend_id = fr.char_id ";
$sql .= "WHERE friends.char_id = ? ORDER BY fr.name ASC";
$sth = $server->connection->getStatement($sql);
Expand All @@ -114,14 +103,9 @@
if ($char->party_leader_id) {
$sql = "SELECT p.char_id, p.name, p.class, p.base_level, p.job_level, ";
$sql .= "guild.guild_id, guild.name AS guild_name, p.online, ";
if(Flux::config('EmblemUseWebservice'))
$sql .= "guild_emblems.file_data as guild_emblem_len ";
else
$sql .= "guild.emblem_len as guild_emblem_len ";
$sql .= "guild.emblem_id AS emblem ";
$sql .= "FROM {$server->charMapDatabase}.`char` AS p ";
$sql .= "LEFT OUTER JOIN {$server->charMapDatabase}.guild ON guild.guild_id = p.guild_id ";
if(Flux::config('EmblemUseWebservice'))
$sql .= "LEFT OUTER JOIN {$server->charMapDatabase}.`guild_emblems` ON `guild_emblems`.guild_id = p.guild_id ";
$sql .= "WHERE p.party_id = ? AND p.char_id != ? ORDER BY p.name ASC";
$sth = $server->connection->getStatement($sql);

Expand Down
22 changes: 11 additions & 11 deletions modules/guild/emblem.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ function flux_display_empty_emblem()
if (!$athenaServer || $guildID < 0)
flux_display_empty_emblem();
else {
$dirname = FLUX_DATA_DIR."/tmp/emblems/$serverName/$athenaServerName";
$filename = "$dirname/$guildID.png";

if ($interval=Flux::config('EmblemCacheInterval')) {
$interval *= 60;
$dirname = FLUX_DATA_DIR."/tmp/emblems/$serverName/$athenaServerName";
$filename = "$dirname/$guildID.png";

if (!is_dir($dirname))
if (Flux::config('RequireOwnership'))
Expand All @@ -48,24 +49,23 @@ function flux_display_empty_emblem()
}

if(Flux::config('EmblemUseWebservice')) {
$db = $athenaServer->charMapDatabase;
$db = $athenaServer->webDatabase;
$sql = "SELECT file_type, file_data FROM $db.guild_emblems WHERE guild_id = ? LIMIT 1";
$sth = $athenaServer->connection->getStatement($sql);
$sth = $athenaServer->connection->getStatementForWeb($sql);
$sth->execute(array($guildID));
$res = $sth->fetch();

if (!$res->file_data)
flux_display_empty_emblem();
else {
$data = 'data:image/gif;base64,'.base64_encode($res->file_data);

if ($interval)
file_put_contents($filename, $res->file_data);

/* TODO; add gif animation at first image load */
$image = imagecreatefromstring($res->file_data);
$rgb = imagecolorexact ($image, 255,0,255);
imagecolortransparent($image, $rgb);

header("Content-Type: image/png");

if ($interval)
imagepng($image, $filename);

imagepng($image);
exit;
}
Expand Down
Loading

0 comments on commit a2a068a

Please sign in to comment.