Pastebin

fetch_posts.php

von Kirk
Beschreibung:
fetch_posts.php von Yetikisdorf
Snippet erstellt:
19.02.2017 18:30
Snippet wird automatisch gelöscht:
Niemals

Dein Code:
  1. <?php
  2. /**
  3. *
  4. * @package Board3 Portal v2.1
  5. * @copyright (c) 2014 Board3 Group ( www.board3.de )
  6. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
  7. *
  8. */
  9.  
  10. namespace board3\portal\portal;
  11.  
  12. class fetch_posts
  13. {
  14.     /**
  15.     * phpBB auth
  16.     * @var \phpbb\auth\auth
  17.     */
  18.     protected $auth;
  19.  
  20.     /**
  21.     * phpBB cache
  22.     * @var \phpbb\cache\driver
  23.     */
  24.     protected $cache;
  25.  
  26.     /**
  27.     * phpBB config
  28.     * @var \phpbb\config\config
  29.     */
  30.     protected $config;
  31.  
  32.     /**
  33.     * phpBB db driver
  34.     * @var \phpbb\db\driver_interface
  35.     */
  36.     protected $db;
  37.  
  38.     /**
  39.     * Modules helper
  40.     * @var \board3\portal\includes\modules_helper
  41.     */
  42.     protected $modules_helper;
  43.  
  44.     /**
  45.     * phpBB user
  46.     * @var \phpbb\user
  47.     */
  48.     protected $user;
  49.  
  50.     /**
  51.     * SQL Query where constraint
  52.     * @var string
  53.     */
  54.     protected $where_string = '';
  55.  
  56.     /**
  57.     * SQL topic type constraint
  58.     * @var string
  59.     */
  60.     protected $topic_type = '';
  61.  
  62.     /**
  63.     * SQL user link constraint
  64.     * @var string
  65.     */
  66.     protected $user_link = '';
  67.  
  68.     /**
  69.     * SQL post link constraint
  70.     * @var string
  71.     */
  72.     protected $post_link = '';
  73.  
  74.     /**
  75.     * SQL topic order constraint
  76.     * @var string
  77.     */
  78.     protected $topic_order = '';
  79.  
  80.     /**
  81.     * Module ID
  82.     * @var int
  83.     */
  84.     protected $module_id;
  85.  
  86.     /**
  87.     * Forum ID to use for global topics
  88.     * @var int
  89.     */
  90.     protected $global_id;
  91.  
  92.     /**
  93.     * Type of posts to fetch
  94.     * @var string
  95.     */
  96.     protected $type;
  97.  
  98.     /**
  99.     * Constructor
  100.     * NOTE: The parameters of this method must match in order and type with
  101.     * the dependencies defined in the services.yml file for this service.
  102.     * @param \phpbb\auth\auth            $auth    phpBB auth object
  103.     * @param \phpbb\cache\driver            $cache    phpBB cache driver
  104.     * @param \phpbb\config\config            $config    phpBB config
  105.     * @param \phpbb\db\driver_interface        $db    phpBB database driver
  106.     * @param \board3\portal\includes\modules_helper    $modules_helper Board3 modules helper
  107.     * @param \phpbb\user                $user    phpBB user object
  108.     */
  109.     public function __construct($auth, $cache, $config, $db, $modules_helper, $user)
  110.     {
  111.         $this->auth = $auth;
  112.         $this->cache = $cache;
  113.         $this->config = $config;
  114.         $this->db = $db;
  115.         $this->modules_helper = $modules_helper;
  116.         $this->user = $user;
  117.     }
  118.  
  119.     /**
  120.     * Get posts defined by type and other settings
  121.     *
  122.     * @param string $forum_from Forums from which the posts should be retrieved
  123.     * @param bool $permissions Whether permissions should be taken into account
  124.     *                     during retrieval
  125.     * @param int $number_of_posts Number of posts to get
  126.     * @param int $text_length Length the text should be shortened to
  127.     * @param int $time The amount of days ago the posts could have been posted
  128.     * @param string $type Type of posts to get
  129.     * @param int $start At which position the query should start
  130.     * @param bool $invert Whether the permissions should be inverted
  131.     *
  132.     * @return array An array containing the posts that were retrieved from the database
  133.     */
  134.     public function get_posts($forum_from, $permissions, $number_of_posts, $text_length, $time, $type, $start = 0, $invert = false)
  135.     {
  136.         $posts = array();
  137.         $post_time = $this->get_setting_based_data($time == 0, '', 'AND t.topic_time > ' . (time() - $time * 86400));
  138.         $forum_from = $this->get_setting_based_data(strpos($forum_from, ',') !== false, explode(',', $forum_from), $this->get_setting_based_data($forum_from != '', array($forum_from), array()));
  139.         $topic_icons = array(0);
  140.         $have_icons = 0;
  141.         $this->global_id = 0;
  142.         $this->type = $type;
  143.         $this->reset_constraints();
  144.  
  145.         // Get disallowed forums
  146.         $disallow_access = $this->modules_helper->get_disallowed_forums($permissions);
  147.  
  148.         // Set forums that should be excluded or included
  149.         if (!$this->set_forum_constraints($forum_from, $disallow_access, $invert))
  150.         {
  151.             return array();
  152.         }
  153.  
  154.         // Get SQL constraints
  155.         $this->get_type_constraints();
  156.  
  157.         // Get global forum ID for announcements
  158.         if (!$this->get_global_id())
  159.         {
  160.             return array();
  161.         }
  162.  
  163.         $result = $this->run_sql_query($post_time, $number_of_posts, $start);
  164.  
  165.         $i = 0;
  166.  
  167.         // Fill posts array
  168.         while ($row = $this->db->sql_fetchrow($result))
  169.         {
  170.             $this->fill_posts_array($row, $text_length, $i, $have_icons, $posts, $topic_icons);
  171.             ++$i;
  172.         }
  173.         $this->db->sql_freeresult($result);
  174.  
  175.         $posts['topic_icons'] = $this->get_setting_based_data(max($topic_icons) > 0 && $have_icons, true, false);
  176.         $posts['topic_count'] = $i;
  177.  
  178.         if ($this->global_id < 1)
  179.         {
  180.             return array();
  181.         }
  182.         else
  183.         {
  184.             return $posts;
  185.         }
  186.     }
  187.  
  188.     /**
  189.     * Run SQL query for fetching posts from database
  190.     *
  191.     * @param int    $post_time        Post time
  192.     * @param int    $number_of_posts    Number of posts to fetch
  193.     * @param int    $start            Start of query
  194.     *
  195.     * @return int Result pointer
  196.     */
  197.     protected function run_sql_query($post_time, $number_of_posts, $start)
  198.     {
  199.         $sql_array = array(
  200.             'SELECT' => 't.forum_id,
  201.                t.topic_id,
  202.                t.topic_last_post_id,
  203.                t.topic_last_post_time,
  204.                t.topic_time,
  205.                t.topic_title,
  206.                t.topic_attachment,
  207.                t.topic_views,
  208.                t.poll_title,
  209.                t.topic_posts_approved,
  210.                t.topic_posts_unapproved,
  211.                t.topic_posts_softdeleted,
  212.                t.topic_poster,
  213.                t.topic_type,
  214.                t.topic_status,
  215.                t.topic_last_poster_name,
  216.                t.topic_last_poster_id,
  217.                t.topic_last_poster_colour,
  218.                t.icon_id,
  219.                u.username,
  220.                u.user_id,
  221.                u.user_type,
  222.                u.user_colour,
  223.                p.post_id,
  224.                p.poster_id,
  225.                p.post_time,
  226.                p.post_text,
  227.                p.post_attachment,
  228.                p.post_username,
  229.                p.enable_smilies,
  230.                p.enable_bbcode,
  231.                p.enable_magic_url,
  232.                p.bbcode_bitfield,
  233.                p.bbcode_uid,
  234.                f.forum_name,
  235.                f.enable_icons',
  236.             'FROM' => array(
  237.                 TOPICS_TABLE => 't',
  238.             ),
  239.             'LEFT_JOIN' => array(
  240.                 array(
  241.                     'FROM' => array(USERS_TABLE => 'u'),
  242.                     'ON' => $this->user_link,
  243.                 ),
  244.                 array(
  245.                     'FROM' => array(FORUMS_TABLE => 'f'),
  246.                     'ON' => 't.forum_id=f.forum_id',
  247.                 ),
  248.                 array(
  249.                     'FROM' => array(POSTS_TABLE => 'p'),
  250.                     'ON' => $this->post_link,
  251.                 ),
  252.             ),
  253.             'WHERE' => $this->topic_type . '
  254.                    ' . $post_time . '
  255.                    AND t.topic_status <> ' . ITEM_MOVED . '
  256.                    AND t.topic_visibility = 1
  257.                    AND t.topic_moved_id = 0
  258.                    ' . $this->where_string,
  259.             'ORDER_BY' => $this->topic_order,
  260.         );
  261.  
  262.         $sql_array['LEFT_JOIN'][] = array('FROM' => array(TOPICS_POSTED_TABLE => 'tp'), 'ON' => 'tp.topic_id = t.topic_id AND tp.user_id = ' . $this->user->data['user_id']);
  263.         $sql_array['SELECT'] .= ', tp.topic_posted';
  264.         $sql = $this->db->sql_build_query('SELECT', $sql_array);
  265.  
  266.         if ($number_of_posts != 0)
  267.         {
  268.             $result = $this->db->sql_query_limit($sql, $number_of_posts, $start);
  269.         }
  270.         else
  271.         {
  272.             $result = $this->db->sql_query($sql);
  273.         }
  274.  
  275.         return $result;
  276.     }
  277.  
  278.     /**
  279.      * Fill posts array with data
  280.      *
  281.      * @param array $row Database row
  282.      * @param int $text_length Text length
  283.      * @param int $i Array pointer
  284.      * @param int $have_icons Whether any post has icons
  285.      * @param array $posts The posts array
  286.      * @param array $topic_icons List of topic icons
  287.      */
  288.     public function fill_posts_array($row, $text_length, $i, &$have_icons, &$posts, &$topic_icons)
  289.     {
  290.         $update_count = array();
  291.  
  292.         // Get attachments
  293.         $attachments = $this->get_post_attachments($row);
  294.  
  295.         $posts[$i]['bbcode_uid'] = $row['bbcode_uid'];
  296.  
  297.         // Format message
  298.         $message = $this->format_message($row, $text_length, $posts[$i]['striped']);
  299.  
  300.         $row['bbcode_options'] = $this->get_setting_based_data($row['enable_bbcode'], OPTION_FLAG_BBCODE, 0) + $this->get_setting_based_data($row['enable_smilies'], OPTION_FLAG_SMILIES, 0) + $this->get_setting_based_data($row['enable_magic_url'], OPTION_FLAG_LINKS, 0);
  301.         $message = generate_text_for_display($message, $row['bbcode_uid'], $row['bbcode_bitfield'], $row['bbcode_options']);
  302.  
  303.         if (!empty($attachments))
  304.         {
  305.             parse_attachments($row['forum_id'], $message, $attachments, $update_count);
  306.         }
  307.  
  308.         // Get proper global ID
  309.         $this->global_id = $this->get_setting_based_data($this->global_id, $this->global_id, $row['forum_id']);
  310.  
  311.         $topic_icons[] = $row['enable_icons'];
  312.         $have_icons = $this->get_setting_based_data($row['icon_id'], 1, $have_icons);
  313.  
  314.         $posts[$i] = array_merge($posts[$i], array(
  315.             'post_text'                => ap_validate($message),
  316.             'topic_id'                => $row['topic_id'],
  317.             'topic_last_post_id'    => $row['topic_last_post_id'],
  318.             'topic_type'            => $row['topic_type'],
  319.             'topic_posted'            => $this->get_setting_based_data(isset($row['topic_posted']) && $row['topic_posted'], true, false),
  320.             'icon_id'                => $row['icon_id'],
  321.             'topic_status'            => $row['topic_status'],
  322.             'forum_id'                => $row['forum_id'],
  323.             'topic_replies'            => $row['topic_posts_approved'] + $row['topic_posts_unapproved'] + $row['topic_posts_softdeleted'],
  324.             'topic_replies_real'    => $row['topic_posts_approved'],
  325.             'topic_time'            => $this->user->format_date($row['post_time']),
  326.             'topic_last_post_time'    => $row['topic_last_post_time'],
  327.             'topic_title'            => $row['topic_title'],
  328.             'username'                => $row['username'],
  329.             'username_full'            => get_username_string('full', $row['user_id'], $row['username'], $row['user_colour'], $row['post_username']),
  330.             'username_full_last'    => get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour'], $row['topic_last_poster_name']),
  331.             'user_id'                => $row['user_id'],
  332.             'user_type'                => $row['user_type'],
  333.             'user_colour'            => $row['user_colour'],
  334.             'poll'                    => $this->get_setting_based_data($row['poll_title'], true, false),
  335.             'attachment'            => $this->get_setting_based_data($row['topic_attachment'], true, false),
  336.             'topic_views'            => $row['topic_views'],
  337.             'forum_name'            => $row['forum_name'],
  338.             'attachments'            => $this->get_setting_based_data($attachments, $attachments, array()),
  339.         ));
  340.         $posts['global_id'] = $this->global_id;
  341.     }
  342.  
  343.     /**
  344.     * Get type constraints for database query
  345.     *
  346.     * @return null
  347.     * @throws \InvalidArgumentexception If unknown type is used
  348.     */
  349.     protected function get_type_constraints()
  350.     {
  351.         switch($this->type)
  352.         {
  353.             case "announcements":
  354.                 $this->get_announcements_constraints();
  355.             break;
  356.             case "news":
  357.                 $this->get_news_constraints();
  358.             break;
  359.             case "news_all":
  360.                 $this->get_news_all_constraints();
  361.             break;
  362.  
  363.             default:
  364.                 // Method was called with unsupported type
  365.                 throw new \InvalidArgumentexception($this->user->lang('B3P_WRONG_METHOD_CALL', __FUNCTION__));
  366.         }
  367.     }
  368.  
  369.     /**
  370.     * Get type constraints for announcements
  371.     *
  372.     * @return null
  373.     */
  374.     protected function get_announcements_constraints()
  375.     {
  376.         $this->topic_type = '((t.topic_type = ' . POST_ANNOUNCE . ') OR (t.topic_type = ' . POST_GLOBAL . '))';
  377.         $this->where_string = (strlen($this->where_string) > 0) ? 'AND (t.forum_id = 0 OR (' . trim(substr($this->where_string, 0, -4)) . '))' : '';
  378.         $this->user_link = 't.topic_poster = u.user_id';
  379.         $this->post_link = 't.topic_first_post_id = p.post_id';
  380.         $this->topic_order = 't.topic_time DESC';
  381.     }
  382.  
  383.     /**
  384.     * Get type constraints for news
  385.     *
  386.     * @return null
  387.     */
  388.     protected function get_news_constraints()
  389.     {
  390.         $this->topic_type = 't.topic_type = ' . POST_NORMAL;
  391.         $this->where_string = (strlen($this->where_string) > 0) ? 'AND (' . trim(substr($this->where_string, 0, -4)) . ')' : '';
  392.         $this->user_link = $this->get_setting_based_data($this->config['board3_news_style_' . $this->module_id], 't.topic_poster = u.user_id', $this->get_setting_based_data($this->config['board3_news_show_last_' . $this->module_id], 't.topic_last_poster_id = u.user_id', 't.topic_poster = u.user_id')) ;
  393.         $this->post_link = $this->get_setting_based_data($this->config['board3_news_style_' . $this->module_id], 't.topic_first_post_id = p.post_id', $this->get_setting_based_data($this->config['board3_news_show_last_' . $this->module_id], 't.topic_last_post_id = p.post_id', 't.topic_first_post_id = p.post_id'));
  394.         $this->topic_order = $this->get_setting_based_data($this->config['board3_news_show_last_' . $this->module_id], 't.topic_last_post_time DESC', 't.topic_time DESC');
  395.     }
  396.  
  397.     /**
  398.     * Get additional type constraints for all news
  399.     * Overwrites topic type of get_news_constraints().
  400.     *
  401.     * @return null
  402.     */
  403.     protected function get_news_all_constraints()
  404.     {
  405.         $this->get_news_constraints();
  406.         $this->topic_type = '(t.topic_type <> ' . POST_ANNOUNCE . ') AND (t.topic_type <> ' . POST_GLOBAL . ')';
  407.     }
  408.  
  409.     /**
  410.     * Set module id
  411.     *
  412.     * @param    int    $module_id    Module ID
  413.     * @return null
  414.     */
  415.     public function set_module_id($module_id)
  416.     {
  417.         $this->module_id = $module_id;
  418.     }
  419.  
  420.     /**
  421.     * Set forums to exclude or include
  422.     *
  423.     * @param    array    $forum_from    Forums that should be shown or
  424.     *                    excluded from being shown
  425.     * @param    array    $disallowed_forums    Forums that user is not
  426.     *                    allowed to see
  427.     * @param    bool    $invert        Whether forum IDs in forum_from
  428.     *                    should be used for excluding or
  429.     *                    including forums
  430.     * @return bool True if valid constraints were generated, false if not
  431.     */
  432.     protected function set_forum_constraints($forum_from, $disallowed_forums, $invert = false)
  433.     {
  434.         if ($invert == true || empty($forum_from))
  435.         {
  436.             $access_list = array_merge($disallowed_forums, $forum_from);
  437.             $sql_operator = '<>';
  438.             $sql_append = 'AND';
  439.         }
  440.         else
  441.         {
  442.             $access_list = array_diff($forum_from, $disallowed_forums);
  443.             $sql_operator = '=';
  444.             $sql_append = 'OR';
  445.  
  446.             if (empty($access_list) && !empty($forum_from))
  447.             {
  448.                 return false;
  449.             }
  450.         }
  451.  
  452.         // Generate where string
  453.         $this->generate_where_string($access_list, $sql_operator, $sql_append);
  454.  
  455.         return true;
  456.     }
  457.  
  458.     /**
  459.     * Generate where string for database query
  460.     *
  461.     * @param    array    $access_list    Array containing the forum IDs
  462.     *                    the user has access to
  463.     * @param    string    $sql_operator    The sql operator to use
  464.     * @param    string    $sql_append    The sql append type to use.
  465.     *                    Should be either AND or OR
  466.     * @return null
  467.     */
  468.     protected function generate_where_string($access_list, $sql_operator, $sql_append)
  469.     {
  470.         foreach ($access_list as $acc_id)
  471.         {
  472.             $acc_id = (int) $acc_id;
  473.             $this->where_string .= 't.forum_id ' . $sql_operator . " $acc_id $sql_append ";
  474.             if ($sql_operator === '=' && $this->type == 'announcements' && $this->global_id < 1 && $acc_id > 0)
  475.             {
  476.                 $this->global_id = $acc_id;
  477.             }
  478.         }
  479.     }
  480.  
  481.     /**
  482.     * Gets the a global forum ID for global announcements
  483.     *
  484.     * @return bool True if proper ID was selected, false if not
  485.     */
  486.     protected function get_global_id()
  487.     {
  488.         if ($this->type == 'announcements' && $this->global_id < 1)
  489.         {
  490.             if (!empty($this->where_string) || ($row = $this->cache->get('_forum_id_first_forum_post')) === false)
  491.             {
  492.                 $row = $this->get_first_forum_id();
  493.             }
  494.  
  495.             if (empty($row))
  496.             {
  497.                 return false;
  498.             }
  499.             $this->global_id = $row['forum_id'];
  500.         }
  501.  
  502.         return true;
  503.     }
  504.  
  505.     /**
  506.     * Gets the first forum_id of FORUM_POST type forums
  507.     *
  508.     * @return array Database row of query
  509.     */
  510.     protected function get_first_forum_id()
  511.     {
  512.         $sql = 'SELECT forum_id
  513.            FROM ' . FORUMS_TABLE . '
  514.            WHERE forum_type = ' . FORUM_POST . '
  515.            ' . str_replace('t.', '', $this->where_string) . '
  516.            ORDER BY forum_id';
  517.         $result = $this->db->sql_query_limit($sql, 1);
  518.         $row = $this->db->sql_fetchrow($result);
  519.         $this->db->sql_freeresult($result);
  520.  
  521.         if (empty($this->where_string))
  522.         {
  523.             // Cache first forum ID for one day = 86400 s
  524.             $this->cache->put('_forum_id_first_forum_post', $row, 86400);
  525.         }
  526.  
  527.         return $row;
  528.     }
  529.  
  530.     /**
  531.     * Resets constraints that might have been set before
  532.     *
  533.     * @return null
  534.     */
  535.     protected function reset_constraints()
  536.     {
  537.         $this->where_string = '';
  538.     }
  539.  
  540.     /**
  541.      * Get valid data based on setting
  542.      *
  543.      * @param mixed $setting Setting to check
  544.      * @param mixed $setting_true Data if setting is 'on' (not empty)
  545.      * @param mixed $setting_false Data if setting is 'off' (empty or 0)
  546.      *
  547.      * @return mixed Valid data based on setting
  548.      */
  549.     protected function get_setting_based_data($setting, $setting_true, $setting_false)
  550.     {
  551.         return (!empty($setting)) ? $setting_true : $setting_false;
  552.     }
  553.  
  554.     /**
  555.      * Assert that all supplied arguments evaluate to true
  556.      *
  557.      * @return bool True if all evaluate to true, false if not
  558.      */
  559.     protected function assert_all_true()
  560.     {
  561.         $args = func_get_args();
  562.         $return = true;
  563.  
  564.         foreach ($args as $argument)
  565.         {
  566.             $return = $return && $argument;
  567.         }
  568.  
  569.         return $return;
  570.     }
  571.  
  572.     /**
  573.      * Get attachments of posts
  574.      *
  575.      * @param array $row Database row of post
  576.      *
  577.      * @return array Attachment data
  578.      */
  579.     protected function get_post_attachments($row)
  580.     {
  581.         $attachments = array();
  582.  
  583.         if ($this->user_can_download($row['forum_id']) && $this->assert_all_true($this->config['allow_attachments'], $row['post_id'], $row['post_attachment']))
  584.         {
  585.             // Pull attachment data
  586.             $sql = 'SELECT *
  587.                    FROM ' . ATTACHMENTS_TABLE . '
  588.                    WHERE post_msg_id = '. (int) $row['post_id'] .'
  589.                    AND in_message = 0
  590.                    ORDER BY filetime DESC';
  591.             $result = $this->db->sql_query($sql);
  592.  
  593.             while ($row = $this->db->sql_fetchrow($result))
  594.             {
  595.                 $attachments[] = $row;
  596.             }
  597.             $this->db->sql_freeresult($result);
  598.         }
  599.  
  600.         return $attachments;
  601.     }
  602.  
  603.     /**
  604.      * Check if user can download a file in this forum
  605.      *
  606.      * @param int $forum_id Forum ID to check
  607.      *
  608.      * @return bool True if user can download, false if not
  609.      */
  610.     protected function user_can_download($forum_id)
  611.     {
  612.         return $this->auth->acl_get('u_download') && ($this->auth->acl_get('f_download', $forum_id) || $forum_id == 0);
  613.     }
  614.  
  615.     /**
  616.      * Format message for display
  617.      *
  618.      * @param array $row Database row
  619.      * @param int $text_length Length of text
  620.      * @param bool $posts_striped Whether post is striped
  621.      *
  622.      * @return mixed|string
  623.      */
  624.     protected function format_message($row, $text_length, &$posts_striped)
  625.     {
  626.         if (($text_length !== 0) && (strlen($row['post_text']) > $text_length))
  627.         {
  628.             $message = str_replace(array("\n", "\r"), array('<br />', "\n"), $row['post_text']);
  629.             $message = get_sub_taged_string($message, $row['bbcode_uid'], $text_length);
  630.             $posts_striped = true;
  631.         }
  632.         else
  633.         {
  634.             $message = str_replace("\n", '<br/> ', $row['post_text']);
  635.         }
  636.  
  637.         return $message;
  638.     }
  639. }

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. Alternativ kannst du den gesamten Eintrag auch als Datei herunterladen.