Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • cHttpRequest
  • cHttpRequestCurl
  • cHttpRequestSocket
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: /**
  4:  * This file contains an implementation of HttpRequest using fsockopen
  5:  *
  6:  * @package Core
  7:  * @subpackage Core
  8:  * @author Mischa Holz
  9:  * @copyright four for business AG <www.4fb.de>
 10:  * @license http://www.contenido.org/license/LIZENZ.txt
 11:  * @link http://www.4fb.de
 12:  * @link http://www.contenido.org
 13:  */
 14: 
 15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
 16: 
 17: /**
 18:  * fsockopen implementation of HttpRequest.
 19:  *
 20:  * @package Core
 21:  * @subpackage Core
 22:  */
 23: class cHttpRequestSocket extends cHttpRequest {
 24: 
 25:     /**
 26:      * Array for the post parameters.
 27:      *
 28:      * @var array
 29:      */
 30:     protected $postArray;
 31: 
 32:     /**
 33:      * Array for the get parameters.
 34:      *
 35:      * @var array
 36:      */
 37:     protected $getArray;
 38: 
 39:     /**
 40:      * Array for the HTTP-headers.
 41:      *
 42:      * @var array
 43:      */
 44:     protected $headerArray;
 45: 
 46:     /**
 47:      * Request URL.
 48:      *
 49:      * @var string
 50:      */
 51:     protected $url;
 52: 
 53:     /**
 54:      * Boundary for the multipart from-data.
 55:      *
 56:      * @var string
 57:      */
 58:     protected $boundary;
 59: 
 60:     /**
 61:      * The HTTP header.
 62:      *
 63:      * @var string
 64:      */
 65:     protected $header;
 66: 
 67:     /**
 68:      * The HTTP body.
 69:      *
 70:      * @var string
 71:      */
 72:     protected $body;
 73: 
 74:     /**
 75:      * Constructor to create an instance of this class.
 76:      *
 77:      * @see cHttpRequest::__construct()
 78:      * @see cHttpRequest::getHttpRequest()
 79:      * @param string $url [optional]
 80:      *         URL for the request
 81:      */
 82:     public function __construct($url = '') {
 83:         $this->url = $url;
 84:     }
 85: 
 86:     /**
 87:      * Set the request URL.
 88:      *
 89:      * @see cHttpRequest::setURL()
 90:      * @param string $url
 91:      *         the URL
 92:      * @return cHttpRequest
 93:      */
 94:     public function setURL($url) {
 95:         $this->url = $url;
 96: 
 97:         return $this;
 98:     }
 99: 
100:     /**
101:      * Set the GET parameters.
102:      *
103:      * @see cHttpRequest::setGetParams()
104:      * @param array $array
105:      *         associative array containing keys and values of the GET parameters
106:      * @return cHttpRequest
107:      */
108:     public function setGetParams($array) {
109:         $this->getArray = $array;
110: 
111:         return $this;
112:     }
113: 
114:     /**
115:      * Set the POST parameters.
116:      *
117:      * @see cHttpRequest::setPostParams()
118:      * @param array $array
119:      *         associative array containing keys and values of the POST parameters
120:      * @return cHttpRequest
121:      */
122:     public function setPostParams($array) {
123:         $this->postArray = $array;
124: 
125:         return $this;
126:     }
127: 
128:     /**
129:      * Set the HTTP headers.
130:      *
131:      * @see cHttpRequest::setHeaders()
132:      * @param array $array
133:      *         associative array containing the HTTP headers
134:      * @return cHttpRequest
135:      */
136:     public function setHeaders($array) {
137:         $this->headerArray = $array;
138: 
139:         return $this;
140:     }
141: 
142:     /**
143:      * Inserts the custom headers into the header string.
144:      */
145:     protected function prepareHeaders() {
146:         $this->header = '';
147:         if (!is_array($this->headerArray)) {
148:             return;
149:         }
150:         foreach ($this->headerArray as $key => $value) {
151:             $headerString = '';
152:             if (is_array($value)) {
153:                 $headerString .= $value[0] . ': ' . $value[1];
154:             } else {
155:                 $headerString .= $key . ': ' . $value;
156:             }
157:             $this->header .= $headerString . "\r\n";
158:         }
159:     }
160: 
161:     /**
162:      * Appends teh GET array to the URL.
163:      */
164:     protected function prepareGetRequest() {
165:         if (is_array($this->getArray)) {
166:             if (!cString::contains($this->url, '?')) {
167:                 $this->url .= '?';
168:             } else {
169:                 $this->url .= '&';
170:             }
171:             foreach ($this->getArray as $key => $value) {
172:                 $this->url .= urlencode($key) . '=' . urlencode($value) . '&';
173:             }
174:             $this->url = substr($this->url, 0, strlen($this->url) - 1);
175:         }
176:     }
177: 
178:     /**
179:      * Prepares the headers to send a POST request and encodes the data.
180:      */
181:     protected function preparePostRequest() {
182:         $this->boundary = md5(time()) . md5(time() * rand());
183:         $this->headerArray['Content-Type'] = 'multipart/form-data; boundary=' . $this->boundary;
184:         $this->boundary = '--' . $this->boundary;
185: 
186:         $this->body = $this->boundary . "\r\n";
187:         foreach ($this->postArray as $key => $value) {
188:             $this->body .= 'Content-Disposition: form-data; name="' . $key . "\"\r\n\r\n";
189:             $this->body .= $value . "\r\n";
190:             $this->body .= $this->boundary . "\r\n";
191:         }
192:         $this->headerArray['Content-Length'] = strlen($this->body);
193:     }
194: 
195:     /**
196:      * Send the request to the server.
197:      *
198:      * @param bool $return
199:      *         Wether the function should return the servers response
200:      * @param string $method
201:      *         GET or PUT
202:      * @param bool $returnHeaders [optional]
203:      *         Wether the headers should be included in the response
204:      * @return string|bool
205:      */
206:     protected function sendRequest($return, $method, $returnHeaders = false) {
207:         if (!(strpos($this->url, 'http') === 0)) {
208:             $this->url = 'http://' . $this->url;
209:         }
210: 
211:         $urlInfo = @parse_url($this->url);
212:         $scheme = '';
213:         if ($urlInfo['port'] == '') {
214:             if ($urlInfo['scheme'] == 'https') {
215:                 $urlInfo['port'] = 443;
216:                 $scheme = 'ssl://';
217:             } else {
218:                 $urlInfo['port'] = 80;
219:             }
220:         }
221: 
222:         $this->headerArray['Host'] = ($this->headerArray['Host'] != '') ? $this->headerArray['Host'] : $urlInfo['host'];
223:         $this->headerArray['Connection'] = ($this->headerArray['Connection'] != '') ? $this->headerArray['Host'] : 'close';
224:         $this->headerArray['Accept'] = ($this->headerArray['Accept'] != '') ? $this->headerArray['Host'] : '*/*';
225: 
226:         $this->prepareHeaders();
227: 
228:         $handle = @fsockopen($scheme . $urlInfo['host'], $urlInfo['port']);
229:         if (!$handle) {
230:             return false;
231:         }
232: 
233:         $request = $method . ' ';
234:         $request .= $urlInfo['path'] . '?' . $urlInfo['query'] . ' HTTP/1.1' . "\r\n";
235:         $request .= $this->header . "\r\n";
236:         $request .= $this->body;
237: 
238:         fwrite($handle, $request);
239: 
240:         $ret = '';
241:         while (!feof($handle)) {
242:             $ret .= fgets($handle);
243:         }
244: 
245:         fclose($handle);
246: 
247:         if ($return) {
248:             if (!$returnHeaders) {
249:                 $ret = substr(cString::strstr($ret, "\r\n\r\n"), strlen("\r\n\r\n"));
250:             }
251:             return $ret;
252:         } else {
253:             return strpos(cString::strstr($ret, '\r\n', true), '200') !== false;
254:         }
255:     }
256: 
257:     /**
258:      * Perform the request using POST.
259:      *
260:      * @see cHttpRequest::postRequest()
261:      * @param bool $return [optional]
262:      *         If true, response of the server gets returned as string
263:      * @param bool $returnHeaders [optional]
264:      *         If true, headers will be included in the response
265:      * @return string|bool
266:      *         False on error, response otherwise
267:      */
268:     public function postRequest($return = true, $returnHeaders = false) {
269:         $this->preparePostRequest();
270: 
271:         return $this->sendRequest($return, 'POST', $returnHeaders);
272:     }
273: 
274:     /**
275:      * Perform the request using GET.
276:      *
277:      * @see cHttpRequest::getRequest()
278:      * @param bool $return [optional]
279:      *         If true, response of the server gets returned as string
280:      * @param bool $returnHeaders [optional]
281:      *         If true, headers will be included in the response
282:      * @return string|bool
283:      *         False on error, response otherwise
284:      */
285:     public function getRequest($return = true, $returnHeaders = false) {
286:         $this->prepareGetRequest();
287: 
288:         return $this->sendRequest($return, 'GET', $returnHeaders);
289:     }
290: 
291:     /**
292:      * Perform the request using POST AND append all GET parameters.
293:      *
294:      * @see cHttpRequest::request()
295:      * @param bool $return [optional]
296:      *         If true, response of the server gets returned as string
297:      * @param bool $returnHeaders [optional]
298:      *         If true, headers will be included in the response
299:      * @return string|bool
300:      *         False on error, response otherwise
301:      */
302:     public function request($return = true, $returnHeaders = false) {
303:         $this->prepareGetRequest();
304:         $this->preparePostRequest();
305: 
306:         return $this->sendRequest($return, 'POST', $returnHeaders);
307:     }
308: }
309: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0