| 1 |
<?php |
| 2 |
/** |
| 3 |
* Laconica, the distributed open-source microblogging tool |
| 4 |
* |
| 5 |
* Plugin to check submitted notices with Mollom |
| 6 |
* |
| 7 |
* PHP version 5 |
| 8 |
* |
| 9 |
* LICENCE: This program is free software: you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU Affero General Public License as published by |
| 11 |
* the Free Software Foundation, either version 3 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU Affero General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Affero General Public License |
| 20 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
* |
| 22 |
* Mollom is a bayesian spam checker, wrapped into a webservice |
| 23 |
* This plugin is based on the Drupal Mollom module |
| 24 |
* |
| 25 |
* @category Plugin |
| 26 |
* @package Laconica |
| 27 |
* @author Brenda Wallace <brenda@cpan.org> |
| 28 |
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 |
| 29 |
* |
| 30 |
*/ |
| 31 |
|
| 32 |
if (!defined('LACONICA')) { |
| 33 |
exit(1); |
| 34 |
} |
| 35 |
|
| 36 |
define('MOLLOMPLUGIN_VERSION', '0.1'); |
| 37 |
define('MOLLOM_API_VERSION', '1.0'); |
| 38 |
|
| 39 |
define('MOLLOM_ANALYSIS_UNKNOWN' , 0); |
| 40 |
define('MOLLOM_ANALYSIS_HAM' , 1); |
| 41 |
define('MOLLOM_ANALYSIS_SPAM' , 2); |
| 42 |
define('MOLLOM_ANALYSIS_UNSURE' , 3); |
| 43 |
|
| 44 |
define('MOLLOM_MODE_DISABLED', 0); |
| 45 |
define('MOLLOM_MODE_CAPTCHA' , 1); |
| 46 |
define('MOLLOM_MODE_ANALYSIS', 2); |
| 47 |
|
| 48 |
define('MOLLOM_FALLBACK_BLOCK' , 0); |
| 49 |
define('MOLLOM_FALLBACK_ACCEPT', 1); |
| 50 |
|
| 51 |
define('MOLLOM_ERROR' , 1000); |
| 52 |
define('MOLLOM_REFRESH' , 1100); |
| 53 |
define('MOLLOM_REDIRECT', 1200); |
| 54 |
|
| 55 |
/** |
| 56 |
* Plugin to check submitted notices with Mollom |
| 57 |
* |
| 58 |
* Mollom is a bayesian spam filter provided by webservice. |
| 59 |
* |
| 60 |
* @category Plugin |
| 61 |
* @package Laconica |
| 62 |
* @author Brenda Wallace <shiny@cpan.org> |
| 63 |
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 |
| 64 |
* |
| 65 |
* @see Event |
| 66 |
*/ |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
class MollomPlugin extends Plugin |
| 71 |
{ |
| 72 |
function __construct($url=null) { |
| 73 |
parent::__construct(); |
| 74 |
} |
| 75 |
|
| 76 |
function onStartNoticeSave($notice) |
| 77 |
{ |
| 78 |
error_log(print_r($notice, 1)); |
| 79 |
if (common_config('mollom', 'public_key')) { |
| 80 |
//Check spam |
| 81 |
$data = array( |
| 82 |
'post_body' => $notice->content, |
| 83 |
'author_name' => $profile->nickname, |
| 84 |
'author_url' => $profile->homepage, |
| 85 |
'author_id' => $profile->id, |
| 86 |
'author_ip' => $this->getClientIp(), |
| 87 |
); |
| 88 |
$response = $this->mollom('mollom.checkContent', $data); |
| 89 |
if ($response['spam'] == MOLLOM_ANALYSIS_SPAM) { |
| 90 |
throw new ClientException(_("Spam Detected"), 400); |
| 91 |
} |
| 92 |
if ($response['spam'] == MOLLOM_ANALYSIS_UNSURE) { |
| 93 |
//if unsure, let through |
| 94 |
} |
| 95 |
if($response['spam'] == MOLLOM_ANALYSIS_HAM) { |
| 96 |
// all good! :-) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
function getClientIP() { |
| 104 |
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) { |
| 105 |
// Note: order matters here; use proxy-forwarded stuff first |
| 106 |
foreach (array('HTTP_X_FORWARDED_FOR', 'CLIENT-IP', 'REMOTE_ADDR') as $k) { |
| 107 |
if (isset($_SERVER[$k])) { |
| 108 |
return $_SERVER[$k]; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
return '127.0.0.1'; |
| 113 |
} |
| 114 |
/** |
| 115 |
* Call a remote procedure at the Mollom server. This function will |
| 116 |
* automatically add the information required to authenticate against |
| 117 |
* Mollom. |
| 118 |
*/ |
| 119 |
function mollom($method, $data = array()) { |
| 120 |
if (!extension_loaded('xmlrpc')) { |
| 121 |
if (!dl('xmlrpc.so')) { |
| 122 |
common_log(LOG_ERR, "Can't pingback; xmlrpc extension not available."); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
// Construct the server URL: |
| 127 |
$public_key = common_config('mollom', 'public_key'); |
| 128 |
// Retrieve the list of Mollom servers from the database: |
| 129 |
$servers = common_config('mollom', 'servers'); |
| 130 |
|
| 131 |
if ($servers == NULL) { |
| 132 |
// Retrieve a list of valid Mollom servers from mollom.com: |
| 133 |
$servers = $this->xmlrpc('http://xmlrpc.mollom.com/'. MOLLOM_API_VERSION, 'mollom.getServerList', $this->authentication()); |
| 134 |
|
| 135 |
// Store the list of servers in the database: |
| 136 |
// TODO! variable_set('mollom_servers', $servers); |
| 137 |
} |
| 138 |
|
| 139 |
if (is_array($servers)) { |
| 140 |
// Send the request to the first server, if that fails, try the other servers in the list: |
| 141 |
foreach ($servers as $server) { |
| 142 |
$auth = $this->authentication(); |
| 143 |
$data = array_merge($data, $auth); |
| 144 |
$result = $this->xmlrpc($server .'/'. MOLLOM_API_VERSION, $method, $data); |
| 145 |
|
| 146 |
// Debug output: |
| 147 |
if (isset($data['session_id'])) { |
| 148 |
error_log("called $method at server $server with session ID '". $data['session_id'] ."'"); |
| 149 |
} |
| 150 |
else { |
| 151 |
error_log("called $method at server $server with no session ID"); |
| 152 |
} |
| 153 |
|
| 154 |
if ($errno = $this->xmlrpc_errno()) { |
| 155 |
error_log(sprintf('Error @errno: %s - %s - %s - <pre>%s</pre>', $this->xmlrpc_errno(), $server, $this->xmlrpc_error_msg(), $method, print_r($data, TRUE))); |
| 156 |
|
| 157 |
if ($errno == MOLLOM_REFRESH) { |
| 158 |
// Retrieve a list of valid Mollom servers from mollom.com: |
| 159 |
$servers = $this->xmlrpc('http://xmlrpc.mollom.com/'. MOLLOM_API_VERSION, 'mollom.getServerList', $this->authentication()); |
| 160 |
|
| 161 |
// Store the updated list of servers in the database: |
| 162 |
//tODO variable_set('mollom_servers', $servers); |
| 163 |
} |
| 164 |
else if ($errno == MOLLOM_ERROR) { |
| 165 |
return $result; |
| 166 |
} |
| 167 |
else if ($errno == MOLLOM_REDIRECT) { |
| 168 |
// Do nothing, we select the next client automatically. |
| 169 |
} |
| 170 |
|
| 171 |
// Reset the XMLRPC error: |
| 172 |
$this->xmlrpc_error(0); // FIXME: this is crazy. |
| 173 |
} |
| 174 |
else { |
| 175 |
error_log("Result = " . print_r($result, TRUE)); |
| 176 |
return $result; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
// If none of the servers worked, activate the fallback mechanism: |
| 182 |
error_log("none of the servers worked"); |
| 183 |
// _mollom_fallback(); |
| 184 |
|
| 185 |
// If everything failed, we reset the server list to force Mollom to request a new list: |
| 186 |
//TODO variable_set('mollom_servers', array()); |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* This function generate an array with all the information required to |
| 191 |
* authenticate against Mollom. To prevent that requests are forged and |
| 192 |
* that you are impersonated, each request is signed with a hash computed |
| 193 |
* based on a private key and a timestamp. |
| 194 |
* |
| 195 |
* Both the client and the server share the secret key that is used to |
| 196 |
* create the authentication hash based on a timestamp. They both hash |
| 197 |
* the timestamp with the secret key, and if the hashes match, the |
| 198 |
* authenticity of the message has been validated. |
| 199 |
* |
| 200 |
* To avoid that someone can intercept a (hash, timestamp)-pair and |
| 201 |
* use that to impersonate a client, Mollom will reject the request |
| 202 |
* when the timestamp is more than 15 minutes off. |
| 203 |
* |
| 204 |
* Make sure your server's time is synchronized with the world clocks, |
| 205 |
* and that you don't share your private key with anyone else. |
| 206 |
*/ |
| 207 |
private function authentication() { |
| 208 |
|
| 209 |
$public_key = common_config('mollom', 'public_key'); |
| 210 |
$private_key = common_config('mollom', 'private_key'); |
| 211 |
|
| 212 |
// Generate a timestamp according to the dateTime format (http://www.w3.org/TR/xmlschema-2/#dateTime): |
| 213 |
$time = gmdate("Y-m-d\TH:i:s.\\0\\0\\0O", time()); |
| 214 |
|
| 215 |
// Calculate a HMAC-SHA1 according to RFC2104 (http://www.ietf.org/rfc/rfc2104.txt): |
| 216 |
$hash = base64_encode( |
| 217 |
pack("H*", sha1((str_pad($private_key, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) . |
| 218 |
pack("H*", sha1((str_pad($private_key, 64, chr(0x00)) ^ (str_repeat(chr(0x36), 64))) . |
| 219 |
$time)))) |
| 220 |
); |
| 221 |
|
| 222 |
// Store everything in an array. Elsewhere in the code, we'll add the |
| 223 |
// acutal data before we pass it onto the XML-RPC library: |
| 224 |
$data['public_key'] = $public_key; |
| 225 |
$data['time'] = $time; |
| 226 |
$data['hash'] = $hash; |
| 227 |
|
| 228 |
return $data; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
function xmlrpc($url) { |
| 233 |
//require_once './includes/xmlrpc.inc'; |
| 234 |
$args = func_get_args(); |
| 235 |
return call_user_func_array(array('MollomPlugin', '_xmlrpc'), $args); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Recursively turn a data structure into objects with 'data' and 'type' attributes. |
| 240 |
* |
| 241 |
* @param $data |
| 242 |
* The data structure. |
| 243 |
* @param $type |
| 244 |
* Optional type assign to $data. |
| 245 |
* @return |
| 246 |
* Object. |
| 247 |
*/ |
| 248 |
function xmlrpc_value($data, $type = FALSE) { |
| 249 |
$xmlrpc_value = new stdClass(); |
| 250 |
$xmlrpc_value->data = $data; |
| 251 |
if (!$type) { |
| 252 |
$type = $this->xmlrpc_value_calculate_type($xmlrpc_value); |
| 253 |
} |
| 254 |
$xmlrpc_value->type = $type; |
| 255 |
if ($type == 'struct') { |
| 256 |
// Turn all the values in the array into new xmlrpc_values |
| 257 |
foreach ($xmlrpc_value->data as $key => $value) { |
| 258 |
$xmlrpc_value->data[$key] = $this->xmlrpc_value($value); |
| 259 |
} |
| 260 |
} |
| 261 |
if ($type == 'array') { |
| 262 |
for ($i = 0, $j = count($xmlrpc_value->data); $i < $j; $i++) { |
| 263 |
$xmlrpc_value->data[$i] = $this->xmlrpc_value($xmlrpc_value->data[$i]); |
| 264 |
} |
| 265 |
} |
| 266 |
return $xmlrpc_value; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Map PHP type to XML-RPC type. |
| 271 |
* |
| 272 |
* @param $xmlrpc_value |
| 273 |
* Variable whose type should be mapped. |
| 274 |
* @return |
| 275 |
* XML-RPC type as string. |
| 276 |
* @see |
| 277 |
* http://www.xmlrpc.com/spec#scalars |
| 278 |
*/ |
| 279 |
function xmlrpc_value_calculate_type(&$xmlrpc_value) { |
| 280 |
// http://www.php.net/gettype: Never use gettype() to test for a certain type [...] Instead, use the is_* functions. |
| 281 |
if (is_bool($xmlrpc_value->data)) { |
| 282 |
return 'boolean'; |
| 283 |
} |
| 284 |
if (is_double($xmlrpc_value->data)) { |
| 285 |
return 'double'; |
| 286 |
} |
| 287 |
if (is_int($xmlrpc_value->data)) { |
| 288 |
return 'int'; |
| 289 |
} |
| 290 |
if (is_array($xmlrpc_value->data)) { |
| 291 |
// empty or integer-indexed arrays are 'array', string-indexed arrays 'struct' |
| 292 |
return empty($xmlrpc_value->data) || range(0, count($xmlrpc_value->data) - 1) === array_keys($xmlrpc_value->data) ? 'array' : 'struct'; |
| 293 |
} |
| 294 |
if (is_object($xmlrpc_value->data)) { |
| 295 |
if ($xmlrpc_value->data->is_date) { |
| 296 |
return 'date'; |
| 297 |
} |
| 298 |
if ($xmlrpc_value->data->is_base64) { |
| 299 |
return 'base64'; |
| 300 |
} |
| 301 |
$xmlrpc_value->data = get_object_vars($xmlrpc_value->data); |
| 302 |
return 'struct'; |
| 303 |
} |
| 304 |
// default |
| 305 |
return 'string'; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Generate XML representing the given value. |
| 310 |
* |
| 311 |
* @param $xmlrpc_value |
| 312 |
* @return |
| 313 |
* XML representation of value. |
| 314 |
*/ |
| 315 |
function xmlrpc_value_get_xml($xmlrpc_value) { |
| 316 |
switch ($xmlrpc_value->type) { |
| 317 |
case 'boolean': |
| 318 |
return '<boolean>'. (($xmlrpc_value->data) ? '1' : '0') .'</boolean>'; |
| 319 |
break; |
| 320 |
case 'int': |
| 321 |
return '<int>'. $xmlrpc_value->data .'</int>'; |
| 322 |
break; |
| 323 |
case 'double': |
| 324 |
return '<double>'. $xmlrpc_value->data .'</double>'; |
| 325 |
break; |
| 326 |
case 'string': |
| 327 |
// Note: we don't escape apostrophes because of the many blogging clients |
| 328 |
// that don't support numerical entities (and XML in general) properly. |
| 329 |
return '<string>'. htmlspecialchars($xmlrpc_value->data) .'</string>'; |
| 330 |
break; |
| 331 |
case 'array': |
| 332 |
$return = '<array><data>'."\n"; |
| 333 |
foreach ($xmlrpc_value->data as $item) { |
| 334 |
$return .= ' <value>'. $this->xmlrpc_value_get_xml($item) ."</value>\n"; |
| 335 |
} |
| 336 |
$return .= '</data></array>'; |
| 337 |
return $return; |
| 338 |
break; |
| 339 |
case 'struct': |
| 340 |
$return = '<struct>'."\n"; |
| 341 |
foreach ($xmlrpc_value->data as $name => $value) { |
| 342 |
$return .= " <member><name>". htmlentities($name) ."</name><value>"; |
| 343 |
$return .= $this->xmlrpc_value_get_xml($value) ."</value></member>\n"; |
| 344 |
} |
| 345 |
$return .= '</struct>'; |
| 346 |
return $return; |
| 347 |
break; |
| 348 |
case 'date': |
| 349 |
return $this->xmlrpc_date_get_xml($xmlrpc_value->data); |
| 350 |
break; |
| 351 |
case 'base64': |
| 352 |
return $this->xmlrpc_base64_get_xml($xmlrpc_value->data); |
| 353 |
break; |
| 354 |
} |
| 355 |
return FALSE; |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Perform an HTTP request. |
| 360 |
* |
| 361 |
* This is a flexible and powerful HTTP client implementation. Correctly handles |
| 362 |
* GET, POST, PUT or any other HTTP requests. Handles redirects. |
| 363 |
* |
| 364 |
* @param $url |
| 365 |
* A string containing a fully qualified URI. |
| 366 |
* @param $headers |
| 367 |
* An array containing an HTTP header => value pair. |
| 368 |
* @param $method |
| 369 |
* A string defining the HTTP request to use. |
| 370 |
* @param $data |
| 371 |
* A string containing data to include in the request. |
| 372 |
* @param $retry |
| 373 |
* An integer representing how many times to retry the request in case of a |
| 374 |
* redirect. |
| 375 |
* @return |
| 376 |
* An object containing the HTTP request headers, response code, headers, |
| 377 |
* data and redirect status. |
| 378 |
*/ |
| 379 |
function http_request($url, $headers = array(), $method = 'GET', $data = NULL, $retry = 3) { |
| 380 |
global $db_prefix; |
| 381 |
|
| 382 |
$result = new stdClass(); |
| 383 |
|
| 384 |
// Parse the URL and make sure we can handle the schema. |
| 385 |
$uri = parse_url($url); |
| 386 |
|
| 387 |
if ($uri == FALSE) { |
| 388 |
$result->error = 'unable to parse URL'; |
| 389 |
return $result; |
| 390 |
} |
| 391 |
|
| 392 |
if (!isset($uri['scheme'])) { |
| 393 |
$result->error = 'missing schema'; |
| 394 |
return $result; |
| 395 |
} |
| 396 |
|
| 397 |
switch ($uri['scheme']) { |
| 398 |
case 'http': |
| 399 |
$port = isset($uri['port']) ? $uri['port'] : 80; |
| 400 |
$host = $uri['host'] . ($port != 80 ? ':'. $port : ''); |
| 401 |
$fp = @fsockopen($uri['host'], $port, $errno, $errstr, 15); |
| 402 |
break; |
| 403 |
case 'https': |
| 404 |
// Note: Only works for PHP 4.3 compiled with OpenSSL. |
| 405 |
$port = isset($uri['port']) ? $uri['port'] : 443; |
| 406 |
$host = $uri['host'] . ($port != 443 ? ':'. $port : ''); |
| 407 |
$fp = @fsockopen('ssl://'. $uri['host'], $port, $errno, $errstr, 20); |
| 408 |
break; |
| 409 |
default: |
| 410 |
$result->error = 'invalid schema '. $uri['scheme']; |
| 411 |
return $result; |
| 412 |
} |
| 413 |
|
| 414 |
// Make sure the socket opened properly. |
| 415 |
if (!$fp) { |
| 416 |
// When a network error occurs, we use a negative number so it does not |
| 417 |
// clash with the HTTP status codes. |
| 418 |
$result->code = -$errno; |
| 419 |
$result->error = trim($errstr); |
| 420 |
|
| 421 |
// Mark that this request failed. This will trigger a check of the web |
| 422 |
// server's ability to make outgoing HTTP requests the next time that |
| 423 |
// requirements checking is performed. |
| 424 |
// @see system_requirements() |
| 425 |
//TODO variable_set('drupal_http_request_fails', TRUE); |
| 426 |
|
| 427 |
return $result; |
| 428 |
} |
| 429 |
|
| 430 |
// Construct the path to act on. |
| 431 |
$path = isset($uri['path']) ? $uri['path'] : '/'; |
| 432 |
if (isset($uri['query'])) { |
| 433 |
$path .= '?'. $uri['query']; |
| 434 |
} |
| 435 |
|
| 436 |
// Create HTTP request. |
| 437 |
$defaults = array( |
| 438 |
// RFC 2616: "non-standard ports MUST, default ports MAY be included". |
| 439 |
// We don't add the port to prevent from breaking rewrite rules checking the |
| 440 |
// host that do not take into account the port number. |
| 441 |
'Host' => "Host: $host", |
| 442 |
'User-Agent' => 'User-Agent: Drupal (+http://drupal.org/)', |
| 443 |
'Content-Length' => 'Content-Length: '. strlen($data) |
| 444 |
); |
| 445 |
|
| 446 |
// If the server url has a user then attempt to use basic authentication |
| 447 |
if (isset($uri['user'])) { |
| 448 |
$defaults['Authorization'] = 'Authorization: Basic '. base64_encode($uri['user'] . (!empty($uri['pass']) ? ":". $uri['pass'] : '')); |
| 449 |
} |
| 450 |
|
| 451 |
// If the database prefix is being used by SimpleTest to run the tests in a copied |
| 452 |
// database then set the user-agent header to the database prefix so that any |
| 453 |
// calls to other Drupal pages will run the SimpleTest prefixed database. The |
| 454 |
// user-agent is used to ensure that multiple testing sessions running at the |
| 455 |
// same time won't interfere with each other as they would if the database |
| 456 |
// prefix were stored statically in a file or database variable. |
| 457 |
if (is_string($db_prefix) && preg_match("/^simpletest\d+$/", $db_prefix, $matches)) { |
| 458 |
$defaults['User-Agent'] = 'User-Agent: ' . $matches[0]; |
| 459 |
} |
| 460 |
|
| 461 |
foreach ($headers as $header => $value) { |
| 462 |
$defaults[$header] = $header .': '. $value; |
| 463 |
} |
| 464 |
|
| 465 |
$request = $method .' '. $path ." HTTP/1.0\r\n"; |
| 466 |
$request .= implode("\r\n", $defaults); |
| 467 |
$request .= "\r\n\r\n"; |
| 468 |
$request .= $data; |
| 469 |
|
| 470 |
$result->request = $request; |
| 471 |
|
| 472 |
fwrite($fp, $request); |
| 473 |
|
| 474 |
// Fetch response. |
| 475 |
$response = ''; |
| 476 |
while (!feof($fp) && $chunk = fread($fp, 1024)) { |
| 477 |
$response .= $chunk; |
| 478 |
} |
| 479 |
fclose($fp); |
| 480 |
|
| 481 |
// Parse response. |
| 482 |
list($split, $result->data) = explode("\r\n\r\n", $response, 2); |
| 483 |
$split = preg_split("/\r\n|\n|\r/", $split); |
| 484 |
|
| 485 |
list($protocol, $code, $text) = explode(' ', trim(array_shift($split)), 3); |
| 486 |
$result->headers = array(); |
| 487 |
|
| 488 |
// Parse headers. |
| 489 |
while ($line = trim(array_shift($split))) { |
| 490 |
list($header, $value) = explode(':', $line, 2); |
| 491 |
if (isset($result->headers[$header]) && $header == 'Set-Cookie') { |
| 492 |
// RFC 2109: the Set-Cookie response header comprises the token Set- |
| 493 |
// Cookie:, followed by a comma-separated list of one or more cookies. |
| 494 |
$result->headers[$header] .= ','. trim($value); |
| 495 |
} |
| 496 |
else { |
| 497 |
$result->headers[$header] = trim($value); |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
$responses = array( |
| 502 |
100 => 'Continue', 101 => 'Switching Protocols', |
| 503 |
200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', |
| 504 |
300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', |
| 505 |
400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', |
| 506 |
500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported' |
| 507 |
); |
| 508 |
// RFC 2616 states that all unknown HTTP codes must be treated the same as the |
| 509 |
// base code in their class. |
| 510 |
if (!isset($responses[$code])) { |
| 511 |
$code = floor($code / 100) * 100; |
| 512 |
} |
| 513 |
|
| 514 |
switch ($code) { |
| 515 |
case 200: // OK |
| 516 |
case 304: // Not modified |
| 517 |
break; |
| 518 |
case 301: // Moved permanently |
| 519 |
case 302: // Moved temporarily |
| 520 |
case 307: // Moved temporarily |
| 521 |
$location = $result->headers['Location']; |
| 522 |
|
| 523 |
if ($retry) { |
| 524 |
$result = drupal_http_request($result->headers['Location'], $headers, $method, $data, --$retry); |
| 525 |
$result->redirect_code = $result->code; |
| 526 |
} |
| 527 |
$result->redirect_url = $location; |
| 528 |
|
| 529 |
break; |
| 530 |
default: |
| 531 |
$result->error = $text; |
| 532 |
} |
| 533 |
|
| 534 |
$result->code = $code; |
| 535 |
return $result; |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Construct an object representing an XML-RPC message. |
| 540 |
* |
| 541 |
* @param $message |
| 542 |
* String containing XML as defined at http://www.xmlrpc.com/spec |
| 543 |
* @return |
| 544 |
* Object |
| 545 |
*/ |
| 546 |
function xmlrpc_message($message) { |
| 547 |
$xmlrpc_message = new stdClass(); |
| 548 |
$xmlrpc_message->array_structs = array(); // The stack used to keep track of the current array/struct |
| 549 |
$xmlrpc_message->array_structs_types = array(); // The stack used to keep track of if things are structs or array |
| 550 |
$xmlrpc_message->current_struct_name = array(); // A stack as well |
| 551 |
$xmlrpc_message->message = $message; |
| 552 |
return $xmlrpc_message; |
| 553 |
} |
| 554 |
|
| 555 |
/** |
| 556 |
* Parse an XML-RPC message. If parsing fails, the faultCode and faultString |
| 557 |
* will be added to the message object. |
| 558 |
* |
| 559 |
* @param $xmlrpc_message |
| 560 |
* Object generated by xmlrpc_message() |
| 561 |
* @return |
| 562 |
* TRUE if parsing succeeded; FALSE otherwise |
| 563 |
*/ |
| 564 |
function xmlrpc_message_parse(&$xmlrpc_message) { |
| 565 |
// First remove the XML declaration |
| 566 |
$xmlrpc_message->message = preg_replace('/<\?xml(.*)?\?'.'>/', '', $xmlrpc_message->message); |
| 567 |
if (trim($xmlrpc_message->message) == '') { |
| 568 |
return FALSE; |
| 569 |
} |
| 570 |
$xmlrpc_message->_parser = xml_parser_create(); |
| 571 |
// Set XML parser to take the case of tags into account. |
| 572 |
xml_parser_set_option($xmlrpc_message->_parser, XML_OPTION_CASE_FOLDING, FALSE); |
| 573 |
// Set XML parser callback functions |
| 574 |
xml_set_element_handler($xmlrpc_message->_parser, array('MollomPlugin', 'xmlrpc_message_tag_open'), array('MollomPlugin', 'xmlrpc_message_tag_close')); |
| 575 |
xml_set_character_data_handler($xmlrpc_message->_parser, array('MollomPlugin', 'xmlrpc_message_cdata')); |
| 576 |
$this->xmlrpc_message_set($xmlrpc_message); |
| 577 |
if (!xml_parse($xmlrpc_message->_parser, $xmlrpc_message->message)) { |
| 578 |
return FALSE; |
| 579 |
} |
| 580 |
xml_parser_free($xmlrpc_message->_parser); |
| 581 |
// Grab the error messages, if any |
| 582 |
$xmlrpc_message = $this->xmlrpc_message_get(); |
| 583 |
if ($xmlrpc_message->messagetype == 'fault') { |
| 584 |
$xmlrpc_message->fault_code = $xmlrpc_message->params[0]['faultCode']; |
| 585 |
$xmlrpc_message->fault_string = $xmlrpc_message->params[0]['faultString']; |
| 586 |
} |
| 587 |
return TRUE; |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Store a copy of the $xmlrpc_message object temporarily. |
| 592 |
* |
| 593 |
* @param $value |
| 594 |
* Object |
| 595 |
* @return |
| 596 |
* The most recently stored $xmlrpc_message |
| 597 |
*/ |
| 598 |
function xmlrpc_message_set($value = NULL) { |
| 599 |
static $xmlrpc_message; |
| 600 |
if ($value) { |
| 601 |
$xmlrpc_message = $value; |
| 602 |
} |
| 603 |
return $xmlrpc_message; |
| 604 |
} |
| 605 |
|
| 606 |
function xmlrpc_message_get() { |
| 607 |
return $this->xmlrpc_message_set(); |
| 608 |
} |
| 609 |
|
| 610 |
function xmlrpc_message_tag_open($parser, $tag, $attr) { |
| 611 |
$xmlrpc_message = $this->xmlrpc_message_get(); |
| 612 |
$xmlrpc_message->current_tag_contents = ''; |
| 613 |
$xmlrpc_message->last_open = $tag; |
| 614 |
switch ($tag) { |
| 615 |
case 'methodCall': |
| 616 |
case 'methodResponse': |
| 617 |
case 'fault': |
| 618 |
$xmlrpc_message->messagetype = $tag; |
| 619 |
break; |
| 620 |
// Deal with stacks of arrays and structs |
| 621 |
case 'data': |
| 622 |
$xmlrpc_message->array_structs_types[] = 'array'; |
| 623 |
$xmlrpc_message->array_structs[] = array(); |
| 624 |
break; |
| 625 |
case 'struct': |
| 626 |
$xmlrpc_message->array_structs_types[] = 'struct'; |
| 627 |
$xmlrpc_message->array_structs[] = array(); |
| 628 |
break; |
| 629 |
} |
| 630 |
$this->xmlrpc_message_set($xmlrpc_message); |
| 631 |
} |
| 632 |
|
| 633 |
function xmlrpc_message_cdata($parser, $cdata) { |
| 634 |
$xmlrpc_message = $this->xmlrpc_message_get(); |
| 635 |
$xmlrpc_message->current_tag_contents .= $cdata; |
| 636 |
$this->xmlrpc_message_set($xmlrpc_message); |
| 637 |
} |
| 638 |
|
| 639 |
function xmlrpc_message_tag_close($parser, $tag) { |
| 640 |
$xmlrpc_message = $this->xmlrpc_message_get(); |
| 641 |
$value_flag = FALSE; |
| 642 |
switch ($tag) { |
| 643 |
case 'int': |
| 644 |
case 'i4': |
| 645 |
$value = (int)trim($xmlrpc_message->current_tag_contents); |
| 646 |
$value_flag = TRUE; |
| 647 |
break; |
| 648 |
case 'double': |
| 649 |
$value = (double)trim($xmlrpc_message->current_tag_contents); |
| 650 |
$value_flag = TRUE; |
| 651 |
break; |
| 652 |
case 'string': |
| 653 |
$value = $xmlrpc_message->current_tag_contents; |
| 654 |
$value_flag = TRUE; |
| 655 |
break; |
| 656 |
case 'dateTime.iso8601': |
| 657 |
$value = xmlrpc_date(trim($xmlrpc_message->current_tag_contents)); |
| 658 |
// $value = $iso->getTimestamp(); |
| 659 |
$value_flag = TRUE; |
| 660 |
break; |
| 661 |
case 'value': |
| 662 |
// If no type is indicated, the type is string |
| 663 |
// We take special care for empty values |
| 664 |
if (trim($xmlrpc_message->current_tag_contents) != '' || (isset($xmlrpc_message->last_open) && ($xmlrpc_message->last_open == 'value'))) { |
| 665 |
$value = (string)$xmlrpc_message->current_tag_contents; |
| 666 |
$value_flag = TRUE; |
| 667 |
} |
| 668 |
unset($xmlrpc_message->last_open); |
| 669 |
break; |
| 670 |
case 'boolean': |
| 671 |
$value = (boolean)trim($xmlrpc_message->current_tag_contents); |
| 672 |
$value_flag = TRUE; |
| 673 |
break; |
| 674 |
case 'base64': |
| 675 |
$value = base64_decode(trim($xmlrpc_message->current_tag_contents)); |
| 676 |
$value_flag = TRUE; |
| 677 |
break; |
| 678 |
// Deal with stacks of arrays and structs |
| 679 |
case 'data': |
| 680 |
case 'struct': |
| 681 |
$value = array_pop($xmlrpc_message->array_structs ); |
| 682 |
array_pop($xmlrpc_message->array_structs_types); |
| 683 |
$value_flag = TRUE; |
| 684 |
break; |
| 685 |
case 'member': |
| 686 |
array_pop($xmlrpc_message->current_struct_name); |
| 687 |
break; |
| 688 |
case 'name': |
| 689 |
$xmlrpc_message->current_struct_name[] = trim($xmlrpc_message->current_tag_contents); |
| 690 |
break; |
| 691 |
case 'methodName': |
| 692 |
$xmlrpc_message->methodname = trim($xmlrpc_message->current_tag_contents); |
| 693 |
break; |
| 694 |
} |
| 695 |
if ($value_flag) { |
| 696 |
if (count($xmlrpc_message->array_structs ) > 0) { |
| 697 |
// Add value to struct or array |
| 698 |
if ($xmlrpc_message->array_structs_types[count($xmlrpc_message->array_structs_types)-1] == 'struct') { |
| 699 |
// Add to struct |
| 700 |
$xmlrpc_message->array_structs [count($xmlrpc_message->array_structs )-1][$xmlrpc_message->current_struct_name[count($xmlrpc_message->current_struct_name)-1]] = $value; |
| 701 |
} |
| 702 |
else { |
| 703 |
// Add to array |
| 704 |
$xmlrpc_message->array_structs [count($xmlrpc_message->array_structs )-1][] = $value; |
| 705 |
} |
| 706 |
} |
| 707 |
else { |
| 708 |
// Just add as a parameter |
| 709 |
$xmlrpc_message->params[] = $value; |
| 710 |
} |
| 711 |
} |
| 712 |
if (!in_array($tag, array("data", "struct", "member"))) { |
| 713 |
$xmlrpc_message->current_tag_contents = ''; |
| 714 |
} |
| 715 |
$this->xmlrpc_message_set($xmlrpc_message); |
| 716 |
} |
| 717 |
|
| 718 |
/** |
| 719 |
* Construct an object representing an XML-RPC request |
| 720 |
* |
| 721 |
* @param $method |
| 722 |
* The name of the method to be called |
| 723 |
* @param $args |
| 724 |
* An array of parameters to send with the method. |
| 725 |
* @return |
| 726 |
* Object |
| 727 |
*/ |
| 728 |
function xmlrpc_request($method, $args) { |
| 729 |
$xmlrpc_request = new stdClass(); |
| 730 |
$xmlrpc_request->method = $method; |
| 731 |
$xmlrpc_request->args = $args; |
| 732 |
$xmlrpc_request->xml = <<<EOD |
| 733 |
<?xml version="1.0"?> |
| 734 |
<methodCall> |
| 735 |
<methodName>{$xmlrpc_request->method}</methodName> |
| 736 |
<params> |
| 737 |
|
| 738 |
EOD; |
| 739 |
foreach ($xmlrpc_request->args as $arg) { |
| 740 |
$xmlrpc_request->xml .= '<param><value>'; |
| 741 |
$v = $this->xmlrpc_value($arg); |
| 742 |
$xmlrpc_request->xml .= $this->xmlrpc_value_get_xml($v); |
| 743 |
$xmlrpc_request->xml .= "</value></param>\n"; |
| 744 |
} |
| 745 |
$xmlrpc_request->xml .= '</params></methodCall>'; |
| 746 |
return $xmlrpc_request; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
function xmlrpc_error($code = NULL, $message = NULL, $reset = FALSE) { |
| 751 |
static $xmlrpc_error; |
| 752 |
if (isset($code)) { |
| 753 |
$xmlrpc_error = new stdClass(); |
| 754 |
$xmlrpc_error->is_error = TRUE; |
| 755 |
$xmlrpc_error->code = $code; |
| 756 |
$xmlrpc_error->message = $message; |
| 757 |
} |
| 758 |
elseif ($reset) { |
| 759 |
$xmlrpc_error = NULL; |
| 760 |
} |
| 761 |
return $xmlrpc_error; |
| 762 |
} |
| 763 |
|
| 764 |
function xmlrpc_error_get_xml($xmlrpc_error) { |
| 765 |
return <<<EOD |
| 766 |
<methodResponse> |
| 767 |
<fault> |
| 768 |
<value> |
| 769 |
<struct> |
| 770 |
<member> |
| 771 |
<name>faultCode</name> |
| 772 |
<value><int>{$xmlrpc_error->code}</int></value> |
| 773 |
</member> |
| 774 |
<member> |
| 775 |
<name>faultString</name> |
| 776 |
<value><string>{$xmlrpc_error->message}</string></value> |
| 777 |
</member> |
| 778 |
</struct> |
| 779 |
</value> |
| 780 |
</fault> |
| 781 |
</methodResponse> |
| 782 |
|
| 783 |
EOD; |
| 784 |
} |
| 785 |
|
| 786 |
function xmlrpc_date($time) { |
| 787 |
$xmlrpc_date = new stdClass(); |
| 788 |
$xmlrpc_date->is_date = TRUE; |
| 789 |
// $time can be a PHP timestamp or an ISO one |
| 790 |
if (is_numeric($time)) { |
| 791 |
$xmlrpc_date->year = gmdate('Y', $time); |
| 792 |
$xmlrpc_date->month = gmdate('m', $time); |
| 793 |
$xmlrpc_date->day = gmdate('d', $time); |
| 794 |
$xmlrpc_date->hour = gmdate('H', $time); |
| 795 |
$xmlrpc_date->minute = gmdate('i', $time); |
| 796 |
$xmlrpc_date->second = gmdate('s', $time); |
| 797 |
$xmlrpc_date->iso8601 = gmdate('Ymd\TH:i:s', $time); |
| 798 |
} |
| 799 |
else { |
| 800 |
$xmlrpc_date->iso8601 = $time; |
| 801 |
$time = str_replace(array('-', ':'), '', $time); |
| 802 |
$xmlrpc_date->year = substr($time, 0, 4); |
| 803 |
$xmlrpc_date->month = substr($time, 4, 2); |
| 804 |
$xmlrpc_date->day = substr($time, 6, 2); |
| 805 |
$xmlrpc_date->hour = substr($time, 9, 2); |
| 806 |
$xmlrpc_date->minute = substr($time, 11, 2); |
| 807 |
$xmlrpc_date->second = substr($time, 13, 2); |
| 808 |
} |
| 809 |
return $xmlrpc_date; |
| 810 |
} |
| 811 |
|
| 812 |
function xmlrpc_date_get_xml($xmlrpc_date) { |
| 813 |
return '<dateTime.iso8601>'. $xmlrpc_date->year . $xmlrpc_date->month . $xmlrpc_date->day .'T'. $xmlrpc_date->hour .':'. $xmlrpc_date->minute .':'. $xmlrpc_date->second .'</dateTime.iso8601>'; |
| 814 |
} |
| 815 |
|
| 816 |
function xmlrpc_base64($data) { |
| 817 |
$xmlrpc_base64 = new stdClass(); |
| 818 |
$xmlrpc_base64->is_base64 = TRUE; |
| 819 |
$xmlrpc_base64->data = $data; |
| 820 |
return $xmlrpc_base64; |
| 821 |
} |
| 822 |
|
| 823 |
function xmlrpc_base64_get_xml($xmlrpc_base64) { |
| 824 |
return '<base64>'. base64_encode($xmlrpc_base64->data) .'</base64>'; |
| 825 |
} |
| 826 |
|
| 827 |
/** |
| 828 |
* Execute an XML remote procedural call. This is private function; call xmlrpc() |
| 829 |
* in common.inc instead of this function. |
| 830 |
* |
| 831 |
* @return |
| 832 |
* A $xmlrpc_message object if the call succeeded; FALSE if the call failed |
| 833 |
*/ |
| 834 |
function _xmlrpc() { |
| 835 |
$args = func_get_args(); |
| 836 |
$url = array_shift($args); |
| 837 |
$this->xmlrpc_clear_error(); |
| 838 |
if (is_array($args[0])) { |
| 839 |
$method = 'system.multicall'; |
| 840 |
$multicall_args = array(); |
| 841 |
foreach ($args[0] as $call) { |
| 842 |
$multicall_args[] = array('methodName' => array_shift($call), 'params' => $call); |
| 843 |
} |
| 844 |
$args = array($multicall_args); |
| 845 |
} |
| 846 |
else { |
| 847 |
$method = array_shift($args); |
| 848 |
} |
| 849 |
$xmlrpc_request = $this->xmlrpc_request($method, $args); |
| 850 |
$result = $this->http_request($url, array("Content-Type" => "text/xml"), 'POST', $xmlrpc_request->xml); |
| 851 |
if ($result->code != 200) { |
| 852 |
$this->xmlrpc_error($result->code, $result->error); |
| 853 |
return FALSE; |
| 854 |
} |
| 855 |
$message = $this->xmlrpc_message($result->data); |
| 856 |
// Now parse what we've got back |
| 857 |
if (!$this->xmlrpc_message_parse($message)) { |
| 858 |
// XML error |
| 859 |
$this->xmlrpc_error(-32700, t('Parse error. Not well formed')); |
| 860 |
return FALSE; |
| 861 |
} |
| 862 |
// Is the message a fault? |
| 863 |
if ($message->messagetype == 'fault') { |
| 864 |
$this->xmlrpc_error($message->fault_code, $message->fault_string); |
| 865 |
return FALSE; |
| 866 |
} |
| 867 |
// Message must be OK |
| 868 |
return $message->params[0]; |
| 869 |
} |
| 870 |
|
| 871 |
/** |
| 872 |
* Returns the last XML-RPC client error number |
| 873 |
*/ |
| 874 |
function xmlrpc_errno() { |
| 875 |
$error = $this->xmlrpc_error(); |
| 876 |
return ($error != NULL ? $error->code : NULL); |
| 877 |
} |
| 878 |
|
| 879 |
/** |
| 880 |
* Returns the last XML-RPC client error message |
| 881 |
*/ |
| 882 |
function xmlrpc_error_msg() { |
| 883 |
$error = xmlrpc_error(); |
| 884 |
return ($error != NULL ? $error->message : NULL); |
| 885 |
} |
| 886 |
|
| 887 |
/** |
| 888 |
* Clears any previous error. |
| 889 |
*/ |
| 890 |
function xmlrpc_clear_error() { |
| 891 |
$this->xmlrpc_error(NULL, NULL, TRUE); |
| 892 |
} |
| 893 |
|
| 894 |
} |