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