diff --git a/httprl.module b/httprl.module old mode 100644 new mode 100755 index ad6faf2..0d9fb03 --- a/httprl.module +++ b/httprl.module @@ -234,39 +234,43 @@ function httprl_override_core($url, $options = array()) { /** * Helper function to build an URL for asynchronous requests to self. * - * @param string $path - * Path to a URI excluding everything to the left and including the base path. - * @param bool $detect_schema - * If TRUE it will see if this request is https; if so, it will set the full - * url to be https as well. - */ -function httprl_build_url_self($path = '', $detect_schema = FALSE) { - global $base_path; + * @param string $level + * How deep to go when setting the base path. + */ +function _httprl_build_drupal_root($level = 0) { + static $webroot; + $root_path = '/'; + if ($level > 0) { + // Work backwards from this file till we find drupal's index.php. + if (!isset($webroot)) { + $webroot = str_replace('\\', '/', dirname(__FILE__)); + while (!empty($webroot)) { + if (file_exists($webroot . '/index.php') && strpos(file_get_contents($webroot . '/index.php'), 'menu_execute_active_handler();') !== FALSE) { + break; + } + $new_webroot = str_replace('\\', '/', dirname($webroot)); + if ($new_webroot == $webroot) { + $webroot = str_replace('\\', '/', getcwd()); + break; + } + $webroot = $new_webroot; + } + } - if (!empty($base_path)) { - $root_path = $base_path; + $root_path = ''; + $webroot_array = explode('/', $webroot); + while ($level > 0 && count($webroot_array) != 0) { + $level--; + $root_path = array_pop($webroot_array) . '/' . $root_path; + } + $root_path = '/' . $root_path; } else { - // $_SERVER['SCRIPT_NAME'] can, in contrast to $_SERVER['PHP_SELF'], not - // be modified by a visitor. - if ($dir = trim(dirname($_SERVER['SCRIPT_NAME']), '\,/')) { - $root_path = "/$dir/"; - } - else { - $root_path = '/'; + if (!empty($GLOBALS['base_path'])) { + $root_path = $GLOBALS['base_path']; } } - // Fix root_path. - // With dirname() if there are no slashes in the path, a dot is returned, - if ($root_path == '/./') { - $root_path = '/'; - } - // If ran from the command line SCRIPT_NAME contains the working directory. - elseif (isset($_SERVER['argc']) || isset($_SERVER['argv'])) { - $cwd = isset($_SERVER['PWD']) ? $_SERVER['PWD'] : getcwd(); - $root_path = str_replace($cwd . '/', '', $root_path); - } // Server auth. $auth = ''; @@ -278,8 +282,14 @@ function httprl_build_url_self($path = '', $detect_schema = FALSE) { $ip = httprl_variable_get('httprl_server_addr', FALSE); if ($ip == -1) { $ip = $_SERVER['HTTP_HOST']; + // If the host is bad don't use it. + if (isset($_SERVER['argc']) || isset($_SERVER['argv'])) { + if (gethostbyname($_SERVER['HTTP_HOST']) == $_SERVER['HTTP_HOST']) { + $ip = ''; + } + } } - elseif (empty($ip)) { + if (empty($ip)) { $ip = empty($_SERVER['SERVER_ADDR']) ? '127.0.0.1' : $_SERVER['SERVER_ADDR']; // Check for IPv6. If IPv6 convert to IPv4 if possible. if (strpos($ip, ':') !== FALSE) { @@ -292,9 +302,15 @@ function httprl_build_url_self($path = '', $detect_schema = FALSE) { elseif (!empty($_SERVER['HTTP_HOST'])) { // Last option is to use the IP from the host name. $ip = gethostbyname($_SERVER['HTTP_HOST']); + if (gethostbyname($_SERVER['HTTP_HOST']) == $_SERVER['HTTP_HOST']) { + $ip = ''; + } } } } + if (empty($ip)) { + $ip = '127.0.0.1'; + } // Port. $port = ''; @@ -321,7 +337,52 @@ function httprl_build_url_self($path = '', $detect_schema = FALSE) { } } - return $schema . $auth . $ip . $port . $root_path . $path; + return $schema . $auth . $ip . $port . $root_path; +} + +/** + * Helper function to build an URL for asynchronous requests to self. + * + * @param string $path + * Path to a URI excluding everything to the left and including the base path. + * @param bool $detect_schema + * If TRUE it will see if this request is https; if so, it will set the full + * url to be https as well. + */ +function httprl_build_url_self($path = '', $detect_schema = FALSE) { + static $drupal_root; + if (!isset($drupal_root)) { + $drupal_root = _httprl_build_drupal_root(); + + // If ran from the command line, the drupal root might be in a subdir. Test to + // make sure we have the right directory. + if (isset($_SERVER['argc']) || isset($_SERVER['argv'])) { + $level = 0; + $found = FALSE; + while (!$found) { + // Trick due to not knowing the subdir. + // http://stackoverflow.com/questions/8361355/get-apache-document-root-from-command-line-execution-no-browser/8415235#8415235 + $headers = get_headers($drupal_root . 'httprl_async_function_callback'); + if (!empty($headers)) { + foreach ($headers as $header) { + if (stripos($header, 'X-HTTPRL') !== FALSE) { + $found = TRUE; + } + } + } + if (!$found) { + $level++; + $new_drupal_root = _httprl_build_drupal_root($level); + if ($new_drupal_root == $drupal_root) { + break; + } + $drupal_root = $new_drupal_root; + } + } + } + } + + return $drupal_root . $path; } /**