Projet

Général

Profil

Paste
Télécharger (34,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / update / update.compare.inc @ 01dfd3b5

1
<?php
2

    
3
/**
4
 * @file
5
 * Code required only when comparing available updates to existing data.
6
 */
7

    
8
/**
9
 * Fetches an array of installed and enabled projects.
10
 *
11
 * This is only responsible for generating an array of projects (taking into
12
 * account projects that include more than one module or theme). Other
13
 * information like the specific version and install type (official release,
14
 * dev snapshot, etc) is handled later in update_process_project_info() since
15
 * that logic is only required when preparing the status report, not for
16
 * fetching the available release data.
17
 *
18
 * This array is fairly expensive to construct, since it involves a lot of disk
19
 * I/O, so we cache the results into the {cache_update} table using the
20
 * 'update_project_projects' cache ID. However, since this is not the data about
21
 * available updates fetched from the network, it is acceptable to invalidate it
22
 * somewhat quickly. If we keep this data for very long, site administrators are
23
 * more likely to see incorrect results if they upgrade to a newer version of a
24
 * module or theme but do not visit certain pages that automatically clear this
25
 * cache.
26
 *
27
 * @return
28
 *   An associative array of currently enabled projects keyed by the
29
 *   machine-readable project short name. Each project contains:
30
 *   - name: The machine-readable project short name.
31
 *   - info: An array with values from the main .info file for this project.
32
 *     - name: The human-readable name of the project.
33
 *     - package: The package that the project is grouped under.
34
 *     - version: The version of the project.
35
 *     - project: The Drupal.org project name.
36
 *     - datestamp: The date stamp of the project's main .info file.
37
 *     - _info_file_ctime: The maximum file change time for all of the .info
38
 *       files included in this project.
39
 *   - datestamp: The date stamp when the project was released, if known.
40
 *   - includes: An associative array containing all projects included with this
41
 *     project, keyed by the machine-readable short name with the human-readable
42
 *     name as value.
43
 *   - project_type: The type of project. Allowed values are 'module' and
44
 *     'theme'.
45
 *   - project_status: This indicates if the project is enabled and will always
46
 *     be TRUE, as the function only returns enabled projects.
47
 *   - sub_themes: If the project is a theme it contains an associative array of
48
 *     all sub-themes.
49
 *   - base_themes: If the project is a theme it contains an associative array
50
 *     of all base-themes.
51
 *
52
 * @see update_process_project_info()
53
 * @see update_calculate_project_data()
54
 * @see update_project_cache()
55
 */
56
function update_get_projects() {
57
  $projects = &drupal_static(__FUNCTION__, array());
58
  if (empty($projects)) {
59
    // Retrieve the projects from cache, if present.
60
    $projects = update_project_cache('update_project_projects');
61
    if (empty($projects)) {
62
      // Still empty, so we have to rebuild the cache.
63
      $module_data = system_rebuild_module_data();
64
      $theme_data = system_rebuild_theme_data();
65
      _update_process_info_list($projects, $module_data, 'module', TRUE);
66
      _update_process_info_list($projects, $theme_data, 'theme', TRUE);
67
      if (variable_get('update_check_disabled', FALSE)) {
68
        _update_process_info_list($projects, $module_data, 'module', FALSE);
69
        _update_process_info_list($projects, $theme_data, 'theme', FALSE);
70
      }
71
      // Allow other modules to alter projects before fetching and comparing.
72
      drupal_alter('update_projects', $projects);
73
      // Cache the site's project data for at most 1 hour.
74
      _update_cache_set('update_project_projects', $projects, REQUEST_TIME + 3600);
75
    }
76
  }
77
  return $projects;
78
}
79

    
80
/**
81
 * Populates an array of project data.
82
 *
83
 * This iterates over a list of the installed modules or themes and groups them
84
 * by project and status. A few parts of this function assume that enabled
85
 * modules and themes are always processed first, and if disabled modules or
86
 * themes are being processed (there is a setting to control if disabled code
87
 * should be included or not in the 'Available updates' report), those are only
88
 * processed after $projects has been populated with information about the
89
 * enabled code. Modules and themes set as hidden are always ignored. This
90
 * function also records the latest change time on the .info files for each
91
 * module or theme, which is important data which is used when deciding if the
92
 * cached available update data should be invalidated.
93
 *
94
 * @param $projects
95
 *   Reference to the array of project data of what's installed on this site.
96
 * @param $list
97
 *   Array of data to process to add the relevant info to the $projects array.
98
 * @param $project_type
99
 *   The kind of data in the list. Can be 'module' or 'theme'.
100
 * @param $status
101
 *   Boolean that controls what status (enabled or disabled) to process out of
102
 *   the $list and add to the $projects array.
103
 *
104
 * @see update_get_projects()
105
 */
106
function _update_process_info_list(&$projects, $list, $project_type, $status) {
107
  $admin_theme = variable_get('admin_theme', 'seven');
108
  foreach ($list as $file) {
109
    // The admin theme is a special case. It should always be considered enabled
110
    // for the purposes of update checking.
111
    if ($file->name === $admin_theme) {
112
      $file->status = TRUE;
113
    }
114
    // A disabled base theme of an enabled sub-theme still has all of its code
115
    // run by the sub-theme, so we include it in our "enabled" projects list.
116
    if ($status && !$file->status && !empty($file->sub_themes)) {
117
      foreach ($file->sub_themes as $key => $name) {
118
        // Build a list of enabled sub-themes.
119
        if ($list[$key]->status) {
120
          $file->enabled_sub_themes[$key] = $name;
121
        }
122
      }
123
      // If there are no enabled subthemes, we should ignore this base theme
124
      // for the enabled case. If the site is trying to display disabled
125
      // themes, we'll catch it then.
126
      if (empty($file->enabled_sub_themes)) {
127
        continue;
128
      }
129
    }
130
    // Otherwise, just add projects of the proper status to our list.
131
    elseif ($file->status != $status) {
132
      continue;
133
    }
134

    
135
    // Skip if the .info file is broken.
136
    if (empty($file->info)) {
137
      continue;
138
    }
139

    
140
    // Skip if it's a hidden module or theme.
141
    if (!empty($file->info['hidden'])) {
142
      continue;
143
    }
144

    
145
    // If the .info doesn't define the 'project', try to figure it out.
146
    if (!isset($file->info['project'])) {
147
      $file->info['project'] = update_get_project_name($file);
148
    }
149

    
150
    // If we still don't know the 'project', give up.
151
    if (empty($file->info['project'])) {
152
      continue;
153
    }
154

    
155
    // If we don't already know it, grab the change time on the .info file
156
    // itself. Note: we need to use the ctime, not the mtime (modification
157
    // time) since many (all?) tar implementations will go out of their way to
158
    // set the mtime on the files it creates to the timestamps recorded in the
159
    // tarball. We want to see the last time the file was changed on disk,
160
    // which is left alone by tar and correctly set to the time the .info file
161
    // was unpacked.
162
    if (!isset($file->info['_info_file_ctime'])) {
163
      $info_filename = dirname($file->uri) . '/' . $file->name . '.info';
164
      $file->info['_info_file_ctime'] = filectime($info_filename);
165
    }
166

    
167
    if (!isset($file->info['datestamp'])) {
168
      $file->info['datestamp'] = 0;
169
    }
170

    
171
    $project_name = $file->info['project'];
172

    
173
    // Figure out what project type we're going to use to display this module
174
    // or theme. If the project name is 'drupal', we don't want it to show up
175
    // under the usual "Modules" section, we put it at a special "Drupal Core"
176
    // section at the top of the report.
177
    if ($project_name == 'drupal') {
178
      $project_display_type = 'core';
179
    }
180
    else {
181
      $project_display_type = $project_type;
182
    }
183
    if (empty($status) && empty($file->enabled_sub_themes)) {
184
      // If we're processing disabled modules or themes, append a suffix.
185
      // However, we don't do this to a a base theme with enabled
186
      // subthemes, since we treat that case as if it is enabled.
187
      $project_display_type .= '-disabled';
188
    }
189
    // Add a list of sub-themes that "depend on" the project and a list of base
190
    // themes that are "required by" the project.
191
    if ($project_name == 'drupal') {
192
      // Drupal core is always required, so this extra info would be noise.
193
      $sub_themes = array();
194
      $base_themes = array();
195
    }
196
    else {
197
      // Add list of enabled sub-themes.
198
      $sub_themes = !empty($file->enabled_sub_themes) ? $file->enabled_sub_themes : array();
199
      // Add list of base themes.
200
      $base_themes = !empty($file->base_themes) ? $file->base_themes : array();
201
    }
202
    if (!isset($projects[$project_name])) {
203
      // Only process this if we haven't done this project, since a single
204
      // project can have multiple modules or themes.
205
      $projects[$project_name] = array(
206
        'name' => $project_name,
207
        // Only save attributes from the .info file we care about so we do not
208
        // bloat our RAM usage needlessly.
209
        'info' => update_filter_project_info($file->info),
210
        'datestamp' => $file->info['datestamp'],
211
        'includes' => array($file->name => $file->info['name']),
212
        'project_type' => $project_display_type,
213
        'project_status' => $status,
214
        'sub_themes' => $sub_themes,
215
        'base_themes' => $base_themes,
216
      );
217
    }
218
    elseif ($projects[$project_name]['project_type'] == $project_display_type) {
219
      // Only add the file we're processing to the 'includes' array for this
220
      // project if it is of the same type and status (which is encoded in the
221
      // $project_display_type). This prevents listing all the disabled
222
      // modules included with an enabled project if we happen to be checking
223
      // for disabled modules, too.
224
      $projects[$project_name]['includes'][$file->name] = $file->info['name'];
225
      $projects[$project_name]['info']['_info_file_ctime'] = max($projects[$project_name]['info']['_info_file_ctime'], $file->info['_info_file_ctime']);
226
      $projects[$project_name]['datestamp'] = max($projects[$project_name]['datestamp'], $file->info['datestamp']);
227
      if (!empty($sub_themes)) {
228
        $projects[$project_name]['sub_themes'] += $sub_themes;
229
      }
230
      if (!empty($base_themes)) {
231
        $projects[$project_name]['base_themes'] += $base_themes;
232
      }
233
    }
234
    elseif (empty($status)) {
235
      // If we have a project_name that matches, but the project_display_type
236
      // does not, it means we're processing a disabled module or theme that
237
      // belongs to a project that has some enabled code. In this case, we add
238
      // the disabled thing into a separate array for separate display.
239
      $projects[$project_name]['disabled'][$file->name] = $file->info['name'];
240
    }
241
  }
242
}
243

    
244
/**
245
 * Determines what project a given file object belongs to.
246
 *
247
 * @param $file
248
 *   A file object as returned by system_get_files_database().
249
 *
250
 * @return
251
 *   The canonical project short name.
252
 *
253
 * @see system_get_files_database()
254
 */
255
function update_get_project_name($file) {
256
  $project_name = '';
257
  if (isset($file->info['project'])) {
258
    $project_name = $file->info['project'];
259
  }
260
  elseif (isset($file->info['package']) && (strpos($file->info['package'], 'Core') === 0)) {
261
    $project_name = 'drupal';
262
  }
263
  return $project_name;
264
}
265

    
266
/**
267
 * Determines version and type information for currently installed projects.
268
 *
269
 * Processes the list of projects on the system to figure out the currently
270
 * installed versions, and other information that is required before we can
271
 * compare against the available releases to produce the status report.
272
 *
273
 * @param $projects
274
 *   Array of project information from update_get_projects().
275
 */
276
function update_process_project_info(&$projects) {
277
  foreach ($projects as $key => $project) {
278
    // Assume an official release until we see otherwise.
279
    $install_type = 'official';
280

    
281
    $info = $project['info'];
282

    
283
    if (isset($info['version'])) {
284
      // Check for development snapshots
285
      if (preg_match('@(dev|HEAD)@', $info['version'])) {
286
        $install_type = 'dev';
287
      }
288

    
289
      // Figure out what the currently installed major version is. We need
290
      // to handle both contribution (e.g. "5.x-1.3", major = 1) and core
291
      // (e.g. "5.1", major = 5) version strings.
292
      $matches = array();
293
      if (preg_match('/^(\d+\.x-)?(\d+)\..*$/', $info['version'], $matches)) {
294
        $info['major'] = $matches[2];
295
      }
296
      elseif (!isset($info['major'])) {
297
        // This would only happen for version strings that don't follow the
298
        // drupal.org convention. We let contribs define "major" in their
299
        // .info in this case, and only if that's missing would we hit this.
300
        $info['major'] = -1;
301
      }
302
    }
303
    else {
304
      // No version info available at all.
305
      $install_type = 'unknown';
306
      $info['version'] = t('Unknown');
307
      $info['major'] = -1;
308
    }
309

    
310
    // Finally, save the results we care about into the $projects array.
311
    $projects[$key]['existing_version'] = $info['version'];
312
    $projects[$key]['existing_major'] = $info['major'];
313
    $projects[$key]['install_type'] = $install_type;
314
  }
315
}
316

    
317
/**
318
 * Calculates the current update status of all projects on the site.
319
 *
320
 * The results of this function are expensive to compute, especially on sites
321
 * with lots of modules or themes, since it involves a lot of comparisons and
322
 * other operations. Therefore, we cache the results into the {cache_update}
323
 * table using the 'update_project_data' cache ID. However, since this is not
324
 * the data about available updates fetched from the network, it is ok to
325
 * invalidate it somewhat quickly. If we keep this data for very long, site
326
 * administrators are more likely to see incorrect results if they upgrade to a
327
 * newer version of a module or theme but do not visit certain pages that
328
 * automatically clear this cache.
329
 *
330
 * @param array $available
331
 *   Data about available project releases.
332
 *
333
 * @return
334
 *   An array of installed projects with current update status information.
335
 *
336
 * @see update_get_available()
337
 * @see update_get_projects()
338
 * @see update_process_project_info()
339
 * @see update_project_cache()
340
 */
341
function update_calculate_project_data($available) {
342
  // Retrieve the projects from cache, if present.
343
  $projects = update_project_cache('update_project_data');
344
  // If $projects is empty, then the cache must be rebuilt.
345
  // Otherwise, return the cached data and skip the rest of the function.
346
  if (!empty($projects)) {
347
    return $projects;
348
  }
349
  $projects = update_get_projects();
350
  update_process_project_info($projects);
351
  foreach ($projects as $project => $project_info) {
352
    if (isset($available[$project])) {
353
      update_calculate_project_update_status($project, $projects[$project], $available[$project]);
354
    }
355
    else {
356
      $projects[$project]['status'] = UPDATE_UNKNOWN;
357
      $projects[$project]['reason'] = t('No available releases found');
358
    }
359
  }
360
  // Give other modules a chance to alter the status (for example, to allow a
361
  // contrib module to provide fine-grained settings to ignore specific
362
  // projects or releases).
363
  drupal_alter('update_status', $projects);
364

    
365
  // Cache the site's update status for at most 1 hour.
366
  _update_cache_set('update_project_data', $projects, REQUEST_TIME + 3600);
367
  return $projects;
368
}
369

    
370
/**
371
 * Calculates the current update status of a specific project.
372
 *
373
 * This function is the heart of the update status feature. For each project it
374
 * is invoked with, it first checks if the project has been flagged with a
375
 * special status like "unsupported" or "insecure", or if the project node
376
 * itself has been unpublished. In any of those cases, the project is marked
377
 * with an error and the next project is considered.
378
 *
379
 * If the project itself is valid, the function decides what major release
380
 * series to consider. The project defines what the currently supported major
381
 * versions are for each version of core, so the first step is to make sure the
382
 * current version is still supported. If so, that's the target version. If the
383
 * current version is unsupported, the project maintainer's recommended major
384
 * version is used. There's also a check to make sure that this function never
385
 * recommends an earlier release than the currently installed major version.
386
 *
387
 * Given a target major version, the available releases are scanned looking for
388
 * the specific release to recommend (avoiding beta releases and development
389
 * snapshots if possible). For the target major version, the highest patch level
390
 * is found. If there is a release at that patch level with no extra ("beta",
391
 * etc.), then the release at that patch level with the most recent release date
392
 * is recommended. If every release at that patch level has extra (only betas),
393
 * then the latest release from the previous patch level is recommended. For
394
 * example:
395
 *
396
 * - 1.6-bugfix <-- recommended version because 1.6 already exists.
397
 * - 1.6
398
 *
399
 * or
400
 *
401
 * - 1.6-beta
402
 * - 1.5 <-- recommended version because no 1.6 exists.
403
 * - 1.4
404
 *
405
 * Also, the latest release from the same major version is looked for, even beta
406
 * releases, to display to the user as the "Latest version" option.
407
 * Additionally, the latest official release from any higher major versions that
408
 * have been released is searched for to provide a set of "Also available"
409
 * options.
410
 *
411
 * Finally, and most importantly, the release history continues to be scanned
412
 * until the currently installed release is reached, searching for anything
413
 * marked as a security update. If any security updates have been found between
414
 * the recommended release and the installed version, all of the releases that
415
 * included a security fix are recorded so that the site administrator can be
416
 * warned their site is insecure, and links pointing to the release notes for
417
 * each security update can be included (which, in turn, will link to the
418
 * official security announcements for each vulnerability).
419
 *
420
 * This function relies on the fact that the .xml release history data comes
421
 * sorted based on major version and patch level, then finally by release date
422
 * if there are multiple releases such as betas from the same major.patch
423
 * version (e.g., 5.x-1.5-beta1, 5.x-1.5-beta2, and 5.x-1.5). Development
424
 * snapshots for a given major version are always listed last.
425
 *
426
 * @param $unused
427
 *   Input is not being used, but remains in function for API compatibility
428
 *   reasons.
429
 * @param $project_data
430
 *   An array containing information about a specific project.
431
 * @param $available
432
 *   Data about available project releases of a specific project.
433
 */
434
function update_calculate_project_update_status($unused, &$project_data, $available) {
435
  foreach (array('title', 'link') as $attribute) {
436
    if (!isset($project_data[$attribute]) && isset($available[$attribute])) {
437
      $project_data[$attribute] = $available[$attribute];
438
    }
439
  }
440

    
441
  // If the project status is marked as something bad, there's nothing else
442
  // to consider.
443
  if (isset($available['project_status'])) {
444
    switch ($available['project_status']) {
445
      case 'insecure':
446
        $project_data['status'] = UPDATE_NOT_SECURE;
447
        if (empty($project_data['extra'])) {
448
          $project_data['extra'] = array();
449
        }
450
        $project_data['extra'][] = array(
451
          'class' => array('project-not-secure'),
452
          'label' => t('Project not secure'),
453
          'data' => t('This project has been labeled insecure by the Drupal security team, and is no longer available for download. Immediately disabling everything included by this project is strongly recommended!'),
454
        );
455
        break;
456
      case 'unpublished':
457
      case 'revoked':
458
        $project_data['status'] = UPDATE_REVOKED;
459
        if (empty($project_data['extra'])) {
460
          $project_data['extra'] = array();
461
        }
462
        $project_data['extra'][] = array(
463
          'class' => array('project-revoked'),
464
          'label' => t('Project revoked'),
465
          'data' => t('This project has been revoked, and is no longer available for download. Disabling everything included by this project is strongly recommended!'),
466
        );
467
        break;
468
      case 'unsupported':
469
        $project_data['status'] = UPDATE_NOT_SUPPORTED;
470
        if (empty($project_data['extra'])) {
471
          $project_data['extra'] = array();
472
        }
473
        $project_data['extra'][] = array(
474
          'class' => array('project-not-supported'),
475
          'label' => t('Project not supported'),
476
          'data' => t('This project is no longer supported, and is no longer available for download. Disabling everything included by this project is strongly recommended!'),
477
        );
478
        break;
479
      case 'not-fetched':
480
        $project_data['status'] = UPDATE_NOT_FETCHED;
481
        $project_data['reason'] = t('Failed to get available update data.');
482
        break;
483

    
484
      default:
485
        // Assume anything else (e.g. 'published') is valid and we should
486
        // perform the rest of the logic in this function.
487
        break;
488
    }
489
  }
490

    
491
  if (!empty($project_data['status'])) {
492
    // We already know the status for this project, so there's nothing else to
493
    // compute. Record the project status into $project_data and we're done.
494
    $project_data['project_status'] = $available['project_status'];
495
    return;
496
  }
497

    
498
  // Figure out the target major version.
499
  $existing_major = $project_data['existing_major'];
500
  $supported_majors = array();
501
  if (isset($available['supported_majors'])) {
502
    $supported_majors = explode(',', $available['supported_majors']);
503
  }
504
  elseif (isset($available['default_major'])) {
505
    // Older release history XML file without supported or recommended.
506
    $supported_majors[] = $available['default_major'];
507
  }
508

    
509
  if (in_array($existing_major, $supported_majors)) {
510
    // Still supported, stay at the current major version.
511
    $target_major = $existing_major;
512
  }
513
  elseif (isset($available['recommended_major'])) {
514
    // Since 'recommended_major' is defined, we know this is the new XML
515
    // format. Therefore, we know the current release is unsupported since
516
    // its major version was not in the 'supported_majors' list. We should
517
    // find the best release from the recommended major version.
518
    $target_major = $available['recommended_major'];
519
    $project_data['status'] = UPDATE_NOT_SUPPORTED;
520
  }
521
  elseif (isset($available['default_major'])) {
522
    // Older release history XML file without recommended, so recommend
523
    // the currently defined "default_major" version.
524
    $target_major = $available['default_major'];
525
  }
526
  else {
527
    // Malformed XML file? Stick with the current version.
528
    $target_major = $existing_major;
529
  }
530

    
531
  // Make sure we never tell the admin to downgrade. If we recommended an
532
  // earlier version than the one they're running, they'd face an
533
  // impossible data migration problem, since Drupal never supports a DB
534
  // downgrade path. In the unfortunate case that what they're running is
535
  // unsupported, and there's nothing newer for them to upgrade to, we
536
  // can't print out a "Recommended version", but just have to tell them
537
  // what they have is unsupported and let them figure it out.
538
  $target_major = max($existing_major, $target_major);
539

    
540
  $release_patch_changed = '';
541
  $patch = '';
542

    
543
  // If the project is marked as UPDATE_FETCH_PENDING, it means that the
544
  // data we currently have (if any) is stale, and we've got a task queued
545
  // up to (re)fetch the data. In that case, we mark it as such, merge in
546
  // whatever data we have (e.g. project title and link), and move on.
547
  if (!empty($available['fetch_status']) && $available['fetch_status'] == UPDATE_FETCH_PENDING) {
548
    $project_data['status'] = UPDATE_FETCH_PENDING;
549
    $project_data['reason'] = t('No available update data');
550
    $project_data['fetch_status'] = $available['fetch_status'];
551
    return;
552
  }
553

    
554
  // Defend ourselves from XML history files that contain no releases.
555
  if (empty($available['releases'])) {
556
    $project_data['status'] = UPDATE_UNKNOWN;
557
    $project_data['reason'] = t('No available releases found');
558
    return;
559
  }
560
  foreach ($available['releases'] as $version => $release) {
561
    // First, if this is the existing release, check a few conditions.
562
    if ($project_data['existing_version'] === $version) {
563
      if (isset($release['terms']['Release type']) &&
564
          in_array('Insecure', $release['terms']['Release type'])) {
565
        $project_data['status'] = UPDATE_NOT_SECURE;
566
      }
567
      elseif ($release['status'] == 'unpublished') {
568
        $project_data['status'] = UPDATE_REVOKED;
569
        if (empty($project_data['extra'])) {
570
          $project_data['extra'] = array();
571
        }
572
        $project_data['extra'][] = array(
573
          'class' => array('release-revoked'),
574
          'label' => t('Release revoked'),
575
          'data' => t('Your currently installed release has been revoked, and is no longer available for download. Disabling everything included in this release or upgrading is strongly recommended!'),
576
        );
577
      }
578
      elseif (isset($release['terms']['Release type']) &&
579
              in_array('Unsupported', $release['terms']['Release type'])) {
580
        $project_data['status'] = UPDATE_NOT_SUPPORTED;
581
        if (empty($project_data['extra'])) {
582
          $project_data['extra'] = array();
583
        }
584
        $project_data['extra'][] = array(
585
          'class' => array('release-not-supported'),
586
          'label' => t('Release not supported'),
587
          'data' => t('Your currently installed release is now unsupported, and is no longer available for download. Disabling everything included in this release or upgrading is strongly recommended!'),
588
        );
589
      }
590
    }
591

    
592
    // Otherwise, ignore unpublished, insecure, or unsupported releases.
593
    if ($release['status'] == 'unpublished' ||
594
        (isset($release['terms']['Release type']) &&
595
         (in_array('Insecure', $release['terms']['Release type']) ||
596
          in_array('Unsupported', $release['terms']['Release type'])))) {
597
      continue;
598
    }
599

    
600
    // See if this is a higher major version than our target and yet still
601
    // supported. If so, record it as an "Also available" release.
602
    // Note: some projects have a HEAD release from CVS days, which could
603
    // be one of those being compared. They would not have version_major
604
    // set, so we must call isset first.
605
    if (isset($release['version_major']) && $release['version_major'] > $target_major) {
606
      if (in_array($release['version_major'], $supported_majors)) {
607
        if (!isset($project_data['also'])) {
608
          $project_data['also'] = array();
609
        }
610
        if (!isset($project_data['also'][$release['version_major']])) {
611
          $project_data['also'][$release['version_major']] = $version;
612
          $project_data['releases'][$version] = $release;
613
        }
614
      }
615
      // Otherwise, this release can't matter to us, since it's neither
616
      // from the release series we're currently using nor the recommended
617
      // release. We don't even care about security updates for this
618
      // branch, since if a project maintainer puts out a security release
619
      // at a higher major version and not at the lower major version,
620
      // they must remove the lower version from the supported major
621
      // versions at the same time, in which case we won't hit this code.
622
      continue;
623
    }
624

    
625
    // Look for the 'latest version' if we haven't found it yet. Latest is
626
    // defined as the most recent version for the target major version.
627
    if (!isset($project_data['latest_version'])
628
        && $release['version_major'] == $target_major) {
629
      $project_data['latest_version'] = $version;
630
      $project_data['releases'][$version] = $release;
631
    }
632

    
633
    // Look for the development snapshot release for this branch.
634
    if (!isset($project_data['dev_version'])
635
        && $release['version_major'] == $target_major
636
        && isset($release['version_extra'])
637
        && $release['version_extra'] == 'dev') {
638
      $project_data['dev_version'] = $version;
639
      $project_data['releases'][$version] = $release;
640
    }
641

    
642
    // Look for the 'recommended' version if we haven't found it yet (see
643
    // phpdoc at the top of this function for the definition).
644
    if (!isset($project_data['recommended'])
645
        && $release['version_major'] == $target_major
646
        && isset($release['version_patch'])) {
647
      if ($patch != $release['version_patch']) {
648
        $patch = $release['version_patch'];
649
        $release_patch_changed = $release;
650
      }
651
      if (empty($release['version_extra']) && $patch == $release['version_patch']) {
652
        $project_data['recommended'] = $release_patch_changed['version'];
653
        $project_data['releases'][$release_patch_changed['version']] = $release_patch_changed;
654
      }
655
    }
656

    
657
    // Stop searching once we hit the currently installed version.
658
    if ($project_data['existing_version'] === $version) {
659
      break;
660
    }
661

    
662
    // If we're running a dev snapshot and have a timestamp, stop
663
    // searching for security updates once we hit an official release
664
    // older than what we've got. Allow 100 seconds of leeway to handle
665
    // differences between the datestamp in the .info file and the
666
    // timestamp of the tarball itself (which are usually off by 1 or 2
667
    // seconds) so that we don't flag that as a new release.
668
    if ($project_data['install_type'] == 'dev') {
669
      if (empty($project_data['datestamp'])) {
670
        // We don't have current timestamp info, so we can't know.
671
        continue;
672
      }
673
      elseif (isset($release['date']) && ($project_data['datestamp'] + 100 > $release['date'])) {
674
        // We're newer than this, so we can skip it.
675
        continue;
676
      }
677
    }
678

    
679
    // See if this release is a security update.
680
    if (isset($release['terms']['Release type'])
681
        && in_array('Security update', $release['terms']['Release type'])) {
682
      $project_data['security updates'][] = $release;
683
    }
684
  }
685

    
686
  // If we were unable to find a recommended version, then make the latest
687
  // version the recommended version if possible.
688
  if (!isset($project_data['recommended']) && isset($project_data['latest_version'])) {
689
    $project_data['recommended'] = $project_data['latest_version'];
690
  }
691

    
692
  //
693
  // Check to see if we need an update or not.
694
  //
695

    
696
  if (!empty($project_data['security updates'])) {
697
    // If we found security updates, that always trumps any other status.
698
    $project_data['status'] = UPDATE_NOT_SECURE;
699
  }
700

    
701
  if (isset($project_data['status'])) {
702
    // If we already know the status, we're done.
703
    return;
704
  }
705

    
706
  // If we don't know what to recommend, there's nothing we can report.
707
  // Bail out early.
708
  if (!isset($project_data['recommended'])) {
709
    $project_data['status'] = UPDATE_UNKNOWN;
710
    $project_data['reason'] = t('No available releases found');
711
    return;
712
  }
713

    
714
  // If we're running a dev snapshot, compare the date of the dev snapshot
715
  // with the latest official version, and record the absolute latest in
716
  // 'latest_dev' so we can correctly decide if there's a newer release
717
  // than our current snapshot.
718
  if ($project_data['install_type'] == 'dev') {
719
    if (isset($project_data['dev_version']) && $available['releases'][$project_data['dev_version']]['date'] > $available['releases'][$project_data['latest_version']]['date']) {
720
      $project_data['latest_dev'] = $project_data['dev_version'];
721
    }
722
    else {
723
      $project_data['latest_dev'] = $project_data['latest_version'];
724
    }
725
  }
726

    
727
  // Figure out the status, based on what we've seen and the install type.
728
  switch ($project_data['install_type']) {
729
    case 'official':
730
      if ($project_data['existing_version'] === $project_data['recommended'] || $project_data['existing_version'] === $project_data['latest_version']) {
731
        $project_data['status'] = UPDATE_CURRENT;
732
      }
733
      else {
734
        $project_data['status'] = UPDATE_NOT_CURRENT;
735
      }
736
      break;
737

    
738
    case 'dev':
739
      $latest = $available['releases'][$project_data['latest_dev']];
740
      if (empty($project_data['datestamp'])) {
741
        $project_data['status'] = UPDATE_NOT_CHECKED;
742
        $project_data['reason'] = t('Unknown release date');
743
      }
744
      elseif (($project_data['datestamp'] + 100 > $latest['date'])) {
745
        $project_data['status'] = UPDATE_CURRENT;
746
      }
747
      else {
748
        $project_data['status'] = UPDATE_NOT_CURRENT;
749
      }
750
      break;
751

    
752
    default:
753
      $project_data['status'] = UPDATE_UNKNOWN;
754
      $project_data['reason'] = t('Invalid info');
755
  }
756
}
757

    
758
/**
759
 * Retrieves data from {cache_update} or empties the cache when necessary.
760
 *
761
 * Two very expensive arrays computed by this module are the list of all
762
 * installed modules and themes (and .info data, project associations, etc), and
763
 * the current status of the site relative to the currently available releases.
764
 * These two arrays are cached in the {cache_update} table and used whenever
765
 * possible. The cache is cleared whenever the administrator visits the status
766
 * report, available updates report, or the module or theme administration
767
 * pages, since we should always recompute the most current values on any of
768
 * those pages.
769
 *
770
 * Note: while both of these arrays are expensive to compute (in terms of disk
771
 * I/O and some fairly heavy CPU processing), neither of these is the actual
772
 * data about available updates that we have to fetch over the network from
773
 * updates.drupal.org. That information is stored with the
774
 * 'update_available_releases' cache ID -- it needs to persist longer than 1
775
 * hour and never get invalidated just by visiting a page on the site.
776
 *
777
 * @param $cid
778
 *   The cache ID of data to return from the cache. Valid options are
779
 *   'update_project_data' and 'update_project_projects'.
780
 *
781
 * @return
782
 *   The cached value of the $projects array generated by
783
 *   update_calculate_project_data() or update_get_projects(), or an empty array
784
 *   when the cache is cleared.
785
 */
786
function update_project_cache($cid) {
787
  $projects = array();
788

    
789
  // On certain paths, we should clear the cache and recompute the projects for
790
  // update status of the site to avoid presenting stale information.
791
  $q = $_GET['q'];
792
  $paths = array(
793
    'admin/modules',
794
    'admin/modules/update',
795
    'admin/appearance',
796
    'admin/appearance/update',
797
    'admin/reports',
798
    'admin/reports/updates',
799
    'admin/reports/updates/update',
800
    'admin/reports/status',
801
    'admin/reports/updates/check',
802
  );
803
  if (in_array($q, $paths)) {
804
    _update_cache_clear($cid);
805
  }
806
  else {
807
    $cache = _update_cache_get($cid);
808
    if (!empty($cache->data) && $cache->expire > REQUEST_TIME) {
809
      $projects = $cache->data;
810
    }
811
  }
812
  return $projects;
813
}
814

    
815
/**
816
 * Filters the project .info data to only save attributes we need.
817
 *
818
 * @param array $info
819
 *   Array of .info file data as returned by drupal_parse_info_file().
820
 *
821
 * @return
822
 *   Array of .info file data we need for the update manager.
823
 *
824
 * @see _update_process_info_list()
825
 */
826
function update_filter_project_info($info) {
827
  $whitelist = array(
828
    '_info_file_ctime',
829
    'datestamp',
830
    'major',
831
    'name',
832
    'package',
833
    'project',
834
    'project status url',
835
    'version',
836
  );
837
  return array_intersect_key($info, drupal_map_assoc($whitelist));
838
}