ConfigPhp.php 4.8 KB

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