-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.php
246 lines (204 loc) · 7.07 KB
/
post.php
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/* LICENSE
*
* BanSE - a site base (designed to be the SCEngine website)
* Copyright (C) 2007-2012 Colomban Wendling <[email protected]>
*
* 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/>.
*
*/
/*
* This file is used to interact with the database
* Actions are writing, updating or deleting data from the "news"-like tables
*/
require_once ('include/defines.php');
require_once ('lib/User.php');
require_once ('lib/MyDB.php');
require_once ('lib/BCode.php');
require_once ('lib/string.php');
require_once ('lib/TypedDialog.php');
require_once ('lib/feeds.php');
require_once ('lib/UrlTable.php');
// adresse de redirection par défaut, utilisée si aucune autre n'est trouvée
$refresh = UrlTable::home ();
if (isset ($_GET['redirect'])) {
// if a redirect was sepcified, use it
$refresh = urldecode ($_GET['redirect']);
}
else {
// else, try to get previous URL
// on vérifie que le client a donné une adresse de page précédante
if ($_SERVER['HTTP_REFERER']) {
// on vérifie que la page pérécdante correspond à une page du site
if (str_has_prefix ($_SERVER['HTTP_REFERER'], 'http://'.$_SERVER['SERVER_NAME'])) {
$refresh = $_SERVER['HTTP_REFERER'];
if (isset ($_GET['id']) && ctype_digit ($_GET['id']))
{
/* don't redirect to a removed page */
if ($_GET['act'] == 'rm')
$refresh = UrlTable::news ();
else
$refresh = UrlTable::news ($_GET['id']);
}
}
}
}
/* dialog creation */
$dialog = new TypedDialog (DIALOG_TYPE_INFO, $refresh);
/* abstract class to update news */
abstract class PostNews {
/*
* Table shape:
* - id INT(11) PRIMARY KEY AUTO_INCREMENT
* - date BIGINT(20) NOT NULL
* - mdate BIGINT(20) NOT NULL
* - title VARCHAR(256) NOT NULL
* - content TEXT NOT NULL
* - source TEXT NOT NULL
* - author VARCHAR(256) NOT NULL
* - mauthor VARCHAR(256) NOT NULL
* - published BOOLEAN NOT NULL
*/
const SECTION = 'news';
private static $table = NEWS_TABLE;
protected static function update_feed () {
feed_update_news ();
}
public static function save ($title, $content, $publish = false) {
global $dialog;
//$content = parse ($content);
$source = $content;
$content = BCode::parse ($source);
$author = User::get_name ();
$date = time ();
if (! empty ($title) && ! empty ($content) && ! empty ($source) && ! empty ($author)) {
$db = new MyDB (DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME, DB_TRANSFERT_ENCODING);
$db->select_table (self::$table);
if ($db->insert (array ('date' => $date, 'mdate' => $date,
'title' => $title, 'content' => $content,
'source' => $source, 'author' => $author,
'mauthor' => $author,
'published' => ($publish == true)))) {
$dialog->add_info_message ('News postée avec succès.');
self::update_feed ();
}
else {
$dialog->add_error_message ('Une erreur est survenue lors de l\'insertion des '.
'données dans la base de données. Veuillez contacter '.
'votre administrateur pour qu\'il corrige l\'erreur.');
}
unset ($db);
}
else
$dialog->add_error_message ('Aucune information n\'a été trouvée pour poster la news !');
}
public static function remove ($id) {
global $dialog;
if (!empty ($id)) {
$db = new MyDB (DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME, DB_TRANSFERT_ENCODING);
$db->select_table (self::$table);
if ($db->delete (array ('id' => $id))) {
$dialog->add_info_message ('News supprimée avec succès.');
self::update_feed ();
}
else {
$dialog->add_error_message ('Erreur lors de la suppression de la news.');
}
unset ($db);
}
else {
$dialog->add_error_message ('Pas d\'ID spécifiée.');
}
}
public static function edit ($id, $title, $content, $update_mdate = true, $publish = false) {
global $dialog;
$source = $content;
$content = BCode::parse ($source);
$title = $title;
if (! empty ($id) && ! empty ($title) && ! empty ($content)) {
$data = array ('title' => $title,
'content' => $content,
'source' => $source,
'published' => ($publish == true));
if ($update_mdate) {
$data['mdate'] = time ();
$data['mauthor'] = User::get_name ();
}
$db = new MyDB (DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME, DB_TRANSFERT_ENCODING);
$db->select_table (self::$table);
/* if we publish for the first time, update date */
if ($publish) {
$db->select ('published', array ('id' => $id));
$resp = $db->fetch_response ();
if (! $resp['published']) {
$data['date'] = $data['mdate'] = time ();
}
}
if ($db->update ($data, array ('id' => $id))) {
$dialog->add_info_message ('News éditée avec succès.');
self::update_feed ();
}
else
$dialog->add_error_message ('Erreur lors de l\'édition de la news.');
unset ($db);
}
else
$dialog->add_error_message ('Les données puxxent !!!');
}
public static function get_table() {
return self::$table;
}
}
/******************************************************************************/
if (User::get_logged ())
{
if (PostNews::SECTION == $_GET['sec'])
{
if (User::has_rights (ADMIN_LEVEL_NEWS))
{
$publish = filter_input (INPUT_POST, 'publish', FILTER_VALIDATE_BOOLEAN);
// new news
if ($_GET['act'] == 'new')
PostNews::save ($_POST['title'], $_POST['content'], $publish);
// edit an existing news
else if ($_GET['act'] == 'edit')
{
if (!empty ($_GET['id']))
PostNews::edit ($_GET['id'], $_POST['title'], $_POST['content'],
! filter_input (INPUT_POST, 'noupdate'),
$publish);
else
$dialog->add_error_message ('Aucun ID spécifié');
}
// remove an existing news
else if ($_GET['act'] == 'rm')
{
if (!empty ($_GET['id']))
PostNews::remove ($_GET['id']);
else
$dialog->add_error_message ('Aucun ID spécifié');
}
else // invalid action request
$dialog->add_error_message ('Action invalide.');
}
else // user level for news
$dialog->add_error_message ('Vous n\'avez pas le droit d\'effectuer cette action.');
}
else // invalid section post (news || devel)
$dialog->add_error_message ('Aucune section spécifiée.');
}
else // not logged in
$dialog->add_error_message ('Vous n\'avez pas le droit d\'effectuer cette action.');
/* display the dialog */
$dialog->flush ();