00001 <?php 00042 class PasswordChangeField extends CollectionField 00043 { 00044 static protected $meterJsDrawn = false; 00045 static protected $suggestJsDrawn = false; 00046 static protected $cssDrawn = false; 00047 protected $meter; 00048 protected $suggest; 00049 00056 public function __construct($name, Form &$parentForm, array $attribs = array()) 00057 { 00058 $attribs['fields'] = array( 00059 array('PasswordField', "${name}_1", $attribs), 00060 array('PasswordField', "${name}_2", $attribs) 00061 ); 00062 00063 $attribs['arrangement'] = 'vertical'; 00064 00065 $this->meter = true; 00066 $this->suggest = false; 00067 parent::__construct($name, $parentForm, $attribs); 00068 } 00069 00070 00076 public function __set($name, $value) 00077 { 00078 if($name == 'meter') 00079 $this->meter = $value; 00080 elseif($name == 'suggest') 00081 $this->suggest = $value; 00082 else 00083 parent::__set($name, $value); 00084 } 00085 00089 public function drawInput() 00090 { 00091 $this->fields[0]->onkeyup = "updatePasswordMeter('$this->name', testPasswordStrength(this.value))"; 00092 parent::drawInput(); 00093 00094 if($this->suggest) 00095 { 00096 $passwords = null; 00097 exec('apg -n 5', $passwords); 00098 00099 echo '<div class="pwdSuggest">'; 00100 echo '<div style="border-bottom: 1px solid gray;">Password Suggestions</div>'; 00101 00102 foreach($passwords as $passwd) 00103 echo "<span onclick=\"useSuggestion('$this->name', this);\" class='passwd'>$passwd</span><br/>"; 00104 00105 echo '</div>'; 00106 } 00107 00108 if($this->meter) 00109 { 00110 echo <<<EOHTML 00111 <span id='{$this->name}_meterlabel' class='pwdMeterLabel'>Password Quality:</span> 00112 <div> 00113 <div id='{$this->name}_meter1' class='pwdMeterInactive'></div> 00114 <div id='{$this->name}_meter2' class='pwdMeterInactive'></div> 00115 <div id='{$this->name}_meter3' class='pwdMeterInactive'></div> 00116 <div id='{$this->name}_meter4' class='pwdMeterInactive'></div> 00117 <div style="clear:both;"></div> 00118 </div> 00119 EOHTML; 00120 } 00121 } 00122 00123 00128 public function validate() 00129 { 00130 parent::validate(); 00131 00132 if($this->fields[0]->getData() != $this->fields[1]->getData()) 00133 { 00134 $this->hasError = true; 00135 $this->errorMessage = "The passwords entered do not match"; 00136 00137 # clear the 2nd password field, and mark it as error'ed 00138 $this->fields[1]->value = null; 00139 $this->fields[1]->forceError(); 00140 } 00141 } 00142 00147 public function getData() 00148 { 00149 return $this->fields[0]->getData(); 00150 } 00151 00156 public function getSqlImpl() 00157 { 00158 # Use the first PasswordField's getSqlImpl, changing the column name to this field's name 00159 return str_replace("{$this->name}_1", $this->name, $this->fields[0]->getSqlImpl()); 00160 } 00161 00165 public function getJs() 00166 { 00167 $js = null; 00168 00169 if(!self::$meterJsDrawn && $this->meter) 00170 { 00171 self::$meterJsDrawn = true; 00172 $js = file_get_contents('inc/form/js/passwordchange-meter.js', true); # this is going to cause troubles... 00173 } 00174 00175 if(!self::$suggestJsDrawn && $this->suggest) 00176 { 00177 self::$suggestJsDrawn = true; 00178 $js .= file_get_contents('inc/form/js/passwordchange-suggest.js', true); 00179 } 00180 00181 return $js; 00182 } 00183 00184 00189 public function getCss() 00190 { 00191 if(self::$cssDrawn) 00192 return null; 00193 00194 self::$cssDrawn = true; 00195 return <<<EOCSS 00196 *.pwdMeterActive, *.pwdMeterInactive 00197 { 00198 border: 1px solid black; 00199 height: 5px; 00200 width: 25px; 00201 margin: 0px 0px 3px 3px; 00202 float: left; 00203 } 00204 00205 *.pwdMeterActive 00206 { 00207 background-color: green; 00208 } 00209 00210 *.pwdMeterInactive 00211 { 00212 background-color: grey; 00213 } 00214 00215 *.pwdMeterLabel 00216 { 00217 font-size: 0.8em; 00218 } 00219 00220 *.pwdSuggest 00221 { 00222 border: 1px solid gray; 00223 background-color: #FFFFCC; 00224 width: 10em; 00225 font-size: 0.7em; 00226 } 00227 EOCSS; 00228 } 00229 } 00230 ?>