Overview

Packages

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