| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | defined('PIWIK_INCLUDE_PATH') or die('Restricted access'); |
|---|
| 12 | |
|---|
| 13 | class 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 | |
|---|
| 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 | |
|---|
| 43 | $idSite = Piwik_Common::getRequestVar('idsite', 1, 'integer'); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | $excludeCookie = new Piwik_Cookie($this->cookieName); |
|---|
| 47 | if ($excludeCookie->isCookieFound()) { |
|---|
| 48 | |
|---|
| 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 | |
|---|
| 62 | class 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 | |
|---|
| 70 | $excludeCookie = new Piwik_Cookie($this->cookieName); |
|---|
| 71 | $excluded = (bool)$excludeCookie->get($idSite); |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | $params = array( |
|---|
| 75 | 'module=BlockingCookie', |
|---|
| 76 | 'action=setTracking', |
|---|
| 77 | 'idSite=' . $idSite, |
|---|
| 78 | 'exclude=' . (int)(!$excluded) |
|---|
| 79 | ); |
|---|
| 80 | |
|---|
| 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 | |
|---|
| 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 | |
|---|
| 101 | |
|---|
| 102 | public function setTracking () { |
|---|
| 103 | $idSite = Piwik_Common::getRequestVar('idSite', 1, 'integer'); |
|---|
| 104 | $exclude = Piwik_Common::getRequestVar('exclude', 0, 'integer'); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | $excludeCookie = new Piwik_Cookie($this->cookieName); |
|---|
| 108 | $excludeCookie->set($idSite, $exclude); |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 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 | |
|---|
| 117 | $canDeleteCookie = false; |
|---|
| 118 | break; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | if ($canDeleteCookie) { |
|---|
| 123 | $excludeCookie->delete(); |
|---|
| 124 | } else { |
|---|
| 125 | $excludeCookie->save(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 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 | |
|---|
| 140 | Piwik_Url::redirectToUrl('index.php?' . implode('&', $params)); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | } |
|---|