identity.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. * @since 1.4.2
  13. */
  14. /** Used to simplify includes
  15. * @ignore
  16. */
  17. if (!defined('SM_PATH')) {
  18. define('SM_PATH','../');
  19. }
  20. /** preference and signature functions */
  21. include_once(SM_PATH . 'include/load_prefs.php');
  22. /**
  23. * Returns an array of all the identities.
  24. * Array is keyed: full_name, reply_to, email_address, index, signature
  25. * @return array full_name,reply_to,email_address,index,signature
  26. * @since 1.4.2
  27. */
  28. function get_identities() {
  29. global $username, $data_dir, $domain;
  30. $em = getPref($data_dir,$username,'email_address');
  31. if ( ! $em ) {
  32. if (strpos($username , '@') == false) {
  33. $em = $username.'@'.$domain;
  34. } else {
  35. $em = $username;
  36. }
  37. }
  38. $identities = array();
  39. /* We always have this one, even if the user doesn't use multiple identities */
  40. $identities[] = array('full_name' => getPref($data_dir,$username,'full_name'),
  41. 'email_address' => $em,
  42. 'reply_to' => getPref($data_dir,$username,'reply_to'),
  43. 'signature' => getSig($data_dir,$username,'g'),
  44. 'index' => 0 );
  45. $num_ids = getPref($data_dir,$username,'identities');
  46. /* If there are any others, add them to the array */
  47. if (!empty($num_ids) && $num_ids > 1) {
  48. for ($i=1;$i<$num_ids;$i++) {
  49. $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i),
  50. 'email_address' => getPref($data_dir,$username,'email_address' . $i),
  51. 'reply_to' => getPref($data_dir,$username,'reply_to' . $i),
  52. 'signature' => getSig($data_dir,$username,$i),
  53. 'index' => $i );
  54. }
  55. }
  56. return $identities;
  57. }
  58. /**
  59. * Function to save the identities array
  60. *
  61. * @param array $identities Array of identities
  62. * @since 1.5.1 and 1.4.5
  63. */
  64. function save_identities($identities) {
  65. global $username, $data_dir, $domain;
  66. if (empty($identities) || !is_array($identities)) {
  67. return;
  68. }
  69. $num_cur = getPref($data_dir, $username, 'identities');
  70. $cnt = count($identities);
  71. // Remove any additional identities in prefs //
  72. for($i=$cnt; $i <= $num_cur; $i++) {
  73. removePref($data_dir, $username, 'full_name' . $i);
  74. removePref($data_dir, $username, 'email_address' . $i);
  75. removePref($data_dir, $username, 'reply_to' . $i);
  76. setSig($data_dir, $username, $i, '');
  77. }
  78. foreach($identities as $id=>$ident) {
  79. $key = ($id?$id:'');
  80. setPref($data_dir, $username, 'full_name' . $key, $ident['full_name']);
  81. setPref($data_dir, $username, 'email_address' . $key, $ident['email_address']);
  82. setPref($data_dir, $username, 'reply_to' . $key, $ident['reply_to']);
  83. if ($id === 0) {
  84. setSig($data_dir, $username, 'g', $ident['signature']);
  85. } else {
  86. setSig($data_dir, $username, $key, $ident['signature']);
  87. }
  88. }
  89. setPref($data_dir, $username, 'identities', $cnt);
  90. }
  91. /**
  92. * Returns an array with a fixed set of identities
  93. *
  94. * @param array $identities Array of identities
  95. * @param int $id Identity to modify
  96. * @param string $action Action to perform
  97. * @return array
  98. * @since 1.5.1 and 1.4.5
  99. */
  100. function sqfixidentities( $identities, $id, $action ) {
  101. $fixed = array();
  102. $tmp_hold = array();
  103. $i = 0;
  104. if (empty($identities) || !is_array($identities)) {
  105. return $fixed;
  106. }
  107. foreach( $identities as $key=>$ident ) {
  108. if (empty_identity($ident)) {
  109. continue;
  110. }
  111. switch($action) {
  112. case 'makedefault':
  113. if ($key == $id) {
  114. $fixed[0] = $ident;
  115. // inform plugins about renumbering of ids
  116. do_hook('options_identities_renumber', $id, 'default');
  117. continue 2;
  118. } else {
  119. $fixed[$i+1] = $ident;
  120. }
  121. break;
  122. case 'move':
  123. if ($key == ($id - 1)) {
  124. $tmp_hold = $ident;
  125. // inform plugins about renumbering of ids
  126. do_hook('options_identities_renumber', $id , $id - 1);
  127. continue 2;
  128. } else {
  129. $fixed[$i] = $ident;
  130. if ($key == $id) {
  131. $i++;
  132. $fixed[$i] = $tmp_hold;
  133. }
  134. }
  135. break;
  136. case 'delete':
  137. if ($key == $id) {
  138. continue 2;
  139. } else {
  140. $fixed[$i] = $ident;
  141. }
  142. break;
  143. // Process actions from plugins and save/update action //
  144. default:
  145. /**
  146. * send action and id information. number of hook arguments
  147. * differs from 1.4.4 or older and 1.5.0. count($args) can
  148. * be used to detect modified hook. Older hook does not
  149. * provide information that can be useful for plugins.
  150. */
  151. do_hook('options_identities_process', $action, $id);
  152. $fixed[$i] = $ident;
  153. }
  154. // Inc array index //
  155. $i++;
  156. }
  157. ksort($fixed);
  158. return $fixed;
  159. }
  160. /**
  161. * Function to test if identity is empty
  162. *
  163. * @param array $identity Identitiy Array
  164. * @return boolean
  165. * @since 1.5.1 and 1.4.5
  166. */
  167. function empty_identity($ident) {
  168. if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
  169. return true;
  170. } else {
  171. return false;
  172. }
  173. }
  174. ?>