Skip to content

Commit c792a3c

Browse files
authored
Merge pull request #4725 from dpalou/MOBILE-4925
MOBILE-4925 core: Use referer fix script if available
2 parents 8ee7428 + ec4c413 commit c792a3c

File tree

6 files changed

+64
-7
lines changed

6 files changed

+64
-7
lines changed

src/core/classes/sites/site.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,8 @@ export class CoreSite extends CoreAuthenticatedSite {
681681
return url;
682682
}
683683

684-
if (CoreUrl.isTokenPluginFileUrl(url)) {
685-
// Tokenpluginfile URLs authenticate the user using the access key, no need to use auto-login.
684+
if (CoreUrl.isTokenPluginFileUrl(url) || CoreUrl.isRefererScriptUrl(url, this.siteUrl)) {
685+
// URL doesn't need login or it already has a different login system.
686686
return url;
687687
}
688688

@@ -901,14 +901,18 @@ export class CoreSite extends CoreAuthenticatedSite {
901901
}
902902

903903
/**
904-
* Given a URL, if it requires a referer, fix it to use a redirect script that will add the referer.
904+
* Given a URL, fix it to use a redirect script that will add the referer if possible.
905905
*
906906
* @param url URL to fix.
907907
* @returns Fixed URL or original URL if no need to fix it.
908908
*/
909909
fixRefererForUrl(url: string): string {
910-
// @todo: This function will be implemented in MOBILE-4924 once this functionality is supported in Moodle LMS.
911-
return url;
910+
if (!this.isVersionGreaterEqualThan('5.2')) {
911+
// The referer script was added in Moodle 5.2. If the site is older, referer cannot be fixed.
912+
return url;
913+
}
914+
915+
return CoreUrl.toRefererScriptUrl(url, this.siteUrl);
912916
}
913917

914918
}

src/core/components/iframe/iframe.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export class CoreIframeComponent implements OnChanges, OnDestroy {
7676
@Input({ transform: toBoolean }) showFullscreenOnToolbar = false;
7777
@Input({ transform: toBoolean }) autoFullscreenOnRotate = false;
7878
@Input({ transform: toBoolean }) allowAutoLogin = true;
79+
@Input({ transform: toBoolean }) addSiteReferer = false;
7980
@Output() loaded = new EventEmitter<HTMLIFrameElement>();
8081

8182
loading?: boolean;
@@ -271,7 +272,7 @@ export class CoreIframeComponent implements OnChanges, OnDestroy {
271272
url = CoreUrl.addParamsToUrl(autoLoginUrl, { lang }, {
272273
checkAutoLoginUrl: autoLoginUrl !== url,
273274
});
274-
} else {
275+
} else if (this.addSiteReferer || CoreUrl.urlNeedsReferer(url)) {
275276
url = currentSite?.fixRefererForUrl(url) || url;
276277
}
277278

src/core/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ export const DATA_APP_ALT_URL_TYPE = 'data-app-alt-url-type';
194194
export const DATASET_APP_ALT_URL_TYPE = 'appAltUrlType';
195195
export const DATA_APP_ALT_URL_LABEL = 'data-app-alt-url-label';
196196
export const DATASET_APP_ALT_URL_LABEL = 'appAltUrlLabel';
197+
export const DATA_APP_SITE_REFERER = 'data-app-site-referer';
198+
export const DATASET_APP_SITE_REFERER = 'appSiteReferer';
197199

198200
/** @deprecated since 5.2. Use DATA_APP_OPEN_IN instead. */
199201
export const DATA_APP_OPEN_IN_LEGACY = 'data-open-in';

src/core/directives/format-text.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ import {
6666
DATASET_APP_ALT_URL_TYPE,
6767
DATASET_APP_OPEN_IN,
6868
DATASET_APP_OPEN_IN_LEGACY,
69+
DATASET_APP_SITE_REFERER,
6970
DATASET_APP_URL,
7071
DATASET_APP_URL_CONFIRM,
7172
DATASET_APP_URL_RESUME_ACTION,
@@ -1049,7 +1050,7 @@ export class CoreFormatTextDirective implements OnDestroy, AsyncDirective {
10491050
CoreIframe.treatFrame(iframe, false);
10501051

10511052
return;
1052-
} else if (site) {
1053+
} else if (site && (iframe.dataset[DATASET_APP_SITE_REFERER] === 'true' || CoreUrl.urlNeedsReferer(src))) {
10531054
src = site.fixRefererForUrl(src);
10541055
}
10551056

src/core/static/tests/url.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,21 @@ describe('CoreUrl', () => {
355355
expect(CoreUrl.getPluginFileArgs('http://mysite.com/pluginfile.php/6/')).toEqual(undefined);
356356
});
357357

358+
it('converts to referer script URL', () => {
359+
const siteUrl = 'https://mysite.com';
360+
expect(CoreUrl.toRefererScriptUrl('https://myvideoplayer.com/videoid', siteUrl)).toEqual(
361+
`${siteUrl}/admin/tool/mobile/referer.php?url=https%3A%2F%2Fmyvideoplayer.com%2Fvideoid`,
362+
);
363+
expect(CoreUrl.toRefererScriptUrl('https://foo.bar', siteUrl)).toEqual(
364+
`${siteUrl}/admin/tool/mobile/referer.php?url=https%3A%2F%2Ffoo.bar`,
365+
);
366+
});
367+
368+
it('checks if a URL is a referer script URL', () => {
369+
const siteUrl = 'https://mysite.com';
370+
expect(CoreUrl.isRefererScriptUrl(`${siteUrl}/admin/tool/mobile/referer.php`, siteUrl)).toBe(true);
371+
expect(CoreUrl.isRefererScriptUrl(`${siteUrl}/admin/tool/mobile/refererr.php`, siteUrl)).toBe(false);
372+
expect(CoreUrl.isRefererScriptUrl(`${siteUrl}/myplugin/admin/tool/mobile/referer.php`, siteUrl)).toBe(false);
373+
});
374+
358375
});

src/core/static/url.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,38 @@ export class CoreUrl {
10851085
return url;
10861086
}
10871087

1088+
/**
1089+
* Check whether a URL needs (or could need) a referer to work.
1090+
*
1091+
* @param url URL to check.
1092+
* @returns Whether the URL needs a referer.
1093+
*/
1094+
static urlNeedsReferer(url: string): boolean {
1095+
return CoreUrl.isYoutubeURL(url) || CoreUrl.isVimeoVideoUrl(url);
1096+
}
1097+
1098+
/**
1099+
* Get the URL to use to view a URL adding a site referer.
1100+
*
1101+
* @param url URL to treat.
1102+
* @param siteUrl The URL of the site to use for the referer.
1103+
* @returns URL to use to add a referer.
1104+
*/
1105+
static toRefererScriptUrl(url: string, siteUrl: string): string {
1106+
return CorePath.concatenatePaths(siteUrl, 'admin/tool/mobile/referer.php') + '?url=' + encodeURIComponent(url);
1107+
}
1108+
1109+
/**
1110+
* Check if a URL is a referer script URL.
1111+
*
1112+
* @param url URL to check.
1113+
* @param siteUrl The URL of the site to use for the referer.
1114+
* @returns Whether the URL is a referer script URL.
1115+
*/
1116+
static isRefererScriptUrl(url: string, siteUrl: string): boolean {
1117+
return url.startsWith(CorePath.concatenatePaths(siteUrl, 'admin/tool/mobile/referer.php'));
1118+
}
1119+
10881120
}
10891121

10901122
export type CoreUrlParams = { [key: string]: string };

0 commit comments

Comments
 (0)