PageRenderTime 52ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/module/module.controller.php

https://github.com/prologos/xe-core
PHP | 1368 lines | 966 code | 197 blank | 205 comment | 178 complexity | 8bf975c3b45d5c6054851858e06d548a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /* Copyright (C) NAVER <http://www.navercorp.com> */
  3. /**
  4. * @class moduleController
  5. * @author NAVER (developers@xpressengine.com)
  6. * @brief controller class of the module module
  7. */
  8. class moduleController extends module
  9. {
  10. /**
  11. * @brief Initialization
  12. */
  13. function init()
  14. {
  15. }
  16. /**
  17. * @brief Add action forward
  18. * Action forward finds and forwards if an action is not in the requested module
  19. * This is used when installing a module
  20. */
  21. function insertActionForward($module, $type, $act)
  22. {
  23. $args = new stdClass();
  24. $args->module = $module;
  25. $args->type = $type;
  26. $args->act = $act;
  27. $output = executeQuery('module.insertActionForward', $args);
  28. $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE);
  29. if($oCacheHandler->isSupport())
  30. {
  31. $cache_key = 'action_forward';
  32. $oCacheHandler->delete($cache_key);
  33. }
  34. return $output;
  35. }
  36. /**
  37. * @brief Delete action forward
  38. */
  39. function deleteActionForward($module, $type, $act)
  40. {
  41. $args = new stdClass();
  42. $args->module = $module;
  43. $args->type = $type;
  44. $args->act = $act;
  45. $output = executeQuery('module.deleteActionForward', $args);
  46. $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE);
  47. if($oCacheHandler->isSupport())
  48. {
  49. $cache_key = 'action_forward';
  50. $oCacheHandler->delete($cache_key);
  51. }
  52. return $output;
  53. }
  54. /**
  55. * @brief Add module trigger
  56. * module trigger is to call a trigger to a target module
  57. *
  58. */
  59. function insertTrigger($trigger_name, $module, $type, $called_method, $called_position)
  60. {
  61. $args = new stdClass();
  62. $args->trigger_name = $trigger_name;
  63. $args->module = $module;
  64. $args->type = $type;
  65. $args->called_method = $called_method;
  66. $args->called_position = $called_position;
  67. $output = executeQuery('module.insertTrigger', $args);
  68. //remove from cache
  69. $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE);
  70. if($oCacheHandler->isSupport())
  71. {
  72. $oCacheHandler->invalidateGroupKey('triggers');
  73. }
  74. // Delete all the files which contain trigger information
  75. FileHandler::removeFilesInDir("./files/cache/triggers");
  76. return $output;
  77. }
  78. /**
  79. * @brief Delete module trigger
  80. *
  81. */
  82. function deleteTrigger($trigger_name, $module, $type, $called_method, $called_position)
  83. {
  84. $args = new stdClass();
  85. $args->trigger_name = $trigger_name;
  86. $args->module = $module;
  87. $args->type = $type;
  88. $args->called_method = $called_method;
  89. $args->called_position = $called_position;
  90. $output = executeQuery('module.deleteTrigger', $args);
  91. //remove from cache
  92. $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE);
  93. if($oCacheHandler->isSupport())
  94. {
  95. $oCacheHandler->invalidateGroupKey('triggers');
  96. }
  97. // Remove the trigger cache
  98. FileHandler::removeFilesInDir('./files/cache/triggers');
  99. return $output;
  100. }
  101. /**
  102. * @brief Add module extend
  103. *
  104. */
  105. function insertModuleExtend($parent_module, $extend_module, $type, $kind='')
  106. {
  107. if($kind != 'admin') $kind = '';
  108. if(!in_array($type,array('model','controller','view','api','mobile'))) return false;
  109. if(in_array($parent_module, array('module','addon','widget','layout'))) return false;
  110. $cache_file = './files/config/module_extend.php';
  111. FileHandler::removeFile($cache_file);
  112. $args = new stdClass;
  113. $args->parent_module = $parent_module;
  114. $args->extend_module = $extend_module;
  115. $args->type = $type;
  116. $args->kind = $kind;
  117. $output = executeQuery('module.getModuleExtendCount', $args);
  118. if($output->data->count>0) return false;
  119. $output = executeQuery('module.insertModuleExtend', $args);
  120. return $output;
  121. }
  122. /**
  123. * @brief Delete module extend
  124. *
  125. */
  126. function deleteModuleExtend($parent_module, $extend_module, $type, $kind='')
  127. {
  128. $cache_file = './files/config/module_extend.php';
  129. FileHandler::removeFile($cache_file);
  130. $args = new stdClass;
  131. $args->parent_module = $parent_module;
  132. $args->extend_module = $extend_module;
  133. $args->type = $type;
  134. $args->kind = $kind;
  135. $output = executeQuery('module.deleteModuleExtend', $args);
  136. return $output;
  137. }
  138. function updateModuleConfig($module, $config, $site_srl = 0)
  139. {
  140. $args = new stdClass();
  141. $args->module = $module;
  142. $args->site_srl = $site_srl;
  143. $oModuleModel = getModel('module');
  144. $origin_config = $oModuleModel->getModuleConfig($module, $site_srl);
  145. if(!$origin_config) $origin_config = new stdClass;
  146. foreach($config as $key => $val)
  147. {
  148. $origin_config->{$key} = $val;
  149. }
  150. return $this->insertModuleConfig($module, $origin_config, $site_srl);
  151. }
  152. /**
  153. * @brief Enter a specific set of modules
  154. * In order to manage global configurations of modules such as board, member and so on
  155. */
  156. function insertModuleConfig($module, $config, $site_srl = 0)
  157. {
  158. $args =new stdClass();
  159. $args->module = $module;
  160. $args->config = serialize($config);
  161. $args->site_srl = $site_srl;
  162. $output = executeQuery('module.deleteModuleConfig', $args);
  163. if(!$output->toBool()) return $output;
  164. $output = executeQuery('module.insertModuleConfig', $args);
  165. //remove from cache
  166. $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE);
  167. if($oCacheHandler->isSupport())
  168. {
  169. $oCacheHandler->invalidateGroupKey('site_and_module');
  170. }
  171. return $output;
  172. }
  173. /**
  174. * @brief Save module configurations of the mid
  175. * Manage mid configurations depending on module
  176. */
  177. function insertModulePartConfig($module, $module_srl, $config)
  178. {
  179. $args = new stdClass();
  180. $args->module = $module;
  181. $args->module_srl = $module_srl;
  182. $args->config = serialize($config);
  183. $output = executeQuery('module.deleteModulePartConfig', $args);
  184. if(!$output->toBool()) return $output;
  185. $output = executeQuery('module.insertModulePartConfig', $args);
  186. //remove from cache
  187. $oCacheHandler = CacheHandler::getInstance('object', NULL, TRUE);
  188. if($oCacheHandler->isSupport())
  189. {
  190. $oCacheHandler->invalidateGroupKey('site_and_module');
  191. }
  192. return $output;
  193. }
  194. /**
  195. * @brief create virtual site
  196. */
  197. function insertSite($domain, $index_module_srl)
  198. {
  199. if(isSiteID($domain))
  200. {
  201. $oModuleModel = getModel('module');
  202. if($oModuleModel->isIDExists($domain, 0)) return new Object(-1,'msg_already_registed_vid');
  203. }
  204. else
  205. {
  206. $domain = strtolower($domain);
  207. }
  208. $args = new stdClass;
  209. $args->site_srl = getNextSequence();
  210. $args->domain = (substr_compare($domain, '/', -1) === 0) ? substr($domain, 0, -1) : $domain;
  211. $args->index_module_srl = $index_module_srl;
  212. $args->default_language = Context::getLangType();
  213. $columnList = array('modules.site_srl');
  214. $oModuleModel = getModel('module');
  215. $output = $oModuleModel->getSiteInfoByDomain($args->domain, $columnList);
  216. if($output) return new Object(-1,'msg_already_registed_vid');
  217. $output = executeQuery('module.insertSite', $args);
  218. if(!$output->toBool()) return $output;
  219. $output->add('site_srl', $args->site_srl);
  220. return $output;
  221. }
  222. /**
  223. * @brief modify virtual site
  224. */
  225. function updateSite($args)
  226. {
  227. $oModuleModel = getModel('module');
  228. $columnList = array('sites.site_srl', 'sites.domain');
  229. $site_info = $oModuleModel->getSiteInfo($args->site_srl, $columnList);
  230. if(!$args->domain && $site_info->site_srl == $args->site_srl)
  231. {
  232. $args->domain = $site_info->domain;
  233. }
  234. if($site_info->domain != $args->domain)
  235. {
  236. $info = $oModuleModel->getSiteInfoByDomain($args->domain, $columnList);
  237. if($info->site_srl && $info->site_srl != $args->site_srl) return new Object(-1,'msg_already_registed_domain');
  238. if(isSiteID($args->domain) && $oModuleModel->isIDExists($args->domain)) return new Object(-1,'msg_already_registed_vid');
  239. if($args->domain && !isSiteID($args->domain))
  240. {
  241. $args->domain = (strlen($args->domain) >= 1 && substr_compare($args->domain, '/', -1) === 0) ? substr($args->domain, 0, -1) : $args->domain;
  242. }
  243. }
  244. $output = executeQuery('module.updateSite', $args);
  245. //clear cache for default mid
  246. if($args->site_srl == 0) $vid='';
  247. else $vid=$args->domain;
  248. $module_info = $oModuleModel->getModuleInfoByModuleSrl($args->index_module_srl);
  249. $mid = $module_info->mid;
  250. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  251. if($oCacheHandler->isSupport())
  252. {
  253. $oCacheHandler->invalidateGroupKey('site_and_module');
  254. }
  255. return $output;
  256. }
  257. /**
  258. * @brief Arrange module information
  259. */
  260. function arrangeModuleInfo(&$args, &$extra_vars)
  261. {
  262. // Remove unnecessary information
  263. unset($args->body);
  264. unset($args->act);
  265. unset($args->page);
  266. // Test mid value
  267. if(!preg_match("/^[a-z][a-z0-9_]+$/i", $args->mid)) return new Object(-1, 'msg_limit_mid');
  268. // Test variables (separate basic vars and other vars in modules)
  269. $extra_vars = clone($args);
  270. unset($extra_vars->module_srl);
  271. unset($extra_vars->module);
  272. unset($extra_vars->module_category_srl);
  273. unset($extra_vars->layout_srl);
  274. unset($extra_vars->mlayout_srl);
  275. unset($extra_vars->use_mobile);
  276. unset($extra_vars->menu_srl);
  277. unset($extra_vars->site_srl);
  278. unset($extra_vars->mid);
  279. unset($extra_vars->is_skin_fix);
  280. unset($extra_vars->skin);
  281. unset($extra_vars->is_mskin_fix);
  282. unset($extra_vars->mskin);
  283. unset($extra_vars->browser_title);
  284. unset($extra_vars->description);
  285. unset($extra_vars->is_default);
  286. unset($extra_vars->content);
  287. unset($extra_vars->mcontent);
  288. unset($extra_vars->open_rss);
  289. unset($extra_vars->header_text);
  290. unset($extra_vars->footer_text);
  291. $args = delObjectVars($args, $extra_vars);
  292. return new Object();
  293. }
  294. /**
  295. * @brief Insert module
  296. */
  297. function insertModule($args)
  298. {
  299. if(isset($args->isMenuCreate))
  300. {
  301. $isMenuCreate = $args->isMenuCreate;
  302. }
  303. else
  304. {
  305. $isMenuCreate = TRUE;
  306. }
  307. $output = $this->arrangeModuleInfo($args, $extra_vars);
  308. if(!$output->toBool()) return $output;
  309. // Check whether the module name already exists
  310. if(!$args->site_srl) $args->site_srl = 0;
  311. $oModuleModel = getModel('module');
  312. if($oModuleModel->isIDExists($args->mid, $args->site_srl)) return new Object(-1, 'msg_module_name_exists');
  313. // begin transaction
  314. $oDB = &DB::getInstance();
  315. $oDB->begin();
  316. // Get colorset from the skin information
  317. $module_path = ModuleHandler::getModulePath($args->module);
  318. $skin_info = $oModuleModel->loadSkinInfo($module_path, $args->skin);
  319. $skin_vars = new stdClass();
  320. $skin_vars->colorset = $skin_info->colorset[0]->name;
  321. // Arrange variables and then execute a query
  322. if(!$args->module_srl) $args->module_srl = getNextSequence();
  323. // default value
  324. if($args->skin == '/USE_DEFAULT/')
  325. {
  326. $args->is_skin_fix = 'N';
  327. }
  328. else
  329. {
  330. if(isset($args->is_skin_fix))
  331. {
  332. $args->is_skin_fix = ($args->is_skin_fix != 'Y') ? 'N' : 'Y';
  333. }
  334. else
  335. {
  336. $args->is_skin_fix = 'Y';
  337. }
  338. }
  339. if($args->mskin == '/USE_DEFAULT/')
  340. {
  341. $args->is_mskin_fix = 'N';
  342. }
  343. else
  344. {
  345. if(isset($args->is_mskin_fix))
  346. {
  347. $args->is_mskin_fix = ($args->is_mskin_fix != 'Y') ? 'N' : 'Y';
  348. }
  349. else
  350. {
  351. $args->is_mskin_fix = 'Y';
  352. }
  353. }
  354. unset($output);
  355. if($isMenuCreate == TRUE)
  356. {
  357. $menuArgs = new stdClass;
  358. $menuArgs->menu_srl = $args->menu_srl;
  359. $menuOutput = executeQuery('menu.getMenu', $menuArgs);
  360. // if menu is not created, create menu also. and does not supported that in virtual site.
  361. if(!$menuOutput->data && !$args->site_srl)
  362. {
  363. $oMenuAdminModel = getAdminModel('menu');
  364. $oMenuAdminController = getAdminController('menu');
  365. $menuSrl = $oMenuAdminController->getUnlinkedMenu();
  366. $menuArgs->menu_srl = $menuSrl;
  367. $menuArgs->menu_item_srl = getNextSequence();
  368. $menuArgs->parent_srl = 0;
  369. $menuArgs->open_window = 'N';
  370. $menuArgs->url = $args->mid;
  371. $menuArgs->expand = 'N';
  372. $menuArgs->is_shortcut = 'N';
  373. $menuArgs->name = $args->browser_title;
  374. $menuArgs->listorder = $args->menu_item_srl * -1;
  375. $menuItemOutput = executeQuery('menu.insertMenuItem', $menuArgs);
  376. if(!$menuItemOutput->toBool())
  377. {
  378. $oDB->rollback();
  379. return $menuItemOutput;
  380. }
  381. $oMenuAdminController->makeXmlFile($menuSrl);
  382. }
  383. }
  384. $args->menu_srl = $menuArgs->menu_srl;
  385. // Insert a module
  386. $output = executeQuery('module.insertModule', $args);
  387. if(!$output->toBool())
  388. {
  389. $oDB->rollback();
  390. return $output;
  391. }
  392. // Insert module extra vars
  393. $this->insertModuleExtraVars($args->module_srl, $extra_vars);
  394. // commit
  395. $oDB->commit();
  396. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  397. if($oCacheHandler->isSupport())
  398. {
  399. $oCacheHandler->invalidateGroupKey('site_and_module');
  400. }
  401. $output->add('module_srl',$args->module_srl);
  402. return $output;
  403. }
  404. /**
  405. * @brief Modify module information
  406. */
  407. function updateModule($args)
  408. {
  409. $output = $this->arrangeModuleInfo($args, $extra_vars);
  410. if(!$output->toBool()) return $output;
  411. // begin transaction
  412. $oDB = &DB::getInstance();
  413. $oDB->begin();
  414. $oModuleModel = getModel('module');
  415. $columnList = array('module_srl', 'site_srl', 'browser_title', 'mid');
  416. $module_info = $oModuleModel->getModuleInfoByModuleSrl($args->module_srl);
  417. if(!$args->site_srl || !$args->browser_title)
  418. {
  419. if(!$args->site_srl) $args->site_srl = (int)$module_info->site_srl;
  420. if(!$args->browser_title) $args->browser_title = $module_info->browser_title;
  421. }
  422. $output = executeQuery('module.isExistsModuleName', $args);
  423. if(!$output->toBool() || $output->data->count)
  424. {
  425. $oDB->rollback();
  426. return new Object(-1, 'msg_module_name_exists');
  427. }
  428. // default value
  429. if($args->skin == '/USE_DEFAULT/')
  430. {
  431. $args->is_skin_fix = 'N';
  432. }
  433. else
  434. {
  435. if(isset($args->is_skin_fix))
  436. {
  437. $args->is_skin_fix = ($args->is_skin_fix != 'Y') ? 'N' : 'Y';
  438. }
  439. else
  440. {
  441. $args->is_skin_fix = 'Y';
  442. }
  443. }
  444. if($args->mskin == '/USE_DEFAULT/')
  445. {
  446. $args->is_mskin_fix = 'N';
  447. }
  448. else
  449. {
  450. if(isset($args->is_mskin_fix))
  451. {
  452. $args->is_mskin_fix = ($args->is_mskin_fix != 'Y') ? 'N' : 'Y';
  453. }
  454. else
  455. {
  456. $args->is_mskin_fix = 'Y';
  457. }
  458. }
  459. $output = executeQuery('module.updateModule', $args);
  460. if(!$output->toBool())
  461. {
  462. $oDB->rollback();
  463. return $output;
  464. }
  465. $menuArgs = new stdClass;
  466. $menuArgs->url = $module_info->mid;
  467. $menuArgs->site_srl = $module_info->site_srl;
  468. $menuOutput = executeQueryArray('menu.getMenuItemByUrl', $menuArgs);
  469. if($menuOutput->data && count($menuOutput->data))
  470. {
  471. $oMenuAdminController = getAdminController('menu');
  472. foreach($menuOutput->data as $itemInfo)
  473. {
  474. $itemInfo->url = $args->mid;
  475. $updateMenuItemOutput = $oMenuAdminController->updateMenuItem($itemInfo);
  476. if(!$updateMenuItemOutput->toBool())
  477. {
  478. $oDB->rollback();
  479. return $updateMenuItemOutput;
  480. }
  481. }
  482. }
  483. // if mid changed, change mid of success_return_url to new mid
  484. if($module_info->mid != $args->mid && Context::get('success_return_url'))
  485. {
  486. changeValueInUrl('mid', $args->mid, $module_info->mid);
  487. }
  488. // Insert module extra vars
  489. $this->insertModuleExtraVars($args->module_srl, $extra_vars);
  490. $oDB->commit();
  491. $output->add('module_srl',$args->module_srl);
  492. //remove from cache
  493. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  494. if($oCacheHandler->isSupport())
  495. {
  496. $oCacheHandler->invalidateGroupKey('site_and_module');
  497. }
  498. return $output;
  499. }
  500. /**
  501. * @brief Change the module's virtual site
  502. */
  503. function updateModuleSite($module_srl, $site_srl, $layout_srl = 0)
  504. {
  505. $args = new stdClass;
  506. $args->module_srl = $module_srl;
  507. $args->site_srl = $site_srl;
  508. $args->layout_srl = $layout_srl;
  509. $output = executeQuery('module.updateModuleSite', $args);
  510. if(!$output->toBool()) return $output;
  511. //remove from cache
  512. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  513. if($oCacheHandler->isSupport())
  514. {
  515. $oCacheHandler->invalidateGroupKey('site_and_module');
  516. }
  517. return $output;
  518. }
  519. /**
  520. * Delete module
  521. * Attempt to delete all related information when deleting a module.
  522. * Origin method is changed. because menu validation check is needed
  523. */
  524. function deleteModule($module_srl, $site_srl = 0)
  525. {
  526. if(!$module_srl) return new Object(-1,'msg_invalid_request');
  527. $site_module_info = Context::get('site_module_info');
  528. $oModuleModel = getModel('module');
  529. $output = $oModuleModel->getModuleInfoByModuleSrl($module_srl);
  530. $args = new stdClass();
  531. $args->url = $output->mid;
  532. $args->is_shortcut = 'N';
  533. if(!$site_srl) $args->site_srl = $site_module_info->site_srl;
  534. else $args->site_srl = $site_srl;
  535. unset($output);
  536. $oMenuAdminModel = getAdminModel('menu');
  537. $menuOutput = $oMenuAdminModel->getMenuList($args);
  538. // get menu_srl by site_srl
  539. if(is_array($menuOutput->data))
  540. {
  541. foreach($menuOutput->data AS $key=>$value)
  542. {
  543. $args->menu_srl = $value->menu_srl;
  544. break;
  545. }
  546. }
  547. $output = executeQuery('menu.getMenuItemByUrl', $args);
  548. // menu delete
  549. if($output->data)
  550. {
  551. unset($args);
  552. $args = new stdClass;
  553. $args->menu_srl = $output->data->menu_srl;
  554. $args->menu_item_srl = $output->data->menu_item_srl;
  555. $args->is_force = 'N';
  556. $oMenuAdminController = getAdminController('menu');
  557. $output = $oMenuAdminController->deleteItem($args);
  558. if($output->isSuccess)
  559. {
  560. return new Object(0, 'success_deleted');
  561. }
  562. else
  563. {
  564. return new Object($output->error, $output->message);
  565. }
  566. }
  567. // only delete module
  568. else
  569. {
  570. return $this->onlyDeleteModule($module_srl);
  571. }
  572. }
  573. /**
  574. * Delete module
  575. * Attempt to delete all related information when deleting a module.
  576. */
  577. public function onlyDeleteModule($module_srl)
  578. {
  579. if(!$module_srl) return new Object(-1,'msg_invalid_request');
  580. // check start module
  581. $oModuleModel = getModel('module');
  582. $columnList = array('sites.index_module_srl');
  583. $start_module = $oModuleModel->getSiteInfo(0, $columnList);
  584. if($module_srl == $start_module->index_module_srl) return new Object(-1, 'msg_cannot_delete_startmodule');
  585. // Call a trigger (before)
  586. $trigger_obj = new stdClass();
  587. $trigger_obj->module_srl = $module_srl;
  588. $output = ModuleHandler::triggerCall('module.deleteModule', 'before', $trigger_obj);
  589. if(!$output->toBool()) return $output;
  590. // begin transaction
  591. $oDB = &DB::getInstance();
  592. $oDB->begin();
  593. $args = new stdClass();
  594. $args->module_srl = $module_srl;
  595. // Delete module information from the DB
  596. $output = executeQuery('module.deleteModule', $args);
  597. if(!$output->toBool())
  598. {
  599. $oDB->rollback();
  600. return $output;
  601. }
  602. // Delete permission information
  603. $this->deleteModuleGrants($module_srl);
  604. // Remove skin information
  605. $this->deleteModuleSkinVars($module_srl);
  606. // Delete module extra vars
  607. $this->deleteModuleExtraVars($module_srl);
  608. // Remove the module manager
  609. $this->deleteAdminId($module_srl);
  610. // Call a trigger (after)
  611. if($output->toBool())
  612. {
  613. $trigger_output = ModuleHandler::triggerCall('module.deleteModule', 'after', $trigger_obj);
  614. if(!$trigger_output->toBool())
  615. {
  616. $oDB->rollback();
  617. return $trigger_output;
  618. }
  619. }
  620. // commit
  621. $oDB->commit();
  622. //remove from cache
  623. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  624. if($oCacheHandler->isSupport())
  625. {
  626. $oCacheHandler->invalidateGroupKey('site_and_module');
  627. }
  628. return $output;
  629. }
  630. /**
  631. * @brief Change other information of the module
  632. * @deprecated
  633. */
  634. function updateModuleSkinVars($module_srl, $skin_vars)
  635. {
  636. return new Object();
  637. }
  638. /**
  639. * @brief Set is_default as N in all modules(the default module is disabled)
  640. */
  641. function clearDefaultModule()
  642. {
  643. $output = executeQuery('module.clearDefaultModule');
  644. if(!$output->toBool()) return $output;
  645. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  646. if($oCacheHandler->isSupport())
  647. {
  648. $oCacheHandler->invalidateGroupKey('site_and_module');
  649. }
  650. return $output;
  651. }
  652. /**
  653. * @brief Update menu_srl of mid which belongs to menu_srl
  654. */
  655. function updateModuleMenu($args)
  656. {
  657. $output = executeQuery('module.updateModuleMenu', $args);
  658. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  659. if($oCacheHandler->isSupport())
  660. {
  661. $oCacheHandler->invalidateGroupKey('site_and_module');
  662. }
  663. return $output;
  664. }
  665. /**
  666. * @brief Update layout_srl of mid which belongs to menu_srl
  667. */
  668. function updateModuleLayout($layout_srl, $menu_srl_list)
  669. {
  670. if(!count($menu_srl_list)) return;
  671. $args = new stdClass;
  672. $args->layout_srl = $layout_srl;
  673. $args->menu_srls = implode(',',$menu_srl_list);
  674. $output = executeQuery('module.updateModuleLayout', $args);
  675. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  676. if($oCacheHandler->isSupport())
  677. {
  678. $oCacheHandler->invalidateGroupKey('site_and_module');
  679. }
  680. return $output;
  681. }
  682. /**
  683. * @brief Change the site administrator
  684. */
  685. function insertSiteAdmin($site_srl, $arr_admins)
  686. {
  687. // Remove the site administrator
  688. $args = new stdClass;
  689. $args->site_srl = $site_srl;
  690. $output = executeQuery('module.deleteSiteAdmin', $args);
  691. if(!$output->toBool()) return $output;
  692. // Get user id of an administrator
  693. if(!is_array($arr_admins) || !count($arr_admins)) return new Object();
  694. foreach($arr_admins as $key => $user_id)
  695. {
  696. if(!trim($user_id)) continue;
  697. $admins[] = trim($user_id);
  698. }
  699. if(!count($admins)) return new Object();
  700. $oMemberModel = getModel('member');
  701. $member_config = $oMemberModel->getMemberConfig();
  702. if($member_config->identifier == 'email_address')
  703. {
  704. $args->email_address = '\''.implode('\',\'',$admins).'\'';
  705. }
  706. else
  707. {
  708. $args->user_ids = '\''.implode('\',\'',$admins).'\'';
  709. }
  710. $output = executeQueryArray('module.getAdminSrls', $args);
  711. if(!$output->toBool()||!$output->data) return $output;
  712. foreach($output->data as $key => $val)
  713. {
  714. unset($args);
  715. $args = new stdClass;
  716. $args->site_srl = $site_srl;
  717. $args->member_srl = $val->member_srl;
  718. $output = executeQueryArray('module.insertSiteAdmin', $args);
  719. if(!$output->toBool()) return $output;
  720. }
  721. return new Object();
  722. }
  723. /**
  724. * @brief Specify the admin ID to a module
  725. */
  726. function insertAdminId($module_srl, $admin_id)
  727. {
  728. $oMemberModel = getModel('member');
  729. $member_config = $oMemberModel->getMemberConfig();
  730. if($member_config->identifier == 'email_address')
  731. $member_info = $oMemberModel->getMemberInfoByEmailAddress($admin_id);
  732. else
  733. $member_info = $oMemberModel->getMemberInfoByUserID($admin_id);
  734. if(!$member_info->member_srl) return;
  735. $args = new stdClass();
  736. $args->module_srl = $module_srl;
  737. $args->member_srl = $member_info->member_srl;
  738. return executeQuery('module.insertAdminId', $args);
  739. }
  740. /**
  741. * @brief Remove the admin ID from a module
  742. */
  743. function deleteAdminId($module_srl, $admin_id = '')
  744. {
  745. $args = new stdClass();
  746. $args->module_srl = $module_srl;
  747. if($admin_id)
  748. {
  749. $oMemberModel = getModel('member');
  750. $member_info = $oMemberModel->getMemberInfoByUserID($admin_id);
  751. if($member_info->member_srl) $args->member_srl = $member_info->member_srl;
  752. }
  753. return executeQuery('module.deleteAdminId', $args);
  754. }
  755. /**
  756. * Insert skin vars to a module
  757. * @param $module_srl Sequence of module
  758. * @param $obj Skin variables
  759. */
  760. function insertModuleSkinVars($module_srl, $obj)
  761. {
  762. return $this->_insertModuleSkinVars($module_srl, $obj, 'P');
  763. }
  764. /**
  765. * Insert mobile skin vars to a module
  766. * @param $module_srl Sequence of module
  767. * @param $obj Skin variables
  768. */
  769. function insertModuleMobileSkinVars($module_srl, $obj)
  770. {
  771. return $this->_insertModuleSkinVars($module_srl, $obj, 'M');
  772. }
  773. /**
  774. * @brief Insert skin vars to a module
  775. */
  776. function _insertModuleSkinVars($module_srl, $obj, $mode)
  777. {
  778. $mode = $mode === 'P' ? 'P' : 'M';
  779. $oDB = DB::getInstance();
  780. $oDB->begin();
  781. $output = $this->_deleteModuleSkinVars($module_srl, $mode);
  782. if(!$output->toBool())
  783. {
  784. $oDB->rollback();
  785. return $output;
  786. }
  787. getDestroyXeVars($obj);
  788. if(!$obj || !count($obj)) return new Object();
  789. $args = new stdClass;
  790. $args->module_srl = $module_srl;
  791. foreach($obj as $key => $val)
  792. {
  793. // #17927989 For an old board which used the old blog module
  794. // it often saved menu item(stdClass) on the skin info column
  795. // When updating the module on XE core 1.2.0 later versions, it occurs an error
  796. // fixed the error
  797. if (is_object($val)) continue;
  798. if (is_array($val)) $val = serialize($val);
  799. $args->name = trim($key);
  800. $args->value = trim($val);
  801. if(!$args->name || !$args->value) continue;
  802. if($mode === 'P')
  803. {
  804. $output = executeQuery('module.insertModuleSkinVars', $args);
  805. }
  806. else
  807. {
  808. $output = executeQuery('module.insertModuleMobileSkinVars', $args);
  809. }
  810. if(!$output->toBool())
  811. {
  812. return $output;
  813. $oDB->rollback();
  814. }
  815. }
  816. $oDB->commit;
  817. return new Object();
  818. }
  819. /**
  820. * Remove skin vars ofa module
  821. * @param $module_srl seqence of module
  822. */
  823. function deleteModuleSkinVars($module_srl)
  824. {
  825. return $this->_deleteModuleSkinVars($module_srl, 'P');
  826. }
  827. /**
  828. * Remove mobile skin vars ofa module
  829. * @param $module_srl seqence of module
  830. */
  831. function deleteModuleMobileSkinVars($module_srl)
  832. {
  833. return $this->_deleteModuleSkinVars($module_srl, 'M');
  834. }
  835. /**
  836. * @brief Remove skin vars of a module
  837. */
  838. function _deleteModuleSkinVars($module_srl, $mode)
  839. {
  840. $args = new stdClass();
  841. $args->module_srl = $module_srl;
  842. $mode = $mode === 'P' ? 'P' : 'M';
  843. if($mode === 'P')
  844. {
  845. $object_key = 'module_skin_vars:'.$module_srl;
  846. $query = 'module.deleteModuleSkinVars';
  847. }
  848. else
  849. {
  850. $object_key = 'module_mobile_skin_vars:'.$module_srl;
  851. $query = 'module.deleteModuleMobileSkinVars';
  852. }
  853. //remove from cache
  854. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  855. $cache_key = $oCacheHandler->getGroupKey('site_and_module', $object_key);
  856. if($oCacheHandler->isSupport())
  857. {
  858. $oCacheHandler->delete($cache_key);
  859. }
  860. return executeQuery($query, $args);
  861. }
  862. /**
  863. * @brief Register extra vars to the module
  864. */
  865. function insertModuleExtraVars($module_srl, $obj)
  866. {
  867. $this->deleteModuleExtraVars($module_srl);
  868. getDestroyXeVars($obj);
  869. if(!$obj || !count($obj)) return;
  870. foreach($obj as $key => $val)
  871. {
  872. $args = new stdClass();
  873. $args->module_srl = $module_srl;
  874. $args->name = trim($key);
  875. $args->value = trim($val);
  876. if(!$args->name || !$args->value) continue;
  877. $output = executeQuery('module.insertModuleExtraVars', $args);
  878. }
  879. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  880. if($oCacheHandler->isSupport())
  881. {
  882. $object_key = 'module_extra_vars:'.$module_srl;
  883. $cache_key = $oCacheHandler->getGroupKey('site_and_module', $object_key);
  884. $oCacheHandler->delete($cache_key);
  885. }
  886. }
  887. /**
  888. * @brief Remove extra vars from the module
  889. */
  890. function deleteModuleExtraVars($module_srl)
  891. {
  892. $args = new stdClass();
  893. $args->module_srl = $module_srl;
  894. $output = executeQuery('module.deleteModuleExtraVars', $args);
  895. //remove from cache
  896. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  897. if($oCacheHandler->isSupport())
  898. {
  899. $object_key = 'module_extra_vars:'.$module_srl;
  900. $cache_key = $oCacheHandler->getGroupKey('site_and_module', $object_key);
  901. $oCacheHandler->delete($cache_key);
  902. }
  903. return $output;
  904. }
  905. /**
  906. * @brief Grant permission to the module
  907. */
  908. function insertModuleGrants($module_srl, $obj)
  909. {
  910. $this->deleteModuleGrants($module_srl);
  911. if(!$obj || !count($obj)) return;
  912. foreach($obj as $name => $val)
  913. {
  914. if(!$val || !count($val)) continue;
  915. foreach($val as $group_srl)
  916. {
  917. $args = new stdClass();
  918. $args->module_srl = $module_srl;
  919. $args->name = $name;
  920. $args->group_srl = trim($group_srl);
  921. if(!$args->name || !$args->group_srl) continue;
  922. executeQuery('module.insertModuleGrant', $args);
  923. }
  924. }
  925. }
  926. /**
  927. * @brief Remove permission from the module
  928. */
  929. function deleteModuleGrants($module_srl)
  930. {
  931. $args = new stdClass();
  932. $args->module_srl = $module_srl;
  933. return executeQuery('module.deleteModuleGrants', $args);
  934. }
  935. /**
  936. * @brief Change user-defined language
  937. */
  938. function replaceDefinedLangCode(&$output, $isReplaceLangCode = true)
  939. {
  940. if($isReplaceLangCode)
  941. {
  942. $output = preg_replace_callback('!\$user_lang->([a-z0-9\_]+)!is', array($this,'_replaceLangCode'), $output);
  943. }
  944. }
  945. function _replaceLangCode($matches)
  946. {
  947. static $lang = null;
  948. if(is_null($lang))
  949. {
  950. $site_module_info = Context::get('site_module_info');
  951. if(!$site_module_info)
  952. {
  953. $oModuleModel = getModel('module');
  954. $site_module_info = $oModuleModel->getDefaultMid();
  955. Context::set('site_module_info', $site_module_info);
  956. }
  957. $cache_file = sprintf('%sfiles/cache/lang_defined/%d.%s.php', _XE_PATH_, $site_module_info->site_srl, Context::getLangType());
  958. if(!file_exists($cache_file))
  959. {
  960. $oModuleAdminController = getAdminController('module');
  961. $oModuleAdminController->makeCacheDefinedLangCode($site_module_info->site_srl);
  962. }
  963. if(file_exists($cache_file))
  964. {
  965. $moduleAdminControllerMtime = filemtime(_XE_PATH_ . 'modules/module/module.admin.controller.php');
  966. $cacheFileMtime = filemtime($cache_file);
  967. if($cacheFileMtime < $moduleAdminControllerMtime)
  968. {
  969. $oModuleAdminController = getAdminController('module');
  970. $oModuleAdminController->makeCacheDefinedLangCode($site_module_info->site_srl);
  971. }
  972. require_once($cache_file);
  973. }
  974. }
  975. if(!Context::get($matches[1]) && $lang[$matches[1]]) return $lang[$matches[1]];
  976. return str_replace('$user_lang->','',$matches[0]);
  977. }
  978. /**
  979. * @brief Add and update a file into the file box
  980. */
  981. function procModuleFileBoxAdd()
  982. {
  983. $ajax = Context::get('ajax');
  984. if ($ajax) Context::setRequestMethod('JSON');
  985. $logged_info = Context::get('logged_info');
  986. if($logged_info->is_admin !='Y' && !$logged_info->is_site_admin) return new Object(-1, 'msg_not_permitted');
  987. $vars = Context::gets('addfile','filter');
  988. $attributeNames = Context::get('attribute_name');
  989. $attributeValues = Context::get('attribute_value');
  990. if(is_array($attributeNames) && is_array($attributeValues) && count($attributeNames) == count($attributeValues))
  991. {
  992. $attributes = array();
  993. foreach($attributeNames as $no => $name)
  994. {
  995. if(empty($name))
  996. {
  997. continue;
  998. }
  999. $attributes[] = sprintf('%s:%s', $name, $attributeValues[$no]);
  1000. }
  1001. $attributes = implode(';', $attributes);
  1002. }
  1003. $vars->comment = $attributes;
  1004. $module_filebox_srl = Context::get('module_filebox_srl');
  1005. $ext = strtolower(substr(strrchr($vars->addfile['name'],'.'),1));
  1006. $vars->ext = $ext;
  1007. if($vars->filter) $filter = explode(',',$vars->filter);
  1008. else $filter = array('jpg','jpeg','gif','png');
  1009. if(!in_array($ext,$filter)) return new Object(-1, 'msg_error_occured');
  1010. $vars->member_srl = $logged_info->member_srl;
  1011. // update
  1012. if($module_filebox_srl > 0)
  1013. {
  1014. $vars->module_filebox_srl = $module_filebox_srl;
  1015. $output = $this->updateModuleFileBox($vars);
  1016. }
  1017. // insert
  1018. else
  1019. {
  1020. if(!Context::isUploaded()) return new Object(-1, 'msg_error_occured');
  1021. $addfile = Context::get('addfile');
  1022. if(!is_uploaded_file($addfile['tmp_name'])) return new Object(-1, 'msg_error_occured');
  1023. if($vars->addfile['error'] != 0) return new Object(-1, 'msg_error_occured');
  1024. $output = $this->insertModuleFileBox($vars);
  1025. }
  1026. $this->setTemplatePath($this->module_path.'tpl');
  1027. if (!$ajax)
  1028. {
  1029. $returnUrl = Context::get('success_return_url') ? Context::get('success_return_url') : getNotEncodedUrl('', 'module', 'admin', 'act', 'dispModuleAdminFileBox');
  1030. $this->setRedirectUrl($returnUrl);
  1031. return;
  1032. }
  1033. else
  1034. {
  1035. if($output) $this->add('save_filename', $output->get('save_filename'));
  1036. else $this->add('save_filename', '');
  1037. }
  1038. }
  1039. /**
  1040. * @brief Update a file into the file box
  1041. */
  1042. function updateModuleFileBox($vars)
  1043. {
  1044. $args = new stdClass;
  1045. // have file
  1046. if($vars->addfile['tmp_name'] && is_uploaded_file($vars->addfile['tmp_name']))
  1047. {
  1048. $oModuleModel = getModel('module');
  1049. $output = $oModuleModel->getModuleFileBox($vars->module_filebox_srl);
  1050. FileHandler::removeFile($output->data->filename);
  1051. $path = $oModuleModel->getModuleFileBoxPath($vars->module_filebox_srl);
  1052. FileHandler::makeDir($path);
  1053. $save_filename = sprintf('%s%s.%s',$path, $vars->module_filebox_srl, $ext);
  1054. $tmp = $vars->addfile['tmp_name'];
  1055. // Check uploaded file
  1056. if(!checkUploadedFile($tmp)) return false;
  1057. if(!@move_uploaded_file($tmp, $save_filename))
  1058. {
  1059. return false;
  1060. }
  1061. $args->fileextension = strtolower(substr(strrchr($vars->addfile['name'],'.'),1));
  1062. $args->filename = $save_filename;
  1063. $args->filesize = $vars->addfile['size'];
  1064. }
  1065. $args->module_filebox_srl = $vars->module_filebox_srl;
  1066. $args->comment = $vars->comment;
  1067. // FIXME $args ??
  1068. return executeQuery('module.updateModuleFileBox', $vars);
  1069. }
  1070. /**
  1071. * @brief Add a file into the file box
  1072. */
  1073. function insertModuleFileBox($vars)
  1074. {
  1075. // set module_filebox_srl
  1076. $vars->module_filebox_srl = getNextSequence();
  1077. // get file path
  1078. $oModuleModel = getModel('module');
  1079. $path = $oModuleModel->getModuleFileBoxPath($vars->module_filebox_srl);
  1080. FileHandler::makeDir($path);
  1081. $save_filename = sprintf('%s%s.%s',$path, $vars->module_filebox_srl, $vars->ext);
  1082. $tmp = $vars->addfile['tmp_name'];
  1083. // Check uploaded file
  1084. if(!checkUploadedFile($tmp)) return false;
  1085. // upload
  1086. if(!@move_uploaded_file($tmp, $save_filename))
  1087. {
  1088. return false;
  1089. }
  1090. // insert
  1091. $args = new stdClass;
  1092. $args->module_filebox_srl = $vars->module_filebox_srl;
  1093. $args->member_srl = $vars->member_srl;
  1094. $args->comment = $vars->comment;
  1095. $args->filename = $save_filename;
  1096. $args->fileextension = strtolower(substr(strrchr($vars->addfile['name'],'.'),1));
  1097. $args->filesize = $vars->addfile['size'];
  1098. $output = executeQuery('module.insertModuleFileBox', $args);
  1099. $output->add('save_filename', $save_filename);
  1100. return $output;
  1101. }
  1102. /**
  1103. * @brief Delete a file from the file box
  1104. */
  1105. function procModuleFileBoxDelete()
  1106. {
  1107. $logged_info = Context::get('logged_info');
  1108. if($logged_info->is_admin !='Y' && !$logged_info->is_site_admin) return new Object(-1, 'msg_not_permitted');
  1109. $module_filebox_srl = Context::get('module_filebox_srl');
  1110. if(!$module_filebox_srl) return new Object(-1, 'msg_invalid_request');
  1111. $vars = new stdClass();
  1112. $vars->module_filebox_srl = $module_filebox_srl;
  1113. $output = $this->deleteModuleFileBox($vars);
  1114. if(!$output->toBool()) return $output;
  1115. }
  1116. function deleteModuleFileBox($vars)
  1117. {
  1118. // delete real file
  1119. $oModuleModel = getModel('module');
  1120. $output = $oModuleModel->getModuleFileBox($vars->module_filebox_srl);
  1121. FileHandler::removeFile($output->data->filename);
  1122. $args = new stdClass();
  1123. $args->module_filebox_srl = $vars->module_filebox_srl;
  1124. return executeQuery('module.deleteModuleFileBox', $args);
  1125. }
  1126. /**
  1127. * @brief function of locking (timeout is in seconds)
  1128. */
  1129. function lock($lock_name, $timeout, $member_srl = null)
  1130. {
  1131. $this->unlockTimeoutPassed();
  1132. $args = new stdClass;
  1133. $args->lock_name = $lock_name;
  1134. if(!$timeout) $timeout = 60;
  1135. $args->deadline = date("YmdHis", $_SERVER['REQUEST_TIME'] + $timeout);
  1136. if($member_srl) $args->member_srl = $member_srl;
  1137. $output = executeQuery('module.insertLock', $args);
  1138. if($output->toBool())
  1139. {
  1140. $output->add('lock_name', $lock_name);
  1141. $output->add('deadline', $args->deadline);
  1142. }
  1143. return $output;
  1144. }
  1145. function unlockTimeoutPassed()
  1146. {
  1147. executeQuery('module.deleteLocksTimeoutPassed');
  1148. }
  1149. function unlock($lock_name, $deadline)
  1150. {
  1151. $args = new stdClass;
  1152. $args->lock_name = $lock_name;
  1153. $args->deadline = $deadline;
  1154. $output = executeQuery('module.deleteLock', $args);
  1155. return $output;
  1156. }
  1157. function updateModuleInSites($site_srls, $args)
  1158. {
  1159. $args = new stdClass;
  1160. $args->site_srls = $site_srls;
  1161. $output = executeQuery('module.updateModuleInSites', $args);
  1162. $oCacheHandler = CacheHandler::getInstance('object', null, true);
  1163. if($oCacheHandler->isSupport())
  1164. {
  1165. $oCacheHandler->invalidateGroupKey('site_and_module');
  1166. }
  1167. return $output;
  1168. }
  1169. }
  1170. /* End of file module.controller.php */
  1171. /* Location: ./modules/module/module.controller.php */