identity.php 6.0 KB

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