2015-07-27 17:18:44 +00:00
|
|
|
<?php
|
|
|
|
|
2018-07-19 10:48:17 +00:00
|
|
|
namespace CookieWarning;
|
2016-09-14 15:11:38 +00:00
|
|
|
|
2018-07-19 10:48:17 +00:00
|
|
|
use Config;
|
|
|
|
use ConfigException;
|
|
|
|
use ExtensionRegistry;
|
|
|
|
use Html;
|
|
|
|
use MediaWiki;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use MobileContext;
|
|
|
|
use MWException;
|
|
|
|
use OutputPage;
|
2020-10-03 18:13:17 +00:00
|
|
|
use Skin;
|
2018-07-19 10:48:17 +00:00
|
|
|
use Title;
|
|
|
|
use User;
|
|
|
|
use WebRequest;
|
|
|
|
|
|
|
|
class Hooks {
|
2016-09-14 15:11:38 +00:00
|
|
|
/**
|
|
|
|
* BeforeInitialize hook handler.
|
|
|
|
*
|
|
|
|
* If the disablecookiewarning POST data is send, disables the cookiewarning bar with a
|
|
|
|
* cookie or a user preference, if the user is logged in.
|
|
|
|
*
|
2018-01-11 20:09:29 +00:00
|
|
|
* @param Title &$title
|
|
|
|
* @param null &$unused
|
|
|
|
* @param OutputPage &$output
|
|
|
|
* @param User &$user
|
2016-09-14 15:11:38 +00:00
|
|
|
* @param WebRequest $request
|
|
|
|
* @param MediaWiki $mediawiki
|
2018-07-18 09:50:46 +00:00
|
|
|
* @throws MWException
|
2016-09-14 15:11:38 +00:00
|
|
|
*/
|
|
|
|
public static function onBeforeInitialize( Title &$title, &$unused, OutputPage &$output,
|
|
|
|
User &$user, WebRequest $request, MediaWiki $mediawiki
|
|
|
|
) {
|
|
|
|
if ( !$request->wasPosted() || !$request->getVal( 'disablecookiewarning' ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $user->isLoggedIn() ) {
|
|
|
|
$user->setOption( 'cookiewarning_dismissed', 1 );
|
|
|
|
$user->saveSettings();
|
|
|
|
} else {
|
|
|
|
$request->response()->setCookie( 'cookiewarning_dismissed', true );
|
|
|
|
}
|
|
|
|
$output->redirect( $request->getRequestURL() );
|
|
|
|
}
|
2018-07-18 09:50:46 +00:00
|
|
|
|
2016-01-10 17:58:21 +00:00
|
|
|
/**
|
2020-10-03 18:13:17 +00:00
|
|
|
* SiteNoticeAfter hook handler.
|
2016-01-10 17:58:21 +00:00
|
|
|
*
|
2020-10-03 18:13:17 +00:00
|
|
|
* Adds the CookieWarning information bar to the output html for mobile.
|
2016-01-10 17:58:21 +00:00
|
|
|
*
|
2020-10-03 18:13:17 +00:00
|
|
|
* @param string &$siteNotice
|
|
|
|
* @param Skin $skin
|
2018-07-18 09:50:46 +00:00
|
|
|
* @throws ConfigException
|
|
|
|
* @throws MWException
|
2020-10-03 18:13:17 +00:00
|
|
|
* @return bool|void True or no return value to continue or false to abort
|
2016-01-10 17:58:21 +00:00
|
|
|
*/
|
2020-10-03 18:13:17 +00:00
|
|
|
public static function onSiteNoticeAfter(
|
|
|
|
string &$siteNotice,
|
|
|
|
Skin $skin
|
2016-01-10 17:58:21 +00:00
|
|
|
) {
|
2018-07-19 10:48:17 +00:00
|
|
|
/** @var Decisions $cookieWarningDecisions */
|
2018-07-18 09:50:46 +00:00
|
|
|
$cookieWarningDecisions = MediaWikiServices::getInstance()
|
|
|
|
->getService( 'CookieWarning.Decisions' );
|
|
|
|
|
2020-10-03 18:13:17 +00:00
|
|
|
if ( !$cookieWarningDecisions->shouldShowCookieWarning( $skin->getContext() ) ) {
|
2016-09-14 15:11:38 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-10-03 18:13:17 +00:00
|
|
|
|
|
|
|
$isMobile = ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) &&
|
|
|
|
MobileContext::singleton()->shouldDisplayMobileView();
|
|
|
|
|
|
|
|
$siteNotice .= self::generateElements( $skin, $isMobile );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the elements for the banner.
|
|
|
|
*
|
|
|
|
* @param Skin $skin
|
|
|
|
* @param bool $isMobile This will return true if using mobile site.
|
|
|
|
* @return string|null The html for cookie notice.
|
|
|
|
*/
|
|
|
|
private static function generateElements( Skin $skin, bool $isMobile ) {
|
2016-09-15 21:07:08 +00:00
|
|
|
$moreLink = self::getMoreLink();
|
2018-07-18 09:50:46 +00:00
|
|
|
|
2016-09-15 21:07:08 +00:00
|
|
|
if ( $moreLink ) {
|
2018-08-10 18:20:42 +00:00
|
|
|
$moreLink = "\u{00A0}" . Html::element(
|
2015-08-16 10:00:33 +00:00
|
|
|
'a',
|
2016-09-14 15:23:11 +00:00
|
|
|
[ 'href' => $moreLink ],
|
2020-10-03 18:13:17 +00:00
|
|
|
$skin->msg( 'cookiewarning-moreinfo-label' )->text()
|
2015-08-16 10:00:33 +00:00
|
|
|
);
|
|
|
|
}
|
2016-09-14 15:11:38 +00:00
|
|
|
|
2016-11-04 16:47:15 +00:00
|
|
|
$form = Html::openElement( 'form', [ 'method' => 'POST' ] ) .
|
2017-06-20 02:40:07 +00:00
|
|
|
Html::submitButton(
|
2020-10-03 18:13:17 +00:00
|
|
|
$skin->msg( 'cookiewarning-ok-label' )->text(),
|
2017-06-20 02:40:07 +00:00
|
|
|
[
|
|
|
|
'name' => 'disablecookiewarning',
|
|
|
|
'class' => 'mw-cookiewarning-dismiss'
|
|
|
|
]
|
|
|
|
) .
|
|
|
|
Html::closeElement( 'form' );
|
2016-11-04 16:47:15 +00:00
|
|
|
|
2018-08-10 18:20:42 +00:00
|
|
|
$cookieImage = Html::element(
|
|
|
|
'div',
|
|
|
|
[ 'class' => 'mw-cookiewarning-cimage' ],
|
|
|
|
"\u{1F36A}"
|
|
|
|
);
|
2016-11-04 16:47:15 +00:00
|
|
|
|
2020-10-03 18:13:17 +00:00
|
|
|
return Html::openElement(
|
2016-09-14 15:11:38 +00:00
|
|
|
'div',
|
2018-05-19 09:25:31 +00:00
|
|
|
// banner-container marks this as a banner for Minerva
|
|
|
|
// Note to avoid this class, in future we may want to make use of SiteNotice
|
|
|
|
// or banner display
|
|
|
|
[ 'class' => 'mw-cookiewarning-container banner-container' ]
|
2016-09-14 15:11:38 +00:00
|
|
|
) .
|
|
|
|
Html::openElement(
|
|
|
|
'div',
|
2016-09-14 15:23:11 +00:00
|
|
|
[ 'class' => 'mw-cookiewarning-text' ]
|
2016-09-14 15:11:38 +00:00
|
|
|
) .
|
2017-06-20 02:40:07 +00:00
|
|
|
( $isMobile ? $cookieImage : '' ) .
|
2016-09-14 15:11:38 +00:00
|
|
|
Html::element(
|
|
|
|
'span',
|
2016-09-14 15:23:11 +00:00
|
|
|
[],
|
2020-10-03 18:13:17 +00:00
|
|
|
$skin->msg( 'cookiewarning-info' )->text()
|
2016-09-14 15:11:38 +00:00
|
|
|
) .
|
|
|
|
$moreLink .
|
|
|
|
Html::closeElement( 'div' ) .
|
2020-10-03 18:13:17 +00:00
|
|
|
$form .
|
2016-09-14 15:11:38 +00:00
|
|
|
Html::closeElement( 'div' );
|
2015-07-27 17:18:44 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 21:07:08 +00:00
|
|
|
/**
|
|
|
|
* Returns the target for the "More information" link of the cookie warning bar, if one is set.
|
|
|
|
* The link can be set by either (checked in this order):
|
|
|
|
* - the configuration variable $wgCookieWarningMoreUrl
|
|
|
|
* - the interface message MediaWiki:Cookiewarning-more-link
|
|
|
|
* - the interface message MediaWiki:Cookie-policy-link (bc T145781)
|
|
|
|
*
|
|
|
|
* @return string|null The url or null if none set
|
2018-07-18 09:50:46 +00:00
|
|
|
* @throws ConfigException
|
2016-09-15 21:07:08 +00:00
|
|
|
*/
|
|
|
|
private static function getMoreLink() {
|
2018-07-18 09:50:46 +00:00
|
|
|
$conf = self::getConfig();
|
2016-09-15 21:07:08 +00:00
|
|
|
if ( $conf->get( 'CookieWarningMoreUrl' ) ) {
|
|
|
|
return $conf->get( 'CookieWarningMoreUrl' );
|
|
|
|
}
|
2018-07-18 09:50:46 +00:00
|
|
|
|
2016-09-15 21:07:08 +00:00
|
|
|
$cookieWarningMessage = wfMessage( 'cookiewarning-more-link' );
|
|
|
|
if ( $cookieWarningMessage->exists() && !$cookieWarningMessage->isDisabled() ) {
|
2018-08-25 07:59:19 +00:00
|
|
|
return $cookieWarningMessage->text();
|
2016-09-15 21:07:08 +00:00
|
|
|
}
|
2018-07-18 09:50:46 +00:00
|
|
|
|
2016-09-15 21:07:08 +00:00
|
|
|
$cookiePolicyMessage = wfMessage( 'cookie-policy-link' );
|
|
|
|
if ( $cookiePolicyMessage->exists() && !$cookiePolicyMessage->isDisabled() ) {
|
2018-08-25 07:59:19 +00:00
|
|
|
return $cookiePolicyMessage->text();
|
2016-09-15 21:07:08 +00:00
|
|
|
}
|
2018-07-18 09:50:46 +00:00
|
|
|
|
2016-09-15 21:07:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2016-01-10 17:58:21 +00:00
|
|
|
/**
|
|
|
|
* BeforePageDisplay hook handler.
|
|
|
|
*
|
|
|
|
* Adds the required style and JS module, if cookiewarning is enabled.
|
|
|
|
*
|
|
|
|
* @param OutputPage $out
|
2018-07-18 09:50:46 +00:00
|
|
|
* @throws ConfigException
|
|
|
|
* @throws MWException
|
2016-01-10 17:58:21 +00:00
|
|
|
*/
|
2015-07-27 17:18:44 +00:00
|
|
|
public static function onBeforePageDisplay( OutputPage $out ) {
|
2018-07-19 10:48:17 +00:00
|
|
|
/** @var Decisions $cookieWarningDecisions */
|
2018-07-18 09:50:46 +00:00
|
|
|
$cookieWarningDecisions = MediaWikiServices::getInstance()
|
|
|
|
->getService( 'CookieWarning.Decisions' );
|
|
|
|
|
|
|
|
if ( !$cookieWarningDecisions->shouldShowCookieWarning( $out->getContext() ) ) {
|
|
|
|
return;
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
}
|
2018-07-18 09:50:46 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) &&
|
|
|
|
MobileContext::singleton()->shouldDisplayMobileView()
|
|
|
|
) {
|
|
|
|
$moduleStyles = [ 'ext.CookieWarning.mobile.styles' ];
|
|
|
|
} else {
|
|
|
|
$moduleStyles = [ 'ext.CookieWarning.styles' ];
|
|
|
|
}
|
|
|
|
$modules = [ 'ext.CookieWarning' ];
|
|
|
|
|
|
|
|
if ( $cookieWarningDecisions->shouldAddResourceLoaderComponents() ) {
|
|
|
|
$modules[] = 'ext.CookieWarning.geolocation';
|
|
|
|
$moduleStyles[] = 'ext.CookieWarning.geolocation.styles';
|
|
|
|
}
|
|
|
|
$out->addModules( $modules );
|
|
|
|
$out->addModuleStyles( $moduleStyles );
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ResourceLoaderGetConfigVars hook handler.
|
|
|
|
*
|
2018-01-11 20:09:29 +00:00
|
|
|
* @param array &$vars
|
2018-07-18 09:50:46 +00:00
|
|
|
* @throws ConfigException
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
*/
|
|
|
|
public static function onResourceLoaderGetConfigVars( array &$vars ) {
|
2018-07-19 10:48:17 +00:00
|
|
|
/** @var Decisions $cookieWarningDecisions */
|
2018-07-18 09:50:46 +00:00
|
|
|
$cookieWarningDecisions = MediaWikiServices::getInstance()
|
|
|
|
->getService( 'CookieWarning.Decisions' );
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
$conf = self::getConfig();
|
2018-07-18 09:50:46 +00:00
|
|
|
|
|
|
|
if ( $cookieWarningDecisions->shouldAddResourceLoaderComponents() ) {
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
$vars += [
|
|
|
|
'wgCookieWarningGeoIPServiceURL' => $conf->get( 'CookieWarningGeoIPServiceURL' ),
|
|
|
|
'wgCookieWarningForCountryCodes' => $conf->get( 'CookieWarningForCountryCodes' ),
|
|
|
|
];
|
2015-07-27 17:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
/**
|
2018-08-14 09:40:21 +00:00
|
|
|
* Returns the Config object for the CookieWarning extension.
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
*
|
|
|
|
* @return Config
|
|
|
|
*/
|
|
|
|
private static function getConfig() {
|
2018-07-18 09:50:46 +00:00
|
|
|
return MediaWikiServices::getInstance()->getService( 'CookieWarning.Config' );
|
Add option to limit the cookie warning to specific regions
With this change, CookieWarning gets two new ways to limit the warning
bar to users of specific (configurable) regions. Based on the geo location
service (which can be configured, too, defaults to freegeoip.net), the
extension will try to get the region of the user (based on the IP address)
and checks it against the configured regions.
With the configuration variable $wgCookieWarningGeoIPLookup, the wiki sysadmin
can configure, if the lookup should be done with PHP (the timeout for the
request is hardcoded to 2 sedonds max), which will save the paylod sent to
the user, or with JavaScript, which will be asynchronous.
Deactivating this feature is easily done by simply setting the configured regions
to false, instead of an object.
Bug: T145780
Change-Id: I4d9371b8608ef270c11c42bdb3a773675977ab3a
2016-09-16 19:18:49 +00:00
|
|
|
}
|
|
|
|
|
2015-08-16 09:49:05 +00:00
|
|
|
/**
|
|
|
|
* GetPreferences hook handler
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
|
|
|
|
*
|
|
|
|
* @param User $user
|
2018-01-11 20:09:29 +00:00
|
|
|
* @param array &$defaultPreferences
|
2015-08-16 09:49:05 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onGetPreferences( User $user, &$defaultPreferences ) {
|
2016-09-14 15:23:11 +00:00
|
|
|
$defaultPreferences['cookiewarning_dismissed'] = [
|
2015-08-16 09:49:05 +00:00
|
|
|
'type' => 'api',
|
|
|
|
'default' => '0',
|
2016-09-14 15:23:11 +00:00
|
|
|
];
|
2015-08-16 09:49:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
2015-07-27 17:18:44 +00:00
|
|
|
}
|