2015-07-27 17:18:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CookieWarningHooks {
|
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.
|
|
|
|
*
|
|
|
|
* @param Title $title
|
|
|
|
* @param null $unused
|
|
|
|
* @param OutputPage $output
|
|
|
|
* @param User $user
|
|
|
|
* @param WebRequest $request
|
|
|
|
* @param MediaWiki $mediawiki
|
|
|
|
*/
|
|
|
|
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() );
|
|
|
|
}
|
|
|
|
|
2016-01-10 17:58:21 +00:00
|
|
|
/**
|
|
|
|
* SkinTemplateOutputPageBeforeExec hook handler.
|
|
|
|
*
|
|
|
|
* Adds the CookieWarning information bar to the output html.
|
|
|
|
*
|
|
|
|
* @param SkinTemplate $sk
|
|
|
|
* @param QuickTemplate $tpl
|
|
|
|
*/
|
|
|
|
public static function onSkinTemplateOutputPageBeforeExec(
|
|
|
|
SkinTemplate &$sk, QuickTemplate &$tpl
|
|
|
|
) {
|
2016-09-14 15:11:38 +00:00
|
|
|
// if the cookiewarning should not be visible to the user, exit.
|
|
|
|
if ( !self::showWarning( $sk->getContext() ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-15 21:07:08 +00:00
|
|
|
$moreLink = self::getMoreLink();
|
2016-01-10 17:58:21 +00:00
|
|
|
// if a "more information" URL was configured, add a link to it in the cookiewarning
|
|
|
|
// information bar
|
2016-09-15 21:07:08 +00:00
|
|
|
if ( $moreLink ) {
|
2015-08-16 10:00:33 +00:00
|
|
|
$moreLink = Html::element(
|
|
|
|
'a',
|
2016-09-15 21:07:08 +00:00
|
|
|
array( '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-09-14 15:11:38 +00:00
|
|
|
$tpl->data['headelement'] .= Html::openElement(
|
|
|
|
'div',
|
|
|
|
array( 'class' => 'mw-cookiewarning-container' )
|
|
|
|
) .
|
|
|
|
Html::openElement(
|
|
|
|
'div',
|
|
|
|
array( 'class' => 'mw-cookiewarning-text' )
|
|
|
|
) .
|
|
|
|
Html::element(
|
|
|
|
'span',
|
|
|
|
array(),
|
|
|
|
$sk->msg( 'cookiewarning-info' )->text()
|
|
|
|
) .
|
|
|
|
$moreLink .
|
|
|
|
Html::openElement( 'form', array( 'method' => 'POST' ) ) .
|
|
|
|
Html::submitButton(
|
2016-09-14 15:17:35 +00:00
|
|
|
$sk->msg( 'cookiewarning-ok-label' )->text(),
|
2016-09-14 15:11:38 +00:00
|
|
|
array(
|
|
|
|
'name' => 'disablecookiewarning',
|
|
|
|
'class' => 'mw-cookiewarning-dismiss'
|
|
|
|
)
|
|
|
|
) .
|
|
|
|
Html::closeElement( 'form' ) .
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
private static function getMoreLink() {
|
|
|
|
// Config instance of CookieWarning
|
|
|
|
$conf = ConfigFactory::getDefaultInstance()->makeConfig( 'cookiewarning' );
|
|
|
|
if ( $conf->get( 'CookieWarningMoreUrl' ) ) {
|
|
|
|
return $conf->get( 'CookieWarningMoreUrl' );
|
|
|
|
}
|
|
|
|
$cookieWarningMessage = wfMessage( 'cookiewarning-more-link' );
|
|
|
|
if ( $cookieWarningMessage->exists() && !$cookieWarningMessage->isDisabled() ) {
|
|
|
|
return $cookieWarningMessage->escaped();
|
|
|
|
}
|
|
|
|
$cookiePolicyMessage = wfMessage( 'cookie-policy-link' );
|
|
|
|
if ( $cookiePolicyMessage->exists() && !$cookiePolicyMessage->isDisabled() ) {
|
|
|
|
return $cookiePolicyMessage->escaped();
|
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
2015-07-27 17:18:44 +00:00
|
|
|
public static function onBeforePageDisplay( OutputPage $out ) {
|
2016-01-10 17:58:21 +00:00
|
|
|
if ( self::showWarning( $out->getContext() ) ) {
|
2015-07-27 17:18:44 +00:00
|
|
|
$out->addModuleStyles( array( 'ext.CookieWarning.styles' ) );
|
|
|
|
$out->addModules( array( 'ext.CookieWarning' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-10 17:58:21 +00:00
|
|
|
/**
|
|
|
|
* Checks, if the CookieWarning information bar should be visible to this user on
|
|
|
|
* this page.
|
|
|
|
*
|
|
|
|
* @param IContextSource $context
|
|
|
|
* @return boolean Returns true, if the cookie warning should be visible, false otherwise.
|
|
|
|
*/
|
2015-07-27 17:18:44 +00:00
|
|
|
private static function showWarning( IContextSource $context ) {
|
2015-08-16 09:49:05 +00:00
|
|
|
$user = $context->getUser();
|
2015-07-27 17:18:44 +00:00
|
|
|
$conf = ConfigFactory::getDefaultInstance()->makeConfig( 'cookiewarning' );
|
|
|
|
if (
|
2016-01-10 17:58:21 +00:00
|
|
|
// if enabled in LocalSettings.php
|
2015-07-27 17:18:44 +00:00
|
|
|
$conf->get( 'CookieWarningEnabled' ) &&
|
2016-01-10 17:58:21 +00:00
|
|
|
// if not already dismissed by this user (and saved in the user prefs)
|
2015-08-16 09:49:05 +00:00
|
|
|
!$user->getBoolOption( 'cookiewarning_dismissed', false ) &&
|
2016-01-10 17:58:21 +00:00
|
|
|
// if not already dismissed by this user (and saved in the browser cookies)
|
2015-07-27 17:18:44 +00:00
|
|
|
!$context->getRequest()->getCookie( 'cookiewarning_dismissed' )
|
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-16 09:49:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GetPreferences hook handler
|
|
|
|
*
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetPreferences
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
* @param array $defaultPreferences
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function onGetPreferences( User $user, &$defaultPreferences ) {
|
|
|
|
$defaultPreferences['cookiewarning_dismissed'] = array(
|
|
|
|
'type' => 'api',
|
|
|
|
'default' => '0',
|
|
|
|
);
|
|
|
|
return true;
|
|
|
|
}
|
2015-07-27 17:18:44 +00:00
|
|
|
}
|