| Total Complexity | 103 |
| Total Lines | 572 |
| Duplicated Lines | 0 % |
| Changes | 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); |
||
| 25 | class Post extends \XoopsObject |
||
| 26 | { |
||
| 27 | private $post_id; |
||
|
|
|||
| 28 | private $topic_id; |
||
| 29 | private $forum_id; |
||
| 30 | private $post_time; |
||
| 31 | private $poster_ip; |
||
| 32 | private $poster_name; |
||
| 33 | private $subject; |
||
| 34 | private $pid; |
||
| 35 | private $dohtml; |
||
| 36 | private $dosmiley; |
||
| 37 | private $doxcode; |
||
| 38 | private $doimage; |
||
| 39 | private $dobr; |
||
| 40 | private $uid; |
||
| 41 | private $icon; |
||
| 42 | private $attachsig; |
||
| 43 | private $approved; |
||
| 44 | private $post_karma; |
||
| 45 | private $require_reply; |
||
| 46 | private $attachment; |
||
| 47 | private $post_text; |
||
| 48 | private $post_edit; |
||
| 49 | |||
| 50 | public $attachment_array = []; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Post constructor. |
||
| 54 | */ |
||
| 55 | public function __construct() |
||
| 56 | { |
||
| 57 | //$this->ArtObject("bb_posts"); |
||
| 58 | $this->initVar('post_id', XOBJ_DTYPE_INT); |
||
| 59 | $this->initVar('topic_id', XOBJ_DTYPE_INT, 0, true); |
||
| 60 | $this->initVar('forum_id', XOBJ_DTYPE_INT, 0, true); |
||
| 61 | $this->initVar('post_time', XOBJ_DTYPE_INT, 0, true); |
||
| 62 | $this->initVar('poster_ip', XOBJ_DTYPE_INT, 0); |
||
| 63 | $this->initVar('poster_name', XOBJ_DTYPE_TXTBOX, ''); |
||
| 64 | $this->initVar('subject', XOBJ_DTYPE_TXTBOX, '', true); |
||
| 65 | $this->initVar('pid', XOBJ_DTYPE_INT, 0); |
||
| 66 | $this->initVar('dohtml', XOBJ_DTYPE_INT, 0); |
||
| 67 | $this->initVar('dosmiley', XOBJ_DTYPE_INT, 1); |
||
| 68 | $this->initVar('doxcode', XOBJ_DTYPE_INT, 1); |
||
| 69 | $this->initVar('doimage', XOBJ_DTYPE_INT, 1); |
||
| 70 | $this->initVar('dobr', XOBJ_DTYPE_INT, 1); |
||
| 71 | $this->initVar('uid', XOBJ_DTYPE_INT, 1); |
||
| 72 | $this->initVar('icon', XOBJ_DTYPE_TXTBOX, ''); |
||
| 73 | $this->initVar('attachsig', XOBJ_DTYPE_INT, 0); |
||
| 74 | $this->initVar('approved', XOBJ_DTYPE_INT, 1); |
||
| 75 | $this->initVar('post_karma', XOBJ_DTYPE_INT, 0); |
||
| 76 | $this->initVar('require_reply', XOBJ_DTYPE_INT, 0); |
||
| 77 | $this->initVar('attachment', XOBJ_DTYPE_TXTAREA, ''); |
||
| 78 | $this->initVar('post_text', XOBJ_DTYPE_TXTAREA, ''); |
||
| 79 | $this->initVar('post_edit', XOBJ_DTYPE_TXTAREA, ''); |
||
| 80 | } |
||
| 81 | |||
| 82 | // //////////////////////////////////////////////////////////////////////////////////// |
||
| 83 | // attachment functions TODO: there should be a file/attachment management class |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return array|mixed|null |
||
| 87 | */ |
||
| 88 | public function getAttachment() |
||
| 89 | { |
||
| 90 | if (count($this->attachment_array)) { |
||
| 91 | return $this->attachment_array; |
||
| 92 | } |
||
| 93 | $attachment = $this->getVar('attachment'); |
||
| 94 | if (empty($attachment)) { |
||
| 95 | $this->attachment_array = null; |
||
| 96 | } else { |
||
| 97 | $this->attachment_array = @unserialize(base64_decode($attachment, true)); |
||
| 98 | } |
||
| 99 | |||
| 100 | return $this->attachment_array; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param $attach_key |
||
| 105 | * @return bool |
||
| 106 | */ |
||
| 107 | public function incrementDownload($attach_key) |
||
| 108 | { |
||
| 109 | if (!$attach_key) { |
||
| 110 | return false; |
||
| 111 | } |
||
| 112 | $this->attachment_array[(string)$attach_key]['num_download']++; |
||
| 113 | |||
| 114 | return $this->attachment_array[(string)$attach_key]['num_download']; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | public function saveAttachment() |
||
| 121 | { |
||
| 122 | $attachment_save = ''; |
||
| 123 | if ($this->attachment_array && is_array($this->attachment_array)) { |
||
| 124 | $attachment_save = base64_encode(serialize($this->attachment_array)); |
||
| 125 | } |
||
| 126 | $this->setVar('attachment', $attachment_save); |
||
| 127 | $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('bb_posts') . ' SET attachment=' . $GLOBALS['xoopsDB']->quoteString($attachment_save) . ' WHERE post_id = ' . $this->getVar('post_id'); |
||
| 128 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 129 | //xoops_error($GLOBALS['xoopsDB']->error()); |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | return true; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param null $attach_array |
||
| 138 | * @return bool |
||
| 139 | */ |
||
| 140 | public function deleteAttachment($attach_array = null) |
||
| 141 | { |
||
| 142 | /** @var Xoopspoll\Helper $helper */ |
||
| 143 | $helper = Xoopspoll\Helper::getInstance(); |
||
| 144 | |||
| 145 | $attach_old = $this->getAttachment(); |
||
| 146 | if (!is_array($attach_old) || count($attach_old) < 1) { |
||
| 147 | return true; |
||
| 148 | } |
||
| 149 | $this->attachment_array = []; |
||
| 150 | |||
| 151 | if (null === $attach_array) { |
||
| 152 | $attach_array = array_keys($attach_old); |
||
| 153 | } // to delete all! |
||
| 154 | if (!is_array($attach_array)) { |
||
| 155 | $attach_array = [$attach_array]; |
||
| 156 | } |
||
| 157 | |||
| 158 | foreach ($attach_old as $key => $attach) { |
||
| 159 | if (in_array($key, $attach_array, true)) { |
||
| 160 | @unlink(XOOPS_ROOT_PATH . '/' . $helper->getConfig('dir_attachments') . '/' . $attach['name_saved']); |
||
| 161 | @unlink(XOOPS_ROOT_PATH . '/' . $helper->getConfig('dir_attachments') . '/thumbs/' . $attach['name_saved']); // delete thumbnails |
||
| 162 | continue; |
||
| 163 | } |
||
| 164 | $this->attachment_array[$key] = $attach; |
||
| 165 | } |
||
| 166 | $attachment_save = ''; |
||
| 167 | if ($this->attachment_array && is_array($this->attachment_array)) { |
||
| 168 | $attachment_save = base64_encode(serialize($this->attachment_array)); |
||
| 169 | } |
||
| 170 | $this->setVar('attachment', $attachment_save); |
||
| 171 | |||
| 172 | return true; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param string $name_saved |
||
| 177 | * @param string $name_display |
||
| 178 | * @param string $mimetype |
||
| 179 | * @param int $num_download |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | public function setAttachment($name_saved = '', $name_display = '', $mimetype = '', $num_download = 0) |
||
| 183 | { |
||
| 184 | static $counter = 0; |
||
| 185 | $this->attachment_array = $this->getAttachment(); |
||
| 186 | if ($name_saved) { |
||
| 187 | $key = (string)(time() + $counter++); |
||
| 188 | $this->attachment_array[$key] = [ |
||
| 189 | 'name_saved' => $name_saved, |
||
| 190 | 'name_display' => $name_display ?? $name_saved, |
||
| 191 | 'mimetype' => $mimetype, |
||
| 192 | 'num_download' => isset($num_download) ? (int)$num_download : 0, |
||
| 193 | ]; |
||
| 194 | } |
||
| 195 | $attachment_save = null; |
||
| 196 | if (is_array($this->attachment_array)) { |
||
| 197 | $attachment_save = base64_encode(serialize($this->attachment_array)); |
||
| 198 | } |
||
| 199 | $this->setVar('attachment', $attachment_save); |
||
| 200 | |||
| 201 | return true; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * TODO: refactor |
||
| 206 | * @param bool $asSource |
||
| 207 | * @return string |
||
| 208 | */ |
||
| 209 | public function displayAttachment($asSource = false) |
||
| 210 | { |
||
| 211 | global $xoopsModule; |
||
| 212 | /** @var Xoopspoll\Helper $helper */ |
||
| 213 | $helper = Xoopspoll\Helper::getInstance(); |
||
| 214 | |||
| 215 | $post_attachment = ''; |
||
| 216 | $attachments = $this->getAttachment(); |
||
| 217 | if ($attachments && is_array($attachments)) { |
||
| 218 | $iconHandler = newbb_getIconHandler(); |
||
| 219 | $mime_path = $iconHandler->getPath('mime'); |
||
| 220 | require_once XOOPS_ROOT_PATH . '/modules/' . $xoopsModule->getVar('dirname', 'n') . '/include/functions.image.php'; |
||
| 221 | $image_extensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp']; // need improve !!! |
||
| 222 | $post_attachment .= '<br><strong>' . _MD_ATTACHMENT . '</strong>:'; |
||
| 223 | $post_attachment .= '<br><hr size="1" 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(XOOPS_ROOT_PATH . '/' . $mime_path . '/' . $filetype . '.gif')) { |
||
| 228 | $icon_filetype = XOOPS_URL . '/' . $mime_path . '/' . $filetype . '.gif'; |
||
| 229 | } else { |
||
| 230 | $icon_filetype = XOOPS_URL . '/' . $mime_path . '/unknown.gif'; |
||
| 231 | } |
||
| 232 | $file_size = @filesize(XOOPS_ROOT_PATH . '/' . $helper->getConfig('dir_attachments') . '/' . $att['name_saved']); |
||
| 233 | $file_size = number_format($file_size / 1024, 2) . ' KB'; |
||
| 234 | if (in_array(mb_strtolower($file_extension), $image_extensions, true) && $helper->getConfig('media_allowed')) { |
||
| 235 | $post_attachment .= '<br><img src="' . $icon_filetype . '" alt="' . $filetype . '"><strong> ' . $att['name_display'] . '</strong> <small>(' . $file_size . ')</small>'; |
||
| 236 | $post_attachment .= '<br>' . newbb_attachmentImage($att['name_saved']); |
||
| 237 | $isDisplayed = true; |
||
| 238 | } else { |
||
| 239 | $post_attachment .= '<a href="' |
||
| 240 | . XOOPS_URL |
||
| 241 | . '/modules/' |
||
| 242 | . $xoopsModule->getVar('dirname', 'n') |
||
| 243 | . '/dl_attachment.php?attachid=' |
||
| 244 | . $key |
||
| 245 | . '&post_id=' |
||
| 246 | . $this->getVar('post_id') |
||
| 247 | . '"> <img src="' |
||
| 248 | . $icon_filetype |
||
| 249 | . '" alt="' |
||
| 250 | . $filetype |
||
| 251 | . '"> ' |
||
| 252 | . $att['name_display'] |
||
| 253 | . '</a> ' |
||
| 254 | . _MD_FILESIZE |
||
| 255 | . ': ' |
||
| 256 | . $file_size |
||
| 257 | . '; ' |
||
| 258 | . _MD_HITS |
||
| 259 | . ': ' |
||
| 260 | . $att['num_download']; |
||
| 261 | } |
||
| 262 | $post_attachment .= '<br>'; |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | return $post_attachment; |
||
| 267 | } |
||
| 268 | |||
| 269 | // attachment functions |
||
| 270 | // //////////////////////////////////////////////////////////////////////////////////// |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param string $poster_name |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | public function setPostEdit($poster_name = '') |
||
| 277 | { |
||
| 278 | global $xoopsUser; |
||
| 279 | /** @var Xoopspoll\Helper $helper */ |
||
| 280 | $helper = Xoopspoll\Helper::getInstance(); |
||
| 281 | |||
| 282 | if (empty($helper->getConfig('recordedit_timelimit')) |
||
| 283 | || (time() - $this->getVar('post_time')) < $helper->getConfig('recordedit_timelimit') * 60 |
||
| 284 | || $this->getVar('approved') < 1) { |
||
| 285 | return true; |
||
| 286 | } |
||
| 287 | if (is_object($xoopsUser) && $xoopsUser->isActive()) { |
||
| 288 | if ($helper->getConfig('show_realname') && $xoopsUser->getVar('name')) { |
||
| 289 | $edit_user = $xoopsUser->getVar('name'); |
||
| 290 | } else { |
||
| 291 | $edit_user = $xoopsUser->getVar('uname'); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | $post_edit = []; |
||
| 295 | $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. |
||
| 296 | $post_edit['edit_time'] = time(); |
||
| 297 | |||
| 298 | $post_edits = $this->getVar('post_edit'); |
||
| 299 | if (!empty($post_edits)) { |
||
| 300 | $post_edits = unserialize(base64_decode($post_edits, true)); |
||
| 301 | } |
||
| 302 | if (!is_array($post_edits)) { |
||
| 303 | $post_edits = []; |
||
| 304 | } |
||
| 305 | $post_edits[] = $post_edit; |
||
| 306 | $post_edit = base64_encode(serialize($post_edits)); |
||
| 307 | unset($post_edits); |
||
| 308 | $this->setVar('post_edit', $post_edit); |
||
| 309 | |||
| 310 | return true; |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @return bool|string |
||
| 315 | */ |
||
| 316 | public function displayPostEdit() |
||
| 317 | { |
||
| 318 | global $myts; |
||
| 319 | /** @var Xoopspoll\Helper $helper */ |
||
| 320 | $helper = Xoopspoll\Helper::getInstance(); |
||
| 321 | |||
| 322 | if (empty($helper->getConfig('recordedit_timelimit'))) { |
||
| 323 | return false; |
||
| 324 | } |
||
| 325 | |||
| 326 | $post_edit = ''; |
||
| 327 | $post_edits = $this->getVar('post_edit'); |
||
| 328 | if (!empty($post_edits)) { |
||
| 329 | $post_edits = unserialize(base64_decode($post_edits, true)); |
||
| 330 | } |
||
| 331 | if (!isset($post_edits) || !is_array($post_edits)) { |
||
| 332 | $post_edits = []; |
||
| 333 | } |
||
| 334 | if ($post_edits && is_array($post_edits)) { |
||
| 335 | foreach ($post_edits as $postedit) { |
||
| 336 | $edit_time = (int)$postedit['edit_time']; |
||
| 337 | $edit_user = ($postedit['edit_user']); |
||
| 338 | $post_edit .= _MD_EDITEDBY . ' ' . $edit_user . ' ' . _MD_ON . ' ' . formatTimestamp($edit_time) . '<br>'; |
||
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | return $post_edit; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return array |
||
| 347 | */ |
||
| 348 | public function &getPostBody() |
||
| 349 | { |
||
| 350 | global $xoopsConfig, $xoopsUser, $myts; |
||
| 351 | /** @var Xoopspoll\Helper $helper */ |
||
| 352 | $helper = Xoopspoll\Helper::getInstance(); |
||
| 353 | |||
| 354 | require_once XOOPS_ROOT_PATH . '/modules/newbb/include/functions.user.php'; |
||
| 355 | require_once XOOPS_ROOT_PATH . '/modules/newbb/include/functions.render.php'; |
||
| 356 | |||
| 357 | $uid = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0; |
||
| 358 | $karmaHandler = Newbb\Helper::getInstance()->getHandler('Karma'); |
||
| 359 | $user_karma = $karmaHandler->getUserKarma(); |
||
| 360 | |||
| 361 | $post = []; |
||
| 362 | $post['attachment'] = false; |
||
| 363 | $post_text = &newbb_displayTarea($this->vars['post_text']['value'], $this->getVar('dohtml'), $this->getVar('dosmiley'), $this->getVar('doxcode'), $this->getVar('doimage'), $this->getVar('dobr')); |
||
| 364 | if (newbb_isAdmin($this->getVar('forum_id')) || $this->checkIdentity()) { |
||
| 365 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
| 366 | } elseif ($helper->getConfig('enable_karma') && $this->getVar('post_karma') > $user_karma) { |
||
| 367 | $post['text'] = sprintf(_MD_KARMA_REQUIREMENT, $user_karma, $this->getVar('post_karma')); |
||
| 368 | } elseif ($helper->getConfig('allow_require_reply') && $this->getVar('require_reply') |
||
| 369 | && (!$uid |
||
| 370 | || !isset($viewtopic_users[$uid]))) { |
||
| 371 | $post['text'] = _MD_REPLY_REQUIREMENT; |
||
| 372 | } else { |
||
| 373 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
| 374 | } |
||
| 375 | /** @var \XoopsMemberHandler $memberHandler */ |
||
| 376 | $memberHandler = xoops_getHandler('member'); |
||
| 377 | $eachposter = $memberHandler->getUser($this->getVar('uid')); |
||
| 378 | if (is_object($eachposter) && $eachposter->isActive()) { |
||
| 379 | if ($helper->getConfig('show_realname') && $eachposter->getVar('name')) { |
||
| 380 | $post['author'] = $eachposter->getVar('name'); |
||
| 381 | } else { |
||
| 382 | $post['author'] = $eachposter->getVar('uname'); |
||
| 383 | } |
||
| 384 | unset($eachposter); |
||
| 385 | } else { |
||
| 386 | $post['author'] = $this->getVar('poster_name') ?: $xoopsConfig['anonymous']; |
||
| 387 | } |
||
| 388 | |||
| 389 | $post['subject'] = newbb_htmlspecialchars($this->vars['subject']['value']); |
||
| 390 | |||
| 391 | $post['date'] = $this->getVar('post_time'); |
||
| 392 | |||
| 393 | return $post; |
||
| 394 | } |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | public function isTopic() |
||
| 400 | { |
||
| 401 | return !$this->getVar('pid'); |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param string $action_tag |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | public function checkTimelimit($action_tag = 'edit_timelimit') |
||
| 409 | { |
||
| 410 | require_once XOOPS_ROOT_PATH . '/modules/newbb/include/functions.config.php'; |
||
| 411 | $newbb_config = newbb_loadConfig(); |
||
| 412 | if (empty($newbb_config['edit_timelimit'])) { |
||
| 413 | return true; |
||
| 414 | } |
||
| 415 | |||
| 416 | return ($this->getVar('post_time') > time() - $newbb_config[$action_tag] * 60); |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param int $uid |
||
| 421 | * @return bool |
||
| 422 | */ |
||
| 423 | public function checkIdentity($uid = -1) |
||
| 439 | } |
||
| 440 | |||
| 441 | // TODO: cleaning up and merge with post hanldings in viewpost.php |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param $isadmin |
||
| 445 | * @return array |
||
| 446 | */ |
||
| 447 | public function showPost($isadmin) |
||
| 597 | } |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Class PostHandler |
||
| 602 | */ |
||
| 603 | class PostHandler extends \XoopsPersistableObjectHandler |
||
| 604 | { |
||
| 605 | /** |
||
| 1108 |