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