readfiles.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php namespace HashOver;
  2. // Copyright (C) 2010-2017 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. // Read and count comments
  27. class ReadFiles
  28. {
  29. public $storageMode;
  30. public $setup;
  31. public $metadata = array ();
  32. public function __construct (Setup $setup)
  33. {
  34. $this->setup = $setup;
  35. $this->storageMode = 'flat-file';
  36. $metadata_file = $this->setup->dir . '/.metadata';
  37. $update_metadata = false;
  38. // Read exist metadata file if one exists
  39. if (file_exists ($metadata_file)) {
  40. $json_metadata = json_decode (file_get_contents ($metadata_file), true);
  41. $this->setup->metadata = array_merge ($this->setup->metadata, $json_metadata);
  42. }
  43. // Check if comment thread directory exists
  44. if ($setup->usage['context'] !== 'api') {
  45. if (file_exists (dirname ($metadata_file)) and is_writable (dirname ($metadata_file))) {
  46. // Check whether the page and metadata URLs differ
  47. if ($this->setup->metadata['title'] !== $setup->pageTitle) {
  48. $this->setup->metadata['title'] = $setup->pageTitle;
  49. $update_metadata = true;
  50. }
  51. // Check whether the page and metadata titles differ
  52. if ($this->setup->metadata['url'] !== $setup->pageURL) {
  53. $this->setup->metadata['url'] = $setup->pageURL;
  54. $update_metadata = true;
  55. }
  56. // Update metadata if the data has changed
  57. if ($update_metadata === true and
  58. $this->saveMetadata ($this->setup->metadata, $metadata_file) === false)
  59. {
  60. throw new \Exception ('Failed to create metadata file.');
  61. }
  62. }
  63. }
  64. }
  65. // Save comment metadata
  66. public function saveMetadata (array $data, $file)
  67. {
  68. if (defined ('JSON_PRETTY_PRINT')) {
  69. $json = str_replace (' ', "\t", json_encode ($data, JSON_PRETTY_PRINT));
  70. } else {
  71. $json = json_encode ($data);
  72. }
  73. return file_put_contents ($file, $json);
  74. }
  75. // Read directory contents, put filenames in array, count files
  76. public function loadFiles ($extension, array $files = array (), $auto = true)
  77. {
  78. if ($auto === true) {
  79. $files = glob ($this->setup->dir . '/*.' . $extension, GLOB_NOSORT);
  80. }
  81. if (!empty ($files)) {
  82. $comments = array ();
  83. foreach ($files as $file) {
  84. $key = basename ($file, '.' . $extension);
  85. $comments[$key] =(string) $key;
  86. }
  87. return $comments;
  88. }
  89. return false;
  90. }
  91. // Check if comment thread directory exists
  92. public function checkThread ()
  93. {
  94. if (!file_exists ($this->setup->dir)) {
  95. // If no, attempt to create the directory
  96. if (!@mkdir ($this->setup->dir, 0755, true) and !@chmod ($this->setup->dir, 0755)) {
  97. throw new \Exception ('Failed to create comment thread directory at "' . $this->setup->dir . '"');
  98. return false;
  99. }
  100. }
  101. // If yes, check if it is or can be made to be writable
  102. if (!is_writable ($this->setup->dir) and !@chmod ($this->setup->dir, 0755)) {
  103. throw new \Exception ('Comment thread directory at "' . $this->setup->dir . '" is not writable.');
  104. return false;
  105. }
  106. return true;
  107. }
  108. // Convert a string to OS-specific line endings
  109. public function osLineEndings ($string)
  110. {
  111. // Convert string to UNIX line endings
  112. $string = str_replace (array ("\r\n", "\r", "\n"), "\n", $string);
  113. // Check if OS line endings isn't UNIX-style
  114. if (PHP_EOL !== "\n") {
  115. // If so, convert string to OS-specific line endings
  116. $string = str_replace ("\n", PHP_EOL, $string);
  117. }
  118. return $string;
  119. }
  120. }