Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.14% covered (success)
97.14%
34 / 35
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
phpbb_session_testable_facade
97.14% covered (success)
97.14%
34 / 35
80.00% covered (warning)
80.00%
4 / 5
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 extract_current_hostname
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 session_begin
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 session_create
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
2.00
 validate_referer
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
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
14require_once __DIR__ . '/testable_factory.php';
15require_once __DIR__ . '/../../phpBB/phpbb/session.php';
16
17/**
18 * This class exists to expose session.php's functions in a more testable way.
19 *
20 * Since many functions in session.php have global variables inside the function,
21 * this exposes those functions through a testable facade that uses
22 * testable_factory's mock global variables to modify global variables used in
23 * the functions.
24 *
25 * This is using the facade pattern to provide a testable "front" to the
26 * functions in sessions.php.
27 *
28 */
29class phpbb_session_testable_facade
30{
31    protected $db;
32    protected $session_factory;
33
34    function __construct($db, $session_factory)
35    {
36        $this->db = $db;
37        $this->session_factory = $session_factory;
38    }
39
40    function extract_current_hostname(
41        $host,
42        $server_name_config,
43        $cookie_domain_config
44    )
45    {
46        $session = $this->session_factory->get_session($this->db);
47        global $config, $request;
48        $config['server_name'] = $server_name_config;
49        $config['cookie_domain'] = $cookie_domain_config;
50        $request->overwrite('SERVER_NAME', $host, \phpbb\request\request_interface::SERVER);
51        $request->overwrite('Host', $host, \phpbb\request\request_interface::SERVER);
52        // Note: There is a php_uname function used as a fallthrough
53        //       that this function doesn't override
54        return $session->extract_current_hostname();
55    }
56
57    /**
58     *
59     * This function has a lot of dependencies, so instead of naming them all,
60     * just ask for overrides
61     *
62     * @param update_session_page Boolean of whether to set page of the session
63     * @param config_overrides An array of overrides for the global config object
64     * @param request_overrides An array of overrides for the global request object
65     * @return boolean False if the user is identified, otherwise true.
66     */
67    function session_begin(
68        $update_session_page = true,
69        $config_overrides = array(),
70        $request_overrides = array(),
71        $cookies_overrides = array()
72    )
73    {
74        $this->session_factory->merge_config_data($config_overrides);
75        $this->session_factory->merge_server_data($request_overrides);
76        $this->session_factory->set_cookies($cookies_overrides);
77        $session = $this->session_factory->get_session($this->db);
78        $session->session_begin($update_session_page);
79        return $session;
80    }
81
82    function session_create(
83        $user_id = false,
84        $set_admin = false,
85        $persist_login = false,
86        $viewonline = true,
87        array $config_overrides = array(),
88        $user_agent = 'user agent',
89        $ip_address = '127.0.0.1',
90        array $bot_overrides = array(),
91        $uri_sid = ""
92    )
93    {
94        $this->session_factory->merge_config_data($config_overrides);
95        // Bots
96        $this->session_factory->merge_cache_data(array('_bots' => $bot_overrides));
97        global $request, $symfony_request, $phpbb_filesystem, $phpbb_root_path;
98        $session = $this->session_factory->get_session($this->db);
99        $session->browser = $user_agent;
100        $session->ip = $ip_address;
101        $session->page = $session->extract_current_page($phpbb_root_path);
102
103        // Uri sid
104        if ($uri_sid)
105        {
106            $_GET['sid'] = $uri_sid;
107        }
108        $session->session_create($user_id, $set_admin, $persist_login, $viewonline);
109        return $session;
110    }
111
112    function validate_referer(
113        $check_script_path,
114        $referer,
115        $host,
116        $force_server_vars,
117        $server_port,
118        $server_name,
119        $root_script_path
120    )
121    {
122        $session = $this->session_factory->get_session($this->db);
123        global $config, $request;
124        $session->referer = $referer;
125        $session->page['root_script_path'] = $root_script_path;
126        $session->host = $host;
127        $config['force_server_vars'] = $force_server_vars;
128        $config['server_name'] = $server_name;
129        $request->overwrite('SERVER_PORT', $server_port, \phpbb\request\request_interface::SERVER);
130        return $session->validate_referer($check_script_path);
131    }
132}