Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function isajaxrequest() #1400

Merged
merged 18 commits into from
Apr 4, 2017
Merged
14 changes: 14 additions & 0 deletions system/src/Grav/Common/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public function getFunctions()
new \Twig_SimpleFunction('get_cookie', [$this, 'getCookie']),
new \Twig_SimpleFunction('redirect_me', [$this, 'redirectFunc']),
new \Twig_SimpleFunction('range', [$this, 'rangeFunc']),
new \Twig_SimpleFunction('isajaxrequest', [$this, 'isAjaxFunc']),
];
}

Expand Down Expand Up @@ -848,4 +849,17 @@ public function rangeFunc($start = 0, $end = 100, $step = 1)
{
return range($start, $end, $step);
}

/**
* Check if HTTP_X_REQUESTED_WITH has been set to xmlhttprequest,
* in which case we may unsafely assume ajax. Non critical use only.
*
* @return true if HTTP_X_REQUESTED_WITH exists and has been set to xmlhttprequest
*/
public function isAjaxFunc()
{
return (
!empty($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
}
}