aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/classes/datafiles.php
blob: 7e2eb1e1f3ea46cfc947a685bfe490098284b5dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php namespace HashOver;

// Copyright (C) 2018 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/>.


class DataFiles
{
	public $setup;

	public function __construct (Setup $setup)
	{
		$this->setup = $setup;
	}

	public function readJSON ($file)
	{
		// Read JSON comment file
		$data = @file_get_contents ($file);

		// Parse JSON comment file
		$json = @json_decode ($data, true);

		// Check for JSON parse error
		if ($json !== null) {
			return $json;
		}

		return false;
	}

	public function saveJSON ($file, array $contents = array ())
	{
		// Check if we have pretty print support
		if (defined ('JSON_PRETTY_PRINT')) {
			// If so, encode comment to JSON with pretty print
			$json = json_encode ($contents, JSON_PRETTY_PRINT);

			// And convert spaces indentation to tabs
			$json = str_replace ('    ', "\t", $json);
		} else {
			// If not, encode comment to JSON normally
			$json = json_encode ($contents);
		}

		// Convert line endings to OS specific style
		$json = $this->osLineEndings ($json);

		// Save the JSON data to the comment file
		if (@file_put_contents ($file, $json)) {
			return true;
		}

		return false;
	}

	// Gets the appropriate thread directory path
	protected function getMetaRoot ($thread)
	{
		// Using the automatically setup thread if told to
		if ($thread === 'auto') {
			return $this->setup->threadDirectory;
		}

		// Otherwise construct thread directory path
		return $this->setup->pagesDirectory . '/' . $thread;
	}

	// Gets the appropriate metadata directory path
	protected function getMetaDirectory ($thread, $global)
	{
		// Check if we're getting metadata for a specific thread
		if ($global !== true) {
			// If so, use the thread's path
			$directory = $this->getMetaRoot ($thread) . '/metadata';
		} else {
			// If not, use the global metadata path
			$directory = $this->setup->commentsDirectory . '/metadata';
		}

		// Return metadata directory path
		return $directory;
	}

	// Gets the appropriate metadata file path
	protected function getMetaPath ($name, $thread, $global)
	{
		// Metadata directory path
		$directory = $this->getMetaDirectory ($thread, $global);

		// Metadata file path
		$path = $directory . '/' . $name . '.json';

		// Return metadata file path
		return $path;
	}

	// Creates metadata directory if it doesn't exist
	protected function setupMeta ($thread, $global)
	{
		// Metadata directory path
		$metadata = $this->getMetaDirectory ($thread, $global);

		// Check if metadata root directory exists
		if (file_exists ($this->getMetaRoot ($thread))) {
			// If so, attempt to create metadata directory
			if (file_exists ($metadata) or @mkdir ($metadata, 0755, true)) {
				// If successful, set permissions to 0755 again
				@chmod ($metadata, 0755);
				return true;
			}

			// Otherwise throw exception
			throw new \Exception (sprintf (
				'Failed to create metadata directory at: "%s"!',
				$metadata
			));
		}
	}

	// Read and return specific metadata from JSON file
	public function readMeta ($name, $thread = 'auto', $global = false)
	{
		// Metadata JSON file path
		$metadata_path = $this->getMetaPath ($name, $thread, $global);

		// Return metadata if read successfully
		return $this->readJSON ($metadata_path);
	}

	// Save metadata to specific metadata JSON file
	public function saveMeta ($name, array $data, $thread = 'auto', $global = false)
	{
		// Metadata JSON file path
		$metadata_path = $this->getMetaPath ($name, $thread, $global);

		// Create metadata directory if it doesn't exist
		$this->setupMeta ($thread, $global);

		// Check if metadata root directory exists
		if (file_exists ($this->getMetaRoot ($thread))) {
			// If so, attempt to save data to metadata JSON file
			if ($this->saveJSON ($metadata_path, $data) === false) {
				// Throw exception on failure
				throw new \Exception ('Failed to save metadata!');
			}
		}

		return true;
	}

	// Convert a string to OS-specific line endings
	public function osLineEndings ($string)
	{
		// Different line ending styles
		$eol_styles = array ("\r\n", "\r", "\n");

		// Actual consersion
		$string = str_replace ($eol_styles, PHP_EOL, $string);

		return $string;
	}
}