[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/inc/ -> class.vfs_mimetypes.inc.php (source)

   1  <?php
   2    /***************************************************************************\
   3    * eGroupWare - File Manager                                                 *
   4    * http://www.egroupware.org                                                 *
   5    * Written by:                                                               *
   6    *  - Vinicius Cubas Brand <viniciuscb@users.sourceforge.net>                *
   7    *  sponsored by Thyamad - http://www.thyamad.com                            *
   8    * ------------------------------------------------------------------------- *
   9    * Description: Mime type class handler for VFS (SQL implementation v2)      *
  10    * ------------------------------------------------------------------------- *
  11    *  This program is free software; you can redistribute it and/or modify it  *
  12    *  under the terms of the GNU General Public License as published by the    *
  13    *  Free Software Foundation; either version 2 of the License, or (at your   *
  14    *  option) any later version.                                               *
  15    \***************************************************************************/
  16  
  17      #based on the filetypes.inc.php class by intermesh, see groupoffice.com
  18  
  19      #02 Sep 2004 viniciuscb Initial Release
  20  
  21      class vfs_mimetypes
  22      {
  23          var $default_filetype_icon;
  24          var $db;
  25          var $vfs; //Just to get mime types from vfs->get_ext_mime_type()
  26  
  27          var $default_mimetype = "application/OCTET-STREAM";
  28  
  29  		function vfs_mimetypes($create_vfs=true)
  30          {
  31              //use create_vfs=false and after this use $this->set_vfs to keep the
  32              // same object (i.e. use reference) in $this->vfs
  33              if ($create_vfs)
  34              {
  35                  $this->vfs =& CreateObject('phpgwapi.vfs');
  36              }
  37  
  38              $this->default_filetype_icon = PHPGW_INCLUDE_ROOT.'/filescenter/icons/default.gif';
  39  
  40              $this->db = clone($GLOBALS['phpgw']->db);
  41              
  42          }
  43  
  44          /*!
  45           * function add_filetype
  46           * @description Adds a new mime_type to the mime_types repository
  47           * @note One of the two: extension or mime are required
  48           * @param extension string  A file extension
  49           * @param mime string  the mime type according to the RFCs 2046/1524
  50           * @param friendly string  File type description in human-friendly
  51           *                                                               format
  52           * @param image string content of icon file
  53           * @param icon_name string complete path filename of icon image
  54           *      (note: will be used only when icon_data is not set)
  55           * @param mime_magic data used for mime_magic functions (planned)
  56           * @param return_image bool if true will return the binary data of image
  57           * @result Array with mime_id and other mime info, false otherwise
  58           */
  59  		function add_filetype($data,$return_image=false,$dont_update=false)
  60          {
  61              if (!$data['extension'] && !$data['mime'])
  62              {
  63                  return false;
  64              }
  65  
  66  
  67              if (!$data['mime'])
  68              {
  69                  $data['mime'] = $this->get_mime_type($data['extension']);
  70              }
  71              elseif (!$data['extension'])
  72              {
  73                  $data['extension'] = '(n/a)'; //Thinking in 'Directory'
  74              }
  75  
  76              if (!$data['friendly'])
  77              {
  78                  if ($data['extension'] != '(n/a)')
  79                  {
  80                      $data['friendly'] = strtoupper($data['extension']).' File';
  81                  }
  82                  else
  83                  {
  84                      $data['friendly'] = $data['mime'];
  85                  }
  86                  
  87              }
  88  
  89              if (!$data['image'])
  90              {
  91                  if (!$data['icon_name'] || !file_exists($data['icon_name']))
  92                  {
  93                      $data['icon_name'] = $this->default_filetype_icon;
  94                  }
  95                  $fp = fopen($data['icon_name'],"r");
  96                  $data['image'] = fread($fp,filesize($data['icon_name']));
  97                  fclose($fp);
  98  
  99                  $data['image'] = $this->db->quote($data['image'],'blob');
 100  
 101                  unset($data['icon_name']);
 102              }
 103  
 104  
 105              $where['extension'] = $data['extension'];
 106              #$where['mime'] = $data['mime'];
 107  
 108              if ($dont_update)
 109              {
 110                  $this->db->select('phpgw_vfs2_mimetypes','mime_id',$where,__LINE__,__FILE__);
 111                  if ($this->db->next_record())
 112                  {
 113                      return false; //msgerror Existent register that cannot be overwritten
 114                  }
 115              }
 116  
 117  
 118              $res = $this->db->insert('phpgw_vfs2_mimetypes',$data,$where,__LINE__,__FILE__);
 119              
 120  
 121              if($res)
 122              { 
 123                  $this->db->select('phpgw_vfs2_mimetypes','mime_id',$where,
 124                      __LINE__,__FILE__);
 125  
 126                  $this->db->next_record();
 127          
 128                  $data['mime_id'] = $this->db->Record['mime_id'];
 129  
 130                  if (!$return_image)
 131                  {
 132                      unset($data['image']);
 133                  }
 134  
 135                  return $data;
 136              }
 137  
 138              return false;
 139          }
 140  
 141          /*!
 142           * function edit_filetype
 143           * @description Edits a mime_type of the mime_types repository
 144           * @note mime_id required. Will change only passed vars.
 145           * @param extension string  A file extension
 146           * @param mime string  the mime type according to the RFCs 2046/1524
 147           * @param friendly string  File type description in human-friendly
 148           *                                                               format
 149           * @param image string content of icon file
 150           * @param icon_name string complete path filename of icon image
 151           *      (note: will be used only when icon_data is not set)
 152           * @param mime_magic data used for mime_magic functions (planned)
 153           * @param return_image bool if true will return the binary data of image
 154           * @result Array with mime_id and other mime info, false otherwise
 155           */
 156  		function edit_filetype($data,$return_image=false)
 157          {
 158  
 159              if (!$data['mime_id'])
 160              {
 161                  return false;
 162              }
 163  
 164              $where['mime_id'] = $data['mime_id'];
 165  
 166              if ($data['image'])
 167              {
 168                  $data['image'] = $this->db->quote($data['image'],'blob');
 169              }
 170  
 171              $res = $this->db->update('phpgw_vfs2_mimetypes',$data,$where,__LINE__,__FILE__);
 172              
 173  
 174              if($res)
 175              { 
 176                  if ($return_image)
 177                  {
 178                      $return_fields = '*';
 179                  }
 180                  else
 181                  {
 182                      $return_fields = 'mime_id,extension,mime,friendly,mime_magic,proper_id';
 183                  }
 184                  $this->db->select('phpgw_vfs2_mimetypes',$return_fields,$where,
 185                      __LINE__,__FILE__);
 186  
 187                  $this->db->next_record();
 188          
 189                  return $this->db->Record;
 190              }
 191  
 192              return false;
 193          }
 194  
 195  
 196  
 197  
 198          /*!
 199           * function get_type
 200           * @description Returns a mime type, based in extension or mime_id
 201           * @param $desc array  have index 'extension', 'mime_id' or 'mime'
 202           * @param $return_image  if true will return the binary data of image
 203           * @result Array with mime_id and other mime info, false otherwise
 204           */
 205  		function get_type($data, $return_image=false)
 206          {
 207              //TODO error messages
 208              if ((!$data['extension'] || $data['extension'] == '(n/a)') &&
 209                  !$data['mime_id'] && !$data['mime'])
 210                  return false;
 211  
 212  
 213              $return_fields = ($return_image)?'*':'mime_id,extension,friendly,mime_magic,mime,proper_id';
 214  
 215              if ($data['mime_id'])
 216              {
 217                  $this->db->select('phpgw_vfs2_mimetypes',$return_fields,
 218                      array('mime_id'=>$data['mime_id']),__LINE__,__FILE__);
 219              }
 220              else if ($data['extension'])
 221              {
 222                  $this->db->select('phpgw_vfs2_mimetypes',$return_fields,
 223                      array('extension'=>$data['extension']),__LINE__,__FILE__);
 224              }
 225              else if ($data['mime'])
 226              {
 227                  $this->db->select('phpgw_vfs2_mimetypes',$return_fields,
 228                      array('mime'=>$data['mime']),__LINE__,__FILE__);
 229              }
 230      
 231              if ($data['extension'] && $data['mime'])
 232              {
 233                  //if there is extension and mime specified and nothing was found only with extension, search mime
 234                  if ($this->db->next_record())
 235                      return $this->db->Record;
 236                  else
 237                  {
 238                      $this->db->select('phpgw_vfs2_mimetypes',$return_fields,
 239                          array('mime'=>$data['mime']),__LINE__,__FILE__);
 240  
 241                      if ($this->db->next_record())
 242                          return $this->db->Record;
 243                  }
 244              }
 245              else
 246              {
 247                  if ($this->db->next_record())
 248                      return $this->db->Record;
 249              }
 250  
 251              return false;
 252  
 253          }
 254  
 255          /*!
 256           * function get_filetypes
 257           * @description Returns an array with all file types in the repository
 258           * @param $return_images if true, the images are returned in the array
 259           * @param $offset int if specified, will bring a subset of repository
 260           * @result Array: $arr[1]['mime_id'] ...
 261           */
 262  		function get_filetypes($return_images=false,$offset=false)
 263          {
 264              $return_fields = ($return_images)?'*':'mime_id,extension,friendly,mime_magic,mime,proper_id';
 265  
 266              $this->db->select('phpgw_vfs2_mimetypes',$return_fields,false,
 267                  __LINE__,__FILE__,$offset);
 268  
 269              $result = array();
 270              while ($this->db->next_record())
 271              {
 272                  $result[] = $this->db->Record;
 273              }
 274              return $result;
 275          }
 276  
 277          /*!
 278           * function update_filetype
 279           * @description Updates a file type information in the filetype
 280           *  repository
 281           * @param $return_image  if true will return the binary data of image
 282           * @note parameters in $data: the same of add_filetype()
 283           * @result bool true on success, false otherwise
 284           */
 285  		function update_filetype($data)
 286          {
 287              if (!$data['mime_id'])
 288              {
 289                  return false;
 290              }
 291  
 292              if ($data['icon_name'])
 293              {
 294                  if (file_exists($data['icon_name']))
 295                  {
 296                      $fp = fopen($data['icon_name'],"r");
 297                      $data['image'] = fread($fp,filesize($icon));
 298                      fclose($fp);
 299                  }
 300  
 301                  $data['image'] = $this->db->quote($data['image'],'blob');
 302  
 303                  unset($data['icon_name']);
 304              }
 305  
 306      
 307              $where['mime_id'] = $data['mime_id'];
 308  
 309              if ($this->db->update('phpgw_vfs2_mimetypes',$data,$where,__LINE__,
 310                  __FILE__))
 311                  return true;
 312  
 313              return false;
 314          }
 315  
 316  
 317          /*!
 318           * function delete_filetype
 319           * @description deletes a type from the file type repository
 320           * @param $mime_id  the mime_id of the record
 321           * @result bool true on success, false otherwise
 322           */
 323  		function delete_filetype($mime_id)
 324          {
 325              if ($this->db->delete('phpgw_vfs2_mimetypes',
 326                  array('mime_id'=>$mime_id),__LINE__,__FILE__))
 327                  return true;
 328  
 329              return false;
 330          }
 331  
 332  		function set_vfs(&$vfs)
 333          {
 334              $this->vfs =& $vfs;
 335          }
 336  
 337          #private methods
 338  
 339  		function get_mime_type($extension)
 340          {
 341              return $this->vfs->get_ext_mime_type(array('string'=>$extension));
 342          }
 343  
 344  		function default_values ($data, $default_values)
 345          {
 346              for ($i = 0; list ($key, $value) = each ($default_values); $i++)
 347              {
 348                  if (!isset ($data[$key]))
 349                  {
 350                      $data[$key] = $value;
 351                  }
 352              }
 353  
 354              return $data;
 355          }
 356  
 357      }
 358  
 359  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7