identity.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * identity.php
  4. *
  5. * This contains utility functions for dealing with multiple identities
  6. *
  7. * @copyright &copy; 1999-2006 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. $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i),
  41. 'email_address' => getPref($data_dir,$username,'email_address' . $i),
  42. 'reply_to' => getPref($data_dir,$username,'reply_to' . $i),
  43. 'signature' => getSig($data_dir,$username,$i),
  44. 'index' => $i );
  45. }
  46. }
  47. return $identities;
  48. }
  49. /**
  50. * Function to save the identities array
  51. *
  52. * @param array $identities Array of identities
  53. * @since 1.5.1 and 1.4.5
  54. */
  55. function save_identities($identities) {
  56. global $username, $data_dir, $domain;
  57. if (empty($identities) || !is_array($identities)) {
  58. return;
  59. }
  60. $num_cur = getPref($data_dir, $username, 'identities');
  61. $cnt = count($identities);
  62. // Remove any additional identities in prefs //
  63. for($i=$cnt; $i <= $num_cur; $i++) {
  64. removePref($data_dir, $username, 'full_name' . $i);
  65. removePref($data_dir, $username, 'email_address' . $i);
  66. removePref($data_dir, $username, 'reply_to' . $i);
  67. setSig($data_dir, $username, $i, '');
  68. }
  69. foreach($identities as $id=>$ident) {
  70. $key = ($id?$id:'');
  71. setPref($data_dir, $username, 'full_name' . $key, $ident['full_name']);
  72. setPref($data_dir, $username, 'email_address' . $key, $ident['email_address']);
  73. setPref($data_dir, $username, 'reply_to' . $key, $ident['reply_to']);
  74. if ($id === 0) {
  75. setSig($data_dir, $username, 'g', $ident['signature']);
  76. } else {
  77. setSig($data_dir, $username, $key, $ident['signature']);
  78. }
  79. }
  80. setPref($data_dir, $username, 'identities', $cnt);
  81. }
  82. /**
  83. * Returns an array with a fixed set of identities
  84. *
  85. * @param array $identities Array of identities
  86. * @param int $id Identity to modify
  87. * @param string $action Action to perform
  88. * @return array
  89. * @since 1.5.1 and 1.4.5
  90. */
  91. function sqfixidentities( $identities, $id, $action ) {
  92. $fixed = array();
  93. $tmp_hold = array();
  94. $i = 0;
  95. if (empty($identities) || !is_array($identities)) {
  96. return $fixed;
  97. }
  98. foreach( $identities as $key=>$ident ) {
  99. if (empty_identity($ident)) {
  100. continue;
  101. }
  102. switch($action) {
  103. case 'makedefault':
  104. if ($key == $id) {
  105. $fixed[0] = $ident;
  106. // inform plugins about renumbering of ids
  107. do_hook('options_identities_renumber', $id, 'default');
  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. // inform plugins about renumbering of ids
  117. do_hook('options_identities_renumber', $id , $id - 1);
  118. continue 2;
  119. } else {
  120. $fixed[$i] = $ident;
  121. if ($key == $id) {
  122. $i++;
  123. $fixed[$i] = $tmp_hold;
  124. }
  125. }
  126. break;
  127. case 'delete':
  128. if ($key == $id) {
  129. // inform plugins about deleted id
  130. do_hook('options_identities_process', $action, $id);
  131. continue 2;
  132. } else {
  133. $fixed[$i] = $ident;
  134. }
  135. break;
  136. // Process actions from plugins and save/update action //
  137. default:
  138. /**
  139. * send action and id information. number of hook arguments
  140. * differs from 1.4.4 or older and 1.5.0. count($args) can
  141. * be used to detect modified hook. Older hook does not
  142. * provide information that can be useful for plugins.
  143. */
  144. do_hook('options_identities_process', $action, $id);
  145. $fixed[$i] = $ident;
  146. }
  147. // Inc array index //
  148. $i++;
  149. }
  150. ksort($fixed);
  151. return $fixed;
  152. }
  153. /**
  154. * Function to test if identity is empty
  155. *
  156. * @param array $identity Identitiy Array
  157. * @return boolean
  158. * @since 1.5.1 and 1.4.5
  159. */
  160. function empty_identity($ident) {
  161. if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
  162. return true;
  163. } else {
  164. return false;
  165. }
  166. }
  167. ?>