elFinderVolumeMySQL.class.php 28 KB

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