-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyntax.php
More file actions
222 lines (200 loc) · 5.35 KB
/
Copy pathsyntax.php
File metadata and controls
222 lines (200 loc) · 5.35 KB
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
<?php
// must be run within DokuWiki
if(!defined('DOKU_INC')) die();
if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
require_once DOKU_PLUGIN.'syntax.php';
/**
* Change {{ passwords }} to passwords.
* set amount/length using url parameters like passwords?length=3&amount=2.
*/
class syntax_plugin_passwords extends DokuWiki_Syntax_Plugin
{
/**
* @var string allowed characters
*/
private $allowedCharacters = 'abcdefghjkmnpqrstuvwxyz123456789ABCDEFGHJKLMNPQRSTUVWXYZ';
/**
* @var int amount of passwords
*/
private $amount = 4;
/**
* @var int length of password
*/
private $length = 8;
/**
* @var int iterator
*/
private $iterator = 0;
/**
* get plugin type
*
* @return string
*/
public function getType() {
return 'substition';
}
/**
* get sorting order
*
* @return int
*/
public function getSort() {
return 32;
}
/**
* Define matching strings
*
* @param string $mode
*/
public function connectTo($mode) {
$this->Lexer->addSpecialPattern('\{\{ passwords }}', $mode, 'plugin_passwords');
$this->Lexer->addSpecialPattern('\{\{ passwords\? }}', $mode, 'plugin_passwords');
$this->Lexer->addSpecialPattern('\{\{ passwords\?amount=\d*&length=\d* }}', $mode, 'plugin_passwords');
$this->Lexer->addSpecialPattern('\{\{ passwords\?length=\d*&amount=\d* }}', $mode, 'plugin_passwords');
$this->Lexer->addSpecialPattern('\{\{ passwords\?amount=\d* }}', $mode, 'plugin_passwords');
$this->Lexer->addSpecialPattern('\{\{ passwords\?length=\d* }}', $mode, 'plugin_passwords');
}
/**
* set the parameters for $data for the render function
*
* @param string $match gematchte string
* @param int $state
* @param int $pos
* @param DokuHandler $handler
*
* @return array
*/
public function handle($match, $state, $pos, Doku_Handler $handler) {
return array($match, $state, $pos);
}
/**
* Render the passwords
*
* @param string $format
* @param Doku_Renderer_metadata $renderer
* @param array $data
*
* @return type boolean is de operatie geslaagd?
*/
public function render($format, Doku_Renderer $renderer, $data) {
if('xhtml' === $format) {
$this->iterator = 0;
$this->parseData($data[0]);
for($i=0; $i<$this->getAmount(); $i++) {
$renderer->doc .= $this->getPasswordHtml($this->getLength());
}
return true;
} else {
return false;
}
}
/**
* Parse data to filter out variables
*
* @param string $data
*
* @return boolean success
*/
private function parseData($data) {
$regexAmount = '#amount=(\d*)#i';
$regexLength = '#length=(\d*)#i';
preg_match($regexAmount, $data, $resultAmount);
$this->setAmount($resultAmount[1]);
preg_match($regexLength, $data, $resultLength);
$this->setLength($resultLength[1]);
return true;
}
/**
* Generate the password
*
* @param int $length lengte
*
* @return string
*/
private function generatePassword($length) {
$password = '';
for($j=0; $j < $length; $j++) {
$r = rand(0,strlen($this->getAllowedCharacters())-1);
$c = substr($this->getAllowedCharacters(), $r, 1);
$password .= $c;
}
return $password;
}
/**
* Generate HTML
*
* @param int $passwordLength length of password
*
* @return string
*/
private function getPasswordHtml($passwordLength) {
$iterator = $this->getIterator();
$iteratorHtml = '';
if($iterator < 10) {
$iteratorHtml .= '0';
}
$iteratorHtml .= $iterator;
return $iteratorHtml
. '<input class="inputfield" '
. ' type="text" '
. ' value="' . $this->generatePassword($passwordLength) . '"'
. ' onmouseover="this.select();" '
. ' onclick="this.select();" '
. ' />'
. '<br>';
}
/**
* Haal de iterator op
*
* @return int
*/
private function getIterator() {
return ++$this->iterator;
}
/**
* Get the allowed characters
*
* @return string
*/
private function getAllowedCharacters() {
return $this->allowedCharacters;
}
/**
* Get the amount of passwords
*
* @return int
*/
private function getAmount() {
return $this->amount;
}
/**
* Set the amount of passwords
*
* @param int $amount
*/
private function setAmount($amount) {
$amount = (int) $amount;
if($amount > 0) {
$this->amount = $amount;
}
}
/**
* Get the lenght of the password
*
* @return int
*/
private function getLength() {
return $this->length;
}
/**
* Set the lenght of the password
*
* @param int $length
*/
private function setLength($length) {
$length = (int) $length;
if($length > 0) {
$this->length = $length;
}
}
}