* performance and optimisations fixes part 2, according to awesome PHPStorm Php Inspections plugin!

This commit is contained in:
slawkens 2018-05-29 21:16:48 +02:00
parent 857aa110c8
commit 68d74a490b
4 changed files with 78 additions and 61 deletions

View File

@ -65,7 +65,7 @@ function getForumBoardLink($board_id, $page = NULL)
function getPlayerLink($name, $generate = true)
{
global $ots, $config;
global $config;
if(is_numeric($name))
{
@ -431,10 +431,12 @@ function template_place_holder($type)
if(array_key_exists($type, $template_place_holders) && is_array($template_place_holders[$type]))
$ret = implode($template_place_holders[$type]);
if($type === 'head_start')
if($type === 'head_start') {
$ret .= template_header();
elseif($type === 'body_end')
}
elseif($type === 'body_end') {
$ret .= template_ga_code();
}
return $ret;
}
@ -546,7 +548,7 @@ function getStyle($i)
return is_int($i / 2) ? $config['darkborder'] : $config['lightborder'];
}
$vowels = array("e", "y", "u", "i", "o", "a");
$vowels = array('e', 'y', 'u', 'i', 'o', 'a');
function getCreatureName($killer, $showStatus = false, $extendedInfo = false)
{
global $vowels, $ots, $config;
@ -710,7 +712,7 @@ function get_locales()
$ret = array();
$path = LOCALE;
foreach(scandir($path) as $file)
foreach(scandir($path, 0) as $file)
{
if($file[0] != '.' && $file != '..' && is_dir($path . $file))
$ret[] = $file;
@ -746,7 +748,7 @@ function get_templates()
$ret = array();
$path = TEMPLATES;
foreach(scandir($path) as $file)
foreach(scandir($path, 0) as $file)
{
if($file[0] !== '.' && $file !== '..' && is_dir($path . $file))
$ret[] = $file;
@ -764,7 +766,7 @@ function get_plugins()
$ret = array();
$path = PLUGINS;
foreach(scandir($path) as $file) {
foreach(scandir($path, 0) as $file) {
$file_ext = pathinfo($file, PATHINFO_EXTENSION);
$file_name = pathinfo($file, PATHINFO_FILENAME);
if ($file === '.' || $file === '..' || $file === 'disabled' || $file === 'example.json' || $file_ext !== 'json' || is_dir($path . $file))
@ -815,7 +817,7 @@ function _mail($to, $subject, $body, $altBody = '', $add_html_tags = true)
if($config['smtp_enabled'])
{
$mailer->IsSMTP();
$mailer->isSMTP();
$mailer->Host = $config['smtp_host'];
$mailer->Port = (int)$config['smtp_port'];
$mailer->SMTPAuth = $config['smtp_auth'];
@ -823,24 +825,26 @@ function _mail($to, $subject, $body, $altBody = '', $add_html_tags = true)
$mailer->Password = $config['smtp_pass'];
$mailer->SMTPSecure = isset($config['smtp_secure']) ? $config['smtp_secure'] : '';
}
else
$mailer->IsMail();
else {
$mailer->isMail();
}
$mailer->IsHTML(isset($body[0]) > 0);
$mailer->isHTML(isset($body[0]) > 0);
$mailer->From = $config['mail_address'];
$mailer->Sender = $config['mail_address'];
$mailer->CharSet = 'utf-8';
$mailer->FromName = $config['lua']['serverName'];
$mailer->Subject = $subject;
$mailer->AddAddress($to);
$mailer->addAddress($to);
$mailer->Body = $tmp_body;
$signature_plain = '';
if(isset($config['mail_signature']['plain']))
$signature_plain = $config['mail_signature']['plain'];
if(isset($altBody[0]))
if(isset($altBody[0])) {
$mailer->AltBody = $altBody . $signature_plain;
}
else { // automatically generate plain html
$mailer->AltBody = strip_tags(preg_replace('/<a(.*)href="([^"]*)"(.*)>/','$2', $body)) . "\n" . $signature_plain;
}
@ -856,7 +860,7 @@ function convert_bytes($size)
function log_append($file, $str)
{
$f = fopen(LOGS . $file, 'a');
$f = fopen(LOGS . $file, 'ab');
fwrite($f, '[' . date(DateTime::RFC1123) . '] ' . $str . PHP_EOL);
fclose($f);
}
@ -875,7 +879,7 @@ function load_config_lua($filename)
$result = array();
$config_string = str_replace(array("\r\n", "\r"), "\n", file_get_contents($filename));
$lines = explode("\n", $config_string);
if(count($lines) > 0)
if(count($lines) > 0) {
foreach($lines as $ln => $line)
{
$tmp_exp = explode('=', $line, 2);
@ -891,7 +895,7 @@ function load_config_lua($filename)
else if(count($tmp_exp) >= 2)
{
$key = trim($tmp_exp[0]);
if(substr($key, 0, 2) != '--')
if(0 !== strpos($key, '--'))
{
$value = trim($tmp_exp[1]);
if(strpos($value, '--') !== false) {// found some deep comment
@ -900,11 +904,11 @@ function load_config_lua($filename)
if(is_numeric($value))
$result[$key] = (float) $value;
elseif(in_array(substr($value, 0 , 1), array("'", '"')) && in_array(substr($value, -1 , 1), array("'", '"')))
elseif(in_array(@$value[0], array("'", '"')) && in_array(@$value[strlen($value) - 1], array("'", '"')))
$result[$key] = (string) substr(substr($value, 1), 0, -1);
elseif(in_array($value, array('true', 'false')))
$result[$key] = ($value == 'true') ? true : false;
elseif(substr($value, 0 , 1) == '{' && substr($value, -1 , 1) == '}') {
$result[$key] = ($value === 'true') ? true : false;
elseif(@$value[0] === '{' && @$value[strlen($value) - 1] === '}') {
// arrays are not supported yet
// just ignore the error
}
@ -922,7 +926,7 @@ function load_config_lua($filename)
}
}
}
}
$result = array_merge($result, isset($config['lua']) ? $config['lua'] : array());
return $result;
@ -1010,8 +1014,8 @@ function deleteDirectory($dir) {
if(!is_dir($dir)) {
return unlink($dir);
}
foreach(scandir($dir) as $item) {
foreach(scandir($dir, 0) as $item) {
if($item === '.' || $item === '..') {
continue;
}

View File

@ -453,8 +453,8 @@ class POT
*
* @version 0.1.5
* @since 0.0.5
* @param string $ip IP to ban.
* @param string $mask Mask for ban (by default bans only given IP).
* @param mixed|string $ip IP to ban.
* @param mixed|string $mask Mask for ban (by default bans only given IP).
* @param int $time Time for time until expires (0 - forever).
* @throws PDOException On PDO operation error.
* @deprecated 0.1.5 Use OTS_IPBan class.
@ -463,7 +463,7 @@ class POT
{
// long2ip( ip2long('255.255.255.255') ) != '255.255.255.255' -.-'
// it's because that PHP integer types are signed
if($ip == '255.255.255.255')
if($ip === '255.255.255.255')
{
$ip = 4294967295;
}
@ -472,7 +472,7 @@ class POT
$ip = sprintf('%u', ip2long($ip) );
}
if($mask == '255.255.255.255')
if($mask === '255.255.255.255')
{
$mask = 4294967295;
}
@ -500,7 +500,7 @@ class POT
*
* @version 0.1.5
* @since 0.0.5
* @param string $ip IP to ban.
* @param mixed|string $ip IP to ban.
* @param string $mask Mask for ban (by default 255.255.255.255) - not used thought.
* @throws PDOException On PDO operation error.
* @deprecated 0.1.5 Use OTS_IPBan class.
@ -509,7 +509,7 @@ class POT
{
// long2ip( ip2long('255.255.255.255') ) != '255.255.255.255' -.-'
// it's because that PHP integer types are signed
if($ip == '255.255.255.255')
if($ip === '255.255.255.255')
{
$ip = 4294967295;
}
@ -531,7 +531,7 @@ class POT
*
* @version 0.1.5
* @since 0.0.5
* @param string $ip IP to ban.
* @param mixed|string $ip IP to ban.
* @return bool True if IP number is banned, false otherwise.
* @throws PDOException On PDO operation error.
* @deprecated 0.1.5 Use OTS_IPBan class.
@ -540,7 +540,7 @@ class POT
{
// long2ip( ip2long('255.255.255.255') ) != '255.255.255.255' -.-'
// it's because that PHP integer types are signed
if($ip == '255.255.255.255')
if($ip === '255.255.255.255')
{
$ip = 4294967295;
}
@ -551,7 +551,7 @@ class POT
// finds ban entry
$ban = new OTS_IPBan();
$ban->find($ip, $mask);
$ban->find($ip);
return $ban->isLoaded() && $ban->isActive() && ( $ban->getExpires() == 0 || $ban->getExpires() > time() );
}

View File

@ -55,10 +55,8 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
{
$ret = '';
$argc = func_num_args();
$argv = func_get_args();
for($i = 0; $i < $argc; $i++) {
$ret .= $this->fieldName($argv[$i]) . ',';
foreach(func_get_args() as $v) {
$ret .= $this->fieldName($v) . ',';
}
$ret[strlen($ret) - 1] = '';
@ -77,7 +75,7 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
}
/**
* @param stirng $string String to be quoted.
* @param string $string String to be quoted.
* @return string Quoted string.
* @deprecated 0.0.5 Use PDO::quote().
* @version 0.0.7
@ -113,9 +111,11 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
$fields = array_keys($data);
$values = array_values($data);
$query = 'SELECT * FROM ' . $this->tableName($table) . ' WHERE (';
for ($i = 0; $i < count($fields); $i++)
$count = count($fields);
for ($i = 0; $i < $count; $i++)
$query.= $this->fieldName($fields[$i]).' = '.$this->quote($values[$i]).' AND ';
$query = substr($query, 0, strlen($query)-4);
$query = substr($query, 0, -4);
$query.=');';
$query = $this->query($query);
@ -128,20 +128,25 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
$fields = array_keys($data);
$values = array_values($data);
$query = 'INSERT INTO ' . $this->tableName($table) . ' (';
foreach ($fields as $field)
foreach ($fields as $field) {
$query.= $this->fieldName($field).',';
}
$query = substr($query, 0, strlen($query) - 1);
$query = substr($query, 0, -1);
$query .= ') VALUES (';
foreach ($values as $value)
if ($value === null)
foreach ($values as $value) {
if ($value === null) {
$query .= 'NULL,';
else
}
else {
$query .= $this->quote($value).',';
}
}
$query = substr($query, 0, strlen($query) - 1);
$query = substr($query, 0, -1);
$query .= ')';
$this->query($query);
$this->exec($query);
return true;
}
@ -153,7 +158,7 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
foreach ($fields as $field)
$query.= $this->fieldName($field).',';
$query = substr($query, 0, strlen($query) - 1);
$query = substr($query, 0, -1);
$query.= ') VALUES (';
foreach ($values as $value)
if ($value === null)
@ -161,7 +166,7 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
else
$query.= $this->quote($value).',';
$query = substr($query, 0, strlen($query) - 1);
$query = substr($query, 0, -1);
$query .= ')';
$this->query($query);
@ -174,24 +179,27 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
$values = array_values($data);
$query = 'UPDATE '.$this->tableName($table).' SET ';
for ($i = 0; $i < count($fields); $i++)
$count = count($fields);
for ($i = 0; $i < $count; $i++)
$query.= $this->fieldName($fields[$i]).' = '.$this->quote($values[$i]).', ';
$query = substr($query, 0, strlen($query)-2);
$query = substr($query, 0, -2);
$query.=' WHERE (';
$fields = array_keys($where);
$values = array_values($where);
for ($i = 0; $i < count($fields); $i++)
$count = count($fields);
for ($i = 0; $i < $count; $i++)
$query.= $this->fieldName($fields[$i]).' = '.$this->quote($values[$i]).' AND ';
$query = substr($query, 0, strlen($query)-4);
$query = substr($query, 0, -4);
if (isset($limit))
$query .=') LIMIT '.$limit.';';
else
$query .=');';
$this->query($query);
$this->exec($query);
return true;
}
@ -201,21 +209,26 @@ abstract class OTS_Base_DB extends PDO implements IOTS_DB
$values = array_values($data);
$query = 'DELETE FROM ' . $this->tableName($table) . ' WHERE (';
for ($i = 0; $i < count($fields); $i++)
$count = count($fields);
for ($i = 0; $i < $count; $i++) {
$query .= $this->fieldName($fields[$i]) . ' = ' . $this->quote($values[$i]) . ' AND ';
}
$query = substr($query, 0, strlen($query) - 4);
if ($limit > 0)
$query = substr($query, 0, -4);
if ($limit > 0) {
$query.=') LIMIT '.$limit.';';
else
}
else {
$query.=');';
}
$this->query($query);
$this->exec($query);
return true;
}
/**
* LIMIT/OFFSET clause for queries.
*
*
* @param int|bool $limit Limit of rows to be affected by query (false if no limit).
* @param int|bool $offset Number of rows to be skipped before applying query effects (false if no offset).
* @return string LIMIT/OFFSET SQL clause for query.

View File

@ -105,7 +105,7 @@ class OTS_DB_MySQL extends OTS_Base_DB
$need_revalidation = true;
if($cache->fetch('database_checksum', $tmp) && $tmp) {
$tmp = unserialize($tmp);
if(sha1($config['database_host'] . '.' . $config['database_name']) == $tmp) {
if(sha1($config['database_host'] . '.' . $config['database_name']) === $tmp) {
$need_revalidation = false;
}
}
@ -185,7 +185,7 @@ class OTS_DB_MySQL extends OTS_Base_DB
private function hasTableInternal($name) {
global $config;
return ($this->has_table_cache[$name] = $this->query("SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = " . $this->quote($config['database_name']) . " AND `TABLE_NAME` = " . $this->quote($name) . " LIMIT 1;")->rowCount() > 0);
return ($this->has_table_cache[$name] = $this->query('SELECT `TABLE_NAME` FROM `information_schema`.`tables` WHERE `TABLE_SCHEMA` = ' . $this->quote($config['database_name']) . ' AND `TABLE_NAME` = ' . $this->quote($name) . ' LIMIT 1;')->rowCount() > 0);
}
public function hasColumn($table, $column) {
@ -197,7 +197,7 @@ class OTS_DB_MySQL extends OTS_Base_DB
}
private function hasColumnInternal($table, $column) {
return $this->hasTable($table) && ($this->has_column_cache[$table . '.' . $column] = count($this->query("SHOW COLUMNS FROM `" . $table . "` LIKE '" . $column . "'")->fetchAll()) > 0);
return $this->hasTable($table) && ($this->has_column_cache[$table . '.' . $column] = count($this->query('SHOW COLUMNS FROM `' . $table . "` LIKE '" . $column . "'")->fetchAll()) > 0);
}
public function revalidateCache() {