error()) return false; $required = array( 'name', 'path', ); if (0 == $id) { $this->setReqAttr($required); return; } if (!$this->chkTable_()) return; $this->attr['id'] = $id; // Initialise object via Table object $this->Table->setId($id); // select data from table if (!$this->Table->select()) { $this->attr['id'] = -1; $this->setError_(get_class($this) . ': unable to select: ' . $this->Table->getErrorMsg()); return; } // fetch table row, $this->attr = $this->Table->fetchArray(); if (!$this->attr) { $this->attr = $this->resetAttr_(); $this->setError_(get_class($this) . ': unable to fetch array: ' . $this->Table->getErrorMsg()); return; } $this->oldAttr_ = $this->attr; $this->setReqAttr($required); } function &_fetch_users() { if (!is_array($this->_users)) { $this->_users = array(); $query = 'SELECT uid FROM ' . FILESERVER_TABLE_VALID_USERS . ' WHERE sid="' . $this->getId() . '"'; if (false === ($res = mysql_query($query))) { $this->setError_('mysql_query() failed: ' . db_error()); return false; } while(list($uid) = mysql_fetch_row($res)) $this->_users[$uid] = true; mysql_free_result($res); $this->_users_old = $this->_users; } return $this->_users; } function resetAttr_() { return array( 'id' => 0, 'name' => '', 'path' => '', 'description' => '', 'writable' => 'false', 'guest_ok' => 'false', 'valid_users' => 'any', ); } function getInputableAttr_() { $ret = $this->resetAttr_(); unset($ret['id']); return $ret; } function initFromSubmit() { if (false === cObject::initFromSubmit()) { $this->setError_('cObject::initFromSubmit() failed: ' . $this->getErrorMsg()); return false; } if ('select' == $this->getValidUsers()) { $users = get_gpc('users', array()); $this->clear_users(); foreach (explode(',', $users) AS $uid) { if (false === $this->add_user($uid)) { $this->setError_(get_class($this) . '::add_user() failed: ' . $this->getErrorMsg()); } } } //valid_users init to-do !!! return true; } function &get_users() { if (false === ($users =& $this->_fetch_users())) return false; foreach ($users AS $uid => $name) { if (is_object($name)) continue; include_once('modules/fileserver/FileServerUser.php'); $myFileServerUser =& objectMaker('FileServerUser', $uid); if ($myFileServerUser->error()) { $this->setError_('Unable to create FileServerUser objetc: ' . $this->getErrorMsg()); return false; } $users[$uid] =& $myFileServerUser; } return $users; } function add_user($id) { if (false === ($users =& $this->_fetch_users())) return false; $users[$id] = true; return true; } function del_user($id) { if (false === ($users =& $this->_fetch_users())) return false; unset($users[$id]); return true; } function clear_users() { if (false === ($users =& $this->_fetch_users())) return false; $users = array(); return true; } function isExist($name = '') { if (!(isset($this) && is_object($this) && get_class($this) == 'fileservershare')) return FileServerShare::isExist(0, $name); if (!$this->chkTable_()) return false; $ret = $this->Table->isExist($this->getId(), $this->getName()); if ($this->Table->error()) { $this->setError_($this->Table->getErrorMsg()); return false; } return $ret; } function update() { $create = 0 == $this->getId(); //update share in db if (false === cObject::update()) return false; if (is_array($this->_users)) { //update valid_users in db $deleted_users = array_diff_assoc($this->_users_old, $this->_users); foreach (array_keys($deleted_users) AS $uid) { $query = 'DELETE FROM ' . FILESERVER_TABLE_VALID_USERS . ' WHERE sid="' . $this->getId() . '" AND uid="' . $uid . '"'; if (false === mysql_query($query)) { $this->setError_('mysql_query() failed: ' . db_error()); return false; } } $added_users = array_diff_assoc($this->_users, $this->_users_old); foreach (array_keys($added_users) AS $uid) { $query = 'REPLACE INTO ' . FILESERVER_TABLE_VALID_USERS . ' SET sid="' . $this->getId() . '", uid="' . $uid . '"'; if (false === mysql_query($query)) { $this->setError_('mysql_query() failed: ' . db_error()); return false; } } } include_once('modules/fileserver/FileServerManager.php'); $myFileServerManager =& FileServerManagerMaker(); if ($myFileServerManager->error()) { $this->setError_('Unable to create FileServerManager object: ' . $myFileServerManager->getErrormsg()); return false; } if ('any' == $this->getValidUsers()) { $valid_users = false; } elseif ('none' == $this->getValidUsers()) { $valid_users = array(); } else { $valid_users = array(); foreach ($this->get_users() AS $myFileServerUser) $valid_users[] = $myFileServerUser->getName(); } if ($create) { if (false === $myFileServerManager->add_share($this->getName(), $this->getPath(), $this->isWritable(), $this->isGuestOk(), $this->getDescription(), $valid_users, true)) { $this->setError_('FileServerManager::add_share() failed: ' . $myFileServerManager->getErrorMsg()); return false; } } else { if ($this->isAttrChanged('name') && (false === $myFileServerManager->rename_share($this->getOldAttr('name'), $this->getName(), true))) { $this->setError_('FileServerManager::rename_share() failed: ' . $myFileServerManager->getErrorMsg()); return false; } if (($this->isAttrChanged('path') || $this->isAttrChanged('writable') || $this->isAttrChanged('guest_ok') || $this->isAttrChanged('description') || ( is_array($this->_users) && ( count(array_diff_assoc($this->_users_old, $this->_users)) > 0 || count(array_diff_assoc($this->_users, $this->_users_old)) > 0))) && (false === $myFileServerManager->update_share($this->getName(), $this->getPath(), $this->isWritable(), $this->isGuestOk(), $this->getDescription(), $valid_users, true))) { } } return true; } function delete() { if (0 == $this->getId()) return true; //clear valid users in db $query = 'DELETE FROM ' . FILESERVER_TABLE_VALID_USERS . ' WHERE sid="' . $this->getId() . '"'; if (false === mysql_query($query)) { $this->setError_('mysql_query() failed: ' . db_error()); return false; } //clear share in db if (false === cObject::delete()) return false; include_once('modules/fileserver/FileServerManager.php'); $myFileServerManager =& FileServerManagerMaker(); if ($myFileServerManager->error()) { $this->setError_('Unable to create FileServerManager object: ' . $myFileServerManager->getErrormsg()); return false; } if (false === $myFileServerManager->del_share($this->getName())) { $this->setError_('FileServerManager::add_user() failed: ' . $myFileServerManager->getErrorMsg()); return false; } return true; } function setName($name) { return $this->setAttr('name', $name); } function setPath($path) { return $this->setAttr('path', $path); } function setDescription($description) { return $this->setAttr('description', $description); } function setWritable($writable) { return $this->setAttr('writable', $writable); } function setValidUsers($valid_users) { return $this->setAttr('valid_users', $valid_users); } function onChange_valid_users_() { if ('any' == $this->getValidUsers()) $this->clear_users(); return true; } function getName() { return $this->getAttr('name'); } function getPath() { return $this->getAttr('path'); } function getDescription() { return $this->getAttr('description'); } function getWritable() { return $this->getAttr('writable'); } function getGuestOk() { return $this->getAttr('guest_ok'); } function getValidUsers() { return $this->getAttr('valid_users'); } function isWritable() { return 'true' == $this->getWritable(); } function isGuestOk() { return 'true' == $this->getGuestOk(); } function chk_name_($name) { return mb_strlen($name) > 0; } function chk_path_($path) { $ret = util_exec('filemng', array('psaadm', 'file', '-L', $path), 'lst'); if (false === $ret) { global $plesk_errormsg; FileManager::setError_('filemng file failed: ' . $plesk_errormsg); return false; } $re = "/^(?:.+):\s([^:]+)$/"; if (!preg_match($re, $ret[0], $matches) || 'directory' != $matches[1]) return false; include_once('class.Checker.php'); return Checker::filepath($path); } function chk_description_($description) { return true; } function chk_writable_($writable) { return in_array($writable, array('true', 'false')); } function chk_guest_ok_($guest_ok) { return in_array($guest_ok, array('true', 'false')); } function chk_valid_users_($valid_users) { return in_array($valid_users, array('any', 'select')); } } //______________________________________________________________________________ include_once('class.Table.php'); class FileServerShareTable extends Table { function FileServerShareTable($fieldNames) { if (!is_array($fieldNames)) { $this->setError_('field names are not an array'); return; } $tableCards = FILESERVER_TABLE_SHARES; $fNames = array_keys($fieldNames); $this->Table($tableCards, $fNames); } function isExist($id, $name) { $name = idn_toascii($name); $query = 'SELECT count(*) FROM ' . FILESERVER_TABLE_SHARES . ' WHERE name="' . mysql_escape_string($name) . '" AND id !="' . $id . '"'; $res = mysql_query($query); if (!$res) { if (is_object($this)) { $this->setError_('Unable to query: ' . db_error()); return false; } psaerror('Unable to query: ' . db_error()); } list($num) = mysql_fetch_row($res); mysql_free_result($res); return ($num + 0 > 0); } } ?>