domain.inc.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * Domain name management
  4. *
  5. * @package siteadm
  6. */
  7. class domain_manager extends db_object_manager
  8. {
  9. static protected $name = "domain";
  10. }
  11. /**
  12. * Domain name
  13. *
  14. * @package siteadm
  15. */
  16. class domain extends db_object
  17. {
  18. static protected $_name = "domain";
  19. static protected $_db_table = "domain";
  20. public $email_nb;
  21. public $email_alias_nb;
  22. public $website_nb;
  23. public $website_alias_nb;
  24. static public $_f = array
  25. (
  26. "account_id" => array("type"=>"object", "otype"=>"account"),
  27. "name" => array("type"=>"string", "nonempty"=>true),
  28. "email_actif" => array("type"=>"boolean"),
  29. "creation_date" => array("type"=>"date"),
  30. "renew_date" => array("type"=>"date"),
  31. );
  32. // ACCESS
  33. /**
  34. * Retrieve managing account
  35. *
  36. * @return account
  37. */
  38. public function account()
  39. {
  40. if ($account=account()->get($this->account_id))
  41. return $account;
  42. else
  43. return account_common();
  44. }
  45. /* FOLDERS */
  46. /**
  47. * Returns apache config folder name
  48. * @return string
  49. */
  50. function apache_conf_folder()
  51. {
  52. return $this->account()->conf_folder()."/apache";
  53. }
  54. /**
  55. * Returns websites log folder name
  56. * @return string
  57. */
  58. public function apache_log_folder()
  59. {
  60. return $this->account()->log_folder()."/apache";
  61. }
  62. /**
  63. * Returns awstats conf folder name
  64. * @return string
  65. */
  66. public function awstats_conf_folder()
  67. {
  68. return $this->account()->conf_folder()."/awstats";
  69. }
  70. /**
  71. * Returns awstats log folder name
  72. * @return string
  73. */
  74. public function awstats_log_folder()
  75. {
  76. return $this->account()->log_folder()."/awstats";
  77. }
  78. // FILES
  79. /**
  80. * Returns websites cumulative access log filename
  81. *
  82. * @return string
  83. */
  84. public function logaccess_file()
  85. {
  86. return $this->apache_log_folder()."/$this->name.access.log";
  87. }
  88. /**
  89. * Returns awstats cumulative file
  90. *
  91. * @return string
  92. */
  93. public function awstats_conf_file()
  94. {
  95. return $this->awstats_conf_folder()."/$this->name.conf";
  96. }
  97. /**
  98. * @return string
  99. */
  100. function ssl_cert_file()
  101. {
  102. return $this->apache_conf_folder()."/".$this->name.".crt";
  103. }
  104. /**
  105. * @return string
  106. */
  107. function ssl_key_file()
  108. {
  109. return $this->apache_conf_folder()."/".$this->name.".key";
  110. }
  111. /**
  112. * @return string
  113. */
  114. function ssl_csr_file()
  115. {
  116. return $this->apache_conf_folder()."/".$this->name.".csr";
  117. }
  118. /**
  119. * @return string
  120. */
  121. function ssl_info_file()
  122. {
  123. return $this->apache_conf_folder()."/".$this->name.".sslinfo";
  124. }
  125. // PERM
  126. /**
  127. * @see db_object::insert_perm()
  128. */
  129. static public function insert_perm()
  130. {
  131. // Admin
  132. if (login()->perm("admin"))
  133. {
  134. return "admin";
  135. }
  136. // Domain Manager
  137. elseif (login()->perm("manager"))
  138. {
  139. return "manager";
  140. }
  141. // User
  142. elseif (login()->id)
  143. {
  144. return "user";
  145. }
  146. else
  147. {
  148. return false;
  149. }
  150. }
  151. /**
  152. * @see db_object::update_perm()
  153. */
  154. public function update_perm()
  155. {
  156. // Admin
  157. if (login()->perm("admin"))
  158. {
  159. return "admin";
  160. }
  161. // Domain Manager
  162. elseif (($account=$this->account()) && $account->manager_id == login()->id)
  163. {
  164. return "manager";
  165. }
  166. // User
  167. elseif ($this->account_id == login()->id)
  168. {
  169. return "user";
  170. }
  171. else
  172. {
  173. return false;
  174. }
  175. }
  176. // UPDATE
  177. /**
  178. * @see db_object::insert()
  179. */
  180. public function insert($infos)
  181. {
  182. if (!($perm=static::insert_perm()))
  183. return false;
  184. if ($perm != "admin" && ($perm != "manager" || false))
  185. {
  186. $infos["account_id"] = login()->id;
  187. }
  188. return db_object::insert($infos);
  189. }
  190. /**
  191. * @see db_object::update()
  192. */
  193. public function update($infos)
  194. {
  195. if (!($perm=$this->update_perm()))
  196. return false;
  197. if ($perm != "admin")
  198. {
  199. if (isset($infos["account_id"]))
  200. unset($infos["account_id"]);
  201. }
  202. if (isset($infos["name"]))
  203. unset($infos["name"]);
  204. return db_object::update($infos);
  205. }
  206. /* DB */
  207. /**
  208. * @see db_object::db_retrieve_more()
  209. */
  210. function db_retrieve_more($id)
  211. {
  212. // TODO : voir si besoin de mise à jour systématique
  213. $this->email_nb = array_pop(mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `email` WHERE `domain_id`='$this->id'")));
  214. $this->email_alias_nb = array_pop(mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `email_alias` WHERE `domain_id`='$this->id'")));
  215. $this->website_nb = array_pop(mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `website` WHERE `domain_id`='$this->id'")));
  216. $this->website_alias_nb = array_pop(mysql_fetch_row(mysql_query("SELECT COUNT(*) FROM `website_alias` WHERE `domain_id`='$this->id'")));
  217. }
  218. /* REPLACE MAP */
  219. function replace_map()
  220. {
  221. $map = array
  222. (
  223. "{DOMAIN_NAME}" => $this->name,
  224. "{DOMAIN_NAME_REGEX}" => str_replace(".", "\\.", $this->name),
  225. "{DOMAIN_LOG_ACCESS}" => $this->logaccess_file(),
  226. "{AWSTATS_DATA_DIR}" => $this->awstats_log_folder(),
  227. "{DOMAIN_SSL_CERT}" => $this->ssl_cert_file(),
  228. "{DOMAIN_SSL_KEY}" => $this->ssl_key_file(),
  229. );
  230. if ($account=$this->account())
  231. replace_map_merge($map, $account->replace_map());
  232. return $map;
  233. }
  234. /* ROOT SCRIPTS */
  235. /**
  236. * Create SSL certificate
  237. */
  238. function script_ssl_create()
  239. {
  240. $account = $this->account();
  241. $replace_map = $this->replace_map();
  242. $account->rm($this->ssl_csr_file());
  243. $account->rm($this->ssl_cert_file());
  244. $account->rm($this->ssl_key_file());
  245. $account->copy_tpl("ssl/ssl-info", $this->ssl_info_file(), $replace_map);
  246. exec("openssl genrsa -out ".$this->ssl_key_file()." 1024");
  247. exec("cat ".$this->ssl_info_file()." | openssl req -new -key ".$this->ssl_key_file()." -out ".$this->ssl_csr_file());
  248. exec("openssl x509 -req -days 3650 -in ".$this->ssl_csr_file()." -CA ".SSL_CA_CRT." -CAkey ".SSL_CA_KEY." -CAcreateserial -out ".$this->ssl_cert_file());
  249. $account->rm($this->ssl_info_file());
  250. filesystem::chmod($this->ssl_csr_file(), "640");
  251. filesystem::chmod($this->ssl_cert_file(), "640");
  252. filesystem::chmod($this->ssl_key_file(), "640");
  253. }
  254. /**
  255. * @see db_object::script_structure()
  256. */
  257. function script_structure()
  258. {
  259. $this->account()->mkdir(SITEADM_DOMAIN_DIR."/".$this->name, "750", "root");
  260. }
  261. /**
  262. * @see db_object::script_insert()
  263. */
  264. function script_insert()
  265. {
  266. $this->script_ssl_create();
  267. db_object::script_insert();
  268. }
  269. /**
  270. * @see db_object::script_update()
  271. */
  272. function script_update()
  273. {
  274. $account = $this->account();
  275. $replace_map = $this->replace_map();
  276. // Awstats
  277. $account->copy_tpl("awstats/awstats.domain.conf", $this->awstats_conf_file(), $replace_map, "644", "root");
  278. filesystem::link($this->awstats_conf_file(), AWSTATS_CONFIG_DIR."/awstats.$this->name.conf");
  279. }
  280. /**
  281. * @see db_object::script_delete()
  282. */
  283. function script_delete()
  284. {
  285. $account->rm($this->awstats_conf_file());
  286. filesystem::rm(AWSTATS_CONFIG_DIR."/awstats.$this->name.conf");
  287. filesystem::rmdir(SITEADM_DOMAIN_DIR."/".$this->name);
  288. }
  289. }
  290. ?>