1 | <?php |
||
2 | /** |
||
3 | * ObjectCache.php |
||
4 | * |
||
5 | * -Description- |
||
6 | * |
||
7 | * This program is free software: you can redistribute it and/or modify |
||
8 | * it under the terms of the GNU General Public License as published by |
||
9 | * the Free Software Foundation, either version 3 of the License, or |
||
10 | * (at your option) any later version. |
||
11 | * |
||
12 | * This program is distributed in the hope that it will be useful, |
||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the |
||
15 | * GNU General Public License for more details. |
||
16 | * |
||
17 | * You should have received a copy of the GNU General Public License |
||
18 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
19 | * |
||
20 | * @link https://www.librenms.org |
||
21 | * |
||
22 | * @copyright 2015 Daniel Preussker <[email protected]> |
||
23 | * @copyright 2015 LibreNMS |
||
24 | * @author Daniel Preussker (f0o) <[email protected]> |
||
25 | */ |
||
26 | |||
27 | namespace LibreNMS; |
||
28 | |||
29 | use ArrayAccess; |
||
30 | |||
31 | class ObjectCache implements ArrayAccess |
||
32 | { |
||
33 | private $data = []; |
||
34 | |||
35 | private $obj = ''; |
||
36 | |||
37 | /** |
||
38 | * Initialize ObjectCache |
||
39 | * |
||
40 | * @param string $obj Name of Object |
||
41 | */ |
||
42 | public function __construct($obj) |
||
43 | { |
||
44 | $this->obj = $obj; |
||
45 | if (isset($GLOBALS['_ObjCache'][$obj])) { |
||
46 | $this->data = $GLOBALS['_ObjCacheSkell'][$obj]; |
||
47 | } else { |
||
48 | if (! isset($GLOBALS['_ObjCacheSkell']) || ! is_array($GLOBALS['_ObjCacheSkell'])) { |
||
49 | $GLOBALS['_ObjCacheSkell'] = []; |
||
50 | } |
||
51 | |||
52 | if (! isset($GLOBALS['_ObjCache']) || ! is_array($GLOBALS['_ObjCache'])) { |
||
53 | $GLOBALS['_ObjCache'] = []; |
||
54 | } |
||
55 | |||
56 | if (file_exists(\LibreNMS\Config::get('install_dir') . '/includes/caches/' . $obj . '.inc.php')) { |
||
57 | $data = []; |
||
58 | include \LibreNMS\Config::get('install_dir') . '/includes/caches/' . $obj . '.inc.php'; |
||
59 | $this->data = $data; |
||
60 | $GLOBALS['_ObjCacheSkell'][$obj] = $this->data; |
||
61 | if (! (isset($GLOBALS['_ObjCache'][$obj]) && is_array($GLOBALS['_ObjCache'][$obj]))) { |
||
62 | $GLOBALS['_ObjCache'][$obj] = $this->data; |
||
63 | } |
||
64 | } |
||
65 | }//end if |
||
66 | } |
||
67 | |||
68 | //end __construct() |
||
69 | |||
70 | /** |
||
71 | * Check if data exists |
||
72 | * |
||
73 | * @param string $obj Name of Data-Object |
||
74 | * @return bool |
||
75 | */ |
||
76 | public function offsetExists($obj) |
||
77 | { |
||
78 | if (isset($this->data[$obj])) { |
||
79 | return true; |
||
80 | } |
||
81 | |||
82 | return false; |
||
83 | } |
||
84 | |||
85 | //end offsetExists() |
||
86 | |||
87 | /** |
||
88 | * Get Data-Object |
||
89 | * |
||
90 | * @param string $obj Name of Data-Object |
||
91 | * @return mixed |
||
92 | */ |
||
93 | public function offsetGet($obj) |
||
94 | { |
||
95 | if (isset($this->data[$obj])) { |
||
96 | if (isset($this->data[$obj]['value'])) { |
||
97 | return $this->data[$obj]['value']; |
||
98 | } elseif (isset($GLOBALS['_ObjCache'][$this->obj][$obj]['value'])) { |
||
99 | return $GLOBALS['_ObjCache'][$this->obj][$obj]['value']; |
||
100 | } else { |
||
101 | $GLOBALS['_ObjCache'][$this->obj][$obj]['value'] = dbFetchRows($this->data[$obj]['query'], isset($this->data[$obj]['params']) ? $this->data[$obj]['params'] : []); |
||
102 | if (sizeof($GLOBALS['_ObjCache'][$this->obj][$obj]['value']) == 1 && sizeof($GLOBALS['_ObjCache'][$this->obj][$obj]['value'][0]) == 1) { |
||
103 | $GLOBALS['_ObjCache'][$this->obj][$obj]['value'] = current($GLOBALS['_ObjCache'][$this->obj][$obj]['value'][0]); |
||
104 | } |
||
105 | |||
106 | return $GLOBALS['_ObjCache'][$this->obj][$obj]['value']; |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | |||
111 | //end offsetGet() |
||
112 | |||
113 | /** |
||
114 | * Overrides internal Cache-Object |
||
115 | * |
||
116 | * @param string $obj Name of Data-Object |
||
117 | * @param mixed $value Value |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function offsetSet($obj, $value) |
||
121 | { |
||
122 | if (! isset($this->data[$obj])) { |
||
123 | $this->data[$obj] = []; |
||
124 | } |
||
125 | |||
126 | $this->data[$obj]['value'] = $value; |
||
127 | |||
128 | return $this->data[$obj]['value']; |
||
129 | } |
||
130 | |||
131 | //end offsetSet() |
||
132 | |||
133 | /** |
||
134 | * Reset Data-Object |
||
135 | * |
||
136 | * @param string $obj Name of Data-Object |
||
137 | * @return mixed |
||
138 | */ |
||
139 | public function offsetUnset($obj) |
||
140 | { |
||
141 | unset($this->data[$obj]['value']); |
||
142 | |||
143 | return true; |
||
0 ignored issues
–
show
|
|||
144 | } |
||
145 | |||
146 | //end offsetUnset() |
||
147 | }//end class |
||
148 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: