elFinderVolumeMySQL.class.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. <?php
  2. /**
  3. * Simple elFinder driver for MySQL.
  4. *
  5. * @author Dmitry (dio) Levashov
  6. **/
  7. class elFinderVolumeMySQL extends elFinderVolumeDriver {
  8. /**
  9. * Driver id
  10. * Must be started from letter and contains [a-z0-9]
  11. * Used as part of volume id
  12. *
  13. * @var string
  14. **/
  15. protected $driverId = 'm';
  16. /**
  17. * Database object
  18. *
  19. * @var mysqli
  20. **/
  21. protected $db = null;
  22. /**
  23. * Tables to store files
  24. *
  25. * @var string
  26. **/
  27. protected $tbf = '';
  28. /**
  29. * Directory for tmp files
  30. * If not set driver will try to use tmbDir as tmpDir
  31. *
  32. * @var string
  33. **/
  34. protected $tmpPath = '';
  35. /**
  36. * Numbers of sql requests (for debug)
  37. *
  38. * @var int
  39. **/
  40. protected $sqlCnt = 0;
  41. /**
  42. * Last db error message
  43. *
  44. * @var string
  45. **/
  46. protected $dbError = '';
  47. /**
  48. * This root has parent id
  49. *
  50. * @var boolean
  51. */
  52. protected $rootHasParent = false;
  53. /**
  54. * Constructor
  55. * Extend options with required fields
  56. *
  57. * @author Dmitry (dio) Levashov
  58. */
  59. public function __construct() {
  60. $opts = array(
  61. 'host' => 'localhost',
  62. 'user' => '',
  63. 'pass' => '',
  64. 'db' => '',
  65. 'port' => null,
  66. 'socket' => null,
  67. 'files_table' => 'elfinder_file',
  68. 'tmbPath' => '',
  69. 'tmpPath' => '',
  70. 'rootCssClass' => 'elfinder-navbar-root-sql',
  71. 'noSessionCache' => array('hasdirs')
  72. );
  73. $this->options = array_merge($this->options, $opts);
  74. $this->options['mimeDetect'] = 'internal';
  75. }
  76. /*********************************************************************/
  77. /* INIT AND CONFIGURE */
  78. /*********************************************************************/
  79. /**
  80. * Prepare driver before mount volume.
  81. * Connect to db, check required tables and fetch root path
  82. *
  83. * @return bool
  84. * @author Dmitry (dio) Levashov
  85. **/
  86. protected function init() {
  87. if (!($this->options['host'] || $this->options['socket'])
  88. || !$this->options['user']
  89. || !$this->options['pass']
  90. || !$this->options['db']
  91. || !$this->options['path']
  92. || !$this->options['files_table']) {
  93. return false;
  94. }
  95. $this->db = new mysqli($this->options['host'], $this->options['user'], $this->options['pass'], $this->options['db'], $this->options['port'], $this->options['socket']);
  96. if ($this->db->connect_error || mysqli_connect_error()) {
  97. return false;
  98. }
  99. $this->db->set_charset('utf8');
  100. if ($res = $this->db->query('SHOW TABLES')) {
  101. while ($row = $res->fetch_array()) {
  102. if ($row[0] == $this->options['files_table']) {
  103. $this->tbf = $this->options['files_table'];
  104. break;
  105. }
  106. }
  107. }
  108. if (!$this->tbf) {
  109. return false;
  110. }
  111. $this->updateCache($this->options['path'], $this->_stat($this->options['path']));
  112. // enable command archive
  113. $this->options['useRemoteArchive'] = true;
  114. return true;
  115. }
  116. /**
  117. * Set tmp path
  118. *
  119. * @return void
  120. * @author Dmitry (dio) Levashov
  121. **/
  122. protected function configure() {
  123. parent::configure();
  124. if (($tmp = $this->options['tmpPath'])) {
  125. if (!file_exists($tmp)) {
  126. if (mkdir($tmp)) {
  127. chmod($tmp, $this->options['tmbPathMode']);
  128. }
  129. }
  130. $this->tmpPath = is_dir($tmp) && is_writable($tmp) ? $tmp : false;
  131. }
  132. if (!$this->tmpPath && ($tmp = elFinder::getStaticVar('commonTempPath'))) {
  133. $this->tmpPath = $tmp;
  134. }
  135. // fallback of $this->tmp
  136. if (!$this->tmpPath && $this->tmbPathWritable) {
  137. $this->tmpPath = $this->tmbPath;
  138. }
  139. $this->mimeDetect = 'internal';
  140. }
  141. /**
  142. * Close connection
  143. *
  144. * @return void
  145. * @author Dmitry (dio) Levashov
  146. **/
  147. public function umount() {
  148. $this->db->close();
  149. }
  150. /**
  151. * Return debug info for client
  152. *
  153. * @return array
  154. * @author Dmitry (dio) Levashov
  155. **/
  156. public function debug() {
  157. $debug = parent::debug();
  158. $debug['sqlCount'] = $this->sqlCnt;
  159. if ($this->dbError) {
  160. $debug['dbError'] = $this->dbError;
  161. }
  162. return $debug;
  163. }
  164. /**
  165. * Perform sql query and return result.
  166. * Increase sqlCnt and save error if occured
  167. *
  168. * @param string $sql query
  169. * @return misc
  170. * @author Dmitry (dio) Levashov
  171. **/
  172. protected function query($sql) {
  173. $this->sqlCnt++;
  174. $res = $this->db->query($sql);
  175. if (!$res) {
  176. $this->dbError = $this->db->error;
  177. }
  178. return $res;
  179. }
  180. /**
  181. * Create empty object with required mimetype
  182. *
  183. * @param string $path parent dir path
  184. * @param string $name object name
  185. * @param string $mime mime type
  186. * @return bool
  187. * @author Dmitry (dio) Levashov
  188. **/
  189. protected function make($path, $name, $mime) {
  190. $sql = 'INSERT INTO %s (`parent_id`, `name`, `size`, `mtime`, `mime`, `content`, `read`, `write`, `locked`, `hidden`, `width`, `height`) VALUES (\'%s\', \'%s\', 0, %d, \'%s\', \'\', \'%d\', \'%d\', \'%d\', \'%d\', 0, 0)';
  191. $sql = sprintf($sql, $this->tbf, $path, $this->db->real_escape_string($name), time(), $mime, $this->defaults['read'], $this->defaults['write'], $this->defaults['locked'], $this->defaults['hidden']);
  192. // echo $sql;
  193. return $this->query($sql) && $this->db->affected_rows > 0;
  194. }
  195. /*********************************************************************/
  196. /* FS API */
  197. /*********************************************************************/
  198. /**
  199. * Cache dir contents
  200. *
  201. * @param string $path dir path
  202. * @return string
  203. * @author Dmitry Levashov
  204. **/
  205. protected function cacheDir($path) {
  206. $this->dirsCache[$path] = array();
  207. $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
  208. FROM '.$this->tbf.' AS f
  209. LEFT JOIN '.$this->tbf.' AS ch ON ch.parent_id=f.id AND ch.mime=\'directory\'
  210. WHERE f.parent_id=\''.$path.'\'
  211. GROUP BY f.id, ch.id';
  212. $res = $this->query($sql);
  213. if ($res) {
  214. while ($row = $res->fetch_assoc()) {
  215. $id = $row['id'];
  216. if ($row['parent_id'] && $id != $this->root) {
  217. $row['phash'] = $this->encode($row['parent_id']);
  218. }
  219. if ($row['mime'] == 'directory') {
  220. unset($row['width']);
  221. unset($row['height']);
  222. $row['size'] = 0;
  223. } else {
  224. unset($row['dirs']);
  225. }
  226. unset($row['id']);
  227. unset($row['parent_id']);
  228. if (($stat = $this->updateCache($id, $row)) && empty($stat['hidden'])) {
  229. $this->dirsCache[$path][] = $id;
  230. }
  231. }
  232. }
  233. return $this->dirsCache[$path];
  234. }
  235. /**
  236. * Return array of parents paths (ids)
  237. *
  238. * @param int $path file path (id)
  239. * @return array
  240. * @author Dmitry (dio) Levashov
  241. **/
  242. protected function getParents($path) {
  243. $parents = array();
  244. while ($path) {
  245. if ($file = $this->stat($path)) {
  246. array_unshift($parents, $path);
  247. $path = isset($file['phash']) ? $this->decode($file['phash']) : false;
  248. }
  249. }
  250. if (count($parents)) {
  251. array_pop($parents);
  252. }
  253. return $parents;
  254. }
  255. /**
  256. * Return correct file path for LOAD_FILE method
  257. *
  258. * @param string $path file path (id)
  259. * @return string
  260. * @author Troex Nevelin
  261. **/
  262. protected function loadFilePath($path) {
  263. $realPath = realpath($path);
  264. if (DIRECTORY_SEPARATOR == '\\') { // windows
  265. $realPath = str_replace('\\', '\\\\', $realPath);
  266. }
  267. return $this->db->real_escape_string($realPath);
  268. }
  269. /**
  270. * Recursive files search
  271. *
  272. * @param string $path dir path
  273. * @param string $q search string
  274. * @param array $mimes
  275. * @return array
  276. * @author Dmitry (dio) Levashov
  277. **/
  278. protected function doSearch($path, $q, $mimes) {
  279. $dirs = array();
  280. $timeout = $this->options['searchTimeout']? $this->searchStart + $this->options['searchTimeout'] : 0;
  281. if ($path != $this->root || $this->rootHasParent) {
  282. $dirs = $inpath = array(intval($path));
  283. while($inpath) {
  284. $in = '('.join(',', $inpath).')';
  285. $inpath = array();
  286. $sql = 'SELECT f.id FROM %s AS f WHERE f.parent_id IN '.$in.' AND `mime` = \'directory\'';
  287. $sql = sprintf($sql, $this->tbf);
  288. if ($res = $this->query($sql)) {
  289. $_dir = array();
  290. while ($dat = $res->fetch_assoc()) {
  291. $inpath[] = $dat['id'];
  292. }
  293. $dirs = array_merge($dirs, $inpath);
  294. }
  295. }
  296. }
  297. $result = array();
  298. if ($mimes) {
  299. $whrs = array();
  300. foreach($mimes as $mime) {
  301. if (strpos($mime, '/') === false) {
  302. $whrs[] = sprintf('f.mime LIKE \'%s/%%\'', $this->db->real_escape_string($mime));
  303. } else {
  304. $whrs[] = sprintf('f.mime = \'%s\'', $this->db->real_escape_string($mime));
  305. }
  306. }
  307. $whr = join(' OR ', $whrs);
  308. } else {
  309. $whr = sprintf('f.name LIKE \'%%%s%%\'', $this->db->real_escape_string($q));
  310. }
  311. if ($dirs) {
  312. $whr = '(' . $whr . ') AND (`parent_id` IN (' . join(',', $dirs) . '))';
  313. }
  314. $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, 0 AS dirs
  315. FROM %s AS f
  316. WHERE %s';
  317. $sql = sprintf($sql, $this->tbf, $whr);
  318. if (($res = $this->query($sql))) {
  319. while ($row = $res->fetch_assoc()) {
  320. if ($timeout && $timeout < time()) {
  321. $this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode($path)));
  322. break;
  323. }
  324. if (!$this->mimeAccepted($row['mime'], $mimes)) {
  325. continue;
  326. }
  327. $id = $row['id'];
  328. if ($id == $this->root) {
  329. continue;
  330. }
  331. if ($row['parent_id'] && $id != $this->root) {
  332. $row['phash'] = $this->encode($row['parent_id']);
  333. }
  334. $row['path'] = $this->_path($id);
  335. if ($row['mime'] == 'directory') {
  336. unset($row['width']);
  337. unset($row['height']);
  338. } else {
  339. unset($row['dirs']);
  340. }
  341. unset($row['id']);
  342. unset($row['parent_id']);
  343. if (($stat = $this->updateCache($id, $row)) && empty($stat['hidden'])) {
  344. $result[] = $stat;
  345. }
  346. }
  347. }
  348. debug($result);
  349. return $result;
  350. }
  351. /*********************** paths/urls *************************/
  352. /**
  353. * Return parent directory path
  354. *
  355. * @param string $path file path
  356. * @return string
  357. * @author Dmitry (dio) Levashov
  358. **/
  359. protected function _dirname($path) {
  360. return ($stat = $this->stat($path)) ? (!empty($stat['phash']) ? $this->decode($stat['phash']) : $this->root) : false;
  361. }
  362. /**
  363. * Return file name
  364. *
  365. * @param string $path file path
  366. * @return string
  367. * @author Dmitry (dio) Levashov
  368. **/
  369. protected function _basename($path) {
  370. return ($stat = $this->stat($path)) ? $stat['name'] : false;
  371. }
  372. /**
  373. * Join dir name and file name and return full path
  374. *
  375. * @param string $dir
  376. * @param string $name
  377. * @return string
  378. * @author Dmitry (dio) Levashov
  379. **/
  380. protected function _joinPath($dir, $name) {
  381. $sql = 'SELECT id FROM '.$this->tbf.' WHERE parent_id=\''.$dir.'\' AND name=\''.$this->db->real_escape_string($name).'\'';
  382. if (($res = $this->query($sql)) && ($r = $res->fetch_assoc())) {
  383. $this->updateCache($r['id'], $this->_stat($r['id']));
  384. return $r['id'];
  385. }
  386. return -1;
  387. }
  388. /**
  389. * Return normalized path, this works the same as os.path.normpath() in Python
  390. *
  391. * @param string $path path
  392. * @return string
  393. * @author Troex Nevelin
  394. **/
  395. protected function _normpath($path) {
  396. return $path;
  397. }
  398. /**
  399. * Return file path related to root dir
  400. *
  401. * @param string $path file path
  402. * @return string
  403. * @author Dmitry (dio) Levashov
  404. **/
  405. protected function _relpath($path) {
  406. return $path;
  407. }
  408. /**
  409. * Convert path related to root dir into real path
  410. *
  411. * @param string $path file path
  412. * @return string
  413. * @author Dmitry (dio) Levashov
  414. **/
  415. protected function _abspath($path) {
  416. return $path;
  417. }
  418. /**
  419. * Return fake path started from root dir
  420. *
  421. * @param string $path file path
  422. * @return string
  423. * @author Dmitry (dio) Levashov
  424. **/
  425. protected function _path($path) {
  426. if (($file = $this->stat($path)) == false) {
  427. return '';
  428. }
  429. $parentsIds = $this->getParents($path);
  430. $path = '';
  431. foreach ($parentsIds as $id) {
  432. $dir = $this->stat($id);
  433. $path .= $dir['name'].$this->separator;
  434. }
  435. return $path.$file['name'];
  436. }
  437. /**
  438. * Return true if $path is children of $parent
  439. *
  440. * @param string $path path to check
  441. * @param string $parent parent path
  442. * @return bool
  443. * @author Dmitry (dio) Levashov
  444. **/
  445. protected function _inpath($path, $parent) {
  446. return $path == $parent
  447. ? true
  448. : in_array($parent, $this->getParents($path));
  449. }
  450. /***************** file stat ********************/
  451. /**
  452. * Return stat for given path.
  453. * Stat contains following fields:
  454. * - (int) size file size in b. required
  455. * - (int) ts file modification time in unix time. required
  456. * - (string) mime mimetype. required for folders, others - optionally
  457. * - (bool) read read permissions. required
  458. * - (bool) write write permissions. required
  459. * - (bool) locked is object locked. optionally
  460. * - (bool) hidden is object hidden. optionally
  461. * - (string) alias for symlinks - link target path relative to root path. optionally
  462. * - (string) target for symlinks - link target path. optionally
  463. *
  464. * If file does not exists - returns empty array or false.
  465. *
  466. * @param string $path file path
  467. * @return array|false
  468. * @author Dmitry (dio) Levashov
  469. **/
  470. protected function _stat($path) {
  471. $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
  472. FROM '.$this->tbf.' AS f
  473. LEFT JOIN '.$this->tbf.' AS ch ON ch.parent_id=f.id AND ch.mime=\'directory\'
  474. WHERE f.id=\''.$path.'\'
  475. GROUP BY f.id, ch.id';
  476. $res = $this->query($sql);
  477. if ($res) {
  478. $stat = $res->fetch_assoc();
  479. if ($stat['id'] == $this->root) {
  480. $this->rootHasParent = true;
  481. $stat['parent_id'] = '';
  482. }
  483. if ($stat['parent_id']) {
  484. $stat['phash'] = $this->encode($stat['parent_id']);
  485. }
  486. if ($stat['mime'] == 'directory') {
  487. unset($stat['width']);
  488. unset($stat['height']);
  489. $stat['size'] = 0;
  490. } else {
  491. if (!$stat['mime']) {
  492. unset($stat['mime']);
  493. }
  494. unset($stat['dirs']);
  495. }
  496. unset($stat['id']);
  497. unset($stat['parent_id']);
  498. return $stat;
  499. }
  500. return array();
  501. }
  502. /**
  503. * Return true if path is dir and has at least one childs directory
  504. *
  505. * @param string $path dir path
  506. * @return bool
  507. * @author Dmitry (dio) Levashov
  508. **/
  509. protected function _subdirs($path) {
  510. return ($stat = $this->stat($path)) && isset($stat['dirs']) ? $stat['dirs'] : false;
  511. }
  512. /**
  513. * Return object width and height
  514. * Usualy used for images, but can be realize for video etc...
  515. *
  516. * @param string $path file path
  517. * @param string $mime file mime type
  518. * @return string
  519. * @author Dmitry (dio) Levashov
  520. **/
  521. protected function _dimensions($path, $mime) {
  522. return ($stat = $this->stat($path)) && isset($stat['width']) && isset($stat['height']) ? $stat['width'].'x'.$stat['height'] : '';
  523. }
  524. /******************** file/dir content *********************/
  525. /**
  526. * Return files list in directory.
  527. *
  528. * @param string $path dir path
  529. * @return array
  530. * @author Dmitry (dio) Levashov
  531. **/
  532. protected function _scandir($path) {
  533. return isset($this->dirsCache[$path])
  534. ? $this->dirsCache[$path]
  535. : $this->cacheDir($path);
  536. }
  537. /**
  538. * Open file and return file pointer
  539. *
  540. * @param string $path file path
  541. * @param string $mode open file mode (ignored in this driver)
  542. * @return resource|false
  543. * @author Dmitry (dio) Levashov
  544. **/
  545. protected function _fopen($path, $mode='rb') {
  546. $fp = $this->tmbPath
  547. ? fopen($this->getTempFile($path), 'w+')
  548. : $this->tmpfile();
  549. if ($fp) {
  550. if (($res = $this->query('SELECT content FROM '.$this->tbf.' WHERE id=\''.$path.'\''))
  551. && ($r = $res->fetch_assoc())) {
  552. fwrite($fp, $r['content']);
  553. rewind($fp);
  554. return $fp;
  555. } else {
  556. $this->_fclose($fp, $path);
  557. }
  558. }
  559. return false;
  560. }
  561. /**
  562. * Close opened file
  563. *
  564. * @param resource $fp file pointer
  565. * @param string $path
  566. * @return bool
  567. * @author Dmitry (dio) Levashov
  568. */
  569. protected function _fclose($fp, $path='') {
  570. is_resource($fp) && fclose($fp);
  571. if ($path) {
  572. $file = $this->getTempFile($path);
  573. is_file($file) && unlink($file);
  574. }
  575. }
  576. /******************** file/dir manipulations *************************/
  577. /**
  578. * Create dir and return created dir path or false on failed
  579. *
  580. * @param string $path parent dir path
  581. * @param string $name new directory name
  582. * @return string|bool
  583. * @author Dmitry (dio) Levashov
  584. **/
  585. protected function _mkdir($path, $name) {
  586. return $this->make($path, $name, 'directory') ? $this->_joinPath($path, $name) : false;
  587. }
  588. /**
  589. * Create file and return it's path or false on failed
  590. *
  591. * @param string $path parent dir path
  592. * @param string $name new file name
  593. * @return string|bool
  594. * @author Dmitry (dio) Levashov
  595. **/
  596. protected function _mkfile($path, $name) {
  597. return $this->make($path, $name, '') ? $this->_joinPath($path, $name) : false;
  598. }
  599. /**
  600. * Create symlink. FTP driver does not support symlinks.
  601. *
  602. * @param string $target link target
  603. * @param string $path symlink path
  604. * @param string $name
  605. * @return bool
  606. * @author Dmitry (dio) Levashov
  607. */
  608. protected function _symlink($target, $path, $name) {
  609. return false;
  610. }
  611. /**
  612. * Copy file into another file
  613. *
  614. * @param string $source source file path
  615. * @param string $targetDir target directory path
  616. * @param string $name new file name
  617. * @return bool
  618. * @author Dmitry (dio) Levashov
  619. **/
  620. protected function _copy($source, $targetDir, $name) {
  621. $this->clearcache();
  622. $id = $this->_joinPath($targetDir, $name);
  623. $sql = $id > 0
  624. ? 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)
  625. : 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);
  626. return $this->query($sql);
  627. }
  628. /**
  629. * Move file into another parent dir.
  630. * Return new file path or false.
  631. *
  632. * @param string $source source file path
  633. * @param $targetDir
  634. * @param string $name file name
  635. * @return bool|string
  636. * @internal param string $target target dir path
  637. * @author Dmitry (dio) Levashov
  638. */
  639. protected function _move($source, $targetDir, $name) {
  640. $sql = 'UPDATE %s SET parent_id=%d, name=\'%s\' WHERE id=%d LIMIT 1';
  641. $sql = sprintf($sql, $this->tbf, $targetDir, $this->db->real_escape_string($name), $source);
  642. return $this->query($sql) && $this->db->affected_rows > 0 ? $source : false;
  643. }
  644. /**
  645. * Remove file
  646. *
  647. * @param string $path file path
  648. * @return bool
  649. * @author Dmitry (dio) Levashov
  650. **/
  651. protected function _unlink($path) {
  652. return $this->query(sprintf('DELETE FROM %s WHERE id=%d AND mime!=\'directory\' LIMIT 1', $this->tbf, $path)) && $this->db->affected_rows;
  653. }
  654. /**
  655. * Remove dir
  656. *
  657. * @param string $path dir path
  658. * @return bool
  659. * @author Dmitry (dio) Levashov
  660. **/
  661. protected function _rmdir($path) {
  662. return $this->query(sprintf('DELETE FROM %s WHERE id=%d AND mime=\'directory\' LIMIT 1', $this->tbf, $path)) && $this->db->affected_rows;
  663. }
  664. /**
  665. * undocumented function
  666. *
  667. * @param $path
  668. * @param $fp
  669. * @author Dmitry Levashov
  670. */
  671. protected function _setContent($path, $fp) {
  672. elFinder::rewind($fp);
  673. $fstat = fstat($fp);
  674. $size = $fstat['size'];
  675. }
  676. /**
  677. * Create new file and write into it from file pointer.
  678. * Return new file path or false on error.
  679. *
  680. * @param resource $fp file pointer
  681. * @param string $dir target dir path
  682. * @param string $name file name
  683. * @param array $stat file stat (required by some virtual fs)
  684. * @return bool|string
  685. * @author Dmitry (dio) Levashov
  686. **/
  687. protected function _save($fp, $dir, $name, $stat) {
  688. $this->clearcache();
  689. $mime = $stat['mime'];
  690. $w = !empty($stat['width']) ? $stat['width'] : 0;
  691. $h = !empty($stat['height']) ? $stat['height'] : 0;
  692. $id = $this->_joinPath($dir, $name);
  693. elFinder::rewind($fp);
  694. $stat = fstat($fp);
  695. $size = $stat['size'];
  696. if (($tmpfile = tempnam($this->tmpPath, $this->id))) {
  697. if (($trgfp = fopen($tmpfile, 'wb')) == false) {
  698. unlink($tmpfile);
  699. } else {
  700. while (!feof($fp)) {
  701. fwrite($trgfp, fread($fp, 8192));
  702. }
  703. fclose($trgfp);
  704. chmod($tmpfile, 0644);
  705. $sql = $id > 0
  706. ? '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)'
  707. : 'INSERT INTO %s (parent_id, name, content, size, mtime, mime, width, height) VALUES (%d, \'%s\', LOAD_FILE(\'%s\'), %d, %d, \'%s\', %d, %d)';
  708. $sql = sprintf($sql, $this->tbf, $dir, $this->db->real_escape_string($name), $this->loadFilePath($tmpfile), $size, time(), $mime, $w, $h);
  709. $res = $this->query($sql);
  710. unlink($tmpfile);
  711. if ($res) {
  712. return $id > 0 ? $id : $this->db->insert_id;
  713. }
  714. }
  715. }
  716. $content = '';
  717. elFinder::rewind($fp);
  718. while (!feof($fp)) {
  719. $content .= fread($fp, 8192);
  720. }
  721. $sql = $id > 0
  722. ? 'REPLACE INTO %s (id, parent_id, name, content, size, mtime, mime, width, height) VALUES ('.$id.', %d, \'%s\', \'%s\', %d, %d, \'%s\', %d, %d)'
  723. : 'INSERT INTO %s (parent_id, name, content, size, mtime, mime, width, height) VALUES (%d, \'%s\', \'%s\', %d, %d, \'%s\', %d, %d)';
  724. $sql = sprintf($sql, $this->tbf, $dir, $this->db->real_escape_string($name), $this->db->real_escape_string($content), $size, time(), $mime, $w, $h);
  725. unset($content);
  726. if ($this->query($sql)) {
  727. return $id > 0 ? $id : $this->db->insert_id;
  728. }
  729. return false;
  730. }
  731. /**
  732. * Get file contents
  733. *
  734. * @param string $path file path
  735. * @return string|false
  736. * @author Dmitry (dio) Levashov
  737. **/
  738. protected function _getContents($path) {
  739. return ($res = $this->query(sprintf('SELECT content FROM %s WHERE id=%d', $this->tbf, $path))) && ($r = $res->fetch_assoc()) ? $r['content'] : false;
  740. }
  741. /**
  742. * Write a string to a file
  743. *
  744. * @param string $path file path
  745. * @param string $content new file content
  746. * @return bool
  747. * @author Dmitry (dio) Levashov
  748. **/
  749. protected function _filePutContents($path, $content) {
  750. 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));
  751. }
  752. /**
  753. * Detect available archivers
  754. *
  755. * @return void
  756. **/
  757. protected function _checkArchivers() {
  758. return;
  759. }
  760. /**
  761. * chmod implementation
  762. *
  763. * @param string $path
  764. * @param string $mode
  765. * @return bool
  766. */
  767. protected function _chmod($path, $mode) {
  768. return false;
  769. }
  770. /**
  771. * Unpack archive
  772. *
  773. * @param string $path archive path
  774. * @param array $arc archiver command and arguments (same as in $this->archivers)
  775. * @return void
  776. * @author Dmitry (dio) Levashov
  777. * @author Alexey Sukhotin
  778. **/
  779. protected function _unpack($path, $arc) {
  780. return;
  781. }
  782. /**
  783. * Recursive symlinks search
  784. *
  785. * @param string $path file/dir path
  786. * @return bool
  787. * @author Dmitry (dio) Levashov
  788. **/
  789. protected function _findSymlinks($path) {
  790. return false;
  791. }
  792. /**
  793. * Extract files from archive
  794. *
  795. * @param string $path archive path
  796. * @param array $arc archiver command and arguments (same as in $this->archivers)
  797. * @return true
  798. * @author Dmitry (dio) Levashov,
  799. * @author Alexey Sukhotin
  800. **/
  801. protected function _extract($path, $arc) {
  802. return false;
  803. }
  804. /**
  805. * Create archive and return its path
  806. *
  807. * @param string $dir target dir
  808. * @param array $files files names list
  809. * @param string $name archive name
  810. * @param array $arc archiver options
  811. * @return string|bool
  812. * @author Dmitry (dio) Levashov,
  813. * @author Alexey Sukhotin
  814. **/
  815. protected function _archive($dir, $files, $name, $arc) {
  816. return false;
  817. }
  818. } // END class