Skip to content

Commit

Permalink
Refactor OC\Server::getRequest
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Summers <18727110+summersab@users.noreply.github.com>
  • Loading branch information
summersab committed Aug 30, 2023
1 parent 613cd16 commit bb9f13f
Show file tree
Hide file tree
Showing 25 changed files with 63 additions and 33 deletions.
3 changes: 2 additions & 1 deletion console.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
require_once __DIR__ . '/lib/versioncheck.php';

use OC\Console\Application;
use OCP\IRequest;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;

Expand Down Expand Up @@ -92,7 +93,7 @@ function exceptionHandler($exception) {
$application = new Application(
\OC::$server->getConfig(),
\OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class),
\OC::$server->getRequest(),
\OC::$server->get(IRequest::class),
\OC::$server->get(\Psr\Log\LoggerInterface::class),
\OC::$server->query(\OC\MemoryInfo::class)
);
Expand Down
4 changes: 3 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
*
*/
require_once __DIR__ . '/lib/versioncheck.php';

use OCP\IRequest;
use Psr\Log\LoggerInterface;

try {
Expand Down Expand Up @@ -64,7 +66,7 @@
OC_Template::printExceptionErrorPage($ex, 500);
}
} catch (\OC\User\LoginException $ex) {
$request = \OC::$server->getRequest();
$request = \OC::$server->get(IRequest::class);
/**
* Routes with the @CORS annotation and other API endpoints should
* not return a webpage, so we only print the error page when html is accepted,
Expand Down
2 changes: 1 addition & 1 deletion lib/private/AppFramework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static function main(string $controllerName, string $methodName, DIContai
$expireDate,
$container->getServer()->getWebRoot(),
null,
$container->getServer()->getRequest()->getServerProtocol() === 'https',
$container->getServer()->get(IRequest::class)->getServerProtocol() === 'https',
true,
$sameSite
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function __construct(string $appName, array $urlParams = [], ServerContai
$this->registerService('Protocol', function (ContainerInterface $c) {
/** @var \OC\Server $server */
$server = $c->get(IServerContainer::class);
$protocol = $server->getRequest()->getHttpProtocol();
$protocol = $server->get(IRequest::class)->getHttpProtocol();
return new Http($_SERVER, $protocol);
});

Expand Down
3 changes: 2 additions & 1 deletion lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use Nextcloud\LogNormalizer\Normalizer;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\Log\BeforeMessageLoggedEvent;
use OCP\Log\IDataLogger;
Expand Down Expand Up @@ -263,7 +264,7 @@ public function getLogLevel($context) {
if (!empty($logCondition)) {
// check for secret token in the request
if (isset($logCondition['shared_secret'])) {
$request = \OC::$server->getRequest();
$request = \OC::$server->get(IRequest::class);

if ($request->getMethod() === 'PUT' &&
!str_contains($request->getHeader('Content-Type'), 'application/x-www-form-urlencoded') &&
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Log/LogDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OC\Log;

use OC\SystemConfig;
use OCP\IRequest;

abstract class LogDetails {
/** @var SystemConfig */
Expand All @@ -51,7 +52,7 @@ public function logDetails(string $app, $message, int $level): array {
// apply timezone if $time is created from UNIX timestamp
$time->setTimezone($timezone);
}
$request = \OC::$server->getRequest();
$request = \OC::$server->get(IRequest::class);
$reqId = $request->getId();
$remoteAddr = $request->getRemoteAddress();
// remove username/passwords from URLs before writing the to the log file
Expand Down
6 changes: 3 additions & 3 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ public function __construct($webRoot, \OC\Config $config) {
$this->registerService(IFactory::class, function (Server $c) {
return new \OC\L10N\Factory(
$c->get(\OCP\IConfig::class),
$c->getRequest(),
$c->get(IRequest::class),
$c->get(IUserSession::class),
$c->get(ICacheFactory::class),
\OC::$SERVERROOT
Expand Down Expand Up @@ -707,7 +707,7 @@ public function __construct($webRoot, \OC\Config $config) {
$this->registerService(\OCP\Activity\IManager::class, function (Server $c) {
$l10n = $this->get(IFactory::class)->get('lib');
return new \OC\Activity\Manager(
$c->getRequest(),
$c->get(IRequest::class),
$c->get(IUserSession::class),
$c->get(\OCP\IConfig::class),
$c->get(IValidator::class),
Expand Down Expand Up @@ -1161,7 +1161,7 @@ public function __construct($webRoot, \OC\Config $config) {
$classExists = false;
}

if ($classExists && $c->get(\OCP\IConfig::class)->getSystemValueBool('installed', false) && $c->get(IAppManager::class)->isInstalled('theming') && $c->getTrustedDomainHelper()->isTrustedDomain($c->getRequest()->getInsecureServerHost())) {
if ($classExists && $c->get(\OCP\IConfig::class)->getSystemValueBool('installed', false) && $c->get(IAppManager::class)->isInstalled('theming') && $c->getTrustedDomainHelper()->isTrustedDomain($c->get(IRequest::class)->getInsecureServerHost())) {
$imageManager = new ImageManager(
$c->get(\OCP\IConfig::class),
$c->getAppDataDir('theming'),
Expand Down
3 changes: 2 additions & 1 deletion lib/private/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
use OCP\Defaults;
use OCP\IGroup;
use OCP\IL10N;
use OCP\IRequest;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -313,7 +314,7 @@ public function install($options) {
return $error;
}

$request = \OC::$server->getRequest();
$request = \OC::$server->get(IRequest::class);

//no errors, good
if (isset($options['trusted_domains'])
Expand Down
3 changes: 2 additions & 1 deletion lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use OCP\IConfig;
use OCP\IInitialStateService;
use OCP\INavigationManager;
use OCP\IRequest;
use OCP\IUserSession;
use OCP\Support\Subscription\IRegistry;
use OCP\Util;
Expand Down Expand Up @@ -251,7 +252,7 @@ public function __construct($renderAs, $appId = '') {
}

try {
$pathInfo = \OC::$server->getRequest()->getPathInfo();
$pathInfo = \OC::$server->get(IRequest::class)->getPathInfo();
} catch (\Exception $e) {
$pathInfo = '';
}
Expand Down
3 changes: 2 additions & 1 deletion lib/private/User/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IGroup;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserBackend;
use OCP\IUserManager;
Expand Down Expand Up @@ -234,7 +235,7 @@ public function checkPassword($loginName, $password) {
$result = $this->checkPasswordNoLogging($loginName, $password);

if ($result === false) {
\OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']);
\OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->get(IRequest::class)->getRemoteAddress(). '\')', ['app' => 'core']);
}

return $result;
Expand Down
4 changes: 2 additions & 2 deletions lib/private/User/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@ public function logout() {
* @param string $token
*/
public function setMagicInCookie($username, $token) {
$secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https';
$secureCookie = OC::$server->get(IRequest::class)->getServerProtocol() === 'https';
$webRoot = \OC::$WEBROOT;
if ($webRoot === '') {
$webRoot = '/';
Expand Down Expand Up @@ -1002,7 +1002,7 @@ public function setMagicInCookie($username, $token) {
*/
public function unsetMagicInCookie() {
//TODO: DI for cookies and IRequest
$secureCookie = OC::$server->getRequest()->getServerProtocol() === 'https';
$secureCookie = OC::$server->get(IRequest::class)->getServerProtocol() === 'https';

unset($_COOKIE['nc_username']); //TODO: DI
unset($_COOKIE['nc_token']);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/legacy/OC_API.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
use OCP\API;
use OCP\AppFramework\Http;
use OCP\IRequest;

class OC_API {
/**
Expand All @@ -44,7 +45,7 @@ class OC_API {
* @psalm-taint-escape html
*/
public static function respond($result, $format = 'xml') {
$request = \OC::$server->getRequest();
$request = \OC::$server->get(IRequest::class);

// Send 401 headers if unauthorised
if ($result->getStatusCode() === \OCP\AppFramework\OCSController::RESPOND_UNAUTHORISED) {
Expand Down
3 changes: 2 additions & 1 deletion lib/private/legacy/OC_App.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
use OCP\Authentication\IAlternativeLogin;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\IRequest;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\App\DependencyAnalyzer;
use OC\App\Platform;
Expand Down Expand Up @@ -404,7 +405,7 @@ public static function getCurrentApp(): string {
return '';
}

$request = \OC::$server->getRequest();
$request = \OC::$server->get(IRequest::class);
$script = substr($request->getScriptName(), strlen(OC::$WEBROOT) + 1);
$topFolder = substr($script, 0, strpos($script, '/') ?: 0);
if (empty($topFolder)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/private/legacy/OC_Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use OCP\Files\Events\BeforeZipCreatedEvent;
use OCP\Files\Events\BeforeDirectFileDownloadEvent;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IRequest;

/**
* Class for file server access
Expand Down Expand Up @@ -174,7 +175,7 @@ public static function get($dir, $files, $params = null) {
throw new \OC\ForbiddenException($event->getErrorMessage());
}

$streamer = new Streamer(\OC::$server->getRequest(), $fileSize, $numberOfFiles);
$streamer = new Streamer(\OC::$server->get(IRequest::class), $fileSize, $numberOfFiles);
OC_Util::obEnd();

$streamer->sendHeaders($name);
Expand Down
7 changes: 5 additions & 2 deletions lib/private/legacy/OC_JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

use OCP\IRequest;

class OC_JSON {
/**
* Check if the app is enabled, send json error msg if not
Expand Down Expand Up @@ -64,12 +67,12 @@ public static function checkLoggedIn() {
* @suppress PhanDeprecatedFunction
*/
public static function callCheck() {
if (!\OC::$server->getRequest()->passesStrictCookieCheck()) {
if (!\OC::$server->get(IRequest::class)->passesStrictCookieCheck()) {
header('Location: '.\OC::$WEBROOT);
exit();
}

if (!\OC::$server->getRequest()->passesCSRFCheck()) {
if (!\OC::$server->get(IRequest::class)->passesCSRFCheck()) {
$l = \OC::$server->getL10N('lib');
self::error([ 'data' => [ 'message' => $l->t('Token expired. Please reload page.'), 'error' => 'token_expired' ]]);
exit();
Expand Down
5 changes: 4 additions & 1 deletion lib/private/legacy/OC_Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

use OCP\IRequest;

class OC_Response {
/**
* Sets the content disposition header (with possible workarounds)
* @param string $filename file name
* @param string $type disposition type, either 'attachment' or 'inline'
*/
public static function setContentDispositionHeader($filename, $type = 'attachment') {
if (\OC::$server->getRequest()->isUserAgent(
if (\OC::$server->get(IRequest::class)->isUserAgent(
[
\OC\AppFramework\Http\Request::USER_AGENT_IE,
\OC\AppFramework\Http\Request::USER_AGENT_ANDROID_MOBILE_CHROME,
Expand Down
3 changes: 2 additions & 1 deletion lib/private/legacy/OC_Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*/
use OC\TemplateLayout;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IRequest;

require_once __DIR__.'/template/functions.php';

Expand Down Expand Up @@ -277,7 +278,7 @@ public static function printErrorPage($error_msg, $hint = '', $statusCode = 500)
public static function printExceptionErrorPage($exception, $statusCode = 503) {
http_response_code($statusCode);
try {
$request = \OC::$server->getRequest();
$request = \OC::$server->get(IRequest::class);
$content = new \OC_Template('', 'exception', 'error', false);
$content->assign('errorClass', get_class($exception));
$content->assign('errorMsg', $exception->getMessage());
Expand Down
3 changes: 2 additions & 1 deletion lib/private/legacy/OC_User.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OC\User\LoginException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ILogger;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\User\Events\BeforeUserLoggedInEvent;
use OCP\User\Events\UserLoggedInEvent;
Expand Down Expand Up @@ -182,7 +183,7 @@ public static function loginWithApache(\OCP\Authentication\IApacheBackend $backe
throw new LoginException($message);
}
$userSession->setLoginName($uid);
$request = OC::$server->getRequest();
$request = OC::$server->get(IRequest::class);
$password = null;
if ($backend instanceof \OCP\Authentication\IProvideUserSecretBackend) {
$password = $backend->getCurrentUserSecret();
Expand Down
3 changes: 2 additions & 1 deletion lib/private/legacy/OC_Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
use OCP\Files\Template\ITemplateManager;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\Share\IManager;
Expand Down Expand Up @@ -785,7 +786,7 @@ public static function checkLoggedIn() {
header('Location: ' . \OC::$server->getURLGenerator()->linkToRoute(
'core.login.showLoginForm',
[
'redirect_url' => \OC::$server->getRequest()->getRequestUri(),
'redirect_url' => \OC::$server->get(IRequest::class)->getRequestUri(),
]
)
);
Expand Down
3 changes: 2 additions & 1 deletion lib/public/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

use OC\AppScriptDependency;
use OC\AppScriptSort;
use OCP\IRequest;
use bantu\IniGetWrapper\IniGetWrapper;
use Psr\Container\ContainerExceptionInterface;

Expand Down Expand Up @@ -294,7 +295,7 @@ public static function linkToRemote($service) {
* @since 5.0.0
*/
public static function getServerHostName() {
$host_name = \OC::$server->getRequest()->getServerHost();
$host_name = \OC::$server->get(IRequest::class)->getServerHost();
// strip away port number (if existing)
$colon_pos = strpos($host_name, ':');
if ($colon_pos != false) {
Expand Down
4 changes: 3 additions & 1 deletion ocs-provider/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@

require_once __DIR__ . '/../lib/base.php';

use OCP\IRequest;

header('Content-Type: application/json');

$server = \OC::$server;

$controller = new \OC\OCS\Provider(
'ocs_provider',
$server->getRequest(),
$server->get(IRequest::class),
$server->getAppManager()
);
echo $controller->buildProviderList()->render();
4 changes: 3 additions & 1 deletion ocs/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
require_once __DIR__ . '/../lib/versioncheck.php';
require_once __DIR__ . '/../lib/base.php';

use OCP\IRequest;

header('Content-type: application/xml');

$request = \OC::$server->getRequest();
$request = \OC::$server->get(IRequest::class);

$url = $request->getServerProtocol() . '://' . substr($request->getServerHost() . $request->getRequestUri(), 0, -17).'ocs/v1.php/';

Expand Down
Loading

0 comments on commit bb9f13f

Please sign in to comment.