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