Skip to content
httprl.install 5.34 KiB
Newer Older
<?php

/**
 * @file
 * Handle HTTP Parallel Request Library installation and upgrade tasks.
 */

/**
 * Implementation of hook_enable().
 */
function httprl_enable() {
}

/**
 * Implementation of hook_disable().
 */
function httprl_disable() {
}

/**
 * Implementation of hook_install().
 */
function httprl_install() {
}

/**
 * Implementation of hook_uninstall().
 */
function httprl_uninstall() {
}

/**
 * Implementation of hook_requirements().
 */
function httprl_requirements($phase) {
  $requirements = array();
  // Ensure translations don't break at install time
  $t = get_t();

  if ($phase == 'runtime' || $phase == 'install') {
    $function_list = array(
      'stream_socket_client',
      'stream_select',
      'stream_set_blocking',
      'stream_get_meta_data',
    );
    // Check each function to make sure it exists.
    foreach ($function_list as $function_name) {
      if (!function_exists($function_name)) {
        $requirements['httprl_function_' . $function_name] = array(
          'title' => $t('HTTPRL'),
          'value' => $phase == 'install' ? FALSE : $function_name,
          'severity' => REQUIREMENT_ERROR,
          'description' => $t('<a href="!url">%name()</a> is disabled on this server. Please contact your hosting provider and see if they can re-enable this function for you.', array(
            '!url' => 'http://php.net/' . str_replace('_', '-', $function_name),
            '%name' => $function_name,
          )),
        );
      }
    }
  }
  if ($phase == 'runtime') {
    $url_path = 'admin/httprl-test';
    // Check that the menu router handler is working. If it's not working the rest
    // of the tests are pointless.
    $item = menu_get_item($url_path);
    if (empty($item['page_callback']) || strpos($item['page_callback'], 'httprl') === FALSE) {
      $item = str_replace('    ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r($item, TRUE))));
      $requirements['httprl_callback'] = array(
        'title'       => $t('HTTPRL - Menu Callback'),
        'severity'    => REQUIREMENT_WARNING,
        'value'       => $t('Flush your caches'),
        'description' => $t('You need to flush your menu cache. The test callback for httprl is not there.'),
      );
      return $requirements;
    }

    // Test the non blocking url
    global $base_root, $base_path, $conf;
    $id = 'httprl_' . md5(mt_rand() . time());
    // Build URL to point to admin/httprl-test on this server.
    $url = httprl_build_url_self($url_path . '?key=' . md5(drupal_get_private_key()) . '&id=' . $id);

    // Set the headers to point to this hostname.
    $headers = array(
      'Host' => $_SERVER['HTTP_HOST'],
    );

    // Add in the headers and set the blocking mode.
    $options = array(
      'headers' => $headers,
      'blocking' => FALSE,
      'timeout' => 6.0,
    );

    // Start the timer & Get a lock.
    timer_start($id);
    lock_acquire($id, 6);

    // Queue up the requests & execute them.
    $requests = httprl_send_request();
    // Wait for the lock and stop the timer.
    lock_wait($id);
    $time = timer_stop($id);
    if (($time['time'] / 1000) > 5) {
      $ip = variable_get('httprl_server_addr', FALSE);
      $lasttime = '';
      $lastrequests = '';
      if (empty($ip)) {
        $lasttime = $time;
        $lastrequests = $requests;
        $conf['httprl_server_addr'] = -1;

        // If lock failed to release in time, try not using an IP when calling self.
        $id = 'httprl_' . md5(mt_rand() . time());
        // Build URL to point to admin/httprl-test on this server.


        $url = httprl_build_url_self($url_path . '?key=' . md5(drupal_get_private_key()) . '&id=' . $id);

        // Set the headers to point to this hostname.
        $headers = array(
          'Host' => $_SERVER['HTTP_HOST'],
        );

        // Add in the headers and set the blocking mode.
        $options = array(
          'headers' => $headers,
          'blocking' => FALSE,
          'timeout' => 6.0,
        );

        // Start the timer & Get a lock.
        timer_start($id);
        lock_acquire($id, 6);

        // Queue up the requests & execute them.
        httprl_request($url, $options);
        $requests = httprl_send_request();

        // Wait for the lock and stop the timer.
        lock_wait($id);
        $time = timer_stop($id);
      }
      if (($time['time'] / 1000) > 5) {
        // Lock timed out and was not reset by the URL ping.
        $requirements['httprl_nonblocking'] = array(
          'title'       => $t('HTTPRL - Non Blocking'),
          'severity'    => REQUIREMENT_WARNING,
          'value'       => $t('This server does not handle hanging connections.'),
          'description' => $t('Using non blocking mode on this server my not work correctly. More info: !info', array('!info' => str_replace('    ', '&nbsp;&nbsp;&nbsp;&nbsp;', nl2br(htmlentities(print_r(array($time, $requests, $lasttime, $lastrequests), TRUE)))))),
        );
      }
      else {
        // Request worked when using the hostname not the ip.
        variable_set('httprl_server_addr', -1);
      }
  if (empty($requirements)) {
    $requirements['httprl'] = array(
      'title'     => $t('HTTPRL'),
      'severity'  => REQUIREMENT_OK,
      'value'     => $phase == 'install' ? TRUE : $t('All the required functions are enabled and non blocking requests are working.'),
    );
  }