aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/backend/form-actions.php
blob: 5bed763debcf9a2e5e6bee652b28895882b665cf (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
<?php namespace HashOver;

// Copyright (C) 2015-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');
	$mode = 'javascript';
	$postget = $_GET;
} else {
	// If not, setup HashOver for JSON
	require ('json-setup.php');
	$mode = 'json';
	$postget = $_POST;
}

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

	// Instantiate class for writing and editing comments
	$write_comments = new WriteComments (
		$hashover->setup,
		$hashover->thread
	);

	// Various POST data actions
	$post_actions = array (
		'login',
		'logout',
		'post',
		'edit',
		'delete'
	);

	// Execute an action (write/edit/login/etc)
	foreach ($post_actions as $action) {
		if (empty ($postget[$action])) {
			continue;
		}

		switch ($action) {
			case 'login': {
				$write_comments->login ();
				break;
			}

			case 'logout': {
				$write_comments->logout ();
				break;
			}

			case 'post': {
				$data = $write_comments->postComment ();
				$hashover->defaultMetadata ();
				break;
			}

			case 'edit': {
				$data = $write_comments->editComment ();
				break;
			}

			case 'delete': {
				$write_comments->deleteComment ();
				break;
			}
		}

		break;
	}

	// Returns comment being saved as JSON
	if (isset ($postget['ajax']) and isset ($data) and is_array ($data)) {
		// Slit file into parts
		$file = $data['file'];
		$key_parts = explode ('-', $file);

		// Parsed comment data
		$comment = $data['comment'];
		$parsed = $hashover->commentParser->parse ($comment, $file, $key_parts);

		// Return JSON or JSONP function call
		echo $hashover->misc->jsonData (array (
			'comment' => $parsed,
			'count' => $hashover->getCommentCount ()
		));
	}
} catch (\Exception $error) {
	$misc = new Misc ($mode);
	$message = $error->getMessage ();
	$misc->displayError ($message);
}