aboutsummaryrefslogtreecommitdiff
path: root/h-source/Application/Controllers/HistoryController.php
blob: 2d965ac2eece776a5ea07dc52c53a18fdfcc3718 (plain) (blame)
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
<?php 

// h-source, a web software to build a community of people that want to share their hardware information.
// Copyright (C) 2010  Antonio Gallo (h-source-copyright.txt)
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program 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 General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

if (!defined('EG')) die('Direct access not allowed!');

class HistoryController extends BaseController
{

	protected $strings = array(
		
		'hide'	=>	array(
			
			'action' 		=>	'hide',
			'check_status'	=>	'no',
			'to_status'		=>	'yes',
			'exec_string'	=>	'The message has been hidden. Just reload the page',
			'error_string'	=>	'Error: the message is already hidden',
		
		),
		
		'show'	=>	array(
		
			'action'		=>	'show',
			'check_status'	=>	'yes',
			'to_status'		=>	'no',
			'exec_string'	=>	'The message is no more hidden. Just reload the page',
			'error_string'	=>	'Error: the message is already visible',
			
		),
		
	);
	
	protected $types = array(
	
		'message'	=>	array(
		
			'clean_type'	=>	'message',
			'model_name'	=>	'MessagesModel',
			'id_name'		=>	'id_mes',
		
		),
	
		'talk'	=>	array(
		
			'clean_type'	=>	'talk',
			'model_name'	=>	'TalkModel',
			'id_name'		=>	'id_talk',
		
		),
		
	);
		
	public function __construct($model, $controller, $queryString)
	{
		parent::__construct($model, $controller, $queryString);
		
		$this->model('HistoryModel');

	}
	
	public function hide($lang = 'en', $token = '')
	{
		$this->generic($lang, $token, 'hide');
	}
	
	public function show($lang = 'en', $token = '')
	{
		$this->generic($lang, $token, 'show');
	}
	
	protected function generic($lang = 'en', $token = '', $action = 'hide')
	{
		header('Content-type: text/html; charset=UTF-8');
		
		$this->shift(2);
		
		$this->clean();
		
		$clean['token'] = sanitizeAlphanum($token);
		
		if ($this->s['registered']->status['status'] === 'logged')
		{
			if ($this->ismoderator)
			{
				if (!$this->s['registered']->checkCsrf($clean['token'])) die("wrong token");
				
				$clean['id_user'] = (int)$this->s['registered']->status['id_user'];
				$clean['id'] = $this->request->post('id',0,'forceInt');
				$type = $this->request->post('type',0,'sanitizeAll');
				$message = $this->request->post('message','');
				
				$modelName = 'error';
				
				if (array_key_exists($type,$this->types))
				{
					$modelName = $this->types[$type]['model_name'];
					$clean['type'] = $this->types[$type]['clean_type'];
					$clean['id_name'] = $this->types[$type]['id_name'];

					//load the right model
					$this->model($modelName);
					$model = $this->m[$modelName];

					$count = $model->select()->where(array($clean['id_name'] => $clean['id'],'deleted' => $this->strings[$action]['check_status']))->rowNumber();

					if ($count > 0)
					{
						if (eg_strlen($message) < 500)
						{
							//hide the message
							$model->values = array('deleted' => $this->strings[$action]['to_status']);
							$model->update($clean['id']);
							
							if ($model->queryResult)
							{
								$this->m['HistoryModel']->setFields('id:forceInt,type,message','sanitizeAll');
								$this->m['HistoryModel']->values['created_by'] = $clean['id_user'];
								$this->m['HistoryModel']->values['action'] = $this->strings[$action]['action'];
								$this->m['HistoryModel']->updateTable('insert');
								
								echo $this->strings[$action]['exec_string'];
							}
							else
							{
								echo "error: one error occurred, please retry later";
							}
						}
						else
						{
							echo "error: the message has too many characters or wrong type";
						}
					}
					else
					{
						echo $this->strings[$action]['error_string'];
					}
				}
			}
		}
	}

	public function viewall($lang = 'en', $type = 'message', $id = 0)
	{
		header('Content-type: text/html; charset=UTF-8');
		
		$this->shift(3);
		
		$this->clean();
		
		if ($this->s['registered']->status['status'] === 'logged')
		{
			if ($this->ismoderator)
			{
				$clean['id'] = (int)$id;
				if (array_key_exists($type,$this->types))
				{
					$clean['type'] = $this->types[$type]['clean_type'];
					
					$data['res'] = $this->m['HistoryModel']->select()->where(array('id'=>$clean['id'],'type'=>$clean['type']))->send();
					
					$data['md_action'] = array('hide'=>'hidden','show'=>'restored');
					
					$this->append($data);
					$this->load('viewall');
				}
			}
		}
	}

}