Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CASClient often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CASClient, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | class CASClient |
||
51 | { |
||
52 | |||
53 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
54 | // XX XX |
||
55 | // XX CONFIGURATION XX |
||
56 | // XX XX |
||
57 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
58 | |||
59 | // ######################################################################## |
||
60 | // HTML OUTPUT |
||
61 | // ######################################################################## |
||
62 | /** |
||
63 | * @addtogroup internalOutput |
||
64 | * @{ |
||
65 | */ |
||
66 | |||
67 | /** |
||
68 | * This method filters a string by replacing special tokens by appropriate values |
||
69 | * and prints it. The corresponding tokens are taken into account: |
||
70 | * - __CAS_VERSION__ |
||
71 | * - __PHPCAS_VERSION__ |
||
72 | * - __SERVER_BASE_URL__ |
||
73 | * |
||
74 | * Used by CASClient::PrintHTMLHeader() and CASClient::printHTMLFooter(). |
||
75 | * |
||
76 | * @param $str the string to filter and output |
||
77 | * |
||
78 | * @private |
||
79 | */ |
||
80 | function HTMLFilterOutput($str) |
||
87 | |||
88 | /** |
||
89 | * A string used to print the header of HTML pages. Written by CASClient::setHTMLHeader(), |
||
90 | * read by CASClient::printHTMLHeader(). |
||
91 | * |
||
92 | * @hideinitializer |
||
93 | * @private |
||
94 | * @see CASClient::setHTMLHeader, CASClient::printHTMLHeader() |
||
95 | */ |
||
96 | var $_output_header = ''; |
||
97 | |||
98 | /** |
||
99 | * This method prints the header of the HTML output (after filtering). If |
||
100 | * CASClient::setHTMLHeader() was not used, a default header is output. |
||
101 | * |
||
102 | * @param $title the title of the page |
||
103 | * |
||
104 | * @see HTMLFilterOutput() |
||
105 | * @private |
||
106 | */ |
||
107 | function printHTMLHeader($title) |
||
117 | |||
118 | /** |
||
119 | * A string used to print the footer of HTML pages. Written by CASClient::setHTMLFooter(), |
||
120 | * read by printHTMLFooter(). |
||
121 | * |
||
122 | * @hideinitializer |
||
123 | * @private |
||
124 | * @see CASClient::setHTMLFooter, CASClient::printHTMLFooter() |
||
125 | */ |
||
126 | var $_output_footer = ''; |
||
127 | |||
128 | /** |
||
129 | * This method prints the footer of the HTML output (after filtering). If |
||
130 | * CASClient::setHTMLFooter() was not used, a default footer is output. |
||
131 | * |
||
132 | * @see HTMLFilterOutput() |
||
133 | * @private |
||
134 | */ |
||
135 | function printHTMLFooter() |
||
141 | |||
142 | /** |
||
143 | * This method set the HTML header used for all outputs. |
||
144 | * |
||
145 | * @param $header the HTML header. |
||
146 | * |
||
147 | * @public |
||
148 | */ |
||
149 | function setHTMLHeader($header) |
||
153 | |||
154 | /** |
||
155 | * This method set the HTML footer used for all outputs. |
||
156 | * |
||
157 | * @param $footer the HTML footer. |
||
158 | * |
||
159 | * @public |
||
160 | */ |
||
161 | function setHTMLFooter($footer) |
||
165 | |||
166 | /** @} */ |
||
167 | // ######################################################################## |
||
168 | // INTERNATIONALIZATION |
||
169 | // ######################################################################## |
||
170 | /** |
||
171 | * @addtogroup internalLang |
||
172 | * @{ |
||
173 | */ |
||
174 | /** |
||
175 | * A string corresponding to the language used by phpCAS. Written by |
||
176 | * CASClient::setLang(), read by CASClient::getLang(). |
||
177 | * @note debugging information is always in english (debug purposes only). |
||
178 | * |
||
179 | * @hideinitializer |
||
180 | * @private |
||
181 | * @sa CASClient::_strings, CASClient::getString() |
||
182 | */ |
||
183 | var $_lang = ''; |
||
184 | |||
185 | /** |
||
186 | * This method returns the language used by phpCAS. |
||
187 | * |
||
188 | * @return a string representing the language |
||
189 | * |
||
190 | * @private |
||
191 | */ |
||
192 | function getLang() |
||
199 | |||
200 | /** |
||
201 | * array containing the strings used by phpCAS. Written by CASClient::setLang(), read by |
||
202 | * CASClient::getString() and used by CASClient::setLang(). |
||
203 | * |
||
204 | * @note This array is filled by instructions in CAS/languages/<$this->_lang>.php |
||
205 | * |
||
206 | * @private |
||
207 | * @see CASClient::_lang, CASClient::getString(), CASClient::setLang(), CASClient::getLang() |
||
208 | */ |
||
209 | var $_strings; |
||
210 | |||
211 | /** |
||
212 | * This method returns a string depending on the language. |
||
213 | * |
||
214 | * @param $str the index of the string in $_string. |
||
215 | * |
||
216 | * @return the string corresponding to $index in $string. |
||
217 | * |
||
218 | * @private |
||
219 | */ |
||
220 | function getString($str) |
||
230 | |||
231 | /** |
||
232 | * This method is used to set the language used by phpCAS. |
||
233 | * @note Can be called only once. |
||
234 | * |
||
235 | * @param $lang a string representing the language. |
||
236 | * |
||
237 | * @public |
||
238 | * @sa CAS_LANG_FRENCH, CAS_LANG_ENGLISH |
||
239 | */ |
||
240 | function setLang($lang) |
||
250 | |||
251 | /** @} */ |
||
252 | // ######################################################################## |
||
253 | // CAS SERVER CONFIG |
||
254 | // ######################################################################## |
||
255 | /** |
||
256 | * @addtogroup internalConfig |
||
257 | * @{ |
||
258 | */ |
||
259 | |||
260 | /** |
||
261 | * a record to store information about the CAS server. |
||
262 | * - $_server["version"]: the version of the CAS server |
||
263 | * - $_server["hostname"]: the hostname of the CAS server |
||
264 | * - $_server["port"]: the port the CAS server is running on |
||
265 | * - $_server["uri"]: the base URI the CAS server is responding on |
||
266 | * - $_server["base_url"]: the base URL of the CAS server |
||
267 | * - $_server["login_url"]: the login URL of the CAS server |
||
268 | * - $_server["service_validate_url"]: the service validating URL of the CAS server |
||
269 | * - $_server["proxy_url"]: the proxy URL of the CAS server |
||
270 | * - $_server["proxy_validate_url"]: the proxy validating URL of the CAS server |
||
271 | * - $_server["logout_url"]: the logout URL of the CAS server |
||
272 | * |
||
273 | * $_server["version"], $_server["hostname"], $_server["port"] and $_server["uri"] |
||
274 | * are written by CASClient::CASClient(), read by CASClient::getServerVersion(), |
||
275 | * CASClient::getServerHostname(), CASClient::getServerPort() and CASClient::getServerURI(). |
||
276 | * |
||
277 | * The other fields are written and read by CASClient::getServerBaseURL(), |
||
278 | * CASClient::getServerLoginURL(), CASClient::getServerServiceValidateURL(), |
||
279 | * CASClient::getServerProxyValidateURL() and CASClient::getServerLogoutURL(). |
||
280 | * |
||
281 | * @hideinitializer |
||
282 | * @private |
||
283 | */ |
||
284 | var $_server = array( |
||
285 | 'version' => -1, |
||
286 | 'hostname' => 'none', |
||
287 | 'port' => -1, |
||
288 | 'uri' => 'none' |
||
289 | ); |
||
290 | |||
291 | /** |
||
292 | * This method is used to retrieve the version of the CAS server. |
||
293 | * @return the version of the CAS server. |
||
294 | * @private |
||
295 | */ |
||
296 | function getServerVersion() |
||
300 | |||
301 | /** |
||
302 | * This method is used to retrieve the hostname of the CAS server. |
||
303 | * @return the hostname of the CAS server. |
||
304 | * @private |
||
305 | */ |
||
306 | function getServerHostname() |
||
310 | |||
311 | /** |
||
312 | * This method is used to retrieve the port of the CAS server. |
||
313 | * @return the port of the CAS server. |
||
314 | * @private |
||
315 | */ |
||
316 | function getServerPort() |
||
320 | |||
321 | /** |
||
322 | * This method is used to retrieve the URI of the CAS server. |
||
323 | * @return a URI. |
||
324 | * @private |
||
325 | */ |
||
326 | function getServerURI() |
||
330 | |||
331 | /** |
||
332 | * This method is used to retrieve the base URL of the CAS server. |
||
333 | * @return a URL. |
||
334 | * @private |
||
335 | */ |
||
336 | function getServerBaseURL() |
||
348 | |||
349 | /** |
||
350 | * This method is used to retrieve the login URL of the CAS server. |
||
351 | * @param $gateway true to check authentication, false to force it |
||
352 | * @param $renew true to force the authentication with the CAS server |
||
353 | * NOTE : It is recommended that CAS implementations ignore the |
||
354 | * "gateway" parameter if "renew" is set |
||
355 | * @return a URL. |
||
356 | * @private |
||
357 | */ |
||
358 | function getServerLoginURL($gateway = false, $renew = false) |
||
378 | |||
379 | /** |
||
380 | * This method sets the login URL of the CAS server. |
||
381 | * @param $url the login URL |
||
382 | * @private |
||
383 | * @since 0.4.21 by Wyman Chan |
||
384 | */ |
||
385 | function setServerLoginURL($url) |
||
389 | |||
390 | |||
391 | /** |
||
392 | * This method sets the serviceValidate URL of the CAS server. |
||
393 | * @param $url the serviceValidate URL |
||
394 | * @private |
||
395 | * @since 1.1.0 by Joachim Fritschi |
||
396 | */ |
||
397 | function setServerServiceValidateURL($url) |
||
401 | |||
402 | |||
403 | /** |
||
404 | * This method sets the proxyValidate URL of the CAS server. |
||
405 | * @param $url the proxyValidate URL |
||
406 | * @private |
||
407 | * @since 1.1.0 by Joachim Fritschi |
||
408 | */ |
||
409 | function setServerProxyValidateURL($url) |
||
413 | |||
414 | |||
415 | /** |
||
416 | * This method sets the samlValidate URL of the CAS server. |
||
417 | * @param $url the samlValidate URL |
||
418 | * @private |
||
419 | * @since 1.1.0 by Joachim Fritschi |
||
420 | */ |
||
421 | function setServerSamlValidateURL($url) |
||
425 | |||
426 | |||
427 | /** |
||
428 | * This method is used to retrieve the service validating URL of the CAS server. |
||
429 | * @return a URL. |
||
430 | * @private |
||
431 | */ |
||
432 | function getServerServiceValidateURL() |
||
448 | |||
449 | /** |
||
450 | * This method is used to retrieve the SAML validating URL of the CAS server. |
||
451 | * @return a URL. |
||
452 | * @private |
||
453 | */ |
||
454 | function getServerSamlValidateURL() |
||
468 | |||
469 | /** |
||
470 | * This method is used to retrieve the proxy validating URL of the CAS server. |
||
471 | * @return a URL. |
||
472 | * @private |
||
473 | */ |
||
474 | View Code Duplication | function getServerProxyValidateURL() |
|
490 | |||
491 | /** |
||
492 | * This method is used to retrieve the proxy URL of the CAS server. |
||
493 | * @return a URL. |
||
494 | * @private |
||
495 | */ |
||
496 | View Code Duplication | function getServerProxyURL() |
|
511 | |||
512 | /** |
||
513 | * This method is used to retrieve the logout URL of the CAS server. |
||
514 | * @return a URL. |
||
515 | * @private |
||
516 | */ |
||
517 | function getServerLogoutURL() |
||
525 | |||
526 | /** |
||
527 | * This method sets the logout URL of the CAS server. |
||
528 | * @param $url the logout URL |
||
529 | * @private |
||
530 | * @since 0.4.21 by Wyman Chan |
||
531 | */ |
||
532 | function setServerLogoutURL($url) |
||
536 | |||
537 | /** |
||
538 | * An array to store extra curl options. |
||
539 | */ |
||
540 | var $_curl_options = array(); |
||
541 | |||
542 | /** |
||
543 | * This method is used to set additional user curl options. |
||
544 | */ |
||
545 | function setExtraCurlOption($key, $value) |
||
549 | |||
550 | /** |
||
551 | * This method checks to see if the request is secured via HTTPS |
||
552 | * @return true if https, false otherwise |
||
553 | * @private |
||
554 | */ |
||
555 | function isHttps() |
||
565 | |||
566 | // ######################################################################## |
||
567 | // CONSTRUCTOR |
||
568 | // ######################################################################## |
||
569 | /** |
||
570 | * CASClient constructor. |
||
571 | * |
||
572 | * @param $server_version the version of the CAS server |
||
573 | * @param $proxy TRUE if the CAS client is a CAS proxy, FALSE otherwise |
||
574 | * @param $server_hostname the hostname of the CAS server |
||
575 | * @param $server_port the port the CAS server is running on |
||
576 | * @param $server_uri the URI the CAS server is responding on |
||
577 | * @param $start_session Have phpCAS start PHP sessions (default true) |
||
578 | * |
||
579 | * @return a newly created CASClient object |
||
580 | * |
||
581 | * @public |
||
582 | */ |
||
583 | function CASClient( |
||
741 | |||
742 | /** @} */ |
||
743 | |||
744 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
745 | // XX XX |
||
746 | // XX AUTHENTICATION XX |
||
747 | // XX XX |
||
748 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
749 | |||
750 | /** |
||
751 | * @addtogroup internalAuthentication |
||
752 | * @{ |
||
753 | */ |
||
754 | |||
755 | /** |
||
756 | * The Authenticated user. Written by CASClient::setUser(), read by CASClient::getUser(). |
||
757 | * @attention client applications should use phpCAS::getUser(). |
||
758 | * |
||
759 | * @hideinitializer |
||
760 | * @private |
||
761 | */ |
||
762 | var $_user = ''; |
||
763 | |||
764 | /** |
||
765 | * This method sets the CAS user's login name. |
||
766 | * |
||
767 | * @param $user the login name of the authenticated user. |
||
768 | * |
||
769 | * @private |
||
770 | */ |
||
771 | function setUser($user) |
||
775 | |||
776 | /** |
||
777 | * This method returns the CAS user's login name. |
||
778 | * @warning should be called only after CASClient::forceAuthentication() or |
||
779 | * CASClient::isAuthenticated(), otherwise halt with an error. |
||
780 | * |
||
781 | * @return the login name of the authenticated user |
||
782 | */ |
||
783 | View Code Duplication | function getUser() |
|
790 | |||
791 | |||
792 | |||
793 | /*********************************************************************************************************************** |
||
794 | * Atrributes section |
||
795 | * |
||
796 | * @author Matthias Crauwels <[email protected]>, Ghent University, Belgium |
||
797 | * |
||
798 | ***********************************************************************************************************************/ |
||
799 | /** |
||
800 | * The Authenticated users attributes. Written by CASClient::setAttributes(), read by CASClient::getAttributes(). |
||
801 | * @attention client applications should use phpCAS::getAttributes(). |
||
802 | * |
||
803 | * @hideinitializer |
||
804 | * @private |
||
805 | */ |
||
806 | var $_attributes = array(); |
||
807 | |||
808 | function setAttributes($attributes) |
||
812 | |||
813 | View Code Duplication | function getAttributes() |
|
820 | |||
821 | function hasAttributes() |
||
825 | |||
826 | function hasAttribute($key) |
||
830 | |||
831 | function getAttribute($key) |
||
837 | |||
838 | /** |
||
839 | * This method is called to renew the authentication of the user |
||
840 | * If the user is authenticated, renew the connection |
||
841 | * If not, redirect to CAS |
||
842 | * @public |
||
843 | */ |
||
844 | function renewAuthentication() |
||
859 | |||
860 | /** |
||
861 | * This method is called to be sure that the user is authenticated. When not |
||
862 | * authenticated, halt by redirecting to the CAS server; otherwise return TRUE. |
||
863 | * @return TRUE when the user is authenticated; otherwise halt. |
||
864 | * @public |
||
865 | */ |
||
866 | function forceAuthentication() |
||
886 | |||
887 | /** |
||
888 | * An integer that gives the number of times authentication will be cached before rechecked. |
||
889 | * |
||
890 | * @hideinitializer |
||
891 | * @private |
||
892 | */ |
||
893 | var $_cache_times_for_auth_recheck = 0; |
||
894 | |||
895 | /** |
||
896 | * Set the number of times authentication will be cached before rechecked. |
||
897 | * |
||
898 | * @param $n an integer. |
||
899 | * |
||
900 | * @public |
||
901 | */ |
||
902 | function setCacheTimesForAuthRecheck($n) |
||
906 | |||
907 | /** |
||
908 | * This method is called to check whether the user is authenticated or not. |
||
909 | * @return TRUE when the user is authenticated, FALSE otherwise. |
||
910 | * @public |
||
911 | */ |
||
912 | function checkAuthentication() |
||
958 | |||
959 | /** |
||
960 | * This method is called to check if the user is authenticated (previously or by |
||
961 | * tickets given in the URL). |
||
962 | * |
||
963 | * @return TRUE when the user is authenticated. Also may redirect to the same URL without the ticket. |
||
964 | * |
||
965 | * @public |
||
966 | */ |
||
967 | function isAuthenticated() |
||
1026 | |||
1027 | /** |
||
1028 | * This method tells if the current session is authenticated. |
||
1029 | * @return true if authenticated based soley on $_SESSION variable |
||
1030 | * @since 0.4.22 by Brendan Arnold |
||
1031 | */ |
||
1032 | function isSessionAuthenticated() |
||
1036 | |||
1037 | /** |
||
1038 | * This method tells if the user has already been (previously) authenticated |
||
1039 | * by looking into the session variables. |
||
1040 | * |
||
1041 | * @note This function switches to callback mode when needed. |
||
1042 | * |
||
1043 | * @return TRUE when the user has already been authenticated; FALSE otherwise. |
||
1044 | * |
||
1045 | * @private |
||
1046 | */ |
||
1047 | function wasPreviouslyAuthenticated() |
||
1100 | |||
1101 | /** |
||
1102 | * This method is used to redirect the client to the CAS server. |
||
1103 | * It is used by CASClient::forceAuthentication() and CASClient::checkAuthentication(). |
||
1104 | * @param $gateway true to check authentication, false to force it |
||
1105 | * @param $renew true to force the authentication with the CAS server |
||
1106 | * @public |
||
1107 | */ |
||
1108 | function redirectToCas($gateway = false, $renew = false) |
||
1123 | |||
1124 | |||
1125 | /** |
||
1126 | * This method is used to logout from CAS. |
||
1127 | * @params $params an array that contains the optional url and service parameters that will be passed to the CAS server |
||
1128 | * @public |
||
1129 | */ |
||
1130 | function logout($params) |
||
1155 | |||
1156 | /** |
||
1157 | * @return true if the current request is a logout request. |
||
1158 | * @private |
||
1159 | */ |
||
1160 | function isLogoutRequest() |
||
1164 | |||
1165 | /** |
||
1166 | * @return true if a logout request is allowed. |
||
1167 | * @private |
||
1168 | */ |
||
1169 | function isLogoutRequestAllowed() |
||
1172 | |||
1173 | /** |
||
1174 | * This method handles logout requests. |
||
1175 | * @param $check_client true to check the client bofore handling the request, |
||
1176 | * false not to perform any access control. True by default. |
||
1177 | * @param $allowed_clients an array of host names allowed to send logout requests. |
||
1178 | * By default, only the CAs server (declared in the constructor) will be allowed. |
||
1179 | * @public |
||
1180 | */ |
||
1181 | function handleLogoutRequests($check_client = true, $allowed_clients = false) |
||
1244 | |||
1245 | /** @} */ |
||
1246 | |||
1247 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
1248 | // XX XX |
||
1249 | // XX BASIC CLIENT FEATURES (CAS 1.0) XX |
||
1250 | // XX XX |
||
1251 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
1252 | |||
1253 | // ######################################################################## |
||
1254 | // ST |
||
1255 | // ######################################################################## |
||
1256 | /** |
||
1257 | * @addtogroup internalBasic |
||
1258 | * @{ |
||
1259 | */ |
||
1260 | |||
1261 | /** |
||
1262 | * the Service Ticket provided in the URL of the request if present |
||
1263 | * (empty otherwise). Written by CASClient::CASClient(), read by |
||
1264 | * CASClient::getST() and CASClient::hasPGT(). |
||
1265 | * |
||
1266 | * @hideinitializer |
||
1267 | * @private |
||
1268 | */ |
||
1269 | var $_st = ''; |
||
1270 | |||
1271 | /** |
||
1272 | * This method returns the Service Ticket provided in the URL of the request. |
||
1273 | * @return The service ticket. |
||
1274 | * @private |
||
1275 | */ |
||
1276 | function getST() |
||
1280 | |||
1281 | /** |
||
1282 | * This method stores the Service Ticket. |
||
1283 | * @param $st The Service Ticket. |
||
1284 | * @private |
||
1285 | */ |
||
1286 | function setST($st) |
||
1290 | |||
1291 | /** |
||
1292 | * This method tells if a Service Ticket was stored. |
||
1293 | * @return TRUE if a Service Ticket has been stored. |
||
1294 | * @private |
||
1295 | */ |
||
1296 | function hasST() |
||
1300 | |||
1301 | /** @} */ |
||
1302 | |||
1303 | // ######################################################################## |
||
1304 | // ST VALIDATION |
||
1305 | // ######################################################################## |
||
1306 | /** |
||
1307 | * @addtogroup internalBasic |
||
1308 | * @{ |
||
1309 | */ |
||
1310 | |||
1311 | /** |
||
1312 | * the certificate of the CAS server. |
||
1313 | * |
||
1314 | * @hideinitializer |
||
1315 | * @private |
||
1316 | */ |
||
1317 | var $_cas_server_cert = ''; |
||
1318 | |||
1319 | /** |
||
1320 | * the certificate of the CAS server CA. |
||
1321 | * |
||
1322 | * @hideinitializer |
||
1323 | * @private |
||
1324 | */ |
||
1325 | var $_cas_server_ca_cert = ''; |
||
1326 | |||
1327 | /** |
||
1328 | * Set to true not to validate the CAS server. |
||
1329 | * |
||
1330 | * @hideinitializer |
||
1331 | * @private |
||
1332 | */ |
||
1333 | var $_no_cas_server_validation = false; |
||
1334 | |||
1335 | /** |
||
1336 | * Set the certificate of the CAS server. |
||
1337 | * |
||
1338 | * @param $cert the PEM certificate |
||
1339 | */ |
||
1340 | function setCasServerCert($cert) |
||
1344 | |||
1345 | /** |
||
1346 | * Set the CA certificate of the CAS server. |
||
1347 | * |
||
1348 | * @param $cert the PEM certificate of the CA that emited the cert of the server |
||
1349 | */ |
||
1350 | function setCasServerCACert($cert) |
||
1354 | |||
1355 | /** |
||
1356 | * Set no SSL validation for the CAS server. |
||
1357 | */ |
||
1358 | function setNoCasServerValidation() |
||
1362 | |||
1363 | /** |
||
1364 | * This method is used to validate a ST; halt on failure, and sets $validate_url, |
||
1365 | * $text_reponse and $tree_response on success. These parameters are used later |
||
1366 | * by CASClient::validatePGT() for CAS proxies. |
||
1367 | * Used for all CAS 1.0 validations |
||
1368 | * @param $validate_url the URL of the request to the CAS server. |
||
1369 | * @param $text_response the response of the CAS server, as is (XML text). |
||
1370 | * @param $tree_response the response of the CAS server, as a DOM XML tree. |
||
1371 | * |
||
1372 | * @return bool TRUE when successfull, halt otherwise by calling CASClient::authError(). |
||
1373 | * |
||
1374 | * @private |
||
1375 | */ |
||
1376 | function validateST($validate_url, &$text_response, &$tree_response) |
||
1486 | |||
1487 | // ######################################################################## |
||
1488 | // SAML VALIDATION |
||
1489 | // ######################################################################## |
||
1490 | /** |
||
1491 | * @addtogroup internalBasic |
||
1492 | * @{ |
||
1493 | */ |
||
1494 | |||
1495 | /** |
||
1496 | * This method is used to validate a SAML TICKET; halt on failure, and sets $validate_url, |
||
1497 | * $text_reponse and $tree_response on success. These parameters are used later |
||
1498 | * by CASClient::validatePGT() for CAS proxies. |
||
1499 | * |
||
1500 | * @param $validate_url the URL of the request to the CAS server. |
||
1501 | * @param $text_response the response of the CAS server, as is (XML text). |
||
1502 | * @param $tree_response the response of the CAS server, as a DOM XML tree. |
||
1503 | * |
||
1504 | * @return bool TRUE when successfull, halt otherwise by calling CASClient::authError(). |
||
1505 | * |
||
1506 | * @private |
||
1507 | */ |
||
1508 | function validateSA($validate_url, &$text_response, &$tree_response) |
||
1576 | |||
1577 | /** |
||
1578 | * This method will parse the DOM and pull out the attributes from the SAML |
||
1579 | * payload and put them into an array, then put the array into the session. |
||
1580 | * |
||
1581 | * @param $text_response the SAML payload. |
||
1582 | * @return bool TRUE when successfull, halt otherwise by calling CASClient::authError(). |
||
1583 | * |
||
1584 | * @private |
||
1585 | */ |
||
1586 | function setSessionAttributes($text_response) |
||
1630 | |||
1631 | /** @} */ |
||
1632 | |||
1633 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
1634 | // XX XX |
||
1635 | // XX PROXY FEATURES (CAS 2.0) XX |
||
1636 | // XX XX |
||
1637 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
1638 | |||
1639 | // ######################################################################## |
||
1640 | // PROXYING |
||
1641 | // ######################################################################## |
||
1642 | /** |
||
1643 | * @addtogroup internalProxy |
||
1644 | * @{ |
||
1645 | */ |
||
1646 | |||
1647 | /** |
||
1648 | * A boolean telling if the client is a CAS proxy or not. Written by CASClient::CASClient(), |
||
1649 | * read by CASClient::isProxy(). |
||
1650 | * |
||
1651 | * @private |
||
1652 | */ |
||
1653 | var $_proxy; |
||
1654 | |||
1655 | /** |
||
1656 | * Tells if a CAS client is a CAS proxy or not |
||
1657 | * |
||
1658 | * @return TRUE when the CAS client is a CAs proxy, FALSE otherwise |
||
1659 | * |
||
1660 | * @private |
||
1661 | */ |
||
1662 | function isProxy() |
||
1666 | |||
1667 | /** @} */ |
||
1668 | // ######################################################################## |
||
1669 | // PGT |
||
1670 | // ######################################################################## |
||
1671 | /** |
||
1672 | * @addtogroup internalProxy |
||
1673 | * @{ |
||
1674 | */ |
||
1675 | |||
1676 | /** |
||
1677 | * the Proxy Grnting Ticket given by the CAS server (empty otherwise). |
||
1678 | * Written by CASClient::setPGT(), read by CASClient::getPGT() and CASClient::hasPGT(). |
||
1679 | * |
||
1680 | * @hideinitializer |
||
1681 | * @private |
||
1682 | */ |
||
1683 | var $_pgt = ''; |
||
1684 | |||
1685 | /** |
||
1686 | * This method returns the Proxy Granting Ticket given by the CAS server. |
||
1687 | * @return The Proxy Granting Ticket. |
||
1688 | * @private |
||
1689 | */ |
||
1690 | function getPGT() |
||
1694 | |||
1695 | /** |
||
1696 | * This method stores the Proxy Granting Ticket. |
||
1697 | * @param $pgt The Proxy Granting Ticket. |
||
1698 | * @private |
||
1699 | */ |
||
1700 | function setPGT($pgt) |
||
1704 | |||
1705 | /** |
||
1706 | * This method tells if a Proxy Granting Ticket was stored. |
||
1707 | * @return TRUE if a Proxy Granting Ticket has been stored. |
||
1708 | * @private |
||
1709 | */ |
||
1710 | function hasPGT() |
||
1714 | |||
1715 | /** @} */ |
||
1716 | |||
1717 | // ######################################################################## |
||
1718 | // CALLBACK MODE |
||
1719 | // ######################################################################## |
||
1720 | /** |
||
1721 | * @addtogroup internalCallback |
||
1722 | * @{ |
||
1723 | */ |
||
1724 | /** |
||
1725 | * each PHP script using phpCAS in proxy mode is its own callback to get the |
||
1726 | * PGT back from the CAS server. callback_mode is detected by the constructor |
||
1727 | * thanks to the GET parameters. |
||
1728 | */ |
||
1729 | |||
1730 | /** |
||
1731 | * a boolean to know if the CAS client is running in callback mode. Written by |
||
1732 | * CASClient::setCallBackMode(), read by CASClient::isCallbackMode(). |
||
1733 | * |
||
1734 | * @hideinitializer |
||
1735 | * @private |
||
1736 | */ |
||
1737 | var $_callback_mode = false; |
||
1738 | |||
1739 | /** |
||
1740 | * This method sets/unsets callback mode. |
||
1741 | * |
||
1742 | * @param $callback_mode TRUE to set callback mode, FALSE otherwise. |
||
1743 | * |
||
1744 | * @private |
||
1745 | */ |
||
1746 | function setCallbackMode($callback_mode) |
||
1750 | |||
1751 | /** |
||
1752 | * This method returns TRUE when the CAs client is running i callback mode, |
||
1753 | * FALSE otherwise. |
||
1754 | * |
||
1755 | * @return A boolean. |
||
1756 | * |
||
1757 | * @private |
||
1758 | */ |
||
1759 | function isCallbackMode() |
||
1763 | |||
1764 | /** |
||
1765 | * the URL that should be used for the PGT callback (in fact the URL of the |
||
1766 | * current request without any CGI parameter). Written and read by |
||
1767 | * CASClient::getCallbackURL(). |
||
1768 | * |
||
1769 | * @hideinitializer |
||
1770 | * @private |
||
1771 | */ |
||
1772 | var $_callback_url = ''; |
||
1773 | |||
1774 | /** |
||
1775 | * This method returns the URL that should be used for the PGT callback (in |
||
1776 | * fact the URL of the current request without any CGI parameter, except if |
||
1777 | * phpCAS::setFixedCallbackURL() was used). |
||
1778 | * |
||
1779 | * @return The callback URL |
||
1780 | * |
||
1781 | * @private |
||
1782 | */ |
||
1783 | function getCallbackURL() |
||
1818 | |||
1819 | /** |
||
1820 | * This method sets the callback url. |
||
1821 | * |
||
1822 | * @param $callback_url url to set callback |
||
1823 | * |
||
1824 | * @private |
||
1825 | */ |
||
1826 | function setCallbackURL($url) |
||
1830 | |||
1831 | /** |
||
1832 | * This method is called by CASClient::CASClient() when running in callback |
||
1833 | * mode. It stores the PGT and its PGT Iou, prints its output and halts. |
||
1834 | * |
||
1835 | * @private |
||
1836 | */ |
||
1837 | function callback() |
||
1850 | |||
1851 | /** @} */ |
||
1852 | |||
1853 | // ######################################################################## |
||
1854 | // PGT STORAGE |
||
1855 | // ######################################################################## |
||
1856 | /** |
||
1857 | * @addtogroup internalPGTStorage |
||
1858 | * @{ |
||
1859 | */ |
||
1860 | |||
1861 | /** |
||
1862 | * an instance of a class inheriting of PGTStorage, used to deal with PGT |
||
1863 | * storage. Created by CASClient::setPGTStorageFile() or CASClient::setPGTStorageDB(), used |
||
1864 | * by CASClient::setPGTStorageFile(), CASClient::setPGTStorageDB() and CASClient::initPGTStorage(). |
||
1865 | * |
||
1866 | * @hideinitializer |
||
1867 | * @private |
||
1868 | */ |
||
1869 | var $_pgt_storage = null; |
||
1870 | |||
1871 | /** |
||
1872 | * This method is used to initialize the storage of PGT's. |
||
1873 | * Halts on error. |
||
1874 | * |
||
1875 | * @private |
||
1876 | */ |
||
1877 | function initPGTStorage() |
||
1887 | |||
1888 | /** |
||
1889 | * This method stores a PGT. Halts on error. |
||
1890 | * |
||
1891 | * @param $pgt the PGT to store |
||
1892 | * @param $pgt_iou its corresponding Iou |
||
1893 | * |
||
1894 | * @private |
||
1895 | */ |
||
1896 | function storePGT($pgt, $pgt_iou) |
||
1903 | |||
1904 | /** |
||
1905 | * This method reads a PGT from its Iou and deletes the corresponding storage entry. |
||
1906 | * |
||
1907 | * @param $pgt_iou the PGT Iou |
||
1908 | * |
||
1909 | * @return The PGT corresponding to the Iou, FALSE when not found. |
||
1910 | * |
||
1911 | * @private |
||
1912 | */ |
||
1913 | function loadPGT($pgt_iou) |
||
1920 | |||
1921 | /** |
||
1922 | * This method is used to tell phpCAS to store the response of the |
||
1923 | * CAS server to PGT requests onto the filesystem. |
||
1924 | * |
||
1925 | * @param $format the format used to store the PGT's (`plain' and `xml' allowed) |
||
1926 | * @param $path the path where the PGT's should be stored |
||
1927 | * |
||
1928 | * @public |
||
1929 | */ |
||
1930 | function setPGTStorageFile( |
||
1942 | |||
1943 | /** |
||
1944 | * This method is used to tell phpCAS to store the response of the |
||
1945 | * CAS server to PGT requests into a database. |
||
1946 | * @note The connection to the database is done only when needed. |
||
1947 | * As a consequence, bad parameters are detected only when |
||
1948 | * initializing PGT storage. |
||
1949 | * |
||
1950 | * @param $user the user to access the data with |
||
1951 | * @param $password the user's password |
||
1952 | * @param $database_type the type of the database hosting the data |
||
1953 | * @param $hostname the server hosting the database |
||
1954 | * @param $port the port the server is listening on |
||
1955 | * @param $database the name of the database |
||
1956 | * @param $table the name of the table storing the data |
||
1957 | * |
||
1958 | * @public |
||
1959 | */ |
||
1960 | function setPGTStorageDB( |
||
1981 | |||
1982 | // ######################################################################## |
||
1983 | // PGT VALIDATION |
||
1984 | // ######################################################################## |
||
1985 | /** |
||
1986 | * This method is used to validate a PGT; halt on failure. |
||
1987 | * |
||
1988 | * @param $validate_url the URL of the request to the CAS server. |
||
1989 | * @param $text_response the response of the CAS server, as is (XML text); result |
||
1990 | * of CASClient::validateST() or CASClient::validatePT(). |
||
1991 | * @param $tree_response the response of the CAS server, as a DOM XML tree; result |
||
1992 | * of CASClient::validateST() or CASClient::validatePT(). |
||
1993 | * |
||
1994 | * @return bool TRUE when successfull, halt otherwise by calling CASClient::authError(). |
||
1995 | * |
||
1996 | * @private |
||
1997 | */ |
||
1998 | function validatePGT(&$validate_url, $text_response, $tree_response) |
||
2028 | |||
2029 | // ######################################################################## |
||
2030 | // PGT VALIDATION |
||
2031 | // ######################################################################## |
||
2032 | |||
2033 | /** |
||
2034 | * This method is used to retrieve PT's from the CAS server thanks to a PGT. |
||
2035 | * |
||
2036 | * @param $target_service the service to ask for with the PT. |
||
2037 | * @param $err_code an error code (PHPCAS_SERVICE_OK on success). |
||
2038 | * @param $err_msg an error message (empty on success). |
||
2039 | * |
||
2040 | * @return a Proxy Ticket, or FALSE on error. |
||
2041 | * |
||
2042 | * @private |
||
2043 | */ |
||
2044 | function retrievePT($target_service, &$err_code, &$err_msg) |
||
2135 | |||
2136 | // ######################################################################## |
||
2137 | // ACCESS TO EXTERNAL SERVICES |
||
2138 | // ######################################################################## |
||
2139 | |||
2140 | /** |
||
2141 | * This method is used to acces a remote URL. |
||
2142 | * |
||
2143 | * @param $url the URL to access. |
||
2144 | * @param $cookies an array containing cookies strings such as 'name=val' |
||
2145 | * @param $headers an array containing the HTTP header lines of the response |
||
2146 | * (an empty array on failure). |
||
2147 | * @param $body the body of the response, as a string (empty on failure). |
||
2148 | * @param $err_msg an error message, filled on failure. |
||
2149 | * |
||
2150 | * @return TRUE on success, FALSE otherwise (in this later case, $err_msg |
||
2151 | * contains an error message). |
||
2152 | * |
||
2153 | * @private |
||
2154 | */ |
||
2155 | function readURL($url, $cookies, &$headers, &$body, &$err_msg) |
||
2249 | |||
2250 | /** |
||
2251 | * This method is used to build the SAML POST body sent to /samlValidate URL. |
||
2252 | * |
||
2253 | * @return the SOAP-encased SAMLP artifact (the ticket). |
||
2254 | * |
||
2255 | * @private |
||
2256 | */ |
||
2257 | function buildSAMLPayload() |
||
2270 | |||
2271 | /** |
||
2272 | * This method is the callback used by readURL method to request HTTP headers. |
||
2273 | */ |
||
2274 | var $_curl_headers = array(); |
||
2275 | |||
2276 | function _curl_read_headers($ch, $header) |
||
2281 | |||
2282 | /** |
||
2283 | * This method is used to access an HTTP[S] service. |
||
2284 | * |
||
2285 | * @param $url the service to access. |
||
2286 | * @param $err_code an error code Possible values are PHPCAS_SERVICE_OK (on |
||
2287 | * success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE, PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE, |
||
2288 | * PHPCAS_SERVICE_PT_FAILURE, PHPCAS_SERVICE_NOT AVAILABLE. |
||
2289 | * @param $output the output of the service (also used to give an error |
||
2290 | * message on failure). |
||
2291 | * |
||
2292 | * @return TRUE on success, FALSE otherwise (in this later case, $err_code |
||
2293 | * gives the reason why it failed and $output contains an error message). |
||
2294 | * |
||
2295 | * @public |
||
2296 | */ |
||
2297 | function serviceWeb($url, &$err_code, &$output) |
||
2358 | |||
2359 | /** |
||
2360 | * This method is used to access an IMAP/POP3/NNTP service. |
||
2361 | * |
||
2362 | * @param $url a string giving the URL of the service, including the mailing box |
||
2363 | * for IMAP URLs, as accepted by imap_open(). |
||
2364 | * @param $service a string giving for CAS retrieve Proxy ticket |
||
2365 | * @param $flags options given to imap_open(). |
||
2366 | * @param $err_code an error code Possible values are PHPCAS_SERVICE_OK (on |
||
2367 | * success), PHPCAS_SERVICE_PT_NO_SERVER_RESPONSE, PHPCAS_SERVICE_PT_BAD_SERVER_RESPONSE, |
||
2368 | * PHPCAS_SERVICE_PT_FAILURE, PHPCAS_SERVICE_NOT AVAILABLE. |
||
2369 | * @param $err_msg an error message on failure |
||
2370 | * @param $pt the Proxy Ticket (PT) retrieved from the CAS server to access the URL |
||
2371 | * on success, FALSE on error). |
||
2372 | * |
||
2373 | * @return an IMAP stream on success, FALSE otherwise (in this later case, $err_code |
||
2374 | * gives the reason why it failed and $err_msg contains an error message). |
||
2375 | * |
||
2376 | * @public |
||
2377 | */ |
||
2378 | function serviceMail($url, $service, $flags, &$err_code, &$err_msg, &$pt) |
||
2410 | |||
2411 | /** @} */ |
||
2412 | |||
2413 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
2414 | // XX XX |
||
2415 | // XX PROXIED CLIENT FEATURES (CAS 2.0) XX |
||
2416 | // XX XX |
||
2417 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
2418 | |||
2419 | // ######################################################################## |
||
2420 | // PT |
||
2421 | // ######################################################################## |
||
2422 | /** |
||
2423 | * @addtogroup internalProxied |
||
2424 | * @{ |
||
2425 | */ |
||
2426 | |||
2427 | /** |
||
2428 | * the Proxy Ticket provided in the URL of the request if present |
||
2429 | * (empty otherwise). Written by CASClient::CASClient(), read by |
||
2430 | * CASClient::getPT() and CASClient::hasPGT(). |
||
2431 | * |
||
2432 | * @hideinitializer |
||
2433 | * @private |
||
2434 | */ |
||
2435 | var $_pt = ''; |
||
2436 | |||
2437 | /** |
||
2438 | * This method returns the Proxy Ticket provided in the URL of the request. |
||
2439 | * @return The proxy ticket. |
||
2440 | * @private |
||
2441 | */ |
||
2442 | function getPT() |
||
2447 | |||
2448 | /** |
||
2449 | * This method stores the Proxy Ticket. |
||
2450 | * @param $pt The Proxy Ticket. |
||
2451 | * @private |
||
2452 | */ |
||
2453 | function setPT($pt) |
||
2457 | |||
2458 | /** |
||
2459 | * This method tells if a Proxy Ticket was stored. |
||
2460 | * @return TRUE if a Proxy Ticket has been stored. |
||
2461 | * @private |
||
2462 | */ |
||
2463 | function hasPT() |
||
2467 | |||
2468 | /** |
||
2469 | * This method returns the SAML Ticket provided in the URL of the request. |
||
2470 | * @return The SAML ticket. |
||
2471 | * @private |
||
2472 | */ |
||
2473 | function getSA() |
||
2477 | |||
2478 | /** |
||
2479 | * This method stores the SAML Ticket. |
||
2480 | * @param $sa The SAML Ticket. |
||
2481 | * @private |
||
2482 | */ |
||
2483 | function setSA($sa) |
||
2487 | |||
2488 | /** |
||
2489 | * This method tells if a SAML Ticket was stored. |
||
2490 | * @return TRUE if a SAML Ticket has been stored. |
||
2491 | * @private |
||
2492 | */ |
||
2493 | function hasSA() |
||
2497 | |||
2498 | /** @} */ |
||
2499 | // ######################################################################## |
||
2500 | // PT VALIDATION |
||
2501 | // ######################################################################## |
||
2502 | /** |
||
2503 | * @addtogroup internalProxied |
||
2504 | * @{ |
||
2505 | */ |
||
2506 | |||
2507 | /** |
||
2508 | * This method is used to validate a ST or PT; halt on failure |
||
2509 | * Used for all CAS 2.0 validations |
||
2510 | * @return bool TRUE when successfull, halt otherwise by calling CASClient::authError(). |
||
2511 | * |
||
2512 | * @private |
||
2513 | */ |
||
2514 | function validatePT(&$validate_url, &$text_response, &$tree_response) |
||
2596 | |||
2597 | /** @} */ |
||
2598 | |||
2599 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
2600 | // XX XX |
||
2601 | // XX MISC XX |
||
2602 | // XX XX |
||
2603 | // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
||
2604 | |||
2605 | /** |
||
2606 | * @addtogroup internalMisc |
||
2607 | * @{ |
||
2608 | */ |
||
2609 | |||
2610 | // ######################################################################## |
||
2611 | // URL |
||
2612 | // ######################################################################## |
||
2613 | /** |
||
2614 | * the URL of the current request (without any ticket CGI parameter). Written |
||
2615 | * and read by CASClient::getURL(). |
||
2616 | * |
||
2617 | * @hideinitializer |
||
2618 | * @private |
||
2619 | */ |
||
2620 | var $_url = ''; |
||
2621 | |||
2622 | /** |
||
2623 | * This method returns the URL of the current request (without any ticket |
||
2624 | * CGI parameter). |
||
2625 | * |
||
2626 | * @return The URL |
||
2627 | * |
||
2628 | * @private |
||
2629 | */ |
||
2630 | function getURL() |
||
2683 | |||
2684 | |||
2685 | /** |
||
2686 | * Removes a parameter from a query string |
||
2687 | * |
||
2688 | * @param string $parameterName |
||
2689 | * @param string $queryString |
||
2690 | * @return string |
||
2691 | * |
||
2692 | * @link http://stackoverflow.com/questions/1842681/regular-expression-to-remove-one-parameter-from-query-string |
||
2693 | */ |
||
2694 | function removeParameterFromQueryString($parameterName, $queryString) |
||
2699 | |||
2700 | |||
2701 | /** |
||
2702 | * This method sets the URL of the current request |
||
2703 | * |
||
2704 | * @param $url url to set for service |
||
2705 | * |
||
2706 | * @private |
||
2707 | */ |
||
2708 | function setURL($url) |
||
2712 | |||
2713 | // ######################################################################## |
||
2714 | // AUTHENTICATION ERROR HANDLING |
||
2715 | // ######################################################################## |
||
2716 | /** |
||
2717 | * This method is used to print the HTML output when the user was not authenticated. |
||
2718 | * |
||
2719 | * @param $failure the failure that occured |
||
2720 | * @param $cas_url the URL the CAS server was asked for |
||
2721 | * @param $no_response the response from the CAS server (other |
||
2722 | * parameters are ignored if TRUE) |
||
2723 | * @param $bad_response bad response from the CAS server ($err_code |
||
2724 | * and $err_msg ignored if TRUE) |
||
2725 | * @param $cas_response the response of the CAS server |
||
2726 | * @param $err_code the error code given by the CAS server |
||
2727 | * @param $err_msg the error message given by the CAS server |
||
2728 | * |
||
2729 | * @private |
||
2730 | */ |
||
2731 | function authError( |
||
2772 | |||
2773 | /** @} */ |
||
2774 | } |
||
2775 |