1: <?php
2:
3: namespace SMSApi\Proxy\Http;
4:
5: class AbstractHttp {
6:
7: protected $protocol;
8: protected $host;
9: protected $port;
10: protected $uri;
11: protected $file;
12: protected $boundary = '**RGRG87VFSGF86796GSD**';
13: protected $method = "POST";
14: protected $timeout = 5;
15: protected $maxRedirects = 1;
16: protected $userAgent = "SMSAPI";
17: protected = array( );
18:
19: public function __construct( $host ) {
20:
21: $tmp = explode( "://", $host );
22:
23: if ( isset( $tmp[ 0 ] ) ) {
24: $this->protocol = $tmp[ 0 ];
25: if ( $this->protocol == "http" ) {
26: $this->port = 80;
27: } else if ( $this->protocol == "https" ) {
28: $this->port = 443;
29: }
30: }
31:
32: if ( isset( $tmp[ 1 ] ) ) {
33: $this->host = $tmp[ 1 ];
34: }
35: }
36:
37: public function getHost() {
38: return $this->host;
39: }
40:
41: public function getPort() {
42: return $this->port;
43: }
44:
45: public function getProtocol() {
46: return $this->protocol;
47: }
48:
49: protected function checkCode( $code ) {
50: if ( $code AND $code < 200 OR $code > 299 ) {
51: throw new \SMSApi\Exception\ProxyException( 'Error fetching remote' );
52: }
53: }
54:
55: protected function detectFileMimeType( $file ) {
56: $type = null;
57:
58: if ( function_exists( 'finfo_open' ) ) {
59: $fo = finfo_open( FILEINFO_MIME );
60: if ( $fo ) {
61: $type = finfo_file( $fo, $file );
62: }
63: } elseif ( function_exists( 'mime_content_type' ) ) {
64: $type = mime_content_type( $file );
65: }
66:
67: if ( !$type ) {
68: $type = 'application/octet-stream';
69: }
70:
71: return $type;
72: }
73:
74: protected function encodeFormData( $boundary, $name, $value, $filename = null, $headers = array( ) ) {
75: $ret = "--{$boundary}\r\n" .
76: 'Content-Disposition: form-data; name="' . $name . '"';
77:
78: if ( $filename ) {
79: $ret .= '; filename="' . $filename . '"';
80: }
81: $ret .= "\r\n";
82:
83: foreach ( $headers as $hname => $hvalue ) {
84: $ret .= "{$hname}: {$hvalue}\r\n";
85: }
86: $ret .= "\r\n";
87: $ret .= "{$value}\r\n";
88:
89: return $ret;
90: }
91:
92: protected function prepareFileContent( $filename ) {
93:
94: $file[ 'formname' ] = 'file';
95: $file[ 'data' ] = file_get_contents( $filename );
96: $file[ 'filename' ] = basename( $filename );
97: $file[ 'ctype' ] = $this->detectFileMimeType( $filename );
98: $fhead = array( 'Content-Type' => $file[ 'ctype' ] );
99:
100: $body = $this->encodeFormData( $this->boundary, $file[ 'formname' ], $file[ 'data' ], $file[ 'filename' ], $fhead );
101:
102: $body .= "--{$this->boundary}--\r\n";
103:
104: return $body;
105: }
106:
107: protected function renderQueryByBody( $query, $body ) {
108:
109: $tmpBody = "";
110:
111: if ( !empty( $query ) && !empty( $body ) ) {
112: $params = array( );
113: parse_str( $query, $params );
114: foreach ( $params as $k2 => $v2 ) {
115: $tmpBody .= $this->encodeFormData( $this->boundary, $k2, $v2 );
116: }
117: } else {
118: $tmpBody = $query;
119: }
120:
121: if ( !empty( $body ) ) {
122: $tmpBody .= $body;
123: }
124:
125: return $tmpBody;
126: }
127:
128: }
129:
130: