Total Complexity | 120 |
Total Lines | 669 |
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 namespace XoopsModules\Newbb; |
||
44 | class Post extends \XoopsObject |
||
45 | { |
||
46 | //class Post extends \XoopsObject { |
||
47 | public $attachmentArray = []; |
||
48 | |||
49 | /** |
||
50 | * |
||
51 | */ |
||
52 | public function __construct() |
||
53 | { |
||
54 | parent::__construct(); |
||
55 | |||
56 | $this->initVar('post_id', XOBJ_DTYPE_INT); |
||
57 | $this->initVar('topic_id', XOBJ_DTYPE_INT, 0, true); |
||
58 | $this->initVar('forum_id', XOBJ_DTYPE_INT, 0, true); |
||
59 | $this->initVar('post_time', XOBJ_DTYPE_INT, 0, true); |
||
60 | // $this->initVar('poster_ip', XOBJ_DTYPE_INT, 0); |
||
61 | $this->initVar('poster_ip', XOBJ_DTYPE_TXTBOX, ''); |
||
62 | $this->initVar('poster_name', XOBJ_DTYPE_TXTBOX, ''); |
||
63 | $this->initVar('subject', XOBJ_DTYPE_TXTBOX, '', true); |
||
64 | $this->initVar('pid', XOBJ_DTYPE_INT, 0); |
||
65 | $this->initVar('dohtml', XOBJ_DTYPE_INT, 0); |
||
66 | $this->initVar('dosmiley', XOBJ_DTYPE_INT, 1); |
||
67 | $this->initVar('doxcode', XOBJ_DTYPE_INT, 1); |
||
68 | $this->initVar('doimage', XOBJ_DTYPE_INT, 1); |
||
69 | $this->initVar('dobr', XOBJ_DTYPE_INT, 1); |
||
70 | $this->initVar('uid', XOBJ_DTYPE_INT, 1); |
||
71 | $this->initVar('icon', XOBJ_DTYPE_TXTBOX, ''); |
||
72 | $this->initVar('attachsig', XOBJ_DTYPE_INT, 0); |
||
73 | $this->initVar('approved', XOBJ_DTYPE_INT, 1); |
||
74 | $this->initVar('post_karma', XOBJ_DTYPE_INT, 0); |
||
75 | $this->initVar('require_reply', XOBJ_DTYPE_INT, 0); |
||
76 | $this->initVar('attachment', XOBJ_DTYPE_TXTAREA, ''); |
||
77 | $this->initVar('post_text', XOBJ_DTYPE_TXTAREA, ''); |
||
78 | $this->initVar('post_edit', XOBJ_DTYPE_TXTAREA, ''); |
||
79 | } |
||
80 | |||
81 | // //////////////////////////////////////////////////////////////////////////////////// |
||
82 | // attachment functions TODO: there should be a file/attachment management class |
||
83 | /** |
||
84 | * @return array|mixed|null |
||
85 | */ |
||
86 | public function getAttachment() |
||
87 | { |
||
88 | if (count($this->attachmentArray)) { |
||
89 | return $this->attachmentArray; |
||
90 | } |
||
91 | $attachment = $this->getVar('attachment'); |
||
92 | if (empty($attachment)) { |
||
93 | $this->attachmentArray = []; |
||
94 | } else { |
||
95 | $this->attachmentArray = @unserialize(base64_decode($attachment)); |
||
96 | } |
||
97 | |||
98 | return $this->attachmentArray; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param $attachKey |
||
103 | * @return bool |
||
104 | */ |
||
105 | public function incrementDownload($attachKey) |
||
106 | { |
||
107 | if (!$attachKey) { |
||
108 | return false; |
||
109 | } |
||
110 | $this->attachmentArray[(string)$attachKey]['numDownload']++; |
||
111 | |||
112 | return $this->attachmentArray[(string)$attachKey]['numDownload']; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @return bool |
||
117 | */ |
||
118 | public function saveAttachment() |
||
119 | { |
||
120 | $attachmentSave = ''; |
||
121 | if (is_array($this->attachmentArray) && count($this->attachmentArray) > 0) { |
||
122 | $attachmentSave = base64_encode(serialize($this->attachmentArray)); |
||
123 | } |
||
124 | $this->setVar('attachment', $attachmentSave); |
||
125 | $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('newbb_posts') . ' SET attachment=' . $GLOBALS['xoopsDB']->quoteString($attachmentSave) . ' WHERE post_id = ' . $this->getVar('post_id'); |
||
126 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
127 | //xoops_error($GLOBALS['xoopsDB']->error()); |
||
128 | return false; |
||
129 | } |
||
130 | |||
131 | return true; |
||
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 | @unlink($GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/' . $attach['name_saved'])); |
||
156 | @unlink($GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/thumbs/' . $attach['name_saved'])); // delete thumbnails |
||
157 | continue; |
||
158 | } |
||
159 | $this->attachmentArray[$key] = $attach; |
||
160 | } |
||
161 | $attachmentSave = ''; |
||
162 | if (is_array($this->attachmentArray) && count($this->attachmentArray) > 0) { |
||
163 | $attachmentSave = base64_encode(serialize($this->attachmentArray)); |
||
164 | } |
||
165 | $this->setVar('attachment', $attachmentSave); |
||
166 | |||
167 | return true; |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * @param string $name_saved |
||
172 | * @param string $nameDisplay |
||
173 | * @param string $mimetype |
||
174 | * @param int $numDownload |
||
175 | * @return bool |
||
176 | */ |
||
177 | public function setAttachment($name_saved = '', $nameDisplay = '', $mimetype = '', $numDownload = 0) |
||
178 | { |
||
179 | static $counter = 0; |
||
180 | $this->attachmentArray = $this->getAttachment(); |
||
181 | if ($name_saved) { |
||
182 | $key = (string)(time() + $counter++); |
||
183 | $this->attachmentArray[$key] = [ |
||
184 | 'name_saved' => $name_saved, |
||
185 | 'nameDisplay' => empty($nameDisplay) ? $nameDisplay : $name_saved, |
||
186 | 'mimetype' => $mimetype, |
||
187 | 'numDownload' => empty($numDownload) ? (int)$numDownload : 0 |
||
188 | ]; |
||
189 | } |
||
190 | $attachmentSave = null; |
||
191 | if (is_array($this->attachmentArray)) { |
||
192 | $attachmentSave = base64_encode(serialize($this->attachmentArray)); |
||
193 | } |
||
194 | $this->setVar('attachment', $attachmentSave); |
||
195 | |||
196 | return true; |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * TODO: refactor |
||
201 | * @param bool $asSource |
||
202 | * @return string |
||
203 | */ |
||
204 | public function displayAttachment($asSource = false) |
||
205 | { |
||
206 | global $xoopsModule; |
||
207 | |||
208 | $post_attachment = ''; |
||
209 | $attachments = $this->getAttachment(); |
||
210 | if (is_array($attachments) && count($attachments) > 0) { |
||
211 | $iconHandler = newbbGetIconHandler(); |
||
212 | $mime_path = $iconHandler->getPath('mime'); |
||
213 | include_once dirname(__DIR__) . '/include/functions.image.php'; |
||
214 | $image_extensions = ['jpg', 'jpeg', 'gif', 'png', 'bmp']; // need improve !!! |
||
215 | $post_attachment .= '<br><strong>' . _MD_NEWBB_ATTACHMENT . '</strong>:'; |
||
216 | $post_attachment .= '<br><hr size="1" noshade="noshade" /><br>'; |
||
217 | foreach ($attachments as $key => $att) { |
||
218 | $file_extension = ltrim(strrchr($att['name_saved'], '.'), '.'); |
||
219 | $filetype = $file_extension; |
||
220 | if (file_exists($GLOBALS['xoops']->path($mime_path . '/' . $filetype . '.gif'))) { |
||
221 | $icon_filetype = XOOPS_URL . '/' . $mime_path . '/' . $filetype . '.gif'; |
||
222 | } else { |
||
223 | $icon_filetype = XOOPS_URL . '/' . $mime_path . '/unknown.gif'; |
||
224 | } |
||
225 | $file_size = @filesize($GLOBALS['xoops']->path($GLOBALS['xoopsModuleConfig']['dir_attachments'] . '/' . $att['name_saved'])); |
||
226 | $file_size = number_format($file_size / 1024, 2) . ' KB'; |
||
227 | if (in_array(strtolower($file_extension), $image_extensions) |
||
228 | && $GLOBALS['xoopsModuleConfig']['media_allowed']) { |
||
229 | $post_attachment .= '<br><img src="' . $icon_filetype . '" alt="' . $filetype . '" /><strong> ' . $att['nameDisplay'] . '</strong> <small>(' . $file_size . ')</small>'; |
||
230 | $post_attachment .= '<br>' . newbbAttachmentImage($att['name_saved']); |
||
231 | $isDisplayed = true; |
||
232 | } else { |
||
233 | if (empty($GLOBALS['xoopsModuleConfig']['show_userattach'])) { |
||
234 | $post_attachment .= '<a href="' |
||
235 | . XOOPS_URL |
||
236 | . '/modules/' |
||
237 | . $xoopsModule->getVar('dirname', 'n') |
||
238 | . '/dl_attachment.php?attachid=' |
||
239 | . $key |
||
240 | . '&post_id=' |
||
241 | . $this->getVar('post_id') |
||
242 | . '"> <img src="' |
||
243 | . $icon_filetype |
||
244 | . '" alt="' |
||
245 | . $filetype |
||
246 | . '" /> ' |
||
247 | . $att['nameDisplay'] |
||
248 | . '</a> ' |
||
249 | . _MD_NEWBB_FILESIZE |
||
250 | . ': ' |
||
251 | . $file_size |
||
252 | . '; ' |
||
253 | . _MD_NEWBB_HITS |
||
254 | . ': ' |
||
255 | . $att['numDownload']; |
||
256 | } elseif ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->uid() > 0 |
||
257 | && $GLOBALS['xoopsUser']->isactive()) { |
||
258 | $post_attachment .= '<a href="' |
||
259 | . XOOPS_URL |
||
260 | . '/modules/' |
||
261 | . $xoopsModule->getVar('dirname', 'n') |
||
262 | . '/dl_attachment.php?attachid=' |
||
263 | . $key |
||
264 | . '&post_id=' |
||
265 | . $this->getVar('post_id') |
||
266 | . '"> <img src="' |
||
267 | . $icon_filetype |
||
268 | . '" alt="' |
||
269 | . $filetype |
||
270 | . '" /> ' |
||
271 | . $att['nameDisplay'] |
||
272 | . '</a> ' |
||
273 | . _MD_NEWBB_FILESIZE |
||
274 | . ': ' |
||
275 | . $file_size |
||
276 | . '; ' |
||
277 | . _MD_NEWBB_HITS |
||
278 | . ': ' |
||
279 | . $att['numDownload']; |
||
280 | } else { |
||
281 | $post_attachment .= _MD_NEWBB_SEENOTGUEST; |
||
282 | } |
||
283 | } |
||
284 | $post_attachment .= '<br>'; |
||
285 | } |
||
286 | } |
||
287 | |||
288 | return $post_attachment; |
||
289 | } |
||
290 | // attachment functions |
||
291 | // //////////////////////////////////////////////////////////////////////////////////// |
||
292 | |||
293 | /** |
||
294 | * @param string $poster_name |
||
295 | * @param string $post_editmsg |
||
296 | * @return bool |
||
297 | */ |
||
298 | public function setPostEdit($poster_name = '', $post_editmsg = '') |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @return bool|string |
||
336 | */ |
||
337 | public function displayPostEdit() |
||
338 | { |
||
339 | global $myts; |
||
340 | |||
341 | if (empty($GLOBALS['xoopsModuleConfig']['recordedit_timelimit'])) { |
||
342 | return false; |
||
343 | } |
||
344 | |||
345 | $post_edit = ''; |
||
346 | $post_edits = $this->getVar('post_edit'); |
||
347 | if (!empty($post_edits)) { |
||
348 | $post_edits = unserialize(base64_decode($post_edits)); |
||
349 | } |
||
350 | if (!isset($post_edits) || !is_array($post_edits)) { |
||
351 | $post_edits = []; |
||
352 | } |
||
353 | if (is_array($post_edits) && count($post_edits) > 0) { |
||
354 | foreach ($post_edits as $postedit) { |
||
355 | $edit_time = (int)$postedit['edit_time']; |
||
356 | $edit_user = $postedit['edit_user']; |
||
357 | $edit_msg = !empty($postedit['edit_msg']) ? $postedit['edit_msg'] : ''; |
||
358 | // Start irmtfan add option to do only the latest edit when do_latestedit=0 (Alfred) |
||
359 | if (empty($GLOBALS['xoopsModuleConfig']['do_latestedit'])) { |
||
360 | $post_edit = ''; |
||
361 | } |
||
362 | // End irmtfan add option to do only the latest edit when do_latestedit=0 (Alfred) |
||
363 | // START hacked by irmtfan |
||
364 | // display/save all edit records. |
||
365 | $post_edit .= _MD_NEWBB_EDITEDBY . ' ' . $edit_user . ' ' . _MD_NEWBB_ON . ' ' . formatTimestamp($edit_time) . '<br>'; |
||
366 | // if reason is not empty |
||
367 | if ('' !== $edit_msg) { |
||
368 | $post_edit .= _MD_NEWBB_EDITEDMSG . ' ' . $edit_msg . '<br>'; |
||
369 | } |
||
370 | // START hacked by irmtfan |
||
371 | } |
||
372 | } |
||
373 | |||
374 | return $post_edit; |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @return array |
||
379 | */ |
||
380 | public function &getPostBody() |
||
381 | { |
||
382 | global $viewtopic_users; |
||
383 | $newbbConfig = newbbLoadConfig(); |
||
384 | include_once __DIR__ . '/../include/functions.user.php'; |
||
385 | include_once __DIR__ . '/../include/functions.render.php'; |
||
386 | |||
387 | $uid = is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0; |
||
388 | /** @var KarmaHandler $karmaHandler */ |
||
389 | $karmaHandler = Newbb\Helper::getInstance()->getHandler('Karma'); |
||
390 | $user_karma = $karmaHandler->getUserKarma(); |
||
391 | |||
392 | $post = []; |
||
393 | $post['attachment'] = false; |
||
394 | $post_text = newbbDisplayTarea($this->vars['post_text']['value'], $this->getVar('dohtml'), $this->getVar('dosmiley'), $this->getVar('doxcode'), $this->getVar('doimage'), $this->getVar('dobr')); |
||
395 | if (newbbIsAdmin($this->getVar('forum_id')) || $this->checkIdentity()) { |
||
396 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
397 | } elseif ($newbbConfig['enable_karma'] && $this->getVar('post_karma') > $user_karma) { |
||
398 | $post['text'] = sprintf(_MD_NEWBB_KARMA_REQUIREMENT, $user_karma, $this->getVar('post_karma')); |
||
399 | } elseif ($newbbConfig['allow_require_reply'] && $this->getVar('require_reply') |
||
400 | && (!$uid || !isset($viewtopic_users[$uid]))) { |
||
401 | $post['text'] = _MD_NEWBB_REPLY_REQUIREMENT; |
||
402 | } else { |
||
403 | $post['text'] = $post_text . '<br>' . $this->displayAttachment(); |
||
404 | } |
||
405 | /** @var \XoopsMemberHandler $memberHandler */ |
||
406 | $memberHandler = xoops_getHandler('member'); |
||
407 | $eachposter = $memberHandler->getUser($this->getVar('uid')); |
||
408 | if (is_object($eachposter) && $eachposter->isActive()) { |
||
409 | if ($newbbConfig['show_realname'] && $eachposter->getVar('name')) { |
||
410 | $post['author'] = $eachposter->getVar('name'); |
||
411 | } else { |
||
412 | $post['author'] = $eachposter->getVar('uname'); |
||
413 | } |
||
414 | unset($eachposter); |
||
415 | } else { |
||
416 | $post['author'] = $this->getVar('poster_name') ?: $GLOBALS['xoopsConfig']['anonymous']; |
||
417 | } |
||
418 | |||
419 | $post['subject'] = newbbHtmlspecialchars($this->vars['subject']['value']); |
||
420 | |||
421 | $post['date'] = $this->getVar('post_time'); |
||
422 | |||
423 | return $post; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * @return bool |
||
428 | */ |
||
429 | public function isTopic() |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * @param string $action_tag |
||
436 | * @return bool |
||
437 | */ |
||
438 | public function checkTimelimit($action_tag = 'edit_timelimit') |
||
439 | { |
||
440 | $newbbConfig = newbbLoadConfig(); |
||
441 | if (empty($newbbConfig['edit_timelimit'])) { |
||
442 | return true; |
||
443 | } |
||
444 | |||
445 | return ($this->getVar('post_time') > time() - $newbbConfig[$action_tag] * 60); |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * @param int $uid |
||
450 | * @return bool |
||
451 | */ |
||
452 | public function checkIdentity($uid = -1) |
||
453 | { |
||
454 | $uid = ($uid > -1) ? $uid : (is_object($GLOBALS['xoopsUser']) ? $GLOBALS['xoopsUser']->getVar('uid') : 0); |
||
455 | if ($this->getVar('uid') > 0) { |
||
456 | $user_ok = ($uid == $this->getVar('uid')); |
||
457 | } else { |
||
458 | static $user_ip; |
||
459 | if (!isset($user_ip)) { |
||
460 | $user_ip = \Xmf\IPAddress::fromRequest()->asReadable(); |
||
461 | } |
||
462 | $user_ok = ($user_ip == $this->getVar('poster_ip')); |
||
463 | } |
||
464 | |||
465 | return $user_ok; |
||
466 | } |
||
467 | |||
468 | // TODO: cleaning up and merge with post hanldings in viewpost.php |
||
469 | |||
470 | /** |
||
471 | * @param $isAdmin |
||
472 | * @return array |
||
473 | */ |
||
474 | public function showPost($isAdmin) |
||
713 | } |
||
714 | } |
||
715 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.