You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcmail_install.php 27KB

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