list-virtual.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. /**
  3. * Postfix Admin
  4. *
  5. * LICENSE
  6. * This source file is subject to the GPL license that is bundled with
  7. * this package in the file LICENSE.TXT.
  8. *
  9. * Further details on the project are available at http://postfixadmin.sf.net
  10. *
  11. * @version $Id: list-virtual.php 1777 2015-04-06 22:09:18Z christian_boltz $
  12. * @license GNU GPL v2 or later.
  13. *
  14. * File: list-virtual.php
  15. * List virtual users for a domain.
  16. *
  17. * Template File: list-virtual.php
  18. *
  19. * Form POST \ GET Variables:
  20. *
  21. * fDomain
  22. * fDisplay
  23. * search
  24. */
  25. require_once('common.php');
  26. authentication_require_role('admin');
  27. $admin_username = authentication_get_username();
  28. $list_domains = list_domains_for_admin($admin_username);
  29. $page_size = $CONF['page_size'];
  30. $fDomain = safepost('fDomain', safeget('domain', safesession('list-virtual:domain')));
  31. if (safesession('list-virtual:domain') != $fDomain) {
  32. unset($_SESSION['list-virtual:limit']);
  33. }
  34. $fDisplay = (int) safepost('limit', safeget('limit', safesession('list-virtual:limit')));
  35. $search = safepost('search', safeget('search', array())); # not remembered in the session
  36. if (!is_array($search)) {
  37. die(Config::Lang('invalid_parameter'));
  38. }
  39. if (count($list_domains) == 0) {
  40. if (authentication_has_role('global-admin')) {
  41. flash_error($PALANG['no_domains_exist']);
  42. } else {
  43. flash_error($PALANG['no_domains_for_this_admin']);
  44. }
  45. header("Location: list.php?table=domain"); # no domains (for this admin at least) - redirect to domain list
  46. exit;
  47. }
  48. if ((is_array ($list_domains) and sizeof ($list_domains) > 0)) {
  49. if (empty ($fDomain)) {
  50. $fDomain = escape_string($list_domains[0]);
  51. }
  52. }
  53. if(!in_array($fDomain, $list_domains)) {
  54. flash_error( $PALANG['invalid_parameter'] );
  55. unset($_SESSION['list-virtual:domain']);
  56. header("Location: list.php?table=domain"); # invalid domain, or not owned by this admin
  57. exit;
  58. }
  59. if (!check_owner(authentication_get_username(), $fDomain)) {
  60. flash_error( $PALANG['invalid_parameter'] . " If you see this message, please open a bugreport"); # this check is most probably obsoleted by the in_array() check above
  61. unset($_SESSION['list-virtual:domain']);
  62. header("Location: list.php?table=domain"); # domain not owned by this admin
  63. exit(0);
  64. }
  65. // store domain and page browser offset in $_SESSION so after adding/editing aliases/mailboxes we can
  66. // take the user back to the appropriate domain listing.
  67. $_SESSION['list-virtual:domain'] = $fDomain;
  68. $_SESSION['prefill:alias:domain'] = $fDomain;
  69. $_SESSION['prefill:mailbox:domain'] = $fDomain;
  70. $_SESSION['prefill:aliasdomain:target_domain'] = $fDomain;
  71. $_SESSION['list-virtual:limit'] = $fDisplay;
  72. #
  73. # alias domain
  74. #
  75. if (Config::bool('alias_domain')) {
  76. $handler = new AliasdomainHandler(0, $admin_username);
  77. $formconf = $handler->webformConfig(); # might change struct
  78. $aliasdomain_data = array(
  79. 'struct' => $handler->getStruct(),
  80. 'msg' => $handler->getMsg(),
  81. 'formconf' => $formconf,
  82. );
  83. $aliasdomain_data['msg']['show_simple_search'] = False; # hide search box
  84. $aliasdomain_data['msg']['can_create'] = 1;
  85. # hide create button if all domains (of this admin) are already used as alias domains
  86. $handler->getList("");
  87. if ( count($handler->result()) + 1 >= count($list_domains) ) $aliasdomain_data['msg']['can_create'] = 0; # all domains (of this admin) are already alias domains
  88. # get the really requested list
  89. if (count($search) == 0) {
  90. $list_param = "alias_domain='$fDomain' OR target_domain='$fDomain'";
  91. } else {
  92. $list_param = $search;
  93. }
  94. $handler->getList($list_param);
  95. $tAliasDomains = $handler->result();
  96. foreach ($tAliasDomains as $row) {
  97. if ($row['alias_domain'] == $fDomain) {
  98. $aliasdomain_data['struct']['target_domain']['linkto'] = 'target';
  99. if (count($search) == 0) {
  100. $aliasdomain_data['struct']['alias_domain']['linkto'] = '';
  101. $aliasdomain_data['msg']['can_create'] = 0; # domain is already an alias domain
  102. }
  103. }
  104. }
  105. if (count($search) > 0) {
  106. $aliasdomain_data['struct']['target_domain']['linkto'] = 'target';
  107. }
  108. }
  109. #
  110. # aliases
  111. #
  112. $table_alias = table_by_key('alias');
  113. $table_mailbox = table_by_key('mailbox');
  114. if (count($search) == 0 || !isset($search['_'])) {
  115. $list_param = "domain='$fDomain'";
  116. } else {
  117. $searchterm = escape_string($search['_']);
  118. $list_param = "(address LIKE '%$searchterm%' OR goto LIKE '%$searchterm%')";
  119. }
  120. $handler = new AliasHandler(0, $admin_username);
  121. $formconf = $handler->webformConfig(); # might change struct
  122. $alias_data = array(
  123. 'formconf' => $formconf,
  124. 'struct' => $handler->getStruct(),
  125. 'msg' => $handler->getMsg(),
  126. );
  127. $alias_data['struct']['goto_mailbox']['display_in_list'] = 0; # not useful/defined for non-mailbox aliases
  128. $alias_data['struct']['on_vacation']['display_in_list'] = 0;
  129. $alias_data['msg']['show_simple_search'] = False; # hide search box
  130. $handler->getList($list_param, array(), $page_size, $fDisplay);
  131. $pagebrowser_alias = $handler->getPagebrowser($list_param, array());
  132. $tAlias = $handler->result();
  133. #
  134. # mailboxes
  135. #
  136. $display_mailbox_aliases = Config::bool('alias_control_admin');
  137. # build the sql query
  138. $sql_select = "SELECT $table_mailbox.* ";
  139. $sql_from = " FROM $table_mailbox ";
  140. $sql_join = "";
  141. $sql_where = " WHERE ";
  142. $sql_order = " ORDER BY $table_mailbox.username ";
  143. $sql_limit = " LIMIT $page_size OFFSET $fDisplay";
  144. if (count($search) == 0 || !isset($search['_'])) {
  145. $sql_where .= " $table_mailbox.domain='$fDomain' ";
  146. } else {
  147. $searchterm = escape_string($search['_']);
  148. $sql_where .= db_in_clause("$table_mailbox.domain", $list_domains) . " ";
  149. $sql_where .= " AND ( $table_mailbox.username LIKE '%$searchterm%' OR $table_mailbox.name LIKE '%$searchterm%' ";
  150. if ($display_mailbox_aliases) {
  151. $sql_where .= " OR $table_alias.goto LIKE '%$searchterm%' ";
  152. }
  153. $sql_where .= " ) "; # $search is already escaped
  154. }
  155. if ($display_mailbox_aliases) {
  156. $sql_select .= ", $table_alias.goto ";
  157. $sql_join .= " LEFT JOIN $table_alias ON $table_mailbox.username=$table_alias.address ";
  158. }
  159. if (Config::bool('vacation_control_admin')) {
  160. $table_vacation = table_by_key('vacation');
  161. $sql_select .= ", $table_vacation.active AS v_active ";
  162. $sql_join .= " LEFT JOIN $table_vacation ON $table_mailbox.username=$table_vacation.email ";
  163. }
  164. if (Config::bool('used_quotas') && Config::bool('new_quota_table')) {
  165. $table_quota2 = table_by_key('quota2');
  166. $sql_select .= ", $table_quota2.bytes as current ";
  167. $sql_join .= " LEFT JOIN $table_quota2 ON $table_mailbox.username=$table_quota2.username ";
  168. }
  169. if (Config::bool('used_quotas') && ( ! Config::bool('new_quota_table') ) ) {
  170. $table_quota = table_by_key('quota');
  171. $sql_select .= ", $table_quota.current ";
  172. $sql_join .= " LEFT JOIN $table_quota ON $table_mailbox.username=$table_quota.username ";
  173. $sql_where .= " AND ( $table_quota.path='quota/storage' OR $table_quota.path IS NULL ) ";
  174. }
  175. $mailbox_pagebrowser_query = "$sql_from\n$sql_join\n$sql_where\n$sql_order" ;
  176. $query = "$sql_select\n$mailbox_pagebrowser_query\n$sql_limit";
  177. $result = db_query ($query);
  178. if ($result['rows'] > 0) {
  179. $delimiter = preg_quote($CONF['recipient_delimiter'], "/");
  180. $goto_single_rec_del = "";
  181. $tMailbox = array();
  182. while ($row = db_array ($result['result'])) {
  183. if ($display_mailbox_aliases) {
  184. $goto_split = explode(",", $row['goto']);
  185. $row['goto_mailbox'] = 0;
  186. $row['goto_other'] = array();
  187. foreach ($goto_split as $goto_single) {
  188. if (!empty($CONF['recipient_delimiter'])) {
  189. $goto_single_rec_del = preg_replace('/' .$delimiter. '[^' .$delimiter. '@]*@/', "@", $goto_single);
  190. }
  191. if ($goto_single == $row['username'] || $goto_single_rec_del == $row['username']) { # delivers to mailbox
  192. $row['goto_mailbox'] = 1;
  193. } elseif (Config::bool('vacation') && strstr($goto_single, '@' . $CONF['vacation_domain']) ) { # vacation alias - TODO: check for full vacation alias
  194. # skip the vacation alias, vacation status is detected otherwise
  195. } else { # forwarding to other alias
  196. $row['goto_other'][] = $goto_single;
  197. }
  198. }
  199. }
  200. if (db_pgsql()) {
  201. // XXX
  202. $row['modified'] = date('Y-m-d H:i', strtotime($row['modified']));
  203. $row['created'] = date('Y-m-d H:i', strtotime($row['created']));
  204. $row['active']=('t'==$row['active']) ? 1 : 0;
  205. if($row['v_active'] == NULL) {
  206. $row['v_active'] = 'f';
  207. }
  208. $row['v_active']=('t'==$row['v_active']) ? 1 : 0;
  209. }
  210. $tMailbox[] = $row;
  211. }
  212. }
  213. $alias_data['msg']['can_create'] = false;
  214. $tCanAddMailbox = false;
  215. $tDisplay_back = "";
  216. $tDisplay_back_show = "";
  217. $tDisplay_up_show = "";
  218. $tDisplay_next = "";
  219. $tDisplay_next_show = "";
  220. $limit = get_domain_properties($fDomain);
  221. if (isset ($limit)) {
  222. if ($fDisplay >= $page_size) {
  223. $tDisplay_back_show = 1;
  224. $tDisplay_back = $fDisplay - $page_size;
  225. }
  226. if (($limit['alias_count'] > $page_size) or ($limit['mailbox_count'] > $page_size)) {
  227. $tDisplay_up_show = 1;
  228. }
  229. if (
  230. (($fDisplay + $page_size) < $limit['alias_count']) or
  231. (($fDisplay + $page_size) < $limit['mailbox_count'])
  232. ) {
  233. $tDisplay_next_show = 1;
  234. $tDisplay_next = $fDisplay + $page_size;
  235. }
  236. if($limit['aliases'] == 0) {
  237. $alias_data['msg']['can_create'] = true;
  238. }
  239. elseif($limit['alias_count'] < $limit['aliases']) {
  240. $alias_data['msg']['can_create'] = true;
  241. }
  242. if($limit['mailboxes'] == 0) {
  243. $tCanAddMailbox = true;
  244. }
  245. elseif($limit['mailbox_count'] < $limit['mailboxes']) {
  246. $tCanAddMailbox = true;
  247. }
  248. $limit ['aliases'] = eval_size ($limit ['aliases']);
  249. $limit ['mailboxes'] = eval_size ($limit ['mailboxes']);
  250. if (Config::bool('quota')) {
  251. $limit ['maxquota'] = eval_size ($limit ['maxquota']);
  252. }
  253. }
  254. $gen_show_status_mailbox = array ();
  255. $divide_quota = array ('current' => array(), 'quota' => array());
  256. if ((is_array ($tMailbox) and sizeof ($tMailbox) > 0)) {
  257. for ($i = 0; $i < sizeof ($tMailbox); $i++) {
  258. $gen_show_status_mailbox [$i] = gen_show_status($tMailbox[$i]['username']);
  259. if(isset($tMailbox[$i]['current'])) {
  260. $divide_quota ['current'][$i] = divide_quota ($tMailbox[$i]['current']);
  261. }
  262. if(isset($tMailbox[$i]['quota'])) {
  263. $divide_quota ['quota'][$i] = divide_quota ($tMailbox[$i]['quota']);
  264. }
  265. if(isset($tMailbox[$i]['quota']) && isset($tMailbox[$i]['current']))
  266. {
  267. $divide_quota ['percent'][$i] = min(100, round(($divide_quota ['current'][$i]/max(1,$divide_quota ['quota'][$i]))*100));
  268. $divide_quota ['quota_width'][$i] = ($divide_quota ['percent'][$i] / 100 * 120);
  269. } else {
  270. $divide_quota ['current'][$i] = Config::Lang('unknown');
  271. $divide_quota ['quota_width'][$i] = 0; # TODO: use special value?
  272. }
  273. }
  274. }
  275. class cNav_bar
  276. {
  277. var $count, $title, $limit, $page_size, $pages, $search; //* arguments
  278. var $url; //* manually
  279. var $fInit, $arr_prev, $arr_next, $arr_top; //* internal
  280. var $anchor;
  281. function cNav_bar ($aTitle, $aLimit, $aPage_size, $aPages, $aSearch) {
  282. $this->count = count($aPages);
  283. $this->title = $aTitle;
  284. $this->limit = $aLimit;
  285. $this->page_size = $aPage_size;
  286. $this->pages = $aPages;
  287. if (is_array($aSearch) && isset($aSearch['_']) && $aSearch['_'] != "") {
  288. $this->search = "&search[_]=" . htmlentities($aSearch['_']);
  289. } else {
  290. $this->search = "";
  291. }
  292. $this->url = '';
  293. $this->fInit = false;
  294. }
  295. function init () {
  296. $this->anchor = 'a'.substr ($this->title, 3);
  297. $this->url .= '#'.$this->anchor;
  298. ($this->limit >= $this->page_size) ? $this->arr_prev = '&nbsp;<a href="?limit='.($this->limit - $this->page_size).$this->search.$this->url.'"><img border="0" src="images/arrow-l.png" title="'.$GLOBALS ['PALANG']['pOverview_left_arrow'].'" alt="'.$GLOBALS ['PALANG']['pOverview_left_arrow'].'"/></a>&nbsp;' : $this->arr_prev = '';
  299. ($this->limit > 0) ? $this->arr_top = '&nbsp;<a href="?limit=0' .$this->search.$this->url.'"><img border="0" src="images/arrow-u.png" title="'.$GLOBALS ['PALANG']['pOverview_up_arrow'].'" alt="'.$GLOBALS ['PALANG']['pOverview_up_arrow'].'"/></a>&nbsp;' : $this->arr_top = '';
  300. (($this->limit + $this->page_size) < ($this->count * $this->page_size)) ? $this->arr_next = '&nbsp;<a href="?limit='.($this->limit + $this->page_size).$this->search.$this->url.'"><img border="0" src="images/arrow-r.png" title="'.$GLOBALS ['PALANG']['pOverview_right_arrow'].'" alt="'.$GLOBALS ['PALANG']['pOverview_right_arrow'].'"/></a>&nbsp;' : $this->arr_next = '';
  301. $this->fInit = true;
  302. }
  303. function display_pre () {
  304. $ret_val = '<div class="nav_bar"';
  305. //$ret_val .= ' style="background-color:#ffa;"';
  306. $ret_val .= '>';
  307. $ret_val .= '<table width="730"><colgroup span="1"><col width="550"></col></colgroup> ';
  308. $ret_val .= '<tr><td align="left">';
  309. return $ret_val;
  310. }
  311. function display_post () {
  312. $ret_val = '</td></tr></table></div>';
  313. return $ret_val;
  314. }
  315. function display_top () {
  316. $ret_val = '';
  317. if ($this->count < 1)
  318. return $ret_val;
  319. if (!$this->fInit)
  320. $this->init ();
  321. $ret_val .= '<a name="'.$this->anchor.'"></a>';
  322. $ret_val .= $this->display_pre ();
  323. $ret_val .= '<b>'.$this->title.'</b>&nbsp;&nbsp;';
  324. ($this->limit >= $this->page_size) ? $highlight_at = $this->limit / $this->page_size : $highlight_at = 0;
  325. for ($i = 0; $i < count ($this->pages); $i++)
  326. {
  327. $lPage = $this->pages [$i];
  328. if ($i == $highlight_at) {
  329. $ret_val .= '<b>'.$lPage.'</b>'."\n";
  330. } else {
  331. $ret_val .= '<a href="?limit='.($i * $this->page_size).$this->search.$this->url.'">'.$lPage.'</a>'."\n";
  332. }
  333. }
  334. $ret_val .= '</td><td valign="middle" align="right">';
  335. $ret_val .= $this->arr_prev;
  336. $ret_val .= $this->arr_top;
  337. $ret_val .= $this->arr_next;
  338. $ret_val .= $this->display_post ();
  339. return $ret_val;
  340. }
  341. function display_bottom () {
  342. $ret_val = '';
  343. if ($this->count < 1)
  344. return $ret_val;
  345. if (!$this->fInit)
  346. $this->init ();
  347. $ret_val .= $this->display_pre ();
  348. $ret_val .= '</td><td valign="middle" align="right">';
  349. $ret_val .= $this->arr_prev;
  350. $ret_val .= $this->arr_top;
  351. $ret_val .= $this->arr_next;
  352. $ret_val .= $this->display_post ();
  353. return $ret_val;
  354. }
  355. }
  356. $nav_bar_alias = new cNav_bar ($PALANG['pOverview_alias_title'], $fDisplay, $CONF['page_size'], $pagebrowser_alias, $search);
  357. $nav_bar_alias->url = '&amp;domain='.$fDomain;
  358. $pagebrowser_mailbox = create_page_browser("$table_mailbox.username", $mailbox_pagebrowser_query);
  359. $nav_bar_mailbox = new cNav_bar ($PALANG['pOverview_mailbox_title'], $fDisplay, $CONF['page_size'], $pagebrowser_mailbox, $search);
  360. $nav_bar_mailbox->url = '&amp;domain='.$fDomain;
  361. //print $nav_bar_alias->display_top ();
  362. // this is why we need a proper template layer.
  363. $fDomain = htmlentities($fDomain, ENT_QUOTES);
  364. if(empty($_GET['domain'])) {
  365. $_GET['domain'] = '';
  366. }
  367. $smarty->assign ('admin_list', array());
  368. $smarty->assign ('select_options', select_options ($list_domains, array ($fDomain)), false);
  369. $smarty->assign ('nav_bar_alias', array ('top' => $nav_bar_alias->display_top (), 'bottom' => $nav_bar_alias->display_bottom ()), false);
  370. $smarty->assign ('nav_bar_mailbox', array ('top' => $nav_bar_mailbox->display_top (), 'bottom' => $nav_bar_mailbox->display_bottom ()), false);
  371. $smarty->assign ('fDomain', $fDomain, false);
  372. $smarty->assign ('search', $search);
  373. $smarty->assign ('list_domains', $list_domains);
  374. $smarty->assign ('limit', $limit);
  375. $smarty->assign ('tDisplay_back_show', $tDisplay_back_show);
  376. $smarty->assign ('tDisplay_back', $tDisplay_back);
  377. $smarty->assign ('tDisplay_up_show', $tDisplay_up_show);
  378. $smarty->assign ('tDisplay_next_show', $tDisplay_next_show);
  379. $smarty->assign ('tDisplay_next', $tDisplay_next);
  380. $smarty->assign ('tAliasDomains', $tAliasDomains);
  381. $smarty->assign ('aliasdomain_data', $aliasdomain_data);
  382. $smarty->assign ('tAlias', $tAlias);
  383. $smarty->assign ('alias_data', $alias_data);
  384. $smarty->assign ('tMailbox', $tMailbox);
  385. $smarty->assign ('gen_show_status_mailbox', $gen_show_status_mailbox, false);
  386. $smarty->assign ('boolconf_used_quotas', Config::bool('used_quotas'));
  387. $smarty->assign ('divide_quota', $divide_quota);
  388. $smarty->assign ('tCanAddMailbox', $tCanAddMailbox);
  389. $smarty->assign ('display_mailbox_aliases', $display_mailbox_aliases);
  390. if (isset ($_GET ['tab']))
  391. $_SESSION ['tab'] = $_GET ['tab'];
  392. //if (empty ($_GET ['tab']))
  393. // unset ($_SESSION ['tab']);
  394. if (!isset ($_SESSION ['tab']))
  395. $_SESSION ['tab'] = 'all';
  396. $smarty->assign ('tab', $_SESSION ['tab']);
  397. $smarty->assign ('smarty_template', 'list-virtual');
  398. $smarty->display ('index.tpl');
  399. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */
  400. ?>