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
52 *          // (shouldn't happen) or CAS server failure (unlikely) so lets just
53 *          // stop if we hit those.
54 *			throw $e;
55 *	} catch (CAS_ProxiedService_Exception $e) {
56 *		// Something prevented the service request from being sent or received.
57 *		// We didn't even get a valid error response (404, 500, etc), so this
58 *		// might be caused by a network error or a DNS resolution failure.
59 *		// We could handle it in some way, but for now we will just stop.
60 *		throw $e;
61 *	}
62 *
63 * @class    CAS_ProxiedService_Http_Post
64 * @category Authentication
65 * @package  PhpCAS
66 * @author   Adam Franco <afranco@middlebury.edu>
67 * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
68 * @link     https://wiki.jasig.org/display/CASC/phpCAS
69 */
70class CAS_ProxiedService_Http_Post
71extends CAS_ProxiedService_Http_Abstract
72{
73
74    /**
75     * The content-type of this request
76     *
77     * @var string $_contentType
78     */
79    private $_contentType;
80
81    /**
82     * The body of the this request
83     *
84     * @var string $_body
85     */
86    private $_body;
87
88    /**
89     * Set the content type of this POST request.
90     *
91     * @param string $contentType content type
92     *
93     * @return void
94     * @throws CAS_OutOfSequenceException If called after the Request has been sent.
95     */
96    public function setContentType ($contentType)
97    {
98        if ($this->hasBeenSent()) {
99            throw new CAS_OutOfSequenceException(
100                'Cannot set the content type, request already sent.'
101            );
102        }
103
104        $this->_contentType = $contentType;
105    }
106
107    /**
108     * Set the body of this POST request.
109     *
110     * @param string $body body to set
111     *
112     * @return void
113     * @throws CAS_OutOfSequenceException If called after the Request has been sent.
114     */
115    public function setBody ($body)
116    {
117        if ($this->hasBeenSent()) {
118            throw new CAS_OutOfSequenceException(
119                'Cannot set the body, request already sent.'
120            );
121        }
122
123        $this->_body = $body;
124    }
125
126    /**
127     * Add any other parts of the request needed by concrete classes
128     *
129     * @param CAS_Request_RequestInterface $request request interface class
130     *
131     * @return void
132     */
133    protected function populateRequest (CAS_Request_RequestInterface $request)
134    {
135        if (empty($this->_contentType) && !empty($this->_body)) {
136            throw new CAS_ProxiedService_Exception(
137                "If you pass a POST body, you must specify a content type via "
138                .get_class($this).'->setContentType($contentType).'
139            );
140        }
141
142        $request->makePost();
143        if (!empty($this->_body)) {
144            $request->addHeader('Content-Type: '.$this->_contentType);
145            $request->addHeader('Content-Length: '.strlen($this->_body));
146            $request->setPostBody($this->_body);
147        }
148    }
149
150
151}
152?>
153