Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additonal areas of cleanup #3762

Merged
merged 5 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 69 additions & 68 deletions SSI.php

Large diffs are not rendered by default.

61 changes: 35 additions & 26 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@
*/
class Bootstrap
{
/**
* What is returned by the function getrusage.
*
* @var mixed[]
*/
/** @var array What is returned by the function getrusage. */
protected $rusage_start = [];

/**
Expand Down Expand Up @@ -104,6 +100,7 @@ private function setConstants()
{
define('ELK', '1');
}

define('ELKBOOT', '1');

// The software version
Expand All @@ -127,7 +124,7 @@ private function setRusage()
private function clearGlobals()
{
// We don't need no globals. (a bug in "old" versions of PHP)
foreach (array('db_character_set', 'cachedir') as $variable)
foreach (['db_character_set', 'cachedir'] as $variable)
{
if (isset($GLOBALS[$variable]))
{
Expand Down Expand Up @@ -162,14 +159,7 @@ private function loadSettingsFile()

if (!defined('IGNORE_INSTALL_DIR'))
{
if (file_exists($settings_loc) && empty($_SESSION['installing']))
{
$redirec_file = 'upgrade.php';
}
else
{
$redirec_file = 'install.php';
}
$redirec_file = file_exists($settings_loc) && empty($_SESSION['installing']) ? 'upgrade.php' : 'install.php';

// To early for constants or autoloader
require_once($boarddir . '/sources/ElkArte/Server.php');
Expand All @@ -182,7 +172,7 @@ private function loadSettingsFile()
$location .= ($temp !== '/') ? $temp : '';

// Too early to use Headers class etc.
header('Location:' . $location. '/install/' . $redirec_file . '?v=' . $version_running);
header('Location:' . $location . '/install/' . $redirec_file . '?v=' . $version_running);
die();
}
}
Expand All @@ -193,8 +183,7 @@ private function loadSettingsFile()
}

/**
* Validate the paths set in Settings.php, correct as needed and move
* them to constants.
* Validate the paths set in Settings.php, correct as needed and move them to constants.
*/
private function validatePaths()
{
Expand Down Expand Up @@ -269,17 +258,29 @@ private function loadAutoloader()
}

/**
* Check if we are in maintance mode, if so end here.
* Check if we are in maintenance mode, if so end here.
*/
private function checkMaintance()
{
global $maintenance, $ssi_maintenance_off;

// Don't do john didley if the forum's been shut down completely.
if (!empty($maintenance) && $maintenance == 2 && (!isset($ssi_maintenance_off) || $ssi_maintenance_off !== true))
if (empty($maintenance))
{
return;
}

if ((int) $maintenance !== 2)
{
return;
}

if (isset($ssi_maintenance_off) && $ssi_maintenance_off === true)
{
Errors::instance()->display_maintenance_message();
return;
}

Errors::instance()->display_maintenance_message();
}

/**
Expand Down Expand Up @@ -323,7 +324,7 @@ private function bringUp()
// Our good ole' contextual array, which will hold everything
if (empty($context))
{
$context = array();
$context = [];
}
}

Expand Down Expand Up @@ -435,6 +436,7 @@ public function ssi_main()
{
$template_layers->addBegin($layer);
}

template_header();
}
else
Expand All @@ -448,14 +450,21 @@ public function ssi_main()
// Make sure they didn't muss around with the settings... but only if it's not cli.
if (isset($_SERVER['REMOTE_ADDR']) && session_id() === '')
{
trigger_error($txt['ssi_session_broken'], E_USER_NOTICE);
trigger_error($txt['ssi_session_broken']);
}

// Without visiting the forum this session variable might not be set on submit.
if (!isset($_SESSION['USER_AGENT']) && (!isset($_GET['ssi_function']) || $_GET['ssi_function'] !== 'pollVote'))
if (isset($_SESSION['USER_AGENT']))
{
$_SESSION['USER_AGENT'] = $req->user_agent();
return;
}

if (isset($_GET['ssi_function']) && $_GET['ssi_function'] === 'pollVote')
{
return;
}

$_SESSION['USER_AGENT'] = $req->user_agent();
}

/**
Expand All @@ -468,8 +477,8 @@ private function _validRequestCheck()
// Check on any hacking attempts.
if (
isset($_REQUEST['GLOBALS']) || isset($_COOKIE['GLOBALS'])
|| isset($_REQUEST['ssi_theme']) && (int) $_REQUEST['ssi_theme'] == (int) $ssi_theme
|| isset($_COOKIE['ssi_theme']) && (int) $_COOKIE['ssi_theme'] == (int) $ssi_theme
|| isset($_REQUEST['ssi_theme']) && (int) $_REQUEST['ssi_theme'] === (int) $ssi_theme
|| isset($_COOKIE['ssi_theme']) && (int) $_COOKIE['ssi_theme'] === (int) $ssi_theme
|| isset($_REQUEST['ssi_layers'], $ssi_layers) && $_REQUEST['ssi_layers'] == $ssi_layers
|| isset($_REQUEST['context']))
{
Expand Down
47 changes: 24 additions & 23 deletions install/CommonCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function test_db_connection()
{
return $class::initiate($db_server, $db_name, $db_user, $db_passwd, $db_prefix, $db_options);
}
catch (\Exception $e)
catch (\Exception)
{
return false;
}
Expand Down Expand Up @@ -250,7 +250,7 @@ function saveFileSettings($config_vars, $settingsArray)
}

$comment = strstr(substr(un_htmlspecialchars($settingsArray[$i]), strpos(un_htmlspecialchars($settingsArray[$i]), ';')), '#');
$settingsArray[$i] = '$' . $var . ' = \'' . $val . '\';' . ($comment == '' ? '' : "\t\t" . rtrim($comment)) . "\n";
$settingsArray[$i] = '$' . $var . " = '" . $val . "';" . ($comment === '' ? '' : "\t\t" . rtrim($comment)) . "\n";

unset($config_vars[$var]);
}
Expand All @@ -265,13 +265,13 @@ function saveFileSettings($config_vars, $settingsArray)
{
if ($val !== '#remove#')
{
$settingsArray[$i++] = "\n" . '$' . $var . ' = \'' . $val . '\';' . "\n";
$settingsArray[$i++] = "\n" . '$' . $var . " = '" . $val . "';" . "\n";
}
}
}

// Write out the new settings.php file
if (trim($settingsArray[0]) != '<?php')
if (trim($settingsArray[0]) !== '<?php')
{
array_unshift($settingsArray,'<?php' . "\n" );
}
Expand All @@ -280,7 +280,7 @@ function saveFileSettings($config_vars, $settingsArray)

// Don't let the OPcache fool us, we need the new file
if ($result !== false && extension_loaded('Zend OPcache') && ini_get('opcache.enable') &&
((ini_get('opcache.restrict_api') === '' || stripos(BOARDDIR, ini_get('opcache.restrict_api')) !== 0)))
((ini_get('opcache.restrict_api') === '' || stripos(BOARDDIR, (string) ini_get('opcache.restrict_api')) !== 0)))
{
opcache_invalidate(TMP_BOARDDIR . '/Settings.php', true);
}
Expand Down Expand Up @@ -378,7 +378,7 @@ function makeFilesWritable(&$files)
$upcontext['chmod']['ftp_error'] = $ftp->last_message ?? '';
}

list ($username, $detect_path, $found_path) = $ftp->detect_path(TMP_BOARDDIR);
[$username, $detect_path, $found_path] = $ftp->detect_path(TMP_BOARDDIR);

if ($found_path || !isset($upcontext['chmod']['path']))
{
Expand Down Expand Up @@ -430,7 +430,7 @@ function makeFilesWritable(&$files)
}

// Assuming that didn't work calculate the path without the boarddir.
if (!is_writable($file) && strpos($file, BOARDDIR) === 0)
if (!is_writable($file) && strpos($file, (string) BOARDDIR) === 0)
{
$ftp_file = strtr($file, [$_SESSION['installer_temp_ftp']['root'] => '']);
$ftp->chmod($ftp_file, 0755);
Expand Down Expand Up @@ -510,6 +510,7 @@ function definePaths()
DEFINE('CACHEDIR', $cachedir);

}

if (!defined('EXTDIR'))
{
DEFINE('EXTDIR', $extdir);
Expand Down Expand Up @@ -555,10 +556,13 @@ class Ftp_Connection
{
/** @var resource|string holds the connection response */
public $connection;

/** @var string|boolean holds any errors */
public $error;

/** @var string holds last message from the server */
public $last_message;

/** @var array Passive connection */
public $pasv;

Expand Down Expand Up @@ -601,10 +605,12 @@ public function connect($ftp_server, $ftp_port = 21, $ftp_user = 'anonymous', $f
{
$ftp_server = 'ssl://' . substr($ftp_server, 7);
}

if (strpos($ftp_server, 'http://') === 0)
{
$ftp_server = substr($ftp_server, 7);
}

$ftp_server = strtr($ftp_server, ['/' => '', ':' => '', '@' => '']);

// Connect to the FTP server.
Expand Down Expand Up @@ -718,17 +724,14 @@ public function unlink($ftp_file)

// Delete file X.
fwrite($this->connection, 'DELE ' . $ftp_file . "\r\n");

// Still no love?
if (!$this->check_response(250))
{
fwrite($this->connection, 'RMD ' . $ftp_file . "\r\n");
$this->error = 'bad_file';

// Still no love?
if (!$this->check_response(250))
{
$this->error = 'bad_file';

return false;
}
return false;
}

return true;
Expand Down Expand Up @@ -880,6 +883,7 @@ public function list_dir($ftp_path = '', $search = false)
{
$data .= fread($fp, 4096);
}

fclose($fp);

// Everything go okay?
Expand All @@ -906,6 +910,7 @@ public function locate($file, $listing = null)
{
$listing = $this->list_dir('', true);
}

$listing = explode("\n", $listing);

@fwrite($this->connection, 'PWD' . "\r\n");
Expand All @@ -916,18 +921,11 @@ public function locate($file, $listing = null)
} while (substr($response, 3, 1) !== ' ' && time() - $time < 5);

// Check for 257!
if (preg_match('~^257 "(.+?)" ~', $response, $match) != 0)
{
$current_dir = strtr($match[1], ['""' => '"']);
}
else
{
$current_dir = '';
}
$current_dir = preg_match('~^257 "(.+?)" ~', $response, $match) === 1 ? strtr($match[1], ['""' => '"']) : '';

for ($i = 0, $n = count($listing); $i < $n; $i++)
{
if (trim($listing[$i]) == '' && isset($listing[$i + 1]))
if (trim($listing[$i]) === '' && isset($listing[$i + 1]))
{
$current_dir = substr(trim($listing[++$i]), 0, -1);
$i++;
Expand All @@ -940,10 +938,12 @@ public function locate($file, $listing = null)
{
return $listing[$i];
}

if (substr($file, -1) === '*' && strpos($file, substr($listing[$i], 0, strlen($file) - 1)) === 0)
{
return $listing[$i];
}

if (basename($listing[$i]) === $file || $listing[$i] === $file)
{
return $listing[$i];
Expand Down Expand Up @@ -1037,6 +1037,7 @@ public function detect_path($filesystem_path, $lookup_file = null)
{
$found_path = dirname($this->locate(basename($lookup_file)));
}

if ($found_path != false)
{
$path = $found_path;
Expand Down
Loading
Loading