~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to auth/cas/CAS/CAS/Request/CurlRequest.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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/Request/CurlRequest.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
 * Provides support for performing web-requests via curl
 
32
 *
 
33
 * @class    CAS_Request_CurlRequest
 
34
 * @category Authentication
 
35
 * @package  PhpCAS
 
36
 * @author   Adam Franco <afranco@middlebury.edu>
 
37
 * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 
38
 * @link     https://wiki.jasig.org/display/CASC/phpCAS
 
39
 */
 
40
class CAS_Request_CurlRequest
 
41
extends CAS_Request_AbstractRequest
 
42
implements CAS_Request_RequestInterface
 
43
{
 
44
 
 
45
    /**
 
46
     * Set additional curl options
 
47
     *
 
48
     * @param array $options option to set
 
49
     *
 
50
     * @return void
 
51
     */
 
52
    public function setCurlOptions (array $options)
 
53
    {
 
54
        $this->_curlOptions = $options;
 
55
    }
 
56
    private $_curlOptions = array();
 
57
 
 
58
    /**
 
59
     * Send the request and store the results.
 
60
     *
 
61
     * @return bool true on success, false on failure.
 
62
     */
 
63
    protected function sendRequest ()
 
64
    {
 
65
        phpCAS::traceBegin();
 
66
 
 
67
        /*********************************************************
 
68
         * initialize the CURL session
 
69
        *********************************************************/
 
70
        $ch = $this->_initAndConfigure();
 
71
 
 
72
        /*********************************************************
 
73
         * Perform the query
 
74
        *********************************************************/
 
75
        $buf = curl_exec($ch);
 
76
        if ( $buf === false ) {
 
77
            phpCAS::trace('curl_exec() failed');
 
78
            $this->storeErrorMessage('CURL error #'.curl_errno($ch).': '.curl_error($ch));
 
79
            $res = false;
 
80
        } else {
 
81
            $this->storeResponseBody($buf);
 
82
            phpCAS::trace("Response Body: \n".$buf."\n");
 
83
            $res = true;
 
84
 
 
85
        }
 
86
        // close the CURL session
 
87
        curl_close($ch);
 
88
 
 
89
        phpCAS::traceEnd($res);
 
90
        return $res;
 
91
    }
 
92
 
 
93
    /**
 
94
     * Internal method to initialize our cURL handle and configure the request.
 
95
     * This method should NOT be used outside of the CurlRequest or the
 
96
     * CurlMultiRequest.
 
97
     *
 
98
     * @return resource The cURL handle on success, false on failure
 
99
     */
 
100
    private function _initAndConfigure()
 
101
    {
 
102
        /*********************************************************
 
103
         * initialize the CURL session
 
104
        *********************************************************/
 
105
        $ch = curl_init($this->url);
 
106
 
 
107
        if (version_compare(PHP_VERSION, '5.1.3', '>=')) {
 
108
            //only avaible in php5
 
109
            curl_setopt_array($ch, $this->_curlOptions);
 
110
        } else {
 
111
            foreach ($this->_curlOptions as $key => $value) {
 
112
                curl_setopt($ch, $key, $value);
 
113
            }
 
114
        }
 
115
 
 
116
        /*********************************************************
 
117
         * Set SSL configuration
 
118
        *********************************************************/
 
119
        if ($this->caCertPath) {
 
120
            if ($this->validateCN) {
 
121
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
 
122
            } else {
 
123
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
 
124
            }
 
125
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
 
126
            curl_setopt($ch, CURLOPT_CAINFO, $this->caCertPath);
 
127
            phpCAS::trace('CURL: Set CURLOPT_CAINFO ' . $this->caCertPath);
 
128
        } else {
 
129
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
 
130
        }
 
131
 
 
132
        /*********************************************************
 
133
         * Configure curl to capture our output.
 
134
        *********************************************************/
 
135
        // return the CURL output into a variable
 
136
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
137
 
 
138
        // get the HTTP header with a callback
 
139
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, '_curlReadHeaders'));
 
140
 
 
141
        /*********************************************************
 
142
         * Add cookie headers to our request.
 
143
        *********************************************************/
 
144
        if (count($this->cookies)) {
 
145
            $cookieStrings = array();
 
146
            foreach ($this->cookies as $name => $val) {
 
147
                $cookieStrings[] = $name.'='.$val;
 
148
            }
 
149
            curl_setopt($ch, CURLOPT_COOKIE, implode(';', $cookieStrings));
 
150
        }
 
151
 
 
152
        /*********************************************************
 
153
         * Add any additional headers
 
154
        *********************************************************/
 
155
        if (count($this->headers)) {
 
156
            curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
 
157
        }
 
158
 
 
159
        /*********************************************************
 
160
         * Flag and Body for POST requests
 
161
        *********************************************************/
 
162
        if ($this->isPost) {
 
163
            curl_setopt($ch, CURLOPT_POST, 1);
 
164
            curl_setopt($ch, CURLOPT_POSTFIELDS, $this->postBody);
 
165
        }
 
166
 
 
167
        return $ch;
 
168
    }
 
169
 
 
170
    /**
 
171
     * Store the response body.
 
172
     * This method should NOT be used outside of the CurlRequest or the
 
173
     * CurlMultiRequest.
 
174
     *
 
175
     * @param string $body body to stor
 
176
     *
 
177
     * @return void
 
178
     */
 
179
    private function _storeResponseBody ($body)
 
180
    {
 
181
        $this->storeResponseBody($body);
 
182
    }
 
183
 
 
184
    /**
 
185
     * Internal method for capturing the headers from a curl request.
 
186
     *
 
187
     * @param handle $ch     handle of curl
 
188
     * @param string $header header
 
189
     *
 
190
     * @return void
 
191
     */
 
192
    private function _curlReadHeaders ($ch, $header)
 
193
    {
 
194
        $this->storeResponseHeader($header);
 
195
        return strlen($header);
 
196
    }
 
197
}