123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <?php namespace HashOver;
- // Copyright (C) 2010-2017 Jacob Barkdull
- // This file is part of HashOver.
- //
- // HashOver is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License as
- // published by the Free Software Foundation, either version 3 of the
- // License, or (at your option) any later version.
- //
- // HashOver is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
- // Display source code
- if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
- if (isset ($_GET['source'])) {
- header ('Content-type: text/plain; charset=UTF-8');
- exit (file_get_contents (basename (__FILE__)));
- } else {
- exit ('<b>HashOver</b>: This is a class file.');
- }
- }
- // Read and count comments
- class ReadFiles
- {
- public $storageMode;
- public $setup;
- public $metadata = array ();
- public function __construct (Setup $setup)
- {
- $this->setup = $setup;
- $this->storageMode = 'flat-file';
- $metadata_file = $this->setup->dir . '/.metadata';
- $update_metadata = false;
- // Read exist metadata file if one exists
- if (file_exists ($metadata_file)) {
- $json_metadata = json_decode (file_get_contents ($metadata_file), true);
- $this->setup->metadata = array_merge ($this->setup->metadata, $json_metadata);
- }
- // Check if comment thread directory exists
- if ($setup->usage['context'] !== 'api') {
- if (file_exists (dirname ($metadata_file)) and is_writable (dirname ($metadata_file))) {
- // Check whether the page and metadata URLs differ
- if ($this->setup->metadata['title'] !== $setup->pageTitle) {
- $this->setup->metadata['title'] = $setup->pageTitle;
- $update_metadata = true;
- }
- // Check whether the page and metadata titles differ
- if ($this->setup->metadata['url'] !== $setup->pageURL) {
- $this->setup->metadata['url'] = $setup->pageURL;
- $update_metadata = true;
- }
- // Update metadata if the data has changed
- if ($update_metadata === true and
- $this->saveMetadata ($this->setup->metadata, $metadata_file) === false)
- {
- throw new \Exception ('Failed to create metadata file.');
- }
- }
- }
- }
- // Save comment metadata
- public function saveMetadata (array $data, $file)
- {
- if (defined ('JSON_PRETTY_PRINT')) {
- $json = str_replace (' ', "\t", json_encode ($data, JSON_PRETTY_PRINT));
- } else {
- $json = json_encode ($data);
- }
- return file_put_contents ($file, $json);
- }
- // Read directory contents, put filenames in array, count files
- public function loadFiles ($extension, array $files = array (), $auto = true)
- {
- if ($auto === true) {
- $files = glob ($this->setup->dir . '/*.' . $extension, GLOB_NOSORT);
- }
- if (!empty ($files)) {
- $comments = array ();
- foreach ($files as $file) {
- $key = basename ($file, '.' . $extension);
- $comments[$key] =(string) $key;
- }
- return $comments;
- }
- return false;
- }
- // Check if comment thread directory exists
- public function checkThread ()
- {
- if (!file_exists ($this->setup->dir)) {
- // If no, attempt to create the directory
- if (!@mkdir ($this->setup->dir, 0755, true) and !@chmod ($this->setup->dir, 0755)) {
- throw new \Exception ('Failed to create comment thread directory at "' . $this->setup->dir . '"');
- return false;
- }
- }
- // If yes, check if it is or can be made to be writable
- if (!is_writable ($this->setup->dir) and !@chmod ($this->setup->dir, 0755)) {
- throw new \Exception ('Comment thread directory at "' . $this->setup->dir . '" is not writable.');
- return false;
- }
- return true;
- }
- // Convert a string to OS-specific line endings
- public function osLineEndings ($string)
- {
- // Convert string to UNIX line endings
- $string = str_replace (array ("\r\n", "\r", "\n"), "\n", $string);
- // Check if OS line endings isn't UNIX-style
- if (PHP_EOL !== "\n") {
- // If so, convert string to OS-specific line endings
- $string = str_replace ("\n", PHP_EOL, $string);
- }
- return $string;
- }
- }
|