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