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

namespace qfq;

11
require_once(__DIR__ . '/../Constants.php');
Carsten  Rose's avatar
Carsten Rose committed
12
require_once(__DIR__ . '/Sanitize.php');
13
14
15

class Support {

16
17
    /**
     * @param array $queryArray Empty or prefilled assoc array with url parameter
18
     * @param string $mode PARAM_T3_NO_ID, PARAM_T3_ALL
19
     */
20
    public static function appendTypo3ParameterToArray(array &$queryArray, $mode = PARAM_T3_ALL) {
21
22
//        if (isset($_GET['id']))
//            $queryArray['id'] = self::getCurrentPage();
23

24
        $store = Store::getInstance();
25

26
27
28
29
30
31
32
33
34
        if ($mode === PARAM_T3_ALL) {
            $queryArray['id'] = $store->getVar(TYPO3_PAGE_ID, STORE_TYPO3);
        }

        // TYPE
        $tmp = $store->getVar(TYPO3_PAGE_TYPE, STORE_TYPO3);
        if ($tmp !== false && $tmp != 0) {
            $queryArray['type'] = $tmp;
        }
35

36
37
38
39
40
        // Language
        $tmp = $store->getVar(TYPO3_PAGE_LANGUAGE, STORE_TYPO3);
        if ($tmp !== false && $tmp != 0) {
            $queryArray['L'] = $tmp;
        }
41
42
    }

43
    /**
44
45
     * Builds a urlencoded query string of an assoc array.
     *
46
     * @param array $queryArray
47
     * @return string Querystring (e.g.: id=23&type=99
48
49
50
51
52
53
54
55
56
57
58
59
60
61
     */
    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);
    }
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

    /**
     * Extract Tag from $tag (might contain further attributes) and wrap it around $value. If $flagOmitEmpty==true && $value=='': return ''.
     *
     * @param $tag
     * @param $value
     * @param bool|false $omitIfValueEmpty
     * @return string
     */
    public static function wrapTag($tag, $value, $omitIfValueEmpty = false) {
        if ($omitIfValueEmpty && $value === "")
            return '';

        // 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;
    }

84
85
86
87
88
    /**
     * Format's an attribute: $type=$value. If $flagOmitEmpty==true && $value=='': return ''.
     *
     * @param $type
     * @param $value
Carsten  Rose's avatar
Carsten Rose committed
89
     * @param bool $flagOmitEmpty
90
91
92
93
94
95
96
97
98
     * @return string
     */
    public static function doAttribute($type, $value, $flagOmitEmpty = true) {
        if ($flagOmitEmpty && $value === "")
            return '';

        return $type . '="' . trim($value) . '" ';
    }

99
100
    /**
     * Wraps some $inner fragment with a CSS styled $tooltipText . CSS is configured in 'Resources/Public/qfq-jqw.css'.
101
     *
102
     * Based on: http://www.w3schools.com/howto/howto_css_tooltip.asp
103
     *
104
     * @param $before
105
106
107
     * @param $tooltipText
     * @return string
     */
108
    public static function appendTooltip($before, $tooltipText) {
109

110
111
112
113
        // addslashes, htmlentities & htmlspecialchars
        // str_replace('"', '&quot;', $tooltipText)

        return $before . "<img src='" . GFX_INFO . "' title=\"" . htmlentities($tooltipText) . "\">";
114
115
    }

116
    /**
117
118
119
120
121
122
123
     * Search for the parameter $needle in $haystack. The argumenst has to be seperated by ','.
     *
     * Returns the false if not found or index of found place. Be carefull: use unary operator to compare for 'false'
     *
     * @param $needle
     * @param $haystack
     * @return boolean     true if found, else false
124
     */
125
126
127
128
    public static function findInSet($needle, $haystack) {
        $arr = explode(',', $haystack);

        return array_search($needle, $arr) !== false;
129

130
    }
131

132
133
134
135
136
137
138
139
    /**
     * Converts a dateTime String to the english format:
     * 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
     *
     * @param $dateTimeString
     * @return string
140
     * @throws UserFormException
141
142
143
144
145
     */
    public static function dateTime2mysql($dateTimeString) {
        $dateRaw = '';
        $timeRaw = '';

146
147
148
149
150

//        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})?$';

151
152
153
154
155
156
        $tmpArr = explode(' ', $dateTimeString);
        switch (count($tmpArr)) {
            case 0:
                return '';

            case 1:
157
                if (strpos($tmpArr[0], ':') === false) {
158
                    $dateRaw = $tmpArr[0];
159
160
                } else {
                    $timeRaw = $tmpArr[0];
161
162
163
164
165
166
167
168
169
                }
                break;

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

            default:
170
                throw new UserFormException('Date/time format not recognised.', ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
171
172
173
174
175
176
                break;
        }

        if ($dateRaw === '') {
            $date = '0000-00-00';
        } else {
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
            // International format: YYYY-MM-DD
            if (preg_match("/" . REGEXP_DATE_INT . "/", $dateRaw) === 1) {
                $date = $dateRaw;

                // German format: 1.1.01 - 11.12.1234
            } elseif (preg_match("/" . REGEXP_DATE_GER . "/", $dateRaw) === 1) {
                $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 {
                throw new UserFormException('Date/time format not recognised.', ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
194
195
196
197
198
199
            }
        }

        if ($timeRaw === '') {
            $time = '00:00:00';
        } else {
200
201
202
203
            if (preg_match("/" . REGEXP_TIME . "/", $timeRaw) !== 1) {
                throw new UserFormException('Date/time format not recognised.', ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
            }

204
205
206
207
208
209
210
211
212
            $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:
213
                    throw new UserFormException('Date/time format not recognised.', ERROR_DATE_TIME_FORMAT_NOT_RECOGNISED);
214
215
216
217
218
219
                    break;
            }
        }

        return $date . ' ' . $time;
    }
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

    /**
     * Encrypt curly braces by an uncommon string. Helps preventing unwished action on curly braces.
     *
     * @param $text
     * @return mixed
     */
    public static function encryptDoubleCurlyBraces($text) {
        $text = str_replace('{{', '#&@[[@_#', $text);
        $text = str_replace('}}', '#&@]]@_#', $text);

        return $text;
    }

    /**
     * Decrypt curly braces by an uncommon string. Helps preventing unwished action on curly braces
     *
     * @param $text
     * @return mixed
     */
    public static function decryptDoubleCurlyBraces($text) {
        $text = str_replace('#&@[[@_#', '{{', $text);
        $text = str_replace('#&@]]@_#', '}}', $text);

        return $text;
    }

247
248
249
250
251
252
253
254
255
256
257
258
259
260
    /**
     * @param int $length Length of the required hash string
     * @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);
    }

261
262
263
264
265
266
267
268
    /**
     * @param $url
     * @param $param
     * @return string
     */
    public static function concatUrlParam($url, $param) {
        if ($param == '')
            return $url;
269

270
271
272
        $token = (strpos($url, '?') === false) ? '?' : '&';
        return $url . $token . $param;
    }
273
274
275
276
277
278
279
280
281
282
283

    /**
     * @param $arr
     * @param $index
     * @param string $value
     */
    public static function setIfNotSet(&$arr, $index, $value = '') {

        if (!isset($arr[$index]))
            $arr[$index] = $value;
    }
284
}