| Total Complexity | 122 |
| Total Lines | 692 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| 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 | public int $post_id; |
||
| 33 | public int $topic_id; |
||
| 34 | public int $forum_id; |
||
| 35 | public int $post_time; |
||
| 36 | // public $poster_ip ; |
||
| 37 | public string $poster_ip; |
||
| 38 | public string $poster_name; |
||
| 39 | public string $subject; |
||
| 40 | public int $pid; |
||
| 41 | public int $dohtml; |
||
| 42 | public int $dosmiley; |
||
| 43 | public int $doxcode; |
||
| 44 | public int $doimage; |
||
| 45 | public int $dobr; |
||
| 46 | public int $uid; |
||
| 47 | public string $icon; |
||
| 48 | public int $attachsig; |
||
| 49 | public int $approved; |
||
| 50 | public int $post_karma; |
||
| 51 | public int $require_reply; |
||
| 52 | public string $attachment; |
||
| 53 | public string $post_text; |
||
| 54 | public string $post_edit; |
||
| 55 | //class Post extends \XoopsObject { |
||
| 56 | private array $attachmentArray = []; |
||
| 57 | |||
| 58 | public function __construct() |
||
| 59 | { |
||
| 60 | parent::__construct(); |
||
| 61 | |||
| 62 | $this->initVar('post_id', \XOBJ_DTYPE_INT); |
||
| 63 | $this->initVar('topic_id', \XOBJ_DTYPE_INT, 0, true); |
||
| 64 | $this->initVar('forum_id', \XOBJ_DTYPE_INT, 0, true); |
||
| 65 | $this->initVar('post_time', \XOBJ_DTYPE_INT, 0, true); |
||
| 66 | // $this->initVar('poster_ip', XOBJ_DTYPE_INT, 0); |
||
| 67 | $this->initVar('poster_ip', \XOBJ_DTYPE_TXTBOX, ''); |
||
| 68 | $this->initVar('poster_name', \XOBJ_DTYPE_TXTBOX, ''); |
||
| 69 | $this->initVar('subject', \XOBJ_DTYPE_TXTBOX, '', true); |
||
| 70 | $this->initVar('pid', \XOBJ_DTYPE_INT, 0); |
||
| 71 | $this->initVar('dohtml', \XOBJ_DTYPE_INT, 0); |
||
| 72 | $this->initVar('dosmiley', \XOBJ_DTYPE_INT, 1); |
||
| 73 | $this->initVar('doxcode', \XOBJ_DTYPE_INT, 1); |
||
| 74 | $this->initVar('doimage', \XOBJ_DTYPE_INT, 1); |
||
| 75 | $this->initVar('dobr', \XOBJ_DTYPE_INT, 1); |
||
| 76 | $this->initVar('uid', \XOBJ_DTYPE_INT, 1); |
||
| 77 | $this->initVar('icon', \XOBJ_DTYPE_TXTBOX, ''); |
||
| 78 | $this->initVar('attachsig', \XOBJ_DTYPE_INT, 0); |
||
| 79 | $this->initVar('approved', \XOBJ_DTYPE_INT, 1); |
||
| 80 | $this->initVar('post_karma', \XOBJ_DTYPE_INT, 0); |
||
| 81 | $this->initVar('require_reply', \XOBJ_DTYPE_INT, 0); |
||
| 82 | $this->initVar('attachment', \XOBJ_DTYPE_TXTAREA, ''); |
||
| 83 | $this->initVar('post_text', \XOBJ_DTYPE_TXTAREA, ''); |
||
| 84 | $this->initVar('post_edit', \XOBJ_DTYPE_TXTAREA, ''); |
||
| 85 | } |
||
| 86 | |||
| 87 | // //////////////////////////////////////////////////////////////////////////////////// |
||
| 88 | // attachment functions TODO: there should be a file/attachment management class |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return array|mixed|null |
||
| 92 | */ |
||
| 93 | public function getAttachment() |
||
| 94 | { |
||
| 95 | if (\count($this->attachmentArray)) { |
||
| 96 | return $this->attachmentArray; |
||
| 97 | } |
||
| 98 | $attachment = $this->getVar('attachment'); |
||
| 99 | if (empty($attachment)) { |
||
| 100 | $this->attachmentArray = []; |
||
| 101 | } else { |
||
| 102 | $this->attachmentArray = @\unserialize(\base64_decode((string) $attachment, true)); |
||
| 103 | } |
||
| 104 | |||
| 105 | return $this->attachmentArray; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @param string $attachKey |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function incrementDownload(string $attachKey): bool |
||
| 113 | { |
||
| 114 | if (!$attachKey) { |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | $this->attachmentArray[(string)$attachKey]['numDownload']++; |
||
| 118 | |||
| 119 | return $this->attachmentArray[(string)$attachKey]['numDownload']; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | public function saveAttachment(): bool |
||
| 126 | { |
||
| 127 | $attachmentSave = ''; |
||
| 128 | if ($this->attachmentArray && \is_array($this->attachmentArray)) { |
||
|
|
|||
| 129 | $attachmentSave = \base64_encode(\serialize($this->attachmentArray)); |
||
| 130 | } |
||
| 131 | $this->setVar('attachment', $attachmentSave); |
||
| 132 | $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('newbb_posts') . ' SET attachment=' . $GLOBALS['xoopsDB']->quoteString($attachmentSave) . ' WHERE post_id = ' . $this->getVar('post_id'); |
||
| 133 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 134 | //xoops_error($GLOBALS['xoopsDB']->error()); |
||
| 135 | return false; |
||
| 136 | } |
||
| 137 | |||
| 138 | return true; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param array|null $attachArray |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function deleteAttachment(array $attachArray = null): bool |
||
| 146 | { |
||
| 147 | $attachOld = $this->getAttachment(); |
||
| 148 | if (!\is_array($attachOld) || \count($attachOld) < 1) { |
||
| 149 | return true; |
||
| 150 | } |
||
| 151 | $this->attachmentArray = []; |
||
| 152 | |||
| 153 | if (null === $attachArray) { |
||
| 154 | $attachArray = \array_keys($attachOld); |
||
| 155 | } // to delete all! |
||
| 156 | if (!\is_array($attachArray)) { |
||
| 157 | $attachArray = [$attachArray]; |
||
| 158 | } |
||
| 159 | |||
| 160 | foreach ($attachOld as $key => $attach) { |
||
| 161 | if (\in_array($key, $attachArray, true)) { |
||
| 162 | $file = $GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/' . $attach['name_saved']); |
||
| 163 | @\unlink($file); |
||
| 164 | $file = $GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/thumbs/' . $attach['name_saved']); // delete thumbnails |
||
| 165 | @\unlink($file); |
||
| 166 | continue; |
||
| 167 | } |
||
| 168 | $this->attachmentArray[$key] = $attach; |
||
| 169 | } |
||
| 170 | $attachmentSave = ''; |
||
| 171 | if ($this->attachmentArray && \is_array($this->attachmentArray)) { |
||
| 172 | $attachmentSave = \base64_encode(\serialize($this->attachmentArray)); |
||
| 173 | } |
||
| 174 | $this->setVar('attachment', $attachmentSave); |
||
| 175 | |||
| 176 | return true; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * @param string $name_saved |
||
| 181 | * @param string $nameDisplay |
||
| 182 | * @param string $mimetype |
||
| 183 | * @param int $numDownload |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function setAttachment(string $name_saved = '', string $nameDisplay = '', string $mimetype = '', int $numDownload = 0): bool |
||
| 187 | { |
||
| 188 | static $counter = 0; |
||
| 189 | $this->attachmentArray = $this->getAttachment(); |
||
| 190 | if ($name_saved) { |
||
| 191 | $key = (string)(\time() + $counter++); |
||
| 192 | $this->attachmentArray[$key] = [ |
||
| 193 | 'name_saved' => $name_saved, |
||
| 194 | // BigKev73 > without this change the nameDisplay will always get set to the $name_Saved, so in the forum it will show the on-disk filename instead of the name of the orginal file |
||
| 195 | //'nameDisplay' => empty($nameDisplay) ? $nameDisplay : $name_saved, |
||
| 196 | 'nameDisplay' => !empty($nameDisplay) ? $nameDisplay : $name_saved, |
||
| 197 | 'mimetype' => $mimetype, |
||
| 198 | // BigKev73 > without this change the numDownload will always be set to 0 |
||
| 199 | //'numDownload' => empty($numDownload) ? (int)$numDownload : 0, |
||
| 200 | 'numDownload' => !empty($numDownload) ? (int)$numDownload : 0, |
||
| 201 | ]; |
||
| 202 | } |
||
| 203 | $attachmentSave = null; |
||
| 204 | if (\is_array($this->attachmentArray)) { |
||
| 205 | $attachmentSave = \base64_encode(\serialize($this->attachmentArray)); |
||
| 206 | } |
||
| 207 | $this->setVar('attachment', $attachmentSave); |
||
| 208 | |||
| 209 | return true; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * TODO: refactor |
||
| 214 | * @param bool $asSource |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function displayAttachment(bool $asSource = false): string |
||
| 218 | { |
||
| 219 | global $xoopsModule; |
||
| 220 | |||
| 221 | $post_attachment = ''; |
||
| 222 | $attachments = $this->getAttachment(); |
||
| 223 | if ($attachments && \is_array($attachments)) { |
||
| 224 | $iconHandler = \newbbGetIconHandler(); |
||
| 225 | $mime_path = $iconHandler->getPath('mime'); |
||
| 226 | require_once \dirname(__DIR__) . '/include/functions.image.php'; |
||
| 227 | $image_extensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp']; // need improve !!! |
||
| 228 | $post_attachment .= '<br><strong>' . \_MD_NEWBB_ATTACHMENT . '</strong>:'; |
||
| 229 | $post_attachment .= '<br><hr size="1" noshade="noshade" ><br>'; |
||
| 230 | foreach ($attachments as $key => $att) { |
||
| 231 | $file_extension = \ltrim(mb_strrchr((string) $att['name_saved'], '.'), '.'); |
||
| 232 | $filetype = $file_extension; |
||
| 233 | if (\file_exists($GLOBALS['xoops']->path($mime_path . '/' . $filetype . '.gif'))) { |
||
| 234 | $icon_filetype = XOOPS_URL . '/' . $mime_path . '/' . $filetype . '.gif'; |
||
| 235 | } else { |
||
| 236 | $icon_filetype = XOOPS_URL . '/' . $mime_path . '/unknown.gif'; |
||
| 237 | } |
||
| 238 | $file_size = @\filesize($GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/' . $att['name_saved'])); |
||
| 239 | $file_size = \number_format($file_size / 1024, 2) . ' KB'; |
||
| 240 | $att['nameDisplay'] ??= ''; |
||
| 241 | if ($GLOBALS['xoopsModuleConfig']['media_allowed'] |
||
| 242 | && \in_array(mb_strtolower($file_extension), $image_extensions, true)) { |
||
| 243 | $post_attachment .= '<br><img src="' . $icon_filetype . '" alt="' . $filetype . '" ><strong> ' . $att['nameDisplay'] . '</strong> <small>(' . $file_size . ')</small>'; |
||
| 244 | $post_attachment .= '<br>' . \newbbAttachmentImage($att['name_saved']); |
||
| 245 | $isDisplayed = true; |
||
| 246 | } elseif (empty($GLOBALS['xoopsModuleConfig']['show_userattach'])) { |
||
| 247 | $post_attachment .= '<a href="' |
||
| 248 | . XOOPS_URL |
||
| 249 | . '/modules/' |
||
| 250 | . $xoopsModule->getVar('dirname', 'n') |
||
| 251 | . '/dl_attachment.php?attachid=' |
||
| 252 | . $key |
||
| 253 | . '&post_id=' |
||
| 254 | . $this->getVar('post_id') |
||
| 255 | . '"> <img src="' |
||
| 256 | . $icon_filetype |
||
| 257 | . '" alt="' |
||
| 258 | . $filetype |
||
| 259 | . '" > ' |
||
| 260 | . $att['nameDisplay'] |
||
| 261 | . '</a> ' |
||
| 262 | . \_MD_NEWBB_FILESIZE |
||
| 263 | . ': ' |
||
| 264 | . $file_size |
||
| 265 | . '; ' |
||
| 266 | . \_MD_NEWBB_HITS |
||
| 267 | . ': ' |
||
| 268 | . (isset($att['numDownload']) ? $att['numDownload'] : ''); |
||
| 269 | } elseif ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->uid() > 0 |
||
| 270 | && $GLOBALS['xoopsUser']->isactive()) { |
||
| 271 | $post_attachment .= '<a href="' |
||
| 272 | . XOOPS_URL |
||
| 273 | . '/modules/' |
||
| 274 | . $xoopsModule->getVar('dirname', 'n') |
||
| 275 | . '/dl_attachment.php?attachid=' |
||
| 276 | . $key |
||
| 277 | . '&post_id=' |
||
| 278 | . $this->getVar('post_id') |
||
| 279 | . '"> <img src="' |
||
| 280 | . $icon_filetype |
||
| 281 | . '" alt="' |
||
| 282 | . $filetype |
||
| 283 | . '" > ' |
||
| 284 | . $att['nameDisplay'] |
||
| 285 | . '</a> ' |
||
| 286 | . \_MD_NEWBB_FILESIZE |
||
| 287 | . ': ' |
||
| 288 | . $file_size |
||
| 289 | . '; ' |
||
| 290 | . \_MD_NEWBB_HITS |
||
| 291 | . ': ' |
||
| 292 | . (isset($att['numDownload']) ? $att['numDownload'] : ''); |
||
| 293 | } else { |
||
| 294 | $post_attachment .= \_MD_NEWBB_SEENOTGUEST; |
||
| 295 | } |
||
| 296 | $post_attachment .= '<br>'; |
||
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | return $post_attachment; |
||
| 301 | } |
||
| 302 | |||
| 303 | // attachment functions |
||
| 304 | // //////////////////////////////////////////////////////////////////////////////////// |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $poster_name |
||
| 308 | * @param string $post_editmsg |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | public function setPostEdit(string $poster_name = '', string $post_editmsg = ''): bool |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @return bool|string |
||
| 350 | */ |
||
| 351 | public function displayPostEdit() |
||
| 352 | { |
||
| 353 | global $myts; |
||
| 354 | |||
| 355 | if (empty($GLOBALS['xoopsModuleConfig']['recordedit_timelimit'])) { |
||
| 356 | return false; |
||
| 357 | } |
||
| 358 | |||
| 359 | $post_edit = ''; |
||
| 360 | $post_edits = $this->getVar('post_edit'); |
||
| 361 | if (!empty($post_edits)) { |
||
| 362 | $post_edits = \unserialize(\base64_decode((string) $post_edits, true)); |
||
| 363 | } |
||
| 364 | if (!isset($post_edits) || !\is_array($post_edits)) { |
||
| 365 | $post_edits = []; |
||
| 366 | } |
||
| 367 | if ($post_edits && \is_array($post_edits)) { |
||
| 368 | foreach ($post_edits as $postedit) { |
||
| 369 | $edit_time = (int)$postedit['edit_time']; |
||
| 370 | $edit_user = $postedit['edit_user']; |
||
| 371 | $edit_msg = !empty($postedit['edit_msg']) ? $postedit['edit_msg'] : ''; |
||
| 372 | // Start irmtfan add option to do only the latest edit when do_latestedit=0 (Alfred) |
||
| 373 | if (empty($GLOBALS['xoopsModuleConfig']['do_latestedit'])) { |
||
| 374 | $post_edit = ''; |
||
| 375 | } |
||
| 376 | // End irmtfan add option to do only the latest edit when do_latestedit=0 (Alfred) |
||
| 377 | // START hacked by irmtfan |
||
| 378 | // display/save all edit records. |
||
| 379 | $post_edit .= \_MD_NEWBB_EDITEDBY . ' ' . $edit_user . ' ' . \_MD_NEWBB_ON . ' ' . \formatTimestamp($edit_time) . '<br>'; |
||
| 380 | // if reason is not empty |
||
| 381 | if ('' !== $edit_msg) { |
||
| 382 | $post_edit .= \_MD_NEWBB_EDITEDMSG . ' ' . $edit_msg . '<br>'; |
||
| 383 | } |
||
| 384 | // START hacked by irmtfan |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | return $post_edit; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return array |
||
| 393 | */ |
||
| 394 | public function &getPostBody(): array |
||
| 395 | { |
||
| 396 | global $viewtopic_users; |
||
| 397 | $newbbConfig = \newbbLoadConfig(); |
||
| 398 | require_once \dirname(__DIR__) . '/include/functions.user.php'; |
||
| 399 | require_once \dirname(__DIR__) . '/include/functions.render.php'; |
||
| 400 | |||
| 401 | $uid = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
| 402 | /** @var KarmaHandler $karmaHandler */ |
||
| 403 | $karmaHandler = Helper::getInstance()->getHandler('Karma'); |
||
| 404 | $user_karma = $karmaHandler->getUserKarma(); |
||
| 405 | |||
| 406 | $post = []; |
||
| 407 | $post['attachment'] = false; |
||
| 408 | $post_text = \newbbDisplayTarea($this->vars['post_text']['value'], $this->getVar('dohtml'), $this->getVar('dosmiley'), $this->getVar('doxcode'), $this->getVar('doimage'), $this->getVar('dobr')); |
||
| 409 | if ($this->checkIdentity() || \newbbIsAdmin($this->getVar('forum_id'))) { |
||
| 410 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
| 411 | } elseif ($newbbConfig['enable_karma'] && $this->getVar('post_karma') > $user_karma) { |
||
| 412 | $post['text'] = \sprintf(\_MD_NEWBB_KARMA_REQUIREMENT, $user_karma, $this->getVar('post_karma')); |
||
| 413 | } elseif ($newbbConfig['allow_require_reply'] && $this->getVar('require_reply') |
||
| 414 | && (!$uid || !isset($viewtopic_users[$uid]))) { |
||
| 415 | $post['text'] = \_MD_NEWBB_REPLY_REQUIREMENT; |
||
| 416 | } else { |
||
| 417 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
| 418 | } |
||
| 419 | /** @var \XoopsMemberHandler $memberHandler */ |
||
| 420 | $memberHandler = \xoops_getHandler('member'); |
||
| 421 | $eachposter = $memberHandler->getUser($this->getVar('uid')); |
||
| 422 | if (\is_object($eachposter) && $eachposter->isActive()) { |
||
| 423 | if ($newbbConfig['show_realname'] && $eachposter->getVar('name')) { |
||
| 424 | $post['author'] = $eachposter->getVar('name'); |
||
| 425 | } else { |
||
| 426 | $post['author'] = $eachposter->getVar('uname'); |
||
| 427 | } |
||
| 428 | unset($eachposter); |
||
| 429 | } else { |
||
| 430 | $post['author'] = $this->getVar('poster_name') ?: $GLOBALS['xoopsConfig']['anonymous']; |
||
| 431 | } |
||
| 432 | |||
| 433 | $post['subject'] = \newbbhtmlspecialchars($this->vars['subject']['value']); |
||
| 434 | |||
| 435 | $post['date'] = $this->getVar('post_time'); |
||
| 436 | |||
| 437 | return $post; |
||
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @return bool |
||
| 442 | */ |
||
| 443 | public function isTopic(): bool |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param string $action_tag |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | public function checkTimelimit(string $action_tag = 'edit_timelimit'): bool |
||
| 453 | { |
||
| 454 | $newbbConfig = \newbbLoadConfig(); |
||
| 455 | if (empty($newbbConfig['edit_timelimit'])) { |
||
| 456 | return true; |
||
| 457 | } |
||
| 458 | |||
| 459 | return ($this->getVar('post_time') > \time() - $newbbConfig[$action_tag] * 60); |
||
| 460 | } |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param int $uid |
||
| 464 | * @return bool |
||
| 465 | */ |
||
| 466 | public function checkIdentity(int $uid = -1): bool |
||
| 480 | } |
||
| 481 | |||
| 482 | // TODO: cleaning up and merge with post hanldings in viewpost.php |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param bool $isAdmin |
||
| 486 | * @return array |
||
| 487 | */ |
||
| 488 | public function showPost(bool $isAdmin): array |
||
| 722 | } |
||
| 723 | } |
||
| 724 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.