aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/comments-ajax.php
blob: 1f987a92cb49df85d2a6d233786272c56c7a4eed (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?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/>.


// Check if request is for JSONP
if (isset ($_GET['jsonp'])) {
	// If so, setup HashOver for JavaScript
	require ('javascript-setup.php');
} else {
	// If not, setup HashOver for JSON
	require ('json-setup.php');
}

try {
	// Instantiate HashOver class
	$hashover = new \HashOver ('json');
	$hashover->setup->setPageURL ('request');
	$hashover->setup->setPageTitle ('request');
	$hashover->setup->setThreadName ('request');
	$hashover->initiate ();
	$hashover->parsePrimary ();
	$hashover->parsePopular ();
	$hashover->finalize ();

	// Page, setup, and comment data array
	$data = array ();

	// Check if we're preparing HashOver
	if ($hashover->setup->getRequest ('prepare') !== false) {
		// Set/update default page metadata
		$hashover->defaultMetadata ();

		// Add locales to data
		$data['locale'] = array (
			'cancel'		=> $hashover->locale->text['cancel'],
			'date-time'		=> $hashover->locale->text['date-time'],
			'dislike-comment'	=> $hashover->locale->text['dislike-comment'],
			'disliked-comment'	=> $hashover->locale->text['disliked-comment'],
			'disliked'		=> $hashover->locale->text['disliked'],
			'dislike'		=> $hashover->locale->text['dislike'],
			'external-image-tip'	=> $hashover->locale->text['external-image-tip'],
			'field-needed'		=> $hashover->locale->text['field-needed'],
			'like-comment'		=> $hashover->locale->text['like-comment'],
			'liked-comment'		=> $hashover->locale->text['liked-comment'],
			'liked'			=> $hashover->locale->text['liked'],
			'like'			=> $hashover->locale->text['like'],
			'today'			=> $hashover->locale->text['date-today'],
			'unlike'		=> $hashover->locale->text['unlike'],
			'commenter-tip'		=> $hashover->locale->text['commenter-tip'],
			'subscribed-tip'	=> $hashover->locale->text['subscribed-tip'],
			'unsubscribed-tip'	=> $hashover->locale->text['unsubscribed-tip'],
			'replies'		=> $hashover->locale->text['replies'],
			'reply'			=> $hashover->locale->text['reply'],
			'no-email-warning'	=> $hashover->locale->text['no-email-warning'],
			'invalid-email'		=> $hashover->locale->text['invalid-email'],
			'reply-needed'		=> $hashover->locale->text['reply-needed'],
			'comment-needed'	=> $hashover->locale->text['comment-needed'],
			'delete-comment'	=> $hashover->locale->text['delete-comment'],
			'loading'		=> $hashover->locale->text['loading'],
			'click-to-close'	=> $hashover->locale->text['click-to-close'],
			'email'			=> $hashover->locale->text['email'],
			'name'			=> $hashover->locale->text['name'],
			'password'		=> $hashover->locale->text['password'],
			'website'		=> $hashover->locale->text['website'],
			'day-names'		=> $hashover->locale->text['date-day-names'],
			'month-names'		=> $hashover->locale->text['date-month-names']
		);

		// Add setup information to data
		$data['setup'] = array (
			'server-eol'		=> PHP_EOL,
			'collapse-limit'	=> $hashover->setup->collapseLimit,
			'default-name'		=> $hashover->setup->defaultName,
			'user-is-logged-in'	=> $hashover->login->userIsLoggedIn,
			'user-is-admin'		=> $hashover->login->userIsAdmin,
			'http-root'		=> $hashover->setup->httpRoot,
			'http-backend'		=> $hashover->setup->httpBackend,
			'allows-dislikes'	=> $hashover->setup->allowsDislikes,
			'allows-likes'		=> $hashover->setup->allowsLikes,
			'time-format'		=> $hashover->setup->timeFormat,
			'image-extensions'	=> $hashover->setup->imageTypes,
			'image-placeholder'	=> $hashover->setup->getImagePath ('place-holder'),
			'stream-mode'		=> ($hashover->setup->replyMode === 'stream'),
			'stream-depth'		=> $hashover->setup->streamDepth,
			'theme-css'		=> $hashover->setup->getThemePath ('comments.css'),
			'device-type'		=> ($hashover->setup->isMobile === true) ? 'mobile' : 'desktop',
			'collapses-interface'	=> $hashover->setup->collapsesInterface,
			'collapses-comments'	=> $hashover->setup->collapsesComments,
			'uses-user-timezone'	=> $hashover->setup->usesUserTimezone,
			'uses-short-dates'	=> $hashover->setup->usesShortDates,
			'allows-images'		=> $hashover->setup->allowsImages,
			'uses-markdown'		=> $hashover->setup->usesMarkdown,
			'uses-cancel-buttons'	=> $hashover->setup->usesCancelButtons,
			'uses-auto-login'	=> $hashover->setup->usesAutoLogin,
			'uses-ajax'		=> $hashover->setup->usesAjax,
			'allows-login'		=> $hashover->setup->allowsLogin,
			'field-options'		=> $hashover->setup->fieldOptions
		);

		// Add UI HTML to data
		$data['ui'] = array (
			'user-avatar'		=> $hashover->ui->userAvatar (),
			'name-link'		=> $hashover->ui->nameElement ('a'),
			'name-span'		=> $hashover->ui->nameElement ('span'),
			'parent-link'		=> $hashover->ui->parentThreadLink (),
			'edit-link'		=> $hashover->ui->formLink ('{{href}}', 'edit'),
			'reply-link'		=> $hashover->ui->formLink ('{{href}}', 'reply'),
			'like-link'		=> $hashover->ui->likeLink ('like'),
			'dislike-link'		=> $hashover->ui->likeLink ('dislike'),
			'like-count'		=> $hashover->ui->likeCount ('likes'),
			'dislike-count'		=> $hashover->ui->likeCount ('dislikes'),
			'name-wrapper'		=> $hashover->ui->nameWrapper (),
			'date-link'		=> $hashover->ui->dateLink (),
			'comment-wrapper'	=> $hashover->ui->commentWrapper (),
			'theme'			=> $hashover->templater->parseTheme ('comments.html'),
			'reply-form'		=> $hashover->ui->replyForm (),
			'edit-form'		=> $hashover->ui->editForm ()
		);
	}

	// HashOver instance information
	$data['instance'] = array (
		'primary-count'		=> $hashover->thread->primaryCount - 1,
		'total-count'		=> $hashover->thread->totalCount - 1,
		'page-url'		=> $hashover->setup->pageURL,
		'page-title'		=> $hashover->setup->pageTitle,
		'thread-name'		=> $hashover->setup->threadName,
		'file-path'		=> $hashover->setup->filePath,
		'initial-html'		=> $hashover->ui->initialHTML (false),
		'comments'		=> $hashover->comments
	);

	// Count according to `$showsReplyCount` setting
	$show_number_comments = $hashover->getCommentCount ('show-number-comments');

	// Add locales for UI uncollapse button
	if ($hashover->setup->collapsesInterface !== false) {
		$data['instance']['show-number-comments'] = $show_number_comments;
		$data['instance']['post-comment-on'] = $hashover->ui->postCommentOn;
	}

	// Text for "Show X Other Comment(s)" link
	if ($hashover->setup->collapsesComments !== false) {
		// Check if at least 1 comment is to be shown
		if ($hashover->setup->collapseLimit >= 1) {
			// If so, use the "Show X Other Comments" locale
			$more_link_locale = $hashover->locale->text['show-other-comments'];

			// Shorter variables
			$total_count = $hashover->thread->totalCount;
			$collapse_limit = $hashover->setup->collapseLimit;

			// Get number of comments after collapse limit
			$other_count = ($total_count - 1) - $collapse_limit;

			// Subtract deleted comment counts
			if ($hashover->setup->countIncludesDeleted === false) {
				$other_count -= $hashover->thread->collapsedDeletedCount;
			}

			// Decide if count is pluralized
			$more_link_plural = ($other_count !== 1) ? 1 : 0;
			$more_link_text = $more_link_locale[$more_link_plural];

			// And inject the count into the locale string
			$more_link_text = sprintf ($more_link_text, $other_count);
		} else {
			// If not, show count according to `$showsReplyCount` setting
			$more_link_text = $show_number_comments;
		}

		// Add "Show X Other Comment(s)" link to instance
		$data['instance']['more-link-text'] = $more_link_text;
	}

	// Generate statistics
	$hashover->statistics->executionEnd ();

	// HashOver statistics
	$data['statistics'] = array (
		'execution-time'	=> $hashover->statistics->executionTime,
		'script-memory'		=> $hashover->statistics->scriptMemory,
		'system-memory'		=> $hashover->statistics->systemMemory
	);

	// Return JSON or JSONP function call
	echo $hashover->misc->jsonData ($data);

} catch (\Exception $error) {
	$misc = new Misc ('json');
	$message = $error->getMessage ();
	$misc->displayError ($message);
}