1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
// This is the Roxen WebServer state mechanism. 
// Copyright © 1999 - 2009, Roxen IS. 
// 
// $Id$ 
 
#ifdef STATE_HANDLER_DEBUG 
# define STATE_WERR(X) werror("State: "+X+"\n") 
#else 
# define STATE_WERR(X) 
#endif 
 
//! This module defines a page state mechanism, i.e. a pike 
//! object in which the "objects" on a page can register 
//! their state. If the state in one object is altered the 
//! state in the others are not lost, as would be the case 
//! if all "objects" on the page made their own 
//! <a href="page.html?variable=value"> links. 
//! 
//! The first thing your (tag) module would have to do, 
//! once it has created a state object, is to register 
//! itself in the page state object. This is done by 
//! providing a suggested id, typically the name of the 
//! tag. The registration method then returns the given id, 
//! which may be a different one than the suggested id. 
//! 
//! @code 
//!   string state_id = "my-tag"; 
//!   object state = Page_state(id); 
//!   state_id = state->register_consumer(state_id, id); 
//! @endcode 
//! 
//! Then it is a good idea to update the state object with 
//! the current page state, as given in the encoded state 
//! variable. This variable is typically URI-encoded and 
//! sent in a forms variable between pages. 
//! 
//! @code 
//!   if(id->real_variables->__state && 
//!      !state->uri_decode(id->real_variables->__state[0])) 
//!     RXML.run_error("Error in state.\n"); 
//! @endcode 
//! 
//! It is now possible to retrieve the state associated 
//! with your page object by calling the get method in the 
//! state object. 
//! 
//! Typically you do not set or alter values in the page 
//! state, since the state of the page is only altered by 
//! user action, which happens upon page loads. Instead you 
//! predict, for each action your object provides, what the 
//! resulting state would be and use the encode or 
//! uri_encode methods to get a representation of that state 
//! that is somehow transfered to the next page. I.e. if 
//! your object has two states, 1 and 2, the following code 
//! would calculate the proper way to alter the state. 
//! 
//! @code 
//!   string get_actions(string uri, int current_state, 
//!                      object state) { 
//!     // encode_revisit_url places the encoded state in the variable 
//!     // __state by default. 
//!     return "<a href='" + state->encode_revisit_url(id, 1) + "'>1</a><br />" 
//!            "<a href='" + state->encode_revisit_url(id, 2) + "'>2</a>"; 
//!   } 
//! @endcode 
 
 
// --- State code ------------------------------------------- 
 
#define CHKSPACE "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/" 
 
//! 
class Page_state { 
 
  RequestID id; 
  int use_checksum=1; 
  string stateid=""; 
 
  //! Initialize the state object 
  void create(RequestID in) { 
    id=in; 
    if(!id->misc->state) 
      id->misc+=(["state":(["keys":(<>),"values":([])])]); 
  } 
 
  string _sprintf() { 
    return "Page_state("+stateid+")"; 
  } 
 
  string register_consumer(string name) 
  //! Register a new state consumer and return state consumer id. 
  { 
    if(id->misc->state->keys[name]) { 
      int prefix=0; 
      while(id->misc->state->keys[(string)prefix+name]) 
        prefix++; 
      name=(string)prefix+name; 
    } 
 
    stateid=name; 
    id->misc->state->keys+=(<name>); 
    return name; 
  } 
 
  string use_session(void|string new_session_id) { 
    new_session_id = new_session_id || 
      id->misc->state->session || 
      roxenp()->create_unique_id(); 
 
    id->misc->state->session = new_session_id; 
 
    id->misc->state->values = (cache.get_session_data(new_session_id)||([])) | 
      id->misc->state->values; 
 
    return new_session_id; 
  } 
 
  int uri_decode(string from) 
  { 
    return decode(replace(from, ([ " ":"+", 
                                   "%2B":"+", 
                                   "%2F":"/", 
                                   "%3D":"=" ]))); 
  } 
 
  int decode(string from) 
  //! Decode states from a string. 
  //! Returns 1 for success, 0 for failure. 
  { 
    if(!from) 
      return 0; 
 
    string session_id; 
    sscanf(from, "%s$%s", session_id, from); 
 
    if(use_checksum){ 
      int chksum=0; 
      for(int i=1; i<sizeof(from); i++) 
        chksum+=from[i]; 
      if(from[0] != CHKSPACE[chksum%64]) { 
        report_fatal("Error in state checksum. (%O, %O)\n", 
                     id->not_query, from); 
        return 0; 
      } 
      from=from[1..]; 
    } 
 
    if(session_id) { 
      id->misc->state->session = id->misc->state->session || session_id; 
      id->misc->state->values = (cache.get_session_data(session_id)||([])) | 
        id->misc->state->values; 
    } 
 
    mixed error=catch { 
      from = MIME.decode_base64(from); 
      object gz = Gz; 
      from = gz->inflate()->inflate(from); 
    }; 
    if(error) { 
      report_fatal("Error in state compression/transport encoding.\n"); 
      return 0; 
    } 
 
    mapping new_state; 
    if( catch(new_state=decode_value(from)) ) { 
      report_fatal("Error in state decode value.\n"); 
      return 0; 
    } 
    if(!new_state) return 1; 
    if(!mappingp(new_state)) return 0; 
 
    id->misc->state->values = id->misc->state->values | new_state; 
 
    return 1; 
  } 
 
  array(string) list_consumers() 
  //! List all registered state consumers. 
  { 
    return indices(id->misc->state->keys); 
  } 
 
  protected string low_encode_state(mapping state, void|mapping diff) { 
    string session_id = id->misc->state->session; 
    if(session_id) { 
      cache.set_session_data(state, session_id); 
      state = diff; 
    } 
    else if(diff) 
      state = state + diff; 
 
    string from = encode_value(state); 
    object gz = Gz; 
    string to = MIME.encode_base64( gz->deflate()->deflate(from), 1); 
 
    if(use_checksum) { 
      int chksum=0; 
      for(int i=0; i<sizeof(to); i++) 
        chksum+=to[i]; 
      to=CHKSPACE[chksum%64..chksum%64]+to; 
    } 
 
    if(session_id) to = session_id + "$" + to; 
 
    return to; 
  } 
 
  mixed get(void|string key) 
  //! Get a specific state. 
  { 
    return id->misc->state->values[key||stateid]; 
    return 0; 
  } 
 
  string alter(mixed value, void|string key) 
  //! Alter a state. 
  { 
    id->misc->state->values[key||stateid]=value; 
    return low_encode_state(id->misc->state->values); 
  } 
 
  //! @decl string encode() 
  //! @decl string encode(mixed value) 
  //! @decl string encode(mixed value, string key) 
  //! @decl string encode(array value, array key) 
  //! Encode present state into a string. 
  string encode(void|mixed value, void|string|array key) { 
    if(value) { 
      if(arrayp(key)) { 
        if(!arrayp(value)) 
          error("Bad argument 1 to encode. " 
                "If key is an array then value also has to be an array."); 
 
        if(sizeof(value) != sizeof(key)) 
          error("encode called on arrays of different sizes (%d != %d).", 
                sizeof(value), sizeof(key)); 
         
        return low_encode_state(id->misc->state->values, 
                                 mkmapping(key, value)); 
      } 
      else 
        return low_encode_state(id->misc->state->values, 
                                 ([key||stateid:value])); 
    } 
    return low_encode_state(id->misc->state->values); 
  } 
 
  string uri_encode(void|mixed value, void|string|array key) 
  //! Encode present state into a URI safe string. 
  { 
    return replace(encode(value,key), ([ "+":"%2B", "/":"%2F", "=":"%3D" ])); 
  } 
 
  string encode_revisit_url (RequestID id, mixed value, 
                             void|string|array key, void|string var) 
  //! Encode present state into an URL to revisit the current page, 
  //! according to @[id]. The encoded state is passed in the variable 
  //! @[var], which defaults to "__state" if not given. All other 
  //! variables that was sent in the URL from the client are retained. 
  //! 
  //! @note 
  //! The other variables in the URL have the values that were sent 
  //! from the client and not the values they have currently. That's a 
  //! feature, since the revisit URL will then "redo" the page in all 
  //! respects except the state change. Variables that were passed as 
  //! headers in a POST method are left out, which also is a feature 
  //! for the same reason, considering the intended use of POST. 
  { 
    string other_vars; 
 
    if (id->query) { 
      other_vars = "&" + id->query; 
      int i = search (other_vars, "&__state="); 
      if (i >= 0) { 
        int j = search (other_vars, "&", i + 1); 
        other_vars = other_vars[..i - 1] + (j > 0 ? other_vars[j..] : ""); 
      } 
    } 
    else other_vars = ""; 
 
    // Use a relative url. It's shorter and doesn't give problems when 
    // result p-code is replicated. 
    return (id->not_query / "/")[-1] + 
      "?" + (var || "__state") + "=" + uri_encode (value, key) + other_vars; 
  } 
} 
 
string decode_session_id(string state) { 
  if(!state) return 0; 
  string session_id; 
  sscanf(state, "%s$", session_id); 
  return session_id; 
}