email.inc.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /**
  3. * eMail box management
  4. *
  5. * @package siteadm
  6. */
  7. class email_manager extends db_object_manager
  8. {
  9. static protected $name = "email";
  10. }
  11. /**
  12. * eMail box
  13. *
  14. * @package siteadm
  15. */
  16. class email extends db_object
  17. {
  18. static protected $_name = "email";
  19. static protected $_db_table = "email";
  20. static public $_f = array
  21. (
  22. "name" => array("type"=>"string", "nonempty"=>true),
  23. "domain_id" => array("type"=>"object", "otype"=>"domain", "nonempty"=>true),
  24. "account_id" => array("type"=>"object", "otype"=>"account"),
  25. "password" => array("type"=>"string"),
  26. "quota" => array("type"=>"select", "list"=>array(10, 100, 1000, 10000)),
  27. "actif" => array("type"=>"boolean", "default"=>"1"),
  28. );
  29. /**
  30. * @see db_object::__toString()
  31. */
  32. function __toString()
  33. {
  34. return $this->name();
  35. }
  36. /**
  37. * @return string;
  38. */
  39. function name()
  40. {
  41. return $this->name."@".$this->domain()->name;
  42. }
  43. // ACCESS
  44. /**
  45. * Returns the associated domain
  46. *
  47. * @return domain
  48. */
  49. function domain()
  50. {
  51. return domain()->get($this->domain_id);
  52. }
  53. /**
  54. * Returns the associated account
  55. *
  56. * Attention, pas toujours le compte de gestion du domaine !
  57. * @return account
  58. */
  59. function account()
  60. {
  61. if ($account=account()->get($this->account_id))
  62. return $account;
  63. elseif ($domain=$this->domain())
  64. return $domain->account();
  65. }
  66. /* FOLDERS */
  67. /**
  68. * @return string
  69. */
  70. function folder()
  71. {
  72. return $this->account()->email_folder()."/".$this->name();
  73. }
  74. // PERM
  75. /**
  76. * @see db_object::insert_perm()
  77. */
  78. static public function insert_perm()
  79. {
  80. // Admin
  81. if (login()->perm("admin"))
  82. {
  83. return "admin";
  84. }
  85. // Special account access
  86. elseif (login()->perm("manager"))
  87. {
  88. return "domain_manager";
  89. }
  90. // Domain User
  91. elseif (login()->id)
  92. {
  93. return "domain_user";
  94. }
  95. else
  96. {
  97. return false;
  98. }
  99. }
  100. /**
  101. * @see db_object::update_perm()
  102. */
  103. public function update_perm()
  104. {
  105. // Admin
  106. if (login()->perm("admin"))
  107. {
  108. return "admin";
  109. }
  110. // Special account access
  111. elseif ($account=account($this->account_id))
  112. {
  113. if ($this->account_id == login()->id)
  114. return "user";
  115. elseif ($account->manager_id == login()->id)
  116. return "manager";
  117. else
  118. return false;
  119. }
  120. // Domain Manager
  121. elseif (login()->perm("manager") && ($domain=$this->domain()) && ($account=$domain->account()) && $account->manager_id == login()->id)
  122. {
  123. return "domain_manager";
  124. }
  125. // Domain User
  126. elseif (($domain=$this->domain()) && $domain->account_id == login()->id)
  127. {
  128. return "domain_user";
  129. }
  130. else
  131. {
  132. return false;
  133. }
  134. }
  135. // UPDATE
  136. /**
  137. * @see db_object::insert($infos)
  138. */
  139. public function insert($infos)
  140. {
  141. if (!($perm=static::insert_perm()) || !is_array($infos))
  142. return false;
  143. if ($perm != "admin" && (!isset($infos["domain_id"]) || !($domain=domain($infos["domain_id"])) || $domain->update_perm()))
  144. {
  145. return false;
  146. }
  147. return db_object::insert($infos);
  148. }
  149. /**
  150. * @see db_object::insert($update)
  151. */
  152. public function update($infos)
  153. {
  154. if (!($perm=$this->update_perm()) || !is_array($infos))
  155. return false;
  156. if (isset($infos["domain_id"]))
  157. unset($infos["domain_id"]);
  158. if (isset($infos["name"]))
  159. unset($infos["name"]);
  160. return db_object::update($infos);
  161. }
  162. /* ROOT SCRIPTS */
  163. /**
  164. * @see db_object::script_update()
  165. */
  166. function script_update()
  167. {
  168. filesystem::link($this->folder(), SITEADM_EMAIL_DIR."/".$this->name());
  169. }
  170. }
  171. ?>