1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CAS response class. |
4
|
|
|
* |
5
|
|
|
* @package \WPCASServerPlugin\Server |
6
|
|
|
* @version 1.2.0 |
7
|
|
|
* @since 1.2.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Cassava\CAS\Response; |
11
|
|
|
|
12
|
|
|
use Cassava\CAS; |
13
|
|
|
use Cassava\Exception\GeneralException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Implements the base CAS response. |
17
|
|
|
* |
18
|
|
|
* @version 1.2.0 |
19
|
|
|
*/ |
20
|
|
|
class BaseResponse { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* CAS XML Namespace URI |
24
|
|
|
*/ |
25
|
|
|
const CAS_NS = 'http://www.yale.edu/tp/cas'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* XML response document. |
29
|
|
|
* @var \DOMDocument |
30
|
|
|
*/ |
31
|
|
|
protected $document; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* XML response node. |
35
|
|
|
* @var \DOMNode |
36
|
|
|
*/ |
37
|
|
|
protected $response; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Response constructor. |
41
|
|
|
* |
42
|
|
|
* @uses \get_bloginfo() |
43
|
|
|
*/ |
44
|
|
|
public function __construct() { |
45
|
|
|
$this->document = new \DOMDocument( '1.0', \get_bloginfo( 'charset' ) ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Wrap a CAS 2.0 XML response and output it as a string. |
50
|
|
|
* |
51
|
|
|
* @return string CAS 2.0+ server response as an XML string. |
52
|
|
|
*/ |
53
|
|
|
public function prepare() { |
54
|
|
|
$root = $this->createElement( 'serviceResponse' ); |
55
|
|
|
|
56
|
|
|
if ( ! empty( $this->response ) ) { |
57
|
|
|
$root->appendChild( $this->response ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Removing all child nodes from response document: |
61
|
|
|
while ( $this->document->firstChild ) { |
62
|
|
|
$this->document->removeChild( $this->document->firstChild ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->document->appendChild( $root ); |
66
|
|
|
|
67
|
|
|
return $this->document->saveXML(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set error response. |
72
|
|
|
* |
73
|
|
|
* @param \WP_Error|null $error Response error. |
74
|
|
|
* @param string $tag Response XML tag (defaults to `authenticationFailure`). |
75
|
|
|
* |
76
|
|
|
* @uses \WP_Error |
77
|
|
|
* @uses \do_action() |
78
|
|
|
*/ |
79
|
|
|
public function setError( \WP_Error $error = null, $tag = 'authenticationFailure' ) { |
80
|
|
|
/** |
81
|
|
|
* Fires if the CAS server has to return an XML error. |
82
|
|
|
* |
83
|
|
|
* @param WP_Error $error WordPress error to return as XML. |
84
|
|
|
*/ |
85
|
|
|
\do_action( 'cas_server_error', $error ); |
86
|
|
|
|
87
|
|
|
$message = __( 'Unknown error', 'wp-cas-server' ); |
88
|
|
|
$code = GeneralException::ERROR_INTERNAL_ERROR; |
89
|
|
|
|
90
|
|
|
if ( ! empty( $error ) ) { |
91
|
|
|
$code = $error->get_error_code(); |
92
|
|
|
$message = $error->get_error_message( $code ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->response = $this->createElement( $tag, $message ); |
96
|
|
|
$this->response->setAttribute( 'code', $code ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create response element. |
101
|
|
|
* |
102
|
|
|
* @param string $element Unqualified element tag name. |
103
|
|
|
* @param string|null $value Optional element value. |
104
|
|
|
* @return \DOMNode XML element. |
105
|
|
|
*/ |
106
|
|
|
protected function createElement( $element, $value = null ) { |
107
|
|
|
return $this->document->createElementNS( static::CAS_NS, "cas:$element", $value ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|