Smutty
MVC Framework
View Code: Smutty_Data
Browse: All · Classes · Plugins
1
<?php
2
3
/**
4
* this class controls access to GET, POST and SERVER data. it's
5
* getInstance() method will return an instance with the correct
6
* type for the request method (GET/POST), but you can use this
7
* object to control access to any kind of data really.
8
*
9
*/
10
11
class Smutty_Data extends Smutty_Object {
12
13
/** internal data store */
14
private $data;
15
16
/** is the message signed */
17
private $isSigned;
18
19
/** is the signed message verified */
20
private $isVerified;
21
22
/** the signed message */
23
private $signedMessage;
24
25
/** singletons */
26
private static $instance = null;
27
private static $postInstance = null;
28
private static $getInstance = null;
29
private static $serverInstance = null;
30
31
/**
32
* constructor. if you pass in some data then that will
33
* be used as this objects data, otherwise the object will
34
* look at the request method to determine get/post
35
*
36
* @param hash $data optional data to use
37
* @return nothing
38
*
39
*/
40
42
43
$this->isSigned = false;
44
$this->signedMessage = null;
45
$this->isVerified = null;;
46
47
if ( $data )
48
$this->data = $data;
49
else {
50
51
$server = Smutty_Data::getServerData();
52
$isPost = ( $server->string('REQUEST_METHOD') == 'POST' );
53
$type = $server->string( 'HTTP_X_OPENPGP_TYPE' );
54
55
// check if it's PGP SIGNED
56
if ( preg_match('/[SE]/',$type) ) {
57
58
// create message body and signed object
59
$data = $isPost ? file_get_contents('php://input') : $server->string('QUERY_STRING');
60
$body = $this->createGpgMessageBody( $data );
61
$this->signedMessage = new Smutty_GPG_SignedMessage( $body );
62
$this->isSigned = true;
63
64
}
65
66
// store data for request
67
$this->data = $isPost ? $_POST : $_GET;
68
69
}
70
71
}
72
73
/**
74
* creates a signed message with the specified data. the message
75
* parameters are filled in from the http headers
76
*
77
* @param String $data data for the message
78
* @return String a gpg message
79
*
80
*/
81
83
84
$server = Smutty_Data::getServerData();
85
$body = "-----BEGIN PGP SIGNED MESSAGE-----
86
Hash: " . $server->string('HTTP_X_OPENPGP_DIGEST_ALGO') . "
87
88
" . $data . "
89
-----BEGIN PGP SIGNATURE-----
90
Version: " . $server->string('HTTP_X_OPENPGP_VERSION') . "
91
92
" . $server->string('HTTP_X_OPENPGP_SIG') . "
93
-----END PGP SIGNATURE------
94
";
95
96
return $body;
97
98
}
99
100
/**
101
* indicates if the data is PGP signed
102
*
103
* @return boolean if the data is signed
104
*
105
*/
106
108
return $this->isSigned;
109
}
110
111
/**
112
* verifies the data's signature (if there is one). returns
113
* a boolean indicating if it's good or not.
114
*
115
* @return boolean if the data is verified
116
*
117
*/
118
120
121
if ( $this->isVerified == null && $this->isSigned ) {
122
$gpg = Smutty_GPG::getInstance();
123
$this->isVerified = $gpg->verify(
124
$this->signedMessage->getText()
125
);
126
}
127
128
return $this->isVerified;
129
130
}
131
132
/**
133
* returns a data object for $_POST
134
*
135
* @return Smutty_Data post data
136
*
137
*/
138
139
public static function &getPostData() {
140
if ( self::$postInstance == null )
141
self::$postInstance = new Smutty_Data( $_POST );
142
return self::$postInstance;
143
}
144
145
/**
146
* returns a data object for $_GET
147
*
148
* @return Smutty_Data get data
149
*
150
*/
151
152
public static function &getGetData() {
153
if ( self::$getInstance == null )
154
self::$getInstance = new Smutty_Data( $_GET );
155
return self::$getInstance;
156
}
157
158
/**
159
* returns a data object for $_SERVER
160
*
161
* @return Smutty_Data server data
162
*
163
*/
164
165
public static function &getServerData() {
166
if ( self::$serverInstance == null )
167
self::$serverInstance = new Smutty_Data( $_SERVER );
168
return self::$serverInstance;
169
}
170
171
/**
172
* used to set values for this object
173
*
174
* @param String $name name of param
175
* @param String $value value to set
176
* @return nothing
177
*
178
*/
179
181
$this->data[ $name ] = $value;
182
}
183
184
/**
185
* static method for fetching the instance of this class (as
186
* it's designed to be a singleton)
187
*
188
* @return Smutty_Data the class instance
189
*
190
*/
191
192
public static function &getInstance() {
193
if ( self::$instance == null )
194
self::$instance = new Smutty_Data();
195
return self::$instance;
196
}
197
198
/**
199
* returns the value of the specifed variable as
200
* an integer (false if it's not valid)
201
*
202
* @param String $name name of variable to fetch
203
* @return integer value
204
*
205
*/
206
208
return Smutty_Data::getInt( v($this->data,$name) );
209
}
210
211
/**
212
* this method can be used staticly to validate data
213
* data as being an integer. returns false when invalid.
214
*
215
* @param String $value the value to validate
216
* @return integer integer
217
*
218
*/
219
221
// is this too heavy-weight?
222
$badchars = preg_replace( '/[0-9]/', '', '' . $value );
223
return $badchars ? false : $value;
224
}
225
226
/**
227
* returns an integer with a padded 0 if it
228
* is less than 10
229
*
230
* @param String $name the name of the variable
231
* @return integer padded integer
232
*
233
*/
234
236
$value = $this->int( $name );
237
return ( $value < 10 ) ? "0$value" : $value;
238
}
239
240
/**
241
* returns the value of the specifed variable
242
* as a string.
243
*
244
* @param String $name name of variable
245
* @return string value
246
*
247
*/
248
250
return v( $this->data, $name );
251
}
252
253
/**
254
* returns an object from the data
255
*
256
* @param String $name name of variable
257
* @return object value
258
*
259
*/
260
262
return isset($this->data[$name]) ? $this->data[$name] : false;
263
}
264
265
/**
266
* returns a data object, which is a new instance of
267
* this object. this will be something like args passed
268
* in from a form using name[value] type args
269
*
270
* @param String $name the name of the data array
271
* @return Smutty_Data hash/data/object
272
*
273
*/
274
276
return new Smutty_Data( v($this->data,$name) );
277
}
278
279
/**
280
* returns the fields that exist in this data model
281
*
282
* @return array array of field names
283
*
284
*/
285
287
$fields = array();
288
foreach ( $this->data as $name => $value )
289
array_push( $fields, $name );
290
return $fields;
291
}
292
293
/**
294
* returns the current date and time formatted as a string
295
* for use with the database
296
*
297
* @return String date format
298
*
299
*/
300
302
$db = Smutty_Database::getInstance();
303
return $db ? $db->getCurrentDate() : 'Y-m-d';
304
}
305
306
/**
307
* checks to see if a value exists (isset)
308
*
309
* @param String $name name of param to check
310
* @return boolean if parameter exists
311
*
312
*/
313
315
return isset( $this->data[$name] );
316
}
317
318
}
319
320
?>
The code shown here is the code that is currently running this site. If you want to view the latest SVN version of the code then go to the Subversion repository.