You are here

function mailmime_requirements in Mail MIME 7

Same name and namespace in other branches
  1. 6 mailmime.install \mailmime_requirements()

Implements hook_requirements().

Creates a PEAR directory to hold downloaded PEAR modules.

Adds the PEAR directory to the front of the include path.

Ensures the availability of requisite PEAR module files by downloading them to the local PEAR directory if necessary.

Returns an appropriate error message if any of the above failed.

File

./mailmime.install, line 20
Ensures certain PEAR modules are available by downloading them if necessary.

Code

function mailmime_requirements($phase) {
  $errors = array();
  $warnings = array();
  $pear = drupal_realpath(file_build_uri('PEAR'));
  $t = get_t();
  if (!file_prepare_directory($pear, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    $warnings[] = $t('Could not create directory %pear', array(
      '%pear' => $pear,
    ));
    $pear = NULL;
  }
  else {
    set_include_path(implode(PATH_SEPARATOR, array_unique(array_merge(array(
      $pear,
    ), explode(PATH_SEPARATOR, get_include_path())))));
  }
  $repository = 'http://svn.php.net/repository/pear/';
  $packages = mailmime_required_PEAR_packages();
  foreach ($packages as $name => $package) {
    $local = $package['local_path'];
    if ($pear) {
      $packagedir = "{$pear}/{$local}";
      if (!file_prepare_directory($packagedir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
        $warnings[] = $t('Could not create directory %local', array(
          '%local' => $packagedir,
        ));
        continue;
      }
      $remote = $package['remote_path'];
    }
    foreach ($package['files'] as $file) {
      $include = "{$local}{$file}";

      // This searches the include_path for $include.
      if ($fd = @fopen($include, 'r', TRUE)) {
        fclose($fd);
        continue;
      }
      if ($pear) {
        $url = "{$repository}{$remote}{$file}";
        $filename = "{$pear}/{$include}";
        if (!($response = drupal_http_request($url)) || !isset($response->data)) {
          $errors[] = $t('Could not open %url.', array(
            '%url' => $url,
          ));
        }
        elseif (!($count = strlen($response->data))) {
          $errors[] = $t('%url returned zero bytes.', array(
            '%url' => $url,
          ));
        }
        elseif ($count != file_put_contents($filename, $response->data)) {
          $errors[] = $t('Could not write %count bytes to %filename.', array(
            '%count' => $count,
            '%filename' => $filename,
          ));
        }
        elseif ($fd = @fopen($include, 'r', TRUE)) {
          fclose($fd);
          continue;
        }
        $errors[] = $t('Installed but could not load %package file %include. ' . 'Add %pear to your %include_path.', array(
          '%package' => "PEAR::{$name}",
          '%include' => $include,
          '%pear' => $pear,
          '%include_path' => 'include_path',
        ));
      }
      else {
        $errors[] = $t('Could not find or download %package file %include.', array(
          '%include' => $include,
          '%package' => "PEAR::{$name}",
        ));
      }
    }
  }
  if (empty($errors) && empty($warnings)) {
    return array();
  }
  return array(
    'MailMIME' => array(
      'title' => $t('Automatic PEAR Mail_Mime package installation'),
      'description' => implode('<br />', $warnings + $errors),
      'severity' => empty($errors) ? REQUIREMENT_WARNING : REQUIREMENT_ERROR,
    ),
  );
}