diff --git a/httprl.module b/httprl.module index d9ce669..1ce7cd5 100644 --- a/httprl.module +++ b/httprl.module @@ -181,25 +181,8 @@ function httprl_build_url_self($path = '', $detect_schema = FALSE) { $auth = $_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW'] . '@'; } - // Pull in the httprl_server_addr variable from the DB. - if (!isset($conf['httprl_server_addr']) && empty($variable_loaded)) { - if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) { - $result = db_query('SELECT value FROM {variable} WHERE name = :name', array(':name' => 'httprl_server_addr'))->fetchAssoc(); - if (!empty($result)) { - $conf['httprl_server_addr'] = unserialize($result['value']); - } - } - else { - $result = db_query("SELECT value FROM {variable} WHERE name = '%s'", 'httprl_server_addr'); - if (!empty($result)) { - $conf['httprl_server_addr'] = unserialize(db_result($result)); - } - } - $variable_loaded = TRUE; - } - - // Host. - $ip = variable_get('httprl_server_addr', FALSE); + // Get Host. + $ip = httprl_variable_get('httprl_server_addr', FALSE); if ($ip == -1) { $ip = $_SERVER['HTTP_HOST']; } @@ -250,14 +233,22 @@ function httprl_parse_url($url, &$result) { // Parse the URL and make sure we can handle the schema. $uri = @parse_url($url); + // If the t function is not available use httprl_pr. + if (function_exists('t')) { + $t = 't'; + } + else { + $t = 'httprl_pr'; + } + if (empty($uri)) { // Set error code for failed request. - $result->error = t('Unable to parse URL.'); + $result->error = $t('Unable to parse URL.'); $result->code = HTTPRL_URL_PARSE_ERROR; } elseif (!isset($uri['scheme'])) { // Set error code for failed request. - $result->error = t('Missing schema.'); + $result->error = $t('Missing schema.'); $result->code = HTTPRL_URL_MISSING_SCHEMA; } @@ -302,7 +293,9 @@ function httprl_set_default_options(&$options) { // Set referrer to current page. if (!isset($options['headers']['Referer']) && !empty($options['referrer'])) { - $options['headers']['Referer'] = $base_root . request_uri(); + if (function_exists('request_uri')) { + $options['headers']['Referer'] = $base_root . request_uri(); + } } // stream_socket_client() requires timeout to be a float. @@ -324,7 +317,7 @@ function httprl_set_default_options(&$options) { */ function httprl_setup_proxy(&$uri, &$options, $url) { // Proxy setup - $proxy_server = variable_get('proxy_server', ''); + $proxy_server = httprl_variable_get('proxy_server', ''); // Use a proxy if one is defined and the host is not on the excluded list. if ($proxy_server && _httprl_use_proxy($uri['host'])) { // Set the scheme so we open a socket to the proxy server. @@ -335,13 +328,13 @@ function httprl_setup_proxy(&$uri, &$options, $url) { unset($uri['query']); // Add in username and password to Proxy-Authorization header if needed. - if ($proxy_username = variable_get('proxy_username', '')) { - $proxy_password = variable_get('proxy_password', ''); + if ($proxy_username = httprl_variable_get('proxy_username', '')) { + $proxy_password = httprl_variable_get('proxy_password', ''); $options['headers']['Proxy-Authorization'] = 'Basic ' . base64_encode($proxy_username . (!empty($proxy_password) ? ":" . $proxy_password : '')); } // Some proxies reject requests with any User-Agent headers, while others // require a specific one. - $proxy_user_agent = variable_get('proxy_user_agent', ''); + $proxy_user_agent = httprl_variable_get('proxy_user_agent', ''); // The default value matches neither condition. if (is_null($proxy_user_agent)) { unset($options['headers']['User-Agent']); @@ -373,7 +366,7 @@ function httprl_set_socket($uri, &$options, $proxy_server, &$result) { switch ($uri['scheme']) { case 'proxy': // Make the socket connection to a proxy server. - $socket = 'tcp://' . $proxy_server . ':' . variable_get('proxy_port', 8080); + $socket = 'tcp://' . $proxy_server . ':' . httprl_variable_get('proxy_port', 8080); // The Host header still needs to match the real request. $options['headers']['Host'] = $uri['host']; $options['headers']['Host'] .= isset($uri['port']) && $uri['port'] != 80 ? ':' . $uri['port'] : ''; @@ -407,7 +400,15 @@ function httprl_set_socket($uri, &$options, $proxy_server, &$result) { break; default: - $result->error = t('Invalid schema @scheme.', array('@scheme' => $uri['scheme'])); + // If the t function is not available use httprl_pr. + if (function_exists('t')) { + $t = 't'; + } + else { + $t = 'httprl_pr'; + } + + $result->error = $t('Invalid schema @scheme.', array('@scheme' => $uri['scheme'])); $result->code = HTTPRL_URL_INVALID_SCHEMA; } @@ -622,28 +623,37 @@ function httprl_build_request_string($uri, $options) { * An object for httprl_send_request. */ function httprl_stream_connection_error_formatter($errno, $errstr, &$result) { - // Make sure drupal_convert_to_utf8() is available. - if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) { - require_once DRUPAL_ROOT . '/includes/unicode.inc'; + // If the t function is not available use httprl_pr. + if (function_exists('t')) { + $t = 't'; + // Make sure drupal_convert_to_utf8() is available. + if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) { + require_once DRUPAL_ROOT . '/includes/unicode.inc'; + } + else { + require_once './includes/unicode.inc'; + } + + // Convert error message to utf-8. Using ISO-8859-1 (Latin-1) as source + // encoding could be wrong; it is a simple workaround :) + $errstr = trim(drupal_convert_to_utf8($errstr, 'ISO-8859-1')); } else { - require_once './includes/unicode.inc'; + $t = 'httprl_pr'; } - // Convert error message to utf-8. Using ISO-8859-1 (Latin-1) as source - // encoding could be wrong; it is a simple workaround :) - $errstr = trim(drupal_convert_to_utf8($errstr, 'ISO-8859-1')); + if (!$errno) { // If $errno is 0, it is an indication that the error occurred // before the connect() call. This is most likely due to a problem // initializing the stream. $result->code = HTTPRL_ERROR_INITIALIZING_STREAM; - $result->error = !empty($errstr) ? $errstr : t('Error initializing socket @socket.', array('@socket' => $result->socket)); + $result->error = !empty($errstr) ? $errstr : $t('Error initializing socket @socket.', array('@socket' => $result->socket)); } else { // When a network error occurs, we use a negative number so it does not // clash with the HTTP status codes. $result->code = (int) -$errno; - $result->error = !empty($errstr) ? $errstr : t('Error opening socket @socket.', array('@socket' => $result->socket)); + $result->error = !empty($errstr) ? $errstr : $t('Error opening socket @socket.', array('@socket' => $result->socket)); } } @@ -913,6 +923,14 @@ function httprl_send_request($results = NULL) { return FALSE; } + // If the t function is not available use httprl_pr. + if (function_exists('t')) { + $t = 't'; + } + else { + $t = 'httprl_pr'; + } + // Create output array. $output = array(); // Remove errors from responses array and set the global timeout. @@ -971,14 +989,14 @@ function httprl_send_request($results = NULL) { if ($global_time <= 0) { // Function timed out & the request is not done. if ($result->status == 'in progress') { - $result->error = t('Function timed out. Write.'); + $result->error = $t('Function timed out. Write.'); // If stream is not done writing, then remove one from the write count. if (isset($result->fp)) { $stream_write_count--; } } else { - $result->error = t('Function timed out.'); + $result->error = $t('Function timed out.'); } $result->code = HTTPRL_FUNCTION_TIMEOUT; $result->status = 'Done.'; @@ -1002,15 +1020,15 @@ function httprl_send_request($results = NULL) { // Connection was dropped or connection timed out. if ($timeout <= 0 || empty($socket_name)) { - $result->error = t('Connection timed out. If you believe this is a false error, turn off async_connect in the httprl options array and try again.'); + $result->error = $t('Connection timed out. If you believe this is a false error, turn off async_connect in the httprl options array and try again.'); // Stream timed out & the request is not done. if ($result->status == 'in progress') { - $result->error .= t(' Write.'); + $result->error .= $t(' Write.'); // If stream is not done writing, then remove one from the write count. $stream_write_count--; } else { - $result->error = t(' Read.'); + $result->error .= $t(' Read.'); } $result->code = HTTPRL_REQUEST_TIMEOUT; $result->status = 'Done.'; @@ -1154,7 +1172,7 @@ function httprl_send_request($results = NULL) { $alive = !$info['eof'] && !feof($r) && !$info['timed_out'] && httprl_strlen($chunk); if (!$alive) { if ($responses[$id]->status == 'in progress') { - $responses[$id]->error = 'Connection refused by destination. Write.'; + $responses[$id]->error = $t('Connection refused by destination. Write.'); $responses[$id]->code = HTTPRL_CONNECTION_REFUSED; } $responses[$id]->status = 'Done.'; @@ -1196,7 +1214,7 @@ function httprl_send_request($results = NULL) { // See if we are done with writing. if ($bytes === FALSE) { // fwrite failed. - $responses[$id]->error = 'fwrite() failed.'; + $responses[$id]->error = $t('fwrite() failed.'); $responses[$id]->code = HTTPRL_REQUEST_FWRITE_FAIL; $responses[$id]->status = 'Done.'; $stream_write_count--; @@ -1241,7 +1259,7 @@ function httprl_send_request($results = NULL) { // 2.5+ seconds, error out. foreach ($this_run as $id => $fp) { // stream_select timed out & the request is not done. - $responses[$id]->error = 'stream_select() timed out.'; + $responses[$id]->error = $t('stream_select() timed out.'); $responses[$id]->code = HTTPRL_STREAM_SELECT_TIMEOUT; $responses[$id]->status = 'Done.'; @@ -1281,6 +1299,14 @@ function httprl_parse_data(&$result) { continue; } + // If the t function is not available use httprl_pr. + if (function_exists('t')) { + $t = 't'; + } + else { + $t = 'httprl_pr'; + } + // Parse response headers from the response body. // Be tolerant of malformed HTTP responses that separate header and body with // \n\n or \r\r instead of \r\n\r\n. @@ -1423,7 +1449,7 @@ function httprl_parse_data(&$result) { // Error out if we hit the max redirect. if ($result->options['max_redirects'] <= 0) { $result->code = HTTPRL_REQUEST_ALLOWED_REDIRECTS_EXHAUSTED; - $result->error = t('Maximum allowed redirects exhausted.'); + $result->error = $t('Maximum allowed redirects exhausted.'); } else { // Redirect to the new location. @@ -1688,7 +1714,7 @@ function httprl_queue_background_callback(&$args, &$result = NULL) { unset($locks[$id]); // Remove the lock_id reference in the database. - if (variable_get('lock_inc', './includes/lock.inc') === './includes/lock.inc') { + if (httprl_variable_get('lock_inc', './includes/lock.inc') === './includes/lock.inc') { if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) { db_update('semaphore') ->fields(array('value' => 'httprl')) @@ -1966,7 +1992,7 @@ function httprl_fast403() { * The name of the lock. */ function httprl_lock_release($name) { - if (variable_get('lock_inc', './includes/lock.inc') !== './includes/lock.inc') { + if (httprl_variable_get('lock_inc', './includes/lock.inc') !== './includes/lock.inc') { lock_release($name); } else { @@ -2031,7 +2057,7 @@ function httprl_pr($data) { * TRUE if a proxy should be used for this host. */ function _httprl_use_proxy($host) { - $proxy_exceptions = variable_get('proxy_exceptions', array('localhost', '127.0.0.1')); + $proxy_exceptions = httprl_variable_get('proxy_exceptions', array('localhost', '127.0.0.1')); return !in_array(strtolower($host), $proxy_exceptions, TRUE); } @@ -2054,19 +2080,36 @@ function _httprl_use_proxy($host) { * @see variable_del(), variable_set() */ function httprl_variable_get($name, $default = NULL) { - if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) { - $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable} WHERE name = :name', array(':name' => $name))->fetchAllKeyed()); - return isset($variables[$name]) ? $variables[$name] : $default; + // Try global configuration variable first. + global $conf; + if (isset($conf[$name])) { + return $conf[$name]; } - else { - $result = db_query("SELECT value FROM {variable} WHERE name = '%s'", $name); - if (!empty($result)) { - $result = db_result($result); + + // Try database next if not at a full bootstrap level. + if (function_exists('db_query') && !httprl_drupal_full_bootstrap()) { + if (defined('VERSION') && substr(VERSION, 0, 1) >= 7) { + $variables = array_map('unserialize', db_query('SELECT name, value FROM {variable} WHERE name = :name', array(':name' => $name))->fetchAllKeyed()); + + // Use the default if need be. + return isset($variables[$name]) ? $variables[$name] : $default; + } + else { + $result = db_query("SELECT value FROM {variable} WHERE name = '%s'", $name); if (!empty($result)) { - $value = unserialize($result); + $result = db_result($result); + if (!empty($result)) { + $value = unserialize($result); + } } + + // Use the default if need be. + return isset($value) ? $value : $default; } - return isset($value) ? $value : $default; + } + else { + // Return default if database is not available or if at a full bootstrap. + return $default; } }