identity.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * identity.php
  4. *
  5. * This contains utility functions for dealing with multiple identities
  6. *
  7. * @copyright 1999-2025 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. $temp = array(&$id, 'default');
  109. do_hook('options_identities_renumber', $temp);
  110. continue 2;
  111. } else {
  112. $fixed[$i+1] = $ident;
  113. }
  114. break;
  115. case 'move':
  116. if ($key == ($id - 1)) {
  117. $tmp_hold = $ident;
  118. // inform plugins about renumbering of ids
  119. $temp = array(&$id , $id - 1);
  120. do_hook('options_identities_renumber', $temp);
  121. continue 2;
  122. } else {
  123. $fixed[$i] = $ident;
  124. if ($key == $id) {
  125. $i++;
  126. $fixed[$i] = $tmp_hold;
  127. }
  128. }
  129. break;
  130. case 'delete':
  131. if ($key == $id) {
  132. // inform plugins about deleted id
  133. $temp = array(&$action, &$id);
  134. do_hook('options_identities_process', $temp);
  135. continue 2;
  136. } else {
  137. $fixed[$i] = $ident;
  138. }
  139. break;
  140. // Process actions from plugins and save/update action //
  141. default:
  142. /**
  143. * send action and id information. number of hook arguments
  144. * differs from 1.4.4 or older and 1.5.0. count($args) can
  145. * be used to detect modified hook. Older hook does not
  146. * provide information that can be useful for plugins.
  147. */
  148. $temp = array(&$action, &$id);
  149. do_hook('options_identities_process', $temp);
  150. $fixed[$i] = $ident;
  151. }
  152. // Inc array index //
  153. $i++;
  154. }
  155. ksort($fixed);
  156. return $fixed;
  157. }
  158. /**
  159. * Function to test if identity is empty
  160. *
  161. * @param array $identity Identitiy Array
  162. * @return boolean
  163. * @since 1.5.1 and 1.4.5
  164. */
  165. function empty_identity($ident) {
  166. if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
  167. return true;
  168. } else {
  169. return false;
  170. }
  171. }
  172. /**
  173. * Construct our "From:" header based on
  174. * a supplied identity number.
  175. * Will fall back when no sensible email address has been defined.
  176. *
  177. * @param int $identity identity# to use
  178. * @since 1.5.2
  179. */
  180. function build_from_header($identity = 0) {
  181. global $domain;
  182. $idents = get_identities();
  183. if (! isset($idents[$identity]) ) $identity = 0;
  184. if ( !empty($idents[$identity]['full_name']) ) {
  185. $from_name = $idents[$identity]['full_name'];
  186. }
  187. $from_mail = $idents[$identity]['email_address'];
  188. if (strpos($from_mail, '@') === FALSE)
  189. $from_mail .= '@' . $domain;
  190. if ( isset($from_name) ) {
  191. $from_name_encoded = encodeHeader('"' . $from_name . '"');
  192. if ($from_name_encoded != $from_name) {
  193. return $from_name_encoded . ' <' . $from_mail . '>';
  194. }
  195. return '"' . $from_name . '" <' . $from_mail . '>';
  196. }
  197. return $from_mail;
  198. }
  199. /**
  200. * Find a matching identity based on a set of emailaddresses.
  201. * Will return the first identity to have a matching address.
  202. * When nothing found, returns the default identity.
  203. *
  204. * @param needles array list of mailadresses
  205. * @returns int identity
  206. * @since 1.5.2
  207. */
  208. function find_identity($needles) {
  209. $idents = get_identities();
  210. if ( count($idents) == 1 || empty($needles) ) return 0;
  211. foreach ( $idents as $nr => $ident ) {
  212. if ( isset($ident['email_address']) ) {
  213. foreach ( $needles as $needle ) {
  214. if ( strcasecmp($needle, $ident['email_address']) == 0 ) {
  215. return $nr;
  216. }
  217. }
  218. }
  219. }
  220. return 0;
  221. }