From 4aa6dce8530fb1f62a33264a1da03dad0f80e7be Mon Sep 17 00:00:00 2001 From: Gilles van Eeden Date: Tue, 4 Apr 2017 16:53:27 +0200 Subject: [PATCH] add function isajaxrequest() (#1400) * Update Taxonomy.php * Update TwigExtension.php * Update TwigExtension.php Incorrect comment updated. * Update TwigExtension.php Add function ishuman() to detect agent disposition. * Update TwigExtension.php * Revert "Merge remote-tracking branch 'origin/develop' into develop" This reverts commit 7a59a8429064244a42ec33f5d25eea062382700c, reversing changes made to 102973d22cdaecd1e191a86128057ea28d3fa98f. * Revert "Revert "Merge remote-tracking branch 'origin/develop' into develop"" This reverts commit 50fc775c69c123fd2b10c641685657413af58359. * Attempt to resolve conflicts * param comment corrected * Update TwigExtension.php * Update TwigExtension.php * Update TwigExtension.php * Update TwigExtension.php * add isajaxrequest Check HTTP_X_REQUESTED_WITH header. Non critical use only. Handy for streamlining ajax presentation. * remove ishuman * remove Browser class --- system/src/Grav/Common/Twig/TwigExtension.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/system/src/Grav/Common/Twig/TwigExtension.php b/system/src/Grav/Common/Twig/TwigExtension.php index e12fa9a4c4..eff3e53fda 100644 --- a/system/src/Grav/Common/Twig/TwigExtension.php +++ b/system/src/Grav/Common/Twig/TwigExtension.php @@ -119,6 +119,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']), ]; } @@ -892,4 +893,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'); + } }