123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986 |
- <?php
- /**
- * User account management
- *
- * @package siteadm
- */
- class account_manager extends db_object_manager
- {
- static protected $name = "account";
- }
- /**
- * User account
- *
- * @package siteadm
- */
- class account extends db_object
- {
- static protected $_name = "account";
- static protected $_db_table = "account";
- protected $password;
- public $language_bin_list = array();
- static public $_f = array
- (
- "type" => array("type"=>"select", "list"=>array("user", "manager", "admin", 'shared'), "nonempty"=>true),
- "name" => array("type"=>"string", "nonempty"=>true),
- "folder" => array("type"=>"string"),
- "actif" => array("type"=>"bool"),
- "manager_id" => array("type"=>"object", "otype"=>"account"),
- "offre_id" => array("type"=>"object", "otype"=>"offer"),
- "civilite" => array("type"=>"select", "list"=>array('m', 'mme', 'mlle')),
- "nom" => array("type"=>"string"),
- "prenom" => array("type"=>"string"),
- "email" => array("type"=>"string"),
- "societe" => array("type"=>"string"),
- "disk_quota_soft" => array("type"=>"int"),
- "disk_quota_hard" => array("type"=>"int"),
- );
- /**
- * @see db_object::__toString()
- */
- function __toString()
- {
- return "$this->nom $this->prenom [$this->name]";
- }
- /**
- * @see db_object::url()
- */
- function url()
- {
- return "account.php?id=$this->id";
- }
- // ACCESS
- function manager()
- {
- return account()->get($this->manager_id);
- }
- /**
- * @return offer
- */
- function offer()
- {
- return offer()->get($this->offre_id);
- }
- function language_bin_list()
- {
- if (!$this->id)
- return array();
- $list = array();
- $query_string = "SELECT `language_bin_id` FROM `account_language_bin_ref` WHERE `account_id`='$this->id'";
- $query = mysql_query($query_string);
- while (list($id)=mysql_fetch_row($query))
- {
- $list[] = language_bin($id);
- }
- return $list;
- }
- function phpapp_list()
- {
- $list = array();
- $query_string = "SELECT t1.id FROM phpapp as t1 WHERE t1.account_id IS NULL OR t1.account_id = '$this->id'";
- $query = mysql_query($query_string);
- while(list($id)=mysql_fetch_row($query))
- $list[] = phpapp($id);
- return $list;
- }
- function phppool_list()
- {
- $list = array();
- $query_string = "SELECT t1.id FROM phppool as t1 LEFT JOIN phpapp AS t2 ON t2.id=t1.phpapp_id AND (t2.account_id IS NULL OR t2.account_id='$this->id') WHERE t1.account_id IS NULL OR t1.account_id = '$this->id'";
- $query = mysql_query($query_string);
- while(list($id)=mysql_fetch_row($query))
- $list[] = phppool($id);
- return $list;
- }
- function phpext_list($language_bin_id=null)
- {
- $list = array();
- $query_string = "SELECT language_php_ext.*, if(language_php_bin_ext_ref.phpext_id, 1, 0) as `already`, if (account_php_ext_ref.account_id, 1, 0) as `authorized`
- FROM language_php_ext
- LEFT JOIN account_php_ext_ref ON language_php_ext.id=account_php_ext_ref.phpext_id AND account_php_ext_ref.account_id='".$this->id."'
- LEFT JOIN language_php_bin_ext_ref ON language_php_ext.id=language_php_bin_ext_ref.phpext_id AND language_php_bin_ext_ref.language_bin_id='".$language_bin_id."'
- WHERE language_php_ext.type='extension'
- ORDER BY language_php_ext.description";
- $query = mysql_query($query_string);
- while($row=mysql_fetch_assoc($query))
- {
- $list[$row["id"]] = $row;
- }
- return $list;
- }
- /**
- * OS (Linux) system user id (uid) and group id (gid)
- *
- * @return int
- */
- public function system_id()
- {
- return (ACCOUNT_UID_MIN+$this->id);
- }
- /**
- * OS (Linux) system user name
- *
- * @return string
- */
- public function system_user()
- {
- return "$this->name$this->id";
- }
- /**
- * OS (Linux) system group name
- *
- * @return string
- */
- public function system_group()
- {
- return $this->system_user();
- }
- /**
- * PHP user ID
- * @return int
- */
- function php_id()
- {
- return (PHP_UID_MIN+$this->id);
- }
- function php_user()
- {
- return "php_".$this->name;
- }
- function php_group()
- {
- return $this->system_group();
- }
- /**
- * Email user ID
- * @return int
- */
- function email_id()
- {
- return (EMAIL_UID_MIN+$this->id);
- }
- function email_user()
- {
- return "email_".$this->name;
- }
- function email_group()
- {
- return $this->system_group();
- }
- /**
- * Update usergroup relative to account
- * @param string $usergroup
- */
- function usergroup(&$usergroup)
- {
- if (is_null($usergroup))
- $usergroup = $this->system_user().":".$this->system_group();
- elseif (!is_numeric($pos=strpos($usergroup, ":")))
- $usergroup = $usergroup.":".$this->system_group();
- elseif ($pos == 0)
- $usergroup = $this->system_user().":".$usergroup;
- }
- /* FOLDERS */
- /**
- * Returns root folder
- *
- * @return string
- */
- function folder()
- {
- return SITEADM_USER_DIR."/$this->folder";
- }
- /**
- * Update folder relative to account
- * @param string $folder
- * @return string
- */
- function subfolder(&$folder)
- {
- if (substr($folder, 0, 1) != "/")
- $folder = $this->folder()."/".$folder;
- }
- /**
- * Log folder
- *
- * @return string
- */
- function log_folder()
- {
- return $this->folder()."/log";
- }
- /**
- * Conf folder
- *
- * @return string
- */
- function conf_folder()
- {
- return $this->folder()."/conf";
- }
- /**
- * PHP sockets folder
- *
- * @return string
- */
- function socket_folder()
- {
- return $this->folder()."/socket";
- }
- /**
- * Public folder
- *
- * @return string
- */
- function public_folder()
- {
- return $this->folder()."/public";
- }
- /**
- * Private folder
- *
- * @return string
- */
- function private_folder()
- {
- return $this->folder()."/private";
- }
- /**
- * CGI-BIN folder
- *
- * @return string
- */
- function cgi_folder()
- {
- return $this->folder()."/cgi-bin";
- }
- /**
- * Temp folder
- *
- * @return string
- */
- function tmp_folder()
- {
- return $this->folder()."/tmp";
- }
- /**
- * Sessions folder
- *
- * @return string
- */
- function session_folder()
- {
- return $this->folder()."/cookies";
- }
- /**
- * Dossier de stockage des emails associés au compte
- * @return string
- */
- function email_folder()
- {
- return $this->folder()."/mail";
- }
- /**
- * Dossier de stockage des backup
- * @return string
- */
- function backup_folder()
- {
- return $this->folder()."/backup";
- }
- // PERM
- /**
- * @see db_object::insert_perm()
- */
- static function insert_perm()
- {
- // Admin
- if (login()->perm("admin"))
- {
- return "admin";
- }
- // Manager
- elseif (login()->perm("manager"))
- {
- return "manager";
- }
- // Other (user or not connected)
- else
- {
- return false;
- }
- }
- /**
- * @see db_object::update_perm()
- */
- function update_perm()
- {
- // Admin
- if (login()->perm("admin"))
- {
- return "admin";
- }
- // Manager
- elseif (($manager=$this->manager()) && $manager->id == login()->id)
- {
- return "manager";
- }
- // User
- elseif ($this->id && $this->id == login()->id)
- {
- return "user";
- }
- // Bad User or not connected
- else
- {
- return false;
- }
- }
- /**
- * Returns if the account has the permission $name
- *
- * @param string $name
- * @return boolean
- */
- public function perm($name)
- {
- if ($name == "manager")
- return (in_array($this->type, array("manager", "admin")));
- elseif ($name == "admin")
- return ($this->type == "admin");
- }
- // UPDATE
- /**
- * @see db_object::insert()
- */
- public function insert($infos)
- {
- if (!($perm=static::insert_perm()) || !is_array($infos))
- return false;
- if ($perm == "manager")
- {
- $infos["manager_id"] = login()->id;
- $infos["type"] = "user";
- }
- if (!isset($infos["name"]) || !is_string($infos["name"]) || !strlen($infos["name"]))
- return false;
- $infos["folder"] = $infos["name"];
- return db_object::insert($infos);
- }
- /**
- * @see db_object::update()
- */
- public function update($infos)
- {
- if (!($perm=$this->update_perm()))
- return false;
- if ($perm == "user" || $perm == "manager")
- {
- if (isset($infos["manager_id"]))
- unset($infos["manager_id"]);
- if (isset($infos["type"]))
- unset($infos["type"]);
- }
- if ($perm == "user")
- {
- if (isset($infos["actif"]))
- unset($infos["actif"]);
- if (isset($infos["offre_id"]))
- unset($infos["offre_id"]);
- }
- if (isset($infos["folder"]))
- unset($infos["folder"]);
- return db_object::update($infos);
- }
- // DB
- /**
- * @see db_object::db_retrieve()
- */
- protected function db_retrieve($id)
- {
- if (!db_object::db_retrieve($id))
- return false;
- $this->language_bin_list = array();
- $query_string = "SELECT `language_bin_id` FROM `account_language_bin_ref` WHERE `account_id`='$this->id'";
- $query = mysql_query($query_string);
- while(list($lang_id)=mysql_fetch_row($query))
- $this->language_bin_list[] = $lang_id;
- return true;
- }
- /**
- * @see db_object::db_insert()
- */
- protected function db_insert($infos)
- {
- if (db_object::db_insert($infos))
- {
- if (isset($infos["language_bin_list"]) && is_array($infos["language_bin_list"]))
- {
- $query_list = array();
- foreach($infos["language_bin_list"] as $lang_id)
- $query_list[] = "('$this->id', '$lang_id')";
- if (count($query_list))
- {
- $query_string = "INSERT INTO account_language_bin_ref (account_id, language_bin_id) VALUES ".implode(" , ",$query_list);
- mysql_query($query_string);
- }
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- /**
- * @see db_object::db_update()
- */
- protected function db_update($infos)
- {
- $return = false;
- if (isset($infos["language_bin_list"]))
- {
- mysql_query("DELETE FROM `account_language_bin_ref` WHERE `account_id`='$this->id'");
- if (is_array($infos["language_bin_list"]))
- {
- $query_list = array();
- foreach($infos["language_bin_list"] as $lang_id)
- $query_list[] = "('$this->id', '$lang_id')";
- if (count($query_list))
- {
- $query_string = "INSERT INTO account_language_bin_ref (account_id, language_bin_id) VALUES ".implode(" , ",$query_list);
- mysql_query($query_string);
- }
- }
- $return = true;
- }
- return (db_object::db_update($infos) || $return);
- }
- /* REPLACE MAP */
- /**
- * Create replacement map for template files
- *
- * @return array
- */
- function replace_map()
- {
- $map = array
- (
- "{ACCOUNT_ID}" => $this->id,
- "{ACCOUNT_NAME}" => $this->name,
- "{ACCOUNT_SYSTEM_ID}" => $this->system_id(),
- "{ACCOUNT_SYSTEM_USER}" => $this->system_user(),
- "{ACCOUNT_SYSTEM_GROUP}" => $this->system_group(),
- "{ACCOUNT_EMAIL}" => $this->email,
- "{ACCOUNT_ROOT}" => $this->folder(),
- "{ACCOUNT_PUBLIC}" => $this->public_folder(),
- "{ACCOUNT_TMP_DIR}" => $this->tmp_folder(),
- "{ACCOUNT_CGI_DIR}" => $this->cgi_folder(),
- "{PHP_TMP_DIR}" => $this->tmp_folder(),
- "{PHP_BASEDIR}" => $this->public_folder(),
- );
- replace_map_merge($map, replace_map());
- return $map;
- }
- /* ROOT METHODS */
- /**
- * Update OS account password
- *
- * This method will also save the password in /path/to/account/private
- * @param string $passwd
- */
- protected function password_update($passwd=null)
- {
- $passfile = $this->folder()."/private/passwd";
- $passfile_crypt = $this->folder()."/private/passwd_crypt";
- if (!$passwd) // TODO : tests de robustesse de mot de passe
- {
- exec("makepasswd --chars 8 > $passfile");
- $passwd = file_get_contents($passfile);
- $passwd = str_replace(array("\r\n","\n","\r"), "", $passwd);
- }
- else
- {
- filesystem::write($passfile_crypt, $passwd);
- }
- $this->passwd = $passwd;
- // Encrypt password
- exec("makepasswd --crypt --clearfrom $passfile > $passfile_crypt");
- unlink($passfile);
- filesystem::chmod($passfile_crypt, "600");
- $passwd_crypt = array_pop(explode(" ",fread(fopen($passfile_crypt,"r"),filesize($passfile_crypt))));
- $passwd_crypt = str_replace(array("\r\n","\n","\r"), "", $passwd_crypt);
- // Update password in database
- mysql_query("UPDATE `account` SET `password`='$passwd' WHERE `id`='$this->id'");
- // Update system password
- exec("usermod -p $passwd_crypt ".$this->system_user());
- }
- /**
- * Update quota
- */
- protected function quota_update()
- {
- // Limitations by offer
- if ($offer=$this->offer())
- {
- if ($offer->disk_quota_soft)
- exec("quotatool -u ".$this->system_user()." -b -q ".($offer->disk_quota_soft*1024)."MB /");
- else
- exec("quotatool -u ".$this->system_user()." -b -q 0 /");
- if ($offer->disk_quota_hard)
- exec("quotatool -u ".$this->system_user()." -b -l ".($offer->disk_quota_soft*1024)."MB /");
- else
- exec("quotatool -u ".$this->system_user()." -b -l 0 /");
- }
- // No limitations
- else
- {
- exec("quotatool -u ".$this->system_user()." -b -q 0 -l 0 /");
- }
- }
- /**
- * Create a subfolder in the account root and eventually chown it
- *
- * @param string $folder
- * @param string $mode
- * @param string $usergroup
- */
- function mkdir($folder, $mode="750", $usergroup=null)
- {
- $this->subfolder($folder);
- $this->usergroup($usergroup);
- filesystem::mkdir($folder);
- $this->chmod($folder, $mode);
- $this->chown($folder, $usergroup);
- }
- /**
- * Delete a folder and all subfolders
- *
- * @param string $folder
- */
- function rmdir($folder)
- {
- $this->subfolder($folder);
- if ($folder && $folder!= "/")
- filesystem::rmdir($folder);
- }
- /**
- * Delete a folder and all subfolders
- *
- * @param string $folder
- */
- function rm($file)
- {
- $this->subfolder($file);
- filesystem::unlink($file);
- }
- /**
- * Chown a file in the account root
- *
- * @param string $file
- * @param string $usergroup
- */
- function chown($file, $usergroup=null, $recursive=false)
- {
- $this->subfolder($file);
- $this->usergroup($usergroup);
- filesystem::chown($file, $usergroup, $recursive);
- }
- /**
- * Chown a file in the account root
- *
- * @param string $file
- * @param string $usergroup
- */
- function chmod($file, $mode="750")
- {
- $this->subfolder($file);
- filesystem::chmod($file, $mode);
- }
- /**
- * Copy a template file into the account root
- *
- * @param string $file_from
- * @param string $file_to
- * @param array $replace_map
- * @param string $mode
- * @param string $usergroup
- */
- function copy_tpl($file_from, $file_to, $replace_map=array(), $mode="0640", $usergroup=null)
- {
- $this->subfolder($file_to);
- $this->usergroup($usergroup);
- copy_tpl($file_from, $file_to, $replace_map, $mode, $usergroup);
- }
- /* ROOT ACCESS SCRIPTS */
- function root_foldersize($folder)
- {
- $this->subfolder($folder);
- $command = "sudo ".SITEADM_SCRIPT_DIR."/db_object.psh account $this->id foldersize $folder";
- return shell_exec($command);
- }
- function root_tail($filename)
- {
- $this->subfolder($filename);
- $command = "sudo ".SITEADM_SCRIPT_DIR."/db_object.psh account $this->id tail $filename";
- return shell_exec($command);
- }
- function root_quota()
- {
- $command = "sudo ".SITEADM_SCRIPT_DIR."/db_object.psh account $this->id quota";
- return shell_exec($command);
- }
- /* ROOT SCRIPTS */
- /**
- * Update account password
- * @param string $password
- */
- function script_password_update($password=null)
- {
- $this->password_update($password);
- }
- /**
- * Create directory structure
- */
- public function script_structure()
- {
- $this->mkdir("", "750", "root");
- filesystem::setacl($this->folder(), WEBSERVER_USER);
- // SSH
- $this->mkdir(".ssh", "700");
- // Config files
- $this->mkdir("conf", "750", "root");
- filesystem::setacl($this->conf_folder(), WEBSERVER_USER);
- filesystem::setacl($this->conf_folder(), AWSTATS_USER);
- // Awstats
- $this->mkdir("conf/awstats", "750", "root");
- filesystem::setacl($this->conf_folder()."/awstats", AWSTATS_USER);
- // Apache
- $this->mkdir("conf/apache", "750", "root");
- filesystem::setacl($this->folder()."/conf/apache", WEBSERVER_USER);
- // PHP
- $this->mkdir("conf/php", "750", "root");
- $this->mkdir("conf/php/pool", "755", "root");
- $this->mkdir("conf/php/ext", "755", "root");
- $this->mkdir("conf/php/vhost", "755", "root");
- // Fetchmail
- $this->mkdir("conf/fetchmail", "750", "root");
- // CRON
- $this->mkdir("conf/cron", "750", "root");
- // CGI-BIN
- $this->mkdir("cgi-bin", "750", "root");
- filesystem::setacl($this->cgi_folder(), WEBSERVER_USER);
- // Backup
- $this->mkdir("backup", "750", "root");
- $this->mkdir("backup/mysql", "755", "root");
- // Logs
- $this->mkdir("log", "750", "root");
- filesystem::setacl($this->log_folder(), WEBSERVER_USER);
- filesystem::setacl($this->log_folder(), AWSTATS_USER);
- $this->mkdir("log/apache", "1750", "root");
- filesystem::setacl($this->log_folder()."/apache", WEBSERVER_USER);
- filesystem::setacl($this->log_folder()."/apache", AWSTATS_USER);
- $this->mkdir("log/php", "1750", $this->php_user());
- $this->mkdir("log/awstats", "1770", "root");
- filesystem::setacl($this->log_folder()."/awstats", AWSTATS_USER, "rwx");
- exec("setfacl -m m::rwx ".$this->log_folder()."/awstats");
- exec("setfacl -m g::rx ".$this->log_folder()."/awstats");
- // Temp (PHP)
- $this->mkdir("tmp", "1777", "root");
- // Cookies (PHP)
- $this->mkdir("cookies", "700", $this->php_user());
- // Socket (PHP)
- $this->mkdir("socket", "750", "root:root");
- filesystem::setacl($this->socket_folder(), $this->php_user());
- filesystem::setacl($this->socket_folder(), WEBSERVER_USER);
- // Private data & config
- $this->mkdir("private", "750", "root");
- $this->mkdir("private/data", "750");
- $this->mkdir("private/ftp", "750");
- // Public websites
- $this->mkdir("public", "750", "root");
- filesystem::setacl($this->public_folder(), WEBSERVER_USER);
- $this->mkdir("public/data", "750");
- $this->mkdir("public/ftp", "755");
- // eMail
- $this->mkdir("mail", "700", $this->email_user());
- }
- /**
- * @see db_object::script_update()
- */
- function script_update()
- {
- $disk_quota_soft = ($this->disk_quota_soft)?$this->disk_quota_soft:0;
- $disk_quota_hard = ($this->disk_quota_hard)?$this->disk_quota_hard:0;
- // gestion de quota
- if ($offer=$this->offer)
- {
- if ($offer->disk_quota_soft && !$disk_quota_soft)
- $disk_quota_soft = $offer->disk_quota_soft;
- if ($offer->disk_quota_hard && !$disk_quota_hard)
- $disk_quota_hard = $offer->disk_quota_hard;
- }
- exec("quotatool -g ".$this->system_group()." -b -q ".(1024*$disk_quota_soft)."MB ".QUOTA_DISK);
- exec("quotatool -g ".$this->system_group()." -b -q ".(1024*$disk_quota_hard)."MB ".QUOTA_DISK);
- }
- /**
- * @see db_object::script_insert()
- */
- function script_insert()
- {
- // Add system group
- exec("groupadd -g ".$this->system_id()." ".$this->system_group());
- // Add system user
- exec("useradd -u ".$this->system_id()." -g ".$this->system_id()." -d ".$this->public_folder()." -s /bin/bash ".$this->system_user());
- // Add system php user
- exec("useradd -u ".$this->php_id()." -g ".$this->system_id()." -d ".$this->public_folder()." -s /bin/false ".$this->php_user());
- // Add system email user
- exec("useradd -u ".$this->email_id()." -g ".$this->system_id()." -d ".$this->email_folder()." -s /bin/false ".$this->email_user());
- // Add user in siteadm_account group,
- // so that sshd_config section authorize only internal-sftp for users matching group ACCOUNT_SYSTEM_GROUP
- exec("addgroup ".$this->system_user()." ".ACCOUNT_SYSTEM_GROUP);
- // User Root folder
- $this->mkdir("", "750", "root");
- $this->script_structure();
- $this->script_password_update();
- $this->script_update();
- }
- /**
- * Returns the quota used by the user's group
- */
- function script_quota()
- {
- echo shell_exec("quota -vlws -g ".$this->system_group());
- }
- /**
- * Returns the size of a folder
- * @param string $folder
- */
- function script_foldersize($folder)
- {
- echo filesystem::foldersize($folder);
- }
- /**
- * Returns the size of a folder
- * @param string $folder
- */
- function script_tail($filename)
- {
- echo filesystem::tail($filename);
- }
- }
- ?>
|