You are here

function apps_retrieve_app_image in Apps 7

Retrieve the image from the manifest.

Parameters

string $app: The app machine name

string $url: The url or local path of the image

string $title: The title of the image

string $alt: The alt text of the image

Return value

array A file object for the download file or FALSE if no image was downloaded

1 call to apps_retrieve_app_image()
apps_request_manifest_image_process in ./apps.manifest.inc
Process images and size with images styles.

File

./apps.manifest.inc, line 321
Handles pulling and processing of app data from the server

Code

function apps_retrieve_app_image($app, $url, $title = '', $alt = '') {

  // Allow for local files.
  $check = parse_url($url);
  $is_local = FALSE;
  if (!$check || !isset($check['host']) || !$check['host']) {
    $old_url = $url;

    // Quick check that file exists
    if (!file_exists($old_url)) {
      return FALSE;
    }
    $url = file_create_url($url);
    $is_local = TRUE;
  }
  $url_parts = explode("/", parse_url($url, PHP_URL_PATH));
  $file_name = array_pop($url_parts);
  $dir = "apps/{$app}";
  $uri = file_build_uri("{$dir}/{$file_name}");
  $current = FALSE;
  $fids = db_select('file_managed', 'f')
    ->condition('uri', $uri)
    ->fields('f', array(
    'fid',
  ))
    ->execute()
    ->fetchCol();
  if (!empty($fids) && isset($fids[0]) && is_numeric($fids[0])) {
    $current = file_load($fids[0]);
  }

  // Check to see if the remote file is newer than the one that.
  // We have and that the one we have still exists.
  if ($current && file_exists($current->uri)) {
    if ($is_local) {
      $use_current = filemtime($old_url) <= $current->timestamp && $current->filesize == filesize($old_url);
    }
    else {

      // Get the remote headers.
      $remote = drupal_http_request($url, array(
        'method' => 'HEAD',
      ));
      $remote = $remote->headers;
      $use_current = !empty($remote['last-modified']) && strtotime($remote['last-modified']) <= $current->timestamp && !empty($remote['content-length']) && $remote['content-length'] == $current->filesize;
    }
    if ($use_current) {
      $current->path = $current->uri;
      $current->title = !empty($title) ? $title : '';
      $current->alt = !empty($alt) ? $alt : $title;
      return (array) $current;
    }
  }
  $request = drupal_http_request($url, array(), 'GET');
  if (isset($request->data) && $request->code == 200) {
    $dir_uri = file_build_uri($dir);
    file_prepare_directory($dir_uri, FILE_CREATE_DIRECTORY || FILE_MODIFY_PERMISSIONS);
    $file = new stdClass();
    $file->filemime = $request->headers['content-type'];
    $file->uri = file_unmanaged_save_data($request->data, $uri, FILE_EXISTS_REPLACE);
    if (empty($file->uri)) {
      watchdog('apps', 'unable to save to @file', array(
        '@file' => $uri,
      ));
      return FALSE;
    }
    if ($current) {
      $file->fid = $current->fid;
    }
    $file->title = !empty($title) ? $title : '';
    $file->alt = !empty($alt) ? $alt : $file->title;
    $file->filename = $file_name;
    $file->status = 1;
    file_save($file);
    $file->path = $file->uri;
    return (array) $file;
  }
  return FALSE;
}