1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5* This class represents a password property in a property form.
6*
7* @author Alex Killing <alex.killing@gmx.de>
8* @version $Id$
9* @ingroup	ServicesForm
10*/
11class ilPasswordInputGUI extends ilSubEnabledFormPropertyGUI
12{
13    protected $value;
14    protected $size = 20;
15    protected $validateauthpost = "";
16    protected $requiredonauth = false;
17    protected $maxlength = false;
18    protected $use_strip_slashes = true;
19
20    /**
21     * @var bool Flag whether the html autocomplete attribute should be set to "off" or not
22     */
23    protected $autocomplete_disabled = true;
24
25    /**
26    * Constructor
27    *
28    * @param	string	$a_title	Title
29    * @param	string	$a_postvar	Post Variable
30    */
31    public function __construct($a_title = "", $a_postvar = "")
32    {
33        global $DIC;
34
35        $this->lng = $DIC->language();
36        parent::__construct($a_title, $a_postvar);
37        $this->setRetype(true);
38        $this->setSkipSyntaxCheck(false);
39    }
40
41    /**
42    * Set Value.
43    *
44    * @param	string	$a_value	Value
45    */
46    public function setValue($a_value)
47    {
48        $this->value = $a_value;
49    }
50
51    /**
52    * Get Value.
53    *
54    * @return	string	Value
55    */
56    public function getValue()
57    {
58        return $this->value;
59    }
60
61    /**
62    * Set retype on/off
63    *
64    * @param	boolean		retype
65    */
66    public function setRetype($a_val)
67    {
68        $this->retype = $a_val;
69    }
70
71    /**
72    * Get retype on/off
73    *
74    * @return	boolean		retype
75    */
76    public function getRetype()
77    {
78        return $this->retype;
79    }
80
81    /**
82    * Set Retype Value.
83    *
84    * @param	string	$a_retypevalue	Retype Value
85    */
86    public function setRetypeValue($a_retypevalue)
87    {
88        $this->retypevalue = $a_retypevalue;
89    }
90
91    /**
92    * Get Retype Value.
93    *
94    * @return	string	Retype Value
95    */
96    public function getRetypeValue()
97    {
98        return $this->retypevalue;
99    }
100
101    /**
102    * Set Max Length.
103    *
104    * @param	int	$a_maxlength	Max Length
105    */
106    public function setMaxLength($a_maxlength)
107    {
108        $this->maxlength = $a_maxlength;
109    }
110
111    /**
112    * Get Max Length.
113    *
114    * @return	int	Max Length
115    */
116    public function getMaxLength()
117    {
118        return $this->maxlength;
119    }
120
121    /**
122    * Set Size.
123    *
124    * @param	int	$a_size	Size
125    */
126    public function setSize($a_size)
127    {
128        $this->size = $a_size;
129    }
130
131    /**
132    * Set value by array
133    *
134    * @param	array	$a_values	value array
135    */
136    public function setValueByArray($a_values)
137    {
138        $this->setValue($a_values[$this->getPostVar()]);
139        $this->setRetypeValue($a_values[$this->getPostVar() . "_retype"]);
140    }
141
142    /**
143    * Get Size.
144    *
145    * @return	int	Size
146    */
147    public function getSize()
148    {
149        return $this->size;
150    }
151
152    /**
153    * Set Validate required status against authentication POST var.
154    *
155    * @param	string	$a_validateauthpost		POST var
156    */
157    public function setValidateAuthPost($a_validateauthpost)
158    {
159        $this->validateauthpost = $a_validateauthpost;
160    }
161
162    /**
163    * Get Validate required status against authentication POST var.
164    *
165    * @return	string		POST var
166    */
167    public function getValidateAuthPost()
168    {
169        return $this->validateauthpost;
170    }
171
172    /**
173    * Set input required, if authentication mode allows password setting.
174    *
175    * @param	boolean	$a_requiredonauth		require input
176    */
177    public function setRequiredOnAuth($a_requiredonauth)
178    {
179        $this->requiredonauth = $a_requiredonauth;
180    }
181
182    /**
183    * Get input required, if authentication mode allows password setting.
184    *
185    * @return	boolean		require input
186    */
187    public function getRequiredOnAuth()
188    {
189        return $this->requiredonauth;
190    }
191
192    /**
193    * Set skip syntax check
194    *
195    * @param	boolean		skip syntax check
196    */
197    public function setSkipSyntaxCheck($a_val)
198    {
199        $this->skip_syntax_check = $a_val;
200    }
201
202    /**
203    * Get skip syntax check
204    *
205    * @return	boolean		skip syntax check
206    */
207    public function getSkipSyntaxCheck()
208    {
209        return $this->skip_syntax_check;
210    }
211
212    /**
213    * Set autocomplete
214    *
215    * @param	bool	$a_value	Value
216    */
217    public function setDisableHtmlAutoComplete($a_value)
218    {
219        $this->autocomplete_disabled = (bool) $a_value;
220    }
221
222    /**
223    * Get autocomplete
224    *
225    * @return	bool	Value
226    */
227    public function isHtmlAutoCompleteDisabled()
228    {
229        return $this->autocomplete_disabled;
230    }
231
232    /**
233     * En/disable use of stripslashes. e.g on login screen.
234     * Otherwise passwords containing "<" are stripped and therefor authentication
235     * fails against external authentication services.
236     * @param type $a_stat
237     */
238    public function setUseStripSlashes($a_stat)
239    {
240        $this->use_strip_slashes = $a_stat;
241    }
242
243    /**
244     *
245     * @return type
246     */
247    public function getUseStripSlashes()
248    {
249        return $this->use_strip_slashes;
250    }
251
252    /**
253    * Check input, strip slashes etc. set alert, if input is not ok.
254    *
255    * @return	boolean		Input ok, true/false
256    */
257    public function checkInput()
258    {
259        $lng = $this->lng;
260
261        if ($this->getUseStripSlashes()) {
262            $_POST[$this->getPostVar()] = ilUtil::stripSlashes($_POST[$this->getPostVar()]);
263            $_POST[$this->getPostVar() . "_retype"] = ilUtil::stripSlashes($_POST[$this->getPostVar() . "_retype"]);
264        }
265        if ($this->getRequired() && trim($_POST[$this->getPostVar()]) == "") {
266            $this->setAlert($lng->txt("msg_input_is_required"));
267
268            return false;
269        }
270        if ($this->getValidateAuthPost() != "") {
271            $auth = ilAuthUtils::_getAuthMode($_POST[$this->getValidateAuthPost()]);
272
273            // check, if password is required dependent on auth mode
274            if ($this->getRequiredOnAuth() && ilAuthUtils::_allowPasswordModificationByAuthMode($auth)
275                && trim($_POST[$this->getPostVar()]) == "") {
276                $this->setAlert($lng->txt("form_password_required_for_auth"));
277
278                return false;
279            }
280
281            // check, if password is allowed to be set for given auth mode
282            if (trim($_POST[$this->getPostVar()]) != "" &&
283                !ilAuthUtils::_allowPasswordModificationByAuthMode($auth)) {
284                $this->setAlert($lng->txt("form_password_not_allowed_for_auth"));
285
286                return false;
287            }
288        }
289        if ($this->getRetype() &&
290            ($_POST[$this->getPostVar()] != $_POST[$this->getPostVar() . "_retype"])) {
291            $this->setAlert($lng->txt("passwd_not_match"));
292
293            return false;
294        }
295        if (!$this->getSkipSyntaxCheck() &&
296            !ilUtil::isPassword($_POST[$this->getPostVar()], $custom_error) &&
297            $_POST[$this->getPostVar()] != "") {
298            if ($custom_error != '') {
299                $this->setAlert($custom_error);
300            } else {
301                $this->setAlert($lng->txt("passwd_invalid"));
302            }
303
304            return false;
305        }
306
307        return $this->checkSubItemsInput();
308    }
309
310    /**
311    * Render item
312    */
313    public function render()
314    {
315        $lng = $this->lng;
316
317        $ptpl = new ilTemplate("tpl.prop_password.html", true, true, "Services/Form");
318
319        if ($this->getRetype()) {
320            $ptpl->setCurrentBlock("retype");
321            $ptpl->setVariable("RSIZE", $this->getSize());
322            $ptpl->setVariable("RID", $this->getFieldId());
323            if ($this->getMaxLength() > 0) {
324                $ptpl->setCurrentBlock("rmaxlength");
325                $ptpl->setVariable("RMAXLENGTH", $this->getMaxLength());
326                $ptpl->parseCurrentBlock();
327            }
328            $ptpl->setVariable("RPOST_VAR", $this->getPostVar());
329
330            if ($this->isHtmlAutoCompleteDisabled()) {
331                $ptpl->setVariable("RAUTOCOMPLETE", "autocomplete=\"off\"");
332            }
333
334            // this is creating an "auto entry" in the setup, if the retype is missing
335            /*$retype_value = ($this->getRetypeValue() != "")
336                ? $this->getRetypeValue()
337                : $this->getValue();*/
338            $retype_value = $this->getRetypeValue();
339            $ptpl->setVariable("PROPERTY_RETYPE_VALUE", ilUtil::prepareFormOutput($retype_value));
340            if ($this->getDisabled()) {
341                $ptpl->setVariable(
342                    "RDISABLED",
343                    " disabled=\"disabled\""
344                );
345            }
346            $ptpl->setVariable("TXT_RETYPE", $lng->txt("form_retype_password"));
347            $ptpl->parseCurrentBlock();
348        }
349
350        if (strlen($this->getValue())) {
351            $ptpl->setCurrentBlock("prop_password_propval");
352            $ptpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($this->getValue()));
353            $ptpl->parseCurrentBlock();
354        }
355        $ptpl->setVariable("POST_VAR", $this->getPostVar());
356        $ptpl->setVariable("ID", $this->getFieldId());
357        $ptpl->setVariable("SIZE", $this->getSize());
358        if ($this->getMaxLength() > 0) {
359            $ptpl->setCurrentBlock("maxlength");
360            $ptpl->setVariable("MAXLENGTH", $this->getMaxLength());
361            $ptpl->parseCurrentBlock();
362        }
363        if ($this->getDisabled()) {
364            $ptpl->setVariable(
365                "DISABLED",
366                " disabled=\"disabled\""
367            );
368        }
369        if ($this->isHtmlAutoCompleteDisabled()) {
370            $ptpl->setVariable("AUTOCOMPLETE", "autocomplete=\"off\"");
371        }
372        if ($this->getRequired()) {
373            $ptpl->setVariable("REQUIRED", "required=\"required\"");
374        }
375        return $ptpl->get();
376    }
377
378    /**
379    * Insert property html
380    *
381    * @return	int	Size
382    */
383    public function insert($a_tpl)
384    {
385        $html = $this->render();
386
387        $a_tpl->setCurrentBlock("prop_generic");
388        $a_tpl->setVariable("PROP_GENERIC", $html);
389        $a_tpl->parseCurrentBlock();
390    }
391}
392