: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Simple elFinder driver for MySQL.
* @author Dmitry (dio) Levashov
class elFinderVolumeMySQL extends elFinderVolumeDriver
* Must be started from letter and contains [a-z0-9]
* Used as part of volume id
protected $driverId = 'm';
* Directory for tmp files
* If not set driver will try to use tmbDir as tmpDir
* Numbers of sql requests (for debug)
* This root has parent id
protected $rootHasParent = false;
* Extend options with required fields
* @author Dmitry (dio) Levashov
public function __construct()
'files_table' => 'elfinder_file',
'rootCssClass' => 'elfinder-navbar-root-sql',
'noSessionCache' => array('hasdirs'),
$this->options = array_merge($this->options, $opts);
$this->options['mimeDetect'] = 'internal';
/*********************************************************************/
/*********************************************************************/
* Prepare driver before mount volume.
* Connect to db, check required tables and fetch root path
* @author Dmitry (dio) Levashov
protected function init()
if (!($this->options['host'] || $this->options['socket'])
|| !$this->options['user']
|| !$this->options['pass']
|| !$this->options['path']
|| !$this->options['files_table']) {
return $this->setError('Required options "host", "socket", "user", "pass", "db", "path" or "files_table" are undefined.');
if ($this->db = @new mysqli($this->options['host'], $this->options['user'], $this->options['pass'], $this->options['db'], $this->options['port'], $this->options['socket'])) {
if ($this->db && $this->db->connect_error) {
$err = $this->db->connect_error;
$err = mysqli_connect_error();
return $this->setError(array('Unable to connect to MySQL server.', $err));
if (!$this->needOnline && empty($this->ARGS['init'])) {
$this->db->set_charset('utf8');
if ($res = $this->db->query('SHOW TABLES')) {
while ($row = $res->fetch_array()) {
if ($row[0] == $this->options['files_table']) {
$this->tbf = $this->options['files_table'];
return $this->setError('The specified database table cannot be found.');
$this->updateCache($this->options['path'], $this->_stat($this->options['path']));
// enable command archive
$this->options['useRemoteArchive'] = true;
$this->isLocalhost = $this->options['isLocalhost'] || $this->options['host'] === 'localhost' || $this->options['host'] === '127.0.0.1' || $this->options['host'] === '::1';
* @throws elFinderAbortException
* @author Dmitry (dio) Levashov
protected function configure()
if (($tmp = $this->options['tmpPath'])) {
if (!file_exists($tmp)) {
chmod($tmp, $this->options['tmbPathMode']);
$this->tmpPath = is_dir($tmp) && is_writable($tmp) ? $tmp : false;
if (!$this->tmpPath && ($tmp = elFinder::getStaticVar('commonTempPath'))) {
// fallback of $this->tmp
if (!$this->tmpPath && $this->tmbPathWritable) {
$this->tmpPath = $this->tmbPath;
$this->mimeDetect = 'internal';
* @author Dmitry (dio) Levashov
$this->db && $this->db->close();
* Return debug info for client
* @author Dmitry (dio) Levashov
$debug = parent::debug();
$debug['sqlCount'] = $this->sqlCnt;
$debug['dbError'] = $this->dbError;
* Perform sql query and return result.
* Increase sqlCnt and save error if occured
* @param string $sql query
* @return bool|mysqli_result
* @author Dmitry (dio) Levashov
protected function query($sql)
$res = $this->db->query($sql);
$this->dbError = $this->db->error;
* Perform sql prepared statement and return result.
* Increase sqlCnt and save error if occurred.
* @param mysqli_stmt $stmt
protected function execute($stmt)
$this->dbError = $this->db->error;
* Create empty object with required mimetype
* @param string $path parent dir path
* @param string $name object name
* @param string $mime mime type
* @author Dmitry (dio) Levashov
protected function make($path, $name, $mime)
$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)';
$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']);
return $this->query($sql) && $this->db->affected_rows > 0;
/*********************************************************************/
/*********************************************************************/
* @param string $path dir path
* @author Dmitry Levashov
protected function cacheDir($path)
$this->dirsCache[$path] = array();
$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
FROM ' . $this->tbf . ' AS f
LEFT JOIN ' . $this->tbf . ' AS ch ON ch.parent_id=f.id AND ch.mime=\'directory\'
WHERE f.parent_id=\'' . $path . '\'
$res = $this->query($sql);
while ($row = $res->fetch_assoc()) {
if ($row['parent_id'] && $id != $this->root) {
$row['phash'] = $this->encode($row['parent_id']);
if ($row['mime'] == 'directory') {
unset($row['parent_id']);
if (($stat = $this->updateCache($id, $row)) && empty($stat['hidden'])) {
$this->dirsCache[$path][] = $id;
return $this->dirsCache[$path];
* Return array of parents paths (ids)
* @param int $path file path (id)
* @author Dmitry (dio) Levashov
protected function getParents($path)
if ($file = $this->stat($path)) {
array_unshift($parents, $path);
$path = isset($file['phash']) ? $this->decode($file['phash']) : false;
* Return correct file path for LOAD_FILE method
* @param string $path file path (id)
protected function loadFilePath($path)
$realPath = realpath($path);
if (DIRECTORY_SEPARATOR == '\\') { // windows
$realPath = str_replace('\\', '\\\\', $realPath);
return $this->db->real_escape_string($realPath);
* @param string $path dir path
* @param string $q search string
* @throws elFinderAbortException
* @author Dmitry (dio) Levashov
protected function doSearch($path, $q, $mimes)
if (!empty($this->doSearchCurrentQuery['matchMethod'])) {
// has custom match method use elFinderVolumeDriver::doSearch()
return parent::doSearch($path, $q, $mimes);
$timeout = $this->options['searchTimeout'] ? $this->searchStart + $this->options['searchTimeout'] : 0;
if ($path != $this->root || $this->rootHasParent) {
$dirs = $inpath = array(intval($path));
$in = '(' . join(',', $inpath) . ')';
$sql = 'SELECT f.id FROM %s AS f WHERE f.parent_id IN ' . $in . ' AND `mime` = \'directory\'';
$sql = sprintf($sql, $this->tbf);
if ($res = $this->query($sql)) {
while ($dat = $res->fetch_assoc()) {
$dirs = array_merge($dirs, $inpath);
foreach ($mimes as $mime) {
if (strpos($mime, '/') === false) {
$whrs[] = sprintf('f.mime LIKE \'%s/%%\'', $this->db->real_escape_string($mime));
$whrs[] = sprintf('f.mime = \'%s\'', $this->db->real_escape_string($mime));
$whr = join(' OR ', $whrs);
$whr = sprintf('f.name LIKE \'%%%s%%\'', $this->db->real_escape_string($q));
$whr = '(' . $whr . ') AND (`parent_id` IN (' . join(',', $dirs) . '))';
$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
$sql = sprintf($sql, $this->tbf, $whr);
if (($res = $this->query($sql))) {
while ($row = $res->fetch_assoc()) {
if ($timeout && $timeout < time()) {
$this->setError(elFinder::ERROR_SEARCH_TIMEOUT, $this->path($this->encode($path)));
if (!$this->mimeAccepted($row['mime'], $mimes)) {
if ($id == $this->root) {
if ($row['parent_id'] && $id != $this->root) {
$row['phash'] = $this->encode($row['parent_id']);
$row['path'] = $this->_path($id);
if ($row['mime'] == 'directory') {
unset($row['parent_id']);
if (($stat = $this->updateCache($id, $row)) && empty($stat['hidden'])) {
/*********************** paths/urls *************************/
* Return parent directory path
* @param string $path file path
* @author Dmitry (dio) Levashov
protected function _dirname($path)
return ($stat = $this->stat($path)) ? (!empty($stat['phash']) ? $this->decode($stat['phash']) : $this->root) : false;
* @param string $path file path
* @author Dmitry (dio) Levashov
protected function _basename($path)
return (($stat = $this->stat($path)) && isset($stat['name'])) ? $stat['name'] : false;
* Join dir name and file name and return full path
* @author Dmitry (dio) Levashov