3 * Bacula(R) - The Network Backup Solution
4 * Baculum - Bacula web interface
6 * Copyright (C) 2013-2017 Kern Sibbald
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.
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.
17 * This notice must be preserved when any source code is
18 * conveyed and/or propagated.
20 * Bacula(R) is a registered trademark of Kern Sibbald.
24 * Store data in session.
30 Prado::using('Application.Common.Class.CommonModule');
31 Prado::using('Application.Common.Class.Interfaces');
33 class SessionRecord extends CommonModule implements SessionItem {
35 private static $lock = false;
36 private static $queue = 0;
38 public function __construct() {
42 public function __destruct() {
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);
58 $emsg = 'Unable to exclusive lock ' . $sessfile;
59 $this->getModule('logging')->log(
62 Logging::CATEGORY_APPLICATION,
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);
82 $emsg = 'Unable to shared lock ' . $sessfile;
83 $this->getModule('logging')->log(
86 Logging::CATEGORY_APPLICATION,
93 $GLOBALS['sess'] = array();
98 public function save() {
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}) {
108 foreach ($vals[$i] as $key => $val) {
109 if (!is_null($this->{$key})) {
111 $vals[$i][$key] = $this->{$key};
121 $vals[] = get_object_vars($this);
124 if ($is_saved || $is_updated) {
127 return ($is_saved || $is_updated);
130 public static function &get() {
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();;
138 return $GLOBALS['sess'][$record_id];
141 public static function findByPk($pk) {
142 $c = get_called_class();
143 $primary_key = $c::getPrimaryKey();
144 $result = self::findBy($primary_key, $pk);
148 public static function findBy($field, $value) {
151 $vals =& self::get();
152 for($i = 0; $i < count($vals); $i++) {
153 if ($vals[$i][$field] === $value) {
161 public static function deleteByPk($pk) {
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);
177 public function forceRefresh() {
178 unset($GLOBALS['sess']);
181 public static function getPrimaryKey() {
184 public static function getRecordId() {
187 public static function getSessionFile() {