Upgrade to Pro — share decks privately, control downloads, hide ads and more …

phpBB, Meet Symfony (php[world 16)

Michael C.
November 17, 2016

phpBB, Meet Symfony (php[world 16)

Michael C.

November 17, 2016
Tweet

More Decks by Michael C.

Other Decks in Programming

Transcript

  1. ME?

  2. EVENT DISPATCHER <?php class phpbb_event_dispatcher extends EventDispatcher { public function

    trigger_event($eventName, $data = array()) { $event = new phpbb_event_data($data); $this->dispatch($eventName, $event); return $event->get_data_filtered(array_keys($data)); } }
  3. EVENT DISPATCHER <?php class dispatcher extends ContainerAwareEventDispatcher implements dispatcher_interface {

    public function trigger_event($eventName, $data = array()) { $event = new \phpbb\event\data($data); $this->dispatch($eventName, $event); return $event->get_data_filtered(array_keys($data)); } public function dispatch($eventName, Event $event = null) { if ($this->disabled) { return $event; } return parent::dispatch($eventName, $event); } }
  4. DEPENDENCY INJECTION CONTAINER protected function dump_container($container_filename) { $dumper = new

    PhpDumper($this->container); $cached_container_dump = $dumper->dump(array( 'class' => 'phpbb_cache_container', 'base_class' => 'Symfony\\Component\\DependencyInjection\\ContainerBuilder', )); file_put_contents($container_filename, $cached_container_dump); }
  5. DEPENDENCY INJECTION CONTAINER $container_filename = $this->get_container_filename(); if (!defined('DEBUG_CONTAINER') && $this->dump_container

    && file_exists($container_filename)) { require($container_filename); $this->container = new \phpbb_cache_container(); } else { // ...
  6. DEPENDENCY INJECTION CONTAINER protected function create_container(array $extensions) { $container =

    new ContainerBuilder(); foreach ($extensions as $extension) { $container->registerExtension($extension); $container->loadFromExtension($extension->getAlias()); } return $container; }
  7. DEPENDENCY INJECTION CONTAINER /** * Loads a specific configuration. *

    * @param array $config An array of configuration values * @param ContainerBuilder $container A ContainerBuilder instance * * @throws \InvalidArgumentException When provided tag is not defined in this extension */ public function load(array $config, ContainerBuilder $container) { $loader = new YamlFileLoader($container, new FileLocator(phpbb_realpath($this->config_path))); $loader->load('services.yml'); }
  8. COMPILER PASSES if ($this->use_custom_pass) { // Symfony Kernel Listeners $this->container->addCompilerPass(new

    \phpbb\di\pass\collection_pass()); $this->container->addCompilerPass( new RegisterListenersPass('dispatcher', 'event.listener_listener', ‘event.listener')); if ($this->use_kernel_pass) { $this->container->addCompilerPass(new RegisterListenersPass('dispatcher')); } }
  9. COMPILER PASSES public function process(ContainerBuilder $container) { foreach ($container->findTaggedServiceIds('service_collection') as

    $id => $data) { $definition = $container->getDefinition($id); foreach ($container->findTaggedServiceIds($data[0]['tag']) as $service_id => $service_data) { $definition->addMethodCall('add', array($service_id)); } } }
  10. FRONT CONTROLLER <?php define('IN_PHPBB', true); $phpbb_root_path = PHPBB_ROOT_PATH; $phpEx =

    substr(strrchr(__FILE__, '.'), 1); include($phpbb_root_path . 'common.' . $phpEx); // Start session management $user->session_begin(); $auth->acl($user->data); $user->setup('app');
  11. FRONT CONTROLLER $http_kernel = $phpbb_container->get('http_kernel'); $symfony_request = $phpbb_container->get('symfony_request'); $response =

    $http_kernel->handle($symfony_request); $response->send(); $http_kernel->terminate($symfony_request, $response);
  12. HTTP * <?php class symfony_request extends Request { public function

    __construct(\phpbb\request\request_interface $phpbb_request) { $get_parameters = $phpbb_request->get_super_global(\phpbb\request\request_interface::GET); $post_parameters = $phpbb_request->get_super_global(\phpbb\request\request_interface::POST); $server_parameters = $phpbb_request->get_super_global(\phpbb\request\request_interface::SERVER); $files_parameters = $phpbb_request->get_super_global(\phpbb\request\request_interface::FILES); $cookie_parameters = $phpbb_request->get_super_global(\phpbb\request\request_interface::COOKIE); parent::__construct($get_parameters, $post_parameters, array(), $cookie_parameters, $files_parameters, $server_parameters); } }
  13. HTTP * <?php /** * Find a list of controllers

    * * @param string $base_path Base path to prepend to file paths * @return provider */ public function find($base_path = '') { $this->routes = new RouteCollection; foreach ($this->routing_files as $file_path) { $loader = new YamlFileLoader(new FileLocator(phpbb_realpath($base_path))); $this->routes->addCollection($loader->load($file_path)); } return $this; }
  14. CONSOLE <?php if (php_sapi_name() != 'cli') { echo 'This program

    must be run from the command line.' . PHP_EOL; exit(1); } define('IN_PHPBB', true); $phpbb_root_path = __DIR__ . '/../'; $phpEx = substr(strrchr(__FILE__, '.'), 1); require($phpbb_root_path . 'includes/startup.' . $phpEx); require($phpbb_root_path . 'config.' . $phpEx); require($phpbb_root_path . 'includes/constants.' . $phpEx); require($phpbb_root_path . 'includes/functions.' . $phpEx); require($phpbb_root_path . 'includes/functions_container.' . $phpEx); require($phpbb_root_path . 'phpbb/class_loader.' . $phpEx); $phpbb_class_loader = new \phpbb\class_loader('phpbb\\', "{$phpbb_root_path}phpbb/", $phpEx); $phpbb_class_loader->register(); $phpbb_class_loader_ext = new \phpbb\class_loader('\\', "{$phpbb_root_path}ext/", $phpEx); $phpbb_class_loader_ext->register(); $phpbb_container = phpbb_create_update_container($phpbb_root_path, $phpEx, "$phpbb_root_path/config"); $application = new \phpbb\console\application('phpBB Console', PHPBB_VERSION); $application->register_container_commands($phpbb_container); $application->run();
  15. CONSOLE <?php if ($input->hasParameterOption(array('--safe-mode'))) { $phpbb_container_builder->set_use_extensions(false); $phpbb_container_builder->set_dump_container(false); } // ...

    $user = $phpbb_container->get('user'); $user->data['user_id'] = ANONYMOUS; $user->ip = '127.0.0.1'; $user->add_lang('acp/common'); $user->add_lang('cli');
  16. FIXING TOKENS Strip Quotes: <!-- INCLUDE '{TEST}.html' to <!-- INCLUDE

    {TEST}.html Fix tokens: <!-- INCLUDE '{TEST}.html' to <!-- INCLUDE ' ~ {TEST} ~ '.html Add surrounding quotes: <!-- INCLUDE '{TEST}.html' to <!-- INCLUDE '' ~ {TEST} ~ '.html'
  17. Replace ELSE IF with ELSEIF Replace our "div by" with

    Twig's divisibleby Replace $TEST with definition.TEST Replace .foo with loops.foo|length Replace .foo.bar with foo.bar|length
  18. PARSER <?php public function parse(\Twig_Token $token) { $expr = $this->parser->getExpressionParser()->parseExpression();

    $stream = $this->parser->getStream(); $stream->expect(\Twig_Token::BLOCK_END_TYPE); return new \phpbb\template\twig\node\event( $expr, $this->parser->getEnvironment(), $token->getLine(), $this->getTag() ); }
  19. 24

  20. “But it was my first experience with phpBB as a

    forum software, and so too easily I formed the opinion that phpBB was easy to inject exploits into. So then subsequently whenever I needed forum software, I shyed away from it. I didn't want my reputation and my clients' sites to be easily compromised.”
  21. PHP

  22. “Tonight's talk made me realise that having that preformed opinion

    and idea about how phpBB performed in the wild, from 12 years ago, was indicative of how closed my mind could be to certain topics. I'd liken it to the same way I felt about Ruby, based on “hipsters” and the plethora of “ninja rockstar guru” developer positions”
  23. “Tonight I realised I still held the "insecure and outdated"

    opinion of phpBB, despite not having touched it since back then - and that saddened me, that I hadn't taken time to be more aware of where it was at.”
  24. “After my Ruby experience a couple of years ago, I

    resolved to be more open-minded about topics on which I had little experience. Michael's talk this evening made me resolve to do that more.”