Skip to content

Commit

Permalink
Replace legacy getBaseUrl() with Laravel url() helper (#2144)
Browse files Browse the repository at this point in the history
This maintenance PR replaces the legacy config base url method with
Laravel's `url()` helper.
  • Loading branch information
williamjallen authored Apr 17, 2024
1 parent 71b230c commit 94d63da
Show file tree
Hide file tree
Showing 19 changed files with 31 additions and 379 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/CoverageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public function manageCoverage(): View|RedirectResponse
$messagePlainText .= 'Details on the submission can be found at ';

$config = Config::getInstance();
$messagePlainText .= $config->getBaseUrl();
$messagePlainText .= url('/');
$messagePlainText .= "\n\n";
$serverName = $config->get('CDASH_SERVER_NAME');
if (strlen($serverName) == 0) {
Expand Down
6 changes: 1 addition & 5 deletions app/Http/Controllers/ManageProjectRolesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,6 @@ public function viewPage(): View|RedirectResponse

private function register_user($projectid, $email, $firstName, $lastName, $repositoryCredential)
{
$config = Config::getInstance();

if(config('auth.project_admin_registration_form_enabled') === false) {
return '<error>Users cannot be registered via this form at the current time.</error>';
}
Expand Down Expand Up @@ -482,8 +480,6 @@ private function register_user($projectid, $email, $firstName, $lastName, $repos
$UserProject->ProjectId = 0;
$UserProject->AddCredential($email); // Add the email by default

$currentURI = $config->getBaseUrl();

$prefix = '';
if (strlen($firstName) > 0) {
$prefix = ' ';
Expand All @@ -496,7 +492,7 @@ private function register_user($projectid, $email, $firstName, $lastName, $repos
// Send the email
$text = 'Hello' . $prefix . $firstName . ",\n\n";
$text .= 'You have been registered to CDash because you have access to the source repository for ' . $projectname . "\n";
$text .= 'To access your CDash account: ' . $currentURI . "/user\n";
$text .= 'To access your CDash account: ' . url('/user') . "\n";
$text .= 'Your login is: ' . $email . "\n";
$text .= 'Your password is: ' . $pass . "\n\n";
$text .= 'Generated by CDash.';
Expand Down
3 changes: 1 addition & 2 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,7 @@ public function recoverPassword(): View
// Create a new password
$password = generate_password(10);

$currentURI = $config->getBaseUrl();
$url = $currentURI . '/user';
$url = url('/user');

$text = "Hello,\n\n You have asked to recover your password for CDash.\n\n";
$text .= 'Your new password is: ' . $password . "\n";
Expand Down
5 changes: 1 addition & 4 deletions app/cdash/app/Model/BuildError.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace CDash\Model;

use App\Utils\RepositoryUtils;
use CDash\Config;

use PDO;
use App\Models\BuildError as EloquentBuildError;
Expand Down Expand Up @@ -157,8 +156,6 @@ public static function marshal($data, Project $project, $revision): array
*/
public function GetUrlForSelf(): string
{
$config = Config::getInstance();
$url = $config->getBaseUrl();
return "{$url}/viewBuildError.php?type={$this->Type}&buildid={$this->BuildId}";
return url('/viewBuildError.php') . "?type={$this->Type}&buildid={$this->BuildId}";
}
}
5 changes: 1 addition & 4 deletions app/cdash/app/Model/BuildFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
namespace CDash\Model;

use App\Utils\RepositoryUtils;
use CDash\Config;
use CDash\Database;
use PDO;

Expand Down Expand Up @@ -330,8 +329,6 @@ public static function marshal($data, Project $project, $revision, $linkifyOutpu
/** Returns a self referencing URI for a the current BuildFailure. */
public function GetUrlForSelf(): string
{
$config = Config::getInstance();
$url = $config->getBaseUrl();
return "{$url}/viewBuildError.php?type={$this->Type}&buildid={$this->BuildId}";
return url('/viewBuildError.php') . "?type={$this->Type}&buildid={$this->BuildId}";
}
}
4 changes: 1 addition & 3 deletions app/cdash/app/Model/BuildUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
=========================================================================*/
namespace CDash\Model;

use CDash\Config;
use CDash\Database;
use Illuminate\Support\Facades\DB;
use PDO;
Expand Down Expand Up @@ -432,7 +431,6 @@ public function FillFromBuildId(): bool
*/
public function GetUrlForSelf(): string
{
$config = Config::getInstance();
return "{$config->getBaseUrl()}/build/{$this->BuildId}/update";
return url("/build/{$this->BuildId}/update");
}
}
6 changes: 1 addition & 5 deletions app/cdash/app/Model/DynamicAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
=========================================================================*/
namespace CDash\Model;

use CDash\Config;
use CDash\Database;
use Illuminate\Support\Facades\DB;

Expand Down Expand Up @@ -336,9 +335,6 @@ public function GetLastId($build): int
/** Returns a self referencing URI for the current DynamicAnalysis. */
public function GetUrlForSelf(): string
{
$config = Config::getInstance();
$base_url = $config->getBaseUrl();

return "{$base_url}/viewDynamicAnalysisFile.php?id={$this->Id}";
return url('/viewDynamicAnalysisFile.php') . "?id={$this->Id}";
}
}
3 changes: 1 addition & 2 deletions app/cdash/app/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -1288,8 +1288,7 @@ public function CheckForTooManyBuilds(): bool

// Perform the "daily update" step asychronously here via cURL.
if (config('cdash.daily_updates') === true) {
$baseUrl = Config::getInstance()->getBaseUrl();
self::curlRequest("{$baseUrl}/ajax/dailyupdatescurl.php?projectid={$this->Id}");
self::curlRequest(url('/ajax/dailyupdatescurl.php') . "?projectid={$this->Id}");
}

$max_builds = (int) config('cdash.builds_per_project');
Expand Down
68 changes: 0 additions & 68 deletions app/cdash/include/CDash/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,72 +44,4 @@ public function getServer(): string
}
return $server;
}

/**
* @deprecated 09/04/2023 Use url() instead.
*/
public function getProtocol(): string
{
$protocol = 'http';
if ($this->get('CDASH_USE_HTTPS') ||
(isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)) {
$protocol = 'https';
}
return $protocol;
}

/**
* @deprecated 09/04/2023 Use url() instead.
*/
public function getServerPort(): ?string
{
if (isset($_SERVER['SERVER_PORT'])
&& $_SERVER['SERVER_PORT'] != 80
&& $_SERVER['SERVER_PORT'] != 443) {
return $_SERVER['SERVER_PORT'];
}
return null;
}

private function getPath(): string
{
$path = config('cdash.curl_localhost_prefix') ?: $_SERVER['REQUEST_URI'];
if (!str_starts_with($path, '/')) {
$path = "/{$path}";
}
return $path;
}

/**
* @deprecated 09/04/2023 Use url() instead.
*/
public function getBaseUrl(): string
{
$uri = config('app.url');

if (!$uri) {
$protocol = $this->getProtocol();
$host = $this->getServer();
$port = $this->getServerPort() !== null ? ":{$this->getServerPort()}" : '';
$path = $this->getPath();

// Trim any known subdirectories off of the path.
$subdirs = ['/ajax/', '/api/', '/auth/'];
foreach ($subdirs as $subdir) {
$pos = strpos($path, $subdir);
if ($pos !== false) {
$path = substr($path, 0, $pos);
}
}

// Also trim any .php files from the path.
if (str_contains($path, '.php')) {
$path = dirname($path);
}

$uri = "{$protocol}://{$host}{$port}{$path}";
}

return rtrim($uri, '/');
}
}
7 changes: 2 additions & 5 deletions app/cdash/include/Messaging/Subscription/Subscription.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace CDash\Messaging\Subscription;

use CDash\Config;
use CDash\Messaging\Topic\TopicCollection;
use CDash\Model\Build;
use CDash\Model\BuildGroup;
Expand Down Expand Up @@ -142,14 +141,12 @@ public function getBuildSummary()
{
if (!$this->summary) {
$project = $this->project;
$config = Config::getInstance();
$baseUrl = $config->getBaseUrl();
$summary = [];
$topics = $this->subscriber->getTopics();
$summary['topics'] = [];
$summary['build_group'] = $this->buildGroup->GetName();
$summary['project_name'] = $project->GetName();
$summary['project_url'] = "{$baseUrl}/index.php?project={$project->Name}";
$summary['project_url'] = url("/index.php") . "?project={$project->Name}";
$summary['site_name'] = $this->site->name;
$summary['build_name'] = '';
$summary['build_subproject_names'] = [];
Expand Down Expand Up @@ -203,7 +200,7 @@ public function getBuildSummary()

if (is_null($summary['build_summary_url'])) {
$id = (int) $summary['build_parent_id'] ?: $build->Id;
$summary['build_summary_url'] = "{$baseUrl}/build/{$id}";
$summary['build_summary_url'] = url("/build/{$id}");
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions app/cdash/include/dailyupdates.php
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,6 @@ function get_repository_commits(int $projectid, $dates): array
function sendEmailExpectedBuilds($projectid, $currentstarttime): void
{
$config = Config::getInstance();
$currentURI = $config->getBaseUrl();

$db = Database::getInstance();

Expand Down Expand Up @@ -832,7 +831,7 @@ function sendEmailExpectedBuilds($projectid, $currentstarttime): void
$missingTitle = 'CDash [' . $projectname . '] - Missing Build for ' . $sitename;
$missingSummary = 'The following expected build(s) for the project ' . $projectname . " didn't submit yesterday:\n";
$missingSummary .= '* ' . $sitename . ' - ' . $buildname . ' (' . $builtype . ")\n";
$missingSummary .= "\n" . $currentURI . '/index.php?project=' . urlencode($projectname) . "\n";
$missingSummary .= "\n" . url('/index.php') . '?project=' . urlencode($projectname) . "\n";
$missingSummary .= "\n-CDash on " . $serverName . "\n";

if (cdashmail($recipients, $missingTitle, $missingSummary)) {
Expand All @@ -848,7 +847,7 @@ function sendEmailExpectedBuilds($projectid, $currentstarttime): void
// Send a summary email to the project administrator or users who want to receive notification
// of missing builds
if ($missingbuilds == 1) {
$summary .= "\n" . $currentURI . '/index.php?project=' . urlencode($projectname) . "\n";
$summary .= "\n" . url('/index.php') . '?project=' . urlencode($projectname) . "\n";
$summary .= "\n-CDash on " . $serverName . "\n";

$title = 'CDash [' . $projectname . '] - Missing Builds';
Expand Down
14 changes: 3 additions & 11 deletions app/cdash/include/sendemail.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ function get_email_summary(int $buildid, array $errors, $errorkey, int $maxitems
$build = new Build();
$build->Id = $buildid;

$config = Config::getInstance();
$serverURI = $config->getBaseUrl();
$serverURI = url('/');
$information = '';

// Update information
Expand Down Expand Up @@ -346,9 +345,6 @@ function get_email_summary(int $buildid, array $errors, $errorkey, int $maxitems
/** Generate the title and body for a broken build. */
function generate_broken_build_message(array $emailtext, $Build, $Project): array|false
{
$config = Config::getInstance();
$serverURI = $config->getBaseUrl();

$preamble = 'A submission to CDash for the project ' . $Project->Name . ' has ';
$titleerrors = '(';

Expand Down Expand Up @@ -437,8 +433,7 @@ function generate_broken_build_message(array $emailtext, $Build, $Project): arra

$body = 'Details on the submission can be found at ';

$body .= $serverURI;
$body .= "/build/{$Build->Id}";
$body .= url("/build/{$Build->Id}");
$body .= "\n\n";

$body .= 'Project: ' . $Project->Name . "\n";
Expand Down Expand Up @@ -559,9 +554,6 @@ function send_update_email(UpdateHandler $handler, int $projectid): void
}

if (!empty($recipients)) {
$config = Config::getInstance();
$serverURI = $config->getBaseUrl();

// Generate the email to send
$subject = 'CDash [' . $Project->Name . '] - Update Errors for ' . $sitename;

Expand All @@ -574,7 +566,7 @@ function send_update_email(UpdateHandler $handler, int $projectid): void

$body = "$sitename has encountered errors during the Update step and you have been identified as the maintainer of this site.\n\n";
$body .= "*Update Errors*\n";
$body .= 'Status: ' . $update['status'] . ' (' . $serverURI . '/build/' . $buildid . "/update)\n";
$body .= 'Status: ' . $update['status'] . ' (' . url('/build/' . $buildid . '/update') . ")\n";
if (cdashmail($recipients, $subject, $body)) {
add_log('email sent to: ' . implode(', ', $recipients), 'send_update_email');
} else {
Expand Down
67 changes: 0 additions & 67 deletions app/cdash/tests/case/CDash/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,73 +60,6 @@ public function testGetServer()
$config->set('CDASH_SERVER_NAME', $backup);
}

public function testGetProtocol()
{
$config = Config::getInstance();
$backup = $config->get('CDASH_USE_HTTPS');
$_SERVER['SERVER_PORT'] = '';

$expected = 'http';
$config->set('CDASH_USE_HTTPS', false);
$actual = $config->getProtocol();

$this->assertEquals($expected, $actual);

$expected = 'https';
$config->set('CDASH_USE_HTTPS', true);
$actual = $config->getProtocol();

$this->assertEquals($expected, $actual);

$_SERVER['SERVER_PORT'] = 443;
$config->set('CDASH_USE_HTTPS', false);
$actual = $config->getProtocol();
$this->assertEquals($expected, $actual);

$config->set('CDASH_USE_HTTPS', $backup);
}

public function testGetServerPort()
{
$config = Config::getInstance();
$_SERVER['SERVER_PORT'] = 80;
$this->assertNull($config->getServerPort());

$_SERVER['SERVER_PORT'] = 443;
$this->assertNull($config->getServerPort());

$_SERVER['SERVER_PORT'] = 8080;
$this->assertEquals(8080, $config->getServerPort());
}

public function testGetBaseUrl()
{
global $CDASH_USE_HTTPS;
include 'config/config.php';

$config = Config::getInstance();
$base_url = config('app.url');

config(['app.url' => null]);
$config->set('CDASH_USE_HTTPS', true);
$_SERVER['SERVER_NAME'] = 'www2.tonyrobins.com';
$_SERVER['SERVER_PORT'] = 8080;
$_SERVER['REQUEST_URI'] = '/path/to/success';


$expected = 'https://www2.tonyrobins.com:8080/path/to/success';
$actual = $config->getBaseUrl();

$this->assertEquals($expected, $actual);

config(['app.url' => 'http://open.cdash.org']);
$expected = 'http://open.cdash.org';
$this->assertEquals($expected, $config->getBaseUrl());

config(['app.url' => $base_url]);
$config->set('CDASH_USE_HTTPS', $CDASH_USE_HTTPS);
}

public function testDisablePullRequestComments()
{
include 'config/config.php';
Expand Down
Loading

0 comments on commit 94d63da

Please sign in to comment.