forked from Da-Fecto/ProcessPageListerUrls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessPageListerUrls.module
423 lines (359 loc) · 11.8 KB
/
ProcessPageListerUrls.module
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
<?php namespace ProcessWire;
/**
* Class ProcessPageListerUrls
*
* Copyright 2016 by Martijn Geerts
*
* ProcessWire 2.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
class ProcessPageListerUrls extends ProcessPageLister implements ConfigurableModule {
/**
* Module info
*
* @return array Associative array with module information
*
*/
public static function getModuleInfo() {
return array(
'title' => 'PLUs (Page Lister URLs)',
'summary' => 'Link to listerpages with predefined settings.',
'version' => '0.0.3',
'author' => 'Martijn Geerts',
'icon' => 'search-plus',
'singular' => true,
'autoload' => false,
'permission' => 'page-lister',
);
}
// @var Page See ___execute
protected $linkPage;
// @var Page ListerPage
protected static $listerPage = null;
// @var string Default cipher method
public static $defaultCipherMethod = 'AES-256-CBC';
// @var array Fieldnames allowed as columns
protected $allowedColumns = null;
public function __construct() {
$this->set('cipher_method', self::$defaultCipherMethod);
$this->set('iv_size', '');
}
/**
* Get the page where this module lives.
*
*/
public function init() {
parent::init(); // always remember to call the parent init
if (!$this->cipher_method) $this->error("Please select a cipher method in the module configuration.");
$this->linkPage = $this->wire('pages')->get('template=admin, process=' . $this->wire('modules')->getModuleID($this));
}
/**
* ListerProUrl playground, handy for creating ListerProUrls.
*
* @return string Markup
*
*/
public function ___execute() {
$string = '';
$alphabet = range('a', 'z');
foreach ($alphabet as $letter) {
$part = $this->wire('input')->get($letter);
if (!$part) break;
$string .= $part;
}
$dirty = wireDecodeJSON($this->decrypt(rawurldecode($string)));
$listerPage = isset($dirty['listerPage']) ? $this->listerPage($dirty['listerPage']) : $this->wire(new NullPage());
// return invalid page
if (!$listerPage->id) {
$message = $this->_("We couldn't load the lister page.");
return $this->invalidPage($message);
}
$allow = array('initSelector', 'defaultSelector', 'columns', 'defaultSort');
$clean = array();
foreach ($allow as $key) {
if (!isset($dirty[$key])) continue;
$value = $dirty[$key];
$sanitized = '';
if ($key === 'initSelector') {
// do access stuf, etc.
$sanitized = $this->validateSelector($value);
} else if ($key === 'defaultSelector') {
// do access stuf, etc.
$sanitized = $this->validateSelector($value);
} elseif ($key === 'columns' && is_array($value)) {
$sanitized = $this->validateColumns($value);
} elseif ($key === 'defaultSort') {
$sanitized = $this->sanitizer->selectorValue($value);
}
// Assign sanitized value to variables
$$key = $sanitized;
}
// Maximum include hidden and not called twice (Bug ListerPro ?)
$includeAmount = (int) substr_count($initSelector . $defaultSelector, 'include=');
if ($includeAmount === 0) {
$initSelector . ', include=hidden';
} else if ($includeAmount > 1) {
$cleanedSelectors = array_map(function($value) {
return trim($value);
}, array_filter(explode(',', $defaultSelector), function($value) {
return strpos($value, 'include=') === false;
}));
$defaultSelector = implode(', ', $cleanedSelectors);
}
$this->wire('session')->set('initSelector', $initSelector);
$clean = array(
'initSelector' => $initSelector,
'defaultSelector' => $defaultSelector,
'columns' => $columns,
'defaultSort' => $defaultSort,
);
foreach (array_keys($clean) as $key) {
$value = $clean[$key];
if ($key === 'columns' && is_string($value)) {
unset($clean[$key]);
} else if (!$value) {
unset($clean[$key]);
}
}
self::$listerPage = $listerPage;
$url = self::addSessionBookmarkCustom(md5(json_encode($clean)), $clean);
if (!$url) return $this->invalidPage();
$this->session->redirect($url);
}
/**
* Given a unique ID and an array of Lister settings (in $bookmark) return a URL to view those pages in Lister
*
* @param string $id ID or name of bookmark
* @param array $bookmark Bookmark data
* @return string Returns URL to Lister with this bookmark or blank on failure (like if user doesn't have access)
*
*/
public static function addSessionBookmarkCustom($id, array $bookmark) {
$user = wire('user');
if(!$user->isSuperuser() && !$user->hasPermission('page-lister')) return '';
$maxBookmarks = 30;
$bookmarks = wire("session")->get(self::sessionBookmarksName);
if(!is_array($bookmarks)) $bookmarks = array();
if(count($bookmarks) > $maxBookmarks) {
// trim bookmarks to max size
$bookmarks = array_slice($bookmarks, -1 * $maxBookmarks, null, true);
}
$bookmarks[$id] = $bookmark;
wire("session")->set(self::sessionBookmarksName, $bookmarks);
return self::$listerPage->url . "?session_bookmark=$id";
}
/**
* Whether or not to allow the given $fields as columns.
*
* @param array $columns Array with column names
* @return array Cleaned array
*
*/
public function validateColumns(array $columns) {
$array = array();
foreach ($columns as $fieldName) {
// System columns (name, status, template, templates_id etc... etc...)
if (isset($this->systemLabels[$fieldName])) {
$array[] = $fieldName;
continue;
}
$a = explode('.', $fieldName);
$field = $this->fields->get($a[0]);
if (!($field instanceof Field)) continue;
if (!$this->allowColumnField($field)) continue;
$array[] = $fieldName;
}
return $array;
}
/**
* Get Initialization Vector size
*
* @param string $method
* @return mixed Integer on succes
*
*/
private function getIvLength($method = null) {
if (!$method) throw new WireException("getIvLength needs a method.");
$length = openssl_cipher_iv_length($method);
if ($length === false && is_string($this->iv_size)) {
$this->error("Can't get the cipher initialization vector (iv) length.");
} else if ($length === false) {
$length = (int) $this->iv_size;
}
return $length;
}
/**
* Encrypt String
*
* @param string $string String to encrypt
* @param string $method Cipher method to use
* @return string Encrypted string
*
*/
public function encrypt($string, $method = null) {
$hash = $this->wire('config')->userAuthSalt;
if (!$method) $method = $this->cipher_method;
$length = $this->getIvLength($method);
// will set error
if ($length === false) return false;
$iv = @mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
$encrypted = @openssl_encrypt($string, $method, $hash, 0, $iv);
return ($encrypted === false) ? false : $iv . $encrypted;
}
/**
* Decrypt encrypted string
*
* @param string $string String to encrypt
* @param string $method Cipher method to use
* @return string Decrypted string
*
*/
public function decrypt($string, $method = null) {
if (!$method) $method = $this->cipher_method;
$length = $this->getIvLength($method);
if (!$length) return false;
$iv = substr($string, 0, $length);
$string = substr($string, $length);
$hash = $this->wire('config')->userAuthSalt;
return openssl_decrypt($string, $method, $hash, 0, $iv);
}
/**
* Invalid Listerpage Markup
*
* @param string $message
* @return string
*
*/
public function invalidPage($message = '') {
if (!$message) $message = $this->_("We encountered a problem.");
$this->headline($this->className());
$wrapper = $this->wire('modules')->get('InputfieldForm');
$f = $this->wire('modules')->get('InputfieldMarkup');
$f->set('label', $this->className());
$f->set('value', $message);
$wrapper->add($f);
return $wrapper->render();
}
/**
* Return a valid ProcessPageListerPro or a NullPage
*
* @param mixed integer|Page $ListerPage Admin page with ProcessPageListerPro process
* @return Page Valid ListerProPage or NullPage on failing.
*
*/
public function listerPage($listerPage) {
// Check for ID
if (!($listerPage instanceof Page)) {
if (!ctype_digit("$listerPage")) return new NullPage;
$listerPage = $this->wire('pages')->get($listerPage);
}
if ($listerPage->template != 'admin') return new NullPage;
if (!$listerPage->fields->has('process')) return new NullPage;
if (strpos('ProcessPageListerPro', $listerPage->process) !== false) return $listerPage;
return $this->wire(new NullPage);
}
/**
* Set sessions and returns an url.
*
* @param Page $ListerPage Destination of the URL.
* @param string $initSelector Initial selector string that all further selections are filtered by.
* @param string $defaultSelector Default selector string that appears but MAY be removed or changed.
* @param array $columns Array of column names to show in lister.
* @param string $defaultSort Where to sort on.
* @return string
*
*/
public function url(Page $listerPage, $initSelector = '', $defaultSelector = '', array $columns = array(), $defaultSort = '') {
if (!$this->cipher_method) {
return $this->wire('page')->url;
}
// Don't give URL to people that don't have page-lister permissions
$user = $this->wire('user');
if (!$user->isSuperuser() && !$user->hasPermission('page-lister')) return '';
$ListerPage = $this->listerPage($listerPage);
// No lister page return empty string
if (!$ListerPage->id) return '';
$query = array(
'listerPage' => $listerPage->id,
'initSelector' => $initSelector,
'defaultSelector' => $defaultSelector,
'columns' => $columns,
'defaultSort' => $defaultSort,
);
// Build get string with variables no longer then the max 512 characters
$encoded = $this->encrypt(wireEncodeJSON($query, true));
if ($encoded === false) {
$this->error("URL encryption error.");
return '';
}
$results = '';
$alphabet = range('a', 'z');
$string = rawurlencode($encoded);
$parts = str_split($string, 256);
foreach ($parts as $key => $part) $results .= '&' . $alphabet[$key] . '=' . $part;
return $this->linkPage->url . "?" . ltrim($results, '&');
}
/**
* Install
*
* Call ___installPage directly (using page in self::getModuleInfo() doesn't work)
*
*/
public function install() {
$name = 'plus';
$parent = $this->wire('pages')->get("parent.id=" . $this->wire('config')->adminRootPageID . ", name=setup");
$title = self::getModuleInfo()['title'];
$template = 'admin';
parent::___installPage($name, $parent, $title, $template, array('status' => Page::statusHidden));
}
/**
* Install
*
* Call ___uninstallPage directly (using page in self::getModuleInfo() doesn't work)
*
*/
public function uninstall() {
parent::___uninstallPage();
}
/**
* Module config
*
* @param array $data
* @return InputfieldWrapper
*
*/
public static function getModuleConfigInputfields(array $data) {
$inputfields = new InputfieldWrapper();
$cipherMethods = openssl_get_cipher_methods();
$name = 'cipher_method';
$value = isset($data[$name]) ? $data[$name] : self::$defaultCipherMethod;
$f = wire('modules')->get("InputfieldSelect");
$f->set('label', __("Cipher method"));
$f->set('description', __("Cipher method to use for building the URL."));
$f->set('required', true);
$f->set('name', $name);
$f->set('value', $value);
$f->addOption('', '');
foreach ($cipherMethods as $method) $f->addOption($method, $method);
$inputfields->add($f);
$name = 'iv_size';
$value = isset($data[$name]) ? $data[$name] : '';
$f = wire('modules')->get("InputfieldInteger");
$f->set('label', __("Initialization vector size"));
$description =
__("In some cases we are unable to determen the initialization vector size based on the cipher method.") . ' ' .
__("When we fail, we try it with the given size.");
$f->set('description', $description);
$f->set('name', $name);
$f->set('value', $value);
$f->attr('size', '3');
$f->attr('type', 'number');
$f->attr('style', 'width: 3.5em;');
$inputfields->add($f);
return $inputfields;
}
}