| 1: | <?php | 
| 2: | /* | 
| 3: | You may not change or alter any portion of this comment or credits | 
| 4: | of supporting developers from this source code or any supporting source code | 
| 5: | which is considered copyrighted (c) material of the original comment or credit authors. | 
| 6: | |
| 7: | This program is distributed in the hope that it will be useful, | 
| 8: | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 9: | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | 
| 10: | */ | 
| 11: | |
| 12: | namespace Xmf\Key; | 
| 13: | |
| 14: | /** | 
| 15: | * Xmf\Key\StorageInterface | 
| 16: | * | 
| 17: | * load a database table | 
| 18: | * | 
| 19: | * @category Xmf\Key\FileStorage | 
| 20: | * @package Xmf | 
| 21: | * @author Richard Griffith <richard@geekwright.com> | 
| 22: | * @copyright 2016-2018 XOOPS Project (https://xoops.org) | 
| 23: | * @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html) | 
| 24: | * @link https://xoops.org | 
| 25: | */ | 
| 26: | class FileStorage implements StorageInterface | 
| 27: | { | 
| 28: | |
| 29: | /** | 
| 30: | * @var string $storagePath filesystem path to storage | 
| 31: | */ | 
| 32: | protected $storagePath; | 
| 33: | |
| 34: | /** | 
| 35: | * @var string $systemSecret prefix unique to this system | 
| 36: | */ | 
| 37: | protected $systemSecret; | 
| 38: | |
| 39: | /** | 
| 40: | * FileStorage constructor. | 
| 41: | * | 
| 42: | * @param string|null $storagePath filesystem path to storage (without trailing slash) | 
| 43: | * @param string|null $systemSecret prefix unique to this system | 
| 44: | */ | 
| 45: | public function __construct($storagePath = null, $systemSecret = null) | 
| 46: | { | 
| 47: | $this->storagePath = (null === $storagePath) ? XOOPS_VAR_PATH . '/data' : $storagePath; | 
| 48: | $this->systemSecret = (null === $systemSecret) ? $this->generateSystemSecret() : $systemSecret; | 
| 49: | } | 
| 50: | |
| 51: | /** | 
| 52: | * Fetch key data by name | 
| 53: | * | 
| 54: | * @param string $name key name | 
| 55: | * | 
| 56: | * @return string file name | 
| 57: | */ | 
| 58: | protected function fileName($name) | 
| 59: | { | 
| 60: | return $this->storagePath . "/{$this->systemSecret}-key-{$name}.php"; | 
| 61: | } | 
| 62: | |
| 63: | /** | 
| 64: | * Construct a string related to the system to make name less predictable | 
| 65: | * | 
| 66: | * @return string | 
| 67: | */ | 
| 68: | protected function generateSystemSecret() | 
| 69: | { | 
| 70: | $db = \XoopsDatabaseFactory::getDatabaseConnection(); | 
| 71: | $prefix = $db->prefix(); | 
| 72: | $secret = md5($prefix); | 
| 73: | $secret = substr($secret, 8, 8); | 
| 74: | return $secret; | 
| 75: | } | 
| 76: | |
| 77: | /** | 
| 78: | * Save key data by name | 
| 79: | * | 
| 80: | * @param string $name key name | 
| 81: | * @param string $data key data, serialized to string if required | 
| 82: | * | 
| 83: | * @return boolean true if key saved, otherwise false | 
| 84: | */ | 
| 85: | public function save($name, $data) | 
| 86: | { | 
| 87: | if (empty($data) || !is_string($data)) { | 
| 88: | throw new \DomainException('Invalid key data'); | 
| 89: | } | 
| 90: | $fileContents = "<?php\n//**Warning** modifying this file will break things!\n" | 
| 91: | . "return '{$data}';\n"; | 
| 92: | return (false !== file_put_contents($this->fileName($name), $fileContents)); | 
| 93: | } | 
| 94: | |
| 95: | /** | 
| 96: | * Fetch key data by name | 
| 97: | * | 
| 98: | * @param string $name key name | 
| 99: | * | 
| 100: | * @return string|false key data (possibly serialized) or false on error | 
| 101: | */ | 
| 102: | public function fetch($name) | 
| 103: | { | 
| 104: | return include $this->fileName($name); | 
| 105: | } | 
| 106: | |
| 107: | /** | 
| 108: | * Check if key data exists | 
| 109: | * | 
| 110: | * @param string $name key name | 
| 111: | * | 
| 112: | * @return boolean true if key exists, otherwise false | 
| 113: | */ | 
| 114: | public function exists($name) | 
| 115: | { | 
| 116: | return file_exists($this->fileName($name)); | 
| 117: | } | 
| 118: | |
| 119: | /** | 
| 120: | * Delete a key | 
| 121: | * | 
| 122: | * @param string $name key name | 
| 123: | * | 
| 124: | * @return boolean true if key deleted, otherwise false | 
| 125: | */ | 
| 126: | public function delete($name) | 
| 127: | { | 
| 128: | return unlink($this->fileName($name)); | 
| 129: | } | 
| 130: | } | 
| 131: |