Support.php 50.5 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/28/16
 * Time: 8:05 AM
 */

Marc Egger's avatar
Marc Egger committed
9
namespace IMATHUZH\Qfq\Core\Helper;
10

11

Marc Egger's avatar
Marc Egger committed
12
use IMATHUZH\Qfq\Core\Store\Store;
13

14
15
16
const LONG_CURLY_OPEN = '#/+open+/#';
const LONG_CURLY_CLOSE = '#/+close+/#';

17
18
19
20
/**
 * Class Support
 * @package qfq
 */
21
22
class Support {

23
24
25
26
27
    /**
     * @var Store
     */
    private static $store = null;

28
    /**
Carsten  Rose's avatar
Carsten Rose committed
29
30
     * @param array $queryArray Empty or prefilled assoc array with url parameter
     * @param string $mode PARAM_T3_NO_ID, PARAM_T3_ALL
Marc Egger's avatar
Marc Egger committed
31
32
33
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
34
     */
35
    public static function appendTypo3ParameterToArray(array &$queryArray, $mode = PARAM_T3_ALL) {
36

37
38
39
        if (!isset(self::$store)) {
            self::$store = Store::getInstance();
        }
40

41
        if ($mode === PARAM_T3_ALL) {
42
            $queryArray['id'] = self::$store->getVar(TYPO3_PAGE_ID, STORE_TYPO3);
43
44
45
        }

        // TYPE
46
        $tmp = self::$store->getVar(TYPO3_PAGE_TYPE, STORE_TYPO3);
47
48
49
        if ($tmp !== false && $tmp != 0) {
            $queryArray['type'] = $tmp;
        }
50

51
        // Language
52
        $tmp = self::$store->getVar(TYPO3_PAGE_LANGUAGE, STORE_TYPO3);
53
54
55
        if ($tmp !== false && $tmp != 0) {
            $queryArray['L'] = $tmp;
        }
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
    /**
     * Check $uri if 'type' and/or 'L' is missing. If yes, append it.
     *
     * Do nothing if $uri is empty.
     * Do nothing if $uri is absolute (=starts with http)
     *
     * @param $uri
     * @return string
     * @throws \CodeException
     * @throws \UserFormException
     */
    public static function appendTypo3ParameterToUrl($uri) {

        if ($uri == '') {
            return '';
        }

        // Full URL: no processing
        $protocol = explode(':', $uri, 2);
        if ($protocol[0] == 'http' || $protocol[0] == 'https') {
            return $uri;
        }

        $arr = KeyValueStringParser::parse($uri, '=', '&');
        $type = self::$store->getVar(TYPO3_PAGE_TYPE, STORE_TYPO3);
        $language = self::$store->getVar(TYPO3_PAGE_LANGUAGE, STORE_TYPO3);

        if ($type != 0 && $type !== false && !isset($arr['type'])) {
            $uri .= '&type=' . $type;
        }

        if ($language != 0 && $language !== false && !isset($arr['L'])) {
            $uri .= '&L=' . $language;
        }

        return $uri;
    }

96
97
98
99
    /**
     * Build the form log filename. Depending on $formLog=FORM_LOG_ALL one file for all BE_USER, or $formLog=FORM_LOG_SESSION one file per BE_USER.
     *
     * @param $formName
100
     * @param $formLogMode
101
     * @return string
Marc Egger's avatar
Marc Egger committed
102
103
104
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
105
     */
106
    public static function getFormLogFileName($formName, $formLogMode) {
107
108
109
110
111

        if (!isset(self::$store)) {
            self::$store = Store::getInstance();
        }

112
        switch ($formLogMode) {
113
114
115
116
            case FORM_LOG_ALL:
                $perBeSession = '';
                break;
            case FORM_LOG_SESSION:
117
                $perBeSession = self::$store->getVar(TYPO3_BE_USER, STORE_TYPO3) . '.';
118
                if (empty($perBeSession)) {
Marc Egger's avatar
Marc Egger committed
119
                    throw new \UserFormException('formLog: no BE User logged in', ERROR_NO_BE_USER_LOGGED);
120
                }
121
122
                break;
            default:
Marc Egger's avatar
Marc Egger committed
123
                throw new \CodeException('Unknown mode: ' . $formLogMode, ERROR_UNKNOWN_TOKEN);
124
125
        }

126
        $filename = SYSTEM_FILEADMIN_PROTECTED_LOG . '/' . $formName . "." . $perBeSession . "log";
127
128

        return sanitize::safeFilename($filename, false, true);
129
130
    }

131
    /**
132
133
     * Builds a urlencoded query string of an assoc array.
     *
134
     * @param array $queryArray
Carsten  Rose's avatar
Carsten Rose committed
135
     *
136
     * @return string Querystring (e.g.: id=23&type=99
137
138
139
140
141
142
143
144
145
146
147
148
149
150
     */
    public static function arrayToQueryString(array $queryArray) {
        $items = array();

        foreach ($queryArray as $key => $value) {
            if (is_int($key)) {
                $items[] = urlencode($value);
            } else {
                $items[] = $key . '=' . urlencode($value);
            }
        }

        return implode('&', $items);
    }
151
152

    /**
Carsten  Rose's avatar
Carsten Rose committed
153
154
     * Extract Tag(s) from $tag (eg: <div><input class="form-control">, might contain further attributes) and wrap it
     * around
Carsten  Rose's avatar
Carsten Rose committed
155
     * $value. If $flagOmitEmpty==true && $value=='': return ''.
156
     *
Carsten  Rose's avatar
Carsten Rose committed
157
158
     * @param string $tag
     * @param string $value
159
     * @param bool|false $omitIfValueEmpty
Carsten  Rose's avatar
Carsten Rose committed
160
     *
161
162
163
     * @return string
     */
    public static function wrapTag($tag, $value, $omitIfValueEmpty = false) {
164
165
166
167
168
169
170
171
172
173
174
175

        $tag = trim($tag);

        if ($tag == '' || ($omitIfValueEmpty && $value == "")) {
            return $value;
        }

        $tagArray = explode('>', $tag, 2);
        if (count($tagArray) > 1 && $tagArray[1] != '') {
            $value = self::wrapTag($tagArray[1], $value, $omitIfValueEmpty);
            $tag = $tagArray[0] . '>';
        }
176
177
178
179
180
181
182
183
184
185

        // a) <div class="container-fluid">, b) <label>
        $arr = explode(' ', $tag);

        $tagPlain = (count($arr) === 1) ? substr($arr[0], 1, strlen($arr[0]) - 2) : substr($arr[0], 1);
        $closing = '</' . $tagPlain . '>';

        return $tag . $value . $closing;
    }

186
187
188
189
190
    /**
     * @param $glyphIcon
     * @param string $value
     * @return string
     */
191
192
193
194
    public static function renderGlyphIcon($glyphIcon, $value = '') {
        return Support::wrapTag("<span class='" . GLYPH_ICON . " $glyphIcon'>", $value);
    }

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
    /**
     * Removes '$tag' and closing $tag from $value, if they are the outermost.
     * E.g.  unWrapTag('<p>', '<p>Hello World</p>')    returns  'Hello World'
     *
     * @param string $tag
     * @param string $value
     *
     * @return string
     */
    public static function unWrapTag($tag, $value) {

        if ($tag == '' || $value == '') {
            return $value;
        }

        $lenTag = strlen($tag);
        $lenValue = strlen($value);

        if ($lenValue < $lenTag + $lenTag + 1) {
            return $value;
        }

        $closeTag = $tag[0] . '/' . substr($tag, 1);

        if (substr($value, 0, $lenTag) == $tag && substr($value, $lenValue - $lenTag - 1) == $closeTag) {
            $value = substr($value, $lenTag, $lenValue - $lenTag - $lenTag - 1);
        }

        return $value;
    }

226
227
228
229
230
    /**
     * Wraps some $inner fragment with a CSS styled $tooltipText . CSS is configured in 'Resources/Public/qfq-jqw.css'.
     *
     * Based on: http://www.w3schools.com/howto/howto_css_tooltip.asp
     *
231
     * @param string $htmlId
232
     * @param string $tooltipText
Carsten  Rose's avatar
Carsten Rose committed
233
     *
234
     * @return string
Marc Egger's avatar
Marc Egger committed
235
     * @throws \CodeException
236
237
238
239
240
241
     */
    public static function doTooltip($htmlId, $tooltipText) {

        return "<img " . self::doAttribute('id', $htmlId) . " src='" . GFX_INFO . "' title=\"" . htmlentities($tooltipText) . "\">";
    }

242
243
    /**
     * Format's an attribute: $type=$value. If $flagOmitEmpty==true && $value=='': return ''.
244
     * Escape double tick - assumes that attributes will always be enclosed by double ticks.
245
     * Add's a space at the end.
246
     *
Carsten  Rose's avatar
Carsten Rose committed
247
     * @param string $type
248
     * @param string|array $value
249
250
     * @param bool $flagOmitEmpty true|false
     * @param string $modeEscape ESCAPE_WITH_BACKSLASH | ESCAPE_WITH_HTML_QUOTE
Carsten  Rose's avatar
Carsten Rose committed
251
     *
252
     * @return string correctly formatted attribute. Space at the end.
Marc Egger's avatar
Marc Egger committed
253
     * @throws \CodeException
254
     */
255
    public static function doAttribute($type, $value, $flagOmitEmpty = true, $modeEscape = ESCAPE_WITH_HTML_QUOTE) {
256
257
258
259
260
261

        // several attributes might be given as an array - concat to a string
        if (is_array($value)) {
            $value = implode(' ', $value);
        }

262
        if ($flagOmitEmpty && trim($value) === "") {
263
            return '';
264
265
        }

266
        switch (strtolower($type)) {
267
268
            case 'size':
            case 'maxlength':
269
                // empty or '0' for attributes of type 'size' or 'maxlength' result in unsuable input elements: skip this.
270
271
272
273
                if ($value === '' || $value == 0) {
                    return '';
                }
                break;
274
275
276
277
278
            // Bad idea to do urlencode on this place: it will convert ?, &, ... which are necessary for a proper URL.
            // Instead the value of a parameter needs to encode. Unfortunately, it's too late on this place.
//            case 'href':
//                $value = urlencode($value);
//                break;
279
280
281
            default:
                break;
        }
282

283
284
        $value = self::escapeDoubleTick(trim($value), $modeEscape);

285
286
287
288
289
        return $type . '="' . $value . '" ';
    }

    /**
     * Escapes Double Ticks ("), which are not already escaped.
290
291
292
     * modeEscape: ESCAPE_WITH_BACKSLASH | ESCAPE_WITH_HTML_QUOTE
     *
     * TinyMCE: Encoding JS Attributes (keys & values) for TinyMCE needs to be encapsulated in '&quot;' instead of '\"'.
293
     *
Carsten  Rose's avatar
Carsten Rose committed
294
     * @param        $str
295
     * @param string $modeEscape
Carsten  Rose's avatar
Carsten Rose committed
296
     *
297
     * @return string
Marc Egger's avatar
Marc Egger committed
298
     * @throws \CodeException
299
     */
300
    public static function escapeDoubleTick($str, $modeEscape = ESCAPE_WITH_BACKSLASH) {
301
302
303
304
305
        $newStr = '';

        for ($ii = 0; $ii < strlen($str); $ii++) {
            if ($str[$ii] === '"') {
                if ($ii === 0 || $str[$ii - 1] != '\\') {
306
307
308
309
310
311
312
313
                    switch ($modeEscape) {
                        case ESCAPE_WITH_BACKSLASH:
                            $newStr .= '\\';
                            break;
                        case ESCAPE_WITH_HTML_QUOTE:
                            $newStr .= '&quot;';
                            continue 2;
                        default:
Marc Egger's avatar
Marc Egger committed
314
                            throw new \CodeException('Unknown modeEscape=' . $modeEscape, ERROR_UNKNOWN_ESCAPE_MODE);
315
                    }
316
317
318
319
320
321
                }
            }
            $newStr .= $str[$ii];
        }

        return $newStr;
322
323
    }

324
325
326
327
    /**
     * Format's an attribute and inserts them at the beginning of the $htmlTag:
     * If $flagOmitEmpty==true && $value=='': insert nothing
     *
Carsten  Rose's avatar
Carsten Rose committed
328
329
     * @param string $htmlTag with open and closing angle.
     * @param string $type
330
     * @param string|array $value
Carsten  Rose's avatar
Carsten Rose committed
331
332
     * @param bool $flagOmitEmpty
     * @param string $modeEscape
Carsten  Rose's avatar
Carsten Rose committed
333
     *
334
     * @return string correctly fomratted attribute. Space at the end.
Marc Egger's avatar
Marc Egger committed
335
     * @throws \CodeException
336
337
338
339
340
341
342
343
     */
    public static function insertAttribute($htmlTag, $type, $value, $flagOmitEmpty = true, $modeEscape = ESCAPE_WITH_BACKSLASH) {
        $htmlTag = trim($htmlTag);

        // e.g. '<div class=...' will be exploded to '<div' and 'class=...'
        $parts = explode(' ', $htmlTag, 2);
        if (count($parts) < 2) {
            if (strlen($htmlTag) < 3) {
Marc Egger's avatar
Marc Egger committed
344
                throw new \CodeException('HTML Token too short (<3 chars):' . $htmlTag, ERROR_HTML_TOKEN_TOO_SHORT);
345
346
347
348
349
350
351
352
353
354
355
            } else {
                $parts[0] = substr($htmlTag, 0, -1);
                $parts[1] = '>';
            }
        }

        $attr = self::doAttribute($type, $value, $flagOmitEmpty, $modeEscape);

        return $parts[0] . ' ' . $attr . $parts[1];
    }

356
    /**
357
     * Search for the parameter $needle in $haystack. The arguments has to be separated by ','.
358
     *
359
     * Returns false if not found, or index (starting with 0) of found place. Be careful: use unary operator to compare for 'false'
360
     *
Carsten  Rose's avatar
Carsten Rose committed
361
362
     * @param string $needle
     * @param string $haystack
Carsten  Rose's avatar
Carsten Rose committed
363
     *
364
     * @return boolean     true if found, else false
365
     */
366
367
368
369
    public static function findInSet($needle, $haystack) {
        $arr = explode(',', $haystack);

        return array_search($needle, $arr) !== false;
370
    }
371

372
    /**
373
     * Converts a dateTime String to the international format:
374
375
376
377
     * 1.2.79 > 1979-02-01 00:00:00
     * 01.02.13 3:24 >  1979-02-01 03:24:00
     * 1.2.1979 14:21:5 > 1979-02-01 14:21:05
     *
Carsten  Rose's avatar
Carsten Rose committed
378
     * @param string $dateTimeString
Carsten  Rose's avatar
Carsten Rose committed
379
     *
380
     * @return string
Marc Egger's avatar
Marc Egger committed
381
     * @throws \UserFormException
382
     */
383
    public static function dateTimeGermanToInternational($dateTimeString) {
384
385
386
        $dateRaw = '';
        $timeRaw = '';

387
388
389
390
391

//        const REGEXP_DATE_INT_ = '^\d{2,4}-\d{2}-\d{2}$';
//        const REGEXP_DATE_GER = '^\d{1,2}\.\d{1,2}\.\d{2}(\d{2})?$';
//        const REGEXP_TIME = '^\d{1,2}:\d{1,2}(:\d{1,2})?$';

392
393
394
395
396
397
        $tmpArr = explode(' ', $dateTimeString);
        switch (count($tmpArr)) {
            case 0:
                return '';

            case 1:
398
                if (strpos($tmpArr[0], ':') === false) {
399
                    $dateRaw = $tmpArr[0];
400
401
                } else {
                    $timeRaw = $tmpArr[0];
402
403
404
405
406
407
408
409
410
                }
                break;

            case 2:
                $dateRaw = $tmpArr[0];
                $timeRaw = $tmpArr[1];
                break;

            default:
Marc Egger's avatar
Marc Egger committed
411
                throw new \UserFormException('Date/time format not recognised.', ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
412
413
414
                break;
        }

415
        if ($dateRaw === '' || $dateRaw === '0000-00-00' || $dateRaw === '00.00.0000') {
416
417
            $date = '0000-00-00';
        } else {
418
            // International format: YYYY-MM-DD
419
420

            if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $dateRaw) === 1) {
421
422
423
                $date = $dateRaw;

                // German format: 1.1.01 - 11.12.1234
424
            } elseif (preg_match('/^\d{1,2}\.\d{1,2}\.\d{2}(\d{2})?$/', $dateRaw) === 1) {
425
426
427
428
429
430
431
432
433
434
                $tmpArr = explode('.', $dateRaw);

                if ($tmpArr[2] < 70) {
                    $tmpArr[2] = 2000 + $tmpArr[2];
                } elseif ($tmpArr[2] < 100) {
                    $tmpArr[2] = 1900 + $tmpArr[2];
                }
                $date = sprintf("%04d-%02d-%02d", $tmpArr[2], $tmpArr[1], $tmpArr[0]);

            } else {
Marc Egger's avatar
Marc Egger committed
435
                throw new \UserFormException('Date/time format not recognised.', ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
436
437
438
            }
        }

439
        if ($timeRaw === '' || $timeRaw === '00:00:00') {
440
441
            $time = '00:00:00';
        } else {
442
            if (preg_match('/^\d{1,2}:\d{1,2}(:\d{1,2})?$/', $timeRaw) !== 1) {
Marc Egger's avatar
Marc Egger committed
443
                throw new \UserFormException('Date/time format not recognised.', ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
444
445
            }

446
447
448
449
450
451
452
453
454
            $tmpArr = explode(':', $timeRaw);
            switch (count($tmpArr)) {
                case 2:
                    $time = sprintf("%02d:%02d:00", $tmpArr[0], $tmpArr[1]);
                    break;
                case 3:
                    $time = sprintf("%02d:%02d:%02d", $tmpArr[0], $tmpArr[1], $tmpArr[2]);
                    break;
                default:
Marc Egger's avatar
Marc Egger committed
455
                    throw new \UserFormException('Date/time format not recognised.', ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
456
457
458
459
460
461
                    break;
            }
        }

        return $date . ' ' . $time;
    }
462

463
464
465
    /**
     * @param string $type date | datetime | time
     * @param string $format FORMAT_DATE_INTERNATIONAL | FORMAT_DATE_GERMAN
466
     * @param string $timeIsOptional
Carsten  Rose's avatar
Carsten Rose committed
467
     *
468
     * @return string
Marc Egger's avatar
Marc Egger committed
469
     * @throws \UserFormException
470
     */
471
    public static function dateTimeRegexp($type, $format, $timeIsOptional = '0') {
472
473
474

        if ($format === FORMAT_DATE_GERMAN) {
            // yyyy-mm-dd | 0000-00-00
Carsten  Rose's avatar
Carsten Rose committed
475
            $date = '(([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])\.([1-9]|0[1-9]|1[012])\.([0-9]{4}|[0-9]{2})|00\.00\.(00){1,2})';
476
477
        } else {
            // FORMAT_DATE_INTERNATIONAL: [d]d.[m]m.[yy]yy | 00.00.0000
Carsten  Rose's avatar
Carsten Rose committed
478
            $date = '([0-9]{4}-([1-9]|0[1-9]|1[012])-([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])|0000-00-00)';
479
480
481
482
483
484
485
486
487
488
        }

        // hh:mm[:ss]
        $time = '(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){1,2}';

        switch ($type) {
            case 'date':
                $pattern = $date;
                break;
            case 'datetime':
489
490
491
492
                if ($timeIsOptional == '1')
                    $pattern = $date . '( ' . $time . ')?';
                else
                    $pattern = $date . ' ' . $time;
493
494
495
496
497
                break;
            case 'time':
                $pattern = $time;
                break;
            default:
Marc Egger's avatar
Marc Egger committed
498
                throw new \UserFormException("Unknown mode: '$type'", ERROR_UNKNOWN_MODE);
499
500
501
502
503
504
505
506
507
508
509
        }

        return '^' . $pattern . '$';
    }

    /**
     * Parses $dateTimeString. If a date is given, detect international or german format automatically.
     * Convert to $dateFormat. 'yy' and 'd' will be converted to 'yyyy' and 'dd'
     * Returned value will be 'date only', 'datetime' oder 'time only', depending on the input value.
     *
     * @param string $dateTimeString
Carsten  Rose's avatar
Carsten Rose committed
510
     * @param string $dateFormat FORMAT_DATE_INTERNATIONAL | FORMAT_DATE_GERMAN
Carsten  Rose's avatar
Carsten Rose committed
511
512
513
     * @param string $showZero
     * @param string $showTime
     * @param string $showSeconds
Carsten  Rose's avatar
Carsten Rose committed
514
     *
515
     * @return string
Marc Egger's avatar
Marc Egger committed
516
     * @throws \UserFormException
517
518
519
520
521
522
523
     */
    public static function convertDateTime($dateTimeString, $dateFormat, $showZero, $showTime, $showSeconds) {
        $givenDate = '';
        $givenTime = '';
        $newDate = '';
        $newTime = '';
        $delim = '';
Carsten  Rose's avatar
Carsten Rose committed
524
        $flagDateAndTime = false;
525
526
527
528
529
530
531
532
533

        $dateTimeString = trim($dateTimeString);


        switch ($dateTimeString) {
            case '':
            case '0':
            case '0000-00-00 00:00:00':
            case '0000-00-00':
534
535
            case '00.00.0000 00:00:00':
            case '00.00.0000':
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
            case '00:00:00':
                return (self::dateTimeZero($dateFormat, $showZero, $showTime, $showSeconds));
                break;

            default:
                break;
        }

        // Main convert
        $arr = explode(' ', $dateTimeString);
        switch (count($arr)) {
            case 1:
                if (strpos($dateTimeString, ':') === false) {
                    $givenDate = $dateTimeString;
                } else {
                    $givenTime = $dateTimeString;
                }
                break;

            case 2:
                $givenDate = $arr[0];
                $givenTime = $arr[1];
Carsten  Rose's avatar
Carsten Rose committed
558
                $flagDateAndTime = true;
559
560
561
562
563
            default:
        }

        // Date
        if ($givenDate != '') {
Carsten  Rose's avatar
Carsten Rose committed
564
            if ($givenDate == '0') {
565
                $givenDate = '0000-00-00';
Carsten  Rose's avatar
Carsten Rose committed
566
            }
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581

            $arr = self::splitDateToArray($givenDate);

            switch ($dateFormat) {
                case FORMAT_DATE_INTERNATIONAL:
                    $newDate = sprintf("%04d-%02d-%02d", $arr[0], $arr[1], $arr[2]);
                    break;
                case FORMAT_DATE_GERMAN:
                    $newDate = sprintf("%02d.%02d.%04d", $arr[2], $arr[1], $arr[0]);
                default:
            }
        }

        // Time
        if ($givenTime != '') {
Carsten  Rose's avatar
Carsten Rose committed
582
            if ($givenTime == '0') {
583
                $givenTime = '0:0:0';
Carsten  Rose's avatar
Carsten Rose committed
584
            }
585
586
587
588
589
590
591
592
593
594
595
596
597

            $arr = explode(':', $givenTime);
            if (count($arr) < 3) {
                $arr[2] = 0;
            }

            if ($showSeconds == 1) {
                $newTime = sprintf("%02d:%02d:%02d", $arr[0], $arr[1], $arr[2]);
            } else {
                $newTime = sprintf("%02d:%02d", $arr[0], $arr[1]);
            }
        }

Carsten  Rose's avatar
Carsten Rose committed
598
        if ($flagDateAndTime) {
599
600
601
602
603
604
605
606
607
608
            $delim = ' ';
        }

        return $newDate . $delim . $newTime;
    }

    /**
     * Returns a representation of 0 in a choosen variant.
     *
     * @param string $dateFormat FORMAT_DATE_INTERNATIONAL | FORMAT_DATE_GERMAN
609
     * @param string $showZero
Carsten  Rose's avatar
Carsten Rose committed
610
     * @param string $showTime '0' | '1'
611
     * @param string $showSeconds '0' | '1'
Carsten  Rose's avatar
Carsten Rose committed
612
     *
613
614
615
616
     * @return string
     */
    private static function dateTimeZero($dateFormat, $showZero, $showTime, $showSeconds) {

617
        if ($showZero != '1') {
618
            return '';
619
        }
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638

        // $dateFormat (INT/GER),  $showTime, $showSeconds
        $arr[0][0][0] = '0000-00-00';
        $arr[0][0][1] = '0000-00-00';
        $arr[0][1][0] = '0000-00-00 00:00';
        $arr[0][1][1] = '0000-00-00 00:00:00';
        $arr[1][0][0] = '00.00.0000';
        $arr[1][0][1] = '00.00.0000';
        $arr[1][1][0] = '00.00.0000 00:00';
        $arr[1][1][1] = '00.00.0000 00:00:00';

        $showFormat = ($dateFormat === FORMAT_DATE_INTERNATIONAL) ? 0 : 1;

        return $arr[$showFormat][$showTime][$showSeconds];
    }

    /**
     * Split date FORMAT_DATE_GERMAN | FORMAT_DATE_INTERNATIONAL to array with arr[0]=yyyy, arr[1]=mm, arr[2]=dd.
     *
Carsten  Rose's avatar
Carsten Rose committed
639
     * @param string $dateString
Carsten  Rose's avatar
Carsten Rose committed
640
     *
641
     * @return array
Marc Egger's avatar
Marc Egger committed
642
     * @throws \UserFormException
643
644
645
646
647
648
649
     */
    private static function splitDateToArray($dateString) {

        if (strpos($dateString, '-') === false) {
            // FORMAT_DATE_GERMAN
            $arr = explode('.', $dateString);
            if (count($arr) != 3) {
Marc Egger's avatar
Marc Egger committed
650
                throw new \UserFormException("Unexpected format for date: $dateString", ERROR_DATE_UNEXPECTED_FORMAT);
651
652
653
654
655
656
657
658
            }
            $tmp = $arr[0];
            $arr[0] = $arr[2];
            $arr[2] = $tmp;
        } else {
            // FORMAT_DATE_INTERNATIONAL
            $arr = explode('-', $dateString);
            if (count($arr) != 3) {
Marc Egger's avatar
Marc Egger committed
659
                throw new \UserFormException("Unexpected format for date: $dateString", ERROR_DATE_UNEXPECTED_FORMAT);
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
            }
        }

        // Adjust yy to yyyy. See https://dev.mysql.com/doc/refman/5.7/en/datetime.html > Dates containing two-digit year values ...
        if ($arr[0] < 100) {
            $add = ($arr[0] < 70) ? 2000 : 1900;
            $arr[0] = $add + $arr[0];
        }

        return $arr;
    }

    /**
     * Calculates the placeholder for date/dateTime/local input fields.
     *
     * @param array $formElement
Carsten  Rose's avatar
Carsten Rose committed
676
     *
677
     * @return string
Marc Egger's avatar
Marc Egger committed
678
     * @throws \UserFormException
679
680
681
682
     */
    public static function getDateTimePlaceholder(array $formElement) {

        $timePattern = ($formElement[FE_SHOW_SECONDS] == 1) ? 'hh:mm:ss' : 'hh:mm';
683
        switch ($formElement[FE_TYPE]) {
684
            case 'date':
685
                $placeholder = $formElement[FE_DATE_FORMAT];
686
687
                break;
            case 'datetime':
688
                $placeholder = $formElement[FE_DATE_FORMAT] . ' ' . $timePattern;
689
690
691
692
693
                break;
            case 'time':
                $placeholder = $timePattern;
                break;
            default:
Marc Egger's avatar
Marc Egger committed
694
                throw new \UserFormException("Unexpected Formelement type: '" . $formElement[FE_TYPE] . "'", ERROR_FORMELEMENT_TYPE);
695
696
697
698
699
700
        }

        return $placeholder;
    }


701
702
703
    /**
     * Encrypt curly braces by an uncommon string. Helps preventing unwished action on curly braces.
     *
Carsten  Rose's avatar
Carsten Rose committed
704
     * @param string $text
Carsten  Rose's avatar
Carsten Rose committed
705
     *
706
707
     * @return mixed
     */
708
    public static function encryptDoubleCurlyBraces($text) {
709
710
        $text = str_replace('{{', LONG_CURLY_OPEN, $text);
        $text = str_replace('}}', LONG_CURLY_CLOSE, $text);
711
712
713
714
715
716
717

        return $text;
    }

    /**
     * Decrypt curly braces by an uncommon string. Helps preventing unwished action on curly braces
     *
Carsten  Rose's avatar
Carsten Rose committed
718
     * @param string $text
Carsten  Rose's avatar
Carsten Rose committed
719
     *
720
721
722
     * @return mixed
     */
    public static function decryptDoubleCurlyBraces($text) {
723
724
725
726
727

        if (!is_string($text)) {
            return $text;
        }

728
729
        $text = str_replace(LONG_CURLY_OPEN, '{{', $text);
        $text = str_replace(LONG_CURLY_CLOSE, '}}', $text);
730
731
732
733

        return $text;
    }

734
735
    /**
     * @param int $length Length of the required hash string
Carsten  Rose's avatar
Carsten Rose committed
736
     *
737
738
739
740
741
742
743
744
745
746
747
748
     * @return string       A random alphanumeric hash
     */
    public static function randomAlphaNum($length) {
        $possible_characters = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $string = "";
        while (strlen($string) < $length) {
            $string .= substr($possible_characters, rand() % (strlen($possible_characters)), 1);
        }

        return ($string);
    }

749
    /**
Carsten  Rose's avatar
Carsten Rose committed
750
751
     * Concatenate URL and Parameter. Depending of if there is a '?' in URL or not,  append the param with '?' or '&'..
     *
Carsten  Rose's avatar
Carsten Rose committed
752
     * @param string $url
753
     * @param string|array $param
Carsten  Rose's avatar
Carsten Rose committed
754
     *
755
756
757
     * @return string
     */
    public static function concatUrlParam($url, $param) {
758
759
760
761
762

        if (is_array($param)) {
            $param = implode('&', $param);
        }

763
        if ($param == '') {
764
            return $url;
765
        }
766

767
        $token = ((strpos($url, '?') === false) && (strpos($url, '&')) === false) ? '?' : '&';
Carsten  Rose's avatar
Carsten Rose committed
768

769
770
        return $url . $token . $param;
    }
771

772
773
774
775
776
777
778
779
780
781
    /**
     * Concatenate $host, $path and $query. A given $host will always append a '/' if none is given.
     * End of  $path, there is no assumption to append '/' - the user needs full control.
     * $query: trailing 'index.php' will be skipped.
     *
     * If HOST and PATH is combined in a var, pass the value as $hostOrPath, leave $host empty.
     *
     * @param string $host
     * @param string $hostOrPath
     * @param string $query
Carsten  Rose's avatar
Carsten Rose committed
782
     *
783
     * @return string
Marc Egger's avatar
Marc Egger committed
784
     * @throws \CodeException
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
     */
    public static function mergeUrlComponents($host, $hostOrPath, $query) {
        $url = '';


        if ($host != '' && substr($host, -1, 1) != '/') {
            $host .= '/';
        }

        if ($host != '' && substr($hostOrPath, 0, 1) == '/') {
            $hostOrPath = substr($hostOrPath, 1);
        }

        if ($host != '' || $hostOrPath != '') {
            $url = $host . $hostOrPath;
        }

        if (substr($query, 0, 9) == 'index.php') {
            $query = substr($query, 9);
        }

        if (false !== strpos(substr($query, 1), '?')) {
Marc Egger's avatar
Marc Egger committed
807
            throw new \CodeException('Found a "?" after the beginning of the query - this is forbidden', ERROR_BROKEN_PARAMETER);
808
809
810
811
812
813
814
815
816
817
        }

        if ($query != '') {
            $url = $url . '?' . $query;
            $url = str_replace('??', '?', $url);
        }

        return $url;
    }

818
819
820
821
    /**
     * Set Defaults for the current formElement.
     *
     * @param array $formElement
822
     * @param array $formSpec
823
     * @return array
824
     *
Marc Egger's avatar
Marc Egger committed
825
826
827
     * @throws \CodeException
     * @throws \UserFormException
     * @throws \UserReportException
828
     */
829
    public static function setFeDefaults(array $formElement, array $formSpec = array()) {
830
831
832
833
834

        $store = Store::getInstance();

        // Some Defaults
        self::setIfNotSet($formElement, FE_SHOW_SECONDS, '0');
835
        self::setIfNotSet($formElement, FE_TIME_IS_OPTIONAL, '0');
836
        self::setIfNotSet($formElement, FE_SHOW_ZERO, '0');
837
        self::setIfNotSet($formElement, FE_HIDE_ZERO, '0');
838
839
        self::setIfNotSet($formElement, FE_DATE_FORMAT, $store->getVar(SYSTEM_DATE_FORMAT, STORE_SYSTEM));

840
841
842
        self::setIfNotSet($formElement, FE_HTML_BEFORE);
        self::setIfNotSet($formElement, FE_HTML_AFTER);

843
        self::setIfNotSet($formElement, FE_DATA_REFERENCE, ($formElement[FE_NAME] == '' ? $formElement[FE_ID] : $formElement[FE_NAME]));
844

Carsten  Rose's avatar
Carsten Rose committed
845
        self::setIfNotSet($formElement, FE_SUBRECORD_TABLE_CLASS, SUBRECORD_TABLE_CLASS_DEFAULT);
846

847
        if (isset($formSpec[F_BS_LABEL_COLUMNS])) {
848
849
850
851
852
853
            self::setIfNotSet($formElement, F_BS_LABEL_COLUMNS, $formSpec[F_BS_LABEL_COLUMNS], '');
            self::setIfNotSet($formElement, F_BS_INPUT_COLUMNS, $formSpec[F_BS_INPUT_COLUMNS], '');
            self::setIfNotSet($formElement, F_BS_NOTE_COLUMNS, $formSpec[F_BS_NOTE_COLUMNS], '');
        }

        if ($formElement[FE_MODE_SQL] != '') {
854
            $formElement[FE_MODE] = $formElement[FE_MODE_SQL];
855
        }
856

857
858
859
860
        if (isset($formSpec[F_MODE])) {
            $formElement[FE_MODE] = self::applyFormModeToFormElement($formElement[FE_MODE], $formSpec[F_MODE]);
        }

861
        // set typeAheadPedantic
862
        if (isset($formElement[FE_TYPEAHEAD_PEDANTIC]) && $formElement[FE_TYPEAHEAD_PEDANTIC] === '') {
863
864
865
866
867
868
            $formElement[FE_TYPEAHEAD_PEDANTIC] = '1'; // support legacy option of 'typeAheadPedantic' without a value
        }
        if (isset($formElement[FE_TYPEAHEAD_LDAP]) || isset($formElement[FE_TYPEAHEAD_SQL])) {
            self::setIfNotSet($formElement, FE_TYPEAHEAD_PEDANTIC, '1');
        }

869
870
        // Will be used to change dynamicUpdate behaviour
        if (isset($formElement[FE_WRAP_ROW_LABEL_INPUT_NOTE])) {
871
872
            $formElement[FE_FLAG_ROW_OPEN_TAG] = self::findInSet('row', $formElement[FE_WRAP_ROW_LABEL_INPUT_NOTE]);
            $formElement[FE_FLAG_ROW_CLOSE_TAG] = self::findInSet('/row', $formElement[FE_WRAP_ROW_LABEL_INPUT_NOTE]);
873
874
875
876
877
        } else {
            $formElement[FE_FLAG_ROW_OPEN_TAG] = true;
            $formElement[FE_FLAG_ROW_CLOSE_TAG] = false;
        }

878
879
        self::setIfNotSet($formElement, FE_INPUT_EXTRA_BUTTON_INFO_CLASS, $store->getVar(FE_INPUT_EXTRA_BUTTON_INFO_CLASS, STORE_SYSTEM));

880
        // For specific FE hard coded 'checkType'
881
882
883
884
        switch ($formElement[FE_TYPE]) {
            case FE_TYPE_IMAGE_CUT:
                $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALLBUT;
                break;
885
886
887
            case FE_TYPE_ANNOTATE:
                $formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
                break;
888
889
890
891
            default:
                break;
        }

892
893
        self::setIfNotSet($formElement, FE_MIN);
        self::setIfNotSet($formElement, FE_MAX);
894
895
896
897
        self::setIfNotSet($formElement, FE_DECIMAL_FORMAT);

        self::setIfNotSet($formElement, F_FE_DATA_PATTERN_ERROR);

898
899
        $typeSpec = $store->getVar($formElement[FE_NAME], STORE_TABLE_COLUMN_TYPES);
        self::adjustFeToColumnDefinition($formElement, $typeSpec);
900
901
902
903
904
905
906
907
908

        return $formElement;
    }

    /**
     * Adjusts several FE parameters using smart guesses based on the table column definition and other parameters.
     *
     * @param array $formElement
     * @param $typeSpec
Marc Egger's avatar
Marc Egger committed
909
     * @throws \UserFormException
910
     */
911
    public static function adjustFeToColumnDefinition(array &$formElement, $typeSpec) {
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935

        self::adjustMaxLength($formElement, $typeSpec);
        self::adjustDecimalFormat($formElement, $typeSpec);

        // Make educated guesses about the desired $min, $max, $checkType, and $inputType

        // $typeSpec = 'tinyint(3) UNSIGNED NOT NULL' | 'int(11) NOT NULL'
        $arr = explode(' ', $typeSpec, 2);
        if (empty($arr[1])) {
            $sign = 'signed';
        } else {
            $arr = explode(' ', $arr[1], 2);
            $sign = $arr[0] == 'unsigned' ? 'unsigned' : 'signed';
        }

        $arr = explode('(', $typeSpec, 2);
        $token = $arr[0];

        # s: signed, u: unsigned.
        # s-min, s-max, s-checktype, u-min, u-max, u-checktype
        $control = [
            'tinyint' => [-128, 127, SANITIZE_ALLOW_NUMERICAL, 0, 255, SANITIZE_ALLOW_DIGIT],
            'smallint' => [-32768, 32767, SANITIZE_ALLOW_NUMERICAL, 0, 65535, SANITIZE_ALLOW_DIGIT],
            'mediumint' => [-8388608, 8388607, SANITIZE_ALLOW_NUMERICAL, 0, 16777215, SANITIZE_ALLOW_DIGIT],
936
            'int' => [-2147483648, 2147483647, SANITIZE_ALLOW_NUMERICAL, 0, 4294967295, SANITIZE_ALLOW_DIGIT],
937
938
939
940
941
            'bigint' => [-9223372036854775808, 9223372036854775807, SANITIZE_ALLOW_NUMERICAL, 0, 18446744073709551615, SANITIZE_ALLOW_DIGIT],
        ];

        $min = '';
        $max = '';
942
        $checkType = SANITIZE_ALLOW_ALNUMX;
943
        $inputType = '';
944
        $isANumber = true;
945

946
947
948
949
950
951
952
953
954
955
956
957
958
        switch ($formElement[FE_TYPE]) {
            case FE_TYPE_PASSWORD:
            case FE_TYPE_NOTE:
                $checkType = SANITIZE_ALLOW_ALL;
                break;

            case FE_TYPE_EDITOR:
            case FE_TYPE_TEXT:
                if ($formElement[FE_ENCODE] === FE_ENCODE_SPECIALCHAR)
                    $checkType = SANITIZE_ALLOW_ALL;
                break;
        }

959
960
961
962
963
964
        switch ($token) {
            case 'tinyint':
            case 'smallint':
            case 'mediumint':
            case 'int':
            case 'bigint':
965
                $inputType = HTML_INPUT_TYPE_NUMBER;
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
                $arr = $control[$token];
                if ($sign == 'signed') {
                    $min = $arr[0];
                    $max = $arr[1];
                    $checkType = $arr[2];
                } else {
                    $min = $arr[3];
                    $max = $arr[4];
                    $checkType = $arr[5];
                }
                break;

            case 'decimal':
            case 'float':
            case 'double':
                $checkType = SANITIZE_ALLOW_NUMERICAL;
                break;

            case 'bit':
985
                $inputType = HTML_INPUT_TYPE_NUMBER;
986
987
                $checkType = SANITIZE_ALLOW_DIGIT;
                break;
988
989
990
991

            default:
                $isANumber = false;
                break;
992
993
        }

994
        // Numbers don't need a maxLength because they are being handled by min/max and/or decimalFormat
995
        if ($isANumber) {
996
            $formElement[FE_MAX_LENGTH] = '';
997
        }
998

999
        if (!empty($formElement[FE_TYPEAHEAD_SQL]) || !empty($formElement[FE_TYPEAHEAD_LDAP])) {
1000
1001
1002
1003
1004
1005
            $inputType = '';
            $checkType = SANITIZE_ALLOW_ALNUMX;
        }

        // Set parameters if not set by user
        if ($formElement[FE_CHECK_TYPE] === SANITIZE_ALLOW_AUTO) {
1006
            $formElement[FE_CHECK_TYPE] = $checkType;
1007
        }
1008

1009
        // If nothing given, set default
1010
        if ($formElement[FE_MIN] == '') {
1011
1012
1013
1014
            $formElement[FE_MIN] = $min;
        }

        // If nothing given, set default
1015
        if ($formElement[FE_MAX] == '') {
1016
1017
1018
            $formElement[FE_MAX] = $max;
        }

1019
1020
1021
        if (empty($formElement[FE_INPUT_TYPE])) {
            $formElement[FE_INPUT_TYPE] = $inputType;
        }
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031

        // If a  $formElement[FE_STEP] is given, the optional boundaries (FE_MIN / FE_MAX) have to be aligned to a multiple of $formElement[FE_STEP].
        if (!empty($formElement[FE_STEP])) {
            if (!empty($formElement[FE_MIN])) {
                $formElement[FE_MIN] = ceil($formElement[FE_MIN] / $formElement[FE_STEP]) * $formElement[FE_STEP];
            }
            if (!empty($formElement[FE_MAX])) {
                $formElement[FE_MAX] = floor($formElement[FE_MAX] / $formElement[FE_STEP]) * $formElement[FE_STEP];
            }
        }
1032

Carsten  Rose's avatar
typo    
Carsten Rose committed
1033
        // If min or max is set and if there is the standard error text given, define a more detailed error text.
1034
        if (($formElement[FE_MIN] != '' || $formElement[FE_MAX] != '') && ($formElement[F_FE_DATA_ERROR] ?? '') == F_FE_DATA_ERROR_DEFAULT) {
1035
            $formElement[F_FE_DATA_ERROR] = F_FE_DATA_ERROR_DEFAULT . ' - allowed values: ' . $formElement[FE_MIN] . '...' . $formElement[FE_MAX];
1036
        }
1037
1038
1039
1040
1041
1042
1043
1044
    }

    /**
     * Sets the decimalFormat of a FormElement based on the parameter definition and table.field definition
     * Affected FormElement fields: FE_DECIMAL_FORMAT
     *
     * @param array $formElement
     * @param $typeSpec
Marc Egger's avatar
Marc Egger committed
1045
     * @throws \UserFormException
1046
     */
1047
    private static function adjustDecimalFormat(array &$formElement, $typeSpec) {
1048

1049
1050
1051
        if (isset($formElement[FE_DECIMAL_FORMAT])) {
            if ($formElement[FE_DECIMAL_FORMAT] === '') {
                // Get decimal format from column definition
1052
1053
                if ($typeSpec !== false) {
                    $fieldTypeInfoArray = preg_split("/[()]/", $typeSpec);
1054
1055
1056
1057
1058
                    if ($fieldTypeInfoArray[0] === 'decimal')
                        $formElement[FE_DECIMAL_FORMAT] = $fieldTypeInfoArray[1];
                }
            } else {
                // Decimal format is defined in parameter field
1059
1060
1061
1062
1063
                $isValidDecimalFormat = preg_match("/^[0-9]+,[0-9]+$/", $formElement[FE_DECIMAL_FORMAT]);
                if ($isValidDecimalFormat) {
                    $decimalFormatArray = explode(',', $formElement[FE_DECIMAL_FORMAT]);
                    $isValidDecimalFormat = $decimalFormatArray[0] >= $decimalFormatArray[1];
                }
1064
                if (!$isValidDecimalFormat) {
Marc Egger's avatar
Marc Egger committed
1065
                    throw new \UserFormException("Invalid decimalFormat: '" . $formElement[FE_DECIMAL_FORMAT] . "'", ERROR_INVALID_DECIMAL_FORMAT);
1066
                }
1067
1068
            }
        }
1069
    }
1070

1071
1072
1073
1074
1075
1076
1077
1078
    /**
     * Calculates the maxLength of an input field, based on formElement type, formElement user definition and
     * table.field definition.
     * Affected formElement fields: FE_MAX_LENGTH
     *
     * @param array $formElement
     * @param $typeSpec
     */
1079
    private static function adjustMaxLength(array &$formElement, $typeSpec) {
1080

1081
1082
1083
1084
1085
1086
1087
1088
1089
        // MIN( $formElement['maxLength'], table definition)
        $maxLength = self::getColumnSize($typeSpec);

        $feMaxLength = false;
        switch ($formElement[FE_TYPE]) {
            case 'date':
                $feMaxLength = 10;
                break;
            case 'datetime':
1090
                $feMaxLength = 19;
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
                break;
            case 'time':
                $feMaxLength = 8;
                break;
        }

        // In case there is no limit of the underlying table column, or a non primary table column, and the FE_TYPE is date/time.
        if ($maxLength === false && $feMaxLength !== false) {
            $maxLength = $feMaxLength;
        }

        // In case the underlying table column is not of type date/time, the $maxLength might be too high: correct
        if ($feMaxLength !== false && $maxLength !== false && $feMaxLength < $maxLength) {
            $maxLength = $feMaxLength;
        }

        // date/datetime
        if ($maxLength !== false) {
1109
            if (isset($formElement[FE_MAX_LENGTH]) && is_numeric($formElement[FE_MAX_LENGTH]) && $formElement[FE_MAX_LENGTH] != 0) {
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
                if ($formElement[FE_MAX_LENGTH] > $maxLength) {
                    $formElement[FE_MAX_LENGTH] = $maxLength;
                }
            } else {
                $formElement[FE_MAX_LENGTH] = $maxLength;
            }
        }
    }

    /**
     * Get column spec from table definition and parse size of it. If nothing defined, return false.
     *
     * @param $typeSpec
     * @return bool|int a) 'false' if there is no length definition, b) length definition, c)
     *                   date|time|datetime|timestamp use hardcoded length
     */
1126
    private static function getColumnSize($typeSpec) {
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154

        $matches = array();

        switch ($typeSpec) {
            case 'date': // yyyy-mm-dd
                return 10;
            case 'datetime': // yyyy-mm-dd hh:mm:ss
            case 'timestamp': // yyyy-mm-dd hh:mm:ss
                return 19;
            case 'time': // hh:mm:ss
                return 8;
            default:
                if (substr($typeSpec, 0, 4) === 'set(' || substr($typeSpec, 0, 5) === 'enum(') {
                    return self::maxLengthSetEnum($typeSpec);
                }
                break;
        }

        // e.g.: string(64) >> 64, enum('yes','no') >> false
        if (1 === preg_match('/\((.+)\)/', $typeSpec, $matches)) {
            if (is_numeric($matches[1]))
                return $matches[1];
        }

        return false;
    }

    /**
1155
1156
     * Get the strlen of the longest element in enum('val1','val2',...,'valn')
     * or the maximum total length of a set('val1','val2',...,'valn')
1157
1158
1159
1160
1161
     *
     * @param string $typeSpec
     *
     * @return int
     */
1162
    private static function maxLengthSetEnum($typeSpec) {
1163
1164
        $isSet = substr($typeSpec, 0, 4) === 'set(';
        $startPos = $isSet ? 4 : 5;
1165
1166
1167
1168
        $max = 0;

        $valueList = substr($typeSpec, $startPos, strlen($typeSpec) - $startPos - 1);
        $valueArr = explode(',', $valueList);
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178

        if ($isSet) { // set
            return strlen(implode(', ', $valueArr));
        } else { // enum
            foreach ($valueArr as $value) {
                $value = trim($value, "'");
                $len = strlen($value);
                if ($len > $max) {
                    $max = $len;
                }
1179
1180
            }

1181
1182
            return $max;
        }
1183
1184
    }

1185
1186
1187
1188
1189
1190
    /**<