Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Post often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Post, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
40 | class Post extends XoopsObject |
||
41 | { |
||
42 | //class Post extends XoopsObject { |
||
43 | public $attachment_array = array(); |
||
44 | |||
45 | /** |
||
46 | * Post constructor. |
||
47 | */ |
||
48 | public function __construct() |
||
49 | { |
||
50 | parent::__construct('bb_posts'); |
||
51 | $this->initVar('post_id', XOBJ_DTYPE_INT); |
||
52 | $this->initVar('topic_id', XOBJ_DTYPE_INT, 0, true); |
||
53 | $this->initVar('forum_id', XOBJ_DTYPE_INT, 0, true); |
||
54 | $this->initVar('post_time', XOBJ_DTYPE_INT, 0, true); |
||
55 | $this->initVar('poster_ip', XOBJ_DTYPE_INT, 0); |
||
56 | $this->initVar('poster_name', XOBJ_DTYPE_TXTBOX, ''); |
||
57 | $this->initVar('subject', XOBJ_DTYPE_TXTBOX, '', true); |
||
58 | $this->initVar('pid', XOBJ_DTYPE_INT, 0); |
||
59 | $this->initVar('dohtml', XOBJ_DTYPE_INT, 0); |
||
60 | $this->initVar('dosmiley', XOBJ_DTYPE_INT, 1); |
||
61 | $this->initVar('doxcode', XOBJ_DTYPE_INT, 1); |
||
62 | $this->initVar('doimage', XOBJ_DTYPE_INT, 1); |
||
63 | $this->initVar('dobr', XOBJ_DTYPE_INT, 1); |
||
64 | $this->initVar('uid', XOBJ_DTYPE_INT, 1); |
||
65 | $this->initVar('icon', XOBJ_DTYPE_TXTBOX, ''); |
||
66 | $this->initVar('attachsig', XOBJ_DTYPE_INT, 0); |
||
67 | $this->initVar('approved', XOBJ_DTYPE_INT, 1); |
||
68 | $this->initVar('post_karma', XOBJ_DTYPE_INT, 0); |
||
69 | $this->initVar('require_reply', XOBJ_DTYPE_INT, 0); |
||
70 | $this->initVar('attachment', XOBJ_DTYPE_TXTAREA, ''); |
||
71 | $this->initVar('post_text', XOBJ_DTYPE_TXTAREA, ''); |
||
72 | $this->initVar('post_edit', XOBJ_DTYPE_TXTAREA, ''); |
||
73 | } |
||
74 | |||
75 | // //////////////////////////////////////////////////////////////////////////////////// |
||
76 | // attachment functions TODO: there should be a file/attachment management class |
||
77 | /** |
||
78 | * @return array|mixed|null |
||
79 | */ |
||
80 | public function getAttachment() |
||
81 | { |
||
82 | if (count($this->attachment_array)) { |
||
83 | return $this->attachment_array; |
||
84 | } |
||
85 | $attachment = $this->getVar('attachment'); |
||
86 | if (empty($attachment)) { |
||
87 | $this->attachment_array = null; |
||
88 | } else { |
||
89 | $this->attachment_array = @unserialize(base64_decode($attachment)); |
||
90 | } |
||
91 | |||
92 | return $this->attachment_array; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param $attach_key |
||
97 | * @return bool |
||
98 | */ |
||
99 | public function incrementDownload($attach_key) |
||
100 | { |
||
101 | if (!$attach_key) { |
||
102 | return false; |
||
103 | } |
||
104 | $this->attachment_array[(string)$attach_key]['num_download']++; |
||
105 | |||
106 | return $this->attachment_array[(string)$attach_key]['num_download']; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function saveAttachment() |
||
113 | { |
||
114 | $attachment_save = ''; |
||
115 | if (is_array($this->attachment_array) && count($this->attachment_array) > 0) { |
||
116 | $attachment_save = base64_encode(serialize($this->attachment_array)); |
||
117 | } |
||
118 | $this->setVar('attachment', $attachment_save); |
||
119 | $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('bb_posts') . ' SET attachment=' . $GLOBALS['xoopsDB']->quoteString($attachment_save) . ' WHERE post_id = ' . $this->getVar('post_id'); |
||
120 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
121 | //xoops_error($GLOBALS["xoopsDB"]->error()); |
||
122 | return false; |
||
123 | } |
||
124 | |||
125 | return true; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param null $attach_array |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function deleteAttachment($attach_array = null) |
||
133 | { |
||
134 | $attach_old = $this->getAttachment(); |
||
135 | if (!is_array($attach_old) || count($attach_old) < 1) { |
||
136 | return true; |
||
137 | } |
||
138 | $this->attachment_array = array(); |
||
139 | |||
140 | if ($attach_array === null) { |
||
141 | $attach_array = array_keys($attach_old); |
||
142 | } // to delete all! |
||
143 | if (!is_array($attach_array)) { |
||
144 | $attach_array = array($attach_array); |
||
145 | } |
||
146 | |||
147 | foreach ($attach_old as $key => $attach) { |
||
148 | if (in_array($key, $attach_array)) { |
||
149 | @unlink(XOOPS_ROOT_PATH . '/' . $GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/' . $attach['name_saved']); |
||
150 | @unlink(XOOPS_ROOT_PATH . '/' . $GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/thumbs/' . $attach['name_saved']); // delete thumbnails |
||
151 | continue; |
||
152 | } |
||
153 | $this->attachment_array[$key] = $attach; |
||
154 | } |
||
155 | if (is_array($this->attachment_array) && count($this->attachment_array) > 0) { |
||
156 | $attachment_save = base64_encode(serialize($this->attachment_array)); |
||
157 | } else { |
||
158 | $attachment_save = ''; |
||
159 | } |
||
160 | $this->setVar('attachment', $attachment_save); |
||
161 | |||
162 | return true; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param string $name_saved |
||
167 | * @param string $name_display |
||
168 | * @param string $mimetype |
||
169 | * @param int $num_download |
||
170 | * @return bool |
||
171 | */ |
||
172 | public function setAttachment($name_saved = '', $name_display = '', $mimetype = '', $num_download = 0) |
||
173 | { |
||
174 | static $counter = 0; |
||
175 | $this->attachment_array = $this->getAttachment(); |
||
176 | if ($name_saved) { |
||
177 | $key = (string)(time() + $counter++); |
||
178 | $this->attachment_array[$key] = array( |
||
179 | 'name_saved' => $name_saved, |
||
180 | 'name_display' => isset($name_display) ? $name_display : $name_saved, |
||
181 | 'mimetype' => $mimetype, |
||
182 | 'num_download' => isset($num_download) ? (int)$num_download : 0 |
||
183 | ); |
||
184 | } |
||
185 | if (is_array($this->attachment_array)) { |
||
186 | $attachment_save = base64_encode(serialize($this->attachment_array)); |
||
187 | } else { |
||
188 | $attachment_save = null; |
||
189 | } |
||
190 | $this->setVar('attachment', $attachment_save); |
||
191 | |||
192 | return true; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * TODO: refactor |
||
197 | * @param bool $asSource |
||
198 | * @return string |
||
199 | */ |
||
200 | public function displayAttachment($asSource = false) |
||
201 | { |
||
202 | $post_attachment = ''; |
||
203 | $attachments = $this->getAttachment(); |
||
204 | if (is_array($attachments) && count($attachments) > 0) { |
||
205 | $iconHandler = newbb_getIconHandler(); |
||
206 | $mime_path = $iconHandler->getPath('mime'); |
||
207 | include_once $GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModule']->getVar('dirname', 'n') . '/include/functions.image.php'); |
||
208 | $image_extensions = array('jpg', 'jpeg', 'gif', 'png', 'bmp'); // need improve !!! |
||
209 | $post_attachment .= '<br><strong>' . _MD_ATTACHMENT . '</strong>:'; |
||
210 | $post_attachment .= "<div style='margin: 1em 0; border-top: 1px solid;'></div>\n"; |
||
211 | // $post_attachment .= '<br><hr style="height: 1px;" noshade="noshade" /><br>'; |
||
212 | foreach ($attachments as $key => $att) { |
||
213 | $file_extension = ltrim(strrchr($att['name_saved'], '.'), '.'); |
||
214 | $filetype = $file_extension; |
||
215 | if (file_exists($GLOBALS['xoops']->path("{$mime_path}/{$filetype}.gif"))) { |
||
216 | $icon_filetype = $GLOBALS['xoops']->url("{$mime_path}/{$filetype}.gif"); |
||
217 | } else { |
||
218 | $icon_filetype = $GLOBALS['xoops']->url("{$mime_path}/unknown.gif"); |
||
219 | } |
||
220 | $file_size = @filesize($GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/' . $att['name_saved'])); |
||
221 | $file_size = number_format($file_size / 1024, 2) . ' KB'; |
||
222 | if (in_array(strtolower($file_extension), $image_extensions) |
||
223 | && $GLOBALS['xoopsModuleConfig']['media_allowed'] |
||
224 | ) { |
||
225 | $post_attachment .= '<br><img src="' . $icon_filetype . '" alt="' . $filetype . '" /><strong> ' . $att['name_display'] . '</strong> <small>(' . $file_size . ')</small>'; |
||
226 | $post_attachment .= '<br>' . newbb_attachmentImage($att['name_saved']); |
||
227 | $isDisplayed = true; |
||
228 | } else { |
||
229 | if (empty($GLOBALS['xoopsModuleConfig']['show_userattach'])) { |
||
230 | $post_attachment .= "<a href='" . $GLOBALS['xoops']->url('/modules/' . $GLOBALS['xoopsModule']->getVar('dirname', 'n') . "/dl_attachment.php?attachid={$key}&post_id=" . $this->getVar('post_id')) |
||
231 | . "'> <img src='{$icon_filetype}' alt='{$filetype}' /> {$att['name_display']}</a> " . _MD_FILESIZE . ": {$file_size}; " . _MD_HITS . ": {$att['num_download']}"; |
||
232 | } elseif (($GLOBALS['xoopsUser'] instanceof XoopsUser) && $GLOBALS['xoopsUser']->uid() > 0 |
||
233 | && $GLOBALS['xoopsUser']->isActive() |
||
234 | ) { |
||
235 | $post_attachment .= "<a href='" . $GLOBALS['xoops']->url('/modules/' . $GLOBALS['xoopsModule']->getVar('dirname', 'n') . "/dl_attachment.php?attachid={$key}&post_id=" . $this->getVar('post_id')) . "'> <img src='" |
||
236 | . $icon_filetype . "' alt='{$filetype}' /> {$att['name_display']}</a> " . _MD_FILESIZE . ": {$file_size}; " . _MD_HITS . ": {$att['num_download']}"; |
||
237 | } else { |
||
238 | $post_attachment .= _MD_NEWBB_SEENOTGUEST; |
||
239 | } |
||
240 | } |
||
241 | $post_attachment .= '<br>'; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | return $post_attachment; |
||
246 | } |
||
247 | // attachment functions |
||
248 | // //////////////////////////////////////////////////////////////////////////////////// |
||
249 | |||
250 | /** |
||
251 | * @param string $poster_name |
||
252 | * @param string $post_editmsg |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function setPostEdit($poster_name = '', $post_editmsg = '') |
||
256 | { |
||
257 | if (empty($GLOBALS['xoopsModuleConfig']['recordedit_timelimit']) |
||
258 | || (time() - $this->getVar('post_time')) < $GLOBALS['xoopsModuleConfig']['recordedit_timelimit'] * 60 |
||
259 | || $this->getVar('approved') < 1 |
||
260 | ) { |
||
261 | return true; |
||
262 | } |
||
263 | if (($GLOBALS['xoopsUser'] instanceof XoopsUser) && $GLOBALS['xoopsUser']->isActive()) { |
||
264 | if ($GLOBALS['xoopsModuleConfig']['show_realname'] && $GLOBALS['xoopsUser']->getVar('name')) { |
||
265 | $edit_user = $GLOBALS['xoopsUser']->getVar('name'); |
||
266 | } else { |
||
267 | $edit_user = $GLOBALS['xoopsUser']->getVar('uname'); |
||
268 | } |
||
269 | } |
||
270 | $post_edit = array(); |
||
271 | $post_edit['edit_user'] = $edit_user; // The proper way is to store uid instead of name. However, to save queries when displaying, the current way is ok. |
||
272 | $post_edit['edit_time'] = time(); |
||
273 | $post_edit['edit_msg'] = $post_editmsg; |
||
274 | |||
275 | $post_edits = $this->getVar('post_edit'); |
||
276 | if (!empty($post_edits)) { |
||
277 | $post_edits = unserialize(base64_decode($post_edits)); |
||
278 | } |
||
279 | if (!is_array($post_edits)) { |
||
280 | $post_edits = array(); |
||
281 | } |
||
282 | $post_edits[] = $post_edit; |
||
283 | $post_edit = base64_encode(serialize($post_edits)); |
||
284 | unset($post_edits); |
||
285 | $this->setVar('post_edit', $post_edit); |
||
286 | |||
287 | return true; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @return bool|string |
||
292 | */ |
||
293 | public function displayPostEdit() |
||
294 | { |
||
295 | global $myts; |
||
296 | |||
297 | if (empty($GLOBALS['xoopsModuleConfig']['recordedit_timelimit'])) { |
||
298 | return false; |
||
299 | } |
||
300 | |||
301 | $post_edit = ''; |
||
302 | $post_edits = $this->getVar('post_edit'); |
||
303 | if (!empty($post_edits)) { |
||
304 | $post_edits = unserialize(base64_decode($post_edits)); |
||
305 | } |
||
306 | if (!isset($post_edits) || !is_array($post_edits)) { |
||
307 | $post_edits = array(); |
||
308 | } |
||
309 | if (is_array($post_edits) && count($post_edits) > 0) { |
||
310 | foreach ($post_edits as $postedit) { |
||
311 | $edit_time = (int)$postedit['edit_time']; |
||
312 | $edit_user = $myts->stripSlashesGPC($postedit['edit_user']); |
||
313 | $edit_msg = (!empty($postedit['edit_msg'])) ? $myts->stripSlashesGPC($postedit['edit_msg']) : ''; |
||
314 | // Start irmtfan add option to do only the latest edit when do_latestedit=0 (Alfred) |
||
315 | if (empty($GLOBALS['xoopsModuleConfig']['do_latestedit'])) { |
||
316 | $post_edit = ''; |
||
317 | } |
||
318 | // End irmtfan add option to do only the latest edit when do_latestedit=0 (Alfred) |
||
319 | // START hacked by irmtfan |
||
320 | // display/save all edit records. |
||
321 | $post_edit .= _MD_EDITEDBY . ' ' . $edit_user . ' ' . _MD_ON . ' ' . newbb_formatTimestamp((int)$edit_time) . '<br>'; |
||
322 | // if reason is not empty |
||
323 | if ($edit_msg !== '') { |
||
324 | $post_edit .= _MD_EDITEDMSG . ' ' . $edit_msg . '<br>'; |
||
325 | } |
||
326 | // START hacked by irmtfan |
||
327 | } |
||
328 | } |
||
329 | |||
330 | return $post_edit; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * @return array |
||
335 | */ |
||
336 | public function &getPostBody() |
||
337 | { |
||
338 | global $myts; |
||
339 | $GLOBALS['xoopsModuleConfig'] = newbb_load_config(); // irmtfan load all newbb configs - newbb config in blocks activated in some modules like profile |
||
340 | mod_loadFunctions('user', 'newbb'); |
||
341 | mod_loadFunctions('render', 'newbb'); |
||
342 | |||
343 | $uid = ($GLOBALS['xoopsUser'] instanceof XoopsUser) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
344 | $karmaHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Karma'); |
||
345 | $user_karma = $karmaHandler->getUserKarma(); |
||
346 | |||
347 | $post = array(); |
||
348 | $post['attachment'] = false; |
||
349 | $post_text = newbb_displayTarea($this->vars['post_text']['value'], $this->getVar('dohtml'), $this->getVar('dosmiley'), $this->getVar('doxcode'), $this->getVar('doimage'), $this->getVar('dobr')); |
||
350 | if (newbb_isAdmin($this->getVar('forum_id')) or $this->checkIdentity()) { |
||
351 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
352 | } elseif ($GLOBALS['xoopsModuleConfig']['enable_karma'] && $this->getVar('post_karma') > $user_karma) { |
||
353 | $post['text'] = sprintf(_MD_KARMA_REQUIREMENT, $user_karma, $this->getVar('post_karma')); |
||
354 | } elseif ($GLOBALS['xoopsModuleConfig']['allow_require_reply'] && $this->getVar('require_reply') |
||
355 | && (!$uid |
||
356 | || !isset($viewtopic_users[$uid])) |
||
357 | ) { |
||
358 | $post['text'] = _MD_REPLY_REQUIREMENT; |
||
359 | } else { |
||
360 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
361 | } |
||
362 | $memberHandler = xoops_getHandler('member'); |
||
363 | $eachposter = $memberHandler->getUser($this->getVar('uid')); |
||
364 | if (is_object($eachposter) && $eachposter->isActive()) { |
||
365 | if ($GLOBALS['xoopsModuleConfig']['show_realname'] && $eachposter->getVar('name')) { |
||
366 | $post['author'] = $eachposter->getVar('name'); |
||
367 | } else { |
||
368 | $post['author'] = $eachposter->getVar('uname'); |
||
369 | } |
||
370 | unset($eachposter); |
||
371 | } else { |
||
372 | $post['author'] = $this->getVar('poster_name') ?: $GLOBALS['xoopsConfig']['anonymous']; |
||
373 | } |
||
374 | |||
375 | $post['subject'] = newbb_htmlspecialchars($this->vars['subject']['value']); |
||
376 | $post['date'] = $this->getVar('post_time'); |
||
377 | |||
378 | return $post; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @return bool |
||
383 | */ |
||
384 | public function isTopic() |
||
385 | { |
||
386 | return !$this->getVar('pid'); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param string $action_tag |
||
391 | * @return bool |
||
392 | */ |
||
393 | public function checkTimelimit($action_tag = 'edit_timelimit') |
||
394 | { |
||
395 | $newbb_config = newbb_load_config(); |
||
396 | if (empty($newbb_config['edit_timelimit'])) { |
||
397 | return true; |
||
398 | } |
||
399 | |||
400 | return ($this->getVar('post_time') > time() - $newbb_config[$action_tag] * 60); |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @param int $uid |
||
405 | * @return bool |
||
406 | */ |
||
407 | public function checkIdentity($uid = -1) |
||
408 | { |
||
409 | $uid = ($uid > -1) ? $uid : (($GLOBALS['xoopsUser'] instanceof XoopsUser) ? $GLOBALS['xoopsUser']->getVar('uid') : 0); |
||
410 | if ($this->getVar('uid') > 0) { |
||
411 | $user_ok = ($uid === $this->getVar('uid')) ? true : false; |
||
412 | } else { |
||
413 | static $user_ip; |
||
414 | if (!isset($user_ip)) { |
||
415 | $user_ip = XoopsUserUtility::getIP(); |
||
416 | } |
||
417 | $user_ok = ($user_ip === $this->getVar('poster_ip')) ? true : false; |
||
418 | } |
||
419 | |||
420 | return $user_ok; |
||
421 | } |
||
422 | |||
423 | // TODO: cleaning up and merge with post hanldings in viewpost.php |
||
424 | /** |
||
425 | * @param $isadmin |
||
426 | * @return array |
||
427 | */ |
||
428 | public function showPost($isadmin) |
||
429 | { |
||
430 | global $myts; |
||
431 | global $forumUrl, $forumImage; |
||
432 | global $viewtopic_users, $viewtopic_posters, $forum_obj, $topic_obj, $online, $user_karma, $viewmode, $order, $start, $total_posts, $topic_status; |
||
433 | static $post_NO = 0; |
||
434 | static $name_anonymous; |
||
435 | |||
436 | if (!isset($name_anonymous)) { |
||
437 | $name_anonymous = $myts->htmlSpecialChars($GLOBALS['xoopsConfig']['anonymous']); |
||
438 | } |
||
439 | |||
440 | mod_loadFunctions('time', 'newbb'); |
||
441 | mod_loadFunctions('render', 'newbb'); |
||
442 | mod_loadFunctions('text', 'newbb'); // irmtfan add text functions |
||
443 | |||
444 | $post_id = $this->getVar('post_id'); |
||
445 | $topic_id = $this->getVar('topic_id'); |
||
446 | $forum_id = $this->getVar('forum_id'); |
||
447 | |||
448 | $query_vars = array('status', 'order', 'start', 'mode', 'viewmode'); |
||
449 | $query_array = array(); |
||
450 | $query_array['topic_id'] = "topic_id={$topic_id}"; |
||
451 | foreach ($query_vars as $var) { |
||
452 | if (!empty($_GET[$var])) { |
||
453 | $query_array[$var] = "{$var}={$_GET[$var]}"; |
||
454 | } |
||
455 | } |
||
456 | $page_query = htmlspecialchars(implode('&', array_values($query_array))); |
||
457 | |||
458 | $uid = ($GLOBALS['xoopsUser'] instanceof XoopsUser) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
459 | |||
460 | ++$post_NO; |
||
461 | if (strtolower($order) === 'desc') { |
||
462 | $post_no = $total_posts - ($start + $post_NO) + 1; |
||
463 | } else { |
||
464 | $post_no = $start + $post_NO; |
||
465 | } |
||
466 | |||
467 | if ($isadmin || $this->checkIdentity()) { |
||
468 | $post_text = $this->getVar('post_text'); |
||
469 | $post_attachment = $this->displayAttachment(); |
||
470 | } elseif ($GLOBALS['xoopsModuleConfig']['enable_karma'] && $this->getVar('post_karma') > $user_karma) { |
||
471 | $post_text = "<div class='karma'>" . sprintf(_MD_KARMA_REQUIREMENT, $user_karma, $this->getVar('post_karma')) . '</div>'; |
||
472 | $post_attachment = ''; |
||
473 | } elseif ($GLOBALS['xoopsModuleConfig']['allow_require_reply'] && $this->getVar('require_reply') |
||
474 | && (!$uid |
||
475 | || !in_array($uid, $viewtopic_posters)) |
||
476 | ) { |
||
477 | $post_text = "<div class='karma'>" . _MD_REPLY_REQUIREMENT . "</div>\n"; |
||
478 | $post_attachment = ''; |
||
479 | } else { |
||
480 | $post_text = $this->getVar('post_text'); |
||
481 | $post_attachment = $this->displayAttachment(); |
||
482 | } |
||
483 | // START irmtfan add highlight feature |
||
484 | // Hightlighting searched words |
||
485 | $post_title = $this->getVar('subject'); |
||
486 | if (isset($_GET['keywords']) && !empty($_GET['keywords'])) { |
||
487 | $keywords = $myts->htmlSpecialChars(trim(urldecode($_GET['keywords']))); |
||
488 | $post_text = newbb_highlightText($post_text, $keywords); |
||
489 | $post_title = newbb_highlightText($post_title, $keywords); |
||
490 | } |
||
491 | // END irmtfan add highlight feature |
||
492 | if (isset($viewtopic_users[$this->getVar('uid')])) { |
||
493 | $poster = $viewtopic_users[$this->getVar('uid')]; |
||
494 | } else { |
||
495 | $name = ($post_name = $this->getVar('poster_name')) ? $post_name : $name_anonymous; |
||
496 | $poster = array( |
||
497 | 'poster_uid' => 0, |
||
498 | 'name' => $name, |
||
499 | 'link' => $name |
||
500 | ); |
||
501 | } |
||
502 | |||
503 | if ($posticon = $this->getVar('icon')) { |
||
504 | $post_image = "<a name='{$post_id}'><img src='" . $GLOBALS['xoops']->url("images/subject/{$posticon}") . "' alt='' /></a>"; |
||
505 | } else { |
||
506 | $post_image = "<a name='{$post_id}'><img src='" . $GLOBALS['xoops']->url('images/icons/posticon.gif') . "' alt='' /></a>"; |
||
507 | } |
||
508 | |||
509 | $thread_buttons = array(); |
||
510 | $mod_buttons = array(); |
||
511 | |||
512 | if ($isadmin |
||
513 | && (($GLOBALS['xoopsUser'] instanceof XoopsUser) |
||
514 | && $GLOBALS['xoopsUser']->getVar('uid') !== $this->getVar('uid')) |
||
515 | && ($this->getVar('uid') > 0) |
||
516 | ) { |
||
517 | $mod_buttons['bann']['image'] = newbb_displayImage('p_bann', _MD_SUSPEND_MANAGEMENT); |
||
518 | $mod_buttons['bann']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/moderate.php?forum={$forum_id}&fuid=" . $this->getVar('uid')); |
||
519 | $mod_buttons['bann']['name'] = _MD_SUSPEND_MANAGEMENT; |
||
520 | $thread_buttons['bann']['image'] = newbb_displayImage('p_bann', _MD_SUSPEND_MANAGEMENT); |
||
521 | $thread_buttons['bann']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/moderate.php?forum={$forum_id}&fuid=" . $this->getVar('uid')); |
||
522 | $thread_buttons['bann']['name'] = _MD_SUSPEND_MANAGEMENT; |
||
523 | } |
||
524 | |||
525 | if ($GLOBALS['xoopsModuleConfig']['enable_permcheck']) { |
||
526 | /** @var NewbbTopicHandler $topicHandler */ |
||
527 | $topicHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Topic'); |
||
528 | $topic_status = $topic_obj->getVar('topic_status'); |
||
529 | if ($topicHandler->getPermission($forum_id, $topic_status, 'edit')) { |
||
530 | $edit_ok = ($isadmin || ($this->checkIdentity() && $this->checkTimelimit('edit_timelimit'))); |
||
531 | if ($edit_ok) { |
||
532 | $thread_buttons['edit']['image'] = newbb_displayImage('p_edit', _EDIT); |
||
533 | $thread_buttons['edit']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/edit.php?{$page_query}"); |
||
534 | $thread_buttons['edit']['name'] = _EDIT; |
||
535 | $mod_buttons['edit']['image'] = newbb_displayImage('p_edit', _EDIT); |
||
536 | $mod_buttons['edit']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/edit.php?{$page_query}"); |
||
537 | $mod_buttons['edit']['name'] = _EDIT; |
||
538 | } |
||
539 | } |
||
540 | |||
541 | if ($topicHandler->getPermission($forum_id, $topic_status, 'delete')) { |
||
542 | $delete_ok = ($isadmin || ($this->checkIdentity() && $this->checkTimelimit('delete_timelimit'))); |
||
543 | |||
544 | if ($delete_ok) { |
||
545 | $thread_buttons['delete']['image'] = newbb_displayImage('p_delete', _DELETE); |
||
546 | $thread_buttons['delete']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/delete.php?{$page_query}"); |
||
547 | $thread_buttons['delete']['name'] = _DELETE; |
||
548 | $mod_buttons['delete']['image'] = newbb_displayImage('p_delete', _DELETE); |
||
549 | $mod_buttons['delete']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/delete.php?{$page_query}"); |
||
550 | $mod_buttons['delete']['name'] = _DELETE; |
||
551 | } |
||
552 | } |
||
553 | if ($topicHandler->getPermission($forum_id, $topic_status, 'reply')) { |
||
554 | $thread_buttons['reply']['image'] = newbb_displayImage('p_reply', _MD_REPLY); |
||
555 | $thread_buttons['reply']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/reply.php?{$page_query}"); |
||
556 | $thread_buttons['reply']['name'] = _MD_REPLY; |
||
557 | |||
558 | $thread_buttons['quote']['image'] = newbb_displayImage('p_quote', _MD_QUOTE); |
||
559 | $thread_buttons['quote']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/reply.php?{$page_query}&quotedac=1"); |
||
560 | $thread_buttons['quote']['name'] = _MD_QUOTE; |
||
561 | } |
||
562 | } else { |
||
563 | $mod_buttons['edit']['image'] = newbb_displayImage('p_edit', _EDIT); |
||
564 | $mod_buttons['edit']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/edit.php?{$page_query}"); |
||
565 | $mod_buttons['edit']['name'] = _EDIT; |
||
566 | |||
567 | $mod_buttons['delete']['image'] = newbb_displayImage('p_delete', _DELETE); |
||
568 | $mod_buttons['delete']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/delete.php?{$page_query}"); |
||
569 | $mod_buttons['delete']['name'] = _DELETE; |
||
570 | |||
571 | $thread_buttons['reply']['image'] = newbb_displayImage('p_reply', _MD_REPLY); |
||
572 | $thread_buttons['reply']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/reply.php?{$page_query}"); |
||
573 | $thread_buttons['reply']['name'] = _MD_REPLY; |
||
574 | } |
||
575 | |||
576 | if (!$isadmin && $GLOBALS['xoopsModuleConfig']['reportmod_enabled']) { |
||
577 | $thread_buttons['report']['image'] = newbb_displayImage('p_report', _MD_REPORT); |
||
578 | $thread_buttons['report']['link'] = $GLOBALS['xoops']->url('modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/report.php?{$page_query}"); |
||
579 | $thread_buttons['report']['name'] = _MD_REPORT; |
||
580 | } |
||
581 | |||
582 | $thread_action = array(); |
||
583 | // irmtfan add pdf permission |
||
584 | if (file_exists($GLOBALS['xoops']->path('Frameworks/tcpdf/tcpdf.php')) |
||
585 | && $topicHandler->getPermission($forum_id, $topic_status, 'pdf') |
||
586 | ) { |
||
587 | $thread_action['pdf']['image'] = newbb_displayImage('pdf', _MD_PDF); |
||
588 | $thread_action['pdf']['link'] = $GLOBALS['xoops']->url('modules/newbb/makepdf.php?type=post&pageid=0'); |
||
589 | $thread_action['pdf']['name'] = _MD_PDF; |
||
590 | $thread_action['pdf']['target'] = '_blank'; |
||
591 | } |
||
592 | // irmtfan add print permission |
||
593 | if ($topicHandler->getPermission($forum_id, $topic_status, 'print')) { |
||
594 | $thread_action['print']['image'] = newbb_displayImage('printer', _MD_PRINT); |
||
595 | $thread_action['print']['link'] = $GLOBALS['xoops']->url("modules/newbb/print.php?form=2&forum={$forum_id}&topic_id={$topic_id}"); |
||
596 | $thread_action['print']['name'] = _MD_PRINT; |
||
597 | $thread_action['print']['target'] = '_blank'; |
||
598 | } |
||
599 | |||
600 | if ($GLOBALS['xoopsModuleConfig']['show_sociallinks']) { |
||
601 | $full_title = $this->getVar('subject'); |
||
602 | $clean_title = preg_replace('/[^A-Za-z0-9-]+/', '+', $this->getVar('subject')); |
||
603 | $full_link = $GLOBALS['xoops']->url("modules/newbb/viewtopic.php?post_id={$post_id}"); |
||
604 | |||
605 | $thread_action['social_twitter']['image'] = newbb_displayImage('twitter', _MD_SHARE_TWITTER); |
||
606 | $thread_action['social_twitter']['link'] = "http://twitter.com/share?text={$clean_title}&url={$full_link}"; |
||
607 | $thread_action['social_twitter']['name'] = _MD_SHARE_TWITTER; |
||
608 | $thread_action['social_twitter']['target'] = '_blank'; |
||
609 | |||
610 | $thread_action['social_facebook']['image'] = newbb_displayImage('facebook', _MD_SHARE_FACEBOOK); |
||
611 | $thread_action['social_facebook']['link'] = "http://www.facebook.com/sharer.php?u={$full_link}"; |
||
612 | $thread_action['social_facebook']['name'] = _MD_SHARE_FACEBOOK; |
||
613 | $thread_action['social_facebook']['target'] = '_blank'; |
||
614 | |||
615 | $thread_action['social_gplus']['image'] = newbb_displayImage('googleplus', _MD_SHARE_GOOGLEPLUS); |
||
616 | $thread_action['social_gplus']['link'] = "https://plusone.google.com/_/+1/confirm?hl=en&url={$full_link}"; |
||
617 | $thread_action['social_gplus']['name'] = _MD_SHARE_GOOGLEPLUS; |
||
618 | $thread_action['social_gplus']['target'] = '_blank'; |
||
619 | |||
620 | $thread_action['social_linkedin']['image'] = newbb_displayImage('linkedin', _MD_SHARE_LINKEDIN); |
||
621 | $thread_action['social_linkedin']['link'] = "http://www.linkedin.com/shareArticle?mini=true&title={$full_title}&url={$full_link}"; |
||
622 | $thread_action['social_linkedin']['name'] = _MD_SHARE_LINKEDIN; |
||
623 | $thread_action['social_linkedin']['target'] = '_blank'; |
||
624 | |||
625 | $thread_action['social_delicious']['image'] = newbb_displayImage('delicious', _MD_SHARE_DELICIOUS); |
||
626 | $thread_action['social_delicious']['link'] = "http://del.icio.us/post?title={$full_title}&url={$full_link}"; |
||
627 | $thread_action['social_delicious']['name'] = _MD_SHARE_DELICIOUS; |
||
628 | $thread_action['social_delicious']['target'] = '_blank'; |
||
629 | |||
630 | $thread_action['social_digg']['image'] = newbb_displayImage('digg', _MD_SHARE_DIGG); |
||
631 | $thread_action['social_digg']['link'] = "http://digg.com/submit?phase=2&title={$full_title}&url={$full_link}"; |
||
632 | $thread_action['social_digg']['name'] = _MD_SHARE_DIGG; |
||
633 | $thread_action['social_digg']['target'] = '_blank'; |
||
634 | |||
635 | $thread_action['social_reddit']['image'] = newbb_displayImage('reddit', _MD_SHARE_REDDIT); |
||
636 | $thread_action['social_reddit']['link'] = "http://reddit.com/submit?title={$full_title}&url={$full_link}"; |
||
637 | $thread_action['social_reddit']['name'] = _MD_SHARE_REDDIT; |
||
638 | $thread_action['social_reddit']['target'] = '_blank'; |
||
639 | |||
640 | $thread_action['social_wong']['image'] = newbb_displayImage('wong', _MD_SHARE_MRWONG); |
||
641 | $thread_action['social_wong']['link'] = "http://www.mister-wong.de/index.php?action=addurl&bm_url=$full_link}"; |
||
642 | $thread_action['social_wong']['name'] = _MD_SHARE_MRWONG; |
||
643 | $thread_action['social_wong']['target'] = '_blank'; |
||
644 | } |
||
645 | |||
646 | $post = array( |
||
647 | 'post_id' => $post_id, |
||
648 | 'post_parent_id' => $this->getVar('pid'), |
||
649 | 'post_date' => newbb_formatTimestamp($this->getVar('post_time')), |
||
650 | 'post_image' => $post_image, |
||
651 | 'post_title' => $post_title, // irmtfan $post_title to add highlight keywords |
||
652 | 'post_text' => $post_text, |
||
653 | 'post_attachment' => $post_attachment, |
||
654 | 'post_edit' => $this->displayPostEdit(), |
||
655 | 'post_no' => $post_no, |
||
656 | 'post_signature' => $this->getVar('attachsig') ? @$poster['signature'] : '', |
||
657 | 'poster_ip' => ($isadmin |
||
658 | && $GLOBALS['xoopsModuleConfig']['show_ip']) ? long2ip($this->getVar('poster_ip')) : '', |
||
659 | 'thread_action' => $thread_action, |
||
660 | 'thread_buttons' => $thread_buttons, |
||
661 | 'mod_buttons' => $mod_buttons, |
||
662 | 'poster' => $poster, |
||
663 | 'post_permalink' => "<a href='" . $GLOBALS['xoops']->url('/modules/' . $GLOBALS['xoopsModule']->getVar('dirname') . "/viewtopic.php?post_id={$post_id}") . "'></a>" |
||
664 | ); |
||
665 | |||
666 | unset($thread_buttons, $mod_buttons, $eachposter); |
||
667 | |||
668 | return $post; |
||
669 | } |
||
670 | } |
||
671 | |||
1256 |