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