identity.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * identity.php
  4. *
  5. * Copyright (c) 1999-2005 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This contains utility functions for dealing with multiple identities
  9. *
  10. * @version $Id$
  11. * @package squirrelmail
  12. */
  13. /** Used to simplify includes */
  14. if (!defined('SM_PATH')) {
  15. define('SM_PATH','../');
  16. }
  17. include_once(SM_PATH . 'include/load_prefs.php');
  18. /**
  19. * Returns an array of all the identities.
  20. * Array is keyed: full_name, reply_to, email_address, index, signature
  21. * @return array full_name,reply_to,email_address,index,signature
  22. */
  23. function get_identities() {
  24. global $username, $data_dir, $domain;
  25. $em = getPref($data_dir,$username,'email_address');
  26. if ( ! $em ) {
  27. if (strpos($username , '@') == false) {
  28. $em = $username.'@'.$domain;
  29. } else {
  30. $em = $username;
  31. }
  32. }
  33. $identities = array();
  34. /* We always have this one, even if the user doesn't use multiple identities */
  35. $identities[] = array('full_name' => getPref($data_dir,$username,'full_name'),
  36. 'email_address' => $em,
  37. 'reply_to' => getPref($data_dir,$username,'reply_to'),
  38. 'signature' => getSig($data_dir,$username,'g'),
  39. 'index' => 0 );
  40. $num_ids = getPref($data_dir,$username,'identities');
  41. /* If there are any others, add them to the array */
  42. if (!empty($num_ids) && $num_ids > 1) {
  43. for ($i=1;$i<$num_ids;$i++) {
  44. $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i),
  45. 'email_address' => getPref($data_dir,$username,'email_address' . $i),
  46. 'reply_to' => getPref($data_dir,$username,'reply_to' . $i),
  47. 'signature' => getSig($data_dir,$username,$i),
  48. 'index' => $i );
  49. }
  50. }
  51. return $identities;
  52. }
  53. /**
  54. * Function to save the identities array
  55. *
  56. * @param array $identities Array of identities
  57. */
  58. function save_identities($identities) {
  59. global $username, $data_dir, $domain;
  60. if (empty($identities) || !is_array($identities)) {
  61. return;
  62. }
  63. $num_cur = getPref($data_dir, $username, 'identities');
  64. $cnt = count($identities);
  65. // Remove any additional identities in prefs //
  66. for($i=$cnt; $i <= $num_cur; $i++) {
  67. removePref($data_dir, $username, 'full_name' . $i);
  68. removePref($data_dir, $username, 'email_address' . $i);
  69. removePref($data_dir, $username, 'reply_to' . $i);
  70. setSig($data_dir, $username, $i, '');
  71. }
  72. foreach($identities as $id=>$ident) {
  73. $key = ($id?$id:'');
  74. setPref($data_dir, $username, 'full_name' . $key, $ident['full_name']);
  75. setPref($data_dir, $username, 'email_address' . $key, $ident['email_address']);
  76. setPref($data_dir, $username, 'reply_to' . $key, $ident['reply_to']);
  77. if ($id === 0) {
  78. setSig($data_dir, $username, 'g', $ident['signature']);
  79. } else {
  80. setSig($data_dir, $username, $key, $ident['signature']);
  81. }
  82. }
  83. setPref($data_dir, $username, 'identities', $cnt);
  84. }
  85. /**
  86. * Returns an array with a fixed set of identities
  87. *
  88. * @param array $identities Array of identities
  89. * @param int $id Identity to modify
  90. * @param string $action Action to perform
  91. * @return array
  92. */
  93. function sqfixidentities( $identities, $id, $action ) {
  94. $fixed = array();
  95. $tmp_hold = array();
  96. $i = 0;
  97. if (empty($identities) || !is_array($identities)) {
  98. return $fixed;
  99. }
  100. foreach( $identities as $key=>$ident ) {
  101. if (empty_identity($ident)) {
  102. continue;
  103. }
  104. switch($action) {
  105. case 'makedefault':
  106. if ($key == $id) {
  107. $fixed[0] = $ident;
  108. continue 2;
  109. } else {
  110. $fixed[$i+1] = $ident;
  111. }
  112. break;
  113. case 'move':
  114. if ($key == ($id - 1)) {
  115. $tmp_hold = $ident;
  116. continue 2;
  117. } else {
  118. $fixed[$i] = $ident;
  119. if ($key == $id) {
  120. $i++;
  121. $fixed[$i] = $tmp_hold;
  122. }
  123. }
  124. break;
  125. case 'delete':
  126. if ($key == $id) {
  127. continue 2;
  128. } else {
  129. $fixed[$i] = $ident;
  130. }
  131. break;
  132. // we should never hit this but just in case //
  133. default:
  134. $fixed[$i] = $ident;
  135. }
  136. // Inc array index //
  137. $i++;
  138. }
  139. ksort($fixed);
  140. return $fixed;
  141. }
  142. /**
  143. * Function to test if identity is empty
  144. *
  145. * @param array $identity Identitiy Array
  146. * @return boolean
  147. */
  148. function empty_identity($ident) {
  149. if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
  150. return true;
  151. } else {
  152. return false;
  153. }
  154. }
  155. ?>