This is CAN-2005-2095
[squirrelmail.git] / functions / identity.php
1 <?php
2
3 /**
4 * identity.php
5 *
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains utility functions for dealing with multiple identities
10 *
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15 /** Used to simplify includes */
16 if (!defined('SM_PATH')) {
17 define('SM_PATH','../');
18 }
19
20 include_once(SM_PATH . 'include/load_prefs.php');
21
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 */
27 function get_identities() {
28
29 global $username, $data_dir, $domain;
30
31 $em = getPref($data_dir,$username,'email_address');
32 if ( ! $em ) {
33 if (strpos($username , '@') == false) {
34 $em = $username.'@'.$domain;
35 } else {
36 $em = $username;
37 }
38 }
39 $identities = array();
40 /* We always have this one, even if the user doesn't use multiple identities */
41 $identities[] = array('full_name' => getPref($data_dir,$username,'full_name'),
42 'email_address' => $em,
43 'reply_to' => getPref($data_dir,$username,'reply_to'),
44 'signature' => getSig($data_dir,$username,'g'),
45 'index' => 0 );
46
47 $num_ids = getPref($data_dir,$username,'identities');
48 /* If there are any others, add them to the array */
49 if (!empty($num_ids) && $num_ids > 1) {
50 for ($i=1;$i<$num_ids;$i++) {
51 $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i),
52 'email_address' => getPref($data_dir,$username,'email_address' . $i),
53 'reply_to' => getPref($data_dir,$username,'reply_to' . $i),
54 'signature' => getSig($data_dir,$username,$i),
55 'index' => $i );
56 }
57 }
58
59 return $identities;
60 }
61
62 /**
63 * Function to save the identities array
64 *
65 * @param array $identities Array of identities
66 */
67 function save_identities($identities) {
68
69 global $username, $data_dir, $domain;
70
71 if (empty($identities) || !is_array($identities)) {
72 return;
73 }
74
75
76 $num_cur = getPref($data_dir, $username, 'identities');
77
78 $cnt = count($identities);
79
80 // Remove any additional identities in prefs //
81 for($i=$cnt; $i <= $num_cur; $i++) {
82 removePref($data_dir, $username, 'full_name' . $i);
83 removePref($data_dir, $username, 'email_address' . $i);
84 removePref($data_dir, $username, 'reply_to' . $i);
85 setSig($data_dir, $username, $i, '');
86 }
87
88 foreach($identities as $id=>$ident) {
89
90 $key = ($id?$id:'');
91
92 setPref($data_dir, $username, 'full_name' . $key, $ident['full_name']);
93 setPref($data_dir, $username, 'email_address' . $key, $ident['email_address']);
94 setPref($data_dir, $username, 'reply_to' . $key, $ident['reply_to']);
95
96 if ($id === 0) {
97 setSig($data_dir, $username, 'g', $ident['signature']);
98 } else {
99 setSig($data_dir, $username, $key, $ident['signature']);
100 }
101
102 }
103
104 setPref($data_dir, $username, 'identities', $cnt);
105
106 }
107
108 /**
109 * Returns an array with a fixed set of identities
110 *
111 * @param array $identities Array of identities
112 * @param int $id Identity to modify
113 * @param string $action Action to perform
114 * @return array
115 */
116 function sqfixidentities( $identities, $id, $action ) {
117
118 $fixed = array();
119 $tmp_hold = array();
120 $i = 0;
121
122 if (empty($identities) || !is_array($identities)) {
123 return $fixed;
124 }
125
126 foreach( $identities as $key=>$ident ) {
127
128 if (empty_identity($ident)) {
129 continue;
130 }
131
132 switch($action) {
133
134 case 'makedefault':
135
136 if ($key == $id) {
137 $fixed[0] = $ident;
138 continue 2;
139 } else {
140 $fixed[$i+1] = $ident;
141 }
142 break;
143
144 case 'move':
145
146 if ($key == ($id - 1)) {
147 $tmp_hold = $ident;
148 continue 2;
149 } else {
150 $fixed[$i] = $ident;
151
152 if ($key == $id) {
153 $i++;
154 $fixed[$i] = $tmp_hold;
155 }
156 }
157 break;
158
159 case 'delete':
160
161 if ($key == $id) {
162 continue 2;
163 } else {
164 $fixed[$i] = $ident;
165 }
166 break;
167
168 // we should never hit this but just in case //
169 default:
170 $fixed[$i] = $ident;
171
172 }
173
174 // Inc array index //
175 $i++;
176 }
177
178 ksort($fixed);
179 return $fixed;
180
181 }
182
183 /**
184 * Function to test if identity is empty
185 *
186 * @param array $identity Identitiy Array
187 * @return boolean
188 */
189 function empty_identity($ident) {
190 if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
191 return true;
192 } else {
193 return false;
194 }
195 }
196
197 ?>