replace string "Cute." with "Invalid URL" (reuse of existing string)
[squirrelmail.git] / functions / identity.php
CommitLineData
b1f45342 1<?php
2
3/**
4 * identity.php
5 *
b1f45342 6 * This contains utility functions for dealing with multiple identities
7 *
4b5049de 8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
4b4abf93 9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 10 * @version $Id$
d6c32258 11 * @package squirrelmail
0392886d 12 * @since 1.4.2
b1f45342 13 */
14
b1f45342 15
16/**
0392886d 17 * Returns an array of all the identities.
18 * Array is keyed: full_name, reply_to, email_address, index, signature
19 * @return array full_name,reply_to,email_address,index,signature
20 * @since 1.4.2
21 */
b1f45342 22function get_identities() {
23
b6e70801 24 global $username, $data_dir, $domain;
25
26 $em = getPref($data_dir,$username,'email_address');
f16477bc 27 if ( ! $em ) {
28 if (strpos($username , '@') == false) {
29 $em = $username.'@'.$domain;
30 } else {
31 $em = $username;
32 }
33 }
b1f45342 34 $identities = array();
35 /* We always have this one, even if the user doesn't use multiple identities */
0f4f003e 36 $identities[] = array('full_name' => getPref($data_dir,$username,'full_name'),
b6e70801 37 'email_address' => $em,
b1f45342 38 'reply_to' => getPref($data_dir,$username,'reply_to'),
39 'signature' => getSig($data_dir,$username,'g'),
40 'index' => 0 );
41
b6e70801 42 $num_ids = getPref($data_dir,$username,'identities');
b1f45342 43 /* If there are any others, add them to the array */
de981f8c 44 if (!empty($num_ids) && $num_ids > 1) {
45 for ($i=1;$i<$num_ids;$i++) {
0f4f003e 46 $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i),
b1f45342 47 'email_address' => getPref($data_dir,$username,'email_address' . $i),
48 'reply_to' => getPref($data_dir,$username,'reply_to' . $i),
49 'signature' => getSig($data_dir,$username,$i),
50 'index' => $i );
51 }
52 }
53
54 return $identities;
55}
56
e7f9c987 57/**
58 * Function to save the identities array
59 *
60 * @param array $identities Array of identities
0392886d 61 * @since 1.5.1 and 1.4.5
e7f9c987 62 */
63function save_identities($identities) {
64
65 global $username, $data_dir, $domain;
66
67 if (empty($identities) || !is_array($identities)) {
68 return;
69 }
70
71
72 $num_cur = getPref($data_dir, $username, 'identities');
202bcbcc 73
e7f9c987 74 $cnt = count($identities);
75
76 // Remove any additional identities in prefs //
77 for($i=$cnt; $i <= $num_cur; $i++) {
78 removePref($data_dir, $username, 'full_name' . $i);
79 removePref($data_dir, $username, 'email_address' . $i);
80 removePref($data_dir, $username, 'reply_to' . $i);
81 setSig($data_dir, $username, $i, '');
82 }
83
84 foreach($identities as $id=>$ident) {
85
86 $key = ($id?$id:'');
87
88 setPref($data_dir, $username, 'full_name' . $key, $ident['full_name']);
89 setPref($data_dir, $username, 'email_address' . $key, $ident['email_address']);
90 setPref($data_dir, $username, 'reply_to' . $key, $ident['reply_to']);
91
92 if ($id === 0) {
93 setSig($data_dir, $username, 'g', $ident['signature']);
94 } else {
95 setSig($data_dir, $username, $key, $ident['signature']);
96 }
97
98 }
99
100 setPref($data_dir, $username, 'identities', $cnt);
101
102}
103
104/**
105 * Returns an array with a fixed set of identities
106 *
107 * @param array $identities Array of identities
108 * @param int $id Identity to modify
109 * @param string $action Action to perform
110 * @return array
0392886d 111 * @since 1.5.1 and 1.4.5
e7f9c987 112 */
113function sqfixidentities( $identities, $id, $action ) {
114
115 $fixed = array();
116 $tmp_hold = array();
117 $i = 0;
118
119 if (empty($identities) || !is_array($identities)) {
120 return $fixed;
121 }
122
123 foreach( $identities as $key=>$ident ) {
124
125 if (empty_identity($ident)) {
126 continue;
127 }
128
129 switch($action) {
130
131 case 'makedefault':
132
133 if ($key == $id) {
134 $fixed[0] = $ident;
3df61ef3 135
136 // inform plugins about renumbering of ids
d849b570 137 do_hook('options_identities_renumber', $temp=array(&$id, 'default'));
3df61ef3 138
e7f9c987 139 continue 2;
140 } else {
141 $fixed[$i+1] = $ident;
142 }
143 break;
144
145 case 'move':
146
147 if ($key == ($id - 1)) {
148 $tmp_hold = $ident;
3df61ef3 149
150 // inform plugins about renumbering of ids
d849b570 151 do_hook('options_identities_renumber', $temp=array(&$id , $id - 1));
3df61ef3 152
e7f9c987 153 continue 2;
154 } else {
155 $fixed[$i] = $ident;
156
157 if ($key == $id) {
158 $i++;
159 $fixed[$i] = $tmp_hold;
160 }
161 }
162 break;
163
164 case 'delete':
165
166 if ($key == $id) {
1d6248ce 167 // inform plugins about deleted id
d849b570 168 do_hook('options_identities_process', $temp=array(&$action, &$id));
1d6248ce 169
e7f9c987 170 continue 2;
171 } else {
172 $fixed[$i] = $ident;
173 }
174 break;
175
3df61ef3 176 // Process actions from plugins and save/update action //
e7f9c987 177 default:
3df61ef3 178 /**
202bcbcc 179 * send action and id information. number of hook arguments
180 * differs from 1.4.4 or older and 1.5.0. count($args) can
181 * be used to detect modified hook. Older hook does not
3df61ef3 182 * provide information that can be useful for plugins.
183 */
d849b570 184 do_hook('options_identities_process', $temp=array(&$action, &$id));
3df61ef3 185
e7f9c987 186 $fixed[$i] = $ident;
187
188 }
189
190 // Inc array index //
191 $i++;
192 }
193
194 ksort($fixed);
195 return $fixed;
196
197}
198
199/**
200 * Function to test if identity is empty
201 *
202 * @param array $identity Identitiy Array
203 * @return boolean
0392886d 204 * @since 1.5.1 and 1.4.5
e7f9c987 205 */
206function empty_identity($ident) {
207 if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
208 return true;
209 } else {
210 return false;
211 }
212}