Token.php 3.67 KB
Newer Older
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
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 2/10/18
 * Time: 3:14 PM
 */

namespace qfq;

class Token {

    /**
     * Verify Empty values. If appropriate, set defaults, if not throw an exception.
     *
     * @param string $key
     * @param string $value
     *
     * @return string
     * @throws UserReportException
     */
    public static function checkForEmptyValue($key, $value) {

        if ($value !== '') {
            return $value;
        }
        $value = '';

        switch ($key) {
            case TOKEN_URL:
            case TOKEN_MAIL:
            case TOKEN_GLYPH:
                throw new UserReportException ("Missing value for token '$key'", ERROR_MISSING_VALUE);
                break;
            case TOKEN_RIGHT:
                $value = 'r';
                break;
            case TOKEN_ENCRYPTION:
                $value = '1';
                break;
            case TOKEN_SIP:
                $value = '1';
                break;
            case TOKEN_BOOTSTRAP_BUTTON:
                $value = '1';
                break;
            case TOKEN_QUESTION:
                $value = DEFAULT_QUESTION_TEXT;
                break;
            case TOKEN_RENDER:
                $value = DEFAULT_RENDER_MODE;
                break;
            case TOKEN_ACTION_DELETE:
                $value = DEFAULT_ACTION_DELETE;
                break;
            default:
        }

        return $value;
    }

    /**
     * Explode a string of the form 'T:<arg1>|W:<arg2>|s' into an array [ 'T' => '<arg1>', 'W' => '<arg2>', 's' => '' ].
     * Checks if a parameter is double defined.
     *
     * @param string $str
     * @return array
     * @throws UserReportException
     */
    public static function explodeTokenString($str) {
        $tokenGiven = array();
        $control = array();

        $param = explode(PARAM_DELIMITER, $str);

        foreach ($param as $item) {

            // Skip empty entries
            if ($item === '') {
                continue;
            }

            // u:www.example.com
            $arr = explode(":", $item, 2);
            $key = isset($arr[0]) ? $arr[0] : '';
            $value = isset($arr[1]) ? $arr[1] : '';

            // Bookkeeping defined parameter.
            if (isset($tokenGiven[$key])) {
                throw new UserReportException ("Multiple definitions for key '$key'", ERROR_MULTIPLE_DEFINITION);
            }
            $tokenGiven[$key] = true;

            $control[$key] = self::checkForEmptyValue($key, $value);
        }

        return $control;
    }

    /**
     * Converts a string of the form '[width]x[height]' to '[-w <width>][ -h <height]'
     *
     * @param string $dimension
     * @return string
     */
    public static function explodeDimension($dimension) {
        $width = '';
        $height = '';

        if ($dimension[0] == 'x') {
            $height = '-h ' . substr($dimension, 1);
        } else {
            $arr = explode('x', $dimension, 2);
            if (count($arr) > 1) {
                $width = '-w ' . $arr[0];
                $height = ' -h ' . $arr[1];
            } else {
                $width = '-w ' . $dimension;
            }
        }

        return $width . $height;
    }

    /**
     * Executes the Command in $cmd
     * RC: if RC==0 Returns Output, else 'RC - Output'
     *
     * @param    string $cmd : command to start
     *
     * @param int $rc
     * @return string The content that is displayed on the website
     */
    public static function qfqExec($cmd, &$rc = 0) {

        exec($cmd, $arr, $rc);

        $output = implode('<br>', $arr);
        if ($rc != 0) {
            $output = $rc . " - " . $output;
        }

        return ($output);
    }

}