Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
tao_helpers_Request | |
0.00% |
0 / 34 |
|
0.00% |
0 / 3 |
210 | |
0.00% |
0 / 1 |
isAjax | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
getRelativeUrl | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
load | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
19 | * (under the project TAO-TRANSFER); |
20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
22 | * 2012-2016 (update and modification) Open Assessment Technologies SA; |
23 | * |
24 | */ |
25 | |
26 | /** |
27 | * Utilities on requests |
28 | * |
29 | * @access public |
30 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
31 | * @package tao |
32 | |
33 | */ |
34 | class tao_helpers_Request |
35 | { |
36 | // --- ASSOCIATIONS --- |
37 | |
38 | |
39 | // --- ATTRIBUTES --- |
40 | |
41 | // --- OPERATIONS --- |
42 | |
43 | /** |
44 | * Enables you to know if the request in the current scope is an ajax |
45 | * |
46 | * @access public |
47 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
48 | * @return boolean |
49 | */ |
50 | public static function isAjax() |
51 | { |
52 | $returnValue = (bool) false; |
53 | |
54 | if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) { |
55 | if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { |
56 | $returnValue = true; |
57 | } |
58 | } |
59 | |
60 | return (bool) $returnValue; |
61 | } |
62 | |
63 | /** |
64 | * Returns the current relative call url, without leading slash |
65 | * |
66 | * @param string $url |
67 | * @throws ResolverException |
68 | * @return string |
69 | */ |
70 | public static function getRelativeUrl($url = null) |
71 | { |
72 | $url = is_null($url) ? '/' . ltrim($_SERVER['REQUEST_URI'], '/') : $url; |
73 | $rootUrlPath = parse_url(ROOT_URL, PHP_URL_PATH); |
74 | $absPath = parse_url($url, PHP_URL_PATH); |
75 | if (substr($absPath, 0, strlen($rootUrlPath)) != $rootUrlPath) { |
76 | throw new ResolverException('Request Uri ' . $url . ' outside of TAO path ' . ROOT_URL); |
77 | } |
78 | if ($absPath === $rootUrlPath) { |
79 | $result = ''; |
80 | } else { |
81 | $result = substr($absPath, strlen($rootUrlPath)); |
82 | } |
83 | return $result; |
84 | } |
85 | |
86 | |
87 | /** |
88 | * Perform an HTTP Request on the defined url and return the content |
89 | * |
90 | * @access public |
91 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
92 | * @param string url |
93 | * @param boolean useSession if you want to use the same session in the remotre server |
94 | * @return string |
95 | * @throws Exception |
96 | */ |
97 | public static function load($url, $useSession = false) |
98 | { |
99 | $returnValue = (string) ''; |
100 | |
101 | |
102 | |
103 | if (!empty($url)) { |
104 | if ($useSession) { |
105 | session_write_close(); |
106 | } |
107 | |
108 | $curlHandler = curl_init(); |
109 | |
110 | //if there is an http auth, it's mandatory to connect with curl |
111 | if (USE_HTTP_AUTH) { |
112 | curl_setopt($curlHandler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
113 | curl_setopt($curlHandler, CURLOPT_USERPWD, USE_HTTP_USER . ":" . USE_HTTP_PASS); |
114 | } |
115 | curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1); |
116 | |
117 | //to keep the session |
118 | if ($useSession) { |
119 | if (!preg_match("/&$/", $url)) { |
120 | $url .= '&'; |
121 | } |
122 | $url .= 'session_id=' . session_id(); |
123 | curl_setopt($curlHandler, CURLOPT_COOKIE, session_name() . '=' . $_COOKIE[session_name()] . '; path=/'); |
124 | } |
125 | |
126 | curl_setopt($curlHandler, CURLOPT_URL, $url); |
127 | |
128 | $returnValue = curl_exec($curlHandler); |
129 | if (curl_errno($curlHandler) > 0) { |
130 | throw new Exception("Request error " . curl_errno($curlHandler) . ": " . curl_error($curlHandler)); |
131 | } |
132 | curl_close($curlHandler); |
133 | } |
134 | |
135 | |
136 | |
137 | return (string) $returnValue; |
138 | } |
139 | } |