Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| posting | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| set_lock_name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| acquire | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| 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\lock; |
| 15 | |
| 16 | use phpbb\cache\driver\driver_interface as cache_interface; |
| 17 | use phpbb\config\config; |
| 18 | |
| 19 | class posting |
| 20 | { |
| 21 | /** @var cache_interface */ |
| 22 | private $cache; |
| 23 | |
| 24 | /** @var config */ |
| 25 | private $config; |
| 26 | |
| 27 | /** @var string */ |
| 28 | private $lock_name = ''; |
| 29 | |
| 30 | /** |
| 31 | * Constructor for posting lock |
| 32 | * |
| 33 | * @param cache_interface $cache |
| 34 | * @param config $config |
| 35 | */ |
| 36 | public function __construct(cache_interface $cache, config $config) |
| 37 | { |
| 38 | $this->cache = $cache; |
| 39 | $this->config = $config; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Set lock name |
| 44 | * |
| 45 | * @param int $creation_time Creation time of form, must be checked already |
| 46 | * @param string $form_token Form token used for form, must be checked already |
| 47 | * |
| 48 | * @return void |
| 49 | */ |
| 50 | private function set_lock_name(int $creation_time, string $form_token): void |
| 51 | { |
| 52 | $this->lock_name = sha1(((string) $creation_time) . $form_token) . '_posting_lock'; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Acquire lock for current posting form submission |
| 57 | * |
| 58 | * @param int $creation_time Creation time of form, must be checked already |
| 59 | * @param string $form_token Form token used for form, must be checked already |
| 60 | * |
| 61 | * @return bool True if lock could be acquired, false if not |
| 62 | */ |
| 63 | public function acquire(int $creation_time, string $form_token): bool |
| 64 | { |
| 65 | $this->set_lock_name($creation_time, $form_token); |
| 66 | |
| 67 | // Lock is held for session, cannot acquire it unless special flag for testing is set |
| 68 | if ($this->cache->_exists($this->lock_name) && !$this->config->offsetExists('ci_tests_no_lock_posting')) |
| 69 | { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | $this->cache->put($this->lock_name, true, $this->config['flood_interval']); |
| 74 | |
| 75 | return true; |
| 76 | } |
| 77 | } |