You are here

mailmime.install in Mail MIME 7

Same filename and directory in other branches
  1. 6.2 mailmime.install
  2. 6 mailmime.install

Ensures certain PEAR modules are available by downloading them if necessary.

File

mailmime.install
View source
<?php

/**
 * @file
 * Ensures certain PEAR modules are available by downloading them if necessary.
 */

/**
 * 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.
 */
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,
    ),
  );
}

/**
 * Return an array of required PEAR packages and files.
 *
 * @return
 *   An array of package items keyed by package name.  Each package item is
 *   an associative array with the following keys:
 *   - local_path: The path relative to the local PEAR repository where the
 *     package files are (to be) installed.
 *   - remote_path: The url path relative to the remote PEAR repository root
 *     where the package source files may be downloaded.
 *   - files: An array of filenames to be read and/or downloaded.
 */
function mailmime_required_PEAR_packages() {
  return array(
    'Mail_mime' => array(
      'local_path' => 'Mail/',
      'remote_path' => 'packages/Mail_Mime/trunk/',
      'files' => array(
        'mime.php',
        'mimeDecode.php',
        'mimePart.php',
        'package.xml',
        'xmail.dtd',
        'xmail.xsl',
      ),
    ),
    'PEAR' => array(
      'local_path' => '',
      'remote_path' => 'pear-core/trunk/',
      'files' => array(
        'PEAR.php',
      ),
    ),
  );
}

Functions

Namesort descending Description
mailmime_required_PEAR_packages Return an array of required PEAR packages and files.
mailmime_requirements Implements hook_requirements().