PageRenderTime 67ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/seotoaster_core/application/controllers/Backend/PluginController.php

https://github.com/seotoaster-team/seotoaster
PHP | 276 lines | 250 code | 25 blank | 1 comment | 52 complexity | 566e6312eb8a390f5aa99dad2ccf13bf MD5 | raw file
  1. <?php
  2. class Backend_PluginController extends Zend_Controller_Action {
  3. public static $_allowedActions = array(
  4. 'fireaction'
  5. );
  6. public function init() {
  7. parent::init();
  8. if(!Tools_Security_Acl::isAllowed(Tools_Security_Acl::RESOURCE_PAGE_PUBLIC)) {
  9. $this->redirect($this->_helper->website->getUrl(), array('exit' => true));
  10. }
  11. if(!Tools_Security_Acl::isActionAllowed(Tools_Security_Acl::RESOURCE_PLUGINS)) {
  12. $this->redirect($this->_helper->website->getUrl(), array('exit' => true));
  13. }
  14. $this->_helper->AjaxContext()->addActionContext('triggerinstall', 'json')->initContext('json');
  15. $this->_helper->AjaxContext()->addActionContext('trigger', 'json')->initContext('json');
  16. $this->_helper->AjaxContext()->addActionContext('delete', 'json')->initContext('json');
  17. $this->_helper->AjaxContext()->addActionContext('list', 'json')->initContext('json');
  18. $this->view->websiteUrl = $this->_helper->website->getUrl();
  19. }
  20. public function pluginAction() {
  21. $this->view->plugins = $this->_getPreparedPlugins();
  22. $this->view->helpSection = 'plugins';
  23. }
  24. public function listAction() {
  25. $this->view->plugins = $this->_getPreparedPlugins();
  26. $this->view->pluginsList = $this->view->render('backend/plugin/list.phtml');
  27. }
  28. public function readmeAction()
  29. {
  30. if ($this->getRequest()->isPost()) {
  31. $pluginName = $this->getRequest()->getParam('pluginName');
  32. $miscData = Zend_Registry::get('misc');
  33. $readmePath = $miscData['pluginsPath'] . $pluginName . '/readme.txt';
  34. $readmeText = '';
  35. if (is_readable($readmePath)) {
  36. $readmeText =nl2br(htmlspecialchars(file_get_contents($readmePath)));
  37. }
  38. if (empty($readmeText)) {
  39. $this->_helper->response->fail($this->_helper->language->translate('Can\'t access readme file'));
  40. } else {
  41. $this->_helper->response->success($readmeText);
  42. }
  43. }
  44. }
  45. private function _getPreparedPlugins() {
  46. $prepared = array();
  47. $plugins = Tools_Plugins_Tools::findAvailablePlugins();
  48. if(!empty ($plugins)) {
  49. foreach ($plugins as $pluginName) {
  50. $plugin = Tools_Plugins_Tools::findPluginByName($pluginName);
  51. $preview = $plugin->getPreview();
  52. $previewPath = str_replace($this->_helper->website->getUrl(), $this->_helper->website->getPath(), $preview);
  53. if(!$preview || !file_exists($previewPath)) {
  54. $plugin->setPreview($this->_helper->website->getUrl() . 'system/images/noimage.png');
  55. }
  56. $prepared[] = $plugin;
  57. }
  58. }
  59. $secureToken = Tools_System_Tools::initSecureToken(Tools_System_Tools::ACTION_PREFIX_PLUGINS);
  60. $this->view->secureToken = $secureToken;
  61. return $prepared;
  62. }
  63. public function triggerinstallAction() {
  64. if ($this->getRequest()->isPost()) {
  65. $tokenToValidate = $this->getRequest()->getParam(Tools_System_Tools::CSRF_SECURE_TOKEN, false);
  66. $valid = Tools_System_Tools::validateToken($tokenToValidate, Tools_System_Tools::ACTION_PREFIX_PLUGINS);
  67. if (!$valid) {
  68. exit;
  69. }
  70. $pluginMapper = Application_Model_Mappers_PluginMapper::getInstance();
  71. $plugin = Tools_Plugins_Tools::findPluginByName($this->getRequest()->getParam('name'));
  72. $miscData = Zend_Registry::get('misc');
  73. if ($plugin->getStatus() == Application_Model_Models_Plugin::DISABLED && $plugin->getId() == null) {
  74. $statusFile = Application_Model_Models_Plugin::INSTALL_FILE_NAME;
  75. $observerAction = Tools_Plugins_GarbageCollector::CLEAN_ONCREATE;
  76. } else {
  77. $statusFile = Application_Model_Models_Plugin::UNINSTALL_FILE_NAME;
  78. $observerAction = Tools_Plugins_GarbageCollector::CLEAN_ONDELETE;
  79. }
  80. if ($observerAction === Tools_Plugins_GarbageCollector::CLEAN_ONCREATE) {
  81. $pluginDependencyFilePath = $this->_helper->website->getPath() . $miscData['pluginsPath'] .
  82. $plugin->getName() . DIRECTORY_SEPARATOR . 'system'. DIRECTORY_SEPARATOR . Application_Model_Models_Plugin::DEPENDENCY_FILE_NAME;
  83. $loaderCanExec = Tools_Plugins_Tools::loaderCanExec($plugin->getName());
  84. if ($loaderCanExec === false) {
  85. $this->_helper->response->fail($this->_helper->language->translate('You must install "IonCube Loaderâ„¢" extension before start using the plugin.'));
  86. }
  87. if (file_exists($pluginDependencyFilePath)) {
  88. $pluginDependencyContent = Tools_Filesystem_Tools::getFile($pluginDependencyFilePath);
  89. if (!empty($pluginDependencyContent)) {
  90. $enabledPlugins = Tools_Plugins_Tools::getEnabledPlugins(true);
  91. $dependentPluginsData = explode(';', $pluginDependencyContent);
  92. $dependentPlugins = array();
  93. array_walk(
  94. $dependentPluginsData,
  95. function ($dependentPlugin) use (&$dependentPlugins) {
  96. $replace = array("\r", "\n", '\r', '\n');
  97. $dependentPlugins[] = str_replace($replace, '', $dependentPlugin);
  98. }
  99. );
  100. $missingPlugins = array_diff($dependentPlugins, $enabledPlugins);
  101. $missingPlugins = array_filter($missingPlugins);
  102. if (!empty($missingPlugins)) {
  103. $missingPluginError = $this->_helper->language->translate(
  104. 'Plugins that should be installed first'
  105. ) . ' ';
  106. foreach ($missingPlugins as $plug) {
  107. $missingPluginError .= $plug . ', ';
  108. }
  109. $this->_helper->response->fail(rtrim($missingPluginError, ', '));
  110. }
  111. }
  112. }
  113. }
  114. if ($observerAction === Tools_Plugins_GarbageCollector::CLEAN_ONCREATE) {
  115. $pluginId = intval($pluginMapper->save($plugin, false));
  116. }
  117. $sqlFilePath = $this->_helper->website->getPath().$miscData['pluginsPath'].$plugin->getName().'/system/'.$statusFile;
  118. if (file_exists($sqlFilePath)) {
  119. try {
  120. $sqlFileContent = Tools_Filesystem_Tools::getFile($sqlFilePath);
  121. if (strlen($sqlFileContent)) {
  122. $queries = Tools_System_SqlSplitter::split($sqlFileContent);
  123. if (is_array($queries) && !empty ($queries)) {
  124. $dbAdapter = Zend_Registry::get('dbAdapter');
  125. try {
  126. array_walk($queries, function($query) use ($dbAdapter) {
  127. if(strlen(trim($query))) {
  128. $dbAdapter->query($query);
  129. }
  130. });
  131. }
  132. catch (Exception $e) {
  133. error_log($e->getMessage());
  134. $pluginMapper->deleteByName($plugin);
  135. $this->_helper->response->fail($e->getMessage());
  136. }
  137. }
  138. }
  139. }
  140. catch (Exceptions_SeotoasterPluginException $se) {
  141. error_log($se->getMessage());
  142. $this->_helper->response->fail($se->getMessage());
  143. }
  144. }
  145. $plugin->registerObserver(
  146. new Tools_Plugins_GarbageCollector(
  147. array('action' => $observerAction)
  148. )
  149. );
  150. if ($plugin->getStatus() == Application_Model_Models_Plugin::DISABLED && $plugin->getId() == null) {
  151. $pluginData = $pluginMapper->getPluginDataById($pluginId);
  152. if(!empty($pluginData)){
  153. $plugin->setTags($pluginData['tags']);
  154. $plugin->setVersion($pluginData['version']);
  155. }
  156. $plugin->setId($pluginId);
  157. $plugin->setStatus(Application_Model_Models_Plugin::ENABLED);
  158. $pluginMapper->save($plugin);
  159. $this->view->buttonText = 'Uninstall';
  160. $this->view->endisButton = true;
  161. }
  162. elseif ($plugin->getStatus() == Application_Model_Models_Plugin::ENABLED || $plugin->getStatus() == Application_Model_Models_Plugin::DISABLED && $plugin->getId() != null) {
  163. $pluginMapper->delete($plugin);
  164. $this->view->buttonText = 'Install';
  165. $this->view->endisButton = false;
  166. }
  167. $this->_helper->cache->clean(null, null, array('plugins'));
  168. $this->_helper->cache->clean('admin_addmenu', $this->_helper->session->getCurrentUser()->getRoleId());
  169. }
  170. }
  171. public function triggerAction()
  172. {
  173. if ($this->getRequest()->isPost()) {
  174. $tokenToValidate = $this->getRequest()->getParam(Tools_System_Tools::CSRF_SECURE_TOKEN, false);
  175. $valid = Tools_System_Tools::validateToken($tokenToValidate, Tools_System_Tools::ACTION_PREFIX_PLUGINS);
  176. if (!$valid) {
  177. exit;
  178. }
  179. $plugin = Tools_Plugins_Tools::findPluginByName($this->getRequest()->getParam('name'));
  180. $plugin->registerObserver(
  181. new Tools_Plugins_GarbageCollector(array('action' => Tools_System_GarbageCollector::CLEAN_ONUPDATE))
  182. );
  183. if ($plugin->getStatus() == Application_Model_Models_Plugin::ENABLED) {
  184. $plugin->setStatus(Application_Model_Models_Plugin::DISABLED);
  185. $buttonText = 'Enable';
  186. } else {
  187. $plugin->setStatus(Application_Model_Models_Plugin::ENABLED);
  188. $buttonText = 'Disable';
  189. }
  190. $this->view->responseText = Application_Model_Mappers_PluginMapper::getInstance()->save($plugin);
  191. $this->view->buttonText = $buttonText;
  192. $this->_helper->cache->clean('admin_addmenu', $this->_helper->session->getCurrentUser()->getRoleId());
  193. }
  194. }
  195. public function deleteAction() {
  196. if($this->getRequest()->isDelete()) {
  197. $plugin = Tools_Plugins_Tools::findPluginByName($this->getRequest()->getParam('id'));
  198. $plugin->registerObserver(new Tools_Plugins_GarbageCollector(array(
  199. 'action' => Tools_System_GarbageCollector::CLEAN_ONDELETE
  200. )));
  201. $miscData = Zend_Registry::get('misc');
  202. $sqlFilePath = $this->_helper->website->getPath() . $miscData['pluginsPath'] . $plugin->getName() . '/system/' .
  203. (Application_Model_Models_Plugin::UNINSTALL_FILE_NAME);
  204. if(file_exists($sqlFilePath)) {
  205. $sqlFileContent = Tools_Filesystem_Tools::getFile($sqlFilePath);
  206. if(strlen($sqlFileContent)) {
  207. $queries = Tools_System_SqlSplitter::split($sqlFileContent);
  208. }
  209. }
  210. $delete = Tools_Filesystem_Tools::deleteDir($this->_helper->website->getPath() . 'plugins/' . $plugin->getName());
  211. if(!$delete) {
  212. $this->_helper->response->fail('Can\'t remove plugin\'s directory (not enough permissions). Plugin was uninstalled.');
  213. exit;
  214. }
  215. if(is_array($queries) && !empty ($queries)) {
  216. $dbAdapter = Zend_Registry::get('dbAdapter');
  217. try {
  218. array_walk($queries, function($query, $key, $adapter) {
  219. if(strlen(trim($query))) {
  220. $adapter->query($query);
  221. }
  222. }, $dbAdapter);
  223. Application_Model_Mappers_PluginMapper::getInstance()->delete($plugin);
  224. }
  225. catch (Exception $e) {
  226. error_log($e->getMessage());
  227. $this->_helper->response->fail($e->getMessage());
  228. }
  229. }
  230. $this->_helper->cache->clean(null, null, array('plugins'));
  231. $this->_helper->cache->clean('admin_addmenu', $this->_helper->session->getCurrentUser()->getRoleId());
  232. $this->_helper->response->success('Removed');
  233. }
  234. }
  235. public function fireactionAction() {
  236. $this->_helper->viewRenderer->setNoRender(true);
  237. $pluginName = $this->getRequest()->getParam('name');
  238. //we will fire the action in the case when plugin is enabled
  239. $toasterPlugin = Application_Model_Mappers_PluginMapper::getInstance()->findByName($pluginName);
  240. if(($toasterPlugin instanceof Application_Model_Models_Plugin) && ($toasterPlugin->getStatus() == Application_Model_Models_Plugin::ENABLED)) {
  241. $pageData = array('websiteUrl' => $this->_helper->website->getUrl());
  242. try {
  243. $plugin = Tools_Factory_PluginFactory::createPlugin($pluginName, array(), $pageData);
  244. $plugin->run($this->getRequest()->getParams());
  245. }
  246. catch (Exception $e) {
  247. die($e->getMessage());
  248. }
  249. }
  250. }
  251. }