[ Index ]

PHP Cross Reference of Quick Form DataBase

title

Body

[close]

/objects/ -> qfdbobj_file.php (source)

   1  <?php
   2  if (!defined('QFDB_SECURITY')) {
   3      die("You can't access this file directly...");
   4  }
   5  
   6  
   7  
   8  class qfdbobj_file {
   9       // 0.4
  10      public static function obj_struct(&$qfdb)
  11      {
  12          $qfdb->m['object'] = 'file'; // string (fixed)
  13          $qfdb->m['status'] = '1234'; // string
  14          $qfdb->m['if'] = ''; // string
  15          $qfdb->m['alias'] = ''; // string
  16  
  17          $qfdb->m['db_field'] = ''; // string
  18          $qfdb->m['form_field'] = ''; // string
  19  
  20  
  21  
  22          $qfdb->m['required_message'] = "Você deve selecionar um arquivo para upload.";
  23  
  24          $qfdb->m['max_file_size'] = '100000'; // string 100000, 100k, 1m
  25          $qfdb->m['max_file_size_message'] = "O tamanho máximo do arquivo para upload é de QFDB_MAX_FILE_SIZE bytes.";
  26  
  27          //      literal   =>  força um nome do arquivo
  28          // __LITERAL__  => usa o mesmo nome do arquivo no computador do usuário
  29          // __FIX__  => usa como base o nome do arquivo apenas com caracteres básicos [\.a-zA-Z0-9_-]
  30          // __FIX_DB__  => se status == 2, usa __WEB__ e se status == 4, nome do DB
  31          // __LITERAL_DB__  => se status == 2, usa __LITERAL, e se status == 4, nome do DB literal   =>  força um nome do arquivo
  32          // __TMP__  => gera um nome único de arquivo e retorna o nome
  33          $qfdb->m['filename'] = ''; // string-> literal ou constantes:
  34          $qfdb->m['dir'] = '';
  35  
  36          $qfdb->m['extension'] = ''; // extensoes aceitas, separadas por virgula
  37          $qfdb->m['extension_message'] = "Somente os seguintes tipos de arquivos são aceitos: FILE_EXTENSION .";
  38  
  39          $qfdb->m['thumbnail'] = 'false'; // exibir miniatura
  40          $qfdb->m['thumbnail_width'] = 'auto'; // largura da miniatura
  41          $qfdb->m['thumbnail_height'] = 'auto'; // altura da miniatura
  42  
  43          $qfdb->m['title'] = ''; // string
  44  
  45          $qfdb->m['id'] = ''; // string
  46          $qfdb->m['size'] = '30'; // int
  47          $qfdb->m['value'] = ''; // string
  48          $qfdb->m['disabled'] = 'false'; // bool
  49          $qfdb->m['readonly'] = 'false'; // bool
  50          $qfdb->m['required'] = 'false'; // bool
  51          $qfdb->m['style'] = ''; // string
  52          $qfdb->m['class'] = $qfdb->get_css_value('qfdb_file'); // string
  53          $qfdb->m['help'] = ''; // string
  54          $qfdb->m['others'] = '';  // string
  55          $qfdb->m['before'] = ''; // string
  56          $qfdb->m['after'] = ''; // string
  57  
  58          $qfdb->m['label'] = ''; // string
  59          $qfdb->m['label_class'] = $qfdb->get_css_value('qfdb_tpl_label'); // string
  60          $qfdb->m['label_others'] = ''; // string
  61  
  62          $qfdb->m['display'] = 'two_cell'; //  string
  63  
  64          $qfdb->m['colspan_class'] = $qfdb->get_css_value('qfdb_tpl_colspan'); //  string
  65          $qfdb->m['colspan_others'] = ''; //  string
  66  
  67          $qfdb->m['left_class'] = $qfdb->get_css_value('qfdb_tpl_left'); // string
  68          $qfdb->m['left_others'] = ''; // string
  69          $qfdb->m['right_class'] = $qfdb->get_css_value('qfdb_tpl_right'); // string
  70          $qfdb->m['right_others'] = ''; // string
  71  
  72          $qfdb->m['validate'] = ''; // array
  73  
  74          $qfdb->m['object_properties'] = count($qfdb->m) +2; // int
  75      } // end func
  76  
  77      // 0.3
  78      public static function obj_html(&$qfdb)
  79      {
  80          $object = '';
  81          // build the file qfdb object
  82          if (!defined('QFDBOBJ_FILE_MAXFS')) {
  83              $object .= '<input type="hidden" name="MAX_FILE_SIZE" value="'.self::get_max_file_size( $qfdb->m['max_file_size']).'" />'."\n";
  84              define('QFDBOBJ_FILE_MAXFS', true);
  85          }
  86  
  87          $object .= $qfdb->i(2).'<input type="file"';
  88          if ($qfdb->m['form_field']) {
  89              $object .= ' name="'.$qfdb->m['form_field'].'"';
  90          }
  91          if ($qfdb->m['id']) {
  92              $object .= ' id="'.$qfdb->m['id'].'"';
  93          } elseif ($qfdb->m['form_field']) {
  94              $object .= ' id="'.$qfdb->m['form_field'].'"';
  95          }
  96          if ($qfdb->m['style']) {
  97              $object .= ' style="'.$qfdb->m['style'].'"';
  98          }
  99          if ($qfdb->m['size']) {
 100              $object .= ' size="'.$qfdb->m['size'].'"';
 101          }
 102          if ($qfdb->m['class']) {
 103              $object .= ' class="'.$qfdb->m['class'].'"';
 104          }
 105          if ($qfdb->m['title']) {
 106              $object .= ' title="'.$qfdb->m['title'].'"';
 107          }
 108          if ($qfdb->m['disabled']) {
 109              $object .= ' disabled="disabled"';
 110          }
 111          if ($qfdb->m['readonly']) {
 112              $object .= ' readonly="readonly"';
 113          }
 114          $object .= $qfdb->m['others'];
 115          if ($qfdb->m['before']) {
 116              $qfdb->m['before'] = $qfdb->m['before']. "\n    ";
 117          }
 118          if ($qfdb->m['after']) {
 119              $qfdb->m['after'] = "\n    ".$qfdb->m['after'];
 120          }
 121          $object .= ' /> ';
 122          $object = $qfdb->m['before'] . $object . $qfdb->m['after'];
 123  
 124          if ($qfdb->m['display'] == 'object_only') {
 125              $qfdb->add_output('html', $object."\n");
 126          } elseif ($qfdb->m['display'] == 'one_cell') {
 127              $label = qfdb_themes::themes_label($qfdb->m);
 128              $qfdb->add_output('html', qfdb_themes::themes_container_one_cell( array('content'=>$object, 'others'=>$qfdb->m['colspan_others'], 'class'=>$qfdb->m['colspan_class'], 'label'=>$label) ));
 129          } else { // $qfdb->m['display'] == 'two_cell'
 130              $label = qfdb_themes::themes_label($qfdb->m);
 131              $qfdb->add_output('html',  qfdb_themes::themes_container_two_cells( array('label'=>$label, 'content'=>$object, 'label_others'=>$qfdb->m['left_others'], 'content_others'=>$qfdb->m['right_others'], 'label_class'=>$qfdb->m['left_class'], 'content_class'=>$qfdb->m['right_class']) ));
 132          }
 133      } // end func
 134  
 135  
 136      // 0.3
 137      public static function obj_db(&$qfdb)
 138      {
 139          if (!isset($_FILES[ $qfdb->m['form_field'] ])) {
 140              $qfdb->add_critical_error('Você deve especificar a propriedade "form_field" para o Objeto QFDB "file". ');
 141              return;
 142          }
 143  
 144          // verify directory
 145          $qfdb->m['dir'] = trim($qfdb->m['dir']);
 146          if (!$qfdb->m['dir']) {
 147              $qfdb->add_critical_error("É necessário especificar o diretório para upload de arquivo.");
 148              return;
 149          }
 150  
 151          if (!is_dir($qfdb->m['dir'])) {
 152              $qfdb->add_critical_error( sprintf("O diretório '%s' não existe ou não é um diretório.", realpath($qfdb->m['dir'])) );
 153              return;
 154          }
 155          clearstatcache();
 156  
 157          $qfdb->m['filename'] = trim($qfdb->m['filename']);
 158  
 159          // get $_FILE var
 160          $file['name'] = $_FILES[ $qfdb->m['form_field'] ]['name'];
 161          $file['type'] = $_FILES[ $qfdb->m['form_field'] ]['type'];
 162          $file['size'] = $_FILES[ $qfdb->m['form_field'] ]['size'];
 163          $file['tmp_name'] = $_FILES[ $qfdb->m['form_field'] ]['tmp_name'];
 164          $file['error'] = $_FILES[ $qfdb->m['form_field'] ]['error'];
 165  
 166  
 167          // get file extension
 168          $file_ext = substr($file['name'], strrpos($file['name'], '.'));
 169  
 170          // verify if has uploaded file
 171          if ($file['error'] == UPLOAD_ERR_NO_FILE) {  // none file select
 172              $qfdb->remove_alias();
 173              return;
 174          } elseif ($file['error'] == UPLOAD_ERR_INI_SIZE) { // upload file great then upload_max_filesize in php.ini
 175              $qfdb->add_user_error( str_replace('QFDB_MAX_FILE_SIZE', ini_get('upload_max_filesize'), $qfdb->m['max_file_size_message']));
 176              return;
 177          } elseif ($file['error'] == UPLOAD_ERR_PARTIAL) { // upload only part of file
 178              $qfdb->add_user_error('Ocorreu um erro ao enviar o arquivo. Tente novamente!');
 179              return;
 180          }
 181          $aux = $qfdb->m['max_file_size'];
 182          $max_file_size = self::get_max_file_size($qfdb->m['max_file_size']);
 183  
 184          if ($file['size'] > $max_file_size || $file['error'] == UPLOAD_ERR_FORM_SIZE) {
 185              $qfdb->add_user_error( str_replace('QFDB_MAX_FILE_SIZE', $aux, $qfdb->m['max_file_size_message']));
 186              return;
 187          }
 188  
 189          // verify error
 190          if (!$file['name'] && $qfdb->get_status() == 2 && $qfdb->m['required']) {
 191              $qfdb->add_user_error( $qfdb->m['required_message']);
 192              return;
 193          }
 194  
 195          if ($qfdb->m['filename'] == '__LITERAL__') {
 196              $qfdb->m['value'] = $file['name'];
 197          } elseif ($qfdb->m['filename'] == '__FIX__') {
 198              $qfdb->m['value'] = ereg_replace('[^0-9a-zA-Z._-]', '_', $file['name']);
 199          } elseif ($qfdb->m['filename'] == '__LITERAL_DB__') {
 200              if ($qfdb->get_status() == 2) {
 201                  $qfdb->m['value'] = $file['name'];
 202              }
 203          } elseif ($qfdb->m['filename'] == '__FIX_DB__') {
 204              if ($qfdb->get_status() == 2) {
 205                  $qfdb->m['value'] = ereg_replace('[^0-9a-zA-Z._-]', '_', $file['name']);
 206              }
 207          } elseif ($qfdb->m['filename'] == '__TMP__') {
 208              $qfdb->m['value'] = tempnam ($qfdb->m['dir'], '');
 209              rename($qfdb->m['value'], $qfdb->m['value'].$file_ext);
 210              $qfdb->m['value'] .= $file_ext;
 211          } else {
 212              $qfdb->m['value'] = $qfdb->m['filename'].$file_ext;
 213          }
 214  
 215          // register var name to used in callback function(after)
 216          $qfdb->add_var('qfdb_file_'.$qfdb->m['form_field'], $qfdb->m['value']);
 217  
 218          // fix extension
 219          $aux = $qfdb->m['extension'];
 220          $qfdb->m['extension'] = ereg_replace('[^a-z,0-9]', '', strtolower($qfdb->m['extension']));
 221          if ($qfdb->m['extension']) {
 222              $qfdb->m['extension'] = ','.$qfdb->m['extension'].',';
 223              $ext = ','.substr(strtolower($file_ext), 1).',';
 224              if (strpos($qfdb->m['extension'], $ext) === false) {
 225                  $qfdb->add_user_error( str_replace('FILE_EXTENSION', $aux, $qfdb->m['extension_message']) );
 226                  return;
 227              }
 228          }
 229  
 230          // move file to $qfdb->m['dir']
 231          if (!@move_uploaded_file($file['tmp_name'], $qfdb->m['dir'].'/'.$qfdb->m['value'])) {
 232              $qfdb->add_user_error('Não foi possível mover fazer o upload do arquivo.');
 233              return;
 234          }
 235      } // end func
 236  
 237  
 238      // 0.3
 239      public static static function obj_debug(&$qfdb)
 240      {
 241      } // end func
 242  
 243  
 244      // 0.4
 245      public static function obj_fixed(&$qfdb)
 246      {
 247          $qfdb->m['id'] = ($qfdb->m['id']) ? $qfdb->m['id'] : $qfdb->m['form_field'];
 248  
 249          $qfdb->fix_disabled();
 250          $qfdb->fix_readonly();
 251          $qfdb->fix_required();
 252          $qfdb->fix_others();
 253          $qfdb->fix_display_3();
 254      } // end func
 255  
 256      // 0.5
 257      public static function get_max_file_size($max_fs_aux) {
 258          $max_fs = intval($max_fs_aux);
 259          $max_fs_modifier = strtoupper(substr( trim($max_fs_aux), -1));
 260  
 261          if ($max_fs_modifier === 'K') {
 262              $max_fs *= 1024;
 263          } elseif ($max_fs_modifier === 'M') {
 264              $max_fs *= 1024*1024;
 265          }
 266          return intval($max_fs);
 267      } // end func
 268  
 269  
 270  } // end class


Generated: Fri Nov 14 17:48:13 2008 Cross-referenced by PHPXref 0.7