PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/CorePluginsAdmin/UpdateCommunication.php

https://github.com/CodeYellowBV/piwik
PHP | 210 lines | 133 code | 37 blank | 40 comment | 15 complexity | 7137e3c0853b6c407e76ce1512125fc2 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\Plugins\CorePluginsAdmin;
  10. use Piwik\Config;
  11. use Piwik\Mail;
  12. use Piwik\Option;
  13. use Piwik\Piwik;
  14. use Piwik\Plugins\UsersManager\API as UsersManagerApi;
  15. use Piwik\SettingsPiwik;
  16. /**
  17. * Class to check and notify users via email if there are plugin updates available.
  18. */
  19. class UpdateCommunication
  20. {
  21. private $enabledOptionName = 'enableUpdateCommunicationPlugins';
  22. /**
  23. * Checks whether plugin update notification is enabled or not. If the marketplace is disabled or if update
  24. * communication is disabled in general, it will return false as well.
  25. *
  26. * @return bool
  27. */
  28. public function isEnabled()
  29. {
  30. if (!$this->canBeEnabled()) {
  31. return false;
  32. }
  33. $isEnabled = Option::get($this->enabledOptionName);
  34. return !empty($isEnabled);
  35. }
  36. /**
  37. * Checks whether a plugin update notification can be enabled or not. It cannot be enabled if for instance the
  38. * Marketplace is disabled or if update notifications are disabled in general.
  39. *
  40. * @return bool
  41. */
  42. public function canBeEnabled()
  43. {
  44. $isEnabled = Config::getInstance()->General['enable_update_communication'];
  45. return CorePluginsAdmin::isMarketplaceEnabled() && !empty($isEnabled);
  46. }
  47. /**
  48. * Enable plugin update notifications.
  49. */
  50. public function enable()
  51. {
  52. Option::set($this->enabledOptionName, 1);
  53. }
  54. /**
  55. * Disable plugin update notifications.
  56. */
  57. public function disable()
  58. {
  59. Option::set($this->enabledOptionName, 0);
  60. }
  61. /**
  62. * Sends an email to all super users if there is an update available for any plugins from the Marketplace.
  63. * For each update we send an email only once.
  64. *
  65. * @return bool
  66. */
  67. public function sendNotificationIfUpdatesAvailable()
  68. {
  69. $pluginsHavingUpdate = $this->getPluginsHavingUpdate();
  70. if (empty($pluginsHavingUpdate)) {
  71. return;
  72. }
  73. $pluginsToBeNotified = array();
  74. foreach ($pluginsHavingUpdate as $plugin) {
  75. if ($this->hasNotificationAlreadyReceived($plugin)) {
  76. continue;
  77. }
  78. $this->setHasLatestUpdateNotificationReceived($plugin);
  79. $pluginsToBeNotified[] = $plugin;
  80. }
  81. if (!empty($pluginsToBeNotified)) {
  82. $this->sendNotifications($pluginsToBeNotified);
  83. }
  84. }
  85. protected function sendNotifications($pluginsToBeNotified)
  86. {
  87. $hasThemeUpdate = false;
  88. $hasPluginUpdate = false;
  89. foreach ($pluginsToBeNotified as $plugin) {
  90. $hasThemeUpdate = $hasThemeUpdate || $plugin['isTheme'];
  91. $hasPluginUpdate = $hasPluginUpdate || !$plugin['isTheme'];
  92. }
  93. $subject = Piwik::translate('CoreUpdater_NotificationSubjectAvailablePluginUpdate');
  94. $message = Piwik::translate('ScheduledReports_EmailHello');
  95. $message .= "\n\n";
  96. $message .= Piwik::translate('CoreUpdater_ThereIsNewPluginVersionAvailableForUpdate');
  97. $message .= "\n\n";
  98. foreach ($pluginsToBeNotified as $plugin) {
  99. $message .= sprintf(' * %s %s', $plugin['name'], $plugin['latestVersion']);
  100. $message .= "\n";
  101. }
  102. $message .= "\n";
  103. $host = SettingsPiwik::getPiwikUrl();
  104. if ($hasThemeUpdate) {
  105. $message .= Piwik::translate('CoreUpdater_NotificationClickToUpdateThemes') . "\n";
  106. $message .= $host. 'index.php?module=CorePluginsAdmin&action=themes';
  107. }
  108. if ($hasPluginUpdate) {
  109. if ($hasThemeUpdate) {
  110. $message .= "\n\n";
  111. }
  112. $message .= Piwik::translate('CoreUpdater_NotificationClickToUpdatePlugins') . "\n";
  113. $message .= $host. 'index.php?module=CorePluginsAdmin&action=plugins';
  114. }
  115. $message .= "\n\n";
  116. $message .= Piwik::translate('Installation_HappyAnalysing');
  117. $this->sendEmailNotification($subject, $message);
  118. }
  119. /**
  120. * Send an email notification to all super users.
  121. *
  122. * @param $subject
  123. * @param $message
  124. */
  125. protected function sendEmailNotification($subject, $message)
  126. {
  127. $superUsers = UsersManagerApi::getInstance()->getUsersHavingSuperUserAccess();
  128. foreach ($superUsers as $superUser) {
  129. $mail = new Mail();
  130. $mail->setDefaultFromPiwik();
  131. $mail->addTo($superUser['email']);
  132. $mail->setSubject($subject);
  133. $mail->setBodyText($message);
  134. $mail->send();
  135. }
  136. }
  137. private function setHasLatestUpdateNotificationReceived($plugin)
  138. {
  139. $latestVersion = $this->getLatestVersion($plugin);
  140. Option::set($this->getNotificationSentOptionName($plugin), $latestVersion);
  141. }
  142. private function getLatestVersionSent($plugin)
  143. {
  144. return Option::get($this->getNotificationSentOptionName($plugin));
  145. }
  146. private function getLatestVersion($plugin)
  147. {
  148. return $plugin['latestVersion'];
  149. }
  150. private function hasNotificationAlreadyReceived($plugin)
  151. {
  152. $latestVersion = $this->getLatestVersion($plugin);
  153. $lastVersionSent = $this->getLatestVersionSent($plugin);
  154. if (!empty($lastVersionSent)
  155. && ($latestVersion == $lastVersionSent
  156. || version_compare($latestVersion, $lastVersionSent) == -1)) {
  157. return true;
  158. }
  159. return false;
  160. }
  161. private function getNotificationSentOptionName($plugin)
  162. {
  163. return 'last_update_communication_sent_plugin_' . $plugin['name'];
  164. }
  165. protected function getPluginsHavingUpdate()
  166. {
  167. $marketplace = new Marketplace();
  168. $pluginsHavingUpdate = $marketplace->getPluginsHavingUpdate($themesOnly = false);
  169. $themesHavingUpdate = $marketplace->getPluginsHavingUpdate($themesOnly = true);
  170. $plugins = array_merge($pluginsHavingUpdate, $themesHavingUpdate);
  171. return $plugins;
  172. }
  173. }