Ticket #42: BlockingCookie.php

File BlockingCookie.php, 4.0 KB (added by black silence, 3 years ago)

much cleaner version, less hardcoded, delete cookie if no site is excluded from tracking, user is sent back to dashboard (renamed widget, delete and add new!)

Line 
1<?php
2/**
3 * Piwik - Open source web analytics
4 *
5 * @link http://piwik.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
7 * @version $Id: BlockingCookie.php 9 2009-05-17 19:19:21Z pat $
8 *
9 * @package Piwik_BlockingCookie
10 */
11defined('PIWIK_INCLUDE_PATH') or die('Restricted access');
12
13class Piwik_BlockingCookie extends Piwik_Plugin
14{
15   
16    private $cookieName = 'piwik_exclude';
17   
18    public function getInformation ()
19    {
20        return array(
21            'name' => 'BlockingCookie',
22            'description' => 'This plugin allows to exclude users from tracking by setting a cookie.',
23            'author' => 'black silence',
24            'homepage' => 'http://www.psycho3d.de/',
25            'version' => '0.2',
26            'TrackerPlugin' => true,
27        );
28    }
29   
30    /**
31     * register hooks
32     */
33    function getListHooksRegistered () {
34        return array(
35            'Tracker.Visit.isExcluded' => 'isVisitorExcluded',
36            'WidgetsList.add' => 'addWidget',
37        );
38    }
39   
40   
41    public function isVisitorExcluded ($notification) {
42        # idsite, NOT idSite
43        $idSite = Piwik_Common::getRequestVar('idsite', 1, 'integer');
44        #printDebug($idSite);
45       
46        $excludeCookie = new Piwik_Cookie($this->cookieName);
47        if ($excludeCookie->isCookieFound()) {
48            # get reference to the value we need to change
49            $excluded =& $notification->getNotificationObject();   
50            $excluded = $excludeCookie->get($idSite);
51        }
52    }
53   
54   
55    public function addWidget ()
56    {
57        Piwik_AddWidget('Special', 'Blocking Cookie', 'BlockingCookie', 'showWidget');
58    }
59   
60}
61
62class Piwik_BlockingCookie_Controller extends Piwik_Controller
63{
64    private $cookieName = 'piwik_exclude';
65   
66    public function showWidget () {
67        $idSite = Piwik_Common::getRequestVar('idSite', 1, 'integer');
68       
69        # check if the current user is excluded
70        $excludeCookie = new Piwik_Cookie($this->cookieName);
71        $excluded = (bool)$excludeCookie->get($idSite);
72       
73        # prepare params for the link
74        $params = array(
75            'module=BlockingCookie',
76            'action=setTracking',
77            'idSite=' . $idSite,
78            'exclude=' . (int)(!$excluded)
79        );
80        # needed for redirecting after changing the exclude state
81        $period = Piwik_Common::getRequestVar('period', 0);
82        if ($period) $params[] = 'period=' . $period;
83        $date = Piwik_Common::getRequestVar('date', 0);
84        if ($date) $params[] = 'date=' . $date;
85       
86        # get the link and text
87        $link = 'index.php?' . implode('&', $params);
88       
89        if ($excluded) {
90            $result = 'You will not be tracked.<br/><a href="' . $link . '">Start tracking me!</a>';
91        } else {
92            $result = 'You will be tracked.<br/><a href="' . $link . '">Stop tracking me!</a>';
93        }
94       
95        echo '<div id="blockingcookie">' . $result . '</div>';
96    }
97   
98   
99    /**
100     * set the tracking cookie as the user requests, then redirect back to dashboard
101     */
102    public function setTracking () {
103        $idSite = Piwik_Common::getRequestVar('idSite', 1, 'integer');
104        $exclude = Piwik_Common::getRequestVar('exclude', 0, 'integer');
105       
106        # store the value in the cookie
107        $excludeCookie = new Piwik_Cookie($this->cookieName);
108        $excludeCookie->set($idSite, $exclude);
109       
110        # check if we can delete the cookie because there are no sites to exclude from tracking
111        # I don't use the SitesManager API because the user doesn't need to be superadmin in my case
112        $canDeleteCookie = true;
113        $siteids = Zend_Registry::get('db')->fetchAll('SELECT idsite FROM ' . Piwik::prefixTable('site'));
114        foreach ($siteids as $siteid) {
115            if ($excludeCookie->get($siteid['idsite'])) {
116                # don't delete cookie if at least one of the sites is excluded from tracking
117                $canDeleteCookie = false;
118                break;
119            }
120        }
121        # now save the cookie
122        if ($canDeleteCookie) {
123            $excludeCookie->delete();
124        } else {
125            $excludeCookie->save();
126        }
127       
128        # build parameters for redirecting back to dashboard
129        $params = array(
130            'module=CoreHome',
131            'action=index',
132            'idSite=' . $idSite
133        );
134        $period = Piwik_Common::getRequestVar('period', 0);
135        if ($period) $params[] = 'period=' . $period;
136        $date = Piwik_Common::getRequestVar('date', 0);
137        if ($date) $params[] = 'date=' . $date;
138       
139        # send the user back to the dashboard
140        Piwik_Url::redirectToUrl('index.php?' . implode('&', $params));
141    }
142
143}