File: address_book_import.php

package info (click to toggle)
squirrelmail-sent-confirmation 1.6-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 172 kB
  • ctags: 149
  • sloc: php: 1,079; sh: 24; makefile: 17
file content (329 lines) | stat: -rw-r--r-- 10,429 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?PHP

  /**
   **  address_book_import.php
   **    
   **  Copyright (c) 1999-2004 The SquirrelMail development team
   **  Licensed under the GNU GPL. For full terms see the file COPYING.
   **            
   **    Import addresses from a sent message
   **      This takes any number of email addresses (as post variables
   **      titled 'address1', 'address2', etc) and processes them for 
   **      importing to the addressbook.  The user may rearrange the
   **      field order and change any of the data.
   **
   **   This was a part of the Squirrelmail source tree that was modified
   **   for use with the address book import/export plugin, and was 
   **   further mauled for use here.
   **/

   $colsize = 15;


   chdir('..');
   define('SM_PATH','../');

   
   // include compatibility plugin
   //
   if (defined('SM_PATH'))
      include_once(SM_PATH . 'plugins/compatibility/functions.php');
   else if (file_exists('../plugins/compatibility/functions.php'))
      include_once('../plugins/compatibility/functions.php');
   else if (file_exists('./plugins/compatibility/functions.php'))
      include_once('./plugins/compatibility/functions.php');


   if (compatibility_check_sm_version(1, 3))
   {
      include_once (SM_PATH . 'include/validate.php');
      include_once (SM_PATH . 'functions/page_header.php');
      include_once (SM_PATH . 'include/load_prefs.php');
      include_once (SM_PATH . 'functions/addressbook.php');
   }
   else
   {
      include_once ('../src/validate.php');
      include_once ('../functions/page_header.php');
      include_once ('../src/load_prefs.php');
      include_once ('../functions/addressbook.php');
   }


   if (compatibility_check_sm_version(1, 3))
      include_once (SM_PATH . 'plugins/sent_confirmation/config.php');
   else
      include_once ('../plugins/sent_confirmation/config.php');


   // get global variable for versions of PHP < 4.1
   //
   if (!compatibility_check_php_version(4,1)) {
      global $HTTP_POST_VARS;
      $_POST = $HTTP_POST_VARS;
   }


   bindtextdomain('sent_confirmation', SM_PATH . 'plugins/sent_confirmation/locale');
   textdomain('sent_confirmation');


   // Local Variables
   $errorstring = '';
   $finish = '';
   $csvmax = 5;
   $key = 0;
   $x = 0;
   $row = 0;
   $cols = 0;
   $colspan = 0;
   $c = 0;
   $error = 0;
   $reorg = array();
   $selrow = '';
   $addresses = array();

   global $color, $emailAddressDelimiter;
   
   // Grab POST variables
   global $finish;
   compatibility_sqextractGlobalVar('finish');


// Here we will split the script into finished and not finished parts 
if (!$finish) {

   foreach ($_POST as $varName => $varValue)
   {
      if (strpos($varName, 'address') !== FALSE)
         $addresses[] = urldecode($varValue);
   }

   displayPageHeader($color, "None");

   // These shouldn't be set at all at this point. 
   $ADDRS['csvdata'] = array(); 
   $ADDRS['csvorder'] = array();

   foreach ($addresses as $addr) { 

      list($address, $nick) = explode('---', $addr);
      if (empty($nick))
         $nick = substr($address, 0, strpos($address, $emailAddressDelimiter));
      if (preg_match('/(\w+), (\w+)/', $nick, $matches))
         $temp = array($nick, $matches[2], $matches[1], $address, '');
      else if (preg_match('/(\w+)[_ ](\w+)/', $nick, $matches))
         $temp = array($nick, $matches[1], $matches[2], $address, '');
      else
         $temp = array($nick, $nick, '', $address, '');
      
      $ADDRS['csvdata'][$key] = $temp;
      $key++;

   }

   unset($ADDRS['csvorder']);

   echo '
   <FORM METHOD="post" action="' . $PHP_SELF . '">
   <CENTER><TABLE WIDTH="95%" FRAME="void" CELLSPACING="1">
   <TR><TD colspan="5"><strong>' . _("Add To Address Book:") . '</strong><br><br></TD></TR>
   ';    // user's data table

   // Here I will create the headers that I want.
   echo '
   <TR BGCOLOR="' . $color[9] . '" ALIGN="center">
   ';
   //<TD WIDTH="1">' .  _("Omit") . '</TD>
   //'; // The Omit column

   for($x = 0; $x < $csvmax; $x++) { // The Drop down boxes to select what each column is
      echo '<TD>';
      create_Select($csvmax,$x); 
      echo '</TD>
      ';
   }

   echo '</TR>
   ';

   do {
      if (count($ADDRS['csvdata'][$row]) >= 5) {    // This if ensures the minimum number of columns
         $cols = count($ADDRS['csvdata'][$row]);    // so importing can function for all 5 fields
      } else {
         $cols = 5;
      }        

      $colspan = $cols + 1;
      if ($row % 2) {                   // Set up the alternating colored rows
         echo '<TR BGCOLOR="' . $color[0] . '">
         ';
      } else {
         echo '<TR>
         ';
      }

      //echo '<TD WIDTH="1"><INPUT TYPE="checkbox" NAME="sel' . $row . '">
      //'; // Print the omit checkbox, to be checked before write

      for($c = 0; $c < $cols; $c++) { // For each column in the current row
         if ($ADDRS['csvdata'][$row][$c] != "") {                                // if not empty, put data in cell.
            echo '<TD ALIGN="CENTER" NOWRAP><INPUT SIZE="' . $colsize . '" NAME="data' . $row . '_' . $c . '" VALUE="' . $ADDRS['csvdata'][$row][$c] . '"></TD>
            ';
         } else {                                          // if empty, put space in cell keeping colors correct.
            echo '<TD ALIGN="CENTER"><INPUT SIZE="' . $colsize . '" NAME="data' . $row . '_' . $c . '"></TD>
            ';
         }
      }
      echo '</TR>
      ';
      $row++;
   } while ($row < count($ADDRS['csvdata']));
   
   echo '
   <TR><TD colspan="5"><br><INPUT TYPE="submit" NAME="finish" VALUE="' . _("Add") . '"></TD></TR>
   </TABLE>
   </CENTER>
   ';

   if(strlen($errorstring)) {   
      echo _("The following rows have errors") . ': <p>
      ' . $errorstring;
   }
   
} else {
   /** 
    **   $abook ---->Setup the addressbook functions for Pallo's Addressbook.
    **/
   $abook = addressbook_init(true, true); // We only need to do this here because we will only access the address book in this section

   // rebuild submit data
   //
   foreach ($_POST as $varName => $varValue)
   {
      preg_match('/^data(\d)_(\d)$/', $varName, $matches);
      if (count($matches))
      {
         $ADDRS['csvdata'][$matches[1]][$matches[2]] = $varValue;
      }
   }


   do {
      if (count($ADDRS['csvdata'][$row]) >= 5) {    // This if ensures the minimum number of columns
         $cols = count($ADDRS['csvdata'][$row]);    // so importing can function for all 5 fields
      } else {
         $cols = 5;
      }  
            
      $reorg = array('', '', '', '', '');
      
      for ($c=0; $c < $cols; $c++) {
         // Reorganize the data to fit the header cells that the user chose
         // concatenate fields based on user input into text boxes.
         $column = "COL$c";
         
         if($_POST[$column] != 5)  {
            if ($_POST[$column] == 4) {
               $reorg[4] .= $ADDRS['csvdata'][$row][$c] . ";";
            } else {
               $reorg[$_POST[$column]] = $ADDRS['csvdata'][$row][$c];
               $reorg[$c] = trim($reorg[$c],"\r\n \"");
            }
         }
      }
      
      $reorg[4] = trim($reorg[4],";");
      $ADDRS['csvdata'][$row] = $reorg;
      unset($reorg); // So that we don't get any weird information from a previous rows

      // If finished, do the import. This uses Pallo's excellent class and object stuff 
      $selrow = 'sel' . $row;
      
      if (!isset($_POST[$selrow])) {
         if (eregi('[ \\:\\|\\#\\"\\!]', $ADDRS['csvdata'][$row][0])) {
            $ADDRS['csvdata'][$row][0] = '';
         }

         //Here we should create the right data to input 
         if (count($ADDRS['csvdata'][$row]) < 5) {
            array_pad($ADDRS['csvdata'][$row],5,'');
         }

         $addaddr['nickname'] 	= $ADDRS['csvdata'][$row][0];
         $addaddr['firstname'] 	= $ADDRS['csvdata'][$row][1];
         $addaddr['lastname'] 	= $ADDRS['csvdata'][$row][2];
         $addaddr['email'] 	= $ADDRS['csvdata'][$row][3];
         $addaddr['label'] 	= $ADDRS['csvdata'][$row][4];

         if (false == $abook->add($addaddr,$abook->localbackend)) {
            $errorstring .= $abook->error . "<br>\n";
            $error++;
         }

         unset($addaddr); // Also so we don't get any weird information from previous rows
      }
      
      $row++;
      
   } while($row < count($ADDRS['csvdata']));

   unset($ADDRS['csvdata']); // Now that we've uploaded this information, we dont' need this variable anymore, aka cleanup

   // Print out that we've completed this operation
   if ($error) {
      // Since we will print something to the page at this point
      displayPageHeader($color, "None");

      echo '<BR>' . _("There were errors uploading the data, as listed below. Entries not listed here were uploaded.") . '<br> ' . $errorstring . '<BR> ';
   } else {
      header('Location: ../../src/addressbook.php');
      exit;
      // Since we will print something to the page at this point
      displayPageHeader($color, "None");

      echo '<BR><BR><H1><STRONG><CENTER>' . _("Upload Completed!") . '</STRONG></H1>' . _("Click on the link below to verify your work.") . '</CENTER>';
   }

   echo '<BR><BR><CENTER><A HREF="../../src/addressbook.php">' . _("Addresses") . '</A></CENTER>
   ';
}

   // Send the field numbers entered in the text boxes by the user back to this script for more processing
   // email is handled differently, not being an array
function create_Select($csvmax,$column) {
   // $column is the one that should be selected out of the bunch
   echo "<SELECT NAME=\"COL$column\">\n";

   if($column > 5)
    $column = 5; // So we have only our normal choices. 
    
   for($temp = 0; $temp <= 5; $temp++) {
      echo "<OPTION value=$temp ";
      if ($column==$temp)
        echo "SELECTED";
      if ($temp == 0)
        echo ">" . _("Nickname") . "</option>\n";
      if ($temp == 1)
        echo ">" . _("First Name") . "</option>\n";
      if ($temp == 2)
        echo ">" . _("Last Name") . "</option>\n";
      if ($temp == 3)
        echo ">" . _("Email") . "</option>\n";
      if ($temp == 4)
        echo ">" . _("Additional Info") . "</option>\n";
      if ($temp == 5)
        echo ">" . _("Do Not Include") . "</option>\n";
   }
   echo "</select>\n";
}


   bindtextdomain('squirrelmail', SM_PATH . 'locale');
   textdomain('squirrelmail');


?>
</FORM>
</BODY>
</HTML>