1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* XOOPS news topic |
4
|
|
|
* |
5
|
|
|
* You may not change or alter any portion of this comment or credits |
6
|
|
|
* of supporting developers from this source code or any supporting source code |
7
|
|
|
* which is considered copyrighted (c) material of the original comment or credit authors. |
8
|
|
|
* This program is distributed in the hope that it will be useful, |
9
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
10
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
11
|
|
|
* |
12
|
|
|
* @copyright (c) 2000-2016 XOOPS Project (www.xoops.org) |
13
|
|
|
* @license GNU GPL 2 (http://www.gnu.org/licenses/gpl-2.0.html) |
14
|
|
|
* @package kernel |
15
|
|
|
* @since 2.0.0 |
16
|
|
|
* @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/ |
17
|
|
|
* @version $Id: xoopstopic.php 13090 2015-06-16 20:44:29Z beckmi $ |
18
|
|
|
* @deprecated |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
defined('XOOPS_ROOT_PATH') || exit('Restricted access'); |
22
|
|
|
|
23
|
|
|
$GLOBALS['xoopsLogger']->addDeprecated("'/class/xoopstopic.php' is deprecated since XOOPS 2.5.4, please create your own class instead."); |
24
|
|
|
|
25
|
|
|
include_once XOOPS_ROOT_PATH . '/class/xoopstree.php'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class XoopsTopic |
29
|
|
|
*/ |
30
|
|
|
class XoopsTopic |
31
|
|
|
{ |
32
|
|
|
public $table; |
33
|
|
|
public $topic_id; |
34
|
|
|
public $topic_pid; |
35
|
|
|
public $topic_title; |
36
|
|
|
public $topic_imgurl; |
37
|
|
|
public $prefix; // only used in topic tree |
38
|
|
|
public $use_permission = false; |
39
|
|
|
public $mid; // module id used for setting permission |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $table |
43
|
|
|
* @param int $topicid |
44
|
|
|
*/ |
45
|
|
|
public function __construct($table, $topicid = 0) |
46
|
|
|
{ |
47
|
|
|
$this->db = XoopsDatabaseFactory::getDatabaseConnection(); |
|
|
|
|
48
|
|
|
$this->table = $table; |
49
|
|
|
if (is_array($topicid)) { |
50
|
|
|
$this->makeTopic($topicid); |
51
|
|
|
} elseif ($topicid != 0) { |
52
|
|
|
$this->getTopic((int)$topicid); |
53
|
|
|
} else { |
54
|
|
|
$this->topic_id = $topicid; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $value |
60
|
|
|
*/ |
61
|
|
|
public function setTopicTitle($value) |
62
|
|
|
{ |
63
|
|
|
$this->topic_title = $value; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $value |
68
|
|
|
*/ |
69
|
|
|
public function setTopicImgurl($value) |
70
|
|
|
{ |
71
|
|
|
$this->topic_imgurl = $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $value |
76
|
|
|
*/ |
77
|
|
|
public function setTopicPid($value) |
78
|
|
|
{ |
79
|
|
|
$this->topic_pid = $value; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param $topicid |
84
|
|
|
*/ |
85
|
|
View Code Duplication |
public function getTopic($topicid) |
86
|
|
|
{ |
87
|
|
|
$topicid = (int)$topicid; |
88
|
|
|
$sql = 'SELECT * FROM ' . $this->table . ' WHERE topic_id=' . $topicid . ''; |
89
|
|
|
$array = $this->db->fetchArray($this->db->query($sql)); |
90
|
|
|
$this->makeTopic($array); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param $array |
95
|
|
|
*/ |
96
|
|
|
public function makeTopic($array) |
97
|
|
|
{ |
98
|
|
|
foreach ($array as $key => $value) { |
99
|
|
|
$this->$key = $value; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $mid |
105
|
|
|
*/ |
106
|
|
|
public function usePermission($mid) |
107
|
|
|
{ |
108
|
|
|
$this->mid = $mid; |
109
|
|
|
$this->use_permission = true; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function store() |
116
|
|
|
{ |
117
|
|
|
$myts = MyTextSanitizer::getInstance(); |
118
|
|
|
$title = ''; |
119
|
|
|
$imgurl = ''; |
120
|
|
|
if (isset($this->topic_title) && $this->topic_title != '') { |
121
|
|
|
$title = $myts->addSlashes($this->topic_title); |
122
|
|
|
} |
123
|
|
|
if (isset($this->topic_imgurl) && $this->topic_imgurl != '') { |
124
|
|
|
$imgurl = $myts->addSlashes($this->topic_imgurl); |
125
|
|
|
} |
126
|
|
|
if (!isset($this->topic_pid) || !is_numeric($this->topic_pid)) { |
127
|
|
|
$this->topic_pid = 0; |
128
|
|
|
} |
129
|
|
|
if (empty($this->topic_id)) { |
130
|
|
|
$this->topic_id = $this->db->genId($this->table . '_topic_id_seq'); |
131
|
|
|
$sql = sprintf("INSERT INTO %s (topic_id, topic_pid, topic_imgurl, topic_title) VALUES (%u, %u, '%s', '%s')", $this->table, $this->topic_id, $this->topic_pid, $imgurl, $title); |
132
|
|
|
} else { |
133
|
|
|
$sql = sprintf("UPDATE %s SET topic_pid = %u, topic_imgurl = '%s', topic_title = '%s' WHERE topic_id = %u", $this->table, $this->topic_pid, $imgurl, $title, $this->topic_id); |
134
|
|
|
} |
135
|
|
|
if (!$result = $this->db->query($sql)) { |
136
|
|
|
ErrorHandler::show('0022'); |
137
|
|
|
} |
138
|
|
|
if ($this->use_permission == true) { |
|
|
|
|
139
|
|
|
if (empty($this->topic_id)) { |
140
|
|
|
$this->topic_id = $this->db->getInsertId(); |
141
|
|
|
} |
142
|
|
|
$xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
143
|
|
|
$parent_topics = $xt->getAllParentId($this->topic_id); |
144
|
|
View Code Duplication |
if (!empty($this->m_groups) && is_array($this->m_groups)) { |
145
|
|
|
foreach ($this->m_groups as $m_g) { |
|
|
|
|
146
|
|
|
$moderate_topics = XoopsPerms::getPermitted($this->mid, 'ModInTopic', $m_g); |
147
|
|
|
$add = true; |
148
|
|
|
// only grant this permission when the group has this permission in all parent topics of the created topic |
149
|
|
|
foreach ($parent_topics as $p_topic) { |
150
|
|
|
if (!in_array($p_topic, $moderate_topics)) { |
151
|
|
|
$add = false; |
152
|
|
|
continue; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
if ($add == true) { |
|
|
|
|
156
|
|
|
$xp = new XoopsPerms(); |
157
|
|
|
$xp->setModuleId($this->mid); |
158
|
|
|
$xp->setName('ModInTopic'); |
159
|
|
|
$xp->setItemId($this->topic_id); |
160
|
|
|
$xp->store(); |
161
|
|
|
$xp->addGroup($m_g); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
View Code Duplication |
if (!empty($this->s_groups) && is_array($this->s_groups)) { |
|
|
|
|
166
|
|
|
foreach ($s_groups as $s_g) { |
|
|
|
|
167
|
|
|
$submit_topics = XoopsPerms::getPermitted($this->mid, 'SubmitInTopic', $s_g); |
168
|
|
|
$add = true; |
169
|
|
|
foreach ($parent_topics as $p_topic) { |
170
|
|
|
if (!in_array($p_topic, $submit_topics)) { |
171
|
|
|
$add = false; |
172
|
|
|
continue; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
if ($add == true) { |
|
|
|
|
176
|
|
|
$xp = new XoopsPerms(); |
177
|
|
|
$xp->setModuleId($this->mid); |
178
|
|
|
$xp->setName('SubmitInTopic'); |
179
|
|
|
$xp->setItemId($this->topic_id); |
180
|
|
|
$xp->store(); |
181
|
|
|
$xp->addGroup($s_g); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
View Code Duplication |
if (!empty($this->r_groups) && is_array($this->r_groups)) { |
|
|
|
|
186
|
|
|
foreach ($r_groups as $r_g) { |
|
|
|
|
187
|
|
|
$read_topics = XoopsPerms::getPermitted($this->mid, 'ReadInTopic', $r_g); |
188
|
|
|
$add = true; |
189
|
|
|
foreach ($parent_topics as $p_topic) { |
190
|
|
|
if (!in_array($p_topic, $read_topics)) { |
191
|
|
|
$add = false; |
192
|
|
|
continue; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
if ($add == true) { |
|
|
|
|
196
|
|
|
$xp = new XoopsPerms(); |
197
|
|
|
$xp->setModuleId($this->mid); |
198
|
|
|
$xp->setName('ReadInTopic'); |
199
|
|
|
$xp->setItemId($this->topic_id); |
200
|
|
|
$xp->store(); |
201
|
|
|
$xp->addGroup($r_g); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return true; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function delete() |
211
|
|
|
{ |
212
|
|
|
$sql = sprintf('DELETE FROM %s WHERE topic_id = %u', $this->table, $this->topic_id); |
213
|
|
|
$this->db->query($sql); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return int |
218
|
|
|
*/ |
219
|
|
|
public function topic_id() |
220
|
|
|
{ |
221
|
|
|
return $this->topic_id; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function topic_pid() |
225
|
|
|
{ |
226
|
|
|
return $this->topic_pid; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param string $format |
231
|
|
|
* |
232
|
|
|
* @return mixed |
233
|
|
|
*/ |
234
|
|
View Code Duplication |
public function topic_title($format = 'S') |
235
|
|
|
{ |
236
|
|
|
$myts = MyTextSanitizer::getInstance(); |
237
|
|
|
switch ($format) { |
238
|
|
|
case 'S': |
239
|
|
|
case 'E': |
240
|
|
|
$title = $myts->htmlSpecialChars($this->topic_title); |
241
|
|
|
break; |
242
|
|
|
case 'P': |
243
|
|
|
case 'F': |
244
|
|
|
$title = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_title)); |
245
|
|
|
break; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $title; |
|
|
|
|
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $format |
253
|
|
|
* |
254
|
|
|
* @return mixed |
255
|
|
|
*/ |
256
|
|
View Code Duplication |
public function topic_imgurl($format = 'S') |
257
|
|
|
{ |
258
|
|
|
$myts = MyTextSanitizer::getInstance(); |
259
|
|
|
switch ($format) { |
260
|
|
|
case 'S': |
261
|
|
|
case 'E': |
262
|
|
|
$imgurl = $myts->htmlSpecialChars($this->topic_imgurl); |
263
|
|
|
break; |
264
|
|
|
case 'P': |
265
|
|
|
case 'F': |
266
|
|
|
$imgurl = $myts->htmlSpecialChars($myts->stripSlashesGPC($this->topic_imgurl)); |
267
|
|
|
break; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
return $imgurl; |
|
|
|
|
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
public function prefix() |
|
|
|
|
274
|
|
|
{ |
275
|
|
|
if (isset($this->prefix)) { |
276
|
|
|
return $this->prefix; |
277
|
|
|
} |
278
|
|
|
return null; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @return array |
283
|
|
|
*/ |
284
|
|
View Code Duplication |
public function getFirstChildTopics() |
285
|
|
|
{ |
286
|
|
|
$ret = array(); |
287
|
|
|
$xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
288
|
|
|
$topic_arr = $xt->getFirstChild($this->topic_id, 'topic_title'); |
289
|
|
|
if (is_array($topic_arr) && count($topic_arr)) { |
290
|
|
|
foreach ($topic_arr as $topic) { |
291
|
|
|
$ret[] = new XoopsTopic($this->table, $topic); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return $ret; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return array |
300
|
|
|
*/ |
301
|
|
View Code Duplication |
public function getAllChildTopics() |
302
|
|
|
{ |
303
|
|
|
$ret = array(); |
304
|
|
|
$xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
305
|
|
|
$topic_arr = $xt->getAllChild($this->topic_id, 'topic_title'); |
306
|
|
|
if (is_array($topic_arr) && count($topic_arr)) { |
307
|
|
|
foreach ($topic_arr as $topic) { |
308
|
|
|
$ret[] = new XoopsTopic($this->table, $topic); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $ret; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return array |
317
|
|
|
*/ |
318
|
|
View Code Duplication |
public function getChildTopicsTreeArray() |
319
|
|
|
{ |
320
|
|
|
$ret = array(); |
321
|
|
|
$xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
322
|
|
|
$topic_arr = $xt->getChildTreeArray($this->topic_id, 'topic_title'); |
323
|
|
|
if (is_array($topic_arr) && count($topic_arr)) { |
324
|
|
|
foreach ($topic_arr as $topic) { |
325
|
|
|
$ret[] = new XoopsTopic($this->table, $topic); |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $ret; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param int $none |
334
|
|
|
* @param $seltopic |
335
|
|
|
* @param string $selname |
336
|
|
|
* @param string $onchange |
337
|
|
|
*/ |
338
|
|
|
public function makeTopicSelBox($none = 0, $seltopic = -1, $selname = '', $onchange = '') |
339
|
|
|
{ |
340
|
|
|
$xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
341
|
|
|
if ($seltopic != -1) { |
342
|
|
|
$xt->makeMySelBox('topic_title', 'topic_title', $seltopic, $none, $selname, $onchange); |
343
|
|
|
} elseif (!empty($this->topic_id)) { |
344
|
|
|
$xt->makeMySelBox('topic_title', 'topic_title', $this->topic_id, $none, $selname, $onchange); |
345
|
|
|
} else { |
346
|
|
|
$xt->makeMySelBox('topic_title', 'topic_title', 0, $none, $selname, $onchange); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
//generates nicely formatted linked path from the root id to a given id |
351
|
|
|
/** |
352
|
|
|
* @param $funcURL |
353
|
|
|
* |
354
|
|
|
* @return string |
355
|
|
|
*/ |
356
|
|
View Code Duplication |
public function getNiceTopicPathFromId($funcURL) |
357
|
|
|
{ |
358
|
|
|
$xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
359
|
|
|
$ret = $xt->getNicePathFromId($this->topic_id, 'topic_title', $funcURL); |
360
|
|
|
|
361
|
|
|
return $ret; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @return array |
366
|
|
|
*/ |
367
|
|
View Code Duplication |
public function getAllChildTopicsId() |
368
|
|
|
{ |
369
|
|
|
$xt = new XoopsTree($this->table, 'topic_id', 'topic_pid'); |
370
|
|
|
$ret = $xt->getAllChildId($this->topic_id, 'topic_title'); |
371
|
|
|
|
372
|
|
|
return $ret; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @return array |
377
|
|
|
*/ |
378
|
|
|
public function getTopicsList() |
379
|
|
|
{ |
380
|
|
|
$result = $this->db->query('SELECT topic_id, topic_pid, topic_title FROM ' . $this->table); |
381
|
|
|
$ret = array(); |
382
|
|
|
$myts = MyTextSanitizer::getInstance(); |
383
|
|
|
while ($myrow = $this->db->fetchArray($result)) { |
384
|
|
|
$ret[$myrow['topic_id']] = array('title' => $myts->htmlspecialchars($myrow['topic_title']), 'pid' => $myrow['topic_pid']); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
return $ret; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* @param $pid |
392
|
|
|
* @param $title |
393
|
|
|
* |
394
|
|
|
* @return bool |
395
|
|
|
*/ |
396
|
|
|
public function topicExists($pid, $title) |
397
|
|
|
{ |
398
|
|
|
$sql = 'SELECT COUNT(*) from ' . $this->table . ' WHERE topic_pid = ' . (int)$pid . " AND topic_title = '" . trim($title) . "'"; |
399
|
|
|
$rs = $this->db->query($sql); |
400
|
|
|
list($count) = $this->db->fetchRow($rs); |
401
|
|
|
if ($count > 0) { |
402
|
|
|
return true; |
403
|
|
|
} else { |
404
|
|
|
return false; |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.