Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
form.class.php
Go to the documentation of this file.
00001 <?php
00030 class form {
00031         
00035         private $_registry;
00036 
00040         private $_view;
00041 
00045         private $_method;
00046 
00050         private $_name;
00051         
00055         private $_validation;
00056 
00060         private $_requestVars;
00061 
00073         function __construct($method, $name, $opts=null) {
00074 
00075                 $this->_registry = registry::instance();
00076 
00077                 $this->_method = $method;       
00078                 $this->_name = $name;
00079 
00080                 $this->_view = new view();
00081 
00082                 $this->_validation = gOpt($opts, 'validation', false);
00083 
00084                 if(gOpt($opts, 'verifyToken')) {
00085                         if(!$this->verifyFormToken($this->_name)) {
00086                                 exit(error::syserrorMessage("form", "construct", __("CSRFDetectError")));
00087                         }
00088                 }
00089                 
00090                 $this->_requestVars = $this->_method == 'post' ? $_POST : ($this->_method == 'get' ? $_GET : $_REQUEST);        
00091         
00092         }
00093         
00099         private function generateFormToken() {
00100                 $token = md5(uniqid(microtime(), true));
00101                 $_SESSION[$this->_name.'_token'] = $token;
00102                 return $token;
00103         }
00104 
00110         private function verifyFormToken() {
00111                 $index = $this->_name.'_token';
00112                 // There must be a token in the session
00113                 if (!isset($_SESSION[$index])) return false;
00114                 // There must be a token in the form
00115                 if (!isset($_POST['token'])) return false;
00116                 // The token must be identical
00117                 if ($_SESSION[$index] !== $_POST['token']) return false;
00118                 return true;
00119         }
00120 
00127         public function load($noerror=false) {
00128                 
00129                 $this->_registry->fvars = array();
00130                 $vars = array();
00131 
00132                 if(isset($_SESSION["formvars_".$this->_name])) {
00133                         if($noerror || (isset($_SESSION['ERRORMSG']) AND !empty($_SESSION['ERRORMSG'])))
00134                                 foreach($_SESSION['formvars_'.$this->_name] as $k=>$v)
00135                                                 $vars[$k] = $v;
00136                         $this->_registry->fvars = $vars;
00137 
00138                         unset($_SESSION['formvars_'.$this->_name]);
00139                 }
00140 
00141         }
00142 
00148         public function save() {
00149                 
00150                 $_SESSION["formvars_".$this->_name] = array();
00151                 foreach($this->_requestVars as $key => $value)
00152                         $_SESSION["formvars_".$this->_name][$key] = $value;
00153 
00154         }
00155 
00163         public function retvar($name, $dft=null) {
00164 
00165                 return isset($this->_registry->fvars[$name]) ? $this->_registry->fvars[$name] : $dft;
00166 
00167         }
00168 
00174         public function free() {
00175         
00176                         unset($_SESSION['formvars_'.$this->_name]);
00177 
00178         }
00179 
00186         public function setRequired($required) {
00187                 
00188                 return !empty($required) ? $this->hidden('required', $required) : '';
00189 
00190         }
00191 
00197         public function checkRequired() {
00198                 
00199                 $error = false;
00200                 $required = isset($this->_requestVars['required']) ? cleanInput($this->_method, 'required', 'string') : '';
00201                 
00202                 if(!empty($required)) {
00203                         foreach(explode(",", $required) as $fieldname) {
00204                                 if($this->_requestVars[trim($fieldname)]=='') $error = true;
00205                         }
00206                 }
00207 
00208                 return $error;
00209 
00210         }
00211 
00223         public function sform($action, $required, $opts=null) {
00224         
00225                 $buffer = "<form name=\"$this->_name\" id=\"".$this->_name."\" method=\"$this->_method\" action=\"$action\"";
00226                 if(gOpt($opts, 'upload')) $buffer .= " enctype=\"multipart/form-data\"";
00227                 if($this->_validation) $buffer .= " onsubmit=\"return validateForm($(this))\"";
00228                 $buffer .= ">\n";
00229                 if(gOpt($opts, 'generateToken')) 
00230                         $buffer .= $this->hidden('token', $this->generateFormToken());
00231 
00232                 $buffer .= $this->setRequired($required);
00233 
00234                 return $buffer;
00235         }
00236 
00242         public function cform() {
00243 
00244                 return "</form>";
00245 
00246         }
00247 
00257         public function ccaptcha($opts=null) {
00258         
00259                 list($l, $d) = array(__("SecureCode"), __("SecureCodeExp"));
00260 
00261                 $this->prepareView('captcha_code', $l, $d, true, gOpt($opts, 'text_add'));
00262                 $this->_view->assign('formfield', $this->captcha($opts));
00263                 
00264                 return $this->_view->render();
00265         }
00266 
00275         public function captcha($opts=null) {
00276 
00277                 $class = gOpt($opts, "class", "left captcha");
00278         
00279                 require_once(ABS_CORE.DS.'captcha.class.php');
00280                 $captcha = new captcha('captcha_code');
00281         
00282                 return "<div class=\"$class\">".$captcha->render()."</div>";
00283 
00284         }
00285 
00291         public function checkCaptcha() {
00292         
00293                 require_once(ABS_CORE.DS.'captcha.class.php');
00294 
00295                 $captcha = new captcha('captcha_code');
00296 
00297                 return $captcha->check();       
00298 
00299         }
00300         
00311         public function fieldset($legend, $content, $opts=null) {
00312 
00313                 $this->_view->setTpl('form_fieldset');
00314                 $this->_view->assign('id', gOpt($opts, "id"));
00315                 $this->_view->assign('legend', $legend);
00316                 $this->_view->assign('content', $content);
00317 
00318                 return $this->_view->render();
00319         }
00320 
00329         public function label($text){
00330 
00331                 if(!$text) return array(null, null);
00332                 if(is_array($text) && count($text)==2) {
00333                         $label = isset($text['label']) ? $text['label'] : $text[0];
00334                         $description = isset($text['description']) ? $text['description'] : $text[1];
00335                 }
00336                 else {$label = $text; $description = null;}
00337                 
00338                 return array($label, $description);
00339         }
00340 
00351         public function hidden($name, $value, $opts=null) {
00352 
00353                 $buffer = "<input type=\"hidden\" name=\"$name\" value=\"$value\" ".(gOpt($opts, 'id')?"id=\"".gOpt($opts, 'id')."\"":"")."/>\n";
00354 
00355                 return $buffer;
00356         }
00357 
00373         private function prepareView($name, $l, $d, $req, $tadd, $opts=null) {
00374                 
00375                 $this->_view->setTpl('form_element');
00376                 $this->_view->assign('name', $name);
00377                 $this->_view->assign('label', $l);
00378                 if(gOpt($opts, 'label_class')) 
00379                         $this->_view->assign('label_class', gOpt($opts, 'label_class'));
00380                 if(gOpt($opts, 'label_form', true)) 
00381                         $this->_view->assign('label_form', $this->_name);
00382                 $this->_view->assign('required', $req);
00383                 $this->_view->assign('description', $d);
00384                 $this->_view->assign('textadd', $tadd);
00385                 $this->_view->assign('more', null);
00386 
00387         }
00388 
00400         public function freeInput($cleft, $cright, $opts=null) {
00401         
00402                 $this->_view->setTpl('form_cell');
00403                 $this->_view->assign('idleft', gOpt($opts, 'idleft', null));
00404                 $this->_view->assign('cleft', $cleft);
00405                 $this->_view->assign('idright', gOpt($opts, 'idright', null));
00406                 $this->_view->assign('cright', $cright);
00407 
00408                 return $this->_view->render();
00409         }
00410 
00433         public function input($name, $type, $value, $opts=null){
00434 
00435                 $dft_pattern = $dft_hint = null;
00436 
00437                 $buffer = "<input type=\"$type\" name=\"$name\" value=\"$value\" ";
00438 
00439                 if($type == 'email') {
00440                         $dft_pattern = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$";
00441                         $dft_hint = __("insertValidEmail");     
00442                 }
00443                 
00444                 $buffer .= gOpt($opts, 'id') ? "id=\"".gOpt($opts, 'id')."\" ":"";
00445                 $buffer .= gOpt($opts, 'class') ? "class=\"".gOpt($opts, 'class')."\" ":"";
00446                 $buffer .= gOpt($opts, 'pattern', $dft_pattern) ? "pattern=\"".gOpt($opts, 'pattern', $dft_pattern)."\" ":"";
00447                 $buffer .= gOpt($opts, 'hint', $dft_hint) ? "data-hint=\"".gOpt($opts, 'hint', $dft_hint)."\" ":"";
00448                 $buffer .= gOpt($opts, 'placeholder') ? "placeholder=\"".gOpt($opts, 'placeholder')."\" ":"";
00449                 $buffer .= gOpt($opts, 'size') ? "size=\"".gOpt($opts, 'size')."\" ":"";
00450                 $buffer .= gOpt($opts, 'maxlength') ? "maxlength=\"".gOpt($opts, 'maxlength')."\" ":"";
00451                 $buffer .= gOpt($opts, 'readonly') ? "readonly=\"".gOpt($opts, 'readonly')."\" ":"";
00452                 $buffer .= gOpt($opts, 'required') ? "required=\"required\" ":"";
00453                 $buffer .= gOpt($opts, 'formnovalidate') ? "formnovalidate ":"";
00454                 $buffer .= gOpt($opts, 'js') ? gOpt($opts, 'js')." ":"";
00455                 $buffer .= gOpt($opts, 'other') ? gOpt($opts, 'other')." ":"";
00456         
00457                 $buffer .= " />";
00458 
00459                 return $buffer;
00460         }
00461         
00475         public function cinput($name, $type, $value, $label, $opts){
00476 
00477                 list($l, $d) = $this->label($label);
00478                 $this->prepareView($name, $l, $d, gOpt($opts, 'required'), gOpt($opts, 'text_add'));
00479                 $this->_view->assign('formfield', $this->input($name, $type, $value, $opts));
00480                 
00481                 return $this->_view->render();
00482         }
00483         
00496         public function cinput_date($name, $value, $label, $opts){
00497 
00498                 $opts['size'] = 10;
00499                 $opts['maxlength'] = 10;
00500                 $opts['pattern'] = "^\d\d\d\d-\d\d-\d\d$";
00501                 $opts['hint'] = "dd/mm/yyyy";
00502 
00503                 $dpjs = "<script type=\"text/javascript\">";
00504                 $dpjs .= "window.int_input_date_$name = setInterval(activateDatePicker$name, 100);";
00505                 $dpjs .= "function activateDatePicker$name() {
00506                         if(typeof $$('input[name=$name]')[0] != undefined) {
00507                                 clearInterval(window.int_input_date_$name);
00508                                 new DatePicker($$('input[name=$name]')[0], {
00509                                         pickerClass: 'datepicker_dashboard', 
00510                                         days: ['".__("Sunday")."', '".__("Monday")."', '".__("Tuesday")."', '".__("Wednesday")."', '".__("Thursday")."', '".__("Friday")."', '".__("Saturday")."'], 
00511                                         months:['".__("January")."', '".__("February")."', '".__("March")."', '".__("April")."', '".__("May")."', '".__("June")."', '".__("July")."', '".__("August")."', '".__("September")."', '".__("October")."', '".__("November")."', '".__("December")."'], 
00512                                         format:'d/m/Y', 
00513                                         inputOutputFormat:'Y-m-d', 
00514                                         startDay: 1, 
00515                                         allowEmpty: ".(gOpt($opts, 'init') ? "false" : "true")."});
00516                         }
00517                 }";
00518                 $dpjs .= "</script>";
00519 
00520                 list($l, $d) = $this->label($label);
00521                 $this->prepareView($name, $l, $d, gOpt($opts, 'required'), gOpt($opts, 'text_add'));
00522                 $this->_view->assign('formfield', $this->input($name, 'text', $value, $opts));
00523                 $this->_view->assign('more', $dpjs);
00524                 
00525                 return $this->_view->render();
00526         }
00527         
00540         public function cinput_datetime($name, $value, $label, $opts){
00541 
00542                 $opts['size'] = gOpt($opts, 'seconds')==true ? 19 : 16;
00543                 $opts['maxlength'] = 19; // the input/output format has always seconds
00544                 $opts['pattern'] = "^\d\d\d\d-\d\d-\d\d \d\d:\d\d(:\d\d)?$";
00545                 $opts['hint'] = "dd/mm/yyyy hh:mm";
00546 
00547                 $dpjs = "<script type=\"text/javascript\">";
00548                 $dpjs .= "window.int_input_datetime_$name = setInterval(activateDatetimePicker$name, 100);";
00549                 $dpjs .= "function activateDatetimePicker$name() {
00550                         if(typeof $$('input[name=$name]')[0] != undefined) {
00551                                 clearInterval(window.int_input_datetime_$name);
00552                                 new DatePicker($$('input[name=$name]'), {
00553                                         timePicker: true, 
00554                                         pickerClass: 'datepicker_dashboard', 
00555                                         days: ['".__("Sunday")."', '".__("Monday")."', '".__("Tuesday")."', '".__("Wednesday")."', '".__("Thursday")."', '".__("Friday")."', '".__("Saturday")."'], 
00556                                         months:['".__("January")."', '".__("February")."', '".__("March")."', '".__("April")."', '".__("May")."', '".__("June")."', '".__("July")."', '".__("August")."', '".__("September")."', '".__("October")."', '".__("November")."', '".__("December")."'], 
00557                                         format: 'd/m/Y H:i".(gOpt($opts, 'seconds')==true ? ":s":"")."', 
00558                                         inputOutputFormat:'Y-m-d H:i:s', 
00559                                         startDay: 1, 
00560                                         allowEmpty: ".(gOpt($opts, 'init') ? "false" : "true")."});
00561                         }
00562                 }";
00563                 $dpjs .= "</script>";
00564                 
00565                 list($l, $d) = $this->label($label);
00566                 $this->prepareView($name, $l, $d, gOpt($opts, 'required'), gOpt($opts, 'text_add'));
00567                 $this->_view->assign('formfield', $this->input($name, 'text', $value, $opts));
00568                 $this->_view->assign('more', $dpjs);
00569                 
00570                 return $this->_view->render();
00571         }
00572 
00585         public function ctextarea($name, $value, $label, $opts=null){
00586 
00587                 list($l, $d) = $this->label($label);
00588                 $this->prepareView($name, $l, $d, gOpt($opts, 'required'), gOpt($opts, 'text_add'));
00589                 $this->_view->assign('formfield', $this->textarea($name, $value, $opts));
00590 
00591                 return $this->_view->render();
00592         }
00593 
00615         public function textarea($name, $value, $opts){
00616                 
00617                 if(gOpt($opts, 'editor', false)) {
00618                         $buffer = "<div id=\"$name\" class=\"html\">$value</div>";
00619                         $buffer .= $this->hidden($name, '');
00620                 }
00621                 else {
00622                         $buffer = "<textarea name=\"$name\" ";
00623 
00624                         $buffer .= gOpt($opts, 'id') ? "id=\"".gOpt($opts, 'id')."\" ":"";
00625                         $buffer .= gOpt($opts, 'class') ? "class=\"".gOpt($opts, 'class')."\" ":"";
00626                         $buffer .= gOpt($opts, 'pattern') ? "pattern=\"".gOpt($opts, 'pattern')."\" ":"";
00627                         $buffer .= gOpt($opts, 'hint') ? "data-hint=\"".gOpt($opts, 'hint')."\" ":"";
00628                         $buffer .= gOpt($opts, 'placeholder') ? "placeholder=\"".gOpt($opts, 'placeholder')."\" ":"";
00629                         $buffer .= gOpt($opts, 'cols') ? "cols=\"".gOpt($opts, 'cols')."\" ":"";
00630                         $buffer .= gOpt($opts, 'rows') ? "rows=\"".gOpt($opts, 'rows')."\" ":"";
00631                         $buffer .= gOpt($opts, 'readonly') ? "readonly=\"".gOpt($opts, 'readonly')."\" ":"";
00632                         $buffer .= gOpt($opts, 'required') ? "required=\"required\" ":"";
00633                         $buffer .= gOpt($opts, 'js') ? gOpt($opts, 'js')." ":"";
00634                         $buffer .= gOpt($opts, 'other') ? gOpt($opts, 'other')." ":"";
00635                         $buffer .= ">";
00636                         $buffer .= "$value</textarea>";
00637                 }
00638                 
00639                 return $buffer;
00640         }
00641 
00657         public function cradio($name, $value, $data, $default, $label, $opts=null){
00658                 
00659                 list($l, $d) = $this->label($label);
00660                 $this->prepareView($name, $l, $d, gOpt($opts, 'required'), gOpt($opts, 'text_add'));
00661                 $this->_view->assign('formfield', $this->radio($name, $value, $data, $default, $opts));
00662                 
00663                 return $this->_view->render();
00664 
00665         }
00666 
00682         public function radio($name, $value, $data, $default, $opts){
00683                 
00684                 $buffer = '';
00685                 $comparison = is_null($value)? $default:$value;
00686                 $space = gOpt($opts, 'aspect')=='v'? "<br />":"&nbsp;";
00687                         
00688                 if(is_array($data)) {
00689                         $i=0;
00690                         foreach($data AS $k => $v) {
00691                                 $buffer .= ($i?$space:'')."<input type=\"radio\" name=\"$name\" value=\"$k\" ".($comparison==$k && !($comparison==='' && $k===0) ?"checked=\"checked\"":"")." ";
00692                                 $buffer .= gOpt($opts, 'id') ? "id=\"".gOpt($opts, 'id')."\" ":"";
00693                                 $buffer .= gOpt($opts, 'js') ? gOpt($opts, 'js')." ":"";
00694                                 $buffer .= gOpt($opts, 'other') ? gOpt($opts, 'other')." ":"";
00695                                 $buffer .= "/>".$v;
00696                                 $i++;
00697                         }
00698                 }
00699                 
00700                 return $buffer;
00701         }
00702         
00716         public function cselect($name, $value, $data, $label, $opts=null) {
00717                 
00718                 list($l, $d) = $this->label($label);
00719                 $this->prepareView($name, $l, $d, gOpt($opts, 'required'), gOpt($opts, 'text_add'));
00720                 $this->_view->assign('formfield', $this->select($name, $value, $data, $opts));
00721                 
00722                 return $this->_view->render();
00723 
00724         }
00725         
00748         public function select($name, $selected, $data, $opts) {
00749                 
00750                 $buffer = "<select name=\"$name\" ";
00751                 $buffer .= gOpt($opts, 'id') ? "id=\"".gOpt($opts, 'id')."\" ":"";
00752                 $buffer .= gOpt($opts, 'classField') ? "class=\"".gOpt($opts, 'classField')."\" ":"";
00753                 $buffer .= gOpt($opts, 'size') ? "size=\"".gOpt($opts, 'size')."\" ":"";
00754                 $buffer .= gOpt($opts, 'multiple') ? "multiple=\"multiple\" ":"";
00755                 $buffer .= gOpt($opts, 'required') ? "required=\"required\" ":"";
00756                 $buffer .= gOpt($opts, 'js') ? gOpt($opts, 'js')." ":"";
00757                 $buffer .= gOpt($opts, 'other') ? gOpt($opts, 'other')." ":"";
00758                 $buffer .= ">\n";
00759 
00760                 if(!is_array($selected)) $selected = array($selected);
00761 
00762                 if(gOpt($opts, 'firstVoice')) $buffer .= "<option value=\"".gOpt($opts, 'firstValue')."\">".gOpt($opts, "firstVoice")."</option>";
00763                 elseif(!gOpt($opts, 'noFirst')) $buffer .= "<option value=\"\"></option>\n";
00764                 
00765                 if(is_array($data)) {
00766                         if(sizeof($data) > 0) {
00767                                 foreach ($data as $key=>$value) {
00768                                         $title = null;
00769                                         if(is_array($value)) { $label = $value['label']; $title = $value['title']; }
00770                                         else $label = $value;
00771                                         if(gOpt($opts, 'maxChars')) $label = cutHtmlText($label, gOpt($opts, 'maxChars'), '...', true, gOpt($opts, 'cutWords', false), true);
00772                                         $buffer .= "<option value=\"$key\" ".(in_array($key, $selected)?"selected=\"selected\"":"")." ".($title ? "title=\"$title\"":"").">".$label."</option>\n";
00773                                 }
00774                         }
00775                 }
00776 
00777                 $buffer .= "</select>\n";
00778 
00779                 return $buffer;
00780         }
00781         
00795         public function ccheckbox($name, $checked, $value, $label, $opts=null){
00796                 
00797                 list($l, $d) = $this->label($label);
00798                 $this->prepareView($name, $l, $d, gOpt($opts, 'required'), gOpt($opts, 'text_add'));
00799                 $this->_view->assign('formfield', $this->checkbox($name, $checked, $value, $opts));
00800                 
00801                 return $this->_view->render();
00802 
00803         }
00804         
00820         public function checkbox($name, $checked, $value, $opts=null){
00821                 
00822                 $buffer = "<input type=\"checkbox\" name=\"$name\" value=\"$value\" ".($checked?"checked=\"checked\"":"")." ";
00823                 $buffer .= gOpt($opts, 'id') ? "id=\"".gOpt($opts, 'id')."\" ":"";
00824                 $buffer .= gOpt($opts, 'classField') ? "class=\"".gOpt($opts, 'classField')."\" ":"";
00825                 $buffer .= gOpt($opts, 'required') ? "required=\"required\" ":"";
00826                 $buffer .= gOpt($opts, 'js') ? gOpt($opts, 'js')." ":"";
00827                 $buffer .= gOpt($opts, 'other') ? gOpt($opts, 'other')." ":"";
00828                 $buffer .= "/>\n";
00829                 
00830                 return $buffer;
00831         }
00832 
00847         public function cmulticheckbox($name, $checked, $values, $label, $opts=null){
00848                 
00849                 $label_class = gOpt($opts, 'label_class', '');
00850                 list($l, $d) = $this->label($label);
00851                 $this->prepareView($name, $l, $d, gOpt($opts, 'required'), gOpt($opts, 'text_add'), array("label_class"=>$label_class));
00852                 $this->_view->assign('formfield', $this->multiplecheckbox($name, $checked, $values, $opts));
00853                 
00854                 return $this->_view->render();
00855 
00856         }
00857 
00872         public function multiplecheckbox($name, $checked, $values, $opts=null){
00873 
00874                 $rows = array();
00875                 $buffer = '';
00876                 $i=0;
00877                 foreach($values as $value) {
00878                         $rows[$i] = array($value['label']);
00879                         $buffer = "<input type=\"checkbox\" name=\"".$name."\" value=\"".$value['value']."\" ".(in_array($value['value'], $checked) ? "checked=\"checked\"":"")." ";
00880                         $buffer .= gOpt($opts, 'id') ? "id=\"".gOpt($opts, 'id')."\" ":"";
00881                         $buffer .= gOpt($opts, 'classField') ? "class=\"".gOpt($opts, 'classField')."\" ":"";
00882                         $buffer .= gOpt($opts, 'js') ? gOpt($opts, 'js')." ":"";
00883                         $buffer .= gOpt($opts, 'other') ? gOpt($opts, 'other')." ":"";
00884                         $buffer .= "/>\n";
00885                         $rows[$i][] = $buffer;
00886                         $i++;
00887                 }
00888                 
00889                 $view = new view($this->_registry);
00890                 $view->setTpl('form_multicheckbox');
00891                 $view->assign('class', '');
00892                 $view->assign('rows', $rows);
00893 
00894                 return $view->render();
00895         }
00896 
00910         public function cinput_file($name, $value, $label, $opts=null){
00911 
00912                 $valid_extension = gOpt($opts, 'extensions', null);
00913                 
00914                 list($l, $d) = $this->label($label);
00915                 $d = $valid_extension ? implode(", ", $valid_extension)."<br />".$d : '';
00916                 $this->prepareView($name, $l, $d, gOpt($opts, 'required'), gOpt($opts, 'text_add'));
00917                 $this->_view->assign('formfield', $this->input_file($name, $value, $opts));
00918 
00919                 return $this->_view->render();
00920         }
00921 
00934         public function input_file($name, $value, $opts=null) {
00935 
00936                 $required = gOpt($opts, 'required', false);
00937                 if($value) $opts['required'] = false;
00938 
00939                 $buffer = $this->input($name, 'file', $value, $opts);
00940 
00941                 $rel_path = gOpt($opts, 'rel_path') ? (substr(gOpt($opts, 'rel_path'), -1)=='/' ? gOpt($opts, 'rel_path') : gOpt($opts, 'rel_path').'/') : null;
00942 
00943                 if($value) {
00944                         $buffer .= "<input type=\"hidden\" name=\"old_$name\" value=\"$value\" />\n";
00945                         $buffer .= "<div style=\"margin-top:5px;\">";
00946                         if(!$required) $buffer .= $this->checkbox("del_".$name, false, 1)." ".__("delete")." ";
00947                         $file_size = $rel_path ? filesize(preg_replace("#".preg_quote(ROOT)."#", "", ABS_ROOT).preg_replace("#/#", DS, $rel_path.$value)) : null;
00948                         if(gOpt($opts, 'preview') && $rel_path) 
00949                                 $value = "<a title=\"$value\" href=\"".$rel_path.$value."\">$value</a><script>$$('a[href=".$rel_path.$value."]')[0].cerabox();</script>";
00950                         $buffer .= $file_size ? sprintf(__("chargedFileFormWithSize"), $value, round($file_size/1024, 1)." Kb") : sprintf(__("chargedFileForm"), $value);
00951                         $buffer .= "</div>\n";
00952                 }
00953 
00954                 return $buffer;
00955 
00956         }
00957 
00979         public function uploadFile($name, $valid_extension, $path, $link_error, $opts) {
00980         
00981                 $path = substr($path, -1) == DS ? $path : $path.DS;
00982 
00983                 if(!is_dir($path)) mkdir($path, 0755, true);
00984                 
00985                 $def_contents = array(
00986                         "text/plain",
00987                         "text/html",
00988                         "text/xml",
00989                         "image/jpeg",
00990                         "image/pjpeg",
00991                         "image/gif",
00992                         "image/png",
00993                         "image/bmp",
00994                         "video/mpeg",
00995                         "audio/midi",
00996                         "application/pdf",
00997                         "application/msword",
00998                         "application/x-compressed",
00999                         "application/x-gtar",
01000                         "application/x-gzip",
01001                         "multipart/x-gzip",
01002                         "application/x-zip-compressed",
01003                         "application/vnd.ms-excel",
01004                         "application/x-msdos-program",
01005                         "application/octet-stream"
01006                 );
01007 
01008                 $error_query = gOpt($opts, 'error_query', null);
01009                 $check_content = gOpt($opts, 'check_content', true);
01010                 $contents_allowed = gOpt($opts, 'contents', $def_contents); 
01011                 $prefix = gOpt($opts, 'prefix', '');
01012                 $max_file_size = gOpt($opts, 'max_file_size', null);
01013 
01014                 if(isset($_FILES[$name]['name']) && $_FILES[$name]['name']) {
01015                         $nfile_size = $_FILES[$name]['size'];
01016                         if($max_file_size && $nfile_size>$max_file_size) {
01017                                 if($error_query) $this->registry->db->executeQuery($error_query);
01018                                 exit(error::errorMessage(array('error'=>__("MaxSizeError")), $link_error));
01019                         }
01020                         $tmp_file = $_FILES[$name]['tmp_name'];
01021                         $nfile = $this->setFileName($name, $path, $prefix); 
01022 
01023                         if(!$this->checkExtension($nfile, $valid_extension) || preg_match('#%00#', $nfile) || ($check_content && !in_array( $_FILES[$name]['type'], $contents_allowed))) {
01024                                 if($error_query) $this->registry->db->executeQuery($error_query);
01025                                 exit(error::errorMessage(array('error'=>__("FileConsistentError")), $link_error));
01026                         }
01027 
01028                 }
01029                 else { $nfile = ''; $tmp_file = ''; }
01030 
01031                 $del_file = isset($this->_requestVars['del_'.$name]) && $this->_requestVars['del_'.$name];
01032 
01033                 $upload = $delete = false;
01034 
01035                 $upload = !empty($nfile);
01036                 $delete = (!empty($nfile) && !empty($this->_requestVars['old_'.$name])) || $del_file;
01037                 
01038                 if($delete) {
01039                         if(is_file($path.$this->_requestVars['old_'.$name]))    
01040                                 if(!@unlink($path.$this->_requestVars['old_'.$name])) {
01041                                         if($error_query) $this->registry->db->executeQuery($error_query);
01042                                         exit(error::errorMessage(array('error'=>__("CantDeleteUploadedFileError")), $link_error));
01043                                 }
01044 
01045                 }
01046 
01047                 if($upload) {
01048                         if(!$this->upload($tmp_file, $nfile, $path)) { 
01049                                 if($error_query) $this->registry->db->executeQuery($error_query);
01050                                 exit(error::errorMessage(array('error'=>__("CantUploadError")), $link_error));
01051                         }
01052                 }
01053 
01054                 if($upload) return $nfile;
01055                 elseif($delete) return '';
01056                 else return $this->_requestVars['old_'.$name];
01057 
01058         }
01059         
01090         public function uploadImage($name, $valid_extension, $path, $link_error, $opts) {
01091         
01092                 $path = substr($path, -1) == DS ? $path : $path.DS;
01093 
01094                 if(!is_dir($path)) mkdir($path, 0755, true);
01095                 
01096                 $def_contents = array(
01097                         "image/jpeg",
01098                         "image/pjpeg",
01099                         "image/gif",
01100                         "image/png"
01101                 );
01102 
01103                 $error_query = gOpt($opts, 'error_query', null);
01104                 $check_content = gOpt($opts, 'check_content', true);
01105                 $contents_allowed = gOpt($opts, 'contents', $def_contents); 
01106                 $prefix = gOpt($opts, 'prefix', '');
01107                 $prefix_thumb = gOpt($opts, 'prefix_thumb', 'thumb_');
01108                 $make_thumb = gOpt($opts, 'make_thumb', false);
01109                 $resize = gOpt($opts, 'resize', false);
01110                 $scale = gOpt($opts, 'scale', false);
01111                 $resize_enlarge = gOpt($opts, 'resize_enlarge', false);
01112                 $resize_width = gOpt($opts, 'resize_width', null);
01113                 $resize_height = gOpt($opts, 'resize_height', null);
01114                 $thumb_width = gOpt($opts, 'thumb_width', null);
01115                 $thumb_height = gOpt($opts, 'thumb_height', null);
01116                 $max_file_size = gOpt($opts, 'max_file_size', null);
01117 
01118                 if(isset($_FILES[$name]['name']) && $_FILES[$name]['name']) {
01119                         $nfile_size = $_FILES[$name]['size'];
01120                         if($max_file_size && $nfile_size>$max_file_size) {
01121                                 if($error_query) $this->registry->db->executeQuery($error_query);
01122                                 exit(error::errorMessage(array('error'=>__("MaxSizeError")), $link_error));
01123                         }
01124                         $tmp_file = $_FILES[$name]['tmp_name'];
01125                         $nfile = $this->setFileName($name, $path, $prefix); 
01126 
01127                         if(!$this->checkExtension($nfile, $valid_extension) || preg_match('#%00#', $nfile) || ($check_content && !in_array( $_FILES[$name]['type'], $contents_allowed))) {
01128                                 if($error_query) $this->registry->db->executeQuery($error_query);
01129                                 exit(error::errorMessage(array('error'=>__("FileConsistentError")), $link_error));
01130                         }
01131 
01132                 }
01133                 else { $nfile = ''; $tmp_file = ''; }
01134 
01135                 $del_file = isset($this->_requestVars['del_'.$name]) && $this->_requestVars['del_'.$name];
01136 
01137                 $upload = $delete = false;
01138 
01139                 $upload = !empty($nfile);
01140                 $delete = (!empty($nfile) && !empty($this->_requestVars['old_'.$name])) || $del_file;
01141                 
01142                 if($delete) {
01143                         if(is_file($path.$this->_requestVars['old_'.$name]))    
01144                                 if(!@unlink($path.$this->_requestVars['old_'.$name])) {
01145                                         if($error_query) $this->registry->db->executeQuery($error_query);
01146                                         exit(error::errorMessage(array('error'=>__("CantDeleteUploadedFileError")), $link_error));
01147                                 }
01148                         if($make_thumb) {
01149                                 $old_file_thumb = $prefix_thumb.$this->_requestVars['old_'.$name];
01150                                 
01151                                 if(is_file($path.$old_file_thumb))      
01152                                         if(!@unlink($path.$old_file_thumb)) {
01153                                                 if($error_query) $this->registry->db->executeQuery($error_query);
01154                                                 exit(error::errorMessage(array('error'=>__("CantDeleteUploadedFileError")), $link_error));
01155                                         }
01156                         }
01157 
01158                 }
01159 
01160                 if($upload) {
01161                         
01162                         if(!$this->upload($tmp_file, $nfile, $path)) { 
01163                                 if($error_query) $this->registry->db->executeQuery($error_query);
01164                                 exit(error::errorMessage(array('error'=>__("CantUploadError")), $link_error));
01165                         }
01166                         
01167                         $image = new image();
01168                         $image->load($path.$nfile);
01169 
01170                         if($resize) {
01171                                 $opts = array("enlarge"=>$resize_enlarge);
01172                                 if($resize_width && $resize_height) {
01173                                         $image->resize($resize_width, $resize_height, $opts);   
01174                                 }
01175                                 elseif($resize_width) {
01176                                         $image->resizeToWidth($resize_width, $opts);    
01177                                 }
01178                                 elseif($resize_height) {
01179                                         $image->resizeToHeight($resize_height, $opts);  
01180                                 }
01181                         }
01182                         elseif($scale) {
01183                                 $image->scale($scale);  
01184                         }
01185                         
01186                         $image->save($path.$nfile, $image->type());
01187 
01188                         if($make_thumb) {
01189                                 $nthumbfile = $prefix_thumb.$nfile; 
01190                                 $opts = array("enlarge"=>true);
01191                                 if($thumb_width && $thumb_height) {
01192                                         $image->resize($thumb_width, $thumb_height, $opts);     
01193                                 }
01194                                 elseif($thumb_width) {
01195                                         $image->resizeToWidth($thumb_width, $opts);     
01196                                 }
01197                                 elseif($thumb_height) {
01198                                         $image->resizeToHeight($thumb_height, $opts);   
01199                                 }
01200 
01201                                 $image->save($path.$nthumbfile, $image->type());
01202                         }
01203                 }
01204 
01205                 if($upload) return $nfile;
01206                 elseif($delete) return '';
01207                 else return $this->_requestVars['old_'.$name];
01208 
01209         }
01210 
01221         private function setFileName($name, $path, $prefix) {
01222         
01223                 $init_name = $_FILES[$name]['name'];
01224                 $n_name = preg_replace("#[^a-zA-Z0-9_\.-]#", "_", $prefix.$init_name);
01225 
01226                 $p_files = scandir($path);
01227 
01228                 $i=1;
01229                 while(in_array($n_name, $p_files)) { $n_name = substr($n_name, 0, strrpos($n_name, '.')+1).$i.substr($n_name, strrpos($n_name, '.')); $i++; }
01230 
01231                 return $n_name;
01232 
01233         }
01234 
01242         private function checkExtension($filename, $valid_extension) {
01243         
01244                 if(!$valid_extension) return true;
01245 
01246                 $fa = explode(".", $filename);
01247                 $extension = end($fa);
01248 
01249                 if(!in_array($extension, $valid_extension)) return false;
01250                 return true;
01251         
01252         } 
01253 
01262         private function upload($tmp_file, $filename, $path) {
01263         
01264                 $file = $path.$filename;
01265                 return move_uploaded_file($tmp_file, $file) ? true : false;
01266 
01267         }
01268 
01269 }
01270 
01271 ?>