Skip to content

Instantly share code, notes, and snippets.

@visualizeq
Created October 16, 2012 08:34
Show Gist options
  • Save visualizeq/3898060 to your computer and use it in GitHub Desktop.
Save visualizeq/3898060 to your computer and use it in GitHub Desktop.
Yii HttpRequest (CSRF)
<?php
class HttpRequest extends CHttpRequest {
public $noCsrfValidationRoutes = array();
/**
* Normalizes the request data.
* This method strips off slashes in request data if get_magic_quotes_gpc() returns true.
* It also performs CSRF validation if {@link enableCsrfValidation} is true.
*/
protected function normalizeRequest()
{
parent::normalizeRequest();
if ($this->getIsPostRequest() && $this->enableCsrfValidation && !$this->checkCurrentRoute())
Yii::app()->detachEventHandler('onbeginRequest', array($this, 'validateCsrfToken'));
}
/**
* Checks if current route should be validated by validateCsrfToken()
*
* @return boolean true if current route should be validated
*/
private function checkCurrentRoute()
{
foreach ($this->noCsrfValidationRoutes as $checkPath) {
if (($pos = strpos($checkPath, "*")) !== false) {
$checkPath = substr($checkPath, 0, $pos - 1);
if (strpos($this->pathInfo, $checkPath) == 0)
return false;
} elseif ($this->pathInfo === $checkPath)
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment