Skip to content

Commit 8562cda

Browse files
committed
Fix expandLink to be simpler.
1 parent 98899e1 commit 8562cda

4 files changed

Lines changed: 451 additions & 151 deletions

File tree

src/UI/Lessons.php

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function header($buffer=false) {
120120
}
121121
if ( isset($this->lessons->headers) && is_array($this->lessons->headers) ) {
122122
foreach($this->lessons->headers as $header) {
123-
$header = self::expandLink($header);
123+
$header = $this->expandLink($header);
124124
echo($header);
125125
echo("\n");
126126
}
@@ -411,26 +411,6 @@ public static function absolute_url_ref(&$url) {
411411
*/
412412
public static function expandLink($url) {
413413
global $CFG;
414-
// Skip expansion if URL already starts with http:// or https://
415-
if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) {
416-
return $url;
417-
}
418-
419-
// Clean up duplicate placeholders first (e.g., {apphome}/{apphome} -> {apphome})
420-
$url = preg_replace('#\{apphome\}/+\{apphome\}#', '{apphome}', $url);
421-
$url = preg_replace('#\{wwwroot\}/+\{wwwroot\}#', '{wwwroot}', $url);
422-
423-
// Check if URL already contains expanded apphome or wwwroot (prevent double expansion)
424-
$has_expanded_apphome = isset($CFG->apphome) && strpos($url, $CFG->apphome) !== false;
425-
$has_expanded_wwwroot = isset($CFG->wwwroot) && strpos($url, $CFG->wwwroot) !== false;
426-
427-
if ($has_expanded_apphome || $has_expanded_wwwroot) {
428-
// URL already contains expanded values, just remove any remaining placeholders
429-
$url = str_replace(array('{apphome}', '{wwwroot}'), '', $url);
430-
// Clean up any double slashes that might result
431-
$url = preg_replace('#([^:])//+#', '$1/', $url);
432-
return $url;
433-
}
434414

435415
$search = array(
436416
"{apphome}",
@@ -442,20 +422,6 @@ public static function expandLink($url) {
442422
);
443423
$url = str_replace($search, $replace, $url);
444424

445-
// Hopefully, the URL starts with http:// or https://
446-
if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) {
447-
return $url;
448-
}
449-
450-
// If we still don't have http:// or https://, and apphome is set, assume apphome, adding a / is the url sode not start with a slash
451-
if ( $url !== '' && isset($CFG->apphome) ) {
452-
if ( strpos($url, '/') !== 0 ) {
453-
$url = $CFG->apphome . '/' . $url;
454-
} else {
455-
$url = $CFG->apphome . $url;
456-
}
457-
}
458-
459425
return $url;
460426
}
461427

src/UI/Lessons2.php

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -428,26 +428,6 @@ public static function absolute_url_ref(&$url) {
428428
*/
429429
public static function expandLink($url) {
430430
global $CFG;
431-
// Skip expansion if URL already starts with http:// or https://
432-
if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) {
433-
return $url;
434-
}
435-
436-
// Clean up duplicate placeholders first (e.g., {apphome}/{apphome} -> {apphome})
437-
$url = preg_replace('#\{apphome\}/+\{apphome\}#', '{apphome}', $url);
438-
$url = preg_replace('#\{wwwroot\}/+\{wwwroot\}#', '{wwwroot}', $url);
439-
440-
// Check if URL already contains expanded apphome or wwwroot (prevent double expansion)
441-
$has_expanded_apphome = isset($CFG->apphome) && strpos($url, $CFG->apphome) !== false;
442-
$has_expanded_wwwroot = isset($CFG->wwwroot) && strpos($url, $CFG->wwwroot) !== false;
443-
444-
if ($has_expanded_apphome || $has_expanded_wwwroot) {
445-
// URL already contains expanded values, just remove any remaining placeholders
446-
$url = str_replace(array('{apphome}', '{wwwroot}'), '', $url);
447-
// Clean up any double slashes that might result
448-
$url = preg_replace('#([^:])//+#', '$1/', $url);
449-
return $url;
450-
}
451431

452432
$search = array(
453433
"{apphome}",
@@ -459,19 +439,6 @@ public static function expandLink($url) {
459439
);
460440
$url = str_replace($search, $replace, $url);
461441

462-
// Hopefully, the URL starts with http:// or https://
463-
if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) {
464-
return $url;
465-
}
466-
467-
// If we still don't have http:// or https://, and apphome is set, assume apphome, adding a / is the url sode not start with a slash
468-
if ( $url !== '' && isset($CFG->apphome) ) {
469-
if ( strpos($url, '/') !== 0 ) {
470-
$url = $CFG->apphome . '/' . $url;
471-
} else {
472-
$url = $CFG->apphome . $url;
473-
}
474-
}
475442
return $url;
476443
}
477444

0 commit comments

Comments
 (0)