* some optimizations & fixes according to awesome PHPStorm Php Inspections plugin!

This commit is contained in:
slawkens 2018-03-21 13:52:41 +01:00
parent c48452b37d
commit eab416c0b4
9 changed files with 191 additions and 191 deletions

View File

@ -30,7 +30,7 @@ define('MYAAC_VERSION', '0.8.0-dev');
define('DATABASE_VERSION', 23);
define('TABLE_PREFIX', 'myaac_');
define('START_TIME', microtime(true));
define('MYAAC_OS', (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') ? 'WINDOWS' : (strtoupper(PHP_OS) == 'DARWIN' ? 'MAC' : 'LINUX'));
define('MYAAC_OS', stripos(PHP_OS, 'WIN') === 0 ? 'WINDOWS' : (strtoupper(PHP_OS) === 'DARWIN' ? 'MAC' : 'LINUX'));
// account flags
define('FLAG_ADMIN', 1);
@ -54,7 +54,7 @@ define('TICKER', 2);
define('ARTICLE', 3);
// directories
define('BASE', dirname(__FILE__) . '/');
define('BASE', __DIR__ . '/');
define('ADMIN', BASE . 'admin/');
define('SYSTEM', BASE . 'system/');
define('CACHE', SYSTEM . 'cache/');
@ -87,16 +87,15 @@ define('TFS_LAST', TFS_03);
// basedir
$basedir = '';
$tmp = explode('/', $_SERVER['SCRIPT_NAME']);
$size = sizeof($tmp) - 1;
$size = count($tmp) - 1;
for($i = 1; $i < $size; $i++)
$basedir .= '/' . $tmp[$i];
$basedir = str_replace('/admin', '', $basedir);
$basedir = str_replace('/install', '', $basedir);
$basedir = str_replace(array('/admin', '/install'), '', $basedir);
define('BASE_DIR', $basedir);
if(isset($_SERVER['HTTP_HOST'])) {
if (isset($_SERVER['HTTPS'][0]) && $_SERVER['HTTPS'] == 'on')
if (isset($_SERVER['HTTPS'][0]) && $_SERVER['HTTPS'] === 'on')
define('SERVER_URL', 'https://' . $_SERVER['HTTP_HOST']);
else
define('SERVER_URL', 'http://' . $_SERVER['HTTP_HOST']);

View File

@ -29,8 +29,8 @@
// ini_set('display_startup_errors', 1);
// error_reporting(E_ALL);
require_once('common.php');
require_once(SYSTEM . 'functions.php');
require_once 'common.php';
require_once SYSTEM . 'functions.php';
$uri = $_SERVER['REQUEST_URI'];
@ -48,7 +48,7 @@ if(preg_match("/^[A-Za-z0-9-_%\'+]+\.png$/i", $uri)) {
$_REQUEST['name'] = urldecode($tmp[0]);
chdir(TOOLS . 'signature');
include(TOOLS . 'signature/index.php');
include TOOLS . 'signature/index.php';
exit();
}
else if(preg_match("/^(.*)\.(gif|jpg|png|jpeg|tiff|bmp|css|js|less|map|html|php|zip|rar|gz|ttf|woff|ico)$/i", $_SERVER['REQUEST_URI'])) {
@ -57,7 +57,7 @@ else if(preg_match("/^(.*)\.(gif|jpg|png|jpeg|tiff|bmp|css|js|less|map|html|php|
}
if(file_exists(BASE . 'config.local.php'))
require_once(BASE . 'config.local.php');
require_once BASE . 'config.local.php';
if(file_exists(BASE . 'install') && (!isset($config['installed']) || !$config['installed']))
{
@ -120,8 +120,7 @@ else {
'/^news\/archive\/?$/' => array('subtopic' => 'newsarchive'),
'/^news\/archive\/[0-9]+\/?$/' => array('subtopic' => 'newsarchive', 'id' => '$2'),
'/^polls\/[0-9]+\/?$/' => array('subtopic' => 'polls', 'id' => '$1'),
'/^spells\/[A-Za-z0-9-_%]+\/[A-Za-z0-9-_]+\/?$/' => array('subtopic' => 'spells', 'vocation' => '$1', 'order' => '$2'),
'/^gifts\/history\/?$/' => array('subtopic' => 'gifts', 'action' => 'show_history'),
'/^spells\/[A-Za-z0-9-_%]+\/[A-Za-z0-9-_]+\/?$/' => array('subtopic' => 'spells', 'vocation' => '$1', 'order' => '$2')
);
foreach($rules as $rule => $redirect) {
@ -164,15 +163,15 @@ define('PAGE', $page);
$template_place_holders = array();
require_once(SYSTEM . 'init.php');
require_once SYSTEM . 'init.php';
// event system
require_once(SYSTEM . 'hooks.php');
require_once SYSTEM . 'hooks.php';
$hooks = new Hooks();
$hooks->load();
require_once(SYSTEM . 'template.php');
require_once(SYSTEM . 'login.php');
require_once(SYSTEM . 'status.php');
require_once SYSTEM . 'template.php';
require_once SYSTEM . 'login.php';
require_once SYSTEM . 'status.php';
$twig->addGlobal('config', $config);
$twig->addGlobal('status', $status);
@ -236,11 +235,11 @@ if(isset($config['anonymous_usage_statistics']) && $config['anonymous_usage_stat
}
if($config['views_counter'])
require_once(SYSTEM . 'counter.php');
require_once SYSTEM . 'counter.php';
if($config['visitors_counter'])
{
require_once(SYSTEM . 'libs/visitors.php');
require_once SYSTEM . 'libs/visitors.php';
$visitors = new Visitors($config['visitors_counter_ttl']);
}
@ -264,7 +263,7 @@ if(fetchDatabaseConfig('site_closed', $site_closed)) {
if(!$logged)
{
ob_start();
require(SYSTEM . 'pages/accountmanagement.php');
require SYSTEM . 'pages/accountmanagement.php';
$content .= ob_get_contents();
ob_end_clean();
$load_it = false;
@ -305,7 +304,7 @@ if($config['backward_support']) {
$config['site']['screenshot_page'] = true;
if($config['forum'] != '')
$config['forum_link'] = (strtolower($config['forum']) == 'site' ? getLink('forum') : $config['forum']);
$config['forum_link'] = (strtolower($config['forum']) === 'site' ? getLink('forum') : $config['forum']);
foreach($status as $key => $value)
$config['status']['serverStatus_' . $key] = $value;
@ -317,7 +316,7 @@ if($load_it)
$content .= '<p class="note">Site is under maintenance (closed mode). Only privileged users can see it.</p>';
if($config['backward_support'])
require(SYSTEM . 'compat_pages.php');
require SYSTEM . 'compat_pages.php';
$ignore = false;
@ -388,7 +387,7 @@ if($load_it)
ob_start();
if($hooks->trigger(HOOK_BEFORE_PAGE)) {
if(!$ignore)
require($file);
require $file;
}
if($config['backward_support'] && isset($main_content[0]))

View File

@ -9,7 +9,7 @@
*/
defined('MYAAC') or die('Direct access not allowed!');
if(!isset($config['database_type'][0]) || !isset($config['database_user'][0]) || !isset($config['database_password'][0]) || !isset($config['database_name'][0]))
if(!isset($config['database_type'][0], $config['database_user'][0], $config['database_password'][0], $config['database_name'][0]))
{
if(isset($config['lua']['sqlType'])) {// tfs 0.3
if(isset($config['lua']['mysqlHost'])) {// tfs 0.2

View File

@ -70,7 +70,7 @@ function getPlayerLink($name, $generate = true)
if(is_numeric($name))
{
$player = new OTS_Player();
$player->load(intval($name));
$player->load((int)$name);
if($player->isLoaded())
$name = $player->getName();
}
@ -88,9 +88,7 @@ function getHouseLink($name, $generate = true)
if(is_numeric($name))
{
$house = $db->query(
'SELECT ' . $db->fieldName('name') .
' FROM ' . $db->tableName('houses') .
' WHERE ' . $db->fieldName('id') . ' = ' . (int)$name);
'SELECT `name` FROM `houses` WHERE `id` = ' . (int)$name);
if($house->rowCount() > 0)
$name = $house->fetchColumn();
}
@ -122,7 +120,7 @@ function getGuildLink($name, $generate = true)
function getItemNameById($id) {
global $db;
$query = $db->query('SELECT `name` FROM `' . TABLE_PREFIX . 'items` WHERE `id` = ' . $db->quote($id) . ' LIMIT 1;');
if($query->rowCount() == 1) {
if($query->rowCount() === 1) {
$item = $query->fetch();
return $item['name'];
}
@ -154,7 +152,7 @@ function getFlagImage($country)
global $config;
if(!isset($config['countries']))
require(SYSTEM . 'countries.conf.php');
require SYSTEM . 'countries.conf.php';
if(!isset($config['countries'][$country])) {
return '';
@ -176,16 +174,18 @@ function getBoolean($v)
}
if(is_numeric($v))
return intval($v) > 0;
return (int)$v > 0;
$v = strtolower($v);
return $v == 'yes' || $v == 'true';
return $v === 'yes' || $v === 'true';
}
/**
* Generates random string.
*
* @param int $length Length of the generated string.
* @param bool $lowCase Should lower case characters be used?
* @param bool $upCase Should upper case characters be used?
* @param bool $numeric Should numbers by used too?
* @param bool $special Should special characters by used?
* @return string Generated string.
@ -242,7 +242,7 @@ function fetchDatabaseConfig($name, &$value)
{
global $db;
$query = $db->query('SELECT ' . $db->fieldName('value') . ' FROM ' . $db->tableName(TABLE_PREFIX . 'config') . ' WHERE ' . $db->fieldName('name') . ' = ' . $db->quote($name));
$query = $db->query('SELECT `value` FROM `' . TABLE_PREFIX . 'config` WHERE `name` = ' . $db->quote($name));
if($query->rowCount() <= 0)
return false;
@ -297,9 +297,9 @@ function encrypt($str)
$str .= $config['database_salt'];
$encryptionType = $config['database_encryption'];
if(isset($encryptionType) && strtolower($encryptionType) != 'plain')
if(isset($encryptionType) && strtolower($encryptionType) !== 'plain')
{
if($encryptionType == 'vahash')
if($encryptionType === 'vahash')
return base64_encode(hash('sha256', $str));
return hash($encryptionType, $str);
@ -350,6 +350,8 @@ function delete_player($name)
$player->delete();
return true;
}
return false;
}
//delete guild with id
@ -407,7 +409,7 @@ function tickers()
{
global $tickers_content, $featured_article;
if(PAGE == 'news') {
if(PAGE === 'news') {
if(isset($tickers_content))
return $tickers_content . $featured_article;
}
@ -429,9 +431,9 @@ 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;
@ -574,7 +576,7 @@ function getCreatureName($killer, $showStatus = false, $extendedInfo = false)
$players_rows .= "item or field";
else
{
if(in_array(substr(strtolower($killer), 0, 1), $vowels))
if(in_array(strtolower($killer[0]), $vowels))
$players_rows .= "an ";
else
$players_rows .= "a ";
@ -746,7 +748,7 @@ function get_templates()
$path = TEMPLATES;
foreach(scandir($path) as $file)
{
if($file[0] != '.' && $file != '..' && is_dir($path . $file))
if($file[0] !== '.' && $file !== '..' && is_dir($path . $file))
$ret[] = $file;
}
@ -765,7 +767,7 @@ function get_plugins()
foreach(scandir($path) as $file) {
$file_ext = pathinfo($file, PATHINFO_EXTENSION);
$file_name = pathinfo($file, PATHINFO_FILENAME);
if ($file == '.' || $file == '..' || $file == 'disabled' || $file == 'example.json' || is_dir($path . $file) || $file_ext != 'json')
if ($file === '.' || $file === '..' || $file === 'disabled' || $file === 'example.json' || $file_ext !== 'json' || is_dir($path . $file))
continue;
$ret[] = str_replace('.json', '', $file_name);
@ -797,7 +799,7 @@ function _mail($to, $subject, $body, $altBody = '', $add_html_tags = true)
global $mailer, $config;
if(!$mailer)
{
require(SYSTEM . 'libs/phpmailer/PHPMailerAutoload.php');
require SYSTEM . 'libs/phpmailer/PHPMailerAutoload.php';
$mailer = new PHPMailer();
$mailer->setLanguage('en', LIBS . 'phpmailer/language/');
}
@ -871,9 +873,7 @@ function load_config_lua($filename)
}
$result = array();
$config_string = file_get_contents($filename);
$config_string = str_replace("\r\n", "\n", $config_string);
$config_string = str_replace("\r", "\n", $config_string);
$config_string = str_replace(array("\r\n", "\r"), "\n", file_get_contents($filename));
$lines = explode("\n", $config_string);
if(count($lines) > 0)
foreach($lines as $ln => $line)
@ -984,12 +984,14 @@ function getTopPlayers($limit = 5) {
$query = $db->query('SELECT `player_id` FROM `players_online` WHERE `player_id` = ' . $player['id']);
$player['online'] = ($query->rowCount() > 0 ? 1 : 0);
}
unset($player);
}
$i = 0;
foreach($players as &$player) {
$player['rank'] = ++$i;
}
unset($player);
if($cache->enabled())
$cache->set('top_' . $limit . '_level', serialize($players), 120);
@ -1008,7 +1010,7 @@ function deleteDirectory($dir) {
}
foreach(scandir($dir) as $item) {
if($item == '.' || $item == '..') {
if($item === '.' || $item === '..') {
continue;
}
@ -1021,6 +1023,6 @@ function deleteDirectory($dir) {
}
// validator functions
require_once(LIBS . 'validator.php');
require_once(SYSTEM . 'compat.php');
require_once LIBS . 'validator.php';
require_once SYSTEM . 'compat.php';
?>

View File

@ -10,9 +10,9 @@
defined('MYAAC') or die('Direct access not allowed!');
// load configuration
require_once(BASE . 'config.php');
require_once BASE . 'config.php';
if(file_exists(BASE . 'config.local.php')) // user customizations
require(BASE . 'config.local.php');
require BASE . 'config.local.php';
if(!isset($config['installed']) || !$config['installed']) {
die('MyAAC has not been installed yet or there was error during installation. Please install again.');
@ -20,7 +20,7 @@ if(!isset($config['installed']) || !$config['installed']) {
date_default_timezone_set($config['date_timezone']);
// take care of trailing slash at the end
if($config['server_path'][strlen($config['server_path']) - 1] != '/')
if($config['server_path'][strlen($config['server_path']) - 1] !== '/')
$config['server_path'] .= '/';
// enable gzip compression if supported by the browser
@ -28,11 +28,11 @@ if($config['gzip_output'] && isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($
ob_start('ob_gzhandler');
// cache
require_once(SYSTEM . 'libs/cache.php');
require_once SYSTEM . 'libs/cache.php';
$cache = Cache::getInstance($config['cache_engine'], $config['cache_prefix']);
// twig
require_once(LIBS . 'Twig/Autoloader.php');
require_once LIBS . 'Twig/Autoloader.php';
Twig_Autoloader::register();
$twig_loader = new Twig_Loader_Filesystem(SYSTEM . 'templates');
@ -117,35 +117,35 @@ if(isset($config['lua']['servername']))
if(isset($config['lua']['houserentperiod']))
$config['lua']['houseRentPeriod'] = $config['lua']['houserentperiod'];
if($config['item_images_url'][strlen($config['item_images_url']) - 1] != '/')
if($config['item_images_url'][strlen($config['item_images_url']) - 1] !== '/')
$config['item_images_url'] .= '/';
// localize data/ directory
if(isset($config['lua']['dataDirectory'][0]))
{
$tmp = $config['lua']['dataDirectory'];
if($tmp[0] != '/')
if($tmp[0] !== '/')
$tmp = $config['server_path'] . $tmp;
if($tmp[strlen($tmp) - 1] != '/') // do not forget about trailing slash
if($tmp[strlen($tmp) - 1] !== '/') // do not forget about trailing slash
$tmp .= '/';
}
else if(isset($config['lua']['data_directory'][0]))
{
$tmp = $config['lua']['data_directory'];
if($tmp[0] != '/')
if($tmp[0] !== '/')
$tmp = $config['server_path'] . $tmp;
if($tmp[strlen($tmp) - 1] != '/') // do not forget about trailing slash
if($tmp[strlen($tmp) - 1] !== '/') // do not forget about trailing slash
$tmp .= '/';
}
else if(isset($config['lua']['datadir'][0]))
{
$tmp = $config['lua']['datadir'];
if($tmp[0] != '/')
if($tmp[0] !== '/')
$tmp = $config['server_path'] . $tmp;
if($tmp[strlen($tmp) - 1] != '/') // do not forget about trailing slash
if($tmp[strlen($tmp) - 1] !== '/') // do not forget about trailing slash
$tmp .= '/';
}
else

View File

@ -260,7 +260,7 @@ class POT
$this->path = str_replace('\\', '/', $path);
// appends ending slash to directory path
if( substr($this->path, -1) != '/')
if( substr($this->path, -1) !== '/')
{
$this->path .= '/';
}
@ -286,7 +286,7 @@ class POT
private function __construct()
{
// default POT directory
$this->path = dirname(__FILE__) . '/';
$this->path = __DIR__ . '/';
// registers POT autoload mechanism
spl_autoload_register( array($this, 'loadClass') );
}

View File

@ -31,7 +31,7 @@ if($current_session !== false)
}
}
if(ACTION == 'logout' && !isset($_REQUEST['account_login'])) {
if(ACTION === 'logout' && !isset($_REQUEST['account_login'])) {
if(isset($account_logged) && $account_logged->isLoaded()) {
if($hooks->trigger(HOOK_LOGOUT, array('account' => $account_logged, 'password' => getSession('password')))) {
unsetSession('account');
@ -52,7 +52,7 @@ if(ACTION == 'logout' && !isset($_REQUEST['account_login'])) {
else
{
// new login with data from form
if(!$logged && isset($_POST['account_login']) && isset($_POST['password_login']))
if(!$logged && isset($_POST['account_login'], $_POST['password_login']))
{
$login_account = $_POST['account_login'];
$login_password = $_POST['password_login'];

View File

@ -56,7 +56,7 @@ if($cache->enabled())
if($fetch_from_db)
{
// get info from db
$status_query = $db->query('SELECT ' . $db->fieldName('name') . ', ' . $db->fieldName('value') . ' FROM ' . $db->tableName(TABLE_PREFIX . 'config') . ' WHERE ' . $db->fieldName('name') . ' LIKE "%status%"');
$status_query = $db->query('SELECT `name`, `value` FROM `' . TABLE_PREFIX . 'config` WHERE ' . $db->fieldName('name') . ' LIKE "%status%"');
if($status_query->rowCount() <= 0) // empty, just insert it
{
foreach($status as $key => $value)

View File

@ -66,7 +66,7 @@ if($exists || ($config['backward_support'] && file_exists(BASE . $template_path
$config[$key] = $value;
}
else if(file_exists(BASE . $template_path . '/config.php'))
require(BASE . $template_path . '/config.php');
require BASE . $template_path . '/config.php';
$template = array();
$template['link_account_manage'] = getLink('account/manage');