Upgrade phpCAS
[piwik-CASLogin.git] / CAS / CAS / ProxiedService / Http / Post.php
1 <?php
2
3 /**
4  * Licensed to Jasig under one or more contributor license
5  * agreements. See the NOTICE file distributed with this work for
6  * additional information regarding copyright ownership.
7  *
8  * Jasig licenses this file to you under the Apache License,
9  * Version 2.0 (the "License"); you may not use this file except in
10  * compliance with the License. You may obtain a copy of the License at:
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * PHP Version 5
21  *
22  * @file     CAS/ProxiedService/Http/Post.php
23  * @category Authentication
24  * @package  PhpCAS
25  * @author   Adam Franco <afranco@middlebury.edu>
26  * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
27  * @link     https://wiki.jasig.org/display/CASC/phpCAS
28  */
29
30 /**
31  * This class is used to make proxied service requests via the HTTP POST method.
32  *
33  * Usage Example:
34  *
35  *                      try {
36  *                              $service = phpCAS::getProxiedService(PHPCAS_PROXIED_SERVICE_HTTP_POST);
37  *                              $service->setUrl('http://www.example.com/path/');
38  *                              $service->setContentType('text/xml');
39  *                              $service->setBody(''<?xml version="1.0"?'.'><methodCall><methodName>example.search</methodName></methodCall>');
40  *                              $service->send();
41  *                              if ($service->getResponseStatusCode() == 200)
42  *                                      return $service->getResponseBody();
43  *                              else
44  *                                      // The service responded with an error code 404, 500, etc.
45  *                                      throw new Exception('The service responded with an error.');
46  *
47  *                      } catch (CAS_ProxyTicketException $e) {
48  *                              if ($e->getCode() == PHPCAS_SERVICE_PT_FAILURE)
49  *                                      return "Your login has timed out. You need to log in again.";
50  *                              else
51  *                                      // Other proxy ticket errors are from bad request format (shouldn't happen)
52  *                                      // or CAS server failure (unlikely) so lets just stop if we hit those.
53  *                                      throw $e;
54  *                      } catch (CAS_ProxiedService_Exception $e) {
55  *                              // Something prevented the service request from being sent or received.
56  *                              // We didn't even get a valid error response (404, 500, etc), so this
57  *                              // might be caused by a network error or a DNS resolution failure.
58  *                              // We could handle it in some way, but for now we will just stop.
59  *                              throw $e;
60  *                      }
61  *
62  * @class    CAS_ProxiedService_Http_Post
63  * @category Authentication
64  * @package  PhpCAS
65  * @author   Adam Franco <afranco@middlebury.edu>
66  * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
67  * @link     https://wiki.jasig.org/display/CASC/phpCAS
68  */
69 class CAS_ProxiedService_Http_Post
70 extends CAS_ProxiedService_Http_Abstract
71 {
72
73     /**
74      * The content-type of this request
75      *
76      * @var string $_contentType
77      */
78     private $_contentType;
79
80     /**
81      * The body of the this request
82      *
83      * @var string $_body
84      */
85     private $_body;
86
87     /**
88      * Set the content type of this POST request.
89      *
90      * @param string $contentType content type
91      *
92      * @return void
93      * @throws CAS_OutOfSequenceException If called after the Request has been sent.
94      */
95     public function setContentType ($contentType)
96     {
97         if ($this->hasBeenSent()) {
98             throw new CAS_OutOfSequenceException('Cannot set the content type, request already sent.');
99         }
100
101         $this->_contentType = $contentType;
102     }
103
104     /**
105      * Set the body of this POST request.
106      *
107      * @param string $body body to set
108      *
109      * @return void
110      * @throws CAS_OutOfSequenceException If called after the Request has been sent.
111      */
112     public function setBody ($body)
113     {
114         if ($this->hasBeenSent()) {
115             throw new CAS_OutOfSequenceException('Cannot set the body, request already sent.');
116         }
117
118         $this->_body = $body;
119     }
120
121     /**
122      * Add any other parts of the request needed by concrete classes
123      *
124      * @param CAS_Request_RequestInterface $request request interface class
125      *
126      * @return void
127      */
128     protected function populateRequest (CAS_Request_RequestInterface $request)
129     {
130         if (empty($this->_contentType) && !empty($this->_body)) {
131             throw new CAS_ProxiedService_Exception("If you pass a POST body, you must specify a content type via ".get_class($this).'->setContentType($contentType).');
132         }
133
134         $request->makePost();
135         if (!empty($this->_body)) {
136             $request->addHeader('Content-Type: '.$this->_contentType);
137             $request->addHeader('Content-Length: '.strlen($this->_body));
138             $request->setPostBody($this->_body);
139         }
140     }
141
142
143 }
144 ?>