File indexing completed on 2025-03-09 05:24:42

0001 <?php
0002 
0003 /**
0004  * Simple elFinder driver for MySQL.
0005  *
0006  * @author Dmitry (dio) Levashov
0007  **/
0008 class elFinderVolumeMySQL extends elFinderVolumeDriver {
0009   
0010   /**
0011    * Driver id
0012    * Must be started from letter and contains [a-z0-9]
0013    * Used as part of volume id
0014    *
0015    * @var string
0016    **/
0017   protected $driverId = 'm';
0018   
0019   /**
0020    * Database object
0021    *
0022    * @var mysqli
0023    **/
0024   protected $db = null;
0025   
0026   /**
0027    * Tables to store files
0028    *
0029    * @var string
0030    **/
0031   protected $tbf = '';
0032   
0033   /**
0034    * Directory for tmp files
0035    * If not set driver will try to use tmbDir as tmpDir
0036    *
0037    * @var string
0038    **/
0039   protected $tmpPath = '';
0040   
0041   /**
0042    * Numbers of sql requests (for debug)
0043    *
0044    * @var int
0045    **/
0046   protected $sqlCnt = 0;
0047   
0048   /**
0049    * Last db error message
0050    *
0051    * @var string
0052    **/
0053   protected $dbError = '';
0054   
0055   /**
0056    * Constructor
0057    * Extend options with required fields
0058    *
0059    * @return void
0060    * @author Dmitry (dio) Levashov
0061    **/
0062   public function __construct() {
0063     $opts = array(
0064       'host'          => 'localhost',
0065       'user'          => '',
0066       'pass'          => '',
0067       'db'            => '',
0068       'port'          => null,
0069       'socket'        => null,
0070       'files_table'   => 'elfinder_file',
0071       'tmbPath'       => '',
0072       'tmpPath'       => ''
0073     );
0074     $this->options = array_merge($this->options, $opts);
0075     $this->options['mimeDetect'] = 'internal';
0076   }
0077   
0078   /*********************************************************************/
0079   /*                        INIT AND CONFIGURE                         */
0080   /*********************************************************************/
0081   
0082   /**
0083    * Prepare driver before mount volume.
0084    * Connect to db, check required tables and fetch root path
0085    *
0086    * @return bool
0087    * @author Dmitry (dio) Levashov
0088    **/
0089   protected function init() {
0090     
0091     if (!($this->options['host'] || $this->options['socket'])
0092     ||  !$this->options['user'] 
0093     ||  !$this->options['pass'] 
0094     ||  !$this->options['db']
0095     ||  !$this->options['path']
0096     ||  !$this->options['files_table']) {
0097       return false;
0098     }
0099     
0100     
0101     $this->db = new mysqli($this->options['host'], $this->options['user'], $this->options['pass'], $this->options['db'], $this->options['port'], $this->options['socket']);
0102     if ($this->db->connect_error || @mysqli_connect_error()) {
0103       return false;
0104     }
0105     
0106     $this->db->set_charset('utf8');
0107 
0108     if ($res = $this->db->query('SHOW TABLES')) {
0109       while ($row = $res->fetch_array()) {
0110         if ($row[0] == $this->options['files_table']) {
0111           $this->tbf = $this->options['files_table'];
0112           break;
0113         }
0114       }
0115     }
0116 
0117     if (!$this->tbf) {
0118       return false;
0119     }
0120 
0121     $this->updateCache($this->options['path'], $this->_stat($this->options['path']));
0122 
0123     return true;
0124   }
0125 
0126 
0127 
0128   /**
0129    * Set tmp path
0130    *
0131    * @return void
0132    * @author Dmitry (dio) Levashov
0133    **/
0134   protected function configure() {
0135     parent::configure();
0136 
0137     if (($tmp = $this->options['tmpPath'])) {
0138       if (!file_exists($tmp)) {
0139         if (@mkdir($tmp)) {
0140           @chmod($tmp, $this->options['tmbPathMode']);
0141         }
0142       }
0143       
0144       $this->tmpPath = is_dir($tmp) && is_writable($tmp) ? $tmp : false;
0145     }
0146     
0147     if (!$this->tmpPath && $this->tmbPath && $this->tmbPathWritable) {
0148       $this->tmpPath = $this->tmbPath;
0149     }
0150 
0151     $this->mimeDetect = 'internal';
0152   }
0153   
0154   /**
0155    * Close connection
0156    *
0157    * @return void
0158    * @author Dmitry (dio) Levashov
0159    **/
0160   public function umount() {
0161     $this->db->close();
0162   }
0163   
0164   /**
0165    * Return debug info for client
0166    *
0167    * @return array
0168    * @author Dmitry (dio) Levashov
0169    **/
0170   public function debug() {
0171     $debug = parent::debug();
0172     $debug['sqlCount'] = $this->sqlCnt;
0173     if ($this->dbError) {
0174       $debug['dbError'] = $this->dbError;
0175     }
0176     return $debug;
0177   }
0178 
0179   /**
0180    * Perform sql query and return result.
0181    * Increase sqlCnt and save error if occured
0182    *
0183    * @param  string  $sql  query
0184    * @return misc
0185    * @author Dmitry (dio) Levashov
0186    **/
0187   protected function query($sql) {
0188     $this->sqlCnt++;
0189     $res = $this->db->query($sql);
0190     if (!$res) {
0191       $this->dbError = $this->db->error;
0192     }
0193     return $res;
0194   }
0195 
0196   /**
0197    * Create empty object with required mimetype
0198    *
0199    * @param  string  $path  parent dir path
0200    * @param  string  $name  object name
0201    * @param  string  $mime  mime type
0202    * @return bool
0203    * @author Dmitry (dio) Levashov
0204    **/
0205   protected function make($path, $name, $mime) {
0206     $sql = 'INSERT INTO %s (`parent_id`, `name`, `size`, `mtime`, `mime`, `content`, `read`, `write`) VALUES ("%s", "%s", 0, %d, "%s", "", "%d", "%d")';
0207     $sql = sprintf($sql, $this->tbf, $path, $this->db->real_escape_string($name), time(), $mime, $this->defaults['read'], $this->defaults['write']);
0208     // echo $sql;
0209     return $this->query($sql) && $this->db->affected_rows > 0;
0210   }
0211 
0212   /**
0213    * Return temporary file path for required file
0214    *
0215    * @param  string  $path   file path
0216    * @return string
0217    * @author Dmitry (dio) Levashov
0218    **/
0219   protected function tmpname($path) {
0220     return $this->tmpPath.DIRECTORY_SEPARATOR.md5($path);
0221   }
0222 
0223   /**
0224    * Resize image
0225    *
0226    * @param  string   $hash    image file
0227    * @param  int      $width   new width
0228    * @param  int      $height  new height
0229    * @param  bool     $crop    crop image
0230    * @return array|false
0231    * @author Dmitry (dio) Levashov
0232    * @author Alexey Sukhotin
0233    **/
0234   public function resize($hash, $width, $height, $x, $y, $mode = 'resize', $bg = '', $degree = 0) {
0235     if ($this->commandDisabled('resize')) {
0236       return $this->setError(elFinder::ERROR_PERM_DENIED);
0237     }
0238     
0239     if (($file = $this->file($hash)) == false) {
0240       return $this->setError(elFinder::ERROR_FILE_NOT_FOUND);
0241     }
0242     
0243     if (!$file['write'] || !$file['read']) {
0244       return $this->setError(elFinder::ERROR_PERM_DENIED);
0245     }
0246     
0247     $path = $this->decode($hash);
0248     
0249     if (!$this->canResize($path, $file)) {
0250       return $this->setError(elFinder::ERROR_UNSUPPORT_TYPE);
0251     }
0252 
0253     $img = $this->tmpname($path);
0254     
0255     if (!($fp = @fopen($img, 'w+'))) {
0256       return false;
0257     }
0258 
0259     if (($res = $this->query('SELECT content FROM '.$this->tbf.' WHERE id="'.$path.'"'))
0260     && ($r = $res->fetch_assoc())) {
0261       fwrite($fp, $r['content']);
0262       rewind($fp);
0263       fclose($fp);
0264     } else {
0265       return false;
0266     }
0267 
0268 
0269     switch($mode) {
0270       
0271       case 'propresize':
0272         $result = $this->imgResize($img, $width, $height, true, true);
0273         break;
0274 
0275       case 'crop':
0276         $result = $this->imgCrop($img, $width, $height, $x, $y);
0277         break;
0278 
0279       case 'fitsquare':
0280         $result = $this->imgSquareFit($img, $width, $height, 'center', 'middle', $bg ? $bg : $this->options['tmbBgColor']);
0281         break;
0282       
0283       default:
0284         $result = $this->imgResize($img, $width, $height, false, true);
0285         break;        
0286       }
0287     
0288     if ($result) {
0289       
0290       $sql = sprintf('UPDATE %s SET content=LOAD_FILE("%s"), mtime=UNIX_TIMESTAMP() WHERE id=%d', $this->tbf, $this->loadFilePath($img), $path);
0291       
0292       if (!$this->query($sql)) {
0293         $content = file_get_contents($img);
0294         $sql = sprintf('UPDATE %s SET content="%s", mtime=UNIX_TIMESTAMP() WHERE id=%d', $this->tbf, $this->db->real_escape_string($content), $path);
0295         if (!$this->query($sql)) {
0296           @unlink($img);
0297           return false;
0298         }
0299       }
0300       @unlink($img);
0301       if (!empty($file['tmb']) && $file['tmb'] != "1") {
0302         $this->rmTmb($file['tmb']);
0303       }
0304       $this->clearcache();
0305       return $this->stat($path);
0306     }
0307     
0308       return false;
0309   }
0310   
0311 
0312   /*********************************************************************/
0313   /*                               FS API                              */
0314   /*********************************************************************/
0315   
0316   /**
0317    * Cache dir contents
0318    *
0319    * @param  string  $path  dir path
0320    * @return void
0321    * @author Dmitry Levashov
0322    **/
0323   protected function cacheDir($path) {
0324     $this->dirsCache[$path] = array();
0325 
0326     $sql = 'SELECT f.id, f.parent_id, f.name, f.size, f.mtime AS ts, f.mime, f.read, f.write, f.locked, f.hidden, f.width, f.height, IF(ch.id, 1, 0) AS dirs 
0327         FROM '.$this->tbf.' AS f 
0328         LEFT JOIN '.$this->tbf.' AS ch ON ch.parent_id=f.id AND ch.mime="directory"
0329         WHERE f.parent_id="'.$path.'"
0330         GROUP BY f.id';
0331         
0332     $res = $this->query($sql);
0333     if ($res) {
0334       while ($row = $res->fetch_assoc()) {
0335         // debug($row);
0336         $id = $row['id'];
0337         if ($row['parent_id']) {
0338           $row['phash'] = $this->encode($row['parent_id']);
0339         } 
0340         
0341         if ($row['mime'] == 'directory') {
0342           unset($row['width']);
0343           unset($row['height']);
0344         } else {
0345           unset($row['dirs']);
0346         }
0347         
0348         unset($row['id']);
0349         unset($row['parent_id']);
0350         
0351         
0352         
0353         if (($stat = $this->updateCache($id, $row)) && empty($stat['hidden'])) {
0354           $this->dirsCache[$path][] = $id;
0355         }
0356       }
0357     }
0358     
0359     return $this->dirsCache[$path];
0360   }
0361 
0362   /**
0363    * Return array of parents paths (ids)
0364    *
0365    * @param  int   $path  file path (id)
0366    * @return array
0367    * @author Dmitry (dio) Levashov
0368    **/
0369   protected function getParents($path) {
0370     $parents = array();
0371 
0372     while ($path) {
0373       if ($file = $this->stat($path)) {
0374         array_unshift($parents, $path);
0375         $path = isset($file['phash']) ? $this->decode($file['phash']) : false;
0376       }
0377     }
0378     
0379     if (count($parents)) {
0380       array_pop($parents);
0381     }
0382     return $parents;
0383   }
0384 
0385   /**
0386    * Return correct file path for LOAD_FILE method
0387    *
0388    * @param  string $path  file path (id)
0389    * @return string
0390    * @author Troex Nevelin
0391    **/
0392   protected function loadFilePath($path) {
0393     $realPath = realpath($path);
0394     if (DIRECTORY_SEPARATOR == '\\') { // windows
0395       $realPath = str_replace('\\', '\\\\', $realPath);
0396     }
0397     return $this->db->real_escape_string($realPath);
0398   }
0399 
0400   /*********************** paths/urls *************************/
0401   
0402   /**
0403    * Return parent directory path
0404    *
0405    * @param  string  $path  file path
0406    * @return string
0407    * @author Dmitry (dio) Levashov
0408    **/
0409   protected function _dirname($path) {
0410     return ($stat = $this->stat($path)) ? ($stat['phash'] ? $this->decode($stat['phash']) : $this->root) : false;
0411   }
0412 
0413   /**
0414    * Return file name
0415    *
0416    * @param  string  $path  file path
0417    * @return string
0418    * @author Dmitry (dio) Levashov
0419    **/
0420   protected function _basename($path) {
0421     return ($stat = $this->stat($path)) ? $stat['name'] : false;
0422   }
0423 
0424   /**
0425    * Join dir name and file name and return full path
0426    *
0427    * @param  string  $dir
0428    * @param  string  $name
0429    * @return string
0430    * @author Dmitry (dio) Levashov
0431    **/
0432   protected function _joinPath($dir, $name) {
0433     $sql = 'SELECT id FROM '.$this->tbf.' WHERE parent_id="'.$dir.'" AND name="'.$this->db->real_escape_string($name).'"';
0434 
0435     if (($res = $this->query($sql)) && ($r = $res->fetch_assoc())) {
0436       $this->updateCache($r['id'], $this->_stat($r['id']));
0437       return $r['id'];
0438     }
0439     return -1;
0440   }
0441   
0442   /**
0443    * Return normalized path, this works the same as os.path.normpath() in Python
0444    *
0445    * @param  string  $path  path
0446    * @return string
0447    * @author Troex Nevelin
0448    **/
0449   protected function _normpath($path) {
0450     return $path;
0451   }
0452   
0453   /**
0454    * Return file path related to root dir
0455    *
0456    * @param  string  $path  file path
0457    * @return string
0458    * @author Dmitry (dio) Levashov
0459    **/
0460   protected function _relpath($path) {
0461     return $path;
0462   }
0463   
0464   /**
0465    * Convert path related to root dir into real path
0466    *
0467    * @param  string  $path  file path
0468    * @return string
0469    * @author Dmitry (dio) Levashov
0470    **/
0471   protected function _abspath($path) {
0472     return $path;
0473   }
0474   
0475   /**
0476    * Return fake path started from root dir
0477    *
0478    * @param  string  $path  file path
0479    * @return string
0480    * @author Dmitry (dio) Levashov
0481    **/
0482   protected function _path($path) {
0483     if (($file = $this->stat($path)) == false) {
0484       return '';
0485     }
0486     
0487     $parentsIds = $this->getParents($path);
0488     $path = '';
0489     foreach ($parentsIds as $id) {
0490       $dir = $this->stat($id);
0491       $path .= $dir['name'].$this->separator;
0492     }
0493     return $path.$file['name'];
0494   }
0495   
0496   /**
0497    * Return true if $path is children of $parent
0498    *
0499    * @param  string  $path    path to check
0500    * @param  string  $parent  parent path
0501    * @return bool
0502    * @author Dmitry (dio) Levashov
0503    **/
0504   protected function _inpath($path, $parent) {
0505     return $path == $parent
0506       ? true
0507       : in_array($parent, $this->getParents($path));
0508   }
0509   
0510   /***************** file stat ********************/
0511   /**
0512    * Return stat for given path.
0513    * Stat contains following fields:
0514    * - (int)    size    file size in b. required
0515    * - (int)    ts      file modification time in unix time. required
0516    * - (string) mime    mimetype. required for folders, others - optionally
0517    * - (bool)   read    read permissions. required
0518    * - (bool)   write   write permissions. required
0519    * - (bool)   locked  is object locked. optionally
0520    * - (bool)   hidden  is object hidden. optionally
0521    * - (string) alias   for symlinks - link target path relative to root path. optionally
0522    * - (string) target  for symlinks - link target path. optionally
0523    *
0524    * If file does not exists - returns empty array or false.
0525    *
0526    * @param  string  $path    file path 
0527    * @return array|false
0528    * @author Dmitry (dio) Levashov
0529    **/
0530   protected function _stat($path) {
0531     $sql = 'SELECT f.id, f.parent_id, f.name, f.size, f.mtime AS ts, f.mime, f.read, f.write, f.locked, f.hidden, f.width, f.height, IF(ch.id, 1, 0) AS dirs
0532         FROM '.$this->tbf.' AS f 
0533         LEFT JOIN '.$this->tbf.' AS p ON p.id=f.parent_id
0534         LEFT JOIN '.$this->tbf.' AS ch ON ch.parent_id=f.id AND ch.mime="directory"
0535         WHERE f.id="'.$path.'"
0536         GROUP BY f.id';
0537 
0538     $res = $this->query($sql);
0539     
0540     if ($res) {
0541       $stat = $res->fetch_assoc();
0542       if ($stat['parent_id']) {
0543         $stat['phash'] = $this->encode($stat['parent_id']);
0544       } 
0545       if ($stat['mime'] == 'directory') {
0546         unset($stat['width']);
0547         unset($stat['height']);
0548       } else {
0549         unset($stat['dirs']);
0550       }
0551       unset($stat['id']);
0552       unset($stat['parent_id']);
0553       return $stat;
0554       
0555     }
0556     return array();
0557   }
0558   
0559   /**
0560    * Return true if path is dir and has at least one childs directory
0561    *
0562    * @param  string  $path  dir path
0563    * @return bool
0564    * @author Dmitry (dio) Levashov
0565    **/
0566   protected function _subdirs($path) {
0567     return ($stat = $this->stat($path)) && isset($stat['dirs']) ? $stat['dirs'] : false;
0568   }
0569   
0570   /**
0571    * Return object width and height
0572    * Usualy used for images, but can be realize for video etc...
0573    *
0574    * @param  string  $path  file path
0575    * @param  string  $mime  file mime type
0576    * @return string
0577    * @author Dmitry (dio) Levashov
0578    **/
0579   protected function _dimensions($path, $mime) {
0580     return ($stat = $this->stat($path)) && isset($stat['width']) && isset($stat['height']) ? $stat['width'].'x'.$stat['height'] : '';
0581   }
0582   
0583   /******************** file/dir content *********************/
0584     
0585   /**
0586    * Return files list in directory.
0587    *
0588    * @param  string  $path  dir path
0589    * @return array
0590    * @author Dmitry (dio) Levashov
0591    **/
0592   protected function _scandir($path) {
0593     return isset($this->dirsCache[$path])
0594       ? $this->dirsCache[$path]
0595       : $this->cacheDir($path);
0596   }
0597     
0598   /**
0599    * Open file and return file pointer
0600    *
0601    * @param  string  $path  file path
0602    * @param  string  $mode  open file mode (ignored in this driver)
0603    * @return resource|false
0604    * @author Dmitry (dio) Levashov
0605    **/
0606   protected function _fopen($path, $mode='rb') {
0607     $fp = $this->tmbPath
0608       ? @fopen($this->tmpname($path), 'w+')
0609       : @tmpfile();
0610     
0611     
0612     if ($fp) {
0613       if (($res = $this->query('SELECT content FROM '.$this->tbf.' WHERE id="'.$path.'"'))
0614       && ($r = $res->fetch_assoc())) {
0615         fwrite($fp, $r['content']);
0616         rewind($fp);
0617         return $fp;
0618       } else {
0619         $this->_fclose($fp, $path);
0620       }
0621     }
0622     
0623     return false;
0624   }
0625   
0626   /**
0627    * Close opened file
0628    *
0629    * @param  resource  $fp  file pointer
0630    * @return bool
0631    * @author Dmitry (dio) Levashov
0632    **/
0633   protected function _fclose($fp, $path='') {
0634     @fclose($fp);
0635     if ($path) {
0636       @unlink($this->tmpname($path));
0637     }
0638   }
0639   
0640   /********************  file/dir manipulations *************************/
0641   
0642   /**
0643    * Create dir and return created dir path or false on failed
0644    *
0645    * @param  string  $path  parent dir path
0646    * @param string  $name  new directory name
0647    * @return string|bool
0648    * @author Dmitry (dio) Levashov
0649    **/
0650   protected function _mkdir($path, $name) {
0651     return $this->make($path, $name, 'directory') ? $this->_joinPath($path, $name) : false;
0652   }
0653   
0654   /**
0655    * Create file and return it's path or false on failed
0656    *
0657    * @param  string  $path  parent dir path
0658    * @param string  $name  new file name
0659    * @return string|bool
0660    * @author Dmitry (dio) Levashov
0661    **/
0662   protected function _mkfile($path, $name) {
0663     return $this->make($path, $name, 'text/plain') ? $this->_joinPath($path, $name) : false;
0664   }
0665   
0666   /**
0667    * Create symlink. FTP driver does not support symlinks.
0668    *
0669    * @param  string  $target  link target
0670    * @param  string  $path    symlink path
0671    * @return bool
0672    * @author Dmitry (dio) Levashov
0673    **/
0674   protected function _symlink($target, $path, $name) {
0675     return false;
0676   }
0677   
0678   /**
0679    * Copy file into another file
0680    *
0681    * @param  string  $source     source file path
0682    * @param  string  $targetDir  target directory path
0683    * @param  string  $name       new file name
0684    * @return bool
0685    * @author Dmitry (dio) Levashov
0686    **/
0687   protected function _copy($source, $targetDir, $name) {
0688     $this->clearcache();
0689     $id = $this->_joinPath($targetDir, $name);
0690 
0691     $sql = $id > 0
0692       ? sprintf('REPLACE INTO %s (id, parent_id, name, content, size, mtime, mime, width, height, `read`, `write`, `locked`, `hidden`) (SELECT %d, %d, name, content, size, mtime, mime, width, height, `read`, `write`, `locked`, `hidden` FROM %s WHERE id=%d)', $this->tbf, $id, $this->_dirname($id), $this->tbf, $source)
0693       : sprintf('INSERT INTO %s (parent_id, name, content, size, mtime, mime, width, height, `read`, `write`, `locked`, `hidden`) SELECT %d, "%s", content, size, %d, mime, width, height, `read`, `write`, `locked`, `hidden` FROM %s WHERE id=%d', $this->tbf, $targetDir, $this->db->real_escape_string($name), time(), $this->tbf, $source);
0694 
0695     return $this->query($sql);
0696   }
0697   
0698   /**
0699    * Move file into another parent dir.
0700    * Return new file path or false.
0701    *
0702    * @param  string  $source  source file path
0703    * @param  string  $target  target dir path
0704    * @param  string  $name    file name
0705    * @return string|bool
0706    * @author Dmitry (dio) Levashov
0707    **/
0708   protected function _move($source, $targetDir, $name) {
0709     $sql = 'UPDATE %s SET parent_id=%d, name="%s" WHERE id=%d LIMIT 1';
0710     $sql = sprintf($sql, $this->tbf, $targetDir, $this->db->real_escape_string($name), $source);
0711     return $this->query($sql) && $this->db->affected_rows > 0;
0712   }
0713     
0714   /**
0715    * Remove file
0716    *
0717    * @param  string  $path  file path
0718    * @return bool
0719    * @author Dmitry (dio) Levashov
0720    **/
0721   protected function _unlink($path) {
0722     return $this->query(sprintf('DELETE FROM %s WHERE id=%d AND mime!="directory" LIMIT 1', $this->tbf, $path)) && $this->db->affected_rows;
0723   }
0724 
0725   /**
0726    * Remove dir
0727    *
0728    * @param  string  $path  dir path
0729    * @return bool
0730    * @author Dmitry (dio) Levashov
0731    **/
0732   protected function _rmdir($path) {
0733     return $this->query(sprintf('DELETE FROM %s WHERE id=%d AND mime="directory" LIMIT 1', $this->tbf, $path)) && $this->db->affected_rows;
0734   }
0735   
0736   /**
0737    * undocumented function
0738    *
0739    * @return void
0740    * @author Dmitry Levashov
0741    **/
0742   protected function _setContent($path, $fp) {
0743     rewind($fp);
0744     $fstat = fstat($fp);
0745     $size = $fstat['size'];
0746     
0747     
0748   }
0749   
0750   /**
0751    * Create new file and write into it from file pointer.
0752    * Return new file path or false on error.
0753    *
0754    * @param  resource  $fp   file pointer
0755    * @param  string    $dir  target dir path
0756    * @param  string    $name file name
0757    * @return bool|string
0758    * @author Dmitry (dio) Levashov
0759    **/
0760   protected function _save($fp, $dir, $name, $mime, $w, $h) {
0761     $this->clearcache();
0762     
0763     $id = $this->_joinPath($dir, $name);
0764     rewind($fp);
0765     $stat = fstat($fp);
0766     $size = $stat['size'];
0767     
0768     if (($tmpfile = tempnam($this->tmpPath, $this->id))) {
0769       if (($trgfp = fopen($tmpfile, 'wb')) == false) {
0770         unlink($tmpfile);
0771       } else {
0772         while (!feof($fp)) {
0773           fwrite($trgfp, fread($fp, 8192));
0774         }
0775         fclose($trgfp);
0776         
0777         $sql = $id > 0
0778           ? 'REPLACE INTO %s (id, parent_id, name, content, size, mtime, mime, width, height) VALUES ('.$id.', %d, "%s", LOAD_FILE("%s"), %d, %d, "%s", %d, %d)'
0779           : 'INSERT INTO %s (parent_id, name, content, size, mtime, mime, width, height) VALUES (%d, "%s", LOAD_FILE("%s"), %d, %d, "%s", %d, %d)';
0780         $sql = sprintf($sql, $this->tbf, $dir, $this->db->real_escape_string($name), $this->loadFilePath($tmpfile), $size, time(), $mime, $w, $h);
0781 
0782         $res = $this->query($sql);
0783         unlink($tmpfile);
0784         
0785         if ($res) {
0786           return $id > 0 ? $id : $this->db->insert_id;
0787         }
0788       }
0789     }
0790 
0791     
0792     $content = '';
0793     rewind($fp);
0794     while (!feof($fp)) {
0795       $content .= fread($fp, 8192);
0796     }
0797     
0798     $sql = $id > 0
0799       ? 'REPLACE INTO %s (id, parent_id, name, content, size, mtime, mime, width, height) VALUES ('.$id.', %d, "%s", "%s", %d, %d, "%s", %d, %d)'
0800       : 'INSERT INTO %s (parent_id, name, content, size, mtime, mime, width, height) VALUES (%d, "%s", "%s", %d, %d, "%s", %d, %d)';
0801     $sql = sprintf($sql, $this->tbf, $dir, $this->db->real_escape_string($name), $this->db->real_escape_string($content), $size, time(), $mime, $w, $h);
0802     
0803     unset($content);
0804 
0805     if ($this->query($sql)) {
0806       return $id > 0 ? $id : $this->db->insert_id;
0807     }
0808     
0809     return false;
0810   }
0811   
0812   /**
0813    * Get file contents
0814    *
0815    * @param  string  $path  file path
0816    * @return string|false
0817    * @author Dmitry (dio) Levashov
0818    **/
0819   protected function _getContents($path) {
0820     return ($res = $this->query(sprintf('SELECT content FROM %s WHERE id=%d', $this->tbf, $path))) && ($r = $res->fetch_assoc()) ? $r['content'] : false;
0821   }
0822   
0823   /**
0824    * Write a string to a file
0825    *
0826    * @param  string  $path     file path
0827    * @param  string  $content  new file content
0828    * @return bool
0829    * @author Dmitry (dio) Levashov
0830    **/
0831   protected function _filePutContents($path, $content) {
0832     return $this->query(sprintf('UPDATE %s SET content="%s", size=%d, mtime=%d WHERE id=%d LIMIT 1', $this->tbf, $this->db->real_escape_string($content), strlen($content), time(), $path));
0833   }
0834 
0835   /**
0836    * Detect available archivers
0837    *
0838    * @return void
0839    **/
0840   protected function _checkArchivers() {
0841     return;
0842   }
0843 
0844   /**
0845    * Unpack archive
0846    *
0847    * @param  string  $path  archive path
0848    * @param  array   $arc   archiver command and arguments (same as in $this->archivers)
0849    * @return void
0850    * @author Dmitry (dio) Levashov
0851    * @author Alexey Sukhotin
0852    **/
0853   protected function _unpack($path, $arc) {
0854     return;
0855   }
0856 
0857   /**
0858    * Recursive symlinks search
0859    *
0860    * @param  string  $path  file/dir path
0861    * @return bool
0862    * @author Dmitry (dio) Levashov
0863    **/
0864   protected function _findSymlinks($path) {
0865     return false;
0866   }
0867 
0868   /**
0869    * Extract files from archive
0870    *
0871    * @param  string  $path  archive path
0872    * @param  array   $arc   archiver command and arguments (same as in $this->archivers)
0873    * @return true
0874    * @author Dmitry (dio) Levashov, 
0875    * @author Alexey Sukhotin
0876    **/
0877   protected function _extract($path, $arc) {
0878     return false;
0879   }
0880   
0881   /**
0882    * Create archive and return its path
0883    *
0884    * @param  string  $dir    target dir
0885    * @param  array   $files  files names list
0886    * @param  string  $name   archive name
0887    * @param  array   $arc    archiver options
0888    * @return string|bool
0889    * @author Dmitry (dio) Levashov, 
0890    * @author Alexey Sukhotin
0891    **/
0892   protected function _archive($dir, $files, $name, $arc) {
0893     return false;
0894   }
0895   
0896 } // END class