[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/phpbb/feed/controller/ -> feed.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\feed\controller;
  15  
  16  use phpbb\auth\auth;
  17  use phpbb\config\config;
  18  use phpbb\db\driver\driver_interface;
  19  use \phpbb\event\dispatcher_interface;
  20  use phpbb\exception\http_exception;
  21  use phpbb\feed\feed_interface;
  22  use phpbb\feed\exception\feed_unavailable_exception;
  23  use phpbb\feed\exception\unauthorized_exception;
  24  use phpbb\feed\helper as feed_helper;
  25  use phpbb\controller\helper as controller_helper;
  26  use phpbb\symfony_request;
  27  use phpbb\user;
  28  use Symfony\Component\DependencyInjection\ContainerInterface;
  29  use Symfony\Component\HttpFoundation\Response;
  30  use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  31  
  32  class feed
  33  {
  34      /**
  35       * @var \Twig_Environment
  36       */
  37      protected $template;
  38  
  39      /**
  40       * @var symfony_request
  41       */
  42      protected $request;
  43  
  44      /**
  45       * @var controller_helper
  46       */
  47      protected $controller_helper;
  48  
  49      /**
  50       * @var config
  51       */
  52      protected $config;
  53  
  54      /**
  55       * @var driver_interface
  56       */
  57      protected $db;
  58  
  59      /**
  60       * @var ContainerInterface
  61       */
  62      protected $container;
  63  
  64      /**
  65       * @var feed_helper
  66       */
  67      protected $feed_helper;
  68  
  69      /**
  70       * @var user
  71       */
  72      protected $user;
  73  
  74      /**
  75       * @var auth
  76       */
  77      protected $auth;
  78  
  79      /**
  80       * @var dispatcher_interface
  81       */
  82      protected $phpbb_dispatcher;
  83  
  84      /**
  85       * @var string
  86       */
  87      protected $php_ext;
  88  
  89      /**
  90       * Constructor
  91       *
  92       * @param \Twig_Environment $twig
  93       * @param symfony_request $request
  94       * @param controller_helper $controller_helper
  95       * @param config $config
  96       * @param driver_interface $db
  97       * @param ContainerInterface $container
  98       * @param feed_helper $feed_helper
  99       * @param user $user
 100       * @param auth $auth
 101       * @param dispatcher_interface $phpbb_dispatcher
 102       * @param string $php_ext
 103       */
 104  	public function __construct(\Twig_Environment $twig, symfony_request $request, controller_helper $controller_helper, config $config, driver_interface $db, ContainerInterface $container, feed_helper $feed_helper, user $user, auth $auth, dispatcher_interface $phpbb_dispatcher, $php_ext)
 105      {
 106          $this->request = $request;
 107          $this->controller_helper = $controller_helper;
 108          $this->config = $config;
 109          $this->db = $db;
 110          $this->container = $container;
 111          $this->feed_helper = $feed_helper;
 112          $this->user = $user;
 113          $this->auth = $auth;
 114          $this->php_ext = $php_ext;
 115          $this->template = $twig;
 116          $this->phpbb_dispatcher = $phpbb_dispatcher;
 117      }
 118  
 119      /**
 120       * Controller for /feed/forums route
 121       *
 122       * @return Response
 123       *
 124       * @throws http_exception when the feed is disabled
 125       */
 126  	public function forums()
 127      {
 128          if (!$this->config['feed_overall_forums'])
 129          {
 130              $this->send_unavailable();
 131          }
 132  
 133          return $this->send_feed($this->container->get('feed.forums'));
 134      }
 135  
 136      /**
 137       * Controller for /feed/news route
 138       *
 139       * @return Response
 140       *
 141       * @throws http_exception when the feed is disabled
 142       */
 143  	public function news()
 144      {
 145          // Get at least one news forum
 146          $sql = 'SELECT forum_id
 147                      FROM ' . FORUMS_TABLE . '
 148                      WHERE ' . $this->db->sql_bit_and('forum_options', FORUM_OPTION_FEED_NEWS, '<> 0');
 149          $result = $this->db->sql_query_limit($sql, 1, 0, 600);
 150          $s_feed_news = (int) $this->db->sql_fetchfield('forum_id');
 151          $this->db->sql_freeresult($result);
 152  
 153          if (!$s_feed_news)
 154          {
 155              $this->send_unavailable();
 156          }
 157  
 158          return $this->send_feed($this->container->get('feed.news'));
 159      }
 160  
 161      /**
 162       * Controller for /feed/topics route
 163       *
 164       * @return Response
 165       *
 166       * @throws http_exception when the feed is disabled
 167       */
 168  	public function topics()
 169      {
 170          if (!$this->config['feed_topics_new'])
 171          {
 172              $this->send_unavailable();
 173          }
 174  
 175          return $this->send_feed($this->container->get('feed.topics'));
 176      }
 177  
 178      /**
 179       * Controller for /feed/topics_new route
 180       *
 181       * @return Response
 182       *
 183       * @throws http_exception when the feed is disabled
 184       */
 185  	public function topics_new()
 186      {
 187          return $this->topics();
 188      }
 189  
 190      /**
 191       * Controller for /feed/topics_active route
 192       *
 193       * @return Response
 194       *
 195       * @throws http_exception when the feed is disabled
 196       */
 197  	public function topics_active()
 198      {
 199          if (!$this->config['feed_topics_active'])
 200          {
 201              $this->send_unavailable();
 202          }
 203  
 204          return $this->send_feed($this->container->get('feed.topics_active'));
 205      }
 206  
 207      /**
 208       * Controller for /feed/forum/{forum_id} route
 209       *
 210       * @param int $forum_id
 211       *
 212       * @return Response
 213       *
 214       * @throws http_exception when the feed is disabled
 215       */
 216  	public function forum($forum_id)
 217      {
 218          if (!$this->config['feed_forum'])
 219          {
 220              $this->send_unavailable();
 221          }
 222  
 223          return $this->send_feed($this->container->get('feed.forum')->set_forum_id($forum_id));
 224      }
 225  
 226      /**
 227       * Controller for /feed/topic/{topic_id} route
 228       *
 229       * @param int $topic_id
 230       *
 231       * @return Response
 232       *
 233       * @throws http_exception when the feed is disabled
 234       */
 235  	public function topic($topic_id)
 236      {
 237          if (!$this->config['feed_topic'])
 238          {
 239              $this->send_unavailable();
 240          }
 241  
 242          return $this->send_feed($this->container->get('feed.topic')->set_topic_id($topic_id));
 243      }
 244  
 245      /**
 246       * Controller for /feed/{mode] route
 247       *
 248       * @return Response
 249       *
 250       * @throws http_exception when the feed is disabled
 251       */
 252  	public function overall()
 253      {
 254          if (!$this->config['feed_overall'])
 255          {
 256              $this->send_unavailable();
 257          }
 258  
 259          return $this->send_feed($this->container->get('feed.overall'));
 260      }
 261  
 262      /**
 263       * Display a given feed
 264       *
 265       * @param feed_interface $feed
 266       *
 267       * @return Response
 268       */
 269  	protected function send_feed(feed_interface $feed)
 270      {
 271          try
 272          {
 273              return $this->send_feed_do($feed);
 274          }
 275          catch (feed_unavailable_exception $e)
 276          {
 277              throw new http_exception(Response::HTTP_NOT_FOUND, $e->getMessage(), $e->get_parameters(), $e);
 278          }
 279          catch (unauthorized_exception $e)
 280          {
 281              throw new http_exception(Response::HTTP_FORBIDDEN, $e->getMessage(), $e->get_parameters(), $e);
 282          }
 283      }
 284  
 285      /**
 286       * Really send the feed
 287       *
 288       * @param feed_interface $feed
 289       *
 290       * @return Response
 291       *
 292       * @throw exception\feed_exception
 293       */
 294  	protected function send_feed_do(feed_interface $feed)
 295      {
 296          $feed_updated_time = 0;
 297          $item_vars = array();
 298  
 299          $board_url = $this->feed_helper->get_board_url();
 300  
 301          // Open Feed
 302          $feed->open();
 303  
 304          // Iterate through items
 305          while ($row = $feed->get_item())
 306          {
 307              /**
 308               * Event to modify the feed row
 309               *
 310               * @event core.feed_modify_feed_row
 311               * @var    int        forum_id    Forum ID
 312               * @var    string    mode        Feeds mode (forums|topics|topics_new|topics_active|news)
 313               * @var    array    row            Array with feed data
 314               * @var    int        topic_id    Topic ID
 315               *
 316               * @since 3.1.10-RC1
 317               */
 318              $vars = array('forum_id', 'mode', 'row', 'topic_id');
 319              extract($this->phpbb_dispatcher->trigger_event('core.feed_modify_feed_row', compact($vars)));
 320  
 321              // BBCode options to correctly disable urls, smilies, bbcode...
 322              if ($feed->get('options') === null)
 323              {
 324                  // Allow all combinations
 325                  $options = 7;
 326  
 327                  if ($feed->get('enable_bbcode') !== null && $feed->get('enable_smilies') !== null && $feed->get('enable_magic_url') !== null)
 328                  {
 329                      $options = (($row[$feed->get('enable_bbcode')]) ? OPTION_FLAG_BBCODE : 0) + (($row[$feed->get('enable_smilies')]) ? OPTION_FLAG_SMILIES : 0) + (($row[$feed->get('enable_magic_url')]) ? OPTION_FLAG_LINKS : 0);
 330                  }
 331              }
 332              else
 333              {
 334                  $options = $row[$feed->get('options')];
 335              }
 336  
 337              $title = (isset($row[$feed->get('title')]) && $row[$feed->get('title')] !== '') ? $row[$feed->get('title')] : ((isset($row[$feed->get('title2')])) ? $row[$feed->get('title2')] : '');
 338  
 339              $published = ($feed->get('published') !== null) ? (int) $row[$feed->get('published')] : 0;
 340              $updated = ($feed->get('updated') !== null) ? (int) $row[$feed->get('updated')] : 0;
 341  
 342              $display_attachments = ($this->auth->acl_get('u_download') && $this->auth->acl_get('f_download', $row['forum_id']) && isset($row['post_attachment']) && $row['post_attachment']) ? true : false;
 343  
 344              $item_row = array(
 345                  'author'        => ($feed->get('creator') !== null) ? $row[$feed->get('creator')] : '',
 346                  'published'        => ($published > 0) ? $this->feed_helper->format_date($published) : '',
 347                  'updated'        => ($updated > 0) ? $this->feed_helper->format_date($updated) : '',
 348                  'link'            => '',
 349                  'title'            => censor_text($title),
 350                  'category'        => ($this->config['feed_item_statistics'] && !empty($row['forum_id'])) ? $board_url . '/viewforum.' . $this->php_ext . '?f=' . $row['forum_id'] : '',
 351                  'category_name'    => ($this->config['feed_item_statistics'] && isset($row['forum_name'])) ? $row['forum_name'] : '',
 352                  'description'    => censor_text($this->feed_helper->generate_content($row[$feed->get('text')], $row[$feed->get('bbcode_uid')], $row[$feed->get('bitfield')], $options, $row['forum_id'], ($display_attachments ? $feed->get_attachments($row['post_id']) : array()))),
 353                  'statistics'    => '',
 354              );
 355  
 356              // Adjust items, fill link, etc.
 357              $feed->adjust_item($item_row, $row);
 358  
 359              $item_vars[] = $item_row;
 360  
 361              $feed_updated_time = max($feed_updated_time, $published, $updated);
 362          }
 363  
 364          // If we do not have any items at all, sending the current time is better than sending no time.
 365          if (!$feed_updated_time)
 366          {
 367              $feed_updated_time = time();
 368          }
 369  
 370          $feed->close();
 371  
 372          $content = $this->template->render('feed.xml.twig', array(
 373              // Some default assignments
 374              // FEED_IMAGE is not used (atom)
 375              'FEED_IMAGE'            => '',
 376              'SELF_LINK'                => $this->controller_helper->route($this->request->attributes->get('_route'), $this->request->attributes->get('_route_params'), true, '', UrlGeneratorInterface::ABSOLUTE_URL),
 377              'FEED_LINK'                => $board_url . '/index.' . $this->php_ext,
 378              'FEED_TITLE'            => $this->config['sitename'],
 379              'FEED_SUBTITLE'            => $this->config['site_desc'],
 380              'FEED_UPDATED'            => $this->feed_helper->format_date($feed_updated_time),
 381              'FEED_LANG'                => $this->user->lang['USER_LANG'],
 382              'FEED_AUTHOR'            => $this->config['sitename'],
 383  
 384              // Feed entries
 385              'FEED_ROWS'                => $item_vars,
 386          ));
 387  
 388          $response = new Response($content);
 389          $response->headers->set('Content-Type', 'application/atom+xml; charset=UTF-8');
 390          $response->setLastModified(new \DateTime('@' . $feed_updated_time));
 391  
 392          if (!empty($this->user->data['is_bot']))
 393          {
 394              // Let reverse proxies know we detected a bot.
 395              $response->headers->set('X-PHPBB-IS-BOT', 'yes');
 396          }
 397  
 398          return $response;
 399      }
 400  
 401      /**
 402       * Throw and exception saying that the feed isn't available
 403       *
 404       * @throw http_exception
 405       */
 406  	protected function send_unavailable()
 407      {
 408          throw new http_exception(404, 'FEATURE_NOT_AVAILABLE');
 409      }
 410  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1