This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHtml.php
211 lines (161 loc) · 6.04 KB
/
CHtml.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
<?php
class CHtml
{
const SALZINDERSUPPE = 'mySecret';
/**
**name CPoll::HTML_getElementValue($htmlName, $prefKey, $initValue)
**description Gets the value for a HTML element by the session data or POST value.
**parameter htmlName: Name of the HTML element.
**parameter prefKey: Variable name of the preference the dialog element stands for.
**parameter initValue: The initial value if the element is shown first.
**returns Returns the default value, the session value or false.
**/
protected function HTML_getElementValue($htmlName, $prefKey, $initValue, $checkbox=false)
{
/*
There are three steps for getting the value for the HTML element ordered by priority:
1. Check if the preference values should be load from the session by force
2. Load the value from the POST variable
3. Load the value from the preference space in the session if it exists
*/
if (isset($_POST[$htmlName]))
$initValue = $_POST[$htmlName];
elseif ($checkbox && !isset($_POST[$htmlName]))
$initValue = false;
else
$met="initValue";
return($initValue);
}
/**
**name CPoll::HTML_getValidSelected($selected, $arrayKeys, $defaultSelection)
**description Checks for a valid selected value from a list of possible values. In case the value could not be found, a default value is taken.
**parameter selected: Array or single value to check, if it is on the list aof array keys.
**parameter arrayKeys: An array that holds the possible returned values (array keys).
**parameter defaultSelection: The value of the item to select by default.
**returns A valid value from a list of possible values.
**/
protected function HTML_getValidSelected($selected, $arrayKeys, $defaultSelection)
{
if (!in_array($selected,$arrayKeys))
{
if (in_array($defaultSelection,$arrayKeys))
$selected = $defaultSelection;
else
$selected = $arrayKeys[0];
}
return($selected);
}
/**
**name CPoll::HTML_selection($htmlName, $array, $type)
**description Shows a list of radio buttons or a selection.
**parameter htmlName: Name of the HTML element.
**parameter array: An array that hold the returned values (array keys) the naming for the elements (array values).
**parameter type: CPoll::TYPE_MULTI for a selection or CPoll::TYPE_SINGLE for radio buttons.
**parameter multipleSize: If set to a number (and not to false) a multi selection is generated, where the user can select multiple entries. The number sets the amount of entries to show the user.
**returns The value of the selected element or false if nothing was selected.
**/
protected function HTML_selection($htmlName, $array, $type)
{
$defaultSelection = false;
$prefKey = false;
$js = "";
$multipleSize = false;
if ($type === CPoll::TYPE_MULTI)
$multipleSize = count($array);
$selected = $this->HTML_getElementValue($htmlName, $prefKey, $defaultSelection);
if (($multipleSize !== false) && (is_numeric($multipleSize)))
{
$multipleSelectEnable = ' multiple="multiple"';
$multipleHtmlNameAdd = '[]';
$multipleSizeAdd = 'size="'.$multipleSize.'"';
}
else
$multipleHtmlNameAdd = $multipleSelectEnable = '';
/*
check if the selected value is a valid array key
and if not
check if the default value is a valid array key and can be assigned
and if not
take the first key from the array
*/
$arrayKeys = array_keys($array);
if (is_array($selected))
{
foreach ($selected as $key => $val)
$selected[$key] = $this->HTML_getValidSelected($val,$arrayKeys,$defaultSelection);
}
else
$selected = $this->HTML_getValidSelected($selected,$arrayKeys,$defaultSelection);
$htmlCode="";
if ($type === CPoll::TYPE_MULTI)
{
$htmlCode='<p style="font-size: small">(Mehrfachauswahl möglich)</p><SELECT '.$js.' name="'.$htmlName.$multipleHtmlNameAdd.'" '.$multipleSelectEnable.' '.$multipleSizeAdd.'>'."\n";
foreach ($array as $value => $description)
{
if ($selected === false) $selected = $value;
$htmlCode.='<option value="'.$value.'">'.$description.'</option>'."\n";
}
$htmlCode.='</SELECT>';
}
elseif ($type === CPoll::TYPE_SINGLE)
{
$htmlBreak="<br>";
foreach ($array as $value => $description)
{
//if the element is checked, set checked flag
if ($value == $selected)
$htmlCode.='<INPUT '.$js.'type="radio" name="'.$htmlName.'" value="'.$value.'" checked> '.$description."$htmlBreak\n";
else
$htmlCode.='<INPUT '.$js.'type="radio" name="'.$htmlName.'" value="'.$value.'"> '.$description."$htmlBreak\n";
}
}
define($htmlName,$htmlCode);
return($selected);
}
/**
**name CPoll::HTML_submit($htmlName,$label,$extra="")
**description Defines a submit button.
**parameter htmlName: Name of the HTML element.
**parameter label: Label of the element.
**parameter extra: Extra options for the HTML input tag.
**returns True if it was clicked otherwise false.
**/
protected function HTML_submit($htmlName,$label,$extra="")
{
define($htmlName,'
<INPUT type="submit" name="'.$htmlName.'" value="'.$label.'"'.$extra.'>
');
return(isset($_POST[$htmlName]) && ($label === stripslashes($_POST[$htmlName])));
}
/**
**name CPoll::CAPTCHA_erstellen()
**description Erstellt und definiert ein einfaches Captcha-HTML-Element mit Eingabe.
**/
protected function CAPTCHA_erstellen()
{
$a = rand (0, 10);
$b = rand (0, 10);
$frage = "Was ist $a + $b?";
$loesung = md5($a + $b + CHtml::SALZINDERSUPPE);
$loesung = " <input name=\"catcha_loesung\" type=\"hidden\" value=\"$loesung\"> <input name=\"catcha_antwort\" size=\"4\" type=\"text\">";
define('CAPTCHA',$frage.$loesung);
}
/**
**name CPoll::CAPTCHA_überprüfen()
**description Überprüft die Antwort eines mit CAPTCHA_erstellen generierten Captchas .
**/
protected function CAPTCHA_überprüfen()
{
if ($_POST['catcha_loesung'] !== md5($_POST['catcha_antwort'] + CHtml::SALZINDERSUPPE))
die('Captcha-Antwort falsch! Bitte nochmal.
<script type="text/javascript">
function goBack()
{
window.history.back()
window.location.href=window.location.href;
}
</script>
<br><input type="button" value="Zurück" onclick="goBack()" />
');
}
}