| 1 |
63
|
kore |
<?php |
|
| 2 |
↓
|
kore |
/** |
|
| 3 |
↓
|
kore |
* phpillow CouchDB backend |
|
| 4 |
↓
|
kore |
* |
|
| 5 |
↓
|
kore |
* This file is part of phpillow. |
|
| 6 |
↓
|
kore |
* |
|
| 7 |
↓
|
kore |
* phpillow is free software; you can redistribute it and/or modify it under |
|
| 8 |
↓
|
kore |
* the terms of the GNU Lesser General Public License as published by the Free |
|
| 9 |
↓
|
kore |
* Software Foundation; version 3 of the License. |
|
| 10 |
↓
|
kore |
* |
|
| 11 |
↓
|
kore |
* phpillow is distributed in the hope that it will be useful, but WITHOUT ANY |
|
| 12 |
↓
|
kore |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
| 13 |
↓
|
kore |
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for |
|
| 14 |
↓
|
kore |
* more details. |
|
| 15 |
↓
|
kore |
* |
|
| 16 |
↓
|
kore |
* You should have received a copy of the GNU Lesser General Public License |
|
| 17 |
↓
|
kore |
* along with phpillow; if not, write to the Free Software Foundation, Inc., 51 |
|
| 18 |
↓
|
kore |
* Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
| 19 |
↓
|
kore |
* |
|
| 20 |
↓
|
kore |
* @package Core |
|
| 21 |
96
|
kore |
* @version $Revision: 159 $ |
|
| 22 |
63
|
kore |
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL |
|
| 23 |
↓
|
kore |
*/ |
|
| 24 |
↓
|
kore |
|
|
| 25 |
↓
|
kore |
/** |
|
| 26 |
↓
|
kore |
* Basic couch DB connection handling class |
|
| 27 |
↓
|
kore |
* |
|
| 28 |
↓
|
kore |
* Connection handler using PHPs stream wrappers. |
|
| 29 |
↓
|
kore |
* |
|
| 30 |
↓
|
kore |
* @package Core |
|
| 31 |
96
|
kore |
* @version $Revision: 159 $ |
|
| 32 |
63
|
kore |
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL |
|
| 33 |
↓
|
kore |
*/ |
|
| 34 |
↓
|
kore |
class phpillowStreamConnection extends phpillowConnection |
|
| 35 |
↓
|
kore |
{ |
|
| 36 |
97
|
kore |
/** |
|
| 37 |
↓
|
kore |
* Create a new couch DB connection instance. |
|
| 38 |
↓
|
kore |
* |
|
| 39 |
↓
|
kore |
* Static method to create a new couch DB connection instance. This method |
|
| 40 |
↓
|
kore |
* should be used to configure the connection for later use. |
|
| 41 |
↓
|
kore |
* |
|
| 42 |
↓
|
kore |
* The host and its port default to localhost:5984. |
|
| 43 |
↓
|
kore |
* |
|
| 44 |
↓
|
kore |
* @param string $host |
|
| 45 |
↓
|
kore |
* @param int $port |
|
| 46 |
106
|
kore |
* @param string $username |
|
| 47 |
↓
|
kore |
* @param string $password |
|
| 48 |
97
|
kore |
* @param string $called |
|
| 49 |
↓
|
kore |
* @return void |
|
| 50 |
↓
|
kore |
*/ |
|
| 51 |
106
|
kore |
public static function createInstance( $host = '127.0.0.1', $port = 5984, $username = null, $password = null, $called = "phpillowStreamConnection" ) |
|
| 52 |
94
|
kore |
{ |
|
| 53 |
106
|
kore |
parent::createInstance( $host, $port, $username, $password, $called ); |
|
| 54 |
94
|
kore |
} |
|
| 55 |
↓
|
kore |
|
|
| 56 |
63
|
kore |
/** |
|
| 57 |
↓
|
kore |
* Perform a request to the server and return the result |
|
| 58 |
↓
|
kore |
* |
|
| 59 |
↓
|
kore |
* Perform a request to the server and return the result converted into a |
|
| 60 |
↓
|
kore |
* phpillowResponse object. If you do not expect a JSON structure, which |
|
| 61 |
↓
|
kore |
* could be converted in such a response object, set the forth parameter to |
|
| 62 |
159
|
kore |
* true, and you get a response object returned, containing the raw body. |
|
| 63 |
63
|
kore |
* |
|
| 64 |
↓
|
kore |
* @param string $method |
|
| 65 |
↓
|
kore |
* @param string $path |
|
| 66 |
↓
|
kore |
* @param string $data |
|
| 67 |
↓
|
kore |
* @return phpillowResponse |
|
| 68 |
↓
|
kore |
*/ |
|
| 69 |
↓
|
kore |
protected function request( $method, $path, $data, $raw = false ) |
|
| 70 |
↓
|
kore |
{ |
|
| 71 |
106
|
kore |
$basicAuth = ''; |
|
| 72 |
↓
|
kore |
if ( $this->options['username'] ) |
|
| 73 |
↓
|
kore |
{ |
|
| 74 |
↓
|
kore |
$basicAuth .= "{$this->options['username']}:{$this->options['password']}@"; |
|
| 75 |
↓
|
kore |
} |
|
| 76 |
↓
|
kore |
|
|
| 77 |
↓
|
kore |
$url = 'http://' . $basicAuth . $this->options['host'] . ':' . $this->options['port'] . $path; |
|
| 78 |
↓
|
kore |
|
|
| 79 |
63
|
kore |
$httpFilePointer = @fopen( |
|
| 80 |
↓
|
kore |
$url = 'http://' . $this->options['host'] . ':' . $this->options['port'] . $path, 'r', false, |
|
| 81 |
↓
|
kore |
stream_context_create( |
|
| 82 |
↓
|
kore |
array( |
|
| 83 |
↓
|
kore |
'http' => array( |
|
| 84 |
↓
|
kore |
'method' => $method, |
|
| 85 |
↓
|
kore |
'content' => $data, |
|
| 86 |
↓
|
kore |
'ignore_errors' => true, |
|
| 87 |
96
|
kore |
'user_agent' => 'PHPillow $Revision: 159 $', |
|
| 88 |
63
|
kore |
'timeout' => $this->options['timeout'], |
|
| 89 |
↓
|
kore |
'header' => 'Content-type: application/json', |
|
| 90 |
↓
|
kore |
), |
|
| 91 |
↓
|
kore |
) |
|
| 92 |
↓
|
kore |
) |
|
| 93 |
↓
|
kore |
); |
|
| 94 |
↓
|
kore |
|
|
| 95 |
↓
|
kore |
// Check if connection has been established successfully |
|
| 96 |
↓
|
kore |
if ( $httpFilePointer === false ) |
|
| 97 |
↓
|
kore |
{ |
|
| 98 |
↓
|
kore |
$error = error_get_last(); |
|
| 99 |
↓
|
kore |
throw new phpillowConnectionException( |
|
| 100 |
↓
|
kore |
"Could not connect to server at %ip:%port: %error", |
|
| 101 |
↓
|
kore |
array( |
|
| 102 |
↓
|
kore |
'ip' => $this->options['ip'], |
|
| 103 |
↓
|
kore |
'port' => $this->options['port'], |
|
| 104 |
↓
|
kore |
'error' => $error['message'], |
|
| 105 |
↓
|
kore |
) |
|
| 106 |
↓
|
kore |
); |
|
| 107 |
↓
|
kore |
} |
|
| 108 |
↓
|
kore |
|
|
| 109 |
↓
|
kore |
// Read request body |
|
| 110 |
↓
|
kore |
$body = ''; |
|
| 111 |
↓
|
kore |
while ( !feof( $httpFilePointer ) ) |
|
| 112 |
↓
|
kore |
{ |
|
| 113 |
↓
|
kore |
$body .= fgets( $httpFilePointer ); |
|
| 114 |
↓
|
kore |
} |
|
| 115 |
159
|
kore |
|
|
| 116 |
63
|
kore |
$metaData = stream_get_meta_data( $httpFilePointer ); |
|
| 117 |
↓
|
kore |
// @TODO: This seems to have changed in last CVS versions of PHP 5.3, |
|
| 118 |
159
|
kore |
// should be removable, once there is a next release of PHP 5.3 |
|
| 119 |
63
|
kore |
$rawHeaders = isset( $metaData['wrapper_data']['headers'] ) ? $metaData['wrapper_data']['headers'] : $metaData['wrapper_data']; |
|
| 120 |
↓
|
kore |
$headers = array(); |
|
| 121 |
↓
|
kore |
|
|
| 122 |
↓
|
kore |
foreach ( $rawHeaders as $lineContent ) |
|
| 123 |
↓
|
kore |
{ |
|
| 124 |
↓
|
kore |
// Extract header values |
|
| 125 |
↓
|
kore |
if ( preg_match( '(^HTTP/(?P<version>\d+\.\d+)\s+(?P<status>\d+))S', $lineContent, $match ) ) |
|
| 126 |
↓
|
kore |
{ |
|
| 127 |
↓
|
kore |
$headers['version'] = $match['version']; |
|
| 128 |
↓
|
kore |
$headers['status'] = (int) $match['status']; |
|
| 129 |
↓
|
kore |
} |
|
| 130 |
↓
|
kore |
else |
|
| 131 |
↓
|
kore |
{ |
|
| 132 |
↓
|
kore |
list( $key, $value ) = explode( ':', $lineContent, 2 ); |
|
| 133 |
↓
|
kore |
$headers[strtolower( $key )] = ltrim( $value ); |
|
| 134 |
↓
|
kore |
} |
|
| 135 |
↓
|
kore |
} |
|
| 136 |
↓
|
kore |
|
|
| 137 |
↓
|
kore |
// If requested log response information to http log |
|
| 138 |
↓
|
kore |
if ( $this->options['http-log'] !== false ) |
|
| 139 |
↓
|
kore |
{ |
|
| 140 |
↓
|
kore |
file_put_contents( $this->options['http-log'], |
|
| 141 |
↓
|
kore |
sprintf( "Requested: %s\n\n%s\n\n%s\n\n", |
|
| 142 |
↓
|
kore |
$url, |
|
| 143 |
↓
|
kore |
implode( "\n", $rawHeaders ), |
|
| 144 |
↓
|
kore |
$body |
|
| 145 |
↓
|
kore |
) |
|
| 146 |
↓
|
kore |
); |
|
| 147 |
↓
|
kore |
} |
|
| 148 |
↓
|
kore |
|
|
| 149 |
159
|
kore |
// Create response object from couch db response |
|
| 150 |
63
|
kore |
return phpillowResponseFactory::parse( $headers, $body, $raw ); |
|
| 151 |
↓
|
kore |
} |
|
| 152 |
↓
|
kore |
} |
|
| 153 |
↓
|
kore |
|
|