nonce; // re-use an unexpired nonce (a small deviation from the "used only once" principle, so long as we do not reset the expiration) // to handle browser pre-fetch or double fetch caused by some browser add-ons/extensions if(empty($nonce)) { // generate a new nonce $nonce = md5(Piwik_Common::getSalt() . time() . Piwik_Common::generateUniqId()); $ns->nonce = $nonce; $ns->setExpirationSeconds($ttl, 'nonce'); } return $nonce; } /** * Verify nonce and check referrer (if present, i.e., it may be suppressed by the browser or a proxy/network). * * @param string $id Unique id * @param string $cnonce Nonce sent to client * @return bool true if valid; false otherwise */ static public function verifyNonce($id, $cnonce) { $ns = new Zend_Session_Namespace($id); $nonce = $ns->nonce; // validate token if(empty($cnonce) || $cnonce !== $nonce) { return false; } // validate referer $referer = Piwik_Url::getReferer(); if(!empty($referer) && (Piwik_Url::getLocalReferer() === false)) { return false; } // validate origin $origin = Piwik_Url::getOrigin(); if(!empty($origin) && ($origin == 'null' || !in_array($origin, Piwik_Url::getAcceptableOrigins()))) { return false; } return true; } }