You are here

function include_check_path in Include 8

Same name and namespace in other branches
  1. 6 include.module \include_check_path()
  2. 7 include.module \include_check_path()

Verifies or installs a file or directory into the include repository.

Parameters

$path: The target path to install, relative to the include file root. If $path is empty or ends in a trailing slash, it is interpreted as a directory name, and both $source and $type are ignored.

$source: (optional) The file data, or a uri where it may be found. If unset or empty, then $path is a directory name, and $type is ignored.

$type: (optional) A string which determines how $source is to be interpreted. Must be one of the following:

  • dir: $path is a directory name, and $source is ignored.
  • file: (Default) drupal_realpath($source) is a local file.
  • url: url($source) is a downloadable url.
  • string: $source is a string containing the file data.

Return value

TRUE if the file was successfully installed; otherwise FALSE.

File

./include.module, line 136
The Include module manages files on the include_files path.

Code

function include_check_path($path, $source = NULL, $type = 'file') {
  if (empty($source) || substr($path, -1) === '/') {
    $type = 'dir';
  }
  $path = trim($path, '/');
  if (empty($path)) {
    $type = 'dir';
  }
  elseif ($path == '.') {
    $type = 'dir';
    $path = '';
  }
  elseif ($path == '..') {
    return TRUE;
  }
  elseif ($type != 'dir' && include_verify($path)) {
    return TRUE;
  }
  elseif (cache_get('include_error', 'cache_menu')) {

    // Something went wrong with previous installs; abort.
    return FALSE;
  }
  elseif ($type != 'dir' && !include_check_path(dirname($path))) {
    return FALSE;
  }
  $target = include_get_root() . '/' . $path;
  $args = array(
    '%function' => __FUNCTION__,
    '%source' => $source,
    '%path' => $path,
    '%target' => $target,
  );
  switch ($type) {
    case 'dir':
      if (!file_prepare_directory($target, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
        drupal_set_message(t('Could not create directory: %target. Check permissions.', $args), 'error');
        watchdog('include', 'Could not create directory: %target.', $args, WATCHDOG_ERROR);
        return FALSE;
      }
      break;
    case 'file':
      if (!($source_path = drupal_realpath($source))) {
        drupal_set_message(t('File not found: %source', $args), 'error');
        watchdog('include', 'File not found: %source', $args, WATCHDOG_ERROR);
        return FALSE;
      }
      elseif (!copy($source_path, $target)) {
        drupal_set_message(t('Could not copy file from (%source) to (%target). Check permissions.', $args), 'error');
        watchdog('include', 'Could not copy file from (%source) to (%target)', $args, WATCHDOG_ERROR);
        return FALSE;
      }
      break;
    case 'url':
      if (!($args['%source'] = url($source))) {
        drupal_set_message(t('Could not resolve (%source) to a download url.', $args), 'error');
        watchdog('include', 'Could not resolve (%source) to a download url.', $args, WATCHDOG_ERROR);
        return FALSE;
      }
      elseif (!($response = drupal_http_request($args['%source']))) {
        drupal_set_message(t('Could not open url (%source).', $args), 'error');
        watchdog('include', 'Could not open url (%source).', $args, WATCHDOG_ERROR);
        return FALSE;
      }
      elseif (!empty($response->error)) {
        $args['@error'] = $response->error;
        drupal_set_message(t('Error downloading (%source): @error', $args), 'error');
        watchdog('include', 'Error downloading (%source): @error', $args, WATCHDOG_ERROR);
        return FALSE;
      }
      $source = $response->data;

    // Fall-through and share code with case 'string'.
    case 'string':
      if (!($args['%count'] = strlen($source))) {
        drupal_set_message(t('Source file at (%source) is empty.', $args), 'error');
        watchdog('include', 'Source file at (%source) is empty.', $args, WATCHDOG_ERROR);
        return FALSE;
      }
      elseif ($args['%count'] != file_put_contents($target, $source)) {
        drupal_set_message(t('Could not write (%count) bytes to file: (%target).  Disk full?', $args), 'error');
        watchdog('include', 'Could not write (%count) bytes to file: (%target).', $args, WATCHDOG_ERROR);
        return FALSE;
      }
      break;
    default:

      // Modules may implement hook_include_install_HOOK().
      $modules = module_implements("include_install_{$type}");
      if (empty($modules)) {
        drupal_set_message(t('No installer found for include type (%type).  Check your dependencies.', $args), 'error');
        watchdog('include', 'No installer found for include type (%type).', $args, WATCHDOG_CRITICAL);
        return FALSE;
      }
      foreach ($modules as $module) {
        $function = $module . '_include_install_' . $type;
        if (!$function($path, $source)) {

          // The hook function is responsible for its own error reporting.
          return FALSE;
        }
      }

      // Each hook function is responsible for calling
      // include_set_root() and include_verify() if necessary.
      return TRUE;
  }
  if (empty($error) && $type != 'dir') {

    // A file was installed; add the include file repository to the PHP
    // include_path and verify that the newly-installed file can be found.
    include_set_root();
    if (!include_verify($path, FALSE)) {
      include_clear_root();
      $args['%dir'] = include_get_root();
      $args['!include_path'] = url('http://php.net/manual/ini.core.php#ini.include-path');
      $args['!set_include_path'] = url('http://php.net/set_include_path');
      $args['!include'] = url('admin/modules', array(
        'fragment' => 'edit-modules-other-include-enable',
      ));
      drupal_set_message(t('The file at %path (realpath %target) could not be found, even after adding %dir to the PHP <a href="!include_path">include_path</a>. Your hosting provider may have disabled the <a href="!set_include_path">set_include_path</a> function. An error flag has been set, and no further attempts will be made, until the <a href="!include">Include</a> module is disabled and then re-enabled.', $args), 'error');
      watchdog('include', 'Error including a downloaded file.  Auto-downloading has been disabled.', WATCHDOG_CRITICAL);
      return FALSE;
    }
    else {
      drupal_set_message(t('Created include %path.', $args));
    }
  }
  if (empty($error)) {
    return TRUE;
  }
  watchdog('include', "Error in %function function: {$error}", $args, $error_level);
  return FALSE;
}