PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/interface/modules/zend_modules/module/Installer/src/Installer/Controller/InstallerController.php

https://bitbucket.org/openemr/openemr
PHP | 796 lines | 600 code | 83 blank | 113 comment | 132 complexity | 36f51e46b5288a0a2a2488999602eb67 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, LGPL-3.0, BSD-3-Clause, Unlicense, MPL-2.0, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * interface/modules/zend_modules/module/Installer/src/Installer/Controller/InstallerController.php
  4. *
  5. * @package OpenEMR
  6. * @link https://www.open-emr.org
  7. * @author Jacob T.Paul <jacob@zhservices.com>
  8. * @author Vipin Kumar <vipink@zhservices.com>
  9. * @author Remesh Babu S <remesh@zhservices.com>
  10. * @author Jerry Padgett <sjpadgett@gmail.com>
  11. * @copyright Copyright (c) 2020 Jerry Padgett <sjpadgett@gmail.com>
  12. * @copyright Copyright (c) 2013 Z&H Consultancy Services Private Limited <sam@zhservices.com>
  13. * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
  14. */
  15. namespace Installer\Controller;
  16. use Laminas\Mvc\Controller\AbstractActionController;
  17. use Laminas\View\Model\ViewModel;
  18. use Laminas\View\Model\JsonModel;
  19. use Laminas\Json\Json;
  20. use Installer\Model\InstModule;
  21. use Application\Listener\Listener;
  22. use Installer\Model\InstModuleTable;
  23. use Laminas\Db\Adapter\Adapter;
  24. use OpenEMR\Common\Utils\RandomGenUtils;
  25. use Laminas\Console\Request as ConsoleRequest;
  26. use OpenEMR\Services\Utils\SQLUpgradeService;
  27. class InstallerController extends AbstractActionController
  28. {
  29. /**
  30. * @var InstModuleTable
  31. */
  32. protected $InstallerTable;
  33. protected $listenerObject;
  34. /**
  35. * @var Laminas\Db\Adapter\Adapter
  36. */
  37. private $dbAdapter;
  38. public function __construct(InstModuleTable $installerTable)
  39. {
  40. $this->listenerObject = new Listener();
  41. $this->InstallerTable = $installerTable;
  42. $this->dbAdapter = $adapter ?? null;
  43. }
  44. public function nolayout()
  45. {
  46. // Turn off the layout, i.e. only render the view script.
  47. $viewModel = new ViewModel();
  48. $viewModel->setTerminal(true);
  49. return $viewModel;
  50. }
  51. public function indexAction()
  52. {
  53. //get the list of installed and new modules
  54. $result = $this->getInstallerTable()->allModules();
  55. $allModules = array();
  56. foreach ($result as $dataArray) {
  57. $mod = new InstModule();
  58. $mod->exchangeArray($dataArray);
  59. $mod = $this->makeButtonForSqlAction($mod);
  60. $mod = $this->makeButtonForAClAction($mod);
  61. array_push($allModules, $mod);
  62. }
  63. return new ViewModel(array(
  64. 'InstallersExisting' => $allModules,
  65. 'InstallersAll' => $allModules,
  66. 'listenerObject' => $this->listenerObject,
  67. 'dependencyObject' => $this->getInstallerTable(),
  68. // TODO: @adunsulag there should be a way to pull this from application.config.php but so far the answer eludes me.
  69. 'coreModules' => ['Application', 'Acl', 'Installer', 'FHIR', 'PatientFlowBoard']
  70. ));
  71. }
  72. /**
  73. * @return Installer\Model\InstModuleTable
  74. */
  75. public function getInstallerTable(): InstModuleTable
  76. {
  77. return $this->InstallerTable;
  78. }
  79. public function registerAction()
  80. {
  81. $status = false;
  82. $request = $this->getRequest();
  83. if (method_exists($request, 'isPost')) {
  84. if ($request->getPost('mtype') == 'zend') {
  85. // TODO: We want to be able to load the modules
  86. // from the database.. however, this can be fairly slow so we might want to do some kind of APC caching of the module
  87. // list that is loaded using the OpenEMR db connector and not the zend db connector, cache the modules, and then
  88. // we can filter / update that list. We'll have to inject the unloaded module list into the installer but that is fine.
  89. $rel_path = "public/" . $request->getPost('mod_name') . "/";
  90. // registering the table inserts the module record into the database.
  91. // it's always loaded regardless, but it inserts it in the database as not activated
  92. if ($this->getInstallerTable()->register($request->getPost('mod_name'), $rel_path, 0, $GLOBALS['zendModDir'])) {
  93. $status = true;
  94. }
  95. } else {
  96. // TODO: there doesn't appear to be any methodology in how to load these custom registered modules... which seems pretty odd.
  97. // there aren't any in the system... but why have this then?
  98. $rel_path = $request->getPost('mod_name') . "/index.php";
  99. if ($this->getInstallerTable()->register($request->getPost('mod_name'), $rel_path)) {
  100. $status = true;
  101. }
  102. }
  103. die($status ? $this->listenerObject->z_xlt("Success") : $this->listenerObject->z_xlt("Failure"));
  104. } else {
  105. $moduleType = $request->getParam('mtype');
  106. $moduleName = $request->getParam('modname');
  107. if ($moduleType == 'zend') {
  108. $rel_path = "public/" . $moduleName . "/";
  109. // registering the table inserts the module record into the database.
  110. // it's always loaded regardless, but it inserts it in the database as not activated
  111. if ($this->getInstallerTable()->register($moduleName, $rel_path, 0, $GLOBALS['zendModDir'])) {
  112. $status = true;
  113. }
  114. die($status ? $this->listenerObject->z_xlt("Success") : $this->listenerObject->z_xlt("Failure"));
  115. } else {
  116. die("not supported");
  117. }
  118. }
  119. }
  120. public function manageAction()
  121. {
  122. $outputToBrowser = '';
  123. $request = $this->getRequest();
  124. $status = $this->listenerObject->z_xlt("Failure");
  125. if ($request->isPost()) {
  126. if ($request->getPost('modAction') == "enable") {
  127. $status = $this->EnableModule($request->getPost('modId'));
  128. } elseif ($request->getPost('modAction') == "disable") {
  129. $status = $this->DisableModule($request->getPost('modId'));
  130. } elseif ($request->getPost('modAction') == "install") {
  131. $modId = $request->getPost('modId');
  132. $mod_enc_menu = $request->getPost('mod_enc_menu');
  133. $mod_nick_name = $request->getPost('mod_nick_name');
  134. $status = $this->InstallModule($modId, $mod_enc_menu, $mod_nick_name);
  135. } elseif ($request->getPost('modAction') == 'install_sql') {
  136. if ($this->InstallModuleSQL($request->getPost('modId'))) {
  137. $status = $this->listenerObject->z_xlt("Success");
  138. } else {
  139. $status = $this->listenerObject->z_xlt("ERROR") . ':' . $this->listenerObject->z_xlt("could not open table") . '.' . $this->listenerObject->z_xlt("sql") . ', ' . $this->listenerObject->z_xlt("broken form") . "?";
  140. }
  141. } elseif ($request->getPost('modAction') == 'upgrade_sql') {
  142. $div = $this->UpgradeModuleSQL($request->getPost('modId'));
  143. $status = $this->listenerObject->z_xlt("Success");
  144. } elseif ($request->getPost('modAction') == 'install_acl') {
  145. if ($div = $this->InstallModuleACL($request->getPost('modId'))) {
  146. $status = $this->listenerObject->z_xlt("Success");
  147. } else {
  148. $status = $this->listenerObject->z_xlt("ERROR") . ':' . $this->listenerObject->z_xlt("could not install ACL");
  149. }
  150. } elseif ($request->getPost('modAction') == 'upgrade_acl') {
  151. if ($div = $this->UpgradeModuleACL($request->getPost('modId'))) {
  152. $status = $this->listenerObject->z_xlt("Success");
  153. } else {
  154. $status = $this->listenerObject->z_xlt("ERROR") . ':' . $this->listenerObject->z_xlt("could not install ACL");
  155. }
  156. } elseif ($request->getPost('modAction') == "unregister") {
  157. $status = $this->UnregisterModule($request->getPost('modId'));
  158. }
  159. }
  160. $output = "";
  161. if (!empty($div) && is_array($div)) {
  162. $output = implode("<br />\n", $div);
  163. }
  164. echo json_encode(["status" => $status, "output" => $output]);
  165. exit(0);
  166. }
  167. /**
  168. * @param $version
  169. * @return int|string
  170. */
  171. function upgradeAclFromVersion($ACL_UPGRADE, $version)
  172. {
  173. $toVersion = '';
  174. foreach ($ACL_UPGRADE as $toVersion => $function) {
  175. if (version_compare($version, $toVersion) < 0) {
  176. $function();
  177. }
  178. }
  179. return $toVersion;
  180. }
  181. /**
  182. * Function to install ACL for the installed modules
  183. *
  184. * @param string $dir Location of the php file which calling functions to add sections,aco etc.
  185. * @return boolean
  186. */
  187. private function installACL($dir)
  188. {
  189. $aclfile = $dir . "/moduleACL.php";
  190. if (file_exists($aclfile)) {
  191. include_once($aclfile);
  192. }
  193. }
  194. /**
  195. * Used to recreate the application config file
  196. *
  197. * @param unknown_type $data
  198. * @return string
  199. */
  200. private function getContent($data)
  201. {
  202. $string = "";
  203. foreach ($data as $key => $value) {
  204. $string .= " '$key' => ";
  205. if (is_array($value)) {
  206. $string .= " array(";
  207. $string .= $this->getContent($value);
  208. $string .= " )";
  209. } else {
  210. $string .= "'$value'";
  211. }
  212. $string .= ",";
  213. }
  214. return $string;
  215. }
  216. public function SaveHooksAction()
  217. {
  218. $request = $this->getRequest();
  219. $postArr = $request->getPost();
  220. //DELETE OLD HOOKS OF A MODULE
  221. $this->getInstallerTable()->deleteModuleHooks($postArr['mod_id']);
  222. if (!empty($postArr['hook_hanger']) && count($postArr['hook_hanger']) > 0) {
  223. foreach ($postArr['hook_hanger'] as $hookId => $hooks) {
  224. foreach ($hooks as $hangerId => $hookHanger) {
  225. $this->getInstallerTable()->saveHooks($postArr['mod_id'], $hookId, $hangerId);
  226. }
  227. }
  228. $return[0] = array('return' => 1, 'msg' => $this->listenerObject->z_xlt("Saved Successfully"));
  229. } else {
  230. $return[0] = array('return' => 1, 'msg' => $this->listenerObject->z_xlt("No Hooks enabled for this Module"));
  231. }
  232. $arr = new JsonModel($return);
  233. return $arr;
  234. }
  235. public function configureAction()
  236. {
  237. $request = $this->getRequest();
  238. $modId = $request->getPost('mod_id');
  239. /** Configuration Details */
  240. $result = $this->getInstallerTable()->getConfigSettings($modId);
  241. $configuration = array();
  242. foreach ($result as $tmp) {
  243. $configuration[$tmp['field_name']] = $tmp;
  244. }
  245. //INSERT MODULE HOOKS IF NOT EXISTS
  246. $moduleDirectory = $this->getInstallerTable()->getModuleDirectory($modId);
  247. //GET MODULE HOOKS FROM A FUNCTION IN CONFIGURATION MODEL CLASS
  248. $hooksArr = $this->getInstallerTable()->getModuleHooks($moduleDirectory);
  249. if (count($hooksArr) > 0) {
  250. foreach ($hooksArr as $hook) {
  251. if (count($hook) > 0) {
  252. if ($this->getInstallerTable()->checkModuleHookExists($modId, $hook['name']) == "0") {
  253. $this->getInstallerTable()->saveModuleHooks($modId, $hook['name'], $hook['title'], $hook['path']);
  254. }
  255. }
  256. }
  257. } else {
  258. //DELETE ADDED HOOKS TO HANGERS OF THIS MODULE, IF NO HOOKS EXIST IN THIS MODULE
  259. $this->getInstallerTable()->deleteModuleHooks($modId);
  260. //DELETE MODULE HOOKS
  261. $this->getInstallerTable()->deleteModuleHookSettings($modId);
  262. }
  263. //GET MODULE ACL SECTION FROM A FUNCTION IN CONFIGURATION MODEL CLASS
  264. $aclArray = $this->getInstallerTable()->getModuleAclSections($moduleDirectory);
  265. if (sizeof($aclArray) > 0) {
  266. $this->getInstallerTable()->insertAclSections($aclArray, $moduleDirectory, $modId);
  267. } else {
  268. $this->getInstallerTable()->deleteACLSections($modId);
  269. }
  270. $obj = $this->getInstallerTable()->getObject($moduleDirectory, 'Controller');
  271. $aclArray = array();
  272. if ($obj) {
  273. $aclArray = $obj->getAclConfig();
  274. }
  275. /** Configuration Form and Configuration Form Class */
  276. $configForm = $this->getInstallerTable()->getFormObject($moduleDirectory);
  277. /** Setup Config Details */
  278. $setup = $this->getInstallerTable()->getSetupObject($moduleDirectory);
  279. return new ViewModel(array(
  280. 'mod_id' => $modId,
  281. 'TabSettings' => $this->getInstallerTable()->getTabSettings($modId),
  282. 'ACL' => $this->getInstallerTable()->getSettings('ACL', $modId),
  283. 'OemrUserGroup' => $this->getInstallerTable()->getOemrUserGroup(),
  284. 'OemrUserGroupAroMap' => $this->getInstallerTable()->getOemrUserGroupAroMap(),
  285. 'ListActiveUsers' => $this->getInstallerTable()->getActiveUsers(),
  286. 'ListActiveACL' => $this->getInstallerTable()->getActiveACL($modId),
  287. 'ListActiveHooks' => $this->getInstallerTable()->getActiveHooks($modId),
  288. 'helperObject' => $this->helperObject,
  289. 'configuration' => $configuration,
  290. 'hangers' => $this->getInstallerTable()->getHangers(),
  291. 'Hooks' => $hooksArr,
  292. 'hookObject' => $this->getInstallerTable(),
  293. 'settings' => $configForm,
  294. 'listenerObject' => $this->listenerObject,
  295. 'setup' => $setup,
  296. ));
  297. }
  298. public function saveConfigAction()
  299. {
  300. $request = $this->getRequest();
  301. $moduleId = $request->getPost()->module_id;
  302. foreach ($request->getPost() as $key => $value) {
  303. $fieldName = $key;
  304. $fieldValue = $value;
  305. if ($fieldName != 'module_id') {
  306. $result = $this->getInstallerTable()->saveSettings($fieldName, $fieldValue, $moduleId);
  307. }
  308. }
  309. $data = array();
  310. $returnArr = array('modeId' => $moduleId);
  311. $return = new JsonModel($returnArr);
  312. return $return;
  313. }
  314. public function DeleteAclAction()
  315. {
  316. $request = $this->getRequest();
  317. $this->getInstallerTable()->DeleteAcl($request->getPost());
  318. $return[0] = array('return' => 1, 'msg' => $this->listenerObject->z_xlt("Deleted Successfully"));
  319. $arr = new JsonModel($return);
  320. return $arr;
  321. }
  322. public function DeleteHooksAction()
  323. {
  324. $request = $this->getRequest();
  325. $this->getInstallerTable()->DeleteHooks($request->getPost());
  326. $return[0] = array('return' => 1, 'msg' => $this->listenerObject->z_xlt("Deleted Successfully"));
  327. $arr = new JsonModel($return);
  328. return $arr;
  329. }
  330. public function nickNameAction()
  331. {
  332. $request = $this->getRequest();
  333. $nickname = $request->getPost()->nickname;
  334. echo $this->getInstallerTable()->validateNickName(trim($nickname));
  335. exit(0);
  336. }
  337. function getModuleVersionFromFile($modId)
  338. {
  339. //SQL version of Module
  340. $dirModule = $this->getInstallerTable()->getRegistryEntry($modId, "mod_directory");
  341. $ModulePath = $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . "zend_modules/module/" . $dirModule->modDirectory;
  342. $version_of_module = $ModulePath . "/version.php";
  343. $table_sql = $ModulePath . "/table.sql";
  344. $install_sql = $ModulePath . "/sql/install.sql";
  345. $install_acl = $ModulePath . "/acl/acl_setup.php";
  346. if (file_exists($version_of_module) && (file_exists($table_sql) || file_exists($install_sql) || file_exists($install_acl))) {
  347. include_once($version_of_module);
  348. $version = $v_major . "." . $v_minor . "." . $v_patch;
  349. return $version;
  350. }
  351. return false;
  352. }
  353. public function getFilesForUpgrade($modDirectory, $sqldir)
  354. {
  355. $ModulePath = $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . "zend_modules/module/" . $modDirectory;
  356. $versions = [];
  357. $dh = opendir($sqldir);
  358. if (!$dh) {
  359. return false;
  360. }
  361. while (false !== ($sfname = readdir($dh))) {
  362. if (substr($sfname, 0, 1) == '.') {
  363. continue;
  364. }
  365. if (preg_match('/^(\d+)_(\d+)_(\d+)-to-\d+_\d+_\d+_upgrade.sql$/', $sfname, $matches)) {
  366. $version = $matches[1] . '.' . $matches[2] . '.' . $matches[3];
  367. $versions[$version] = $sfname;
  368. }
  369. }
  370. $arrayKeys = array_keys($versions);
  371. usort($arrayKeys, 'version_compare');
  372. $sortVersions = array();
  373. foreach ($arrayKeys as $key) {
  374. $sortVersions[$key] = $versions[$key];
  375. }
  376. return $sortVersions;
  377. }
  378. public function makeButtonForSqlAction(InstModule $mod)
  379. {
  380. $dirModule = $this->getInstallerTable()->getRegistryEntry($mod->modId, "mod_directory");
  381. $ModulePath = $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . "zend_modules/module/" . $dirModule->modDirectory;
  382. $sqldir = $ModulePath . "/sql";
  383. if (!is_dir($sqldir)) {
  384. $sqldir = $ModulePath;
  385. }
  386. $mod->sql_action = "";
  387. if (file_exists($sqldir . "/install.sql") && file_exists($ModulePath . "/version.php") && empty($mod->sql_version)) {
  388. $mod->sql_action = "install";
  389. }
  390. if (!empty($mod->sql_version) && $mod->sqlRun == 1) {
  391. $versions = $this->getFilesForUpgrade($mod->modDirectory, $sqldir);
  392. if (count($versions) > 0) {
  393. foreach ($versions as $version => $sfname) {
  394. if (version_compare($version, $mod->sql_version) < 0) {
  395. continue;
  396. }
  397. $mod->sql_action = "upgrade";
  398. }
  399. }
  400. }
  401. return $mod;
  402. }
  403. public function makeButtonForACLAction(InstModule $mod)
  404. {
  405. $dirModule = $this->getInstallerTable()->getRegistryEntry($mod->modId, "mod_directory");
  406. $ModulePath = $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . "zend_modules/module/" . $dirModule->modDirectory;
  407. $sqldir = $ModulePath . "/acl";
  408. $mod->acl_action = "";
  409. if (file_exists($sqldir . "/acl_setup.php") && file_exists($ModulePath . "/version.php") && empty($mod->acl_version)) {
  410. $mod->acl_action = "install";
  411. }
  412. if (file_exists($sqldir . "/acl_upgrade.php") && file_exists($ModulePath . "/version.php") && !empty($mod->acl_version)) {
  413. global $ACL_UPGRADE;
  414. // Pass a variable, so below scripts can not be run on their own
  415. $aclSetupFlag = true;
  416. include_once($sqldir . "/acl_upgrade.php");
  417. foreach ($ACL_UPGRADE as $toVersion => $function) {
  418. if (version_compare($mod->acl_version, $toVersion) > 0) {
  419. continue;
  420. }
  421. $mod->acl_action = "upgrade";
  422. }
  423. }
  424. return $mod;
  425. }
  426. /**
  427. * @param $moduleName
  428. * @return bool
  429. */
  430. public function getModuleId($moduleName)
  431. {
  432. if (empty($moduleName)) {
  433. return false;
  434. }
  435. $allModules = $this->getInstallerTable()->allModules();
  436. foreach ($allModules as $module) {
  437. if ($module["mod_directory"] === $moduleName) {
  438. return $module["mod_id"];
  439. }
  440. }
  441. }
  442. /**
  443. * @param string $modId
  444. * @return bool
  445. */
  446. public function InstallModuleSQL($modId = '')
  447. {
  448. $registryEntry = $this->getInstallerTable()->getRegistryEntry($modId, "mod_directory");
  449. $dirModule = $registryEntry->modDirectory;
  450. $modType = $registryEntry->type;
  451. if ($this->getInstallerTable()->installSQL($modId, $modType, $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . "zend_modules/module/" . $dirModule)) {
  452. $values = array($registryEntry->mod_nick_name,$registryEntry->mod_enc_menu);
  453. $values[2] = $this->getModuleVersionFromFile($modId);
  454. $values[3] = $registryEntry->acl_version;
  455. $this->getInstallerTable()->updateRegistered($modId, '', $values);
  456. return true;
  457. } else {
  458. return false;
  459. }
  460. }
  461. /**
  462. * @param string $modId
  463. * @return array
  464. */
  465. public function UpgradeModuleSQL($modId = '')
  466. {
  467. $Module = $this->getInstallerTable()->getRegistryEntry($modId, "mod_directory");
  468. $modDir = $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . "zend_modules/module/" . $Module->modDirectory;
  469. $sqlInstallLocation = $modDir . '/sql';
  470. // if this is a custom module that for some reason doesn't have the SQL in a sql folder...
  471. if (!file_exists($sqlInstallLocation)) {
  472. $sqlInstallLocation = $modDir;
  473. }
  474. $versions = $this->getFilesForUpgrade($Module->modDirectory, $sqlInstallLocation);
  475. $values = array($Module->mod_nick_name,$Module->mod_enc_menu);
  476. $div = [];
  477. $outputToBrowser = '';
  478. foreach ($versions as $version => $filename) {
  479. if (version_compare($version, $Module->sql_version) < 0) {
  480. continue;
  481. }
  482. ob_start();
  483. $sqlUpgradeService = new SQLUpgradeService();
  484. $sqlUpgradeService->setRenderOutputToScreen(true);
  485. $sqlUpgradeService->upgradeFromSqlFile($filename, $sqlInstallLocation);
  486. $outputToBrowser .= ob_get_contents();
  487. ob_end_clean();
  488. }
  489. if (preg_match_all("/(.*)\<br \/\>\n/i", $outputToBrowser, $matches)) {
  490. $add_query_string = 0;
  491. $add_ended_divs = 0;
  492. $k = 0;
  493. foreach ($matches[1] as $string) {
  494. $prev_html_tag = false;
  495. if (preg_match("/<([a-z]+).*?>([^<]+)<\/([a-z]+)>/i", $string, $mm)) {
  496. if ($add_query_string > 0) {
  497. $div[] = "</div>";
  498. $add_ended_divs++;
  499. }
  500. $div[] = $string;
  501. $prev_html_tag = true;
  502. $curr_html_tag = true;
  503. }
  504. if (!$prev_html_tag && $curr_html_tag) {
  505. $div[] = "<div class='show_hide_log'>" . xlt("show/hide executed query log") . "</div><div class='spoiler' style='margin-left: 10px' >" . $string;
  506. $curr_html_tag = false;
  507. } elseif (!$prev_html_tag && !$curr_html_tag) {
  508. $div[] = $string;
  509. $add_query_string++;
  510. }
  511. if (count($matches[1]) == (count($div) - $add_ended_divs) && (!$prev_html_tag && !$curr_html_tag)) {
  512. $div[] = "</div>";
  513. }
  514. $k++;
  515. }
  516. }
  517. $values[2] = $this->getModuleVersionFromFile($modId);
  518. $values[3] = $Module->acl_version;
  519. $this->getInstallerTable()->updateRegistered($modId, '', $values);
  520. return $div;
  521. }
  522. /**
  523. * @param string $modId
  524. * @return bool
  525. */
  526. public function InstallModuleACL($modId = '')
  527. {
  528. $Module = $this->getInstallerTable()->getRegistryEntry($modId, "mod_directory");
  529. $modDir = $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . "zend_modules/module/" . $Module->modDirectory;
  530. $div = [];
  531. if (file_exists($modDir . "/acl/acl_setup.php") && empty($modDir->acl_version)) {
  532. // Pass a variable, so below scripts can not be run on their own
  533. $aclSetupFlag = true;
  534. ob_start();
  535. include_once($modDir . "/acl/acl_setup.php");
  536. $div[] = ob_get_contents();
  537. ob_end_clean();
  538. $values = array($Module->mod_nick_name,$Module->mod_enc_menu);
  539. $values[2] = $Module->sql_version;
  540. $values[3] = $this->getModuleVersionFromFile($modId);
  541. $this->getInstallerTable()->updateRegistered($modId, '', $values);
  542. return $div;
  543. }
  544. return false;
  545. }
  546. /**
  547. * Function to Enable Module
  548. *
  549. * @param string $dir Location of the php file which calling functions to add sections,aco etc.
  550. * @return boolean
  551. */
  552. public function EnableModule($modId = '')
  553. {
  554. $resp = $this->getInstallerTable()->updateRegistered($modId, "mod_active=0");
  555. if ($resp['status'] == 'failure' && $resp['code'] == '200') {
  556. $status = $resp['value'];
  557. } else {
  558. $status = $this->listenerObject->z_xlt("Success");
  559. }
  560. return $status;
  561. }
  562. /**
  563. * Function to Disable Module
  564. *
  565. * @param string $dir Location of the php file which calling functions to add sections,aco etc.
  566. * @return boolean
  567. */
  568. public function DisableModule($modId = '')
  569. {
  570. $resp = $this->getInstallerTable()->updateRegistered($modId, "mod_active=1");
  571. if ($resp['status'] == 'failure' && $resp['code'] == '200') {
  572. $plural = "Module";
  573. if (count($resp['value']) > 1) {
  574. $plural = "Modules";
  575. }
  576. $status = $this->listenerObject->z_xlt("Dependency Problem") . ':' . implode(", ", $resp['value']) . " " . $this->listenerObject->z_xlt($plural) . " " . $this->listenerObject->z_xlt("Should be Enabled");
  577. } elseif ($resp['status'] == 'failure' && ($resp['code'] == '300' || $resp['code'] == '400')) {
  578. $status = $resp['value'];
  579. } else {
  580. $status = $this->listenerObject->z_xlt("Success");
  581. }
  582. return $status;
  583. }
  584. /**
  585. * Function to Install Module
  586. *
  587. * @param string $dir Location of the php file which calling functions to add sections,aco etc.
  588. * @return boolean
  589. */
  590. public function InstallModule($modId = '', $mod_enc_menu = '', $mod_nick_name = '')
  591. {
  592. $registryEntry = $this->getInstallerTable()->getRegistryEntry($modId, "mod_directory");
  593. $modType = $registryEntry->type;
  594. $dirModule = $registryEntry->modDirectory;
  595. $sqlInstalled = false;
  596. if ($modType == InstModuleTable::MODULE_TYPE_CUSTOM) {
  597. $fullDirectory = $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . $GLOBALS['customModDir'] . "/" . $dirModule;
  598. if ($this->getInstallerTable()->installSQL($modId, $modType, $fullDirectory)) {
  599. $sqlInstalled = true;
  600. } else {
  601. // TODO: This is a wierd error... why is it written like this?
  602. $status = $this->listenerObject->z_xlt("ERROR") . ':' . $this->listenerObject->z_xlt("could not open table") . '.' . $this->listenerObject->z_xlt("sql") . ', ' . $this->listenerObject->z_xlt("broken form") . "?";
  603. }
  604. } else if ($modType == InstModuleTable::MODULE_TYPE_ZEND) {
  605. $fullDirectory = $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . "zend_modules/module/" . $dirModule;
  606. if ($this->getInstallerTable()->installSQL($modId, $modType, $fullDirectory)) {
  607. $sqlInstalled = true;
  608. } else {
  609. $status = $this->listenerObject->z_xlt("ERROR") . ':' . $this->listenerObject->z_xlt("could not run sql query");
  610. }
  611. }
  612. if ($sqlInstalled) {
  613. $values = array($mod_nick_name, $mod_enc_menu);
  614. $values[2] = $this->getModuleVersionFromFile($modId);
  615. $this->getInstallerTable()->updateRegistered($modId, '', $values);
  616. $status = $this->listenerObject->z_xlt("Success");
  617. }
  618. return $status;
  619. }
  620. /**
  621. * Function to Unregister Module
  622. *
  623. * @param string $dir Location of the php file which calling functions to add sections,aco etc.
  624. * @return boolean
  625. */
  626. public function UnregisterModule($modId = '')
  627. {
  628. $resp = $this->getInstallerTable()->unRegister($modId);
  629. if ($resp == 'failure') {
  630. $status = $this->listenerObject->z_xlt("ERROR") . ':' . $this->listenerObject->z_xlt("Failed to unregister module.");
  631. } else {
  632. $status = $this->listenerObject->z_xlt("Success");
  633. }
  634. return $status;
  635. }
  636. /**
  637. * @param string $modId
  638. * @return array|bool
  639. */
  640. public function UpgradeModuleACL($modId = '')
  641. {
  642. $Module = $this->getInstallerTable()->getRegistryEntry($modId, "mod_directory");
  643. $modDir = $GLOBALS['srcdir'] . "/../" . $GLOBALS['baseModDir'] . "zend_modules/module/" . $Module->modDirectory;
  644. $div = [];
  645. if (file_exists($modDir . "/acl/acl_upgrade.php") && !empty($Module->acl_version)) {
  646. // Pass a variable, so below scripts can not be run on their own
  647. $aclSetupFlag = true;
  648. ob_start();
  649. $ACL_UPGRADE = include_once($modDir . "/acl/acl_upgrade.php");
  650. $version = $this->upgradeAclFromVersion($ACL_UPGRADE, $Module->acl_version);
  651. $div[] = ob_get_contents();
  652. ob_end_clean();
  653. if (strlen($version) > 0) {
  654. $values = array($Module->mod_nick_name,$Module->mod_enc_menu);
  655. $values[2] = $Module->sql_version;
  656. $values[3] = $this->getModuleVersionFromFile($modId);
  657. $this->getInstallerTable()->updateRegistered($modId, '', $values);
  658. }
  659. return $div;
  660. }
  661. return false;
  662. }
  663. /**
  664. *
  665. */
  666. public function commandInstallModuleAction()
  667. {
  668. $request = $this->getRequest();
  669. if (!$request instanceof ConsoleRequest) {
  670. throw new RuntimeException('You can only use this action from a console!');
  671. }
  672. $moduleAction = $request->getParam('modaction');
  673. $moduleName = $request->getParam('modname');
  674. $moduleId = null;
  675. $div = [];
  676. echo PHP_EOL . '--- Run command [' . $moduleAction . '] in module: ' . $moduleName . '---' . PHP_EOL;
  677. echo 'start process - ' . date('Y-m-d H:i:s') . PHP_EOL;
  678. if (!empty($moduleAction) && !empty($moduleName) && $moduleName != "all") {
  679. $moduleId = $this->getModuleId($moduleName);
  680. }
  681. if ($moduleId !== null) {
  682. echo 'module [' . $moduleName . '] was found' . PHP_EOL;
  683. $msg = "command completed successfully";
  684. if ($moduleAction === "install_sql") {
  685. $this->InstallModuleSQL($moduleId);
  686. } elseif ($moduleAction === "upgrade_sql") {
  687. $div = $this->UpgradeModuleSQL($moduleId);
  688. } elseif ($moduleAction === "install_acl") {
  689. $div = $this->InstallModuleACL($moduleId);
  690. } elseif ($moduleAction === "upgrade_acl") {
  691. $div = $this->UpgradeModuleACL($moduleId);
  692. } elseif ($moduleAction === "enable") {
  693. $div = $this->DisableModule($moduleId);
  694. } elseif ($moduleAction === "disable") {
  695. $div = $this->EnableModule($moduleId);
  696. } elseif ($moduleAction === "install") {
  697. $div = $this->InstallModule($moduleId);
  698. } elseif ($moduleAction === "unregister") {
  699. $div = $this->UnregisterModule($moduleId);
  700. } else {
  701. $msg = 'Unsupported command';
  702. }
  703. } else {
  704. $msg = "module Id is null";
  705. }
  706. $output = "";
  707. if (is_array($div)) {
  708. $output = implode("<br />\n", $div) . PHP_EOL;
  709. }
  710. echo $output;
  711. exit($msg . PHP_EOL);
  712. }
  713. }