-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchsetbuilder2.php
459 lines (275 loc) · 13 KB
/
chsetbuilder2.php
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
<?php
class CHSBuilder {
private $needs; //assoc array, etc.
private $produce_case; //boolean (1.1)
function __construct() {
$this->CHSETSTR = "";
//$this->ASCII_PRINT95 = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
//1.1 deprecated, see below
//1.1 assoc optimisation
$this->chars = array_flip(['littles', 'CAPS', 'digits', 'specials', 'space', 'excluded']);
$this->chars['littles'] = range('a', 'z');
$this->chars['CAPS'] = range('A', 'Z');
$this->chars['digits'] = range('0 ', '9 '); //the old way - '0 '... is ok
$this->chars['specials'] = str_split("!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~");
$this->chars['space'] = array(" ");
$this->chars['excluded'] = ""; //'excluded' (!) not 'exclude'
//1.1 dev change
//$this->ascii_full = array_merge($this->space, $this->littles, $this->CAPS, $this->digits, $this->specials);
$this->ascii_full = array_merge($this->chars['space'], $this->chars['littles'], $this->chars['CAPS'], $this->chars['digits'], $this->chars['specials']); //no foreach, I want to see the order
$this->ASCII_PRINT95 = implode($this->ascii_full);
echo strlen($this->ASCII_PRINT95);
$this->produce_case = false;
//1.1
//?
$this->needs = array_flip(array('littles', 'CAPS', 'digits', 'specials', 'space', 'exclude'));
foreach ($this->needs as $k=>$val) $this->requirements[$k] = Array();
//former - all false
$this->needs['littles'] = 'az'; //the idea is string of 2 - true
$this->needs['CAPS'] = 'AZ'; //the idea is string af 2 - true
$this->needs['digits'] = true; //plain boolean
$this->needs['specials'] = true; //plain boolean
$this->needs['space'] = false; //plain boolean
$this->needs['exclude'] = ""; //exclude string
$this->build_from_needs();
}
function set_a_need($ch_sort, $need) {
//plain boolean
if (gettype($need) === 'boolean') {
$this->needs[$ch_sort] = $need;
return;
}
if ($ch_sort === 'exclude') {
$this->needs[$ch_sort] = $need; //string
return;
}
//chr
if (in_array($ch_sort, ['littles', 'CAPS'])) {
if ($need && strlen($need) != 2) throw new Exception("Incorrect character range string.");
$this->needs[$ch_sort] = $need; //2 char string or false
}
}
function get_a_need($ch_sort) {
return $this->needs[$ch_sort];
}
function check_needs_empty() {
foreach ($this->needs as $ch_sort=>$need) {
if (!($ch_sort === 'exclude' || $ch_sort === 'space') && $need) return true;
}
return false; //only exclude or/and space
}
function show_needs() {
return $this->needs;
}
function build_from_needs() {
$this->reset_charsetstr();
if ($this->get_a_need('space')) $this->get_space(); //like in preview in chset form
if ($need_littles = $this->get_a_need('littles')) $this->get_ordinary($need_littles[0], $need_littles[-1]);
if ($need_CAPS = $this->get_a_need('CAPS')) $this->get_ordinary($need_CAPS[0], $need_CAPS[-1]);
if ($this->get_a_need('digits')) $this->get_digits();
if ($this->get_a_need('specials')) $this->get_specials();
$need_exclude = $this->get_a_need('exclude');
if (strlen($need_exclude)) {
$this->set_excluded($need_exclude);
$this->exclude_them();
}
if (!$this->check_charsetstr_ok()) {
$this->CHSETSTR = $this->ASCII_PRINT95;
throw new Exception("Bad character number (0/1)!");
}
}
function check_charsetstr_ok() {
if (strlen($this->CHSETSTR) <=1) return false;
return true;
}
function shuffle_charset() {
if (!$this->check_charsetstr_ok()) throw new Exception("bad or missing charset when trying to shuffle");
$chset_arr = str_split($this->CHSETSTR);
//shuffle($chset_arr); //standart php func -not crypto secure
$this->FYshuffle($chset_arr); //my func - crypto secure (see)
$this->CHSETSTR = implode($chset_arr);
}
function FYshuffle(&$items) {
//thanks to Andre Laszlo on SOF
//Fisher-Yates algorithm
//crypto secure because of random_int(), which is cs
$items = array_values($items);
for ($i = count($items) - 1; $i > 0; $i--) {
$j = random_int(0, $i);
$tmp = $items[$i];
$items[$i] = $items[$j];
$items[$j] = $tmp;
}
}
//older part...
function get_ordinary($start, $end) {
if (in_array($start, $this->chars['digits']) and !in_array($end, $this->chars['digits'])) throw new Exception("Wrong character");
if (in_array($start, $this->chars['littles']) and !in_array($end, $this->chars['littles'])) throw new Exception("Wrong character");
if (in_array($start, $this->chars['CAPS']) and !in_array($end, $this->chars['CAPS'])) throw new Exception("Wrong character");
if (ord($end) - ord($start) < 0) throw new Exception("Wrong character order!");
$candCHSETSTR = $this->CHSETSTR . implode(range($start, $end));
if (!$this->check_duplicate($candCHSETSTR)) $this->CHSETSTR = $candCHSETSTR;
else throw new Exception("chsbuilder duplitates in get_ordinary!");
return range($start, $end); //return string is deprecated, it's array
//bellow the same
}
function get_littles() {return $this->get_ordinary('a', 'z');} //only for combgenerator
function get_CAPS() {return $this->get_ordinary('A', 'Z');} //only for combgenerator
function get_specials() {
$candCHSETSTR = $this->CHSETSTR . implode($this->chars['specials']);
if (!$this->check_duplicate($candCHSETSTR)) $this->CHSETSTR = $candCHSETSTR;
else throw new Exception("chsbuilder duplicates in get_specials!");
return $this->chars['specials'];
}
function get_digits() {
$candCHSETSTR = $this->CHSETSTR . implode($this->chars['digits']);
if (!$this->check_duplicate($candCHSETSTR)) $this->CHSETSTR = $candCHSETSTR;
else throw new Exception("chsbuilder duplicates in get_digits!");
return $this->chars['digits'];
}
function get_space() {
$candCHSETSTR = $this->CHSETSTR . " ";
if (!$this->check_duplicate($candCHSETSTR)) $this->CHSETSTR = $candCHSETSTR;
else throw new Exception("chsbuilder duplicates in get_space!");
return str_split(" ");
}
function set_excluded($exclstr, $conforming=false) {
if ($this->check_duplicate($exclstr)) {
$this->needs['exclude'] = "";
throw new Exception("Duplicate characters exist for exclision in chsbuilder!");
}
if (!$conforming && $this->check_exclude_toomuch($exclstr)) {
$this->needs['exclude'] = "";
throw new Exception("Cannot put in excluded whole character class! Use requirement check boxes instead.");
}
//have to be revised the whole idea - conform_chset_toreqs uses set_excluded and excl whole classes
$this->chars['excluded'] = $exclstr;
}
function exclude_them() {
foreach (str_split($this->chars['excluded']) as $exchar) {
$this->CHSETSTR = str_replace($exchar, "", $this->CHSETSTR);
}
}
//analogue of check_duplicate
function check_exclude_toomuch($excl_str) {
//malko se pvtr amashte vidq
$excl_array = str_split($excl_str);
sort($excl_array);
$excl_chsorts = array_flip(array('littles', 'CAPS', 'digits', 'specials', 'space'));
foreach ($excl_chsorts as $k=>$val) $excl_chsorts[$k] = [];
//here improvment - (assoc ar and cycle)->
foreach ($excl_array as $exclchar) {
//sets the excl sets by chsorts
foreach ($this->chars as $k=>$chsort) {
if ($k == 'excluded') continue;
if (in_array($exclchar, $chsort)) array_push($excl_chsorts[$k], $exclchar);
}
}
//then - excl_chsorts, to string , compare with chars['...'] (may be can be esear?)
foreach ($excl_chsorts as $k=>$excl_chsort) {
sort($excl_chsort);
if ($excl_chsort === $this->chars[$k]) return true; //exclude too much - yes
}
return false; //not too much - ok
}
//function set_order() //not now
function check_duplicate($candidate_str) { //quite important, but only check and true/false
$cand_arr = str_split($candidate_str);
if (count($cand_arr) > count(array_unique($cand_arr))) return true; //duplacates - yes
return false; //no duplicates - ok
}
//givers: have to give arrays for dropdowns usage, etc.):
function give_littles() {
return $this->chars['littles'];
}
function give_CAPS() {
return $this->chars['CAPS'];
}
function give_specials() {
return $this->chars['specials'];
}
function give_digits() {
return $this->chars['digits'];
}
function give_space() {
return str_split(" "); //" ";
}
//\"givers" are necessary for other things too - - impose of requirements
function get_charsetstr() {
if (strlen($this->CHSETSTR) === 0) $this->CHSETSTR = $this->ASCII_PRINT95; //the idea is when we work only with reqs
//i.e. without get... etc. (get ALL)
return $this->CHSETSTR;
}
function sniff_charsetstr() {return $this->CHSETSTR;} //for debug purpuses, showing, etc...
function reset_charsetstr() { //kind of start anew
$this->CHSETSTR = "";
}
//1.1
function give_excluded() {return $this->chars['excluded'];} //not obligitory really excl (to show ...)
function build_for_id_produce() { //these is what producing need firmly
$this->reset_charsetstr(); //start from cleaan state
//build...
$this->set_a_need('space', false); //set the needs fo form_chset after that
$this->set_a_need('littles', 'az');
$this->set_a_need('CAPS', 'AZ');
$this->set_a_need('digits', true);
$this->set_a_need('specials', false);
$this->set_a_need('exclude', "hijlnoIJOQ01"); //hard
$this->build_from_needs(); //building...
$this->produce_case = true; //(!)
} //build_for_...
function is_produce_case() {
if ($this->produce_case) return true;
else return false;
}
function not_a_produce_case($no_produce=true) {
if ($no_produce) $this->produce_case = false;
else $this->produce_case = true;
}
//desperate dynamic...
function set_needs_from_string($userstring) {
//build needs
$userarray = str_split($userstring);
sort($userarray);
$user_chsorts = array_flip(array('littles', 'CAPS', 'digits', 'specials', 'space', 'exclude'));
foreach ($user_chsorts as $k=>$val) $user_chsorts[$k] = [];
//here improvment - (assoc ar and cycle)->
foreach ($userarray as $uschar) {
//sets the user sets by chsorts
foreach ($this->chars as $k=>$chsort) {
if ($k == 'excluded') continue;
if (in_array($uschar, $chsort)) array_push($user_chsorts[$k], $uschar);
}
}
$this->needs["exclude"] = "";
if ($user_chsorts["littles"]) {
$this->needs["littles"] = reset($user_chsorts['littles']) . end($user_chsorts['littles']);
//diff between start-end & the real chars
$this->needs["exclude"] .= implode(array_diff(range(reset($user_chsorts['littles']), end($user_chsorts['littles'])), $user_chsorts["littles"]));
}
else $this->needs["littles"] = '';
if ($user_chsorts["CAPS"]) {
$this->needs["CAPS"] = reset($user_chsorts['CAPS']) . end($user_chsorts['CAPS']);
$this->needs["exclude"] .= implode(array_diff(range(reset($user_chsorts['CAPS']), end($user_chsorts['CAPS'])), $user_chsorts["CAPS"]));
}
else $this->needs["CAPS"] = '';
if ($user_chsorts["digits"]) {
$this->needs["digits"] = true;
//diff between all & the real chars
$this->needs["exclude"] .= implode(array_diff($this->chars['digits'], $user_chsorts["digits"]));
}
else $this->needs["digits"] = false;
if ($user_chsorts["specials"]) {
$this->needs["specials"] = true;
$this->needs["exclude"] .= implode(array_diff($this->chars['specials'], $user_chsorts["specials"]));
}
else $this->needs["specials"] = false;
if ($user_chsorts["space"]) //no exclude here
$this->needs["space"] = true;
else $this->needs["space"] = false;
$this->CHSETSTR = $userstring; //shuffle analogy
} //set_needs_from_string
//dd
//\1.1
} //CHSBuilder
?>