BxDolInstallSiteConfig.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. <?php defined('BX_DOL_INSTALL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaInstall UNA Install
  7. * @{
  8. */
  9. define('BX_INSTALL_ERR_GENERAL', 'general');
  10. class BxDolInstallSiteConfig
  11. {
  12. protected $_sSqlDb;
  13. protected $_sSqlMariaDb;
  14. protected $_sSqlAddon;
  15. protected $_sPatternHeader;
  16. protected $_aDbErrorMap;
  17. protected $_aConfig;
  18. protected $_sServerHttpHost;
  19. protected $_sServerPhpSelf;
  20. protected $_sServerDocumentRoot;
  21. protected $_isAutologin;
  22. public function __construct($sServerHttpHost = false, $sServerPhpSelf = false, $sServerDocumentRoot = false, $isAutologin = true)
  23. {
  24. $this->_sServerHttpHost = false === $sServerHttpHost ? $_SERVER['HTTP_HOST'] : $sServerHttpHost;
  25. $this->_sServerPhpSelf = false === $sServerPhpSelf ? $_SERVER['PHP_SELF'] : $sServerPhpSelf;
  26. $this->_sServerDocumentRoot = false === $sServerDocumentRoot ? $_SERVER['DOCUMENT_ROOT'] : $sServerDocumentRoot;
  27. $this->_isAutologin = $isAutologin;
  28. $this->_sSqlDb = BX_INSTALL_DIR . 'sql/system.sql';
  29. $this->_sSqlMariaDb = BX_INSTALL_DIR . 'sql/mariadb.sql';
  30. $this->_sSqlAddon = BX_INSTALL_DIR . 'sql/addon.sql';
  31. $this->_sPatternHeader = BX_INSTALL_DIR . 'patterns/header.inc.php';
  32. $this->_aDbErrorMap = array (
  33. 'Database connect failed' => array ('fields' => array('db_host', 'db_user', 'db_password'), 'msg' => _t('_sys_inst_msg_db_err_connect')),
  34. 'Database select failed' => array ('fields' => array('db_name'), 'msg' => _t('_sys_inst_msg_db_err_select')),
  35. );
  36. $this->_aConfig = array (
  37. // path config
  38. 'section_site_paths_open' => array(
  39. 'name' => _t('_sys_inst_conf_section_paths'),
  40. 'func' => 'rowSectionOpen',
  41. ),
  42. 'site_url' => array(
  43. 'name' => _t('_sys_inst_conf_field_site_url'),
  44. 'ex' => 'http://www.mydomain.com/path/',
  45. 'desc' => _t('_sys_inst_conf_desc_site_url'),
  46. 'def' => 'http://',
  47. 'def_exp' => array('defUrl', ''),
  48. 'check' => array('checkLength', 10),
  49. ),
  50. 'root_dir' => array(
  51. 'name' => _t('_sys_inst_conf_field_root_dir'),
  52. 'ex' => '/home/mydomain/public_html/',
  53. 'desc' => _t('_sys_inst_conf_desc_root_dir'),
  54. 'def_exp' => array('defPath', ''),
  55. 'check' => array('checkLength', 1),
  56. ),
  57. 'section_site_paths_close' => array(
  58. 'func' => 'rowSectionClose',
  59. ),
  60. // db config
  61. 'section_db_config_open' => array(
  62. 'name' => _t('_sys_inst_conf_section_db_config'),
  63. 'func' => 'rowSectionOpen',
  64. ),
  65. 'db_host' => array(
  66. 'name' => _t('_sys_inst_conf_field_db_host'),
  67. 'ex' => 'localhost',
  68. 'desc' => _t('_sys_inst_conf_desc_db_host'),
  69. 'def' => 'localhost',
  70. 'check' => array('checkLength', 1),
  71. 'db_conf' => 'host',
  72. ),
  73. 'db_port' => array(
  74. 'name' => _t('_sys_inst_conf_field_db_port'),
  75. 'ex' => '5506',
  76. 'desc' => _t('_sys_inst_conf_desc_db_port'),
  77. 'db_conf' => 'port',
  78. ),
  79. 'db_sock' => array(
  80. 'name' => _t('_sys_inst_conf_field_db_sock'),
  81. 'ex' => '/tmp/mysql.sock',
  82. 'desc' => _t('_sys_inst_conf_desc_db_sock'),
  83. 'db_conf' => 'sock',
  84. ),
  85. 'db_name' => array(
  86. 'name' => _t('_sys_inst_conf_field_db_name'),
  87. 'ex' => 'mydomian_tri',
  88. 'desc' => _t('_sys_inst_conf_desc_db_name'),
  89. 'check' => array('checkLength', 1),
  90. 'db_conf' => 'name',
  91. ),
  92. 'db_user' => array(
  93. 'name' => _t('_sys_inst_conf_field_db_user'),
  94. 'ex' => 'mydomian_tri',
  95. 'desc' => _t('_sys_inst_conf_desc_db_user'),
  96. 'check' => array('checkLength', 1),
  97. 'db_conf' => 'user',
  98. ),
  99. 'db_password' => array(
  100. 'name' => _t('_sys_inst_conf_field_db_pwd'),
  101. 'ex' => 'Super*Secret#Word_1234',
  102. 'desc' => _t('_sys_inst_conf_desc_db_pwd'),
  103. 'db_conf' => 'pwd',
  104. ),
  105. 'db_engine' => array(
  106. 'name' => _t('_sys_inst_conf_field_db_engine'),
  107. 'ex' => 'INNODB',
  108. 'desc' => _t('_sys_inst_conf_desc_db_engine'),
  109. 'def' => 'MYISAM',
  110. ),
  111. 'section_db_config_close' => array(
  112. 'func' => 'rowSectionClose',
  113. ),
  114. // site config
  115. 'section_site_info_open' => array(
  116. 'name' => _t('_sys_inst_conf_section_site_info'),
  117. 'func' => 'rowSectionOpen',
  118. ),
  119. 'site_title' => array(
  120. 'name' => _t('_sys_inst_conf_field_site_title'),
  121. 'ex' => 'The Best Community',
  122. 'desc' => _t('_sys_inst_conf_desc_site_title'),
  123. 'check' => array('checkLength', 1),
  124. ),
  125. 'site_email' => array(
  126. 'name' => _t('_sys_inst_conf_field_site_email'),
  127. 'ex' => 'no-reply@youdomain.here',
  128. 'desc' => _t('_sys_inst_conf_desc_site_email'),
  129. 'check' => array('checkEmail', 3),
  130. ),
  131. 'admin_email' => array(
  132. 'name' => _t('_sys_inst_conf_field_admin_email'),
  133. 'ex' => 'admin@email.here',
  134. 'desc' => _t('_sys_inst_conf_desc_admin_email'),
  135. 'check' => array('checkEmail', 3),
  136. ),
  137. 'admin_username' => array(
  138. 'name' => _t('_sys_inst_conf_field_admin_username'),
  139. 'ex' => 'admin',
  140. 'desc' => _t('_sys_inst_conf_desc_admin_username'),
  141. 'check' => array('checkLength', 1),
  142. ),
  143. 'admin_password' => array(
  144. 'name' => _t('_sys_inst_conf_field_admin_pwd'),
  145. 'ex' => 'Super*Secret#Word_1234',
  146. 'desc' => _t('_sys_inst_conf_desc_admin_pwd'),
  147. 'check' => array('checkLength', 1),
  148. ),
  149. 'section_site_info_close' => array(
  150. 'func' => 'rowSectionClose',
  151. ),
  152. // link with boonex market
  153. 'section_link_to_market_open' => array(
  154. 'name' => _t('_sys_inst_conf_section_link_with_market'),
  155. 'func' => 'rowSectionOpen',
  156. ),
  157. 'oauth_key' => array(
  158. 'name' => _t('_sys_inst_conf_field_oauth_key'),
  159. 'ex' => '123abc4d',
  160. 'desc' => _t('_sys_inst_conf_desc_oauth_key_secret'),
  161. 'check' => array('checkLengthExactOrEmpty', 10),
  162. ),
  163. 'oauth_secret' => array(
  164. 'name' => _t('_sys_inst_conf_field_oauth_secret'),
  165. 'ex' => '45de12368a9b367abc23512a',
  166. 'desc' => _t('_sys_inst_conf_desc_oauth_key_secret'),
  167. 'check' => array('checkLengthExactOrEmpty', 32),
  168. ),
  169. 'section_link_to_market_close' => array(
  170. 'func' => 'rowSectionClose',
  171. ),
  172. // modules
  173. 'section_modules_open' => array(
  174. 'name' => _t('_sys_inst_conf_section_modules'),
  175. 'func' => 'rowSectionOpen',
  176. ),
  177. BX_DOL_MODULE_TYPE_LANGUAGE => array(
  178. 'name' => _t('_sys_inst_conf_field_language'),
  179. 'desc' => _t('_sys_inst_conf_desc_language'),
  180. 'def' => isset($_COOKIE['lang']) ? $_COOKIE['lang'] : (isset($_GET['lang']) ? $_GET['lang'] : 'en'),
  181. 'func' => 'rowSelect',
  182. 'vals' => $this->getSelectValues(BX_DOL_MODULE_TYPE_LANGUAGE),
  183. ),
  184. BX_DOL_MODULE_TYPE_TEMPLATE => array(
  185. 'name' => _t('_sys_inst_conf_field_template'),
  186. 'desc' => _t('_sys_inst_conf_desc_template'),
  187. 'def' => 'artificer',
  188. 'func' => 'rowSelect',
  189. 'vals' => $this->getSelectValues(BX_DOL_MODULE_TYPE_TEMPLATE),
  190. ),
  191. BX_DOL_MODULE_TYPE_MODULE => array(
  192. 'name' => _t('_sys_inst_conf_field_profiles'),
  193. 'desc' => _t('_sys_inst_conf_desc_profiles'),
  194. 'def' => 'persons',
  195. 'func' => 'rowSelect',
  196. 'vals' => $this->getSelectValues(BX_DOL_MODULE_TYPE_MODULE),
  197. ),
  198. 'section_modules_close' => array(
  199. 'func' => 'rowSectionClose',
  200. ),
  201. );
  202. }
  203. public function getAutoValues($aData = array())
  204. {
  205. $aRet = array();
  206. foreach($this->_aConfig as $sKey => $a) {
  207. $sAutoMessage = '';
  208. $aRet[$sKey] = $this->def ($aData, $sKey, $a, $sAutoMessage);
  209. }
  210. return $aRet;
  211. }
  212. public function getFormHtml($aData = false, $bRedirectOnSuccess = true, &$sOutputErrorMessage = null)
  213. {
  214. if (false === $aData)
  215. $aData = $_POST;
  216. $aErrorFields = array();
  217. if (isset($aData['site_config'])) {
  218. $aErrorFields = $this->processConfigData($this->processInputData($aData));
  219. if (empty($aErrorFields)) {
  220. if ($bRedirectOnSuccess) {
  221. $sHost = $this->_sServerHttpHost;
  222. $sUri = rtrim(dirname($this->_sServerPhpSelf), '/\\');
  223. $sPage = 'index.php?action=finish';
  224. $sProto = $this->proto();
  225. header("Location: {$sProto}{$sHost}{$sUri}/{$sPage}");
  226. exit;
  227. } else {
  228. return true;
  229. }
  230. }
  231. }
  232. $sErrorMessage = '';
  233. if (isset($aErrorFields[BX_INSTALL_ERR_GENERAL]) && $aErrorFields[BX_INSTALL_ERR_GENERAL]) {
  234. $sErrorMessage = '<div class="bx-install-error-message bx-def-padding bx-def-margin-bottom">' . $aErrorFields[BX_INSTALL_ERR_GENERAL] . '</div>';
  235. if (null !== $sOutputErrorMessage)
  236. $sOutputErrorMessage = $aErrorFields[BX_INSTALL_ERR_GENERAL] . (empty($aErrorFields) ? '' : ' / Fields: ' . join(',', array_keys($aErrorFields)));
  237. }
  238. $sRows = $this->getFormFields($aErrorFields, $aData);
  239. $sSubmitTitle = _t('_Submit');
  240. return <<<EOF
  241. {$sErrorMessage}
  242. <form method="post">
  243. <div class="bx-form-advanced-wrapper sys_account_wrapper">
  244. {$sRows}
  245. <div class="bx-form-element-wrapper bx-def-margin-top">
  246. <div class="bx-form-value">
  247. <div class="bx-form-input-wrapper bx-form-input-wrapper-submit">
  248. <button class="bx-def-font-inputs bx-form-input-submit bx-btn bx-btn-primary" type="submit" name="site_config" value="1">
  249. {$sSubmitTitle}
  250. </button>
  251. </div>
  252. </div>
  253. </div>
  254. </div>
  255. <form>
  256. EOF;
  257. }
  258. public function processConfigData ($a)
  259. {
  260. $aSteps = array('checkConfig', 'processConfigDataDb', 'processConfigDataHeader', 'processModules');
  261. foreach ($aSteps as $sFunc) {
  262. $aErrors = $this->{$sFunc} ($a);
  263. if (!empty($aErrors))
  264. return $aErrors;
  265. }
  266. return array();
  267. }
  268. protected function checkConfig($a)
  269. {
  270. $aErrorFields = array();
  271. foreach ($this->_aConfig as $sKey => $r) {
  272. if (!$this->check ($sKey, isset($a[$sKey]) ? $a[$sKey] : '', $r))
  273. $aErrorFields[$sKey] = true;
  274. }
  275. if (!empty($aErrorFields))
  276. $aErrorFields[BX_INSTALL_ERR_GENERAL] = _t('_sys_inst_msg_form_error');
  277. return $aErrorFields;
  278. }
  279. public function processConfigDataDb ($a)
  280. {
  281. $aDbConf = array ('error_checking' => false);
  282. foreach ($this->_aConfig as $sKey => $r)
  283. if (isset($this->_aConfig[$sKey]['db_conf']))
  284. $aDbConf[$this->_aConfig[$sKey]['db_conf']] = $a[$sKey];
  285. if ('INNODB' == getenv('UNA_DB_ENGINE'))
  286. $aDbConf['storage_engine'] = 'INNODB';
  287. $sErrorMessage = '';
  288. $oDb = BxDolDb::getInstanceWithConf($aDbConf, $sErrorMessage);
  289. if (!$oDb) {
  290. $aErrorFields = array();
  291. if (isset($this->_aDbErrorMap[$sErrorMessage])) {
  292. foreach ($this->_aDbErrorMap[$sErrorMessage]['fields'] as $sField)
  293. $aErrorFields[$sField] = true;
  294. $aErrorFields[BX_INSTALL_ERR_GENERAL] = $this->_aDbErrorMap[$sErrorMessage]['msg'];
  295. } else {
  296. $aErrorFields[BX_INSTALL_ERR_GENERAL] = $sErrorMessage;
  297. }
  298. return $aErrorFields;
  299. }
  300. $mixedRes = $oDb->executeSQL($this->_sSqlDb);
  301. if (true !== $mixedRes)
  302. return $this->dbErrors2ErrorFields($mixedRes);
  303. // UUID and VECTOR. MariaDB 11.7 and newer only. MySQL and older MariaDB cannot create these tables.
  304. if ($this->shouldRunMariaDbSql($oDb)) {
  305. $mixedRes = $oDb->executeSQL($this->_sSqlMariaDb);
  306. if (true !== $mixedRes)
  307. return $this->dbErrors2ErrorFields($mixedRes);
  308. }
  309. $mixedRes = $oDb->executeSQL($this->_sSqlAddon, $this->getMarkersForDb($a, $oDb));
  310. if (true !== $mixedRes)
  311. return $this->dbErrors2ErrorFields($mixedRes);
  312. $aSqlDirs = array(
  313. BX_INSTALL_DIR . 'sql/',
  314. BX_INSTALL_DIR_MODULES . 'system/install/sql/',
  315. );
  316. foreach ($aSqlDirs as $sSqlDir) {
  317. if (!is_dir($sSqlDir))
  318. continue;
  319. $aFiles = scandir($sSqlDir);
  320. foreach ($aFiles as $sFile) {
  321. if ($sFile[0] === '.' || $sFile === 'system.sql' || $sFile === 'addon.sql' || $sFile === 'mariadb.sql') {
  322. continue;
  323. }
  324. $sFilePath = $sSqlDir . $sFile;
  325. if (is_file($sFilePath)) {
  326. $mixedRes = $oDb->executeSQL($sFilePath, $this->getMarkersForDb($a, $oDb));
  327. if (true !== $mixedRes) {
  328. return $this->dbErrors2ErrorFields($mixedRes);
  329. }
  330. }
  331. }
  332. }
  333. return array();
  334. }
  335. /**
  336. * mariadb.sql uses UUID and VECTOR, available on MariaDB 11.7 and newer.
  337. */
  338. protected function shouldRunMariaDbSql($oDb)
  339. {
  340. if (!class_exists('BxDolAIVectorStoreMariaDb', false))
  341. require_once(BX_INSTALL_DIR . '../inc/classes/BxDolAIVectorStoreMariaDb.php');
  342. $sVersion = (string)$oDb->getOne('SELECT VERSION()');
  343. $aServer = BxDolAIVectorStoreMariaDb::describe($sVersion);
  344. return ($aServer['engine'] ?? '') === 'mariadb' && !empty($aServer['supported']);
  345. }
  346. public function processConfigDataHeader ($a)
  347. {
  348. $aMarkers = $this->getMarkersForPhp($a);
  349. $sFile = $this->_sPatternHeader;
  350. $sHeader = file_get_contents($sFile);
  351. if (false === $sHeader)
  352. return array(BX_INSTALL_ERR_GENERAL => _t('_sys_inst_msg_file_read_failed', $sFile));
  353. $sHeader = str_replace(array_keys($aMarkers), array_values($aMarkers), $sHeader);
  354. if (false === file_put_contents(BX_INSTALL_PATH_HEADER, $sHeader))
  355. return array(BX_INSTALL_ERR_GENERAL => _t('_sys_inst_msg_file_write_failed', BX_INSTALL_PATH_HEADER));
  356. @chmod(BX_INSTALL_PATH_HEADER, 0666);
  357. if (function_exists('opcache_invalidate')) opcache_invalidate(BX_INSTALL_PATH_HEADER);
  358. return array();
  359. }
  360. public function processModules ($a)
  361. {
  362. require_once(BX_INSTALL_PATH_HEADER);
  363. require_once(BX_DIRECTORY_PATH_INC . "design.inc.php");
  364. $aTypes = array (BX_DOL_MODULE_TYPE_LANGUAGE, BX_DOL_MODULE_TYPE_TEMPLATE, BX_DOL_MODULE_TYPE_MODULE);
  365. foreach ($aTypes as $sModuleType) {
  366. if (empty($a[$sModuleType]))
  367. continue;
  368. $sErrorMessage = $this->processModuleByUri ($a[$sModuleType], array ('install', 'enable'), $sModuleType);
  369. if ($sErrorMessage)
  370. return array(BX_INSTALL_ERR_GENERAL => $sErrorMessage);
  371. }
  372. bx_import('BxDolAccount');
  373. $oAccount = BxDolAccount::getInstance($a['admin_email']);
  374. if (!$oAccount)
  375. return array(BX_INSTALL_ERR_GENERAL => _t('_sys_inst_msg_admin_account_not_found', $a['admin_email']));
  376. if ($this->_isAutologin)
  377. bx_login($oAccount->id());
  378. return array();
  379. }
  380. public function processModuleByUri ($sModuleUri, $aActions = array ('install', 'enable'), $sModuleType = null)
  381. {
  382. return $this->processModuleByField('home_uri', $sModuleUri, $aActions, $sModuleType);
  383. }
  384. public function processModuleByName ($sModuleName, $aActions = array ('install', 'enable'), $sModuleType = null)
  385. {
  386. return $this->processModuleByField('name', $sModuleName, $aActions, $sModuleType);
  387. }
  388. protected function processModuleByField ($sField, $sModuleUri, $aActions = array ('install', 'enable'), $sModuleType = null)
  389. {
  390. if (!file_exists(BX_INSTALL_PATH_HEADER))
  391. return _t('_sys_inst_msg_script_isnt_installed');
  392. bx_import('BxDolStudioInstallerUtils');
  393. bx_import('BxDolLanguages');
  394. BxDolLanguages::getInstance();
  395. $oModulesTools = new BxDolInstallModulesTools();
  396. $aModules = $oModulesTools->getModules($sModuleType);
  397. foreach ($aModules as $aConfig) {
  398. if ($sModuleUri != $aConfig[$sField])
  399. continue;
  400. foreach ($aActions as $sAction) {
  401. $aResult = BxDolStudioInstallerUtils::getInstance()->perform($aConfig['home_dir'], $sAction);
  402. if ((!isset($aResult['code']) || $aResult['code']) && !empty($aResult['message']))
  403. return _t('_sys_inst_msg_module_error', $aConfig['title'], $aResult['message']);
  404. }
  405. }
  406. return '';
  407. }
  408. protected function dbErrors2ErrorFields ($a)
  409. {
  410. $s = '';
  411. foreach ($a as $r)
  412. $s = $r['error'] . ': <br />' . $r['query'] . '<br />';
  413. return array(BX_INSTALL_ERR_GENERAL => $s);
  414. }
  415. protected function processInputData ($a)
  416. {
  417. foreach ($a as $sKey => $mixedValue)
  418. $a[$sKey] = bx_process_input($mixedValue);
  419. return $a;
  420. }
  421. protected function getMarkers($a)
  422. {
  423. $aMarkers = array();
  424. foreach($this->_aConfig as $sKey => $r)
  425. $aMarkers[$sKey] = isset($a[$sKey]) ? $a[$sKey] : '';
  426. $aMarkers['admin_pwd_salt'] = genRndPwd();
  427. $aMarkers['admin_pwd_hash'] = encryptUserPwd($a['admin_password'], $aMarkers['admin_pwd_salt']);
  428. $aMarkers['current_timestamp'] = time();
  429. $aMarkers['version'] = BX_DOL_VER;
  430. $aMarkers['time'] = time();
  431. $aMarkers['secret'] = genRndPwd(11);
  432. $aMarkers['ffmpeg_path'] = $aMarkers['root_dir'] . 'plugins/ffmpeg/ffmpeg.exe';
  433. $aJava = explode(' ', str_replace('java: ', '', trim(`whereis java`)));
  434. $aMarkers['java_path'] = isset($aJava[0]) && $aJava[0] && !is_dir($aJava[0]) ? $aJava[0] : '';
  435. return $aMarkers;
  436. }
  437. protected function getMarkersForDb($a, $oDb)
  438. {
  439. $a = $this->getMarkers($a);
  440. $aMarkers = array();
  441. foreach ($a as $sKey => $mixedVal) {
  442. $aMarkers['from'][] = '{' . $sKey . '}';
  443. $aMarkers['to'][] = $oDb->escape($mixedVal);
  444. }
  445. return $aMarkers;
  446. }
  447. protected function getMarkersForPhp($a)
  448. {
  449. $a = $this->getMarkers($a);
  450. $aMarkers = array();
  451. foreach ($a as $sKey => $mixedVal)
  452. $aMarkers['%' . strtoupper($sKey) . '%'] = bx_php_string_apos($mixedVal);
  453. return $aMarkers;
  454. }
  455. protected function getFormFields($aErrorFields, $aData)
  456. {
  457. $s = '';
  458. foreach($this->_aConfig as $sKey => $a) {
  459. $sFunc = isset($a['func']) ? $a['func'] : 'rowInput';
  460. $s .= $this->$sFunc($aData, $sKey, $a, isset($aErrorFields[$sKey]) ? $aErrorFields[$sKey] : false);
  461. }
  462. return $s;
  463. }
  464. protected function rowInput ($aData, $sKey, $a, $isError = false)
  465. {
  466. $sAutoMessage = '';
  467. $sValue = bx_html_attribute($this->def ($aData, $sKey, $a, $sAutoMessage));
  468. $sInput = '<input type="text" name="' . $sKey. '" value="' . $sValue . '" class="bx-def-font-inputs bx-form-input-text" />';
  469. return $this->rowWrapper ($aData, $sInput, $sAutoMessage, 'text', $sKey, $a, $isError);
  470. }
  471. protected function rowSelect ($aData, $sKey, $a, $isError = false)
  472. {
  473. $sAutoMessage = '';
  474. $sValue = bx_html_attribute($this->def ($aData, $sKey, $a, $sAutoMessage));
  475. $sValues = '';
  476. foreach ($a['vals'] as $sVal => $sTitle)
  477. $sValues .= '<option value="' . $sVal . '" ' . ($sVal == $sValue ? 'selected="selected"' : '') . '>' . $sTitle . '</option>';
  478. $sInput = '<select name="' . $sKey . '" class="bx-def-font-inputs bx-form-input-select">' . $sValues . '</select>';
  479. return $this->rowWrapper ($aData, $sInput, $sAutoMessage, 'select', $sKey, $a, $isError);
  480. }
  481. protected function rowWrapper ($aData, $sInput, $sAutoMessage, $sType, $sKey, $a, $isError = false)
  482. {
  483. $sDesc = _t('_sys_inst_conf_desc', $sAutoMessage, $a['desc'], isset($a['ex']) ? $a['ex'] : _t('_sys_inst_conf_no_example'));
  484. $sError = '';
  485. if ($isError)
  486. $sError = '<div class="bx-form-warn">' . _t('_sys_inst_conf_error') . '</div>';
  487. $sRequired = '';
  488. if (isset($a['check']) && $a['check'])
  489. $sRequired = '<span class="bx-form-required">*</span>';
  490. return <<<EOF
  491. <div class="bx-form-element-wrapper bx-def-margin-top-auto">
  492. <div class="bx-form-caption">
  493. {$a['name']}
  494. {$sRequired}
  495. </div>
  496. <div class="bx-form-value">
  497. <div class="bx-form-input-wrapper bx-form-input-wrapper-{$sType}">
  498. $sInput
  499. </div>
  500. $sError
  501. <div class="bx-form-info bx-def-font-grayed bx-def-font-small">
  502. {$sDesc}
  503. </div>
  504. </div></div>
  505. EOF;
  506. }
  507. protected function rowSectionOpen ($aData, $sKey, $a)
  508. {
  509. return <<<EOF
  510. <div class="bx-form-section-wrapper bx-def-margin-top">
  511. <fieldset class="bx-form-section bx-def-padding-sec-top bx-def-border-top bx-form-section-header">
  512. <legend class="bx-def-padding-sec-right bx-def-font-grayed bx-def-font-h3">{$a['name']}</legend>
  513. <div class="bx-form-section-content bx-def-padding-top bx-def-padding-bottom">
  514. EOF;
  515. }
  516. protected function rowSectionClose ($aData, $sKey, $a)
  517. {
  518. return <<<EOF
  519. </div>
  520. </fieldset>
  521. </div>
  522. EOF;
  523. }
  524. protected function check ($sKey, $sValue, $a)
  525. {
  526. if (empty($a['check']))
  527. return true;
  528. return $this->{$a['check'][0]}($sValue, $a['check'][1]);
  529. }
  530. protected function checkLength ($s, $i)
  531. {
  532. return mb_strlen($s) >= $i ? true : false;
  533. }
  534. protected function checkLengthExactOrEmpty ($s, $i)
  535. {
  536. return empty($s) || mb_strlen($s) == $i;
  537. }
  538. protected function checkEmail ($s, $i)
  539. {
  540. return mb_strlen($s) > $i && false !== strpos($s, '@') ? true : false;
  541. }
  542. protected function def ($aData, $sKey, $a, &$sAutoMessage)
  543. {
  544. if (isset($aData[$sKey]))
  545. return bx_process_pass($aData[$sKey]);
  546. elseif (false !== getenv('UNA_' . strtoupper($sKey)))
  547. return bx_process_pass(getenv('UNA_' . strtoupper($sKey)));
  548. elseif (!empty($a['def_exp'])) {
  549. $s = $this->{$a['def_exp'][0]}($a['def_exp'][1]);
  550. if ($s) {
  551. $sAutoMessage = _t('_sys_inst_conf_found') . '<br />';
  552. return $s;
  553. } else {
  554. $sAutoMessage = _t('_sys_inst_conf_not_found') . '<br />';
  555. }
  556. }
  557. return isset($a['def']) ? $a['def'] : '';
  558. }
  559. protected function defUrl ($foo)
  560. {
  561. $s = $this->proto() . $this->_sServerHttpHost . $this->_sServerPhpSelf;
  562. return preg_replace("/install\/(index\.php$)/", '', $s);
  563. }
  564. protected function defPath ($foo)
  565. {
  566. $s = rtrim($this->_sServerDocumentRoot, '/') . $this->_sServerPhpSelf;
  567. return preg_replace("/install\/(index\.php$)/", '', $s);
  568. }
  569. protected function getSelectValues($sType)
  570. {
  571. $a = array();
  572. $oModulesTools = new BxDolInstallModulesTools();
  573. $aModules = $oModulesTools->getModules($sType);
  574. foreach ($aModules as $sName => $aConfig)
  575. $a[$aConfig['home_uri']] = $aConfig['title'];
  576. return $a;
  577. }
  578. protected function proto()
  579. {
  580. $sProto = 'http://';
  581. if (
  582. (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) ||
  583. (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' == strtolower($_SERVER['HTTP_X_FORWARDED_PROTO'])) ||
  584. getenv('UNA_HTTPS')
  585. )
  586. $sProto = 'https://';
  587. return $sProto;
  588. }
  589. }
  590. /** @} */