]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/protected/Common/Class/SessionRecord.php
baculum: New Baculum API and Baculum Web
[bacula/bacula] / gui / baculum / protected / Common / Class / SessionRecord.php
1 <?php
2 /*
3  * Bacula(R) - The Network Backup Solution
4  * Baculum   - Bacula web interface
5  *
6  * Copyright (C) 2013-2017 Kern Sibbald
7  *
8  * The main author of Baculum is Marcin Haba.
9  * The original author of Bacula is Kern Sibbald, with contributions
10  * from many others, a complete list can be found in the file AUTHORS.
11  *
12  * You may use this file and others of this release according to the
13  * license defined in the LICENSE file, which includes the Affero General
14  * Public License, v3.0 ("AGPLv3") and some additional permissions and
15  * terms pursuant to its AGPLv3 Section 7.
16  *
17  * This notice must be preserved when any source code is
18  * conveyed and/or propagated.
19  *
20  * Bacula(R) is a registered trademark of Kern Sibbald.
21  */
22  
23 /**
24  * Store data in session.
25  * 
26  * @category Data
27  * @package Baculum
28  */
29
30 Prado::using('Application.Common.Class.CommonModule');
31 Prado::using('Application.Common.Class.Interfaces');
32
33 class SessionRecord extends CommonModule implements SessionItem {
34
35         private static $lock = false;
36         private static $queue = 0;
37
38         public function __construct() {
39                 self::restore();
40         }
41
42         public function __destruct() {
43                 self::store();
44         }
45
46         private static function store($wouldblock = true) {
47                 $c = get_called_class();
48                 $sessfile = $c::getSessionFile();
49                 if (array_key_exists('sess', $GLOBALS)) {
50                         $content = serialize($GLOBALS['sess']);
51                         $fp = fopen($sessfile, 'w');
52                         if (flock($fp, LOCK_EX, $wouldblock)) {
53                                 fwrite($fp, $content);
54                                 fflush($fp);
55                                 flock($fp, LOCK_UN);
56                                 self::forceRefresh();
57                         } else {
58                                 $emsg = 'Unable to exclusive lock ' . $sessfile;
59                                 $this->getModule('logging')->log(
60                                         __FUNCTION__,
61                                         $emsg,
62                                         Logging::CATEGORY_APPLICATION,
63                                         __FILE__,
64                                         __LINE__
65                                 );
66                         }
67                         fclose($fp);
68                 }
69         }
70
71         private static function restore($wouldblock = true) {
72                 $c = get_called_class();
73                 $sessfile = $c::getSessionFile();
74                 if (!array_key_exists('sess', $GLOBALS)) {
75                         if (is_readable($sessfile)) {
76                                 $fp = fopen($sessfile, 'r');
77                                 if (flock($fp, LOCK_SH, $wouldblock)) {
78                                         $content = file_get_contents($sessfile);
79                                         $GLOBALS['sess'] = unserialize($content);
80                                         flock($fp, LOCK_UN);
81                                 } else {
82                                         $emsg = 'Unable to shared lock ' . $sessfile;
83                                         $this->getModule('logging')->log(
84                                                 __FUNCTION__,
85                                                 $emsg,
86                                                 Logging::CATEGORY_APPLICATION,
87                                                 __FILE__,
88                                                 __LINE__
89                                         );
90                                 }
91                                 fclose($fp);
92                         } else {
93                                 $GLOBALS['sess'] = array();
94                         }
95                 }
96         }
97
98         public function save() {
99                 $is_saved = false;
100                 $is_updated = false;
101                 $vals =& self::get();
102                 $c = get_called_class();
103                 $primary_key = $c::getPrimaryKey();
104                 for ($i = 0; $i < count($vals); $i++) {
105                         if ($vals[$i][$primary_key] !== $this->{$primary_key}) {
106                                 continue;
107                         }
108                         foreach ($vals[$i] as $key => $val) {
109                                 if (!is_null($this->{$key})) {
110                                         // update record
111                                         $vals[$i][$key] = $this->{$key};
112                                         $is_updated = true;
113                                 }
114                         }
115                         if ($is_updated) {
116                                 break;
117                         }
118                 }
119                 if (!$is_updated) {
120                         // add new record
121                         $vals[] = get_object_vars($this);
122                         $is_saved = true;
123                 }
124                 if ($is_saved || $is_updated) {
125                         self::store();
126                 }
127                 return ($is_saved || $is_updated);
128         }
129
130         public static function &get() {
131                 self::restore();
132                 $result = array();
133                 $c = get_called_class();
134                 $record_id = $c::getRecordId();
135                 if (!array_key_exists($record_id, $GLOBALS['sess'])) {
136                         $GLOBALS['sess'][$record_id] = array();;
137                 }
138                 return $GLOBALS['sess'][$record_id];
139         }
140
141         public static function findByPk($pk) {
142                 $c = get_called_class();
143                 $primary_key = $c::getPrimaryKey();
144                 $result = self::findBy($primary_key, $pk);
145                 return $result;
146         }
147
148         public static function findBy($field, $value) {
149                 self::restore();
150                 $result = null;
151                 $vals =& self::get();
152                 for($i = 0; $i < count($vals); $i++) {
153                         if ($vals[$i][$field] === $value) {
154                                 $result = $vals[$i];
155                                 break;
156                         }
157                 }
158                 return $result;
159         }
160
161         public static function deleteByPk($pk) {
162                 self::restore();
163                 $result = false;
164                 $c = get_called_class();
165                 $vals =& self::get();
166                 $primary_key = $c::getPrimaryKey();
167                 for ($i = 0; $i < count($vals); $i++) {
168                         if ($vals[$i][$primary_key] === $pk) {
169                                 array_splice($vals, $i, 1);
170                                 $result = true;
171                                 break;
172                         }
173                 }
174                 return $result;
175         }
176
177         public function forceRefresh() {
178                 unset($GLOBALS['sess']);
179         }
180
181         public static function getPrimaryKey() {
182         }
183
184         public static function getRecordId() {
185         }
186
187         public static function getSessionFile() {
188         }
189 }