1 |
joko |
1.1 |
<? |
2 |
|
|
## ------------------------------------------------------------------------------------- |
3 |
joko |
1.4 |
## $Id: ProxyObject.php,v 1.3 2002/12/06 04:12:54 joko Exp $ |
4 |
joko |
1.1 |
## ------------------------------------------------------------------------------------- |
5 |
joko |
1.2 |
## $Log: ProxyObject.php,v $ |
6 |
joko |
1.4 |
## Revision 1.3 2002/12/06 04:12:54 joko |
7 |
|
|
## + replaced 'xyzCache' through 'xyzProxy' |
8 |
|
|
## + function store(...) |
9 |
|
|
## |
10 |
joko |
1.3 |
## Revision 1.2 2002/12/05 21:44:09 joko |
11 |
|
|
## + debugging |
12 |
|
|
## |
13 |
joko |
1.2 |
## Revision 1.1 2002/12/01 17:23:58 joko |
14 |
|
|
## + initial check-in |
15 |
|
|
## |
16 |
joko |
1.1 |
## Revision 1.11 2002/11/12 06:05:58 cvsjoko |
17 |
|
|
## + renamed class: Text_Encode -> TextEncode |
18 |
|
|
## |
19 |
|
|
## Revision 1.10 2002/10/29 19:15:33 cvsjoko |
20 |
|
|
## - moved utf8/iso-conversion to lib/utils/Text_Encode.php |
21 |
|
|
## |
22 |
|
|
## Revision 1.9 2002/10/26 12:32:36 cvsjoko |
23 |
|
|
## - removed debugging via "print" |
24 |
|
|
## + added generic logging via PEAR::Log |
25 |
|
|
## |
26 |
|
|
## Revision 1.8 2002/10/22 09:51:38 cvsmax |
27 |
|
|
## + moved semi-method 'createBackend' to method $site->user->create at User.class.php |
28 |
|
|
## |
29 |
|
|
## Revision 1.7 2002/10/21 18:27:09 cvsjoko |
30 |
|
|
## - manually disabled any form of caching |
31 |
|
|
## |
32 |
|
|
## Revision 1.6 2002/10/17 03:48:47 cvsmax |
33 |
|
|
## + new |
34 |
|
|
## + function _createBackend create new Backend |
35 |
|
|
## + function _create api-create |
36 |
|
|
## |
37 |
|
|
## Revision 1.5 2002/10/16 03:37:54 cvsjoko |
38 |
|
|
## + bugfix: wrong comparison in restriction to save to array |
39 |
|
|
## |
40 |
|
|
## Revision 1.4 2002/10/10 03:04:23 cvsjoko |
41 |
|
|
## + no debug-output |
42 |
|
|
## |
43 |
|
|
## Revision 1.3 2002/10/10 03:03:27 cvsjoko |
44 |
|
|
## + bugfix: save object to cache only if payload from backend isn't empty |
45 |
|
|
## |
46 |
|
|
## Revision 1.2 2002/10/10 02:40:06 cvsjoko |
47 |
|
|
## + new level of data-caching (session and persistant) |
48 |
|
|
## + function _loadState loads data from session |
49 |
|
|
## + function _saveState saves data to session |
50 |
|
|
## + function save api-save (session & backend) |
51 |
|
|
## + function _commit saves data to backend |
52 |
|
|
## + handy utils |
53 |
|
|
## + function _setAttributes |
54 |
|
|
## + function flushState |
55 |
|
|
## |
56 |
|
|
## Revision 1.1 2002/10/09 00:51:39 cvsjoko |
57 |
|
|
## + new |
58 |
|
|
## ------------------------------------------------------------------------------------- |
59 |
|
|
|
60 |
|
|
class ProxyObject { |
61 |
|
|
|
62 |
|
|
var $objectId; |
63 |
|
|
// purpose ... |
64 |
|
|
var $meta; |
65 |
|
|
var $payload; |
66 |
|
|
var $attributes; |
67 |
|
|
var $backend; |
68 |
|
|
|
69 |
|
|
function ProxyObject($objectId = "") { |
70 |
|
|
session_register_safe("proxy"); |
71 |
|
|
$this->backend = new Remote; |
72 |
|
|
$this->_init($objectId); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
function _init($objectId="") { |
76 |
|
|
if ($objectId) { |
77 |
|
|
$this->objectId = $objectId; |
78 |
|
|
$this->load(); |
79 |
joko |
1.3 |
//$this->_saveProxy(); |
80 |
joko |
1.1 |
} |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
function load() { |
84 |
|
|
if (!$this->_loadState()) { |
85 |
joko |
1.3 |
if (!$this->_loadProxy()) { |
86 |
joko |
1.1 |
$this->_loadBackend(); |
87 |
|
|
} |
88 |
|
|
} |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
function save($data, $type) { |
92 |
|
|
$this->_setAttributes($data); |
93 |
|
|
$this->_saveState(); |
94 |
|
|
if ($type == 'commit') { |
95 |
|
|
$this->_commit(); |
96 |
|
|
} |
97 |
|
|
if ($type == 'create') { |
98 |
|
|
$this->_create(); |
99 |
|
|
} |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
function flush() { |
103 |
|
|
$this->flushState(); |
104 |
joko |
1.3 |
$this->flushProxy(); |
105 |
joko |
1.1 |
} |
106 |
|
|
|
107 |
|
|
function _commit() { |
108 |
|
|
$this->_saveBackend($this->attributes); |
109 |
|
|
$this->flushState(); |
110 |
joko |
1.3 |
$this->flushProxy(); |
111 |
joko |
1.1 |
} |
112 |
|
|
|
113 |
|
|
|
114 |
|
|
// TODO: make this work |
115 |
|
|
/* |
116 |
|
|
function _create() { |
117 |
|
|
$this->_createBackend($this->attributes); |
118 |
|
|
$this->flushState(); |
119 |
joko |
1.3 |
$this->flushProxy(); |
120 |
joko |
1.1 |
} |
121 |
|
|
*/ |
122 |
|
|
|
123 |
|
|
function getAttributes() { |
124 |
|
|
if (!$this->meta[decoded]) { |
125 |
|
|
$this->_decode(); |
126 |
|
|
$this->_saveState(); |
127 |
|
|
} |
128 |
|
|
return $this->attributes; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
function _setAttributes($data) { |
132 |
|
|
$this->attributes = $data; |
133 |
|
|
} |
134 |
|
|
|
135 |
joko |
1.3 |
function flushProxy() { |
136 |
joko |
1.1 |
connectdb(); |
137 |
|
|
$sql = "DELETE FROM f_proxy WHERE oid='$this->objectId'"; |
138 |
|
|
send_sql($sql); |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
function flushState() { |
142 |
|
|
global $proxy; |
143 |
|
|
unset($proxy[$this->objectId]); |
144 |
joko |
1.4 |
$this->meta[decoded] = 0; |
145 |
joko |
1.1 |
} |
146 |
|
|
|
147 |
|
|
function _loadState() { |
148 |
|
|
global $proxy; |
149 |
|
|
logp(get_class($this) . "->_loadState()", LOG_DEBUG); |
150 |
|
|
if ($this->attributes = $proxy[$this->objectId]) { |
151 |
|
|
//print "_loadState:" . dumpVar($this->attributes); |
152 |
|
|
$this->meta[decoded] = 1; |
153 |
|
|
// TODO: make a parameter from this (0 deactivates session-layer) |
154 |
|
|
return 0; |
155 |
|
|
} |
156 |
|
|
} |
157 |
|
|
|
158 |
|
|
function _saveState() { |
159 |
|
|
global $proxy; |
160 |
|
|
logp(get_class($this) . "->_saveState()", LOG_DEBUG); |
161 |
|
|
$proxy[$this->objectId] = $this->attributes; |
162 |
|
|
//print "_saveState: " . dumpVar($this->attributes); |
163 |
|
|
// TODO: throw exception-message back to user if operation fails |
164 |
|
|
} |
165 |
|
|
|
166 |
joko |
1.3 |
function _loadProxy() { |
167 |
|
|
logp(get_class($this) . "->_loadProxy()", LOG_DEBUG); |
168 |
joko |
1.1 |
connectdb(); |
169 |
|
|
$sql = "SELECT payload FROM f_proxy WHERE oid='$this->objectId'"; |
170 |
|
|
if ($res = send_sql($sql)) { |
171 |
|
|
$row = mysql_fetch_array($res, MYSQL_ASSOC); |
172 |
|
|
if ($row) { |
173 |
|
|
$this->payload = $row[payload]; |
174 |
|
|
// TODO: make a parameter from this (0 deactivates mysqldb-layer) |
175 |
|
|
return 0; |
176 |
|
|
} |
177 |
|
|
} |
178 |
|
|
} |
179 |
|
|
|
180 |
joko |
1.3 |
// TODO: use PEAR here |
181 |
|
|
function _saveProxy() { |
182 |
|
|
logp(get_class($this) . "->_saveProxy()", LOG_DEBUG); |
183 |
joko |
1.1 |
connectdb(); |
184 |
|
|
if ($this->payload) { |
185 |
|
|
//$sql = "INSERT INTO f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
186 |
|
|
$sql = "INSERT INTO f_proxy SET oid='$this->objectId', payload='$this->payload'"; |
187 |
|
|
if (!send_sql($sql)) { |
188 |
|
|
$sql = "UPDATE f_proxy SET payload='$this->payload' WHERE oid='$this->objectId'"; |
189 |
|
|
send_sql($sql); |
190 |
|
|
} |
191 |
|
|
} |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
function _loadBackend() { |
195 |
|
|
logp(get_class($this) . "->_loadBackend()", LOG_DEBUG); |
196 |
|
|
// TODO: test backend for reachability first (eventually cache this information and "reset" it by another party) |
197 |
|
|
if ($result = $this->backend->call('getObject', $this->objectId)) { |
198 |
joko |
1.2 |
//print "result: " . dumpVar($result) . "<br>"; |
199 |
joko |
1.1 |
if (count($result) == 0) { return; } |
200 |
|
|
$encoder = new TextEncode($result); |
201 |
|
|
$encoder->toISO(); |
202 |
|
|
if ($_GET[debug]) { |
203 |
|
|
print dumpVar($result); |
204 |
|
|
} |
205 |
|
|
$this->payload = serialize($result); |
206 |
joko |
1.3 |
$this->_saveProxy(); |
207 |
joko |
1.4 |
//print "oid: $this->objectId<br>"; |
208 |
|
|
$this->flushState(); |
209 |
joko |
1.1 |
} |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
function _saveBackend($result) { |
213 |
|
|
logp(get_class($this) . "->_saveBackend()", LOG_DEBUG); |
214 |
|
|
$encoder = new TextEncode($result); |
215 |
|
|
$encoder->toUTF8(); |
216 |
|
|
$response = $this->backend->send('saveObject', array('oid' => $this->objectId, 'data' => $result) ); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
function _decode() { |
220 |
|
|
// fill attributes-hashtable from message-hashtable |
221 |
|
|
if (!$this->payload) { return; } |
222 |
|
|
//if ($this->attributes = $backend->decodeData($this->payload)) { |
223 |
|
|
if ($this->attributes = unserialize($this->payload)) { |
224 |
|
|
$this->meta[decoded] = 1; |
225 |
|
|
} |
226 |
|
|
} |
227 |
joko |
1.3 |
|
228 |
|
|
function store($struct) { |
229 |
|
|
$this->payload = serialize($struct); |
230 |
|
|
$this->_saveProxy(); |
231 |
|
|
} |
232 |
joko |
1.1 |
|
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
?> |