Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

rcmail_install.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | rcmail_install.php |
  5. | |
  6. | This file is part of the Roundcube Webmail package |
  7. | Copyright (C) 2008-2016, The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. +-----------------------------------------------------------------------+
  13. */
  14. /**
  15. * Class to control the installation process of the Roundcube Webmail package
  16. *
  17. * @category Install
  18. * @package Roundcube
  19. * @author Thomas Bruederli
  20. */
  21. class rcmail_install
  22. {
  23. public $step;
  24. public $last_error;
  25. public $is_post = false;
  26. public $failures = 0;
  27. public $config = array();
  28. public $configured = false;
  29. public $legacy_config = false;
  30. public $email_pattern = '([a-z0-9][a-z0-9\-\.\+\_]*@[a-z0-9]([a-z0-9\-][.]?)*[a-z0-9])';
  31. public $bool_config_props = array();
  32. public $local_config = array('db_dsnw', 'default_host', 'support_url', 'des_key', 'plugins');
  33. public $obsolete_config = array('db_backend', 'db_max_length', 'double_auth');
  34. public $replaced_config = array(
  35. 'skin_path' => 'skin',
  36. 'locale_string' => 'language',
  37. 'multiple_identities' => 'identities_level',
  38. 'addrbook_show_images' => 'show_images',
  39. 'imap_root' => 'imap_ns_personal',
  40. 'pagesize' => 'mail_pagesize',
  41. 'top_posting' => 'reply_mode',
  42. 'keep_alive' => 'refresh_interval',
  43. 'min_keep_alive' => 'min_refresh_interval',
  44. );
  45. // list of supported database drivers
  46. public $supported_dbs = array(
  47. 'MySQL' => 'pdo_mysql',
  48. 'PostgreSQL' => 'pdo_pgsql',
  49. 'SQLite' => 'pdo_sqlite',
  50. 'SQLite (v2)' => 'pdo_sqlite2',
  51. 'SQL Server (SQLSRV)' => 'pdo_sqlsrv',
  52. 'SQL Server (DBLIB)' => 'pdo_dblib',
  53. 'Oracle' => 'oci8',
  54. );
  55. /**
  56. * Constructor
  57. */
  58. public function __construct()
  59. {
  60. $this->step = intval($_REQUEST['_step']);
  61. $this->is_post = $_SERVER['REQUEST_METHOD'] == 'POST';
  62. }
  63. /**
  64. * Singleton getter
  65. */
  66. public static function get_instance()
  67. {
  68. static $inst;
  69. if (!$inst) {
  70. $inst = new rcmail_install();
  71. }
  72. return $inst;
  73. }
  74. /**
  75. * Read the local config files and store properties
  76. */
  77. public function load_config()
  78. {
  79. // defaults
  80. if ($config = $this->load_config_file(RCUBE_CONFIG_DIR . 'defaults.inc.php')) {
  81. $this->config = (array) $config;
  82. $this->defaults = $this->config;
  83. }
  84. $config = null;
  85. // config
  86. if ($config = $this->load_config_file(RCUBE_CONFIG_DIR . 'config.inc.php')) {
  87. $this->config = array_merge($this->config, $config);
  88. }
  89. else {
  90. if ($config = $this->load_config_file(RCUBE_CONFIG_DIR . 'main.inc.php')) {
  91. $this->config = array_merge($this->config, $config);
  92. $this->legacy_config = true;
  93. }
  94. if ($config = $this->load_config_file(RCUBE_CONFIG_DIR . 'db.inc.php')) {
  95. $this->config = array_merge($this->config, $config);
  96. $this->legacy_config = true;
  97. }
  98. }
  99. $this->configured = !empty($config);
  100. }
  101. /**
  102. * Read the default config file and store properties
  103. */
  104. public function load_config_file($file)
  105. {
  106. if (!is_readable($file)) {
  107. return;
  108. }
  109. include $file;
  110. // read comments from config file
  111. if (function_exists('token_get_all')) {
  112. $tokens = token_get_all(file_get_contents($file));
  113. $in_config = false;
  114. $buffer = '';
  115. for ($i = 0; $i < count($tokens); $i++) {
  116. $token = $tokens[$i];
  117. if ($token[0] == T_VARIABLE && ($token[1] == '$config' || $token[1] == '$rcmail_config')) {
  118. $in_config = true;
  119. if ($buffer && $tokens[$i+1] == '[' && $tokens[$i+2][0] == T_CONSTANT_ENCAPSED_STRING) {
  120. $propname = trim($tokens[$i+2][1], "'\"");
  121. $this->comments[$propname] = $buffer;
  122. $buffer = '';
  123. $i += 3;
  124. }
  125. }
  126. else if ($in_config && $token[0] == T_COMMENT) {
  127. $buffer .= strtr($token[1], array('\n' => "\n"));
  128. }
  129. }
  130. }
  131. // deprecated name of config variable
  132. if (is_array($rcmail_config)) {
  133. return $rcmail_config;
  134. }
  135. return $config;
  136. }
  137. /**
  138. * Getter for a certain config property
  139. *
  140. * @param string Property name
  141. * @param string Default value
  142. *
  143. * @return string The property value
  144. */
  145. public function getprop($name, $default = '')
  146. {
  147. $value = $this->config[$name];
  148. if ($name == 'des_key' && !$this->configured && !isset($_REQUEST["_$name"])) {
  149. $value = rcube_utils::random_bytes(24);
  150. }
  151. return $value !== null && $value !== '' ? $value : $default;
  152. }
  153. /**
  154. * Create configuration file that contains parameters
  155. * that differ from default values.
  156. *
  157. * @return string The complete config file content
  158. */
  159. public function create_config()
  160. {
  161. $config = array();
  162. foreach ($this->config as $prop => $default) {
  163. $is_default = !isset($_POST["_$prop"]);
  164. $value = !$is_default || $this->bool_config_props[$prop] ? $_POST["_$prop"] : $default;
  165. // always disable installer
  166. if ($prop == 'enable_installer') {
  167. $value = false;
  168. }
  169. // reset useragent to default (keeps version up-to-date)
  170. if ($prop == 'useragent' && stripos($value, 'Roundcube Webmail/') !== false) {
  171. $value = $this->defaults[$prop];
  172. }
  173. // generate new encryption key, never use the default value
  174. if ($prop == 'des_key' && $value == $this->defaults[$prop]) {
  175. $value = rcube_utils::random_bytes(24);
  176. }
  177. // convert some form data
  178. if ($prop == 'debug_level' && !$is_default) {
  179. if (is_array($value)) {
  180. $val = 0;
  181. foreach ($value as $dbgval) {
  182. $val += intval($dbgval);
  183. }
  184. $value = $val;
  185. }
  186. }
  187. else if ($prop == 'db_dsnw' && !empty($_POST['_dbtype'])) {
  188. if ($_POST['_dbtype'] == 'sqlite') {
  189. $value = sprintf('%s://%s?mode=0646', $_POST['_dbtype'],
  190. $_POST['_dbname']{0} == '/' ? '/' . $_POST['_dbname'] : $_POST['_dbname']);
  191. }
  192. else if ($_POST['_dbtype']) {
  193. $value = sprintf('%s://%s:%s@%s/%s', $_POST['_dbtype'],
  194. rawurlencode($_POST['_dbuser']), rawurlencode($_POST['_dbpass']), $_POST['_dbhost'], $_POST['_dbname']);
  195. }
  196. }
  197. else if ($prop == 'smtp_auth_type' && $value == '0') {
  198. $value = '';
  199. }
  200. else if ($prop == 'default_host' && is_array($value)) {
  201. $value = self::_clean_array($value);
  202. if (count($value) <= 1) {
  203. $value = $value[0];
  204. }
  205. }
  206. else if ($prop == 'mail_pagesize' || $prop == 'addressbook_pagesize') {
  207. $value = max(2, intval($value));
  208. }
  209. else if ($prop == 'smtp_user' && !empty($_POST['_smtp_user_u'])) {
  210. $value = '%u';
  211. }
  212. else if ($prop == 'smtp_pass' && !empty($_POST['_smtp_user_u'])) {
  213. $value = '%p';
  214. }
  215. else if (is_bool($default)) {
  216. $value = (bool) $value;
  217. }
  218. else if (is_numeric($value)) {
  219. $value = intval($value);
  220. }
  221. else if ($prop == 'plugins' && !empty($_POST['submit'])) {
  222. $value = array();
  223. foreach (array_keys($_POST) as $key) {
  224. if (preg_match('/^_plugins_*/', $key)) {
  225. array_push($value, $_POST[$key]);
  226. }
  227. }
  228. }
  229. // skip this property
  230. if ($value == $this->defaults[$prop]
  231. && (!in_array($prop, $this->local_config)
  232. || in_array($prop, array_merge($this->obsolete_config, array_keys($this->replaced_config)))
  233. || preg_match('/^db_(table|sequence)_/', $prop)
  234. )
  235. ) {
  236. continue;
  237. }
  238. // save change
  239. $this->config[$prop] = $value;
  240. $config[$prop] = $value;
  241. }
  242. $out = "<?php\n\n";
  243. $out .= "/* Local configuration for Roundcube Webmail */\n\n";
  244. foreach ($config as $prop => $value) {
  245. // copy option descriptions from existing config or defaults.inc.php
  246. $out .= $this->comments[$prop];
  247. $out .= "\$config['$prop'] = " . self::_dump_var($value, $prop) . ";\n\n";
  248. }
  249. return $out;
  250. }
  251. /**
  252. * save generated config file in RCUBE_CONFIG_DIR
  253. *
  254. * @return boolean True if the file was saved successfully, false if not
  255. */
  256. public function save_configfile($config)
  257. {
  258. if (is_writable(RCUBE_CONFIG_DIR)) {
  259. return file_put_contents(RCUBE_CONFIG_DIR . 'config.inc.php', $config);
  260. }
  261. return false;
  262. }
  263. /**
  264. * Check the current configuration for missing properties
  265. * and deprecated or obsolete settings
  266. *
  267. * @return array List with problems detected
  268. */
  269. public function check_config()
  270. {
  271. $this->load_config();
  272. if (!$this->configured) {
  273. return;
  274. }
  275. $out = $seen = array();
  276. // iterate over the current configuration
  277. foreach (array_keys($this->config) as $prop) {
  278. if ($replacement = $this->replaced_config[$prop]) {
  279. $out['replaced'][] = array('prop' => $prop, 'replacement' => $replacement);
  280. $seen[$replacement] = true;
  281. }
  282. else if (!$seen[$prop] && in_array($prop, $this->obsolete_config)) {
  283. $out['obsolete'][] = array('prop' => $prop);
  284. $seen[$prop] = true;
  285. }
  286. }
  287. // the old default mime_magic reference is obsolete
  288. if ($this->config['mime_magic'] == '/usr/share/misc/magic') {
  289. $out['obsolete'][] = array(
  290. 'prop' => 'mime_magic',
  291. 'explain' => "Set value to null in order to use system default"
  292. );
  293. }
  294. // check config dependencies and contradictions
  295. if ($this->config['enable_spellcheck'] && $this->config['spellcheck_engine'] == 'pspell') {
  296. if (!extension_loaded('pspell')) {
  297. $out['dependencies'][] = array(
  298. 'prop' => 'spellcheck_engine',
  299. 'explain' => "This requires the <tt>pspell</tt> extension which could not be loaded."
  300. );
  301. }
  302. else if (!empty($this->config['spellcheck_languages'])) {
  303. foreach ($this->config['spellcheck_languages'] as $lang => $descr) {
  304. if (!@pspell_new($lang)) {
  305. $out['dependencies'][] = array(
  306. 'prop' => 'spellcheck_languages',
  307. 'explain' => "You are missing pspell support for language $lang ($descr)"
  308. );
  309. }
  310. }
  311. }
  312. }
  313. if ($this->config['log_driver'] == 'syslog') {
  314. if (!function_exists('openlog')) {
  315. $out['dependencies'][] = array(
  316. 'prop' => 'log_driver',
  317. 'explain' => "This requires the <tt>syslog</tt> extension which could not be loaded."
  318. );
  319. }
  320. if (empty($this->config['syslog_id'])) {
  321. $out['dependencies'][] = array(
  322. 'prop' => 'syslog_id',
  323. 'explain' => "Using <tt>syslog</tt> for logging requires a syslog ID to be configured"
  324. );
  325. }
  326. }
  327. // check ldap_public sources having global_search enabled
  328. if (is_array($this->config['ldap_public']) && !is_array($this->config['autocomplete_addressbooks'])) {
  329. foreach ($this->config['ldap_public'] as $ldap_public) {
  330. if ($ldap_public['global_search']) {
  331. $out['replaced'][] = array(
  332. 'prop' => 'ldap_public::global_search',
  333. 'replacement' => 'autocomplete_addressbooks'
  334. );
  335. break;
  336. }
  337. }
  338. }
  339. return $out;
  340. }
  341. /**
  342. * Merge the current configuration with the defaults
  343. * and copy replaced values to the new options.
  344. */
  345. public function merge_config()
  346. {
  347. $current = $this->config;
  348. $this->config = array();
  349. foreach ($this->replaced_config as $prop => $replacement) {
  350. if (isset($current[$prop])) {
  351. if ($prop == 'skin_path') {
  352. $this->config[$replacement] = preg_replace('#skins/(\w+)/?$#', '\\1', $current[$prop]);
  353. }
  354. else if ($prop == 'multiple_identities') {
  355. $this->config[$replacement] = $current[$prop] ? 2 : 0;
  356. }
  357. else {
  358. $this->config[$replacement] = $current[$prop];
  359. }
  360. }
  361. unset($current[$prop]);
  362. }
  363. foreach ($this->obsolete_config as $prop) {
  364. unset($current[$prop]);
  365. }
  366. // add all ldap_public sources having global_search enabled to autocomplete_addressbooks
  367. if (is_array($current['ldap_public'])) {
  368. foreach ($current['ldap_public'] as $key => $ldap_public) {
  369. if ($ldap_public['global_search']) {
  370. $this->config['autocomplete_addressbooks'][] = $key;
  371. unset($current['ldap_public'][$key]['global_search']);
  372. }
  373. }
  374. }
  375. $this->config = array_merge($this->config, $current);
  376. foreach (array_keys((array) $current['ldap_public']) as $key) {
  377. $this->config['ldap_public'][$key] = $current['ldap_public'][$key];
  378. }
  379. }
  380. /**
  381. * Compare the local database schema with the reference schema
  382. * required for this version of Roundcube
  383. *
  384. * @param rcube_db Database object
  385. *
  386. * @return boolean True if the schema is up-to-date, false if not or an error occurred
  387. */
  388. public function db_schema_check($DB)
  389. {
  390. if (!$this->configured) {
  391. return false;
  392. }
  393. // read reference schema from mysql.initial.sql
  394. $db_schema = $this->db_read_schema(INSTALL_PATH . 'SQL/mysql.initial.sql');
  395. $errors = array();
  396. // check list of tables
  397. $existing_tables = $DB->list_tables();
  398. foreach ($db_schema as $table => $cols) {
  399. $table = $this->config['db_prefix'] . $table;
  400. if (!in_array($table, $existing_tables)) {
  401. $errors[] = "Missing table '".$table."'";
  402. }
  403. else { // compare cols
  404. $db_cols = $DB->list_cols($table);
  405. $diff = array_diff(array_keys($cols), $db_cols);
  406. if (!empty($diff)) {
  407. $errors[] = "Missing columns in table '$table': " . join(',', $diff);
  408. }
  409. }
  410. }
  411. return !empty($errors) ? $errors : false;
  412. }
  413. /**
  414. * Utility function to read database schema from an .sql file
  415. */
  416. private function db_read_schema($schemafile)
  417. {
  418. $lines = file($schemafile);
  419. $table_block = false;
  420. $schema = array();
  421. $keywords = array('PRIMARY','KEY','INDEX','UNIQUE','CONSTRAINT','REFERENCES','FOREIGN');
  422. foreach ($lines as $line) {
  423. if (preg_match('/^\s*create table `?([a-z0-9_]+)`?/i', $line, $m)) {
  424. $table_block = $m[1];
  425. }
  426. else if ($table_block && preg_match('/^\s*`?([a-z0-9_-]+)`?\s+([a-z]+)/', $line, $m)) {
  427. $col = $m[1];
  428. if (!in_array(strtoupper($col), $keywords)) {
  429. $schema[$table_block][$col] = $m[2];
  430. }
  431. }
  432. }
  433. return $schema;
  434. }
  435. /**
  436. * Try to detect some file's mimetypes to test the correct behavior of fileinfo
  437. */
  438. public function check_mime_detection()
  439. {
  440. $errors = array();
  441. $files = array(
  442. 'skins/larry/images/roundcube_logo.png' => 'image/png',
  443. 'program/resources/blank.tif' => 'image/tiff',
  444. 'program/resources/blocked.gif' => 'image/gif',
  445. 'skins/larry/README' => 'text/plain',
  446. );
  447. foreach ($files as $path => $expected) {
  448. $mimetype = rcube_mime::file_content_type(INSTALL_PATH . $path, basename($path));
  449. if ($mimetype != $expected) {
  450. $errors[] = array($path, $mimetype, $expected);
  451. }
  452. }
  453. return $errors;
  454. }
  455. /**
  456. * Check the correct configuration of the 'mime_types' mapping option
  457. */
  458. public function check_mime_extensions()
  459. {
  460. $errors = array();
  461. $types = array(
  462. 'application/zip' => 'zip',
  463. 'text/css' => 'css',
  464. 'application/pdf' => 'pdf',
  465. 'image/gif' => 'gif',
  466. 'image/svg+xml' => 'svg',
  467. );
  468. foreach ($types as $mimetype => $expected) {
  469. $ext = rcube_mime::get_mime_extensions($mimetype);
  470. if (!in_array($expected, (array) $ext)) {
  471. $errors[] = array($mimetype, $ext, $expected);
  472. }
  473. }
  474. return $errors;
  475. }
  476. /**
  477. * Getter for the last error message
  478. *
  479. * @return string Error message or null if none exists
  480. */
  481. public function get_error()
  482. {
  483. return $this->last_error['message'];
  484. }
  485. /**
  486. * Return a list with all imap hosts configured
  487. *
  488. * @return array Clean list with imap hosts
  489. */
  490. public function get_hostlist()
  491. {
  492. $default_hosts = (array) $this->getprop('default_host');
  493. $out = array();
  494. foreach ($default_hosts as $key => $name) {
  495. if (!empty($name)) {
  496. $out[] = rcube_utils::parse_host(is_numeric($key) ? $name : $key);
  497. }
  498. }
  499. return $out;
  500. }
  501. /**
  502. * Create a HTML dropdown to select a previous version of Roundcube
  503. */
  504. public function versions_select($attrib = array())
  505. {
  506. $select = new html_select($attrib);
  507. $select->add(array(
  508. '0.1-stable', '0.1.1',
  509. '0.2-alpha', '0.2-beta', '0.2-stable',
  510. '0.3-stable', '0.3.1',
  511. '0.4-beta', '0.4.2',
  512. '0.5-beta', '0.5', '0.5.1', '0.5.2', '0.5.3', '0.5.4',
  513. '0.6-beta', '0.6',
  514. '0.7-beta', '0.7', '0.7.1', '0.7.2', '0.7.3', '0.7.4',
  515. '0.8-beta', '0.8-rc', '0.8.0', '0.8.1', '0.8.2', '0.8.3', '0.8.4', '0.8.5', '0.8.6',
  516. '0.9-beta', '0.9-rc', '0.9-rc2',
  517. // Note: Do not add newer versions here
  518. ));
  519. return $select;
  520. }
  521. /**
  522. * Return a list with available subfolders of the skin directory
  523. */
  524. public function list_skins()
  525. {
  526. $skins = array();
  527. $skindir = INSTALL_PATH . 'skins/';
  528. foreach (glob($skindir . '*') as $path) {
  529. if (is_dir($path) && is_readable($path)) {
  530. $skins[] = substr($path, strlen($skindir));
  531. }
  532. }
  533. return $skins;
  534. }
  535. /**
  536. * Return a list with available subfolders of the plugins directory
  537. * (with their associated description in composer.json)
  538. */
  539. public function list_plugins()
  540. {
  541. $plugins = array();
  542. $plugin_dir = INSTALL_PATH . 'plugins/';
  543. foreach (glob($plugin_dir . '*') as $path) {
  544. if (!is_dir($path)) {
  545. continue;
  546. }
  547. if (is_readable($path . '/composer.json')) {
  548. $file_json = json_decode(file_get_contents($path . '/composer.json'));
  549. $plugin_desc = $file_json->description ?: 'N/A';
  550. }
  551. else {
  552. $plugin_desc = 'N/A';
  553. }
  554. $name = substr($path, strlen($plugin_dir));
  555. $plugins[] = array(
  556. 'name' => $name,
  557. 'desc' => $plugin_desc,
  558. 'enabled' => in_array($name, (array) $this->config['plugins'])
  559. );
  560. }
  561. return $plugins;
  562. }
  563. /**
  564. * Display OK status
  565. *
  566. * @param string Test name
  567. * @param string Confirm message
  568. */
  569. public function pass($name, $message = '')
  570. {
  571. echo rcube::Q($name) . ':&nbsp; <span class="success">OK</span>';
  572. $this->_showhint($message);
  573. }
  574. /**
  575. * Display an error status and increase failure count
  576. *
  577. * @param string Test name
  578. * @param string Error message
  579. * @param string URL for details
  580. * @param bool Do not count this failure
  581. */
  582. public function fail($name, $message = '', $url = '', $optional=false)
  583. {
  584. if (!$optional) {
  585. $this->failures++;
  586. }
  587. echo rcube::Q($name) . ':&nbsp; <span class="fail">NOT OK</span>';
  588. $this->_showhint($message, $url);
  589. }
  590. /**
  591. * Display an error status for optional settings/features
  592. *
  593. * @param string Test name
  594. * @param string Error message
  595. * @param string URL for details
  596. */
  597. public function optfail($name, $message = '', $url = '')
  598. {
  599. echo rcube::Q($name) . ':&nbsp; <span class="na">NOT OK</span>';
  600. $this->_showhint($message, $url);
  601. }
  602. /**
  603. * Display warning status
  604. *
  605. * @param string Test name
  606. * @param string Warning message
  607. * @param string URL for details
  608. */
  609. public function na($name, $message = '', $url = '')
  610. {
  611. echo rcube::Q($name) . ':&nbsp; <span class="na">NOT AVAILABLE</span>';
  612. $this->_showhint($message, $url);
  613. }
  614. private function _showhint($message, $url = '')
  615. {
  616. $hint = rcube::Q($message);
  617. if ($url) {
  618. $hint .= ($hint ? '; ' : '') . 'See <a href="' . rcube::Q($url) . '" target="_blank">' . rcube::Q($url) . '</a>';
  619. }
  620. if ($hint) {
  621. echo '<span class="indent">(' . $hint . ')</span>';
  622. }
  623. }
  624. private static function _clean_array($arr)
  625. {
  626. $out = array();
  627. foreach (array_unique($arr) as $k => $val) {
  628. if (!empty($val)) {
  629. if (is_numeric($k)) {
  630. $out[] = $val;
  631. }
  632. else {
  633. $out[$k] = $val;
  634. }
  635. }
  636. }
  637. return $out;
  638. }
  639. private static function _dump_var($var, $name=null)
  640. {
  641. // special values
  642. switch ($name) {
  643. case 'syslog_facility':
  644. $list = array(32 => 'LOG_AUTH', 80 => 'LOG_AUTHPRIV', 72 => ' LOG_CRON',
  645. 24 => 'LOG_DAEMON', 0 => 'LOG_KERN', 128 => 'LOG_LOCAL0',
  646. 136 => 'LOG_LOCAL1', 144 => 'LOG_LOCAL2', 152 => 'LOG_LOCAL3',
  647. 160 => 'LOG_LOCAL4', 168 => 'LOG_LOCAL5', 176 => 'LOG_LOCAL6',
  648. 184 => 'LOG_LOCAL7', 48 => 'LOG_LPR', 16 => 'LOG_MAIL',
  649. 56 => 'LOG_NEWS', 40 => 'LOG_SYSLOG', 8 => 'LOG_USER', 64 => 'LOG_UUCP'
  650. );
  651. if ($val = $list[$var]) {
  652. return $val;
  653. }
  654. break;
  655. case 'mail_header_delimiter':
  656. $var = str_replace(array("\r", "\n"), array('\r', '\n'), $var);
  657. return '"' . $var. '"';
  658. /*
  659. // RCMAIL_VERSION is undefined here
  660. case 'useragent':
  661. if (preg_match('|^(.*)/('.preg_quote(RCMAIL_VERSION, '|').')$|i', $var, $m)) {
  662. return '"' . addcslashes($var, '"') . '/" . RCMAIL_VERSION';
  663. }
  664. break;
  665. */
  666. }
  667. if (is_array($var)) {
  668. if (empty($var)) {
  669. return 'array()';
  670. }
  671. else { // check if all keys are numeric
  672. $isnum = true;
  673. foreach (array_keys($var) as $key) {
  674. if (!is_numeric($key)) {
  675. $isnum = false;
  676. break;
  677. }
  678. }
  679. if ($isnum) {
  680. return 'array(' . join(', ', array_map(array('rcmail_install', '_dump_var'), $var)) . ')';
  681. }
  682. }
  683. }
  684. return var_export($var, true);
  685. }
  686. /**
  687. * Initialize the database with the according schema
  688. *
  689. * @param object rcube_db Database connection
  690. * @return boolen True on success, False on error
  691. */
  692. public function init_db($DB)
  693. {
  694. $engine = $DB->db_provider;
  695. // read schema file from /SQL/*
  696. $fname = INSTALL_PATH . "SQL/$engine.initial.sql";
  697. if ($sql = @file_get_contents($fname)) {
  698. $DB->set_option('table_prefix', $this->config['db_prefix']);
  699. $DB->exec_script($sql);
  700. }
  701. else {
  702. $this->fail('DB Schema', "Cannot read the schema file: $fname");
  703. return false;
  704. }
  705. if ($err = $this->get_error()) {
  706. $this->fail('DB Schema', "Error creating database schema: $err");
  707. return false;
  708. }
  709. return true;
  710. }
  711. /**
  712. * Update database schema
  713. *
  714. * @param string Version to update from
  715. *
  716. * @return boolen True on success, False on error
  717. */
  718. public function update_db($version)
  719. {
  720. return rcmail_utils::db_update(INSTALL_PATH . 'SQL',
  721. 'roundcube', $version, array('quiet' => true));
  722. }
  723. /**
  724. * Handler for Roundcube errors
  725. */
  726. public function raise_error($p)
  727. {
  728. $this->last_error = $p;
  729. }
  730. }