My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
/**
* Wikka Action Class
*
* An abstract class that can be extended for wikka action classes.
*
*
* @package Action
* @author Tom Atwell <klenwell@gmail.com>
* @copyright Copyright 2010, Tom Atwell <klenwell@gmail.com>
* @license GNU General Public License v3
* @link http://www.opensource.org/licenses/gpl-3.0.html
* @link http://klenwell.com/is
*
*/

/**
* Base wikka action class.
*
* @package Action
* @subpackage Libs
* @author Tom Atwell <klenwell@gmail.com>
*/
class WikkaAction {
# defaults
var $version = '0.8';

# internal
var $Wikka = NULL;
var $Param = array();
var $Get = array();
var $Post = array();
var $Datastore = array(); # active datastore list (data out)
var $_Datastore = array(); # loaded datastore (data in)
var $user = NULL;
var $user_role = 'viewer'; # viewer, user, or admin
var $PageAcl = array();
var $is_admin = FALSE;
var $is_logged_in = FALSE;
var $is_cli = FALSE;
var $here = '';

# ACL Settings
var $AclAllow = array(
'all' => '*',
'users' => '+',
'admins' => '!*',
);

# PhpMailer Settings
var $PhpMailerDefault = array(
'host' => 'smtp.gmail.com',
'port' => 587,
'debug' => 0
);

function __construct($Wakka=NULL, $ActionParams=array())
{
if ( is_null($Wakka) ) {
$this->_abort('must pass wakka object as argument for action class');
}
$this->Wikka = $Wakka;
$this->Param = $ActionParams;
$this->Get = $_GET;
$this->Post = $_POST;
$this->page = $this->Wikka->tag;
$this->is_cli = defined('WIKKA_ALLOW_CLI');

# get url path
if ( isset($_SERVER['SCRIPT_URL']) ) {
$this->here = $_SERVER['SCRIPT_URL'];
}
elseif ( isset($_SERVER['REQUEST_URI']) ) {
$this->here = $_SERVER['REQUEST_URI'];
}
else {
$this->here = $this->Wikka->GetConfigValue('base_url') . $this->page;
}

$this->_load_wikka_acl();
}

function main() {
$this->_abort('this method must be overridden');
}

function output($content, $wikka_format=0) {
if ( $wikka_format ) {
$content = $this->Wikka->format($content);
}
print $content;
}

function action_set_up() {
$this->_abort('this method must be overridden');
}

function has_param($key) {
return isset($this->Param[$key]);
}

function get_param($key) {
if ( ! $this->has_param($key) ) {
return NULL;
}
return $this->Param[$key];
}

function get_config($key) {
return $this->Wikka->GetConfigValue($key);
}

/**
* Safe Sql
* Use parameterized mysql queries
*/
function safe_sql($sqlf, $ParamList) {
$SafeParamList = array();
foreach ( $ParamList as $value ) {
$SafeParamList[] = mysql_real_escape_string($value);
}
$SprintfArgList = array_merge(array($sqlf), $SafeParamList);
return call_user_func_array('sprintf', $SprintfArgList);
}

/**
* Datastore Functions
* These functions can be used to persist data between calls.
*
*/
function store($key, $value) {
$this->Datastore[$key] = $value;
}

function get_store($key, $page=NULL) {
$Datastore = $this->load_datastore($page);
$value = ( isset($Datastore[$key]) ) ? $Datastore[$key] : NULL;
return $value;
}

function load_datastore($page=NULL) {
if ( ! $page ) {
$page = $this->page;
}

if ( isset($this->_Datastore[$page]) ) {
return $this->_Datastore[$page];
}

# load page
$content = $this->load_page_body($page);

# extract datastore
$datastore_regex = $this->_get_datastore_regex();
$is_matched = preg_match_all($datastore_regex, $content, $Matches);
if ( ! $is_matched || !isset($Matches[1][0]) ) {
return NULL;
}

# cache and return
$this->_Datastore[$page] = unserialize( base64_decode($Matches[1][0]) );
return $this->_Datastore[$page];
}

function save_datastore($page=NULL) {
# serialize datastore
$serialization = base64_encode( serialize($this->Datastore) );
$datastore_shell = $this->_get_datastore_regex($return_shell=1);
$datastore = sprintf($datastore_shell, $serialization);

# purge current datastore
$page_content = $this->purge_datastore($page);

# save page with datastore
#print $this->pr(array('before store'=>htmlentities($page_content)));
$page_content = $page_content .= $datastore;
#print $this->pr(array('after store'=>htmlentities($page_content)));
return $this->save_page($page, $page_content);
}

function purge_datastore($page=NULL, $save=FALSE) {
$page_content = $this->load_page_body($page);
$datastore_regex = $this->_get_datastore_regex();
$page_content = preg_replace($datastore_regex, '', $page_content);

if ( $save ) {
$note = 'purging datastore';
$this->save_page($page, $page_content);
}

return $page_content;
}

function _get_datastore_regex($return_shell=FALSE) {
$RegexParts = array(
'""<!-- [wikka_datastore] ',
'(.*?)',
' -->""'
);

if ( $return_shell ) {
return sprintf('%s%s%s', $RegexParts[0], '%s', $RegexParts[2]);
}

$datastore_regex = sprintf('~%s%s%s~',
preg_quote($RegexParts[0]),
$RegexParts[1],
preg_quote($RegexParts[2]));

return $datastore_regex;
}

/**
* Page-Related Database Functions
* Effectively wrappers for wikka functions
*/
function load_page_body($page=NULL) {
if ( is_null($page) ) {
$page = $this->page;
}

$Record = $this->Wikka->LoadPage($page, NULL, 0);
$body = ( isset($Record['body']) ) ? $Record['body'] : NULL;
return $body;
}

function create_page($tag, $body, $note, $user=NULL, $owner=NULL) {

if ( $this->load_page_body($tag) ) {
trigger_error("page '$tag' already exists", E_USER_NOTICE);
return FALSE;
}

if ( ! $owner && $user ) {
$owner = $user;
}
elseif ( ! $owner ) {
$owner = '(Public)';
}

if ( ! $user ) {
$user = '(Action Class)';
}

$sqlf = <<<HERESQL
INSERT INTO %s
SET
tag = '%s',
body = '%s',
note = '%s',
user = '%s',
owner = '%s',
latest = 'Y',
time = NOW()
HERESQL;

$ParamList = array(
$this->get_config('table_prefix') . 'pages',
$tag, $body, $note, $user, $owner
);

# insert new user
$sql = $this->safe_sql($sqlf, $ParamList);
$this->Wikka->Query($sql);
print $this->load_page_body($tag);
}

function save_page($page=NULL, $content='', $note=NULL) {
if ( is_null($page) ) {
$page = $this->page;
}

if ( is_null($note) ) {
$note = 'page saved by action class';
}

return $this->Wikka->SavePage($page, $content, $note);
}

/**
* Mail Functions
*/
function mail($toList, $subject, $body) {

# gmail
if ( $this->Wikka->GetConfigValue('use_gmail') ) {
return $this->mail_with_gmail($toList, $subject, $body);
}

# php mail
else {
return $this->php_mail($toList, $subject, $body);
}

/*# alternative
else {
trigger_error('Action class mail configured only to work with phpmailer.'
. ' Override the mail method to use an alternative method.',
E_USER_WARNING);
return FALSE;
}
*/
}

/**
* Send an email using php's mail function (server must be configured
* properly)
* @link http://php.net/manual/en/function.mail.php
*/
function php_mail($toList, $subject, $body)
{
$SendResults = array();
$from_email = $this->Wikka->GetConfigValue("admin_email");

# build recipient string
$RecipientList = array();
$toList = $this->_normalize_email_list($toList);
foreach ( $toList as $tuple ) {
$RecipientList[] = sprintf('%s <%s>', $tuple[1], $tuple[0]);
}

# build headers
$HeaderList = array();
$HeaderList[] = sprintf('From:%s', $from_email);
$HeaderList[] = sprintf('Reply-To:%s', $from_email);

# send to each
$to = implode(', ', $RecipientList);
$headers = implode("\n", $HeaderList);
return mail($to, $subject, $body, $headers);
}

/**
* Send an email through a gmail account using PhpMailer
* @link http://phpmailer.worxware.com/index.php?pg=examplebgmail
*/
function mail_with_gmail($toList, $subject, $body) {

if ( empty($this->PhpMailer) ) {
require_once $this->Wikka->GetConfigValue('phpmailer_path');
$this->PhpMailer = new PHPMailer();
$this->PhpMailer->IsSMTP();
$this->PhpMailer->SMTPAuth = TRUE;
$this->PhpMailer->SMTPSecure = 'tls';
$this->PhpMailer->Host = $this->PhpMailerDefault['host'];
$this->PhpMailer->Port = $this->PhpMailerDefault['port'];
$this->PhpMailer->SMTPDebug = ($this->PhpMailerDefault['debug']) ? 2 : 0;
$this->PhpMailer->Username = $this->Wikka->GetConfigValue('gmail_user');
$this->PhpMailer->Password = $this->Wikka->GetConfigValue('gmail_pw');

$from_name = $this->Wikka->GetConfigValue('gmail_name');
if ( empty($from_name) ) {
$from_name = $this->PhpMailer->Username;
}
$this->PhpMailer->SetFrom($this->PhpMailer->Username, $from_name);
$this->PhpMailer->AddReplyTo($this->PhpMailer->Username, $from_name);
}

# add senders
$toList = $this->_normalize_email_list($toList);
foreach ( $toList as $tuple ) {
$this->PhpMailer->AddAddress($tuple[0], $tuple[1]);
}

# prepare message
$this->PhpMailer->Subject = $subject;
$this->PhpMailer->Body = $body;

# send
if ( ! $this->PhpMailer->Send() ) {
trigger_error(sprintf('PhpMailer Error: %s',
$this->PhpMailer->ErrorInfo), E_USER_WARNING);
return FALSE;
}
else {
return TRUE;
}
}

/**
* Possible variations of $toList:
* 1) string(email)
* 2) array(email, name)
* 3) array(email, email, ...)
* 4) array( array(email, name), array(email, name), ... )
*
* @return array returns an array of arrays (email, name)
*/
function _normalize_email_list($toList)
{
$EmailList = array();

# case 1: convert string to array
if ( ! is_array($toList) ) {
$toList = array( $toList );
}
# case 2: single email/name pair
elseif ( count($tuple) == 2 && strpos($tuple[1], '@') ) {
$toList = array( array($tuple[0], $tuple[1]) );
}

# list should now be case 3 or 4
foreach ( $toList as $tuple ) {
# case 3: list of emails
if ( ! is_array($tuple) ) {
$NameSplit = explode('@', $tuple);
$EmailList[] = array($tuple, $NameSplit[0]);
}

# case 4: list of email/name pairs assumed
else {
$EmailList[] = array($tuple[0], $tuple[1]);
}
}

return $EmailList;
}

/**
* Utility Methods
*/
function pr($Array, $print=FALSE) {
$dump = print_r($Array,1);
if ( ! $print ) {
return $dump;
}
else {
printf('<pre>%s</pre>', $dump);
}
}

function get_source_code($file) {
return highlight_file($file, 1);
}

/**
* Private Methods
*/
function _load_wikka_acl()
{
# load user
$this->user = $this->Wikka->GetUser();
if ( !empty($this->user) ) {
$this->is_admin = $this->Wikka->IsAdmin($this->user);
}

if ( $this->is_admin ) {
$this->user_role = 'admin';
}
elseif ( $this->user ) {
$this->user_role = 'user';
}

# is logged in
$this->is_logged_in = in_array($this->user_role, array('admin', 'user'));

# load acls
$AclList = $this->Wikka->LoadAllACLs($this->page);
$this->PageAcl['read'] = $AclList['read_acl'];
$this->PageAcl['write'] = $AclList['write_acl'];
$this->PageAcl['comment_read_acl'] = $AclList['comment_read_acl'];
$this->PageAcl['comment_post_acl'] = $AclList['comment_post_acl'];
$this->PageAcl['comment'] = $AclList['comment_post_acl']; # deprecated

return;
}

function _abort($msg) {
$m = sprintf('Action Exception: %s', $msg);
throw new Exception($m);
}
}

Change log

r278 by klenwell on Jan 14, 2012   Diff
updates wikka action lib class to work
with v1.3
Go to: 
Project members, sign in to write a code review

Older revisions

r255 by klenwell on May 13, 2010   Diff
v1s5 complete
r253 by klenwell on Apr 27, 2010   Diff
refactored wikka comment code to use
recaptcha -- needs further testing
r251 by klenwell on Apr 18, 2010   Diff
added sql injection security to wikka
activity summary and updated wikka
action class
All revisions of this file

File info

Size: 13658 bytes, 472 lines
Powered by Google Project Hosting