[ Index ] |
|
Code source de Plume CMS 1.2.2 |
1 <?php 2 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 3 /* 4 # ***** BEGIN LICENSE BLOCK ***** 5 # Version: MPL 1.1/GPL 2.0/LGPL 2.1 6 # 7 # The contents of this file are subject to the Mozilla Public License Version 8 # 1.1 (the "License"); you may not use this file except in compliance with 9 # the License. You may obtain a copy of the License at 10 # http://www.mozilla.org/MPL/ 11 # 12 # Software distributed under the License is distributed on an "AS IS" basis, 13 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 14 # for the specific language governing rights and limitations under the 15 # License. 16 # 17 # The Original Code is Plume CMS. 18 # 19 # The Initial Developer of the Original Code is Loic d'Anterroches 20 # Portions created by the Initial Developer are Copyright (C) 2003 21 # the Initial Developer. All Rights Reserved. 22 # 23 # Credits: 24 # Olivier Meunier 25 # 26 # Contributor(s): 27 # 28 # Alternatively, the contents of this file may be used under the terms of 29 # either the GNU General Public License Version 2 or later (the "GPL"), or 30 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 31 # in which case the provisions of the GPL or the LGPL are applicable instead 32 # of those above. If you wish to allow use of your version of this file only 33 # under the terms of either the GPL or the LGPL, and not to allow others to 34 # use your version of this file under the terms of the MPL, indicate your 35 # decision by deleting the provisions above and replace them with the notice 36 # and other provisions required by the GPL or the LGPL. If you do not delete 37 # the provisions above, a recipient may use your version of this file under 38 # the terms of any one of the MPL, the GPL or the LGPL. 39 # 40 # ***** END LICENSE BLOCK ***** */ 41 42 class form 43 { 44 45 /** 46 * Build a combobox. 47 * 48 * @param string Id and name of the form 49 * @param array Data, the keys of the array are the displayed strings 50 * @param string Selected value ('') 51 * @param string Default selected value ('') 52 * @param int Tabulation index ('') 53 * @param string CSS class of the combobox field ('') 54 * @param string Extra data for javascript scripting ('') 55 * @return string HTML string of the form 56 */ 57 function combobox($id, $arryData, $selected='', $default='', $tabindex='', 58 $class='', $html='') 59 { 60 $res = '<select name="'.$id.'" '; 61 62 if($class != '') 63 $res .= 'class="'.$class.'" '; 64 65 $res .= 'id="'.$id.'" '; 66 67 $res .= ($tabindex != '') ? 'tabindex="'.$tabindex.'" ' : ''; 68 if($html != '') 69 $res .= $html.' '; 70 71 $res .= '>'."\n"; 72 73 if ($selected == '') 74 $selected = $default; 75 76 foreach($arryData as $k => $v) { 77 $res .= '<option value="'.$v.'"'; 78 if($v == $selected) 79 $res .= ' selected="selected"'; 80 81 $res .= '>'.$k.'</option>'."\n"; 82 } 83 84 $res .= '</select>'."\n"; 85 return $res; 86 } 87 88 /** 89 * Build a single line text field. 90 * 91 * @param string Name and id of the field 92 * @param int Size Size of the field 93 * @param int Maximum size of the field (0) 94 * @param string Default value (is htmlspecialchars escaped) ('') 95 * @param int Tabulation index ('') 96 * @param string Extra information for javascript scripting ('') 97 * @return string HTML string of the form 98 */ 99 function textField($id, $size, $max=0, $default='', $tabindex='', $html='') 100 { 101 $res = '<input type="text" size="'.$size 102 .'" name="'.$id.'" id="'.$id.'" '; 103 $res .= ($max != 0) ? 'maxlength="'.$max.'" ' : ''; 104 $res .= ($tabindex != '') ? 'tabindex="'.$tabindex.'" ' : ''; 105 $res .= ($default != '') ? 106 'value="'.htmlspecialchars($default).'" ' : ''; 107 $res .= $html; 108 $res .= ' />'; 109 return $res; 110 } 111 112 /** 113 * Build a single line password text field. 114 * 115 * @param string Name and id of the field 116 * @param int Size Size of the field 117 * @param int Maximum size of the field (0) 118 * @param string Default value (is htmlspecialchars escaped) ('') 119 * @param int Tabulation index ('') 120 * @param string Extra information for javascript scripting ('') 121 * @return string HTML string of the form 122 */ 123 function passwordField($id, $size, $max=0, $default='', 124 $tabindex='', $html='') 125 { 126 $res = '<input type="password" size="'.$size 127 .'" name="'.$id.'" id="'.$id.'" '; 128 $res .= ($max != 0) ? 'maxlength="'.$max.'" ' : ''; 129 $res .= ($tabindex != '') ? 'tabindex="'.$tabindex.'" ' : ''; 130 $res .= ($default != '') ? 131 'value="'.htmlspecialchars($default).'" ' : ''; 132 $res .= $html; 133 $res .= ' />'; 134 return $res; 135 } 136 137 138 /** 139 * Build a date/time set of fields. 140 * The array of the date must be in the same order as the 141 * arguments of the mktime() function. ie. (h,m,s,M,D,Y) 142 * 143 * @param string Id and name of the form 144 * @param array Date to set the form ('') default to now 145 * @param int Tabulation index ('') 146 * @param string Extra data for javascript scripting ('') 147 * @return string HTML string of the form 148 */ 149 function datetime($id, $date='', $tabindex='', $html='') 150 { 151 $id = trim($id); 152 //Need first to get the correct date 153 if (!is_array($date)) { 154 $date = date::explode(date::stamp()); 155 } 156 $months = array(); 157 for ($i=1; $i<=12; $i++) { 158 $month = sprintf('%02d', $i); 159 $months[if_utf8(strftime('%B', strtotime('2000-'.$month.'-01')))] = $month; 160 } 161 162 return form::textField($id.'_d', 2, 2, $date[4], $tabindex).' '. 163 form::combobox($id.'_m', $months, $date[3], $tabindex).' '. 164 form::textField($id.'_y', 4, 4, $date[5], $tabindex).' '. 165 __('Time:').' '. 166 form::textField($id.'_h', 2, 2, $date[0], $tabindex).':'. 167 form::textField($id.'_i', 2, 2, $date[1], $tabindex).':'. 168 form::textField($id.'_s', 2, 2, $date[2], $tabindex); 169 } 170 171 /** 172 * Build a textarea field. 173 * 174 * @param string Name and id 175 * @param int Number of columns 176 * @param int Number of rows 177 * @param string Default value (is htmlspecialchars escaped) ('') 178 * @param int Tabulation index ('') 179 * @param string Extra information for javascript scripting ('') 180 * @return string HTML string of the form element 181 */ 182 function textArea($id, $cols, $rows, $default='', $tabindex='', $html='') 183 { 184 $res = '<textarea cols="'.$cols.'" rows="'.$rows.'" '; 185 $res .= 'name="'.$id.'" id="'.$id.'" '; 186 $res .= ($tabindex != '') ? 'tabindex="'.$tabindex.'" ' : ''; 187 $res .= $html.'>'; 188 $res .= htmlspecialchars($default); 189 $res .= '</textarea>'; 190 191 return $res; 192 } 193 194 /** 195 * Build a button. 196 * 197 * @param string Type ('submit') 198 * @param string Id ('') 199 * @param string Value ('ok') 200 * @param int Tabulation index ('') 201 * @param string Access key ('') 202 * @param string Class ('') 203 * @return string HTML of the button 204 */ 205 function button($type='submit', $id='', $value='ok', 206 $tabindex='', $accesskey='', $class='') 207 { 208 $res = '<input type="'.$type.'" value="'.$value.'" '; 209 $res .= ($id != '') ? 'name="'.$id.'" id="'.$id.'" ' : ''; 210 $res .= ($tabindex != '') ? 'tabindex="'.$tabindex.'" ' : ''; 211 $res .= ($accesskey != '') ? 'accesskey="'.$accesskey.'" ' : ''; 212 $res .= ($class != '') ? 'class="'.$class.'" ' : ''; 213 $res .= '/>'; 214 215 return $res; 216 } 217 218 /** 219 * Build a hidden field. 220 * 221 * @param string Id 222 * @param string Value 223 * @param boolean With_Id 224 * @return string HTML of the field 225 */ 226 function hidden($id, $value, $with_id=true) 227 { 228 if ($with_id) { 229 $res = '<input type="hidden" name="'.$id.'" id="' 230 .$id.'" value="'.$value.'" />'; 231 } else { 232 $res = '<input type="hidden" name="'.$id.'" value="'.$value.'" />'; 233 } 234 235 return $res; 236 } 237 238 /** 239 * Build a checkbox field. 240 * 241 * @param string Id of the field 242 * @param string Value 243 * @param bool Is the checkbox checked (false) 244 * @param int Tabulation index ('') 245 * @param string Extra information for javascript scripting ('') 246 * @return string HTML of the field 247 */ 248 function checkbox($id, $value, $checked=false, $tabindex='', $html='') 249 { 250 $res = '<input type="checkbox" value="'.$value.'" '; 251 $res .= 'name="'.$id.'" id="'.$id.'" '; 252 $res .= ($tabindex != '') ? 'tabindex="'.$tabindex.'" ' : ''; 253 $res .= ($checked) ? 'checked="checked" ' : ''; 254 $res .= $html.' />'; 255 256 return $res; 257 } 258 259 /** 260 * Build a radio button field. 261 * 262 * @param string Name of the field 263 * @param string Value 264 * @param bool Is the checkbox checked (false) 265 * @param string Class name 266 * @param string Id of the field 267 * @return string HTML of the field 268 */ 269 function radio($name, $value, $checked='', $class='', $id='') 270 { 271 $res = '<input type="radio" name="'.$name.'" value="'.$value.'" '; 272 273 if($class != '') { 274 $res .= 'class="'.$class.'" '; 275 } 276 277 if($id != '') { 278 $res .= 'id="'.$id.'" '; 279 } 280 281 if (($checked === 0) or $checked >= 1) { 282 $res .= 'checked="checked" '; 283 } 284 285 $res .= '/>'."\n"; 286 287 return $res; 288 } 289 290 /* ================================================================== * 291 * Methods to get data from forms * 292 * ================================================================== */ 293 294 /** 295 * Get REQUEST field. 296 * A request field is field that can be set through POST, GET or COOKIE 297 * method. 298 * 299 * @param string REQUEST field to get 300 * @return string Empty string if field not set. 301 */ 302 function getField($field) 303 { 304 if (!empty($_REQUEST[$field])) { 305 return $_REQUEST[$field]; 306 } 307 return ''; 308 } 309 310 311 /** 312 * Get POST field. 313 * Returns an empty string if the value is not set. 314 * 315 * @param string POST field to get 316 * @return string Empty string if field not sent 317 */ 318 function getPostField($field) 319 { 320 if (!empty($_POST[$field])) { 321 return $_POST[$field]; 322 } 323 return ''; 324 } 325 326 /** 327 * Get a "time" field from a POST method. 328 * Returns a timestamp. 329 * 330 * @param string Time field to get 331 * @return timestamp Time 332 */ 333 function getTimeField($field) 334 { 335 $field = trim($field); 336 $dt_y = (string) sprintf('%04d', form::getPostField($field.'_y')); 337 $dt_m = (string) sprintf('%02d', form::getPostField($field.'_m')); 338 $dt_d = (string) sprintf('%02d', form::getPostField($field.'_d')); 339 $dt_h = (string) sprintf('%02d', form::getPostField($field.'_h')); 340 $dt_i = (string) sprintf('%02d', form::getPostField($field.'_i')); 341 $dt_s = (string) sprintf('%02d', form::getPostField($field.'_s')); 342 343 //Small "fixes" 344 if ($dt_d > 31 || $dt_d < 1) { $dt_d = '01'; } 345 if ($dt_h > 23 || $dt_h < 0) { $dt_h = '00'; } 346 if ($dt_i > 59 || $dt_i < 0) { $dt_i = '00'; } 347 if ($dt_s > 59 || $dt_s < 0) { $dt_s = '00'; } 348 349 return $dt_y.$dt_m.$dt_d.$dt_h.$dt_i.$dt_s; 350 } 351 } 352 353 354 class Validate 355 { 356 /** 357 * Check if an email has the right format. 358 * 359 * @param string Email 360 * @return bool Success 361 */ 362 function checkEmail($email) 363 { 364 if (!eregi('^[_a-z0-9\-]+(\.[_a-z0-9\-\+]+)*@[a-z0-9\-]+(\.[a-z0-9\-]+)*(\.[a-z]{2,4})$', $email)) { 365 return false; 366 } 367 return true; 368 } 369 370 } 371 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 11:57:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |