dd72f305 |
1 | <?php |
ef870322 |
2 | /** |
3 | ** addressbook_csvimport.php |
4 | ** |
5 | ** Copyright (c) 1999-2000 The SquirrelMail development team |
6 | ** Licensed under the GNU GPL. For full terms see the file COPYING. |
dd72f305 |
7 | ** |
8 | ** Import csv files for address book |
9 | ** This takes a comma delimited file uploaded from addressbook.php |
10 | ** and allows the user to rearrange the field order to better |
11 | ** fit the address book. A subset of data is manipulated to save time. |
12 | ** |
ef870322 |
13 | **/ |
dd72f305 |
14 | |
15 | |
16 | session_start(); |
17 | |
18 | if(!isset($username)) { |
19 | echo "You need a valid user and password to access this page!"; |
20 | exit; |
21 | } |
22 | if (!isset($config_php)) |
d20896f6 |
23 | include("../config/config.php"); |
dd72f305 |
24 | if (!isset($i18n_php)) |
d20896f6 |
25 | include("../functions/i18n.php"); |
26 | if (!isset($page_header_php)) |
27 | include("../functions/page_header.php"); |
28 | if (!isset($addressbook_php)) |
29 | include("../functions/addressbook.php"); |
30 | if (!isset($strings_php)) |
31 | include("../functions/strings.php"); |
dd72f305 |
32 | |
d20896f6 |
33 | include("../src/load_prefs.php"); |
f2da9db0 |
34 | |
35 | /** commented out until a way to deal with file rnaming is figured out. |
d20896f6 |
36 | displayPageHeader($color, "None"); |
37 | |
38 | if (!isset($smusercsv) || $smusercsv == "none") { |
39 | echo "<br><br>\n"; |
40 | echo "<table align=\"center\">\n"; |
41 | echo " <tr>\n"; |
42 | echo " <td>" . _("Please select a file for uploading. You can do this by clicking on the browse button on the ") . "<a href=\"addressbook.php\">" . _("Address Book") . "</a> " . _("page.") . "</td>\n"; |
43 | echo " </tr>\n"; |
44 | echo "</table>\n"; |
45 | echo "</body></html>\n"; |
46 | exit; |
47 | } |
f2da9db0 |
48 | **/ |
dd72f305 |
49 | |
50 | /** |
51 | ** Script-wide vars. Used all over the place or something you might want to change later. |
52 | ** $example -->Sets how many rows of user data the script will use to play with before final write. |
53 | ** $abook ---->Setup the addressbook functions for Pallo's Addressbook. |
f2da9db0 |
54 | ** $tabindex ->A starting value for the loop to assign a tabindex value for the generated text input fields. |
55 | ** As long as this value is greater than the number of static submit buttons all should work fine. |
dd72f305 |
56 | **/ |
f2da9db0 |
57 | $tabindex = 10; |
dd72f305 |
58 | $example = 6; |
59 | $abook = addressbook_init(true, true); |
60 | |
61 | /** See if the submit button has been clicked, if not, set up arrays with empty elements |
62 | ** To change the number of text boxes displayed in headers, add or delete ,"" 's |
63 | **/ |
64 | if(!$submit || $reset){ |
65 | $nickname = array("","",""); |
66 | $firstname = array("","",""); |
67 | $lastname = array("","",""); |
68 | $email = ""; |
69 | $label = array("","","","","","","","",""); |
70 | } |
71 | |
72 | if($flag <= 1) { // If first run of script, setup the filename to copy |
73 | $tempfilename = ($username . (time()+2592000)); |
74 | $nameholder = $tempfilename; |
c7ecee3a |
75 | if(copy($smusercsv, "$attachment_dir$tempfilename")) { // Set up variable to use in printing status |
dd72f305 |
76 | $goodcopy = true; |
77 | } else { |
78 | $goodcopy = false; |
79 | } |
80 | } elseif($flag >= 2) { // If not use the name already set up |
81 | $tempfilename = $nameholder; |
82 | } |
83 | |
84 | // table with directions |
85 | if(!$finish) { |
86 | echo "<FORM METHOD=\"post\">\n"; |
87 | echo "<CENTER><TABLE BGCOLOR=\"$color[9]\" WIDTH=\"70%\" FRAME=\"void\" NOWRAP>\n"; |
88 | echo " <TR>\n"; |
89 | echo " <TD ALIGN=\"center\">", _("All the steps required to complete the process are listed below"), "</TD>\n"; |
90 | echo " </TR>\n"; |
91 | echo " <TR>\n"; |
92 | echo " <TD>"; |
93 | echo _("You uploaded a file named: "); |
94 | echo "<B>$smusercsv_name </B>"; |
95 | echo "</TD>\n"; |
96 | echo " </TR>\n"; |
97 | echo " <TR>\n"; |
98 | echo " <TD>"; |
c7ecee3a |
99 | if(!$goodcopy && $flag == 0) { // print correct status of file copying |
dd72f305 |
100 | echo _("Failed to create working copy, Please try again."); |
101 | } else { |
102 | echo _("Created working copy, continuing with process..."); |
103 | } |
104 | echo "</TD></TR>\n"; |
105 | echo " <TR>\n"; |
106 | echo " <TD>", _("Displaying a small set of your data."), "</TD>"; |
107 | echo " </TR>\n"; |
108 | echo " <TR>\n"; |
109 | echo " <TD>", _("Arrange your data to fit the 5 address book fields. "), _("Do this by inserting the data's field number under the field for which you wish it to be included into the address book. "), _("For example: fields 5, 6, and 7 need to go into the info field 5. "), _("The boxes under field 5 would contain 5, 6, and 7 in seperate boxes. "), "</TD>\n"; |
110 | echo " <TR>\n"; |
111 | echo " <TD>", _("Submit Your reorganized data."), "      ", "\n"; |
f2da9db0 |
112 | echo " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Submit\" TABINDEX=\"1\">", "      \n"; |
dd72f305 |
113 | echo " <INPUT TYPE=\"hidden\" NAME=\"flag\" VALUE=\"2\">\n"; |
114 | echo " <INPUT TYPE=\"hidden\" NAME=\"nameholder\" VALUE=$nameholder>\n"; |
115 | echo " <INPUT TYPE=\"hidden\" NAME=\"email\" VALUE=$email>\n"; |
116 | echo " ", _("Erase entries and re-enter field numbers."), "      ", "\n"; |
f2da9db0 |
117 | echo " <INPUT TYPE=reset VALUE=reset TABINDEX=\"2\">\n"; |
dd72f305 |
118 | echo " </TD>\n"; |
119 | echo " </TR>\n"; |
120 | echo " <TR>\n"; |
5c3377da |
121 | echo " <TD>", _("View full set of imported records in their new format."), "      \n"; |
f2da9db0 |
122 | echo " <INPUT TYPE=\"submit\" NAME=\"all\" VALUE=\"Show all Records\" TABINDEX=\"3\">\n"; |
dd72f305 |
123 | echo " <INPUT TYPE=\"hidden\" NAME=\"submit\" VALUE=\"Submit\">"; |
124 | echo " </TD>"; |
125 | echo " </TR>\n"; |
126 | echo " <TR>\n"; |
127 | echo " <TD>", _("Omit individual records which are not to be included."), "\n"; |
128 | echo " ", _("To the left of each field below the \"Omit\" heading is a checkbox."), "\n"; |
129 | echo " ", _("Click this checkbox to omit individual records."), "\n"; |
130 | echo " </TD>"; |
131 | echo " </TR>\n"; |
132 | echo " <TR>\n"; |
133 | echo " <TD>", _("Final approval. "); |
134 | echo " ", _("After reviewing the rearranged data for accuracy, click \"Finish\"."), "      \n"; |
f2da9db0 |
135 | echo " <INPUT TYPE=\"submit\" NAME=\"finish\" VALUE=\"Finish\" TABINDEX=\"4\">\n"; |
dd72f305 |
136 | echo " </TD>"; |
137 | echo " </TR>\n"; |
138 | echo "</TABLE>\n"; |
139 | } else { |
140 | echo "<BR><BR><H1><STRONG><CENTER>", _("Upload Completed!"), "</STRONG></H1>", _("Click on the link below to verify your work."), "</CENTER>"; |
141 | echo "<BR><BR><CENTER><A HREF=\"addressbook.php\">" . _("Addresses") . "</A></CENTER>\n"; |
142 | } |
143 | |
144 | /** |
145 | ** Use fgetcsv to make an array out of the uploaded file |
146 | ** Display a sample to see if the data looks good to the user. |
147 | **/ |
148 | |
149 | // open the correct filename to work with |
150 | if($flag <= 1) { // before submit |
c7ecee3a |
151 | $fp = fopen("$attachment_dir$tempfilename", "r"); |
dd72f305 |
152 | } elseif($flag >= 2) { // after submit |
c7ecee3a |
153 | $fp = fopen("$attachment_dir$nameholder", "r"); |
dd72f305 |
154 | } |
155 | |
156 | echo "<CENTER><TABLE WIDTH=\"95%\" FRAME=\"void\" CELLSPACING=\"1\">\n"; // user's data table |
157 | |
158 | // This loop sets up a table of the data they uploaded to save time while the user rearranges it. |
159 | $row = 0; |
160 | do { |
161 | ($data = fgetcsv($fp, 4096)); |
162 | $cols = count($data); |
163 | $row++; |
164 | |
165 | if($flag == 0 && !$finish) { // Table header on initial import |
166 | echo " <TH BGCOLOR=\"$color[9]\" HEIGHT=\"35\" COLSPAN=\"$cols\" BGCOLOR=\"$color[11]\">"; |
167 | echo "<STRONG>", _("This table shows your data after uploading it."), "</STRONG>"; |
168 | echo "</TH><BR>\n"; |
169 | } elseif($flag == 2 && !$finish) { // Table header after field changes made |
170 | echo " <TH BGCOLOR=\"$color[9]\" HEIGHT=\"35\" COLSPAN=\"$cols\" BGCOLOR=\"$color[11]\">"; |
171 | echo "<STRONG>", _("This table shows your data after you reorganized it."), "</STRONG>"; |
172 | echo "</TH><BR>\n"; |
173 | } |
174 | |
175 | switch($flag) { // This switch sets up a method so that proper looping can be done for the table output |
176 | |
177 | case 0: |
178 | case 1: |
179 | case 2: // Should probably be header since thats really what they are, maybe later |
180 | |
181 | if(!$finish) { // Set up column headers unless we are finished |
182 | echo " <TR BGCOLOR=\"$color[9]\" ALIGN=\"center\">\n"; // fill in above the omit field |
183 | echo " <TD>", " ", "</TD>\n"; |
184 | echo " <TD>", _("Field Number: "), "0", "<BR>\n"; // number the fields so they know what to put where |
185 | reset($nickname); |
186 | while(list($k,$v)=each($nickname)) { // print out the text boxes for this var |
f2da9db0 |
187 | echo" <INPUT TYPE=text NAME=nickname[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n"; |
188 | $tabindex++; |
dd72f305 |
189 | } |
190 | echo " </TD>\n"; |
191 | echo " <TD>", _("Field Number: "), "1", "<BR>\n"; // number the fields so they know what to put where |
192 | reset($firstname); |
193 | while(list($k,$v)=each($firstname)) { // print out the text boxes for this var |
f2da9db0 |
194 | echo" <INPUT TYPE=text NAME=firstname[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n"; |
195 | $tabindex++; |
dd72f305 |
196 | } |
197 | echo " </TD>\n"; |
198 | echo " <TD>", _("Field Number: "), "2", "<BR>\n"; // number the fields so they know what to put where |
199 | reset($lastname); |
200 | while(list($k,$v)=each($lastname)) { // print out the text boxes for this var |
f2da9db0 |
201 | echo" <INPUT TYPE=text NAME=lastname[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n"; |
202 | $tabindex++; |
dd72f305 |
203 | } |
204 | echo " </TD>\n"; // email isn't an array so no loop needed |
205 | echo " <TD>", _("Field Number: "), "3", "<BR>\n"; // number the fields so they know what to put where |
f2da9db0 |
206 | echo " <INPUT TYPE=text NAME=email MAXLENGTH=\"2\" SIZE=\"2\" TABINDEX=\"".$tabindex."\" VALUE=$email>\n"; |
207 | $tabindex++; |
dd72f305 |
208 | echo " </TD>\n"; |
209 | echo " <TD>", _("Field Number: "), "4", "<BR>\n"; // number the fields so they know what to put where |
210 | reset($label); |
211 | while(list($k,$v)=each($label)) { // print out the text boxes for this var |
f2da9db0 |
212 | echo" <INPUT TYPE=text NAME=label[$k] MAXLENGTH=2 SIZE=2 TABINDEX=\"".$tabindex."\" VALUE=\"".$v."\"><BR>\n"; |
213 | $tabindex++; |
dd72f305 |
214 | } |
215 | echo " </TD>\n"; |
216 | |
217 | // print field numbers for importable fields |
218 | $fcols = $cols; |
219 | $i = 5; |
220 | for($cols > 4;$fcols > 5 ; --$fcols) { |
221 | echo " <TD>", _("Field Number: "), "$i", "</TD>\n"; |
222 | $i++; |
223 | } |
224 | // give the imported columns a name so the user knows what they will be put into |
225 | echo " </TR>\n"; |
226 | echo " <TR BGCOLOR=\"$color[9]\" ALIGN=\"center\">\n"; |
227 | echo " <TD WIDTH=\"1\">", _("Omit"), "</TD>\n"; |
228 | echo " <TD>", _("Nickname:"), "</TD>\n"; |
229 | echo " <TD>", _("First name:"), "</TD>\n"; |
230 | echo " <TD>", _("Last name:"), "</TD>\n"; |
231 | echo " <TD>", _("E-mail address:"), "</TD>\n"; |
232 | echo " <TD>", _("Additional info:"), "</TD>\n"; |
233 | |
234 | // print some instruction in the header above the fields that need to be combined into other fields |
235 | $fcols = $cols; |
236 | for($cols > 4;$fcols > 5 ; --$fcols) { |
237 | echo " <TD>", _("Move to field"), "<BR>", _("1-5 to include."), "</TD>\n"; |
238 | } |
239 | echo " </TR>"; |
240 | echo "</TH><BR>\n"; |
241 | } |
242 | |
243 | case 3: // user's table data |
244 | |
245 | if($row % 2 && !$finish) { // Set up the alternating colored rows |
246 | echo " <TR BGCOLOR=\"$color[0]\">\n"; |
247 | } elseif(!$finish){ |
248 | echo " <TR>\n"; |
249 | } |
250 | echo " <TD WIDTH=\"1\"><INPUT TYPE=checkbox NAME=\"sel[]\" VALUE=\"omit\">"; // Print the omit checkbox, to be checked before write |
251 | |
252 | for($c=0; $c<$cols; $c++) { // Spit out the table cells |
253 | // concatenate fields based on user input into text boxes. |
254 | if($submit) { |
255 | switch($c) { // This switch puts the correct data into the correct fields |
256 | case 0: // concactenate nickname field |
257 | |
258 | reset($nickname); |
259 | $j = 0; |
260 | while(list($k,$v)=each($nickname)) { |
261 | if($v != "" && $j == 0) { |
262 | $reorg = "$data[$v]"; // put data in without coma |
263 | } elseif($v != "") { |
264 | $reorg .= "; $data[$v]"; // put data in with coma |
265 | } else " "; // put in space to keep the row colors going |
266 | $j++; |
267 | } |
268 | $addaddr["nickname"] = $reorg; // assign value for writing |
269 | break; |
270 | |
271 | case 1: // concactenate firstname field |
272 | |
273 | reset($firstname); |
274 | $j = 0; |
275 | while(list($k,$v)=each($firstname)) { |
276 | if($v != "" && $j == 0) { |
277 | $reorg = "$data[$v]"; |
278 | } elseif($v != "") { |
279 | $reorg .= "; $data[$v]"; |
280 | } else " "; |
281 | $j++; |
282 | } |
283 | $addaddr["firstname"] = $reorg; |
284 | break; |
285 | |
286 | case 2: // concactenate lastname field |
287 | |
288 | reset($lastname); |
289 | $j = 0; |
290 | while(list($k,$v)=each($lastname)) { |
291 | if($v != "" && $j == 0) { |
292 | $reorg = "$data[$v]"; |
293 | } elseif($v != "") { |
294 | $reorg .= "; $data[$v]"; |
295 | } else " "; |
296 | $j++; |
297 | } |
298 | $addaddr["lastname"] = $reorg; |
299 | break; |
300 | |
301 | case 3: // should only have one field in $email |
302 | |
303 | $reorg = $data[$email]; |
304 | $addaddr["email"] = $reorg; |
305 | break; |
306 | |
307 | case 4: // concactenate label field |
308 | |
309 | reset($label); |
310 | $j = 0; |
311 | while(list($k,$v)=each($label)) { |
312 | if($v != "" && $j == 0) { |
313 | $reorg = "$data[$v]"; |
314 | } elseif($v != "") { |
315 | $reorg .= "; $data[$v]"; |
316 | } else " "; |
317 | $j++; |
318 | } |
319 | $addaddr["label"] = $reorg; |
320 | } |
321 | } else $reorg = $data[$c]; |
322 | |
323 | if($reorg != "" && !$finish) { // if not empty, put data in cell. |
324 | trim($reorg); |
325 | echo " <TD NOWRAP>$reorg</TD>\n"; |
326 | } elseif(!$finish) { // if empty, put space in cell keeping colors correct. |
327 | echo " <TD> </TD>\n"; |
328 | } else |
329 | $reorg = ""; |
330 | } |
331 | echo " </TR>\n"; |
332 | $flag = 3; |
333 | break; |
334 | } |
335 | // If finished, do the import. This uses Pallo's excellent class and object stuff |
336 | if($finish && !$sel[$row]) { |
337 | $r = $abook->add($addaddr,$abook->localbackend); |
338 | if(!r) { |
339 | print $this->error; |
340 | } |
341 | unset($addaddr); |
342 | } |
343 | // How far should we loop through the users' data. |
344 | if($row < $example && (!$all && !$finish)){ |
345 | $loop = true; |
346 | } elseif(!feof($fp) && ($all || $finish)) { |
347 | $loop = true; |
348 | } else { |
349 | $loop = false; |
350 | } |
351 | |
352 | } while($loop); |
353 | |
354 | echo "</TABLE>"; |
355 | |
356 | fclose($fp); |
357 | // unset each element in the arrays. For some reason, this doesn't work when included in the same loop as the <INPUT. |
358 | reset($nickname); |
359 | while(list($k,$v)=each($nickname)){ |
360 | unset($nickname[$k]); |
361 | } |
362 | reset($lastname); |
363 | while(list($k,$v)=each($lastname)){ |
364 | unset($lastname[$k]); |
365 | } |
366 | reset($firstname); |
367 | while(list($k,$v)=each($firstname)){ |
368 | unset($firstname[$k]); |
369 | } |
370 | reset($label); |
371 | while(list($k,$v)=each($label)){ |
372 | unset($label[$k]); |
373 | } |
374 | |
375 | // Send the field numbers entered in the text boxes by the user back to this script for more processing |
376 | // email is handled differently, not being an array |
377 | if($submit == "Submit"){ |
378 | // loop through each array and send each element |
379 | reset($nickname); |
380 | while(list($k,$v)=each($nickname)){ |
381 | echo" <INPUT TYPE=hidden NAME=nickname[$k] VALUE=\"".$v."\">\n"; |
382 | } |
383 | reset($lastname); |
384 | while(list($k,$v)=each($lastname)){ |
385 | echo" <INPUT TYPE=hidden NAME=lastname[$k] VALUE=\"".$v."\">\n"; |
386 | } |
387 | reset($firstname); |
388 | while(list($k,$v)=each($firstname)){ |
389 | echo" <INPUT TYPE=hidden NAME=firstname[$k] VALUE=\"".$v."\">\n"; |
390 | } |
391 | reset($label); |
392 | while(list($k,$v)=each($label)){ |
393 | echo" <INPUT TYPE=hidden NAME=label[$k] VALUE=\"".$v."\">\n"; |
394 | } |
395 | } |
396 | |
397 | // Clean up after ourselves. |
398 | if($finish) { |
c7ecee3a |
399 | unlink ("$attachment_dir$tempfilename"); |
dd72f305 |
400 | } |
401 | |
402 | ?> |
403 | </FORM> |
404 | </BODY> |
405 | </HTML> |