ConfigJson.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Shaarli\Config;
  3. /**
  4. * Class ConfigJson (ConfigIO implementation)
  5. *
  6. * Handle Shaarli's JSON configuration file.
  7. */
  8. class ConfigJson implements ConfigIO
  9. {
  10. /**
  11. * @inheritdoc
  12. */
  13. public function read($filepath)
  14. {
  15. if (! is_readable($filepath)) {
  16. return array();
  17. }
  18. $data = file_get_contents($filepath);
  19. $data = str_replace(self::getPhpHeaders(), '', $data);
  20. $data = str_replace(self::getPhpSuffix(), '', $data);
  21. $data = json_decode($data, true);
  22. if ($data === null) {
  23. $errorCode = json_last_error();
  24. $error = 'An error occurred while parsing JSON configuration file ('. $filepath .'): error code #';
  25. $error .= $errorCode. '<br>➜ <code>' . json_last_error_msg() .'</code>';
  26. if ($errorCode === JSON_ERROR_SYNTAX) {
  27. $error .= '<br>Please check your JSON syntax (without PHP comment tags) using a JSON lint tool such as ';
  28. $error .= '<a href="http://jsonlint.com/">jsonlint.com</a>.';
  29. }
  30. throw new \Exception($error);
  31. }
  32. return $data;
  33. }
  34. /**
  35. * @inheritdoc
  36. */
  37. public function write($filepath, $conf)
  38. {
  39. // JSON_PRETTY_PRINT is available from PHP 5.4.
  40. $print = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 0;
  41. $data = self::getPhpHeaders() . json_encode($conf, $print) . self::getPhpSuffix();
  42. if (!file_put_contents($filepath, $data)) {
  43. throw new \IOException(
  44. $filepath,
  45. 'Shaarli could not create the config file.
  46. Please make sure Shaarli has the right to write in the folder is it installed in.'
  47. );
  48. }
  49. }
  50. /**
  51. * @inheritdoc
  52. */
  53. public function getExtension()
  54. {
  55. return '.json.php';
  56. }
  57. /**
  58. * The JSON data is wrapped in a PHP file for security purpose.
  59. * This way, even if the file is accessible, credentials and configuration won't be exposed.
  60. *
  61. * Note: this isn't a static field because concatenation isn't supported in field declaration before PHP 5.6.
  62. *
  63. * @return string PHP start tag and comment tag.
  64. */
  65. public static function getPhpHeaders()
  66. {
  67. return '<?php /*'. PHP_EOL;
  68. }
  69. /**
  70. * Get PHP comment closing tags.
  71. *
  72. * Static method for consistency with getPhpHeaders.
  73. *
  74. * @return string PHP comment closing.
  75. */
  76. public static function getPhpSuffix()
  77. {
  78. return PHP_EOL . '*/ ?>';
  79. }
  80. }