From 0501e65cea33716deb1d22b81f7764b21382f5d2 Mon Sep 17 00:00:00 2001 From: Marc Alexander Date: Sun, 18 Jan 2015 18:54:43 +0100 Subject: [PATCH] [ticket/432] Add methods for checking conditionals for post attachments B3P-432 --- portal/fetch_posts.php | 80 +++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/portal/fetch_posts.php b/portal/fetch_posts.php index 2f3c8dd1..4e9b9fdc 100644 --- a/portal/fetch_posts.php +++ b/portal/fetch_posts.php @@ -166,23 +166,8 @@ class fetch_posts while ($row = $this->db->sql_fetchrow($result)) { - $attachments = array(); - if (($this->auth->acl_get('u_download') && ($this->auth->acl_get('f_download', $row['forum_id']) || $row['forum_id'] == 0)) && $this->config['allow_attachments'] && $row['post_id'] && $row['post_attachment']) - { - // Pull attachment data - $sql2 = 'SELECT * - FROM ' . ATTACHMENTS_TABLE . ' - WHERE post_msg_id = '. $row['post_id'] .' - AND in_message = 0 - ORDER BY filetime DESC'; - $result2 = $this->db->sql_query($sql2); - - while ($row2 = $this->db->sql_fetchrow($result2)) - { - $attachments[] = $row2; - } - $this->db->sql_freeresult($result2); - } + // Get attachments + $attachments = $this->get_post_attachments($row); $posts[$i]['bbcode_uid'] = $row['bbcode_uid']; $len_check = $row['post_text']; @@ -559,4 +544,65 @@ class fetch_posts { return (!empty($setting)) ? $string_on : $string_off; } + + /** + * Assert that all supplied arguments evaluate to true + * + * @return bool True if all evaluate to true, false if not + */ + protected function assert_all_true() + { + $args = func_get_args(); + $return = true; + + foreach ($args as $argument) + { + $return = $return && $argument; + } + + return $return; + } + + /** + * Get attachments of posts + * + * @param array $row Database row of post + * + * @return array Attachment data + */ + protected function get_post_attachments($row) + { + $attachments = array(); + + if ($this->user_can_download($row['forum_id']) && $this->assert_all_true($this->config['allow_attachments'], $row['post_id'], $row['post_attachment'])) + { + // Pull attachment data + $sql = 'SELECT * + FROM ' . ATTACHMENTS_TABLE . ' + WHERE post_msg_id = '. (int) $row['post_id'] .' + AND in_message = 0 + ORDER BY filetime DESC'; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + $attachments[] = $row; + } + $this->db->sql_freeresult($result); + } + + return $attachments; + } + + /** + * Check if user can download a file in this forum + * + * @param int $forum_id Forum ID to check + * + * @return bool True if user can download, false if not + */ + protected function user_can_download($forum_id) + { + return $this->auth->acl_get('u_download') && ($this->auth->acl_get('f_download', $forum_id) || $forum_id == 0); + } }