Overview

Namespaces

  • MOC
    • Adapter
    • Core
      • Changelog
      • Depending
      • Drive
        • Directory
        • File
      • Error
        • Register
        • Type
      • Journal
      • Proxy
      • Template
      • Xml
    • Extension
      • Excel
      • Flot
      • Mail
      • Pdf
      • Word
      • Xml
      • YUICompressor
      • Zip
    • Generic
      • Common
      • Device
        • Extension
        • Widget
    • Module
      • Database
        • Driver
      • Drive
      • Image
        • Font
      • Installer
      • Network
        • Ftp
          • Directory
          • File
          • Transport
        • Http
        • ParcelTracker
          • Carrier
      • Office
        • Chart
          • Axis
        • Document
          • Excel
            • Cell
              • Format
              • Style
                • Border
                  • Bottom
                  • Left
                  • Right
                  • Top
                • Font
            • Close
            • Page
            • Worksheet
          • Pdf
            • Close
            • Font
            • Page
              • Margin
              • Position
            • Text
          • Xml
            • Close
        • Image
        • Mail
          • Address
          • Content
      • Packer
        • Yui
        • Zip
      • Template
    • Plugin
      • Gateway
      • Repository
      • Shared
  • PHP

Classes

  • Get
  • Post
  • Uri
  • UriQuery
  • UriScheme
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  1: <?php
  2: /**
  3:  * LICENSE (BSD)
  4:  *
  5:  * Copyright (c) 2013, Gerd Christian Kunze
  6:  * All rights reserved.
  7:  *
  8:  * Redistribution and use in source and binary forms, with or without
  9:  * modification, are permitted provided that the following conditions are
 10:  * met:
 11:  *
 12:  *  * Redistributions of source code must retain the above copyright
 13:  *    notice, this list of conditions and the following disclaimer.
 14:  *
 15:  *  * Redistributions in binary form must reproduce the above copyright
 16:  *    notice, this list of conditions and the following disclaimer in the
 17:  *    documentation and/or other materials provided with the distribution.
 18:  *
 19:  *  * Neither the name of Gerd Christian Kunze nor the names of the
 20:  *    contributors may be used to endorse or promote products derived from
 21:  *    this software without specific prior written permission.
 22:  *
 23:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 24:  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 25:  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 26:  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 27:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 28:  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 29:  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 30:  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 31:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 32:  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 33:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 34:  *
 35:  * Post
 36:  * 18.02.2013 20:56
 37:  */
 38: namespace MOC\Module\Network\Http;
 39: use MOC\Api;
 40: use MOC\Generic\Device\Module;
 41: 
 42: /**
 43:  *
 44:  */
 45: class Post implements Module {
 46:     /**
 47:      * Get Changelog
 48:      *
 49:      * @static
 50:      * @return \MOC\Core\Changelog
 51:      */
 52:     public static function InterfaceChangelog() {
 53:         return Api::Core()->Changelog();
 54:     }
 55: 
 56:     /**
 57:      * Get Dependencies
 58:      *
 59:      * @static
 60:      * @return \MOC\Core\Depending
 61:      */
 62:     public static function InterfaceDepending() {
 63:         return Api::Core()->Depending();
 64:     }
 65: 
 66:     /**
 67:      * Get Singleton/Instance
 68:      *
 69:      * @static
 70:      * @return Post
 71:      */
 72:     public static function InterfaceInstance() {
 73:         return new Post();
 74:     }
 75: 
 76:     /**
 77:      * @param string $Url
 78:      * @param array $Data
 79:      *
 80:      * @return bool|string
 81:      */
 82:     public function Request( $Url, $Data = array() ) {
 83: 
 84:         $Uri = Uri::InterfaceInstance()->Load( $Url );
 85: 
 86:         foreach( (array)$Data as $Key => $Value ) {
 87:             $Uri->UriQuery()->Set( $Key, $Value );
 88:         }
 89: 
 90:         $Header =  "POST ".$Uri->GetPath()." HTTP/1.1\r\n";
 91:         $Header .= "Host: ".$Uri->GetHost()."\r\n";
 92:         $Header .= "Content-Type: ".'application/x-www-form-urlencoded'."\r\n";
 93:         $Header .= "Content-Length: ".strlen( $Uri->UriQuery()->GetQueryString() )."\r\n";
 94:         $Header .= "Connection: close"."\r\n\r\n";
 95: 
 96:         $Body = $Uri->UriQuery()->GetQueryString();
 97: 
 98: 
 99:         $ErrorId = 0;
100:         $ErrorMessage = '';
101:         $Buffer = '';
102: 
103:         Api::Core()->Error()->Reporting()->Display(false)->Debug(false)->Apply();
104: 
105:         if( ( $Socket = fsockopen( $Uri->GetHost(), $Uri->GetPort(), $ErrorId, $ErrorMessage, 15 ) ) ) {
106:             fwrite( $Socket, $Header.$Body );
107:             while( !feof( $Socket ) ) {
108:                 stream_set_timeout( $Socket, 15 );
109:                 if( false === ( $Stream = fread( $Socket, 128 ) ) ) {
110:                     $Buffer = '';
111:                     break;
112:                 } else {
113:                     $Buffer .= $Stream;
114:                 }
115:             }
116:             fclose( $Socket );
117:         }
118: 
119: 
120:         $Buffer = explode( "\r\n\r\n", $Buffer );
121:         $ResponseHeader = explode( "\r\n", array_shift( $Buffer ) );
122:         $ResponseBody = implode( "\r\n\r\n", $Buffer );
123:         if( in_array( 'Transfer-Encoding: chunked', $ResponseHeader ) ) {
124:             $ResponseBody = $this->http_chunked_decode( $ResponseBody );
125:         }
126:         return $ResponseBody;
127:     }
128: 
129:     /**
130:      * @param string $Encoded
131:      *
132:      * @return bool|string
133:      */
134:     private function http_chunked_decode( $Encoded ) {
135:         if( function_exists( 'http_chunked_decode' ) ) {
136:             return http_chunked_decode( $Encoded );
137:         } else {
138:             $Position = 0;
139:             $Length = strlen( $Encoded );
140:             $Decoded = null;
141: 
142:             while(
143:                 ( $Position < $Length )
144:                 && ( $ChunkLength = substr(
145:                     $Encoded, $Position,
146:                         ( $PositionNewLine = strpos( $Encoded, "\n", $Position +1 ) )
147:                         - $Position )
148:                 )
149:             ) {
150:                 $Position = $PositionNewLine + 1;
151:                 $ChunkWidth = hexdec( rtrim( $ChunkLength, "\r\n" ) );
152:                 $Decoded .= substr( $Encoded, $Position, $ChunkWidth );
153:                 $Position = strpos( $Encoded, "\n", $Position + $ChunkWidth ) +1;
154:             }
155:             return $Decoded;
156:         }
157:     }
158: }
159: 
160: /**
161:  *
162:  */
163: class UriQuery implements Module {
164:     /**
165:      * Get Changelog
166:      *
167:      * @static
168:      * @return \MOC\Core\Changelog
169:      */
170:     public static function InterfaceChangelog() {
171:         return Api::Core()->Changelog()->Create( __CLASS__ );
172:     }
173: 
174:     /**
175:      * Get Dependencies
176:      *
177:      * @static
178:      * @return \MOC\Core\Depending
179:      */
180:     public static function InterfaceDepending() {
181:         return Api::Core()->Depending();
182:     }
183: 
184:     /**
185:      * Get Singleton/Instance
186:      *
187:      * @static
188:      * @return UriQuery
189:      */
190:     public static function InterfaceInstance() {
191:         return new UriQuery();
192:     }
193: 
194:     private $QueryContent = array();
195: 
196:     public function Load( $QueryString ) {
197:         $QueryPartList = explode( '&', $QueryString );
198:         foreach( (array)$QueryPartList as $QueryPart ) {
199:             $Key = substr( $QueryPart, 0, strpos( $QueryPart, '=' ) );
200:             $Value = substr( $QueryPart, strpos( $QueryPart, '=' ) +1 );
201:             $this->QueryContent[$Key] = $Value;
202:         }
203:         return $this;
204:     }
205: 
206:     public function Get( $Key ) {
207:         return $this->QueryContent[$Key];
208:     }
209: 
210:     public function Set( $Key, $Value ) {
211:         $this->QueryContent[$Key] = $Value;
212:         return $this;
213:     }
214: 
215:     public function GetQueryString() {
216:         $QueryString = '';
217:         foreach( (array)$this->QueryContent as $Key => $Value ) {
218:             $QueryString .= '&'.$Key.'='.$Value;
219:         }
220:         return substr( $QueryString, 1 );
221:     }
222: 
223:     /**
224:      * @return string
225:      */
226:     function __toString() {
227:         return $this->GetQueryString();
228:     }
229: }
230: 
231: /**
232:  *
233:  */
234: class UriScheme implements Module {
235:     /**
236:      * Get Changelog
237:      *
238:      * @static
239:      * @return \MOC\Core\Changelog
240:      */
241:     public static function InterfaceChangelog() {
242:         return Api::Core()->Changelog()->Create( __CLASS__ );
243:     }
244: 
245:     /**
246:      * Get Dependencies
247:      *
248:      * @static
249:      * @return \MOC\Core\Depending
250:      */
251:     public static function InterfaceDepending() {
252:         return Api::Core()->Depending();
253:     }
254: 
255:     /**
256:      * Get Singleton/Instance
257:      *
258:      * @static
259:      * @return UriScheme
260:      */
261:     public static function InterfaceInstance() {
262:         return new UriScheme();
263:     }
264: 
265:     private $UriScheme = '';
266: 
267:     public function Load( $UriScheme ) {
268:         $this->UriScheme = $UriScheme;
269:         return $this;
270:     }
271: 
272:     public function Http() {
273:         $this->UriScheme = 'http';
274:         return $this;
275:     }
276:     public function Https() {
277:         $this->UriScheme = 'https';
278:         return $this;
279:     }
280:     public function Ftp() {
281:         $this->UriScheme = 'ftp';
282:         return $this;
283:     }
284: 
285:     public function GetSchemeString() {
286:         return $this->UriScheme;
287:     }
288: 
289:     /**
290:      * @return string
291:      */
292:     function __toString() {
293:         return $this->GetSchemeString();
294:     }
295: }
296: 
297: /**
298:  *
299:  */
300: class Uri implements Module {
301: 
302:     /**
303:      * Get Changelog
304:      *
305:      * @static
306:      * @return \MOC\Core\Changelog
307:      */
308:     public static function InterfaceChangelog() {
309:         return Api::Core()->Changelog()->Create( __CLASS__ );
310:     }
311: 
312:     /**
313:      * Get Dependencies
314:      *
315:      * @static
316:      * @return \MOC\Core\Depending
317:      */
318:     public static function InterfaceDepending() {
319:         return Api::Core()->Depending();
320:     }
321: 
322:     /**
323:      * Get Singleton/Instance
324:      *
325:      * @static
326:      * @return Uri
327:      */
328:     public static function InterfaceInstance() {
329:         $Instance = new Uri();
330:         $Instance->Scheme = UriScheme::InterfaceInstance()->Http();
331:         $Instance->Query = UriQuery::InterfaceInstance();
332:         return $Instance;
333:     }
334: 
335:     /** @var UriScheme $Scheme */
336:     private $Scheme = null;
337:     private $Host = 'localhost';
338:     private $Port = 80;
339:     private $Path = '/';
340:     /** @var UriQuery $Query */
341:     private $Query = null;
342:     private $Fragment = '';
343: 
344:     /**
345:      * @param string $UriString Example: "foo://example.com:8042/over/there?name=ferret#nose"
346:      *
347:      * @return \MOC\Module\Network\Http\Uri
348:      */
349:     public function Load( $UriString ) {
350:         $UriComponentList = parse_url( $UriString );
351:         // Read components
352:         if( isset( $UriComponentList['scheme'] ) ) { $this->Scheme = UriScheme::InterfaceInstance()->Load( $UriComponentList['scheme'] ); }
353:         if( isset( $UriComponentList['host'] ) ) { $this->Host = $UriComponentList['host']; }
354:         if( isset( $UriComponentList['port'] ) ) { $this->Port = $UriComponentList['port']; }
355:         if( isset( $UriComponentList['path'] ) ) { $this->Path = $UriComponentList['path']; }
356:         if( isset( $UriComponentList['query'] ) ) { $this->Query = UriQuery::InterfaceInstance()->Load( $UriComponentList['query'] ); }
357:         if( isset( $UriComponentList['fragment'] ) ) { $this->Fragment = $UriComponentList['fragment']; }
358: 
359:         return $this;
360:     }
361: 
362:     public function UriScheme() {
363:         return  $this->Scheme;
364:     }
365:     public function UriQuery() {
366:         return  $this->Query;
367:     }
368: 
369:     public function GetPath() {
370:         return $this->Path;
371:     }
372:     public function GetHost() {
373:         return $this->Host;
374:     }
375:     public function GetPort() {
376:         return $this->Port;
377:     }
378: 
379:     /**
380:      * @return string
381:      */
382:     public function GetUriString() {
383:         return $this->Scheme->GetSchemeString().'://'
384:             .$this->Host
385:             .(!empty($this->Port)?':'.$this->Port:'')
386:             .$this->Path
387:             .(!empty($this->Query)?'?'.$this->Query->GetQueryString():'')
388:             .(!empty($this->Fragment)?'#'.$this->Fragment:'');
389:     }
390: 
391:     /**
392:      * @return string
393:      */
394:     function __toString() {
395:         return $this->GetUriString();
396:     }
397: }
398: 
API documentation generated by ApiGen 2.8.0