ConfigPhp.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Class ConfigPhp (ConfigIO implementation)
  4. *
  5. * Handle Shaarli's legacy PHP configuration file.
  6. * Note: this is only designed to support the transition to JSON configuration.
  7. */
  8. class ConfigPhp implements ConfigIO
  9. {
  10. /**
  11. * @var array List of config key without group.
  12. */
  13. public static $ROOT_KEYS = array(
  14. 'login',
  15. 'hash',
  16. 'salt',
  17. 'timezone',
  18. 'title',
  19. 'titleLink',
  20. 'redirector',
  21. 'disablesessionprotection',
  22. 'privateLinkByDefault',
  23. );
  24. /**
  25. * Map legacy config keys with the new ones.
  26. * If ConfigPhp is used, getting <newkey> will actually look for <legacykey>.
  27. * The Updater will use this array to transform keys when switching to JSON.
  28. *
  29. * @var array current key => legacy key.
  30. */
  31. public static $LEGACY_KEYS_MAPPING = array(
  32. 'credentials.login' => 'login',
  33. 'credentials.hash' => 'hash',
  34. 'credentials.salt' => 'salt',
  35. 'resource.data_dir' => 'config.DATADIR',
  36. 'resource.config' => 'config.CONFIG_FILE',
  37. 'resource.datastore' => 'config.DATASTORE',
  38. 'resource.updates' => 'config.UPDATES_FILE',
  39. 'resource.log' => 'config.LOG_FILE',
  40. 'resource.update_check' => 'config.UPDATECHECK_FILENAME',
  41. 'resource.raintpl_tpl' => 'config.RAINTPL_TPL',
  42. 'resource.raintpl_tmp' => 'config.RAINTPL_TMP',
  43. 'resource.thumbnails_cache' => 'config.CACHEDIR',
  44. 'resource.page_cache' => 'config.PAGECACHE',
  45. 'resource.ban_file' => 'config.IPBANS_FILENAME',
  46. 'security.session_protection_disabled' => 'disablesessionprotection',
  47. 'security.ban_after' => 'config.BAN_AFTER',
  48. 'security.ban_duration' => 'config.BAN_DURATION',
  49. 'general.title' => 'title',
  50. 'general.timezone' => 'timezone',
  51. 'general.header_link' => 'titleLink',
  52. 'updates.check_updates' => 'config.ENABLE_UPDATECHECK',
  53. 'updates.check_updates_branch' => 'config.UPDATECHECK_BRANCH',
  54. 'updates.check_updates_interval' => 'config.UPDATECHECK_INTERVAL',
  55. 'privacy.default_private_links' => 'privateLinkByDefault',
  56. 'feed.rss_permalinks' => 'config.ENABLE_RSS_PERMALINKS',
  57. 'general.links_per_page' => 'config.LINKS_PER_PAGE',
  58. 'thumbnail.enable_thumbnails' => 'config.ENABLE_THUMBNAILS',
  59. 'thumbnail.enable_localcache' => 'config.ENABLE_LOCALCACHE',
  60. 'general.enabled_plugins' => 'config.ENABLED_PLUGINS',
  61. 'redirector.url' => 'redirector',
  62. 'redirector.encode_url' => 'config.REDIRECTOR_URLENCODE',
  63. 'feed.show_atom' => 'config.SHOW_ATOM',
  64. 'privacy.hide_public_links' => 'config.HIDE_PUBLIC_LINKS',
  65. 'privacy.hide_timestamps' => 'config.HIDE_TIMESTAMPS',
  66. 'security.open_shaarli' => 'config.OPEN_SHAARLI',
  67. );
  68. /**
  69. * @inheritdoc
  70. */
  71. function read($filepath)
  72. {
  73. if (! file_exists($filepath) || ! is_readable($filepath)) {
  74. return array();
  75. }
  76. include $filepath;
  77. $out = array();
  78. foreach (self::$ROOT_KEYS as $key) {
  79. $out[$key] = $GLOBALS[$key];
  80. }
  81. $out['config'] = $GLOBALS['config'];
  82. $out['plugins'] = !empty($GLOBALS['plugins']) ? $GLOBALS['plugins'] : array();
  83. return $out;
  84. }
  85. /**
  86. * @inheritdoc
  87. */
  88. function write($filepath, $conf)
  89. {
  90. $configStr = '<?php '. PHP_EOL;
  91. foreach (self::$ROOT_KEYS as $key) {
  92. if (isset($conf[$key])) {
  93. $configStr .= '$GLOBALS[\'' . $key . '\'] = ' . var_export($conf[$key], true) . ';' . PHP_EOL;
  94. }
  95. }
  96. // Store all $conf['config']
  97. foreach ($conf['config'] as $key => $value) {
  98. $configStr .= '$GLOBALS[\'config\'][\''. $key .'\'] = '.var_export($conf['config'][$key], true).';'. PHP_EOL;
  99. }
  100. if (isset($conf['plugins'])) {
  101. foreach ($conf['plugins'] as $key => $value) {
  102. $configStr .= '$GLOBALS[\'plugins\'][\''. $key .'\'] = '.var_export($conf['plugins'][$key], true).';'. PHP_EOL;
  103. }
  104. }
  105. if (!file_put_contents($filepath, $configStr)
  106. || strcmp(file_get_contents($filepath), $configStr) != 0
  107. ) {
  108. throw new IOException(
  109. $filepath,
  110. 'Shaarli could not create the config file.
  111. Please make sure Shaarli has the right to write in the folder is it installed in.'
  112. );
  113. }
  114. }
  115. /**
  116. * @inheritdoc
  117. */
  118. function getExtension()
  119. {
  120. return '.php';
  121. }
  122. }