upgrademodxwidget.snippet.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * UpgradeMODXWidget snippet for UpgradeMODX extra
  4. *
  5. * Copyright 2015-2018 Bob Ray <https://bobsguides.com>
  6. * Created on 08-16-2015
  7. *
  8. * UpgradeMODX is free software; you can redistribute it and/or modify it under the
  9. * terms of the GNU General Public License as published by the Free Software
  10. * Foundation; either version 2 of the License, or (at your option) any later
  11. * version.
  12. *
  13. * UpgradeMODX is distributed in the hope that it will be useful, but WITHOUT ANY
  14. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  15. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * UpgradeMODX; if not, write to the Free Software Foundation, Inc., 59 Temple
  19. * Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * @package upgrademodx
  22. */
  23. /**
  24. * Description
  25. * -----------
  26. * UpgradeMODX Dashboard widget
  27. * This package was inspired by the work of a number of people and I have borrowed some of their code.
  28. * Dmytro Lukianenko (dmi3yy) is the original author of the MODX install script. Susan Sottwell, Sharapov,
  29. * Bumkaka, Inreti, Zaigham Rana, frischnetz, and AgelxNash, also contributed and I'd like to thank all
  30. * of them for laying the groundwork.
  31. *
  32. * Variables
  33. * ---------
  34. * @var $modx modX
  35. * @var $scriptProperties array
  36. *
  37. * @package upgrademodx
  38. **/
  39. /* Properties
  40. * @property &groups textfield -- group, or comma-separated list of groups, who will see the widget; Default: (empty)..
  41. * @property &hideWhenNoUpgrade combo-boolean -- Hide widget when no upgrade is available; Default: No.
  42. * @property &interval textfield -- Interval between checks -- Examples: 1 week, 3 days, 6 hours; Default: 1 week.
  43. * @property &language textfield -- Two-letter code of language to user; Default: en.
  44. * @property &lastCheck textfield -- Date and time of last check -- set automatically; Default: (empty)..
  45. * @property &latestVersion textfield -- Latest version (at last check) -- set automatically; Default: (empty)..
  46. * @property &plOnly combo-boolean -- Show only pl (stable) versions; Default: yes.
  47. * @property &versionsToShow textfield -- Number of versions to show in upgrade form (not widget); Default: 5.
  48. */
  49. /* Initialize */
  50. /* This will execute when in MODX */
  51. $language = $modx->getOption('ugm_language', null, $modx->getOption('manager_language'), true);
  52. $language = empty($language) ? 'en' : $language;
  53. $props = $scriptProperties;
  54. $modx->lexicon->load($language . ':upgrademodx:default');
  55. $devMode = $modx->getOption('ugm.devMode', null, false, true);
  56. $groups = $modx->getOption('ugm_groups', null, 'Administrator', true);
  57. /* Return empty string if user shouldn't see widget */
  58. if (strpos($groups, ',') !== false) {
  59. $groups = explode(',', $groups);
  60. }
  61. if (! $modx->user->isMember($groups)) {
  62. return '';
  63. }
  64. $corePath = $modx->getOption('ugm.core_path', null, $modx->getOption('core_path', null, MODX_CORE_PATH) . 'components/upgrademodx/');
  65. $assetsUrl = $modx->getOption('ugm.assets_url', null, $modx->getOption('assets_url', null, MODX_ASSETS_URL) . 'components/upgrademodx/');
  66. require_once($corePath . 'model/upgrademodx/upgrademodx.class.php');
  67. $upgrade = new UpgradeMODX($modx);
  68. $upgrade->init();
  69. $props['ugm_setup_url'] = MODX_SITE_URL . 'setup/index.php';
  70. unset($props['controller']); // remove trash from scriptProperties
  71. $modx->regClientStartupScript('<script>
  72. var ugmConnectorUrl = "' . $assetsUrl . 'connector.php";
  73. var ugm_config = ' . $modx->toJSON($props) . ';
  74. var ugm_setup_url = "' . MODX_SITE_URL . 'setup/index.php";
  75. </script>'
  76. );
  77. $modx->regClientCSS($assetsUrl . 'css/progress.css');
  78. $modx->regClientStartupScript("//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
  79. $modx->regClientStartupScript($assetsUrl . 'js/modernizr.custom.js');
  80. $hideWhenNoUpgrade = $modx->getOption('ugm_hide_when_no_upgrade', null, false, true);
  81. $settingsVersion = $modx->getOption('settings_version');
  82. /* Set Placeholders */
  83. $placeholders = array();
  84. $placeholders['[[+ugm_assets_url]]'] = $assetsUrl;
  85. $placeholders['[[+ugm_current_version]]'] = $settingsVersion;
  86. $placeholders['[[+ugm_current_version_caption]]'] = $modx->lexicon('ugm_current_version_caption');
  87. $placeholders['[[+ugm_latest_version_caption]]'] = $modx->lexicon('ugm_latest_version_caption');
  88. $upgradeAvailable = $upgrade->upgradeAvailable($settingsVersion);
  89. $placeholders['[[+ugm_latest_version]]'] = $upgrade->getLatestVersion();
  90. if ($devMode) {
  91. $upgradeAvailable = true;
  92. }
  93. if ($upgradeAvailable) {
  94. $versionForm = $upgrade->createVersionForm($modx);
  95. }
  96. $errors = $upgrade->getErrors();
  97. if (!empty($errors)) {
  98. $msg = '';
  99. foreach ($errors as $error) {
  100. $msg .= '<br/><span style="color:red">' . $modx->lexicon('ugm_error') .
  101. ': ' . $error . '</span>';
  102. }
  103. return $msg;
  104. }
  105. /* Process */
  106. /* See if there's a new version */
  107. if ($upgradeAvailable) {
  108. $placeholders['[[+ugm_notice]]'] = $modx->lexicon('ugm_upgrade_available');
  109. $placeholders['[[+ugm_notice_color]]'] = 'green';
  110. $placeholders['[[+ugm_version_form]]'] = $versionForm;
  111. } else {
  112. if ($hideWhenNoUpgrade) {
  113. return '';
  114. } else {
  115. $placeholders['[[+ugm_notice]]'] = $modx->lexicon('ugm_modx_up_to_date');
  116. $placeholders['[[+ugm_notice_color]]'] = 'gray';
  117. }
  118. }
  119. /* Get Tpl */
  120. $tpl = $modx->getChunk('UpgradeMODXTpl');
  121. /* Do the replacements */
  122. $tpl = str_replace(array_keys($placeholders), array_values($placeholders), $tpl);
  123. /*if (php_sapi_name() === 'cli') {
  124. echo $tpl;
  125. }*/
  126. return $tpl;