Total Complexity | 120 |
Total Lines | 672 |
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 |
||
46 | class Post extends \XoopsObject |
||
47 | { |
||
48 | //class Post extends \XoopsObject { |
||
49 | private $attachmentArray = []; |
||
50 | |||
51 | public function __construct() |
||
52 | { |
||
53 | parent::__construct(); |
||
54 | |||
55 | $this->initVar('post_id', \XOBJ_DTYPE_INT); |
||
56 | $this->initVar('topic_id', \XOBJ_DTYPE_INT, 0, true); |
||
57 | $this->initVar('forum_id', \XOBJ_DTYPE_INT, 0, true); |
||
58 | $this->initVar('post_time', \XOBJ_DTYPE_INT, 0, true); |
||
59 | // $this->initVar('poster_ip', XOBJ_DTYPE_INT, 0); |
||
60 | $this->initVar('poster_ip', \XOBJ_DTYPE_TXTBOX, ''); |
||
61 | $this->initVar('poster_name', \XOBJ_DTYPE_TXTBOX, ''); |
||
62 | $this->initVar('subject', \XOBJ_DTYPE_TXTBOX, '', true); |
||
63 | $this->initVar('pid', \XOBJ_DTYPE_INT, 0); |
||
64 | $this->initVar('dohtml', \XOBJ_DTYPE_INT, 0); |
||
65 | $this->initVar('dosmiley', \XOBJ_DTYPE_INT, 1); |
||
66 | $this->initVar('doxcode', \XOBJ_DTYPE_INT, 1); |
||
67 | $this->initVar('doimage', \XOBJ_DTYPE_INT, 1); |
||
68 | $this->initVar('dobr', \XOBJ_DTYPE_INT, 1); |
||
69 | $this->initVar('uid', \XOBJ_DTYPE_INT, 1); |
||
70 | $this->initVar('icon', \XOBJ_DTYPE_TXTBOX, ''); |
||
71 | $this->initVar('attachsig', \XOBJ_DTYPE_INT, 0); |
||
72 | $this->initVar('approved', \XOBJ_DTYPE_INT, 1); |
||
73 | $this->initVar('post_karma', \XOBJ_DTYPE_INT, 0); |
||
74 | $this->initVar('require_reply', \XOBJ_DTYPE_INT, 0); |
||
75 | $this->initVar('attachment', \XOBJ_DTYPE_TXTAREA, ''); |
||
76 | $this->initVar('post_text', \XOBJ_DTYPE_TXTAREA, ''); |
||
77 | $this->initVar('post_edit', \XOBJ_DTYPE_TXTAREA, ''); |
||
78 | } |
||
79 | |||
80 | // //////////////////////////////////////////////////////////////////////////////////// |
||
81 | // attachment functions TODO: there should be a file/attachment management class |
||
82 | |||
83 | /** |
||
84 | * @return array|mixed|null |
||
85 | */ |
||
86 | public function getAttachment() |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param $attachKey |
||
103 | * @return bool |
||
104 | */ |
||
105 | public function incrementDownload($attachKey) |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function saveAttachment() |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param array|null $attachArray |
||
136 | * @return bool |
||
137 | */ |
||
138 | public function deleteAttachment($attachArray = null) |
||
139 | { |
||
140 | $attachOld = $this->getAttachment(); |
||
141 | if (!\is_array($attachOld) || \count($attachOld) < 1) { |
||
142 | return true; |
||
143 | } |
||
144 | $this->attachmentArray = []; |
||
145 | |||
146 | if (null === $attachArray) { |
||
147 | $attachArray = \array_keys($attachOld); |
||
148 | } // to delete all! |
||
149 | if (!\is_array($attachArray)) { |
||
150 | $attachArray = [$attachArray]; |
||
151 | } |
||
152 | |||
153 | foreach ($attachOld as $key => $attach) { |
||
154 | if (\in_array($key, $attachArray)) { |
||
155 | $file = $GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/' . $attach['name_saved']); |
||
156 | @\unlink($file); |
||
157 | $file = $GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/thumbs/' . $attach['name_saved']); // delete thumbnails |
||
158 | @\unlink($file); |
||
159 | continue; |
||
160 | } |
||
161 | $this->attachmentArray[$key] = $attach; |
||
162 | } |
||
163 | $attachmentSave = ''; |
||
164 | if ($this->attachmentArray && \is_array($this->attachmentArray)) { |
||
165 | $attachmentSave = \base64_encode(\serialize($this->attachmentArray)); |
||
166 | } |
||
167 | $this->setVar('attachment', $attachmentSave); |
||
168 | |||
169 | return true; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param string $name_saved |
||
174 | * @param string $nameDisplay |
||
175 | * @param string $mimetype |
||
176 | * @param int $numDownload |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function setAttachment($name_saved = '', $nameDisplay = '', $mimetype = '', $numDownload = 0) |
||
180 | { |
||
181 | static $counter = 0; |
||
182 | $this->attachmentArray = $this->getAttachment(); |
||
183 | if ($name_saved) { |
||
184 | $key = (string)(\time() + $counter++); |
||
185 | $this->attachmentArray[$key] = [ |
||
186 | 'name_saved' => $name_saved, |
||
187 | 'nameDisplay' => empty($nameDisplay) ? $nameDisplay : $name_saved, |
||
188 | 'mimetype' => $mimetype, |
||
189 | 'numDownload' => empty($numDownload) ? (int)$numDownload : 0, |
||
190 | ]; |
||
191 | } |
||
192 | $attachmentSave = null; |
||
193 | if (\is_array($this->attachmentArray)) { |
||
194 | $attachmentSave = \base64_encode(\serialize($this->attachmentArray)); |
||
195 | } |
||
196 | $this->setVar('attachment', $attachmentSave); |
||
197 | |||
198 | return true; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * TODO: refactor |
||
203 | * @param bool $asSource |
||
204 | * @return string |
||
205 | */ |
||
206 | public function displayAttachment($asSource = false) |
||
207 | { |
||
208 | global $xoopsModule; |
||
209 | |||
210 | $post_attachment = ''; |
||
211 | $attachments = $this->getAttachment(); |
||
212 | if ($attachments && \is_array($attachments)) { |
||
213 | $iconHandler = \newbbGetIconHandler(); |
||
214 | $mime_path = $iconHandler->getPath('mime'); |
||
215 | require_once \dirname(__DIR__) . '/include/functions.image.php'; |
||
216 | $image_extensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp']; // need improve !!! |
||
217 | $post_attachment .= '<br><strong>' . \_MD_NEWBB_ATTACHMENT . '</strong>:'; |
||
218 | $post_attachment .= '<br><hr size="1" noshade="noshade" ><br>'; |
||
219 | foreach ($attachments as $key => $att) { |
||
220 | $file_extension = \ltrim(mb_strrchr($att['name_saved'], '.'), '.'); |
||
221 | $filetype = $file_extension; |
||
222 | if (\file_exists($GLOBALS['xoops']->path($mime_path . '/' . $filetype . '.gif'))) { |
||
223 | $icon_filetype = XOOPS_URL . '/' . $mime_path . '/' . $filetype . '.gif'; |
||
224 | } else { |
||
225 | $icon_filetype = XOOPS_URL . '/' . $mime_path . '/unknown.gif'; |
||
226 | } |
||
227 | $file_size = @\filesize($GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/' . $att['name_saved'])); |
||
228 | $file_size = \number_format($file_size / 1024, 2) . ' KB'; |
||
229 | if (\in_array(mb_strtolower($file_extension), $image_extensions) |
||
230 | && $GLOBALS['xoopsModuleConfig']['media_allowed']) { |
||
231 | $post_attachment .= '<br><img src="' . $icon_filetype . '" alt="' . $filetype . '" ><strong> ' . $att['nameDisplay'] . '</strong> <small>(' . $file_size . ')</small>'; |
||
232 | $post_attachment .= '<br>' . \newbbAttachmentImage($att['name_saved']); |
||
233 | $isDisplayed = true; |
||
234 | } else { |
||
235 | if (empty($GLOBALS['xoopsModuleConfig']['show_userattach'])) { |
||
236 | $post_attachment .= '<a href="' |
||
237 | . XOOPS_URL |
||
238 | . '/modules/' |
||
239 | . $xoopsModule->getVar('dirname', 'n') |
||
240 | . '/dl_attachment.php?attachid=' |
||
241 | . $key |
||
242 | . '&post_id=' |
||
243 | . $this->getVar('post_id') |
||
244 | . '"> <img src="' |
||
245 | . $icon_filetype |
||
246 | . '" alt="' |
||
247 | . $filetype |
||
248 | . '" > ' |
||
249 | . $att['nameDisplay'] |
||
250 | . '</a> ' |
||
251 | . \_MD_NEWBB_FILESIZE |
||
252 | . ': ' |
||
253 | . $file_size |
||
254 | . '; ' |
||
255 | . \_MD_NEWBB_HITS |
||
256 | . ': ' |
||
257 | . $att['numDownload']; |
||
258 | } elseif ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->uid() > 0 |
||
259 | && $GLOBALS['xoopsUser']->isactive()) { |
||
260 | $post_attachment .= '<a href="' |
||
261 | . XOOPS_URL |
||
262 | . '/modules/' |
||
263 | . $xoopsModule->getVar('dirname', 'n') |
||
264 | . '/dl_attachment.php?attachid=' |
||
265 | . $key |
||
266 | . '&post_id=' |
||
267 | . $this->getVar('post_id') |
||
268 | . '"> <img src="' |
||
269 | . $icon_filetype |
||
270 | . '" alt="' |
||
271 | . $filetype |
||
272 | . '" > ' |
||
273 | . $att['nameDisplay'] |
||
274 | . '</a> ' |
||
275 | . \_MD_NEWBB_FILESIZE |
||
276 | . ': ' |
||
277 | . $file_size |
||
278 | . '; ' |
||
279 | . \_MD_NEWBB_HITS |
||
280 | . ': ' |
||
281 | . $att['numDownload']; |
||
282 | } else { |
||
283 | $post_attachment .= _MD_NEWBB_SEENOTGUEST; |
||
284 | } |
||
285 | } |
||
286 | $post_attachment .= '<br>'; |
||
287 | } |
||
288 | } |
||
289 | |||
290 | return $post_attachment; |
||
291 | } |
||
292 | |||
293 | // attachment functions |
||
294 | // //////////////////////////////////////////////////////////////////////////////////// |
||
295 | |||
296 | /** |
||
297 | * @param string $poster_name |
||
298 | * @param string $post_editmsg |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function setPostEdit($poster_name = '', $post_editmsg = '') |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @return bool|string |
||
339 | */ |
||
340 | public function displayPostEdit() |
||
341 | { |
||
342 | global $myts; |
||
343 | |||
344 | if (empty($GLOBALS['xoopsModuleConfig']['recordedit_timelimit'])) { |
||
345 | return false; |
||
346 | } |
||
347 | |||
348 | $post_edit = ''; |
||
349 | $post_edits = $this->getVar('post_edit'); |
||
350 | if (!empty($post_edits)) { |
||
351 | $post_edits = \unserialize(\base64_decode($post_edits, true)); |
||
352 | } |
||
353 | if (!isset($post_edits) || !\is_array($post_edits)) { |
||
354 | $post_edits = []; |
||
355 | } |
||
356 | if ($post_edits && \is_array($post_edits)) { |
||
357 | foreach ($post_edits as $postedit) { |
||
358 | $edit_time = (int)$postedit['edit_time']; |
||
359 | $edit_user = $postedit['edit_user']; |
||
360 | $edit_msg = !empty($postedit['edit_msg']) ? $postedit['edit_msg'] : ''; |
||
361 | // Start irmtfan add option to do only the latest edit when do_latestedit=0 (Alfred) |
||
362 | if (empty($GLOBALS['xoopsModuleConfig']['do_latestedit'])) { |
||
363 | $post_edit = ''; |
||
364 | } |
||
365 | // End irmtfan add option to do only the latest edit when do_latestedit=0 (Alfred) |
||
366 | // START hacked by irmtfan |
||
367 | // display/save all edit records. |
||
368 | $post_edit .= \_MD_NEWBB_EDITEDBY . ' ' . $edit_user . ' ' . \_MD_NEWBB_ON . ' ' . \formatTimestamp($edit_time) . '<br>'; |
||
369 | // if reason is not empty |
||
370 | if ('' !== $edit_msg) { |
||
371 | $post_edit .= \_MD_NEWBB_EDITEDMSG . ' ' . $edit_msg . '<br>'; |
||
372 | } |
||
373 | // START hacked by irmtfan |
||
374 | } |
||
375 | } |
||
376 | |||
377 | return $post_edit; |
||
378 | } |
||
379 | |||
380 | /** |
||
381 | * @return array |
||
382 | */ |
||
383 | public function &getPostBody() |
||
384 | { |
||
385 | global $viewtopic_users; |
||
386 | $newbbConfig = \newbbLoadConfig(); |
||
387 | require_once \dirname(__DIR__) . '/include/functions.user.php'; |
||
388 | require_once \dirname(__DIR__) . '/include/functions.render.php'; |
||
389 | |||
390 | $uid = \is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
391 | /** @var KarmaHandler $karmaHandler */ |
||
392 | $karmaHandler = \XoopsModules\Newbb\Helper::getInstance()->getHandler('Karma'); |
||
393 | $user_karma = $karmaHandler->getUserKarma(); |
||
394 | |||
395 | $post = []; |
||
396 | $post['attachment'] = false; |
||
397 | $post_text = \newbbDisplayTarea($this->vars['post_text']['value'], $this->getVar('dohtml'), $this->getVar('dosmiley'), $this->getVar('doxcode'), $this->getVar('doimage'), $this->getVar('dobr')); |
||
398 | if (\newbbIsAdmin($this->getVar('forum_id')) || $this->checkIdentity()) { |
||
399 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
400 | } elseif ($newbbConfig['enable_karma'] && $this->getVar('post_karma') > $user_karma) { |
||
401 | $post['text'] = \sprintf(\_MD_NEWBB_KARMA_REQUIREMENT, $user_karma, $this->getVar('post_karma')); |
||
402 | } elseif ($newbbConfig['allow_require_reply'] && $this->getVar('require_reply') |
||
403 | && (!$uid || !isset($viewtopic_users[$uid]))) { |
||
404 | $post['text'] = \_MD_NEWBB_REPLY_REQUIREMENT; |
||
405 | } else { |
||
406 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
407 | } |
||
408 | /** @var \XoopsMemberHandler $memberHandler */ |
||
409 | $memberHandler = \xoops_getHandler('member'); |
||
410 | $eachposter = $memberHandler->getUser($this->getVar('uid')); |
||
411 | if (\is_object($eachposter) && $eachposter->isActive()) { |
||
412 | if ($newbbConfig['show_realname'] && $eachposter->getVar('name')) { |
||
413 | $post['author'] = $eachposter->getVar('name'); |
||
414 | } else { |
||
415 | $post['author'] = $eachposter->getVar('uname'); |
||
416 | } |
||
417 | unset($eachposter); |
||
418 | } else { |
||
419 | $post['author'] = $this->getVar('poster_name') ?: $GLOBALS['xoopsConfig']['anonymous']; |
||
420 | } |
||
421 | |||
422 | $post['subject'] = \newbbHtmlspecialchars($this->vars['subject']['value']); |
||
423 | |||
424 | $post['date'] = $this->getVar('post_time'); |
||
425 | |||
426 | return $post; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * @return bool |
||
431 | */ |
||
432 | public function isTopic() |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @param string $action_tag |
||
439 | * @return bool |
||
440 | */ |
||
441 | public function checkTimelimit($action_tag = 'edit_timelimit') |
||
442 | { |
||
443 | $newbbConfig = \newbbLoadConfig(); |
||
444 | if (empty($newbbConfig['edit_timelimit'])) { |
||
445 | return true; |
||
446 | } |
||
447 | |||
448 | return ($this->getVar('post_time') > \time() - $newbbConfig[$action_tag] * 60); |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * @param int $uid |
||
453 | * @return bool |
||
454 | */ |
||
455 | public function checkIdentity($uid = -1) |
||
469 | } |
||
470 | |||
471 | // TODO: cleaning up and merge with post hanldings in viewpost.php |
||
472 | |||
473 | /** |
||
474 | * @param $isAdmin |
||
475 | * @return array |
||
476 | */ |
||
477 | public function showPost($isAdmin) |
||
720 |