00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 define('MULTI_SUCCESS', 0);
00019
00020
00021 define('MULTI_NOTHING_TO_DO', 0);
00022
00023
00024 define('MULTI_NAGIOS_OK', 0);
00025
00026
00027 define('MULTI_NAGIOS_WARNING', 1);
00028
00029
00030 define('MULTI_NAGIOS_CRITICAL', 2);
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 function _multi_recommended_version($core, $project = 'drupal', $float = FALSE, $path = FALSE) {
00082
00083 if (!$path) {
00084 $path = 'http://updates.drupal.org/release-history/'. $project. '/'. $core . '.x';
00085 }
00086 if ($xml = @simplexml_load_file($path)) {
00087 if ($error = $xml->xpath('/error')) {
00088 return drush_set_error('MULTI_COULD_NOT_LOAD_UPDATE_FILE', $error[0]);
00089 }
00090 else {
00091 $recommended_major = $xml->xpath("/project/recommended_major");
00092 $xpath_releases = "/project/releases/release[status='published'][version_major=" . (string)$recommended_major[0] . "]";
00093 $releases = $xml->xpath($xpath_releases);
00094 $release = (array)$releases[0];
00095 unset($release['terms']);
00096 return (!$float) ? $release : $release['version'];
00097 }
00098 }
00099 else {
00100
00101 return drush_set_error('MULTI_DOWNLOAD_FAILED',
00102 dt('Could not download project status information from !url',
00103 array('!url' => $path)
00104 )
00105 );
00106 }
00107 }
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123 function _multi_released_versions($core, $project = 'drupal') {
00124 $url = 'http://updates.drupal.org/release-history/' .$project . '/'. $core . '.x';
00125 if ($xml = @simplexml_load_file($url)) {
00126 if ($error = $xml->xpath('/error')) {
00127 return drush_set_error('MULTI_COULD_NOT_LOAD_UPDATE_FILE',
00128 $error[0]
00129 );
00130 }
00131 else {
00132 $recommended_major = $xml->xpath("/project/recommended_major");
00133 $xpath_releases = "/project/releases/release[status='published'][version_major=" . (string)$recommended_major[0] . "]";
00134 $releases = $xml->xpath($xpath_releases);
00135 foreach ($releases as $release) {
00136 $released_versions[] = 'drupal-' . $release->version;
00137 }
00138 usort($released_versions, "strnatcmp");
00139 return $released_versions;
00140 }
00141 }
00142 else {
00143
00144 return drush_set_error('MULTI_DOWNLOAD_FAILED',
00145 dt('Could not download project status information from !url',
00146 array('!url' => $url))
00147 );
00148 }
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162 function _multi_check_project($core, $project = 'drupal') {
00163
00164 $url = 'http://updates.drupal.org/release-history/'. $project . '/'. $core . '.x';
00165 if ($xml = @simplexml_load_file($url)) {
00166 if ($error = $xml->xpath('/error')) {
00167 return drush_set_error('MULTI_INVALID_PROJECT_OR_MAJOR_VERSION',
00168 dt('Combination of project !project and major version !$core does not exist.',
00169 array('!project' => $project, '!core' => $core)
00170 )
00171 );
00172 }
00173 else {
00174 return TRUE;
00175 }
00176 }
00177 else {
00178
00179 return drush_set_error('MULTI_DOWNLOAD_FAILED',
00180 dt('Could not download project status information from !url',
00181 array('!url' => $url))
00182 );
00183 }
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 function _multi_scan_sites($symlinks = TRUE){
00207
00208 $sites = array();
00209 $scan_result = drush_scan_directory(
00210 rtrim(drush_get_context('DRUSH_DRUPAL_ROOT')) . '/sites', '/^settings\.php$/'
00211 );
00212
00213 foreach($scan_result as $site ) {
00214
00215 $tmp = array();
00216 $tmp = explode('/', ltrim($site->filename, '/'));
00217
00218 if (!$symlinks) {
00219 $dir = $tmp;
00220
00221 $dir = implode('/', $dir);
00222
00223 $dir = '/' . $dir;
00224 if (is_link($dir)) {
00225 continue;
00226 }
00227 }
00228 $sites[] = $tmp[count($tmp)-2];
00229 }
00230 return $sites;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 function _multi_scan_symlinks($dir = FALSE, $recurse = TRUE, $min_depth = 0, $depth = 0 ) {
00283
00284 if (!$dir) {
00285 $dir = (string) drush_get_context('DRUSH_DRUPAL_ROOT');
00286 }
00287
00288 $path_parts = pathinfo($dir);
00289 $dir = (is_link($dir) && !realpath($dir)) ? $path_parts['dirname'] . '/' . readlink($dir) : $dir;
00290
00291 $nomask = array('.', '..', 'CVS');
00292 $files = array();
00293
00294 if (is_dir($dir) && $handle = opendir($dir)) {
00295 while (FALSE !== ($file = readdir($handle))) {
00296
00297 if (!in_array($file, $nomask)) {
00298 if (is_dir("$dir/$file") && $recurse && !is_link("$dir/$file")) {
00299
00300 $files = array_merge( _multi_scan_symlinks("$dir/$file", $recurse, $min_depth, $depth + 1), $files);
00301 }
00302 elseif ($depth >= $min_depth) {
00303 $filename = "$dir/$file";
00304 if (is_link($filename)) {
00305 $basename = basename($file);
00306 $files[$basename] = new stdClass();
00307 $files[$basename]->filename = $filename;
00308 $files[$basename]->basename = $basename;
00309 $files[$basename]->target = readlink($filename);
00310 $files[$basename]->dirname = dirname($filename);
00311 }
00312 }
00313 }
00314 }
00315 closedir($handle);
00316 }
00317 return $files;
00318 }
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 function _multi_preserve_symlinks($drupal_root, $new_drupal_root) {
00332
00333
00334 if (drush_op('chdir', realpath($new_drupal_root))) {
00335
00336
00337 $symlinks = array();
00338 $symlinks = _multi_scan_symlinks($drupal_root, TRUE);
00339
00340
00341 foreach ($symlinks as $symlink) {
00342
00343
00344 if (is_file($symlink->basename) || is_dir($symlink->basename)) {
00345 if (!drush_op('rename', $symlink->basename, $symlink->basename . '-' . date('Y-m-d\TH:i') . '-drupalupdate')) {
00346 return drush_set_error('MULTI_COULD_NOT_RENAME',
00347 dt('Could not rename !existing to !backup',
00348 array(
00349 '!existing' => $symlink->basename,
00350 '!backup' => $symlink->basename . '-' . date('Y-m-d\TH:i') . '-drupalupdate'
00351 )
00352 )
00353 );
00354 }
00355 }
00356
00357 $symlink_target = _multi_relative_path(realpath($symlink->target));
00358
00359
00360
00361 $symlink_dir = ltrim(str_replace($drupal_root, '', $symlink->dirname), '/');
00362 (!empty($symlink_dir)) ? $symlink_dir .= '/' : $symlink_dir;
00363
00364 if (!drush_op('symlink', $symlink_target, $symlink_dir . $symlink->basename)) {
00365 return drush_set_error('MULTI_COULD_NOT_CREATE_SYMLINK',
00366 dt('Could not create symbolic link from !target into !pwd.',
00367 array(
00368 '!target' => rtrim($filename, '/'),
00369 '!pwd' => getcwd()
00370 )
00371 )
00372 );
00373 }
00374 }
00375 }
00376
00377 else {
00378 return drush_set_error('DRUSH_SM_COULD_CHANGE_NEW_DRUPAL_ROOT',
00379 dt('chdir(): Could not change to new Drupal root !new.',
00380 array('!new' => $new_drupal_root)
00381 )
00382 );
00383 }
00384 }
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396 function _multi_available_installations() {
00397 $scan_result = array();
00398 $scan_result = drush_scan_directory(
00399 rtrim(drush_get_context('DRUSH_DRUPAL_ROOT')) . '/..',
00400 '/^drupal-' . drush_drupal_major_version().'/', array(), 0, FALSE
00401 );
00402 foreach ($scan_result as $install) {
00403 $installations[] = $install->basename;
00404 }
00405 usort($installations, "strnatcmp");
00406 return $installations;
00407 }
00408
00409
00410
00411
00412
00413
00414
00415
00416 function _multi_previous_installations() {
00417
00418 $installations = array();
00419 if(count($installations = _multi_available_installations()) != 1) {
00420 $size = array_keys($installations, _multi_current_version());
00421 if (sizeof($size) == 0) {
00422 return FALSE;
00423 }
00424
00425 $previous_versions = array_chunk($installations, $size[0]);
00426 if (sizeof($previous_versions[0]) != 0) {
00427 return $previous_versions[0];
00428 }
00429 else {
00430 return FALSE;
00431 }
00432 }
00433 else {
00434 return FALSE;
00435 }
00436 }
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446 function _multi_previous_installation() {
00447
00448 $installations = array();
00449 $installations = _multi_available_installations();
00450 if ($length = count($installations) != 1) {
00451
00452 $pos = array_keys($installations, _multi_current_version());
00453 if (bcsub($pos[0],1) != -1 ) {
00454
00455 return $installations[bcsub($pos[0],1)];
00456 }
00457 else {
00458 return FALSE;
00459 }
00460 }
00461 else {
00462 return FALSE;
00463 }
00464 }
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476 function _multi_relative_path($target) {
00477
00478 $folder = $target;
00479 $target = getcwd();
00480
00481
00482 $target = trim($target,'/');
00483 $folder = trim($folder,'/');
00484 while (substr_count($target, '//') ) $target = str_replace('//', '/', $target);
00485 while (substr_count($folder, '//')) $folder = str_replace('//', '/', $folder);
00486
00487
00488 $arr1 = explode('/', $target);
00489 if ($arr1 == array('')) $arr1 = array();
00490 $arr2 = explode('/', $folder);
00491 if ($arr2 == array('')) $arr2 = array();
00492 $size1 = count($arr1);
00493 $size2 = count($arr2);
00494
00495
00496 $path='';
00497 for($i=0; $i<min($size1,$size2); $i++) {
00498 if ($arr1[$i] == $arr2[$i]) continue;
00499 else $path = '../'.$path.$arr2[$i].'/';
00500 }
00501 if ($size1 > $size2)
00502 for ($i = $size2; $i < $size1; $i++)
00503 $path = '../'.$path;
00504 else if ($size2 > $size1)
00505 for ($i = $size1; $i < $size2; $i++)
00506 $path .= $arr2[$i].'/';
00507
00508 return $path;
00509 }
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523 function _multi_nagios_root() {
00524
00525
00526
00527
00528
00529
00530
00531 if ($file = drush_get_option('file')) {
00532 if (!is_file($file)) {
00533 $file = FALSE;
00534 }
00535 }
00536 else {
00537 $file = FALSE;
00538 }
00539 if ($recommended_version = _multi_recommended_version(drush_drupal_major_version(), 'drupal', FALSE, $file)) {
00540 if ((float) drush_drupal_version() < (float) $recommended_version['version']) {
00541
00542 drush_print(
00543 dt('DRUPAL ROOT CRITICAL - !version @ !drupal_root: drupal-!remcommended_version (!release-date) available, see !release_link for details.',
00544 array(
00545 '!version' => _multi_current_version(),
00546 '!drupal_root' => drush_get_context('DRUSH_DRUPAL_ROOT'),
00547 '!remcommended_version' => $recommended_version['version'],
00548 '!release-date' => date('Y-m-d\ H:i', $recommended_version['date']),
00549 '!release_link' => $recommended_version['release_link'],
00550 )
00551 )
00552 );
00553 return MULTI_NAGIOS_CRITICAL;
00554 }
00555 else {
00556 drush_print(
00557 dt('DRUPAL ROOT OK - !version @ !drupal_root is uptodate.',
00558 array(
00559 '!version' => _multi_current_version(),
00560 '!drupal_root' => drush_get_context('DRUSH_DRUPAL_ROOT'),
00561 )
00562 )
00563 );
00564 return MULTI_NAGIOS_OK;
00565 }
00566 }
00567 else {
00568 return $recommended_version;
00569 }
00570 }
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585 function _multi_nagios_site() {
00586
00587
00588
00589 drush_bootstrap_max();
00590 $core = drush_drupal_major_version();
00591
00592 drush_include_engine('update_info', 'drupal', NULL, DRUSH_BASE_PATH . '/commands/pm/update_info');
00593
00594 $data = array();
00595
00596
00597 ob_start();
00598
00599 if ($core == 5) {
00600 $info = update_status_get_available();
00601 $data = update_status_calculate_project_data($info);
00602 }
00603 else if ($core = 6) {
00604 $available = update_get_available(TRUE);
00605 module_load_include('inc', 'update', 'update.compare');
00606 $data = update_calculate_project_data($available);
00607
00608 }
00609 ob_end_clean();
00610
00611
00612 drush_print_r($info);
00613 drush_print_r($data);
00614
00615 $modules = array();
00616 $updates = FALSE;
00617
00618 foreach($data as $module) {
00619
00620 if ($core == 5) {
00621
00622
00623 drush_print('--');
00624 drush_print('module:' . $module['name']);
00625 drush_print('version:' . $module['existing_version']);
00626 drush_print('recommended:' . $module['recommended']);
00627 drush_print('--');
00628
00629 if($module['existing_version'] != $module['recommended']) {
00630 $updates = TRUE;
00631 $modules[] = $module['name'];
00632 $updates_path = '/?q=admin/logs/updates';
00633 }
00634 }
00635 else if($core == 6) {
00636
00637
00638
00639 drush_print('--');
00640 drush_print('module:' . $module['name']);
00641 drush_print('version:' . $module['info']['version']);
00642 drush_print('recommended:' . $module['recommended']);
00643 drush_print('--');
00644
00645 if($module['info']['version'] != $module['recommended']) {
00646 $updates = TRUE;
00647 $modules[] = $module['name'];
00648 $updates_path = '/?q=admin/reports/updates';
00649 }
00650 }
00651 }
00652
00653 if($updates) {
00654 drush_print(
00655 dt('DRUPAL SITE CRITICAL - !uri @ !site_root: there are updates for !modules, see http://!uri!updates_path for datails.',
00656 array(
00657 '!uri' => drush_get_context('DRUSH_URI'),
00658 '!site_root'=> drush_get_context('DRUSH_DRUPAL_ROOT') . '/' . drush_get_context('DRUSH_DRUPAL_SITE_ROOT'),
00659 '!modules' => implode(',', $modules),
00660 '!updates_path' => $updates_path,
00661 )
00662 )
00663 );
00664 return MULTI_NAGIOS_CRITICAL;
00665 }
00666 else {
00667 drush_print(
00668 dt('DRUPAL SITE OK - !uri @ !site_root: All modules are uptodate.',
00669 array(
00670 '!uri' => drush_get_context('DRUSH_URI'),
00671 '!site_root'=> drush_get_context('DRUSH_DRUPAL_ROOT') . '/' . drush_get_context('DRUSH_DRUPAL_SITE_ROOT'),
00672 )
00673 )
00674 );
00675 return MULTI_NAGIOS_OK;
00676 }
00677 }
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691 function _multi_current_version() {
00692 return 'drupal-' . drush_drupal_version();
00693 }
00694
00695
00696
00697
00698
00699
00700
00701 function _multi_check_site($site) {
00702
00703 $sites = array();
00704 $sites = _multi_scan_sites();
00705 if (in_array($site, $sites)) {
00706 return TRUE;
00707 }
00708 return FALSE;
00709 }
00710
00711
00712
00713
00714
00715
00716
00717
00718 function _multi_check_db_url($site) {
00719
00720 $db_spec = array();
00721 $db_spec = _drush_sql_get_db_spec();
00722
00723
00724 switch ($db_spec['driver']) {
00725 case 'mysql':
00726 $link = @mysql_connect(
00727 $db_spec['host'] . ':' . $db_spec['port'],
00728 $db_spec['username'],
00729 $db_spec['password'],
00730 $db_spec['database']
00731 );
00732 break;
00733 case 'mysqli':
00734
00735 $link = mysqli_connect(
00736 $db_spec['host'],
00737 $db_spec['username'],
00738 $db_spec['password'],
00739 $db_spec['database'],
00740 $db_spec['port']
00741 );
00742 break;
00743 case 'pgsql':
00744
00745
00746 $connection_string = 'host=' . $db_spec['host'] .' ';
00747 $connection_string .= 'user=' . $db_spec['user'] .' ';
00748 $connection_string .= 'password=' . $db_spec['password'] .' ';
00749 $connection_string .= 'dbname=' . $db_spec['database'];
00750 if (!empty($db_spec['port'])) {
00751 $connection_string .= ' port=' . $db_spec['port'];
00752 }
00753 $link = pg_connect($connection_string);
00754 break;
00755 }
00756
00757
00758 if (!$link) {
00759 return FALSE;
00760 }
00761 else {
00762
00763 switch ($db_spec['driver']) {
00764 case 'mysql':
00765 mysql_close($link);
00766 break;
00767 case 'mysqli':
00768 mysqli_close($link);
00769 break;
00770 case 'pgsql':
00771 pg_close($link);
00772 break;
00773 }
00774 return TRUE;
00775 }
00776 }
00777
00778
00779
00780