/app/modules/admin/AdminAPIModule.php

https://github.com/hxfnd/Kurogo-Mobile-Web · PHP · 807 lines · 670 code · 135 blank · 2 comment · 154 complexity · 07f3a3e8fbb263e5bb12a6ac2db8981e MD5 · raw file

  1. <?php
  2. class AdminAPIModule extends APIModule
  3. {
  4. protected $id = 'admin';
  5. protected $vmin = 1;
  6. protected $vmax = 1;
  7. private $loadedConfigs = array();
  8. private $changedConfigs = array();
  9. public function availableVersions() {
  10. return array(1);
  11. }
  12. private function getUnconstantedValue($value, &$constant) {
  13. $constCheck = array(
  14. 'FULL_URL_BASE'=>FULL_URL_BASE,
  15. 'LOG_DIR'=>LOG_DIR,
  16. 'LIB_DIR'=>LIB_DIR,
  17. 'CACHE_DIR'=>CACHE_DIR,
  18. 'DATA_DIR'=>DATA_DIR,
  19. 'SITE_DIR'=>SITE_DIR,
  20. 'ROOT_DIR'=>ROOT_DIR
  21. );
  22. $constant = '';
  23. foreach ($constCheck as $const=>$constValue) {
  24. $i = strpos($value, $constValue);
  25. if ($i !== false) {
  26. if ($i==0) {
  27. $value = substr($value, $i+strlen($constValue)+1);
  28. $constant = $const;
  29. }
  30. }
  31. }
  32. return $value;
  33. }
  34. private function getSiteAdminConfig() {
  35. static $configData;
  36. if (!$configData) {
  37. $file = APP_DIR . "/common/config/admin-site.json";
  38. if (!$configData = json_decode(file_get_contents($file), true)) {
  39. throw new KurogoDataException($this->getLocalizedString('ERROR_PARSING_FILE', $file));
  40. }
  41. }
  42. return $configData;
  43. }
  44. private function getTypeStr($type) {
  45. if ($type=='site') {
  46. return 'site';
  47. } elseif ($type instanceOf Module) {
  48. return $type->configModule;
  49. } else {
  50. throw new Exception("Invalid type $type");
  51. }
  52. }
  53. private function getAdminData($type, $section, $subsection=null) {
  54. if ($type=='site') {
  55. $configData = $this->getSiteAdminConfig();
  56. $module = $this;
  57. } elseif ($type instanceOf Module) {
  58. $configData = $type->getModuleAdminConfig();
  59. $module = $type;
  60. } else {
  61. throw new KurogoConfigurationException("Invalid type $type");
  62. }
  63. if (!isset($configData[$section])) {
  64. throw new KurogoConfigurationException("Invalid section $section");
  65. }
  66. $sectionData = $configData[$section];
  67. if ($subsection) {
  68. if (!isset($configData[$section]['sections'][$subsection])) {
  69. throw new KurogoConfigurationException("Invalid subsection $subsection for section $section");
  70. }
  71. $sectionData = $configData[$section]['sections'][$subsection];
  72. }
  73. $sectionData['section'] = $section;
  74. if (isset($sectionData['titleKey'])) {
  75. $sectionData['title'] = $module->getLocalizedString($sectionData['titleKey']);
  76. unset($sectionData['titleKey']);
  77. }
  78. if (isset($sectionData['descriptionKey'])) {
  79. $sectionData['description'] = $module->getLocalizedString($sectionData['descriptionKey']);
  80. unset($sectionData['descriptionKey']);
  81. }
  82. if (isset($sectionData['fieldgroups'])) {
  83. foreach ($sectionData['fieldgroups'] as $fieldgroup=>&$fieldgroupData) {
  84. if (isset($fieldgroupData['labelKey'])) {
  85. $fieldgroupData['label'] = $module->getLocalizedString($fieldgroupData['labelKey']);
  86. unset($fieldgroupData['labelKey']);
  87. }
  88. if (isset($fieldgroupData['descriptionKey'])) {
  89. $fieldgroupData['description'] = $module->getLocalizedString($fieldgroupData['descriptionKey']);
  90. unset($fieldgroupData['descriptionKey']);
  91. }
  92. }
  93. }
  94. switch ($sectionData['sectiontype'])
  95. {
  96. case 'fields':
  97. foreach ($sectionData['fields'] as $key=>&$field) {
  98. if (isset($field['labelKey'])) {
  99. $field['label'] = $module->getLocalizedString($field['labelKey']);
  100. unset($field['labelKey']);
  101. }
  102. if (isset($field['descriptionKey'])) {
  103. $field['description'] = $module->getLocalizedString($field['descriptionKey']);
  104. unset($field['descriptionKey']);
  105. }
  106. if (isset($field['valueMethod'])) {
  107. $field['value'] = call_user_func(array($module, $field['valueMethod']));
  108. unset($field['valueMethod']);
  109. } elseif (isset($field['valueKey'])) {
  110. $field['value'] = $module->getLocalizedString($field['valueKey']);
  111. unset($field['valueKey']);
  112. } elseif ($type=='site') {
  113. if (isset($field['config'])) {
  114. switch ($field['config'])
  115. {
  116. case 'site':
  117. $field['value'] = Kurogo::getOptionalSiteVar($key, '', $field['section']);
  118. break;
  119. case 'strings':
  120. $field['value'] = Kurogo::getOptionalSiteString($key);
  121. break;
  122. default:
  123. throw new KurogoConfigurationException("Unknown config " . $field['config']);
  124. break;
  125. }
  126. }
  127. } elseif (isset($field['config'], $field['section'])) {
  128. $field['value'] = $module->getOptionalModuleVar($key, isset($field['default']) ? $field['default'] : '', $field['section'], $field['config']);
  129. }
  130. switch ($field['type'])
  131. {
  132. case 'paragraph':
  133. if (is_array($field['value'])) {
  134. $field['value'] = implode("\n\n", $field['value']);
  135. }
  136. break;
  137. case 'select':
  138. if (isset($field['optionsMethod'])) {
  139. if (is_array($field['optionsMethod'])) {
  140. $field['options'] = call_user_func($field['optionsMethod']);
  141. } else {
  142. $field['options'] = $module->$field['optionsMethod']();
  143. }
  144. unset($field['optionsMethod']);
  145. }
  146. if (isset($field['optionsFirst'])) {
  147. $field['options'] = array_merge(array(''=>$field['optionsFirst']), $field['options']);
  148. unset($field['optionsFirst']);
  149. }
  150. }
  151. if (isset($field['value'])) {
  152. $value = $this->getUnconstantedValue($field['value'], $constant);
  153. if ($constant) {
  154. $field['value'] = $value;
  155. $field['constant'] = $constant;
  156. }
  157. }
  158. }
  159. break;
  160. case 'section':
  161. if (isset($sectionData['sectionsmethod'])) {
  162. if (is_array($sectionData['sectionsmethod'])) {
  163. $sectionData['sections'] = call_user_func($sectionData['sectionsmethod']);
  164. } else {
  165. $sectionData['sections'] = $module->$sectionData['sectionsmethod']();
  166. }
  167. unset($sectionData['sectionsmethod']);
  168. } elseif ($type=='site') {
  169. throw new KurogoConfigurationException("Getting sections for site is not written yet");
  170. } else {
  171. $configMode = isset($sectionData['configMode']) ? $sectionData['configMode'] : 0;
  172. $sectionData['sections'] = $module->getModuleSections($sectionData['config'], Config::NO_EXPAND_VALUE, $configMode);
  173. }
  174. if (isset($sectionData['sectionsnoneKey'])) {
  175. $sectionData['sectionsnone'] = $module->getLocalizedString($sectionData['sectionsnoneKey']);
  176. unset($sectionData['sectionsnoneKey']);
  177. }
  178. if (isset($sectionData['sectionaddpromptkey'])) {
  179. $sectionData['sectionaddprompt'] = $module->getLocalizedString($sectionData['sectionaddpromptkey']);
  180. unset($sectionData['sectionaddpromptkey']);
  181. }
  182. foreach ($sectionData['fields'] as $key=>&$field) {
  183. if (isset($field['labelKey'])) {
  184. $field['label'] = $module->getLocalizedString($field['labelKey']);
  185. unset($field['labelKey']);
  186. }
  187. if (isset($field['descriptionKey'])) {
  188. $field['description'] = $module->getLocalizedString($field['descriptionKey']);
  189. unset($field['descriptionKey']);
  190. }
  191. if (isset($field['valueKey'])) {
  192. $field['value'] = $module->getLocalizedString($field['valueKey']);
  193. unset($field['valueKey']);
  194. }
  195. switch ($field['type'])
  196. {
  197. case 'select':
  198. if (isset($field['optionsMethod'])) {
  199. if (is_array($field['optionsMethod'])) {
  200. $field['options'] = call_user_func($field['optionsMethod']);
  201. } else {
  202. $field['options'] = $module->$field['optionsMethod']();
  203. }
  204. unset($field['optionsMethod']);
  205. }
  206. if (isset($field['optionsFirst'])) {
  207. $field['options'] = array_merge(array(''=>$field['optionsFirst']), $field['options']);
  208. unset($field['optionsFirst']);
  209. }
  210. }
  211. }
  212. foreach ($sectionData['sections'] as $section=>&$sectionFields) {
  213. foreach($sectionFields as $key=>&$value) {
  214. if (isset($sectionData['fields'][$key]['type']) && $sectionData['fields'][$key]['type']=='paragraph') {
  215. $value = implode("\n\n", $value);
  216. }
  217. $v = $this->getUnconstantedValue($value, $constant);
  218. if ($constant) {
  219. $value = array($constant, $v, $value);
  220. }
  221. }
  222. }
  223. break;
  224. case 'sections':
  225. foreach ($sectionData['sections'] as $subsection=>&$_sectionData) {
  226. $subsectionData = $this->getAdminData($type, $section, $subsection);
  227. if (isset($subsectionData['showIfSiteVar'])) {
  228. if (Kurogo::getOptionalSiteVar($subsectionData['showIfSiteVar'][0], '') != $subsectionData['showIfSiteVar'][1]) {
  229. unset($sectionData['sections'][$subsection]);
  230. continue;
  231. }
  232. }
  233. if (isset($subsectionData['showIfModuleVar'])) {
  234. if ($type->getOptionalModuleVar($subsectionData['showIfModuleVar'][0], '') != $subsectionData['showIfModuleVar'][1]) {
  235. unset($sectionData['sections'][$subsection]);
  236. continue;
  237. }
  238. }
  239. $sectionData['sections'][$subsection] = $subsectionData;
  240. }
  241. break;
  242. default:
  243. throw new KurogoConfigurationException("Section type " . $sectionData['sectiontype'] . " not understood for section $section");
  244. }
  245. return $sectionData;
  246. }
  247. private function getAdminConfig($type, $config, $opts=0) {
  248. $opts = $opts | ConfigFile::OPTION_IGNORE_LOCAL | ConfigFile::OPTION_IGNORE_MODE;
  249. if ($type=='site') {
  250. $configKey = "site-$config";
  251. if (isset($this->loadedConfigs[$configKey])) {
  252. $config = $this->loadedConfigs[$configKey];
  253. } elseif ($config = ConfigFile::factory($config, 'site', $opts)) {
  254. $this->loadedConfigs[$configKey] = $config;
  255. }
  256. } elseif ($type instanceOf Module) {
  257. $configKey = 'module-' . $type->getConfigModule() . '-' . $config;
  258. if (isset($this->loadedConfigs[$configKey])) {
  259. $config = $this->loadedConfigs[$configKey];
  260. } elseif ($config = $type->getConfig($config, $opts)) {
  261. $this->loadedConfigs[$configKey] = $config;
  262. }
  263. } else {
  264. throw new KurogoConfigurationException("Invalid type $type");
  265. }
  266. return $config;
  267. }
  268. private function setSectionOrder($type, $section, $subsection, $order) {
  269. $sectionData = $this->getAdminData($type, $section, $subsection);
  270. if ($sectionData['sectiontype']!='section') {
  271. throw new KurogoConfigurationException("Cannot set the order of $section $subsection");
  272. }
  273. $config = $this->getAdminConfig($type, $sectionData['config'], ConfigFile::OPTION_CREATE_EMPTY);
  274. if (!$config->setSectionOrder($order, $changed)) {
  275. throw new KurogoConfigurationException("Error setting the order of " . $sectionData['config']);
  276. }
  277. if ($changed) {
  278. if (!in_array($config, $this->changedConfigs)) {
  279. $this->changedConfigs[] = $config;
  280. }
  281. }
  282. }
  283. private function setConfigVar($type, $section, $subsection, $key, $value) {
  284. $typeStr = $this->getTypeStr($type);
  285. Kurogo::log(LOG_DEBUG, "Setting $key to \"$value\" in $typeStr: $section $subsection", 'admin');
  286. $sectionData = $this->getAdminData($type, $section, $subsection);
  287. $changed = false;
  288. switch ($sectionData['sectiontype'])
  289. {
  290. case 'fields':
  291. if (!isset($sectionData['fields'][$key])) {
  292. throw new KurogoConfigurationException("Invalid key $key for $type section $section");
  293. }
  294. $fieldData = $sectionData['fields'][$key];
  295. break;
  296. case 'section':
  297. $fieldData = $sectionData;
  298. break;
  299. default:
  300. throw new KurogoConfigurationException("Unable to handle $type $section. Invalid section type " . $sectionData['sectiontype']);
  301. }
  302. $config = $this->getAdminConfig($type, $fieldData['config'], ConfigFile::OPTION_CREATE_EMPTY);
  303. //remove blank values before validation
  304. if (is_array($value)) {
  305. foreach ($value as $k=>$v) {
  306. $prefix = isset($value[$k . '_prefix']) ? $value[$k . '_prefix'] : '';
  307. if ($prefix && defined($prefix)) {
  308. $value[$k] = constant($prefix) . '/' . $v;
  309. }
  310. if (isset($value[$k . '_prefix'])) {
  311. unset($value[$k . '_prefix']);
  312. }
  313. if (isset($fieldData['fields'][$k]['omitBlankValue']) && $fieldData['fields'][$k]['omitBlankValue'] && strlen($v)==0) {
  314. $changed = $changed || $config->clearVar($key, $k);
  315. unset($value[$k]);
  316. }
  317. if ($fieldData['fields'][$k]['type']=='paragraph') {
  318. $value[$k] = explode("\n\n", str_replace(array("\r\n","\r"), array("\n","\n"), $v));
  319. }
  320. }
  321. }
  322. if (isset($sectionData['sectionvalidatemethod'])) {
  323. $result = call_user_func($sectionData['sectionvalidatemethod'], $key, $value);
  324. if (KurogoError::isError($result)) {
  325. throw new KurogoException($result->getMessage());
  326. }
  327. }
  328. if (is_array($value)) {
  329. $result = true;
  330. foreach ($value as $k=>$v) {
  331. if (preg_match("/^(.*?)_prefix$/", $k,$bits)) {
  332. continue;
  333. }
  334. if (!isset($fieldData['fields'][$k])) {
  335. throw new KurogoConfigurationException("Invalid key $k for $typeStr:" . $fieldData['config'] . " section $key");
  336. }
  337. $prefix = isset($value[$k . '_prefix']) ? $value[$k . '_prefix'] : '';
  338. if ($prefix && defined($prefix)) {
  339. $v = constant($prefix) . '/' . $v;
  340. }
  341. if (!$config->setVar($key, $k, $v, $c)) {
  342. $result = false;
  343. }
  344. $changed = $changed || $c;
  345. }
  346. } else {
  347. if (isset($fieldData['omitBlankValue']) && $fieldData['omitBlankValue'] && strlen($value)==0) {
  348. $changed = $config->clearVar($fieldData['section'], $key);
  349. } else {
  350. if ($fieldData['type']=='paragraph') {
  351. $value = explode("\n\n", str_replace(array("\r\n","\r"), array("\n","\n"), $value));
  352. }
  353. $result = $config->setVar($fieldData['section'], $key, $value, $changed);
  354. if (!$result) {
  355. throw new KurogoConfigurationException("Error setting $config $section $key $value");
  356. }
  357. }
  358. }
  359. if ($changed) {
  360. if (!in_array($config, $this->changedConfigs)) {
  361. $this->changedConfigs[] = $config;
  362. }
  363. }
  364. }
  365. private function uploadFile($type, $section, $subsection, $key, $value) {
  366. $sectionData = $this->getAdminData($type, $section, $subsection);
  367. if (isset($value['error']) && $value['error'] != UPLOAD_ERR_OK) {
  368. throw new KurogoDataException(Kurogo::file_upload_error_message($value['error']));
  369. }
  370. if (!isset($value['tmp_name']) || !is_uploaded_file($value['tmp_name'])) {
  371. throw new KurogoDataException("Error locating uploaded file");
  372. }
  373. switch ($sectionData['sectiontype'])
  374. {
  375. case 'fields':
  376. if (!isset($sectionData['fields'][$key])) {
  377. throw new KurogoConfigurationException("Invalid key $key for $type section $section");
  378. }
  379. $fieldData = $sectionData['fields'][$key];
  380. break;
  381. case 'section':
  382. $fieldData = $sectionData;
  383. throw new KurogoConfigurationException("Code not written for this type of field");
  384. break;
  385. default:
  386. throw new KurogoConfigurationException("Unable to handle $type $section. Invalid section type " . $sectionData['sectiontype']);
  387. }
  388. if (!isset($fieldData['destinationType'])) {
  389. throw new KurogoConfigurationException("Unable to determine destination type");
  390. }
  391. switch ($fieldData['destinationType'])
  392. {
  393. case 'file':
  394. if (!isset($fieldData['destinationFile'])) {
  395. throw new KurogoConfigurationException("Unable to determine destination location");
  396. }
  397. $destination = $fieldData['destinationFile'];
  398. break;
  399. case 'folder':
  400. if (!isset($fieldData['destinationFile'])) {
  401. throw new KurogoConfigurationException("Unable to determine destination location");
  402. }
  403. if (!isset($fieldData['destinationFolder'])) {
  404. throw new KurogoConfigurationException("Unable to determine destination location");
  405. }
  406. $destination = rtrim($fieldData['destinationFolder'], '/') . '/' . ltrim($fieldData['destinationFile'],'/');
  407. break;
  408. }
  409. $prefix = isset($fieldData['destinationPrefix']) ? $fieldData['destinationPrefix'] : '';
  410. if ($prefix && defined($prefix)) {
  411. $destination = constant($prefix) . '/' . $destination;
  412. }
  413. if (isset($fieldData['fileType'])) {
  414. switch ($fieldData['fileType'])
  415. {
  416. case 'image':
  417. $this->setResponseVersion(1);
  418. try {
  419. $imageData = new ImageProcessor($value['tmp_name']);
  420. $transformer = new ImageTransformer($fieldData);
  421. $imageType = isset($fieldData['imageType']) ? $fieldData['imageType'] : null;
  422. $result = $imageData->transform($transformer, $imageType, $destination);
  423. if (KurogoError::isError($result)) {
  424. $this->throwError($result);
  425. }
  426. } catch (KurogoException $e) {
  427. throw new KurogoException("Uploaded file must be a valid image (" . $e->getMessage() . ")");
  428. }
  429. break;
  430. default:
  431. throw new KurogoConfigurationException("Unknown fileType " . $fieldData['fileType']);
  432. }
  433. } else {
  434. if (!move_uploaded_file($value['tmp_name'], $destination)) {
  435. $this->throwError(new KurogoError(1, "Cannot save file", "Unable to save uploaded file"));
  436. }
  437. }
  438. }
  439. public function initializeForCommand() {
  440. $this->requiresAdmin();
  441. switch ($this->command) {
  442. case 'checkversion':
  443. $current = Kurogo::sharedInstance()->checkCurrentVersion();
  444. Kurogo::log(LOG_INFO, sprintf("Checking version. This site: %s Current Kurogo Version: %s", $current, KUROGO_VERSION), 'admin');
  445. $uptodate = version_compare(KUROGO_VERSION, $current,">=");
  446. $messageKey = $uptodate ? 'KUROGO_VERSION_MESSAGE_UPTODATE' : 'KUROGO_VERSION_MESSAGE_NOTUPDATED';
  447. $data = array(
  448. 'current'=>$current,
  449. 'local' =>KUROGO_VERSION,
  450. 'uptodate' =>$uptodate,
  451. 'message'=>$this->getLocalizedString($messageKey, $current, KUROGO_VERSION)
  452. );
  453. $this->setResponse($data);
  454. $this->setResponseVersion(1);
  455. break;
  456. case 'getlocalizedstring':
  457. $key = $this->getArg('key');
  458. $data = array();
  459. if (is_array($key)) {
  460. foreach ($key as $k) {
  461. $data[$k] = $this->getLocalizedString($k);
  462. }
  463. } else {
  464. $data[$key] = $this->getLocalizedString($key);
  465. }
  466. $this->setResponse($data);
  467. $this->setResponseVersion(1);
  468. break;
  469. case 'clearcaches':
  470. Kurogo::log(LOG_NOTICE, "Clearing Site Caches", 'admin');
  471. $result = Kurogo::sharedInstance()->clearCaches();
  472. if ($result===0) {
  473. $this->setResponse(true);
  474. $this->setResponseVersion(1);
  475. } else {
  476. $this->throwError(new KurogoError(1, "Error clearing caches", "There was an error ($result) clearing the caches"));
  477. }
  478. break;
  479. case 'upload':
  480. $type = $this->getArg('type');
  481. $section = $this->getArg('section','');
  482. $subsection = null;
  483. switch ($type)
  484. {
  485. case 'module':
  486. $moduleID = $this->getArg('module','');
  487. $module = WebModule::factory($moduleID);
  488. $type = $module;
  489. break;
  490. case 'site':
  491. break;
  492. default:
  493. throw new KurogoConfigurationException("Invalid type $type");
  494. }
  495. if (count($_FILES)==0) {
  496. throw new KurogoException("No files uploaded");
  497. }
  498. foreach ($_FILES as $key=>$uploadData) {
  499. $this->uploadFile($type, $section, $subsection, $key, $uploadData);
  500. }
  501. $this->setResponseVersion(1);
  502. $this->setResponse(true);
  503. break;
  504. case 'getconfigsections':
  505. $type = $this->getArg('type');
  506. switch ($type)
  507. {
  508. case 'module':
  509. $moduleID = $this->getArg('module','');
  510. $module = WebModule::factory($moduleID);
  511. $sections = $module->getModuleAdminSections();
  512. break;
  513. case 'site':
  514. throw new KurogoConfigurationException("getconfigsections for site not handled yet");
  515. }
  516. $this->setResponse($sections);
  517. $this->setResponseVersion(1);
  518. break;
  519. case 'getconfigdata':
  520. $type = $this->getArg('type');
  521. $section = $this->getArg('section','');
  522. switch ($type)
  523. {
  524. case 'module':
  525. $moduleID = $this->getArg('module','');
  526. $module = WebModule::factory($moduleID);
  527. $adminData = $this->getAdminData($module, $section);
  528. break;
  529. case 'site':
  530. $adminData = $this->getAdminData('site', $section);
  531. break;
  532. }
  533. $this->setResponse($adminData);
  534. $this->setResponseVersion(1);
  535. break;
  536. case 'setconfigdata':
  537. $type = $this->getArg('type');
  538. $data = $this->getArg('data', array());
  539. $section = $this->getArg('section','');
  540. $subsection = null;
  541. if (empty($data)) {
  542. $data = array();
  543. } elseif (!is_array($data)) {
  544. throw new KurogoConfigurationException("Invalid data for $type $section");
  545. }
  546. switch ($type)
  547. {
  548. case 'module':
  549. if ($section == 'overview') {
  550. foreach ($data as $moduleID=>$props) {
  551. $module = WebModule::factory($moduleID);
  552. if (!is_array($props)) {
  553. throw new KurogoConfigurationException("Invalid properties for $type $section");
  554. }
  555. $valid_props = array('protected','secure','disabled','search');
  556. foreach ($props as $key=>$value) {
  557. if (!in_array($key, $valid_props)) {
  558. throw new KurogoConfigurationException("Invalid property $key for module $module");
  559. }
  560. $this->setConfigVar($module, 'general', $subsection, $key, $value);
  561. }
  562. }
  563. foreach ($this->changedConfigs as $config) {
  564. $config->saveFile();
  565. }
  566. $this->setResponse(true);
  567. $this->setResponseVersion(1);
  568. break 2;
  569. } else {
  570. $moduleID = $this->getArg('module','');
  571. $module = WebModule::factory($moduleID);
  572. $type = $module;
  573. }
  574. break;
  575. case 'site':
  576. break;
  577. default:
  578. throw new KurogoConfigurationException("Invalid type $type");
  579. }
  580. foreach ($data as $section=>$fields) {
  581. $adminData = $this->getAdminData($type, $section);
  582. if ($adminData['sectiontype']=='sections') {
  583. $subsection = key($fields);
  584. $fields = current($fields);
  585. $adminData = $this->getAdminData($type, $section, $subsection);
  586. }
  587. $fields = is_array($fields) ? $fields : array();
  588. foreach ($fields as $key=>$value) {
  589. if ($adminData['sectiontype']=='section' && isset($adminData['sectionclearvalues']) && $adminData['sectionclearvalues']) {
  590. if ($config = $this->getAdminConfig($type, $adminData['config'], ConfigFile::OPTION_DO_NOT_CREATE)) {
  591. $config->removeSection($key);
  592. }
  593. }
  594. // ignore prefix values. We'll put it back together later
  595. if (preg_match("/^(.*?)_prefix$/", $key,$bits)) {
  596. continue;
  597. }
  598. $prefix = isset($fields[$key . '_prefix']) ? $fields[$key . '_prefix'] : '';
  599. if ($prefix && defined($prefix)) {
  600. $value = constant($prefix) . '/' . $value;
  601. }
  602. $this->setConfigVar($type, $section, $subsection, $key, $value);
  603. }
  604. }
  605. if ($sectionorder = $this->getArg('sectionorder')) {
  606. foreach ($sectionorder as $section=>$order) {
  607. $this->setSectionOrder($type, $section, $subsection, $order);
  608. }
  609. }
  610. foreach ($this->changedConfigs as $config) {
  611. $config->saveFile();
  612. }
  613. $this->setResponse(true);
  614. $this->setResponseVersion(1);
  615. break;
  616. case 'removeconfigsection':
  617. $type = $this->getArg('type');
  618. $section = $this->getArg('section','');
  619. $key = $this->getArg('key', null);
  620. switch ($type)
  621. {
  622. case 'site':
  623. $subsection = $this->getArg('subsection',null);
  624. $sectionData = $this->getAdminData($type, $section, $subsection);
  625. $config = ConfigFile::factory($sectionData['config'],'site');
  626. break;
  627. case 'module':
  628. $moduleID = $this->getArg('module','');
  629. $module = WebModule::factory($moduleID);
  630. $sectionData = $this->getAdminData($module, $section);
  631. $config = $module->getConfig($sectionData['config']);
  632. break;
  633. default:
  634. throw new KurogoConfigurationException("Invalid type $type");
  635. }
  636. if (!isset($sectionData['sections']) || (!isset($sectionData['sectiondelete']) || !$sectionData['sectiondelete'])) {
  637. throw new KurogoConfigurationException("Config '$section' of module '$moduleID' does not permit removal of items");
  638. }
  639. if (!isset($sectionData['sections'][$key])) {
  640. throw new KurogoConfigurationException("Section $key not found in config '$section' of module '$moduleID'");
  641. }
  642. Kurogo::log(LOG_NOTICE, "Removing section $section from ". $this->getTypeStr($type) . " $subsection", 'admin');
  643. if (!$result = $config->removeSection($key)) {
  644. throw new KurogoException("Error removing item $key from config '" . $sectionData['config'] ."'");
  645. } else {
  646. $config->saveFile();
  647. }
  648. $this->setResponse(true);
  649. $this->setResponseVersion(1);
  650. break;
  651. case 'setmodulelayout':
  652. Kurogo::log(LOG_NOTICE, "Updating module layout", 'admin');
  653. $data = $this->getArg('data', array());
  654. $config = ModuleConfigFile::factory('home', 'module');
  655. if (!isset($data['primary_modules'])) {
  656. $data['primary_modules'] = array();
  657. }
  658. $config->setSection('primary_modules', $data['primary_modules']);
  659. if (!isset($data['secondary_modules'])) {
  660. $data['secondary_modules'] = array();
  661. }
  662. $config->setSection('secondary_modules', $data['secondary_modules']);
  663. $config->saveFile();
  664. $this->setResponse(true);
  665. $this->setResponseVersion(1);
  666. break;
  667. default:
  668. $this->invalidCommand();
  669. break;
  670. }
  671. }
  672. }