Total Complexity | 120 |
Total Lines | 668 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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.
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 declare(strict_types=1); |
||
30 | class Post extends \XoopsObject |
||
31 | { |
||
32 | private $post_id; |
||
33 | private $topic_id; |
||
34 | private $forum_id; |
||
35 | private $post_time; |
||
36 | private $poster_ip; |
||
37 | private $poster_name; |
||
38 | private $subject; |
||
39 | private $pid; |
||
40 | private $dohtml; |
||
41 | private $dosmiley; |
||
42 | private $doxcode; |
||
43 | private $doimage; |
||
44 | private $dobr; |
||
45 | private $uid; |
||
46 | private $icon; |
||
47 | private $attachsig; |
||
48 | private $approved; |
||
49 | private $post_karma; |
||
50 | private $require_reply; |
||
51 | private $attachment; |
||
52 | private $post_text; |
||
53 | private $post_edit; |
||
54 | |||
55 | //class Post extends \XoopsObject { |
||
56 | public $attachment_array = []; |
||
57 | |||
58 | /** |
||
59 | * Post constructor. |
||
60 | */ |
||
61 | public function __construct() |
||
62 | { |
||
63 | parent::__construct('bb_posts'); |
||
64 | $this->initVar('post_id', \XOBJ_DTYPE_INT); |
||
65 | $this->initVar('topic_id', \XOBJ_DTYPE_INT, 0, true); |
||
66 | $this->initVar('forum_id', \XOBJ_DTYPE_INT, 0, true); |
||
67 | $this->initVar('post_time', \XOBJ_DTYPE_INT, 0, true); |
||
68 | $this->initVar('poster_ip', \XOBJ_DTYPE_INT, 0); |
||
69 | $this->initVar('poster_name', \XOBJ_DTYPE_TXTBOX, ''); |
||
70 | $this->initVar('subject', \XOBJ_DTYPE_TXTBOX, '', true); |
||
71 | $this->initVar('pid', \XOBJ_DTYPE_INT, 0); |
||
72 | $this->initVar('dohtml', \XOBJ_DTYPE_INT, 0); |
||
73 | $this->initVar('dosmiley', \XOBJ_DTYPE_INT, 1); |
||
74 | $this->initVar('doxcode', \XOBJ_DTYPE_INT, 1); |
||
75 | $this->initVar('doimage', \XOBJ_DTYPE_INT, 1); |
||
76 | $this->initVar('dobr', \XOBJ_DTYPE_INT, 1); |
||
77 | $this->initVar('uid', \XOBJ_DTYPE_INT, 1); |
||
78 | $this->initVar('icon', \XOBJ_DTYPE_TXTBOX, ''); |
||
79 | $this->initVar('attachsig', \XOBJ_DTYPE_INT, 0); |
||
80 | $this->initVar('approved', \XOBJ_DTYPE_INT, 1); |
||
81 | $this->initVar('post_karma', \XOBJ_DTYPE_INT, 0); |
||
82 | $this->initVar('require_reply', \XOBJ_DTYPE_INT, 0); |
||
83 | $this->initVar('attachment', \XOBJ_DTYPE_TXTAREA, ''); |
||
84 | $this->initVar('post_text', \XOBJ_DTYPE_TXTAREA, ''); |
||
85 | $this->initVar('post_edit', \XOBJ_DTYPE_TXTAREA, ''); |
||
86 | } |
||
87 | |||
88 | // //////////////////////////////////////////////////////////////////////////////////// |
||
89 | // attachment functions TODO: there should be a file/attachment management class |
||
90 | |||
91 | /** |
||
92 | * @return array|mixed|null |
||
93 | */ |
||
94 | public function getAttachment() |
||
95 | { |
||
96 | if (\count($this->attachment_array)) { |
||
97 | return $this->attachment_array; |
||
98 | } |
||
99 | $attachment = $this->getVar('attachment'); |
||
100 | if (empty($attachment)) { |
||
101 | $this->attachment_array = null; |
||
102 | } else { |
||
103 | $this->attachment_array = @\unserialize(\base64_decode($attachment, true)); |
||
104 | } |
||
105 | |||
106 | return $this->attachment_array; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param $attach_key |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function incrementDownload($attach_key) |
||
114 | { |
||
115 | if (!$attach_key) { |
||
116 | return false; |
||
117 | } |
||
118 | $this->attachment_array[(string)$attach_key]['num_download']++; |
||
119 | |||
120 | return $this->attachment_array[(string)$attach_key]['num_download']; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @return bool |
||
125 | */ |
||
126 | public function saveAttachment() |
||
127 | { |
||
128 | $attachment_save = ''; |
||
129 | if ($this->attachment_array && \is_array($this->attachment_array)) { |
||
130 | $attachment_save = \base64_encode(\serialize($this->attachment_array)); |
||
131 | } |
||
132 | $this->setVar('attachment', $attachment_save); |
||
133 | $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('bb_posts') . ' SET attachment=' . $GLOBALS['xoopsDB']->quoteString($attachment_save) . ' WHERE post_id = ' . $this->getVar('post_id'); |
||
134 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
135 | //xoops_error($GLOBALS["xoopsDB"]->error()); |
||
136 | return false; |
||
137 | } |
||
138 | |||
139 | return true; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * @param null $attach_array |
||
144 | * @return bool |
||
145 | */ |
||
146 | public function deleteAttachment($attach_array = null) |
||
147 | { |
||
148 | $attach_old = $this->getAttachment(); |
||
149 | if (!\is_array($attach_old) || \count($attach_old) < 1) { |
||
150 | return true; |
||
151 | } |
||
152 | $this->attachment_array = []; |
||
153 | |||
154 | if (null === $attach_array) { |
||
155 | $attach_array = \array_keys($attach_old); |
||
156 | } // to delete all! |
||
157 | if (!\is_array($attach_array)) { |
||
158 | $attach_array = [$attach_array]; |
||
159 | } |
||
160 | |||
161 | foreach ($attach_old as $key => $attach) { |
||
162 | if (\in_array($key, $attach_array, true)) { |
||
163 | @\unlink(XOOPS_ROOT_PATH . '/' . $GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/' . $attach['name_saved']); |
||
164 | @\unlink(XOOPS_ROOT_PATH . '/' . $GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/thumbs/' . $attach['name_saved']); // delete thumbnails |
||
165 | continue; |
||
166 | } |
||
167 | $this->attachment_array[$key] = $attach; |
||
168 | } |
||
169 | $attachment_save = ''; |
||
170 | if ($this->attachment_array && \is_array($this->attachment_array)) { |
||
171 | $attachment_save = \base64_encode(\serialize($this->attachment_array)); |
||
172 | } |
||
173 | $this->setVar('attachment', $attachment_save); |
||
174 | |||
175 | return true; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param string $name_saved |
||
180 | * @param string $name_display |
||
181 | * @param string $mimetype |
||
182 | * @param int $num_download |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function setAttachment($name_saved = '', $name_display = '', $mimetype = '', $num_download = 0) |
||
186 | { |
||
187 | static $counter = 0; |
||
188 | $this->attachment_array = $this->getAttachment(); |
||
189 | if ($name_saved) { |
||
190 | $key = (string)(\time() + $counter++); |
||
191 | $this->attachment_array[$key] = [ |
||
192 | 'name_saved' => $name_saved, |
||
193 | 'name_display' => $name_display ?? $name_saved, |
||
194 | 'mimetype' => $mimetype, |
||
195 | 'num_download' => isset($num_download) ? (int)$num_download : 0, |
||
196 | ]; |
||
197 | } |
||
198 | $attachment_save = null; |
||
199 | if (\is_array($this->attachment_array)) { |
||
200 | $attachment_save = \base64_encode(\serialize($this->attachment_array)); |
||
201 | } |
||
202 | $this->setVar('attachment', $attachment_save); |
||
203 | |||
204 | return true; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * TODO: refactor |
||
209 | * @param bool $asSource |
||
210 | * @return string |
||
211 | */ |
||
212 | public function displayAttachment($asSource = false) |
||
213 | { |
||
214 | $post_attachment = ''; |
||
215 | $attachments = $this->getAttachment(); |
||
216 | if ($attachments && \is_array($attachments)) { |
||
217 | $iconHandler = newbb_getIconHandler(); |
||
218 | $mime_path = $iconHandler->getPath('mime'); |
||
219 | require_once $GLOBALS['xoops']->path('modules/' . $GLOBALS['xoopsModule']->getVar('dirname', 'n') . '/include/functions.image.php'); |
||
220 | $image_extensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp']; // need improve !!! |
||
221 | $post_attachment .= '<br><strong>' . _MD_ATTACHMENT . '</strong>:'; |
||
222 | $post_attachment .= "<div style='margin: 1em 0; border-top: 1px solid;'></div>\n"; |
||
223 | // $post_attachment .= '<br><hr style="height: 1px;" noshade="noshade"><br>'; |
||
224 | foreach ($attachments as $key => $att) { |
||
225 | $file_extension = \ltrim(mb_strrchr($att['name_saved'], '.'), '.'); |
||
226 | $filetype = $file_extension; |
||
227 | if (\file_exists($GLOBALS['xoops']->path("{$mime_path}/{$filetype}.gif"))) { |
||
228 | $icon_filetype = $GLOBALS['xoops']->url("{$mime_path}/{$filetype}.gif"); |
||
229 | } else { |
||
230 | $icon_filetype = $GLOBALS['xoops']->url("{$mime_path}/unknown.gif"); |
||
231 | } |
||
232 | $file_size = @\filesize($GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/' . $att['name_saved'])); |
||
233 | $file_size = \number_format($file_size / 1024, 2) . ' KB'; |
||
234 | if ($GLOBALS['xoopsModuleConfig']['media_allowed'] |
||
235 | && \in_array(mb_strtolower($file_extension), $image_extensions, true)) { |
||
236 | $post_attachment .= '<br><img src="' . $icon_filetype . '" alt="' . $filetype . '"><strong> ' . $att['name_display'] . '</strong> <small>(' . $file_size . ')</small>'; |
||
237 | $post_attachment .= '<br>' . newbb_attachmentImage($att['name_saved']); |
||
238 | $isDisplayed = true; |
||
239 | } else { |
||
240 | if (empty($GLOBALS['xoopsModuleConfig']['show_userattach'])) { |
||
241 | $post_attachment .= "<a href='" |
||
242 | . $GLOBALS['xoops']->url('/modules/' . $GLOBALS['xoopsModule']->getVar('dirname', 'n') . "/dl_attachment.php?attachid={$key}&post_id=" . $this->getVar('post_id')) |
||
243 | . "'> <img src='{$icon_filetype}' alt='{$filetype}'> {$att['name_display']}</a> " |
||
244 | . _MD_FILESIZE |
||
245 | . ": {$file_size}; " |
||
246 | . _MD_HITS |
||
247 | . ": {$att['num_download']}"; |
||
248 | } elseif (($GLOBALS['xoopsUser'] instanceof \XoopsUser) && $GLOBALS['xoopsUser']->uid() > 0 |
||
249 | && $GLOBALS['xoopsUser']->isActive()) { |
||
250 | $post_attachment .= "<a href='" |
||
251 | . $GLOBALS['xoops']->url('/modules/' . $GLOBALS['xoopsModule']->getVar('dirname', 'n') . "/dl_attachment.php?attachid={$key}&post_id=" . $this->getVar('post_id')) |
||
252 | . "'> <img src='" |
||
253 | . $icon_filetype |
||
254 | . "' alt='{$filetype}'> {$att['name_display']}</a> " |
||
255 | . _MD_FILESIZE |
||
256 | . ": {$file_size}; " |
||
257 | . _MD_HITS |
||
258 | . ": {$att['num_download']}"; |
||
259 | } else { |
||
260 | $post_attachment .= _MD_NEWBB_SEENOTGUEST; |
||
261 | } |
||
262 | } |
||
263 | $post_attachment .= '<br>'; |
||
264 | } |
||
265 | } |
||
266 | |||
267 | return $post_attachment; |
||
268 | } |
||
269 | |||
270 | // attachment functions |
||
271 | // //////////////////////////////////////////////////////////////////////////////////// |
||
272 | |||
273 | /** |
||
274 | * @param string $poster_name |
||
275 | * @param string $post_editmsg |
||
276 | * @return bool |
||
277 | */ |
||
278 | public function setPostEdit($poster_name = '', $post_editmsg = '') |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @return bool|string |
||
314 | */ |
||
315 | public function displayPostEdit() |
||
316 | { |
||
317 | global $myts; |
||
318 | |||
319 | if (empty($GLOBALS['xoopsModuleConfig']['recordedit_timelimit'])) { |
||
320 | return false; |
||
321 | } |
||
322 | |||
323 | $post_edit = ''; |
||
324 | $post_edits = $this->getVar('post_edit'); |
||
325 | if (!empty($post_edits)) { |
||
326 | $post_edits = \unserialize(\base64_decode($post_edits, true)); |
||
327 | } |
||
328 | if (!isset($post_edits) || !\is_array($post_edits)) { |
||
329 | $post_edits = []; |
||
330 | } |
||
331 | if ($post_edits && \is_array($post_edits)) { |
||
332 | foreach ($post_edits as $postedit) { |
||
333 | $edit_time = (int)$postedit['edit_time']; |
||
334 | $edit_user = ($postedit['edit_user']); |
||
335 | $edit_msg = !empty($postedit['edit_msg']) ? ($postedit['edit_msg']) : ''; |
||
336 | // Start irmtfan add option to do only the latest edit when do_latestedit=0 (Alfred) |
||
337 | if (empty($GLOBALS['xoopsModuleConfig']['do_latestedit'])) { |
||
338 | $post_edit = ''; |
||
339 | } |
||
340 | // End irmtfan add option to do only the latest edit when do_latestedit=0 (Alfred) |
||
341 | // START hacked by irmtfan |
||
342 | // display/save all edit records. |
||
343 | $post_edit .= _MD_EDITEDBY . ' ' . $edit_user . ' ' . _MD_ON . ' ' . newbb_formatTimestamp($edit_time) . '<br>'; |
||
344 | // if reason is not empty |
||
345 | if ('' !== $edit_msg) { |
||
346 | $post_edit .= \_MD_EDITEDMSG . ' ' . $edit_msg . '<br>'; |
||
347 | } |
||
348 | // START hacked by irmtfan |
||
349 | } |
||
350 | } |
||
351 | |||
352 | return $post_edit; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @return array |
||
357 | */ |
||
358 | public function &getPostBody() |
||
359 | { |
||
360 | global $myts; |
||
361 | $GLOBALS['xoopsModuleConfig'] = newbb_load_config(); // irmtfan load all newbb configs - newbb config in blocks activated in some modules like profile |
||
362 | // mod_loadFunctions('user', 'newbb'); |
||
363 | // mod_loadFunctions('render', 'newbb'); |
||
364 | require_once \dirname(__DIR__) . '/include/functions.user.php'; |
||
365 | require_once \dirname(__DIR__) . '/include/functions.render.php'; |
||
366 | |||
367 | $uid = ($GLOBALS['xoopsUser'] instanceof \XoopsUser) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
368 | $karmaHandler = Newbb\Helper::getInstance()->getHandler('Karma'); |
||
369 | $user_karma = $karmaHandler->getUserKarma(); |
||
370 | |||
371 | $post = []; |
||
372 | $post['attachment'] = false; |
||
373 | $post_text = &newbb_displayTarea($this->vars['post_text']['value'], $this->getVar('dohtml'), $this->getVar('dosmiley'), $this->getVar('doxcode'), $this->getVar('doimage'), $this->getVar('dobr')); |
||
374 | if (newbb_isAdmin($this->getVar('forum_id')) || $this->checkIdentity()) { |
||
375 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
376 | } elseif ($GLOBALS['xoopsModuleConfig']['enable_karma'] && $this->getVar('post_karma') > $user_karma) { |
||
377 | $post['text'] = \sprintf(_MD_KARMA_REQUIREMENT, $user_karma, $this->getVar('post_karma')); |
||
378 | } elseif ($GLOBALS['xoopsModuleConfig']['allow_require_reply'] && $this->getVar('require_reply') |
||
379 | && (!$uid || !isset($viewtopic_users[$uid]))) { |
||
380 | $post['text'] = _MD_REPLY_REQUIREMENT; |
||
381 | } else { |
||
382 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
383 | } |
||
384 | $memberHandler = \xoops_getHandler('member'); |
||
385 | $eachposter = $memberHandler->getUser($this->getVar('uid')); |
||
386 | if (\is_object($eachposter) && $eachposter->isActive()) { |
||
387 | if ($GLOBALS['xoopsModuleConfig']['show_realname'] && $eachposter->getVar('name')) { |
||
388 | $post['author'] = $eachposter->getVar('name'); |
||
389 | } else { |
||
390 | $post['author'] = $eachposter->getVar('uname'); |
||
391 | } |
||
392 | unset($eachposter); |
||
393 | } else { |
||
394 | $post['author'] = $this->getVar('poster_name') ?: $GLOBALS['xoopsConfig']['anonymous']; |
||
395 | } |
||
396 | |||
397 | $post['subject'] = newbb_htmlspecialchars($this->vars['subject']['value']); |
||
398 | $post['date'] = $this->getVar('post_time'); |
||
399 | |||
400 | return $post; |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * @return bool |
||
405 | */ |
||
406 | public function isTopic() |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * @param string $action_tag |
||
413 | * @return bool |
||
414 | */ |
||
415 | public function checkTimelimit($action_tag = 'edit_timelimit') |
||
416 | { |
||
417 | $newbb_config = newbb_load_config(); |
||
418 | if (empty($newbb_config['edit_timelimit'])) { |
||
419 | return true; |
||
420 | } |
||
421 | |||
422 | return ($this->getVar('post_time') > \time() - $newbb_config[$action_tag] * 60); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @param int $uid |
||
427 | * @return bool |
||
428 | */ |
||
429 | public function checkIdentity($uid = -1) |
||
430 | { |
||
431 | // $uid = ($uid > -1) ? $uid : (($GLOBALS['xoopsUser'] instanceof \XoopsUser) ? $GLOBALS['xoopsUser']->getVar('uid') : 0); |
||
432 | if ($uid < 0 && $GLOBALS['xoopsUser'] instanceof \XoopsUser) { |
||
433 | $uid = $GLOBALS['xoopsUser']->getVar('uid'); |
||
434 | } else { |
||
435 | $uid = 0; |
||
436 | } |
||
437 | if ($this->getVar('uid') > 0) { |
||
438 | $user_ok = $uid === $this->getVar('uid'); |
||
439 | } else { |
||
440 | static $user_ip; |
||
441 | if (!isset($user_ip)) { |
||
442 | $user_ip = \XoopsUserUtility::getIP(); |
||
443 | } |
||
444 | $user_ok = $user_ip === $this->getVar('poster_ip'); |
||
445 | } |
||
446 | |||
447 | return $user_ok; |
||
448 | } |
||
449 | |||
450 | // TODO: cleaning up and merge with post hanldings in viewpost.php |
||
451 | |||
452 | /** |
||
453 | * @param $isadmin |
||
454 | * @return array |
||
455 | */ |
||
456 | public function showPost($isadmin) |
||
698 | } |
||
699 | } |
||
700 |