identity.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /**
  3. * identity.php
  4. *
  5. * This contains utility functions for dealing with multiple identities
  6. *
  7. * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. * @since 1.4.2
  12. */
  13. /**
  14. * Returns an array of all the identities.
  15. * Array is keyed: full_name, reply_to, email_address, index, signature
  16. * @return array full_name,reply_to,email_address,index,signature
  17. * @since 1.4.2
  18. */
  19. function get_identities() {
  20. global $username, $data_dir, $domain;
  21. $em = getPref($data_dir,$username,'email_address');
  22. if ( ! $em ) {
  23. if (strpos($username , '@') == false) {
  24. $em = $username.'@'.$domain;
  25. } else {
  26. $em = $username;
  27. }
  28. }
  29. $identities = array();
  30. /* We always have this one, even if the user doesn't use multiple identities */
  31. $identities[] = array('full_name' => getPref($data_dir,$username,'full_name'),
  32. 'email_address' => $em,
  33. 'reply_to' => getPref($data_dir,$username,'reply_to'),
  34. 'signature' => getSig($data_dir,$username,'g'),
  35. 'index' => 0 );
  36. $num_ids = getPref($data_dir,$username,'identities');
  37. /* If there are any others, add them to the array */
  38. if (!empty($num_ids) && $num_ids > 1) {
  39. for ($i=1;$i<$num_ids;$i++) {
  40. $thisem = getPref($data_dir,$username,'email_address' . $i);
  41. $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i),
  42. 'email_address' => empty($thisem)?$em:$thisem,
  43. 'reply_to' => getPref($data_dir,$username,'reply_to' . $i),
  44. 'signature' => getSig($data_dir,$username,$i),
  45. 'index' => $i );
  46. }
  47. }
  48. return $identities;
  49. }
  50. /**
  51. * Function to save the identities array
  52. *
  53. * @param array $identities Array of identities
  54. * @since 1.5.1 and 1.4.5
  55. */
  56. function save_identities($identities) {
  57. global $username, $data_dir, $domain;
  58. if (empty($identities) || !is_array($identities)) {
  59. return;
  60. }
  61. $num_cur = getPref($data_dir, $username, 'identities');
  62. $cnt = count($identities);
  63. // Remove any additional identities in prefs //
  64. for($i=$cnt; $i <= $num_cur; $i++) {
  65. removePref($data_dir, $username, 'full_name' . $i);
  66. removePref($data_dir, $username, 'email_address' . $i);
  67. removePref($data_dir, $username, 'reply_to' . $i);
  68. setSig($data_dir, $username, $i, '');
  69. }
  70. foreach($identities as $id=>$ident) {
  71. $key = ($id?$id:'');
  72. setPref($data_dir, $username, 'full_name' . $key, $ident['full_name']);
  73. setPref($data_dir, $username, 'email_address' . $key, $ident['email_address']);
  74. setPref($data_dir, $username, 'reply_to' . $key, $ident['reply_to']);
  75. if ($id === 0) {
  76. setSig($data_dir, $username, 'g', $ident['signature']);
  77. } else {
  78. setSig($data_dir, $username, $key, $ident['signature']);
  79. }
  80. }
  81. setPref($data_dir, $username, 'identities', $cnt);
  82. }
  83. /**
  84. * Returns an array with a fixed set of identities
  85. *
  86. * @param array $identities Array of identities
  87. * @param int $id Identity to modify
  88. * @param string $action Action to perform
  89. * @return array
  90. * @since 1.5.1 and 1.4.5
  91. */
  92. function sqfixidentities( $identities, $id, $action ) {
  93. $fixed = array();
  94. $tmp_hold = array();
  95. $i = 0;
  96. if (empty($identities) || !is_array($identities)) {
  97. return $fixed;
  98. }
  99. foreach( $identities as $key=>$ident ) {
  100. if (empty_identity($ident)) {
  101. continue;
  102. }
  103. switch($action) {
  104. case 'makedefault':
  105. if ($key == $id) {
  106. $fixed[0] = $ident;
  107. // inform plugins about renumbering of ids
  108. do_hook('options_identities_renumber', $temp=array(&$id, 'default'));
  109. continue 2;
  110. } else {
  111. $fixed[$i+1] = $ident;
  112. }
  113. break;
  114. case 'move':
  115. if ($key == ($id - 1)) {
  116. $tmp_hold = $ident;
  117. // inform plugins about renumbering of ids
  118. do_hook('options_identities_renumber', $temp=array(&$id , $id - 1));
  119. continue 2;
  120. } else {
  121. $fixed[$i] = $ident;
  122. if ($key == $id) {
  123. $i++;
  124. $fixed[$i] = $tmp_hold;
  125. }
  126. }
  127. break;
  128. case 'delete':
  129. if ($key == $id) {
  130. // inform plugins about deleted id
  131. do_hook('options_identities_process', $temp=array(&$action, &$id));
  132. continue 2;
  133. } else {
  134. $fixed[$i] = $ident;
  135. }
  136. break;
  137. // Process actions from plugins and save/update action //
  138. default:
  139. /**
  140. * send action and id information. number of hook arguments
  141. * differs from 1.4.4 or older and 1.5.0. count($args) can
  142. * be used to detect modified hook. Older hook does not
  143. * provide information that can be useful for plugins.
  144. */
  145. do_hook('options_identities_process', $temp=array(&$action, &$id));
  146. $fixed[$i] = $ident;
  147. }
  148. // Inc array index //
  149. $i++;
  150. }
  151. ksort($fixed);
  152. return $fixed;
  153. }
  154. /**
  155. * Function to test if identity is empty
  156. *
  157. * @param array $identity Identitiy Array
  158. * @return boolean
  159. * @since 1.5.1 and 1.4.5
  160. */
  161. function empty_identity($ident) {
  162. if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. }
  168. /**
  169. * Construct our "From:" header based on
  170. * a supplied identity number.
  171. * Will fall back when no sensible email address has been defined.
  172. *
  173. * @param int $identity identity# to use
  174. * @since 1.5.2
  175. */
  176. function build_from_header($identity = 0) {
  177. $idents = get_identities();
  178. if (! isset($idents[$identity]) ) $identity = 0;
  179. if ( !empty($idents[$identity]['full_name']) ) {
  180. $from_name = $idents[$identity]['full_name'];
  181. }
  182. $from_mail = $idents[$identity]['email_address'];
  183. if ( isset($from_name) ) {
  184. $from_name_encoded = encodeHeader($from_name);
  185. if ($from_name_encoded != $from_name) {
  186. return $from_name_encoded .' <'.$from_mail.'>';
  187. }
  188. return '"'.$from_name .'" <'.$from_mail.'>';
  189. }
  190. return $from_mail;
  191. }
  192. /**
  193. * Find a matching identity based on a set of emailaddresses.
  194. * Will return the first identity to have a matching address.
  195. * When nothing found, returns the default identity.
  196. *
  197. * @param needles array list of mailadresses
  198. * @returns int identity
  199. * @since 1.5.2
  200. */
  201. function find_identity($needles) {
  202. $idents = get_identities();
  203. if ( count($idents) == 1 || empty($needles) ) return 0;
  204. foreach ( $idents as $nr => $ident ) {
  205. if ( isset($ident['email_address']) ) {
  206. foreach ( $needles as $needle ) {
  207. if ( strcasecmp($needle, $ident['email_address']) == 0 ) {
  208. return $nr;
  209. }
  210. }
  211. }
  212. }
  213. return 0;
  214. }