Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.42% covered (warning)
87.42%
139 / 159
69.57% covered (warning)
69.57%
16 / 23
CRAP
0.00% covered (danger)
0.00%
0 / 1
manager
87.42% covered (warning)
87.42%
139 / 159
69.57% covered (warning)
69.57%
16 / 23
79.75
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 load_extensions
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
 get_extension_path
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 get_extension
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 create_extension_metadata_manager
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 update_state
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
5
 enable_step
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
6.01
 enable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 disable_step
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
2.00
 disable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 purge_step
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
4.06
 purge
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 all_available
94.12% covered (success)
94.12%
16 / 17
0.00% covered (danger)
0.00%
0 / 1
6.01
 all_configured
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 all_enabled
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 all_disabled
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 is_available
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 is_enabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_disabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 is_configured
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 version_check
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 is_purged
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_finder
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
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
14namespace phpbb\extension;
15
16use phpbb\exception\runtime_exception;
17use phpbb\file_downloader;
18use phpbb\finder\factory as finder_factory;
19use Symfony\Component\DependencyInjection\ContainerInterface;
20
21/**
22* The extension manager provides means to activate/deactivate extensions.
23*/
24class manager
25{
26    /** @var ContainerInterface */
27    protected $container;
28
29    protected $db;
30    protected $config;
31    protected $finder_factory;
32    protected $cache;
33    protected $extensions;
34    protected $extension_table;
35    protected $phpbb_root_path;
36    protected $cache_name;
37
38    /**
39    * Creates a manager and loads information from database
40    *
41    * @param ContainerInterface $container A container
42    * @param \phpbb\db\driver\driver_interface $db A database connection
43    * @param \phpbb\config\config $config Config object
44    * @param finder_factory $finder_factory Finder factory
45    * @param string $extension_table The name of the table holding extensions
46    * @param string $phpbb_root_path Path to the phpbb includes directory.
47    * @param \phpbb\cache\service|null $cache A cache instance or null
48    * @param string $cache_name The name of the cache variable, defaults to _ext
49    */
50    public function __construct(ContainerInterface $container, \phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, finder_factory $finder_factory, $extension_table, $phpbb_root_path, \phpbb\cache\service|null $cache = null, $cache_name = '_ext')
51    {
52        $this->cache = $cache;
53        $this->cache_name = $cache_name;
54        $this->config = $config;
55        $this->finder_factory = $finder_factory;
56        $this->container = $container;
57        $this->db = $db;
58        $this->extension_table = $extension_table;
59        $this->phpbb_root_path = $phpbb_root_path;
60
61        $this->extensions = ($this->cache) ? $this->cache->get($this->cache_name) : false;
62
63        if ($this->extensions === false)
64        {
65            $this->load_extensions();
66        }
67    }
68
69    /**
70    * Loads all extension information from the database
71    *
72    * @return void
73    */
74    public function load_extensions()
75    {
76        $this->extensions = array();
77
78        // Do not try to load any extensions if the extension table
79        // does not exist or when installing or updating.
80        // Note: database updater invokes this code, and in 3.0
81        // there is no extension table therefore the rest of this function
82        // fails
83        if (defined('IN_INSTALL') || version_compare($this->config['version'], '3.1.0-dev', '<'))
84        {
85            return;
86        }
87
88        $sql = 'SELECT *
89            FROM ' . $this->extension_table;
90
91        $result = $this->db->sql_query($sql);
92        $extensions = $this->db->sql_fetchrowset($result);
93        $this->db->sql_freeresult($result);
94
95        foreach ($extensions as $extension)
96        {
97            $extension['ext_path'] = $this->get_extension_path($extension['ext_name']);
98            $this->extensions[$extension['ext_name']] = $extension;
99        }
100
101        ksort($this->extensions);
102
103        if ($this->cache)
104        {
105            $this->cache->put($this->cache_name, $this->extensions);
106        }
107    }
108
109    /**
110    * Generates the path to an extension
111    *
112    * @param string $name The name of the extension
113    * @param bool $phpbb_relative Whether the path should be relative to phpbb root
114    * @return string Path to an extension
115    */
116    public function get_extension_path($name, $phpbb_relative = false)
117    {
118        $name = str_replace('.', '', $name);
119
120        return (($phpbb_relative) ? $this->phpbb_root_path : '') . 'ext/' . $name . '/';
121    }
122
123    /**
124    * Instantiates the extension meta class for the extension with the given name
125    *
126    * @param string $name The extension name
127    * @return \phpbb\extension\extension_interface Instance of the extension meta class or
128    *                     \phpbb\extension\base if the class does not exist
129    */
130    public function get_extension($name)
131    {
132        $extension_class_name = str_replace('/', '\\', $name) . '\\ext';
133
134        $migrator = $this->container->get('migrator');
135
136        if (class_exists($extension_class_name))
137        {
138            return new $extension_class_name($this->container, $this->get_finder(), $migrator, $name, $this->get_extension_path($name, true));
139        }
140        else
141        {
142            return new \phpbb\extension\base($this->container, $this->get_finder(), $migrator, $name, $this->get_extension_path($name, true));
143        }
144    }
145
146    /**
147    * Instantiates the metadata manager for the extension with the given name
148    *
149    * @param string $name The extension name
150    * @return \phpbb\extension\metadata_manager Instance of the metadata manager
151    */
152    public function create_extension_metadata_manager($name)
153    {
154        if (!isset($this->extensions[$name]['metadata']))
155        {
156            $metadata = new \phpbb\extension\metadata_manager($name, $this->get_extension_path($name, true));
157            $this->extensions[$name]['metadata'] = $metadata;
158        }
159        return $this->extensions[$name]['metadata'];
160    }
161
162    /**
163    * Update the database entry for an extension
164    *
165    * @param string $name Extension name to update
166    * @param array    $data Data to update in the database
167    * @param string    $action Action to perform, by default 'update', may be also 'insert' or 'delete'
168    */
169    protected function update_state($name, $data, $action = 'update')
170    {
171        switch ($action)
172        {
173            case 'insert':
174                $this->extensions[$name] = $data;
175                $this->extensions[$name]['ext_path'] = $this->get_extension_path($name);
176                ksort($this->extensions);
177                $sql = 'INSERT INTO ' . $this->extension_table . ' ' . $this->db->sql_build_array('INSERT', $data);
178                $this->db->sql_query($sql);
179            break;
180
181            case 'update':
182                $this->extensions[$name] = array_merge($this->extensions[$name], $data);
183                $sql = 'UPDATE ' . $this->extension_table . '
184                    SET ' . $this->db->sql_build_array('UPDATE', $data) . "
185                    WHERE ext_name = '" . $this->db->sql_escape($name) . "'";
186                $this->db->sql_query($sql);
187            break;
188
189            case 'delete':
190                unset($this->extensions[$name]);
191                $sql = 'DELETE FROM ' . $this->extension_table . "
192                    WHERE ext_name = '" . $this->db->sql_escape($name) . "'";
193                $this->db->sql_query($sql);
194            break;
195        }
196
197        if ($this->cache)
198        {
199            $this->cache->deferred_purge();
200        }
201    }
202
203    /**
204    * Runs a step of the extension enabling process.
205    *
206    * Allows the exentension to enable in a long running script that works
207    * in multiple steps across requests. State is kept for the extension
208    * in the extensions table.
209    *
210    * @param    string    $name    The extension's name
211    * @return    bool            False if enabling is finished, true otherwise
212    */
213    public function enable_step($name)
214    {
215        // ignore extensions that are already enabled
216        if ($this->is_enabled($name))
217        {
218            return false;
219        }
220
221        $old_state = (isset($this->extensions[$name]['ext_state'])) ? unserialize($this->extensions[$name]['ext_state']) : false;
222
223        $extension = $this->get_extension($name);
224
225        if (!$extension->is_enableable())
226        {
227            return false;
228        }
229
230        $state = $extension->enable_step($old_state);
231
232        $active = ($state === false);
233
234        $extension_data = array(
235            'ext_name'        => $name,
236            'ext_active'    => $active,
237            'ext_state'        => serialize($state),
238        );
239
240        $this->update_state($name, $extension_data, $this->is_configured($name) ? 'update' : 'insert');
241
242        if ($active)
243        {
244            $this->config->increment('assets_version', 1);
245        }
246
247        return !$active;
248    }
249
250    /**
251    * Enables an extension
252    *
253    * This method completely enables an extension. But it could be long running
254    * so never call this in a script that has a max_execution time.
255    *
256    * @param string $name The extension's name
257    * @return void
258    */
259    public function enable($name)
260    {
261        while ($this->enable_step($name));
262    }
263
264    /**
265    * Disables an extension
266    *
267    * Calls the disable method on the extension's meta class to allow it to
268    * process the event.
269    *
270    * @param string $name The extension's name
271    * @return bool False if disabling is finished, true otherwise
272    */
273    public function disable_step($name)
274    {
275        // ignore extensions that are not enabled
276        if (!$this->is_enabled($name))
277        {
278            return false;
279        }
280
281        $old_state = unserialize($this->extensions[$name]['ext_state']);
282
283        $extension = $this->get_extension($name);
284        $state = $extension->disable_step($old_state);
285        $active = ($state !== false);
286
287        $extension_data = array(
288            'ext_active'    => $active,
289            'ext_state'        => serialize($state),
290        );
291        $this->update_state($name, $extension_data);
292
293        return $active;
294    }
295
296    /**
297    * Disables an extension
298    *
299    * Disables an extension completely at once. This process could run for a
300    * while so never call this in a script that has a max_execution time.
301    *
302    * @param string $name The extension's name
303    * @return void
304    */
305    public function disable($name)
306    {
307        while ($this->disable_step($name));
308    }
309
310    /**
311    * Purge an extension
312    *
313    * Disables the extension first if active, and then calls purge on the
314    * extension's meta class to delete the extension's database content.
315    *
316    * @param string $name The extension's name
317    * @return bool False if purging is finished, true otherwise
318    */
319    public function purge_step($name)
320    {
321        // ignore extensions that are not configured
322        if (!$this->is_configured($name))
323        {
324            return false;
325        }
326
327        // disable first if necessary
328        if ($this->extensions[$name]['ext_active'])
329        {
330            $this->disable($name);
331        }
332
333        $old_state = unserialize($this->extensions[$name]['ext_state']);
334
335        $extension = $this->get_extension($name);
336        $state = $extension->purge_step($old_state);
337        $purged = ($state === false);
338
339        $extension_data = array(
340            'ext_state'    => serialize($state),
341        );
342
343        $this->update_state($name, $extension_data, $purged ? 'delete' : 'update');
344
345        // continue until the state is false
346        return !$purged;
347    }
348
349    /**
350    * Purge an extension
351    *
352    * Purges an extension completely at once. This process could run for a while
353    * so never call this in a script that has a max_execution time.
354    *
355    * @param string $name The extension's name
356    * @return void
357    */
358    public function purge($name)
359    {
360        while ($this->purge_step($name));
361    }
362
363    /**
364    * Retrieves a list of all available extensions on the filesystem
365    *
366    * @return array An array with extension names as keys and paths to the
367    *               extension as values
368    */
369    public function all_available()
370    {
371        $available = array();
372        if (!is_dir($this->phpbb_root_path . 'ext/'))
373        {
374            return $available;
375        }
376
377        $iterator = new \phpbb\finder\recursive_path_iterator(
378            $this->phpbb_root_path . 'ext/',
379            \RecursiveIteratorIterator::SELF_FIRST,
380            \FilesystemIterator::NEW_CURRENT_AND_KEY | \FilesystemIterator::FOLLOW_SYMLINKS
381        );
382        $iterator->setMaxDepth(2);
383
384        foreach ($iterator as $file_info)
385        {
386            if ($file_info->isFile() && $file_info->getFilename() == 'composer.json')
387            {
388                $ext_name = $iterator->getInnerIterator()->getSubPath();
389                $ext_name = str_replace(DIRECTORY_SEPARATOR, '/', $ext_name);
390                if ($this->is_available($ext_name))
391                {
392                    $available[$ext_name] = $this->get_extension_path($ext_name, true);
393                }
394            }
395        }
396        ksort($available);
397        return $available;
398    }
399
400    /**
401    * Retrieves all configured extensions.
402    *
403    * All enabled and disabled extensions are considered configured. A purged
404    * extension that is no longer in the database is not configured.
405    *
406    * @param bool $phpbb_relative Whether the path should be relative to phpbb root
407    *
408    * @return array An array with extension names as keys and and the
409    *               database stored extension information as values
410    */
411    public function all_configured($phpbb_relative = true)
412    {
413        $configured = array();
414        foreach ($this->extensions as $name => $data)
415        {
416            if ($this->is_configured($name))
417            {
418                unset($data['metadata']);
419                $data['ext_path'] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path'];
420                $configured[$name] = $data;
421            }
422        }
423        return $configured;
424    }
425
426    /**
427    * Retrieves all enabled extensions.
428    * @param bool $phpbb_relative Whether the path should be relative to phpbb root
429    *
430    * @return array An array with extension names as keys and and the
431    *               database stored extension information as values
432    */
433    public function all_enabled($phpbb_relative = true)
434    {
435        $enabled = array();
436        foreach ($this->extensions as $name => $data)
437        {
438            if ($this->is_enabled($name))
439            {
440                $enabled[$name] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path'];
441            }
442        }
443        return $enabled;
444    }
445
446    /**
447    * Retrieves all disabled extensions.
448    *
449    * @param bool $phpbb_relative Whether the path should be relative to phpbb root
450    *
451    * @return array An array with extension names as keys and and the
452    *               database stored extension information as values
453    */
454    public function all_disabled($phpbb_relative = true)
455    {
456        $disabled = array();
457        foreach ($this->extensions as $name => $data)
458        {
459            if ($this->is_disabled($name))
460            {
461                $disabled[$name] = ($phpbb_relative ? $this->phpbb_root_path : '') . $data['ext_path'];
462            }
463        }
464        return $disabled;
465    }
466
467    /**
468    * Check to see if a given extension is available on the filesystem
469    *
470    * @param string $name Extension name to check NOTE: Can be user input
471    * @return bool Depending on whether or not the extension is available
472    */
473    public function is_available($name)
474    {
475        $md_manager = $this->create_extension_metadata_manager($name);
476        try
477        {
478            return $md_manager->get_metadata('all') && $md_manager->validate_enable();
479        }
480        catch (\phpbb\extension\exception $e)
481        {
482            return false;
483        }
484    }
485
486    /**
487    * Check to see if a given extension is enabled
488    *
489    * @param string $name Extension name to check
490    * @return bool Depending on whether or not the extension is enabled
491    */
492    public function is_enabled($name)
493    {
494        return isset($this->extensions[$name]['ext_active']) && $this->extensions[$name]['ext_active'];
495    }
496
497    /**
498    * Check to see if a given extension is disabled
499    *
500    * @param string $name Extension name to check
501    * @return bool Depending on whether or not the extension is disabled
502    */
503    public function is_disabled($name)
504    {
505        return isset($this->extensions[$name]['ext_active']) && !$this->extensions[$name]['ext_active'];
506    }
507
508    /**
509    * Check to see if a given extension is configured
510    *
511    * All enabled and disabled extensions are considered configured. A purged
512    * extension that is no longer in the database is not configured.
513    *
514    * @param string $name Extension name to check
515    * @return bool Depending on whether or not the extension is configured
516    */
517    public function is_configured($name)
518    {
519        return isset($this->extensions[$name]['ext_active']);
520    }
521
522    /**
523    * Check the version and return the available updates (for an extension).
524    *
525    * @param \phpbb\extension\metadata_manager $md_manager The metadata manager for the version to check.
526    * @param bool $force_update Ignores cached data. Defaults to false.
527    * @param bool $force_cache Force the use of the cache. Override $force_update.
528    * @param string $stability Force the stability (null by default).
529    * @return array
530    * @throws runtime_exception
531    */
532    public function version_check(\phpbb\extension\metadata_manager $md_manager, $force_update = false, $force_cache = false, $stability = null)
533    {
534        $meta = $md_manager->get_metadata('all');
535
536        if (!isset($meta['extra']['version-check']))
537        {
538            throw new runtime_exception('NO_VERSIONCHECK');
539        }
540
541        $version_check = $meta['extra']['version-check'];
542
543        $version_helper = new \phpbb\version_helper($this->cache, $this->config, new file_downloader());
544        $version_helper->set_current_version($meta['version']);
545        $version_helper->set_file_location($version_check['host'], $version_check['directory'], $version_check['filename'], isset($version_check['ssl']) ? $version_check['ssl'] : false);
546        $version_helper->force_stability($stability);
547
548        return $version_helper->get_ext_update_on_branch($force_update, $force_cache);
549    }
550
551    /**
552    * Check to see if a given extension is purged
553    *
554    * An extension is purged if it is available, not enabled and not disabled.
555    *
556    * @param string $name Extension name to check
557    * @return bool Depending on whether or not the extension is purged
558    */
559    public function is_purged($name)
560    {
561        return $this->is_available($name) && !$this->is_configured($name);
562    }
563
564    /**
565    * Instantiates a \phpbb\finder\finder.
566    *
567    * @param bool $use_all_available Should we load all extensions, or just enabled ones
568    * @return \phpbb\finder\finder An extension finder instance
569    */
570    public function get_finder($use_all_available = false)
571    {
572        $finder = $this->finder_factory->get($this->cache_name . '_finder');
573
574        if ($use_all_available)
575        {
576            $finder->set_extensions(array_keys($this->all_available()));
577        }
578        else
579        {
580            $finder->set_extensions(array_keys($this->all_enabled()));
581        }
582
583        return $finder;
584    }
585}