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;
|
|
|
|
use QuickTemplate;
|
|
|
|
use SkinTemplate;
|
|
|
|
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
|
|
|
/**
|
|
|
|
* SkinTemplateOutputPageBeforeExec hook handler.
|
|
|
|
*
|
|
|
|
* Adds the CookieWarning information bar to the output html.
|
|
|
|
*
|
2018-01-11 20:09:29 +00:00
|
|
|
* @param SkinTemplate &$sk
|
|
|
|
* @param QuickTemplate &$tpl
|
2018-07-18 09:50:46 +00:00
|
|
|
* @throws ConfigException
|
|
|
|
* @throws MWException
|
2016-01-10 17:58:21 +00:00
|
|
|
*/
|
|
|
|
public static function onSkinTemplateOutputPageBeforeExec(
|
|
|
|
SkinTemplate &$sk, QuickTemplate &$tpl
|
|
|
|
) {
|
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( $sk->getContext() ) ) {
|
2016-09-14 15:11:38 +00:00
|
|
|
return;
|
|
|
|
}
|
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 ],
|
2016-01-10 17:58:21 +00:00
|
|
|
$sk->msg( 'cookiewarning-moreinfo-label' )->text()
|
2015-08-16 10:00:33 +00:00
|
|
|
);
|
|
|
|
}
|
2016-09-14 15:11:38 +00:00
|
|
|
|
2016-09-17 22:02:05 +00:00
|
|
|
if ( !isset( $tpl->data['headelement'] ) ) {
|
|
|
|
$tpl->data['headelement'] = '';
|
|
|
|
}
|
2016-11-04 16:47:15 +00:00
|
|
|
$form = Html::openElement( 'form', [ 'method' => 'POST' ] ) .
|
2017-06-20 02:40:07 +00:00
|
|
|
Html::submitButton(
|
|
|
|
$sk->msg( 'cookiewarning-ok-label' )->text(),
|
|
|
|
[
|
|
|
|
'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
|
|
|
|
|
|
|
$isMobile = ExtensionRegistry::getInstance()->isLoaded( 'MobileFrontend' ) &&
|
|
|
|
MobileContext::singleton()->shouldDisplayMobileView();
|
2016-09-14 15:11:38 +00:00
|
|
|
$tpl->data['headelement'] .= Html::openElement(
|
|
|
|
'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
|
|
|
) .
|
2017-06-20 02:40:07 +00:00
|
|
|
( $isMobile ? $form : '' ) .
|
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
|
|
|
[],
|
2016-09-14 15:11:38 +00:00
|
|
|
$sk->msg( 'cookiewarning-info' )->text()
|
|
|
|
) .
|
|
|
|
$moreLink .
|
2017-06-20 02:40:07 +00:00
|
|
|
( !$isMobile ? $form : '' ) .
|
2016-09-14 15:11:38 +00:00
|
|
|
Html::closeElement( 'div' ) .
|
|
|
|
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
|
|
|
}
|