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

namespace qfq;

require_once(__DIR__ . '/Sanatize.php');

class Support {

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    /**
     * @param array $queryArray Empty or prefilled assoc array with url parameter
     */
    public static function appendTypo3ParameterToArray(array &$queryArray) {
        if (isset($_GET['id']))
            $queryArray['id'] = self::getCurrentPage();

        if (isset($_GET['L']))
            $queryArray['L'] = $_GET['L'];

        if (isset($_GET['type']))
            $queryArray['type'] = $_GET['type'];

    }

30
31
    /**
     * @return string If exist content of $_GET[], otherwise the name of the first GET Parameter
32
     * @throws CodeException
33
34
35
36
37
38
39
40
41
42
43
44
     */
    public static function getCurrentPage() {
        if (isset($_GET['id'])) {
            $page = $_GET['id'];
        } else {
            $page = explode('&', $_SERVER['QUERY_STRING'])[0];
            $page = explode('=', $page)[0];
        }
        return Sanatize::sanatize($page, SANATIZE_ALLOW_ALNUMX);
    }

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

    /**
     * @throws CodeException
     */
    public static function log($text) {

        if (!is_writable(QFQ_LOG)) {
            throw new CodeException ("The file (" . dirname($_SERVER['SCRIPT_FILENAME']) . '/) ' . QFQ_LOG . " is not writable ()", ERROR_LOG_NOT_WRITABLE);
        }

        if (!$handle = fopen(QFQ_LOG, 'a')) {
            throw new CodeException ("Cannot open file to apped: " . QFQ_LOG, ERROR_LOG_NOT_WRITABLE);
        }

        if (fwrite($handle, $text . chr(10)) === FALSE) {
            throw new CodeException ("Cannot write to file: " . QFQ_LOG, ERROR_LOG_NOT_WRITABLE);
        }
        fclose($handle);
    }
82
}