BxDolStudioOptions.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. define('BX_DOL_STUDIO_STG_TYPE_SYSTEM', 'system');
  10. define('BX_DOL_STUDIO_STG_TYPE_DEFAULT', BX_DOL_STUDIO_STG_TYPE_SYSTEM);
  11. define('BX_DOL_STUDIO_STG_GROUP_SYSTEM', 'system');
  12. define('BX_DOL_STUDIO_STG_GROUP_MODULES', 'modules');
  13. define('BX_DOL_STUDIO_STG_GROUP_LANGUAGES', 'languages');
  14. define('BX_DOL_STUDIO_STG_GROUP_TEMPLATES', 'templates');
  15. define('BX_DOL_STUDIO_STG_CATEGORY_SYSTEM', 'system');
  16. define('BX_DOL_STUDIO_STG_CATEGORY_LANGUAGES', 'languages');
  17. define('BX_DOL_STUDIO_STG_CATEGORY_TEMPLATES', 'templates');
  18. define('BX_DOL_STUDIO_STG_CATEGORY_AUDIT', 'audit');
  19. define('BX_DOL_STUDIO_STG_MIX_SYSTEM', 'system');
  20. define('BX_DOL_STUDIO_STG_MIX_DEFAULT', BX_DOL_STUDIO_STG_MIX_SYSTEM);
  21. class BxDolStudioOptions extends BxDol
  22. {
  23. protected $oDb;
  24. protected $sParamPrefix;
  25. protected $sBaseUrl;
  26. protected $sType;
  27. protected $sCategory;
  28. protected $aCategories;
  29. protected $aCustomCategories;
  30. protected $bManage; //--- Enable/Disable Manage panel for system settings.
  31. protected $bReadOnly; //--- Enable/Disable ReadOnly mode for system settings. Note. It doesn't affect Editable mixes.
  32. protected $bMixes; //--- Enable/Disable mixes feature.
  33. protected $sMix;
  34. protected $aMix;
  35. protected $sStorage;
  36. protected $sTranscoder;
  37. protected $sErrorMessage;
  38. function __construct($sType = '', $mixedCategory = '', $sMix = '')
  39. {
  40. parent::__construct();
  41. $this->oDb = new BxDolStudioOptionsQuery();
  42. $this->sParamPrefix = 'opt';
  43. $this->sBaseUrl = BX_DOL_URL_STUDIO . 'options.php';
  44. $this->sType = BX_DOL_STUDIO_STG_TYPE_DEFAULT;
  45. if(!empty($sType) && is_string($sType))
  46. $this->sType = $sType;
  47. $this->sCategory = '';
  48. if(!empty($mixedCategory)) {
  49. if(is_array($mixedCategory))
  50. $this->sCategory = $mixedCategory;
  51. else if(is_string($mixedCategory)) {
  52. //--- Serialized array
  53. if(preg_match('/^a:[\d+]:\{/', $mixedCategory))
  54. $this->sCategory = json_decode($mixedCategory, true);
  55. //--- Comma separated list
  56. if(empty($this->sCategory) && strpos($mixedCategory, ',') !== false)
  57. $this->sCategory = explode(',', $mixedCategory);
  58. if(empty($this->sCategory))
  59. $this->sCategory = $mixedCategory;
  60. }
  61. }
  62. $this->bManage = false;
  63. $this->bReadOnly = false;
  64. $this->bMixes = false;
  65. $this->sMix = $sMix;
  66. $this->aMix = [];
  67. $this->sStorage = 'sys_images_custom';
  68. $this->sTranscoder = 'sys_custom_images';
  69. $this->sErrorMessage = '';
  70. }
  71. public function checkAction()
  72. {
  73. $sAction = bx_get($this->sParamPrefix . '_action');
  74. if($sAction === false)
  75. return false;
  76. $sAction = bx_process_input($sAction);
  77. $sValue = '';
  78. if(bx_get($this->sParamPrefix . '_value') !== false)
  79. $sValue = bx_process_input(bx_get($this->sParamPrefix . '_value'));
  80. $aResult = ['code' => 0, 'message' => ''];
  81. switch($sAction) {
  82. case 'import':
  83. $aResult = array_merge($aResult, $this->getPopupCodeImport('settings'));
  84. break;
  85. case 'export':
  86. $aResult = array_merge($aResult, $this->export());
  87. break;
  88. case 'download':
  89. $aResult = array_merge($aResult, $this->download());
  90. break;
  91. case 'select-mix':
  92. $aResult = array_merge($aResult, $this->selectMix($sValue));
  93. break;
  94. case 'create-mix':
  95. $aResult = array_merge($aResult, $this->getPopupCodeCreateMix());
  96. break;
  97. case 'import-mix':
  98. $aResult = array_merge($aResult, $this->getPopupCodeImportMix());
  99. break;
  100. case 'export-mix':
  101. $aResult = array_merge($aResult, $this->exportMix((int)$sValue));
  102. break;
  103. case 'download-mix':
  104. $aResult = array_merge($aResult, $this->downloadMix((int)$sValue));
  105. break;
  106. case 'publish-mix':
  107. $aResult = array_merge($aResult, $this->publishMix((int)$sValue));
  108. break;
  109. case 'hide-mix':
  110. $aResult = array_merge($aResult, $this->hideMix((int)$sValue));
  111. break;
  112. case 'delete-mix':
  113. $aResult = array_merge($aResult, $this->deleteMix((int)$sValue));
  114. break;
  115. case 'get-page-by-type':
  116. $this->sType = $sValue;
  117. $aResult['content'] = $this->getPageCode();
  118. break;
  119. }
  120. return $aResult;
  121. }
  122. public function getType()
  123. {
  124. return $this->sType;
  125. }
  126. public function enableManage($bManage = true)
  127. {
  128. $this->bManage = $bManage;
  129. }
  130. public function enableReadOnly($bReadOnly = true)
  131. {
  132. $this->bReadOnly = $bReadOnly;
  133. }
  134. public function enableMixes($bMixes = true)
  135. {
  136. $this->bMixes = $bMixes;
  137. }
  138. public function export()
  139. {
  140. return [
  141. 'url' => bx_append_url_params($this->sBaseUrl, [
  142. $this->sParamPrefix . '_action' => 'download',
  143. ]),
  144. 'eval' => $this->getJsObject() . '.onExport(oData);'
  145. ];
  146. }
  147. public function download()
  148. {
  149. $sType = 'settings';
  150. $mixedFull = false;
  151. $aOptionsExclude = ['sys_revision', 'sys_cron_time', 'sys_eq_time', 'sys_push_queue_time'];
  152. $aTypes = [];
  153. $this->oDb->getTypes(['type' => 'all'], $aTypes, false);
  154. if(empty($aTypes) || !is_array($aTypes))
  155. BxDolStudioTemplate::getInstance()->displayPageNotFound();
  156. $aListTypes = [];
  157. foreach($aTypes as $aType) {
  158. $aCategories = [];
  159. $this->oDb->getCategories(['type' => 'by_type_id_key_name', 'value' => $aType['id']], $aCategories, false);
  160. if(empty($aCategories) || !is_array($aCategories))
  161. continue;
  162. $aListCategories = [];
  163. foreach($aCategories as $aCategory) {
  164. $aOptions = [];
  165. $this->oDb->getOptions(['type' => 'by_category_id', 'value' => $aCategory['id'], 'for_export' => 1, 'exclude' => $aOptionsExclude], $aOptions, false);
  166. if(empty($aOptions) || !is_array($aOptions))
  167. continue;
  168. $aListCategories[] = [
  169. 'name' => $aCategory['name'],
  170. 'options' => $aOptions
  171. ];
  172. }
  173. $aListTypes[] = [
  174. 'name' => $aType['name'],
  175. 'categories' => $aListCategories
  176. ];
  177. }
  178. $iNow = time();
  179. $sContent = json_encode([
  180. 'meta' => [
  181. 'version' => bx_get_ver(),
  182. 'type' => $sType,
  183. 'full' => $mixedFull,
  184. 'date' => date('D, d M Y H:i:s', $iNow),
  185. ],
  186. 'data' => [
  187. 'types' => $aListTypes
  188. ]
  189. ]);
  190. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  191. header("Content-type: application/json");
  192. header("Content-Length: " . strlen($sContent));
  193. header("Content-Disposition: attachment; filename=\"" . $sType . ($mixedFull ? "_f" : "") . "_" . date('d_m_Y', $iNow) . ".json\"");
  194. echo $sContent;
  195. exit;
  196. }
  197. public function selectMix($sName)
  198. {
  199. $this->oDb->updateMixes(['active' => 0], [
  200. 'type' => $this->sType,
  201. 'category' => is_string($this->sCategory) ? $this->sCategory : '',
  202. 'active' => 1
  203. ]);
  204. $aResult = [];
  205. if($sName == BX_DOL_STUDIO_STG_MIX_SYSTEM || $this->oDb->updateMixes(['active' => 1], ['name' => $sName])) {
  206. $this->clearCache();
  207. $aResult = ['eval' => $this->getJsObject() . '.onMixSelect(oData);'];
  208. }
  209. else
  210. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  211. return $aResult;
  212. }
  213. public function exportMix($iId)
  214. {
  215. $aMix = [];
  216. $this->oDb->getMixes(['type' => 'by_id', 'value' => $iId], $aMix, false);
  217. if(empty($aMix) || !is_array($aMix))
  218. return array('message' => _t('_adm_stg_err_cannot_perform'));
  219. return [
  220. 'url' => bx_append_url_params($this->sBaseUrl, [
  221. $this->sParamPrefix . '_action' => 'download-mix',
  222. $this->sParamPrefix . '_value' => $iId,
  223. ]),
  224. 'eval' => $this->getJsObject() . '.onMixExport(oData);'
  225. ];
  226. }
  227. public function downloadMix($iId)
  228. {
  229. $aMix = [];
  230. $this->oDb->getMixes(['type' => 'by_id', 'value' => $iId], $aMix, false);
  231. if(empty($aMix) || !is_array($aMix))
  232. BxDolStudioTemplate::getInstance()->displayPageNotFound();
  233. $aOptions = [];
  234. $this->oDb->getMixesOptions(['type' => 'by_mix_id_pair_option_value', 'value' => $iId, 'for_export' => 1], $aOptions, false);
  235. $sContent = json_encode([
  236. 'mix' => [
  237. 'type' => $aMix['type'],
  238. 'category' => $aMix['category'],
  239. 'name' => $aMix['name'],
  240. 'title' => $aMix['title']
  241. ],
  242. 'options' => $aOptions
  243. ]);
  244. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  245. header("Content-type: application/json");
  246. header("Content-Length: " . strlen($sContent));
  247. header("Content-Disposition: attachment; filename=\"". strtolower($aMix['name']) . ".json\"");
  248. echo $sContent;
  249. exit;
  250. }
  251. public function publishMix($iId)
  252. {
  253. $aResult = [];
  254. if($this->oDb->updateMixes(['published' => 1], ['id' => $iId])) {
  255. $this->clearCache();
  256. $aResult = ['eval' => $this->getJsObject() . '.onMixPublish(oData);'];
  257. }
  258. else
  259. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  260. return $aResult;
  261. }
  262. public function hideMix($iId)
  263. {
  264. $aResult = [];
  265. if($this->oDb->updateMixes(['published' => 0], ['id' => $iId])) {
  266. $this->clearCache();
  267. $aResult = ['eval' => $this->getJsObject() . '.onMixHide(oData);'];
  268. }
  269. else
  270. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  271. return $aResult;
  272. }
  273. public function deleteMix($iId)
  274. {
  275. $aResult = [];
  276. if($this->oDb->deleteMixesOptions(['mix_id' => $iId]) && $this->oDb->deleteMixes(['id' => $iId])) {
  277. $this->clearCache();
  278. $aResult = ['eval' => $this->getJsObject() . '.onMixDelete(oData);'];
  279. }
  280. else
  281. $aResult = ['message' => _t('_adm_stg_err_cannot_perform')];
  282. return $aResult;
  283. }
  284. public function saveChanges(&$oForm)
  285. {
  286. $iMixId = $oForm->getCleanValue('mix_id');
  287. $aCategories = explode(',', $oForm->getCleanValue('categories'));
  288. $sEvalRenewToken = $this->getJsObject() . ".onSubmitted('" . $oForm->getId() . "', '" . $oForm->getCsrfToken() . "', oData);";
  289. foreach ($aCategories as $sCategory) {
  290. $aOptions = [];
  291. $iOptions = $this->oDb->getOptions(['type' => 'by_category_name_full', 'value' => $sCategory], $aOptions);
  292. $aData = [];
  293. foreach($aOptions as $aOption) {
  294. $aData[$aOption['name']] = $this->getSubmittedValue($aOption, $oForm);
  295. if($aData[$aOption['name']] === false && !empty($this->sErrorMessage)) {
  296. $this->sCategory = $sCategory;
  297. return bx_get_js_result([
  298. 'code' => 1,
  299. 'cue' => 'error',
  300. 'message' => _t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($this->sErrorMessage)),
  301. 'translate' => false,
  302. 'eval' => $sEvalRenewToken
  303. ]);
  304. }
  305. if(!empty($aOption['check'])) {
  306. $sCheckerHelper = '';
  307. if(!empty($aOption['type_name']) && BxDolRequest::serviceExists($aOption['type_name'], 'get_settings_checker_helper'))
  308. $sCheckerHelper = BxDolService::call($aOption['type_name'], 'get_settings_checker_helper');
  309. if($sCheckerHelper == '') {
  310. bx_import('BxDolStudioForm');
  311. $sCheckerHelper = 'BxDolStudioFormCheckerHelper';
  312. }
  313. $oChecker = new $sCheckerHelper();
  314. $aCheckFunction = [$oChecker, 'check' . bx_gen_method_name($aOption['check'])];
  315. $aCheckFunctionParams = [$aData[$aOption['name']]];
  316. if(!empty($aOption['check_params']))
  317. $aCheckFunctionParams = array_merge($aCheckFunctionParams, array_values(unserialize($aOption['check_params'])));
  318. if(is_callable($aCheckFunction) && !call_user_func_array($aCheckFunction, $aCheckFunctionParams)) {
  319. $this->sCategory = $sCategory;
  320. return bx_get_js_result([
  321. 'code' => 2,
  322. 'cue' => 'error',
  323. 'message' => _t('_adm_stg_err_save_error_message', _t($aOption['caption']), _t($aOption['check_error'])),
  324. 'translate' => false,
  325. 'eval' => $sEvalRenewToken
  326. ]);
  327. }
  328. }
  329. if(isset($aData[$aOption['name']]))
  330. $aData[$aOption['name']] = $this->getProcessedValue($aOption, $aData[$aOption['name']]);
  331. else
  332. $aData[$aOption['name']] = $this->getEmptyValue($aOption);
  333. $mixedValue = $this->oDb->getParam($aOption['name']);
  334. if($this->oDb->setParam($aOption['name'], $aData[$aOption['name']], $iMixId)) {
  335. $aCategoryInfo = [];
  336. $this->oDb->getCategories(['type' => 'by_name', 'value' => $sCategory], $aCategoryInfo, false);
  337. bx_alert('system', 'save_setting', 0, 0, [
  338. 'category' => $aCategoryInfo,
  339. 'option' => $aOption['name'],
  340. 'value' => $aData[$aOption['name']],
  341. 'value_prior' => $mixedValue
  342. ]);
  343. }
  344. }
  345. }
  346. $this->clearCache();
  347. // The interface sound of the reply (see processJsonData): saved, subtly.
  348. return bx_get_js_result([
  349. 'code' => 0,
  350. 'cue' => ['cue' => 'success', 'emphasis' => 'subtle'],
  351. 'message' => '_adm_stg_scs_save',
  352. 'eval' => $sEvalRenewToken
  353. ]);
  354. }
  355. protected function isReadOnly()
  356. {
  357. $bMix = !empty($this->aMix);
  358. return (!$bMix && $this->bReadOnly) || ($bMix && (int)$this->aMix['editable'] == 0);
  359. }
  360. protected function getSubmittedValue($aOption, &$oForm)
  361. {
  362. $mixedValue = '';
  363. switch($aOption['type']) {
  364. case 'image':
  365. $mixedValue = (int)getParam($aOption['name']);
  366. $mixedIds = $oForm->getCleanValue($aOption['name']);
  367. if(empty($mixedIds)) {
  368. if(!empty($mixedValue))
  369. $mixedValue = 0;
  370. break;
  371. }
  372. $oStorage = BxDolStorage::getObjectInstance($this->sStorage);
  373. if(!$oStorage)
  374. break;
  375. //--- Concatenation integer values as strings is required to get unique content id
  376. $iContentId = (int)($aOption['id'] . (int)$oForm->getCleanValue('mix_id'));
  377. $iImageId = is_array($mixedIds) ? (int)array_shift($mixedIds) : (int)$mixedIds;
  378. $oStorage->updateGhostsContentId($iImageId, false, $iContentId);
  379. $mixedValue = $iImageId;
  380. break;
  381. default:
  382. $mixedValue = $oForm->getCleanValue($aOption['name']);
  383. }
  384. return $mixedValue;
  385. }
  386. protected function getProcessedValue($aOption, $mixedValue)
  387. {
  388. $sMethod = 'processCustomValue' . bx_gen_method_name(trim(str_replace($this->sType, '', $aOption['name']), '_'));
  389. if(method_exists($this, $sMethod))
  390. return $this->$sMethod($aOption, $mixedValue);
  391. switch($aOption['type']) {
  392. case 'secret':
  393. case 'secret_text':
  394. if(strcmp(bx_gen_secret($aOption['value']), $mixedValue) == 0)
  395. $mixedValue = $aOption['value'];
  396. break;
  397. default:
  398. if(is_array($mixedValue))
  399. $mixedValue = implode(',', $mixedValue);
  400. }
  401. return $mixedValue;
  402. }
  403. protected function getEmptyValue($aOption)
  404. {
  405. $mixedValue = '';
  406. switch($aOption['type']) {
  407. case 'digit':
  408. $mixedValue = 0;
  409. break;
  410. case 'select':
  411. if (BxDolService::isSerializedService($aOption['extra']))
  412. $aValues = BxDolService::callSerialized($aOption['extra']);
  413. else
  414. $aValues = explode(',', $aOption['extra']);
  415. $mixedValue = $aValues[0];
  416. break;
  417. case 'text':
  418. case 'checkbox':
  419. case 'file':
  420. $mixedValue = "";
  421. break;
  422. }
  423. return $mixedValue;
  424. }
  425. protected function clearCache()
  426. {
  427. BxDolDb::getInstance()->cacheParams(true, true);
  428. $oCacheUtilities = BxDolCacheUtilities::getInstance();
  429. $oCacheUtilities->clear('db');
  430. $oCacheUtilities->clear('css');
  431. }
  432. protected function getCustomValueCurrencySign($aItem, $mixedValue)
  433. {
  434. return htmlspecialchars($mixedValue);
  435. }
  436. protected function processCustomValueCurrencySign($aItem, $mixedValue)
  437. {
  438. return htmlspecialchars_decode($mixedValue);
  439. }
  440. }
  441. /** @} */