/tags/rel-1_4_10/functions/identity.php

# · PHP · 216 lines · 118 code · 48 blank · 50 comment · 32 complexity · b03de959014e5ffac356dd7949fb3b89 MD5 · raw file

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