Author: Jan Schneider (jan@horde.org)
Author: Chuck Hagenbuch (chuck@horde.org)
Example #1
0
 /**
  * Takes all necessary actions for the given import step, parameters and
  * form values and returns the next necessary step.
  *
  * @param integer $action  The current step. One of the IMPORT_* constants.
  * @param array $param     An associative array containing needed
  *                         parameters for the current step.
  *
  * @return mixed  Either the next step as an integer constant or imported
  *                data set after the final step.
  * @throws Horde_Data_Exception
  */
 public function nextStep($action, $param = array())
 {
     switch ($action) {
         case Horde_Data::IMPORT_FILE:
             parent::nextStep($action, $param);
             return $this->importData($_FILES['import_file']['tmp_name']);
     }
 }
Example #2
0
File: Csv.php Project: horde/horde
 /**
  * Takes all necessary actions for the given import step, parameters and
  * form values and returns the next necessary step.
  *
  * @param integer $action  The current step. One of the IMPORT_* constants.
  * @param array $param     An associative array containing needed
  *                         parameters for the current step. Keys for this
  *                         driver:
  *   - check_charset: (boolean) Do some checks to see if the correct
  *                    charset has been provided. Throws charset exception
  *                    on error.
  *   - import_mapping: TODO
  *
  * @return mixed  Either the next step as an integer constant or imported
  *                data set after the final step.
  * @throws Horde_Data_Exception
  * @throws Horde_Data_Exception_Charset
  */
 public function nextStep($action, array $param = array())
 {
     switch ($action) {
         case Horde_Data::IMPORT_FILE:
             parent::nextStep($action, $param);
             /* Move uploaded file so that we can read it again in the next
                step after the user gave some format details. */
             $file_name = $_FILES['import_file']['tmp_name'];
             if (($file_data = file_get_contents($file_name)) === false) {
                 throw new Horde_Data_Exception(Horde_Data_Translation::t("The uploaded file could not be saved."));
             }
             /* Do charset checking now, if requested. */
             if (isset($param['check_charset'])) {
                 $charset = isset($this->_vars->charset) ? Horde_String::lower($this->_vars->charset) : 'utf-8';
                 switch ($charset) {
                     case 'utf-8':
                         $error = !Horde_String::validUtf8($file_data);
                         break;
                     default:
                         $error = $file_data != Horde_String::convertCharset(Horde_String::convertCharset($file_data, $charset, 'UTF-8'), 'UTF-8', $charset);
                         break;
                 }
                 if ($error) {
                     $e = new Horde_Data_Exception_Charset(Horde_Data_Translation::t("Incorrect charset given for the data."));
                     $e->badCharset = $charset;
                     throw $e;
                 }
             }
             $this->storage->set('charset', $this->_vars->charset);
             $this->storage->set('file_data', $file_data);
             /* Read the file's first two lines to show them to the user. */
             $first_lines = '';
             if ($fp = @fopen($file_name, 'r')) {
                 for ($line_no = 1, $line = fgets($fp); $line_no <= 3 && $line; $line_no++, $line = fgets($fp)) {
                     $line = Horde_String::convertCharset($line, $this->_vars->charset, 'UTF-8');
                     $first_lines .= Horde_String::truncate($line);
                     if (Horde_String::length($line) > 100) {
                         $first_lines .= "\n";
                     }
                 }
             }
             $this->storage->set('first_lines', $first_lines);
             /* Import the first line to guess the number of fields. */
             if ($first_lines) {
                 rewind($fp);
                 $line = self::getCsv($fp);
                 if ($line) {
                     $this->storage->set('fields', count($line));
                 }
             }
             return Horde_Data::IMPORT_CSV;
         case Horde_Data::IMPORT_CSV:
             $this->storage->set('header', $this->_vars->header);
             $import_mapping = array();
             if (isset($param['import_mapping'])) {
                 $import_mapping = $param['import_mapping'];
             }
             $file_name = Horde_Util::getTempFile('import');
             file_put_contents($file_name, $this->storage->get('file_data'));
             $this->storage->set('data', $this->importFile($file_name, $this->_vars->header, $this->_vars->sep, $this->_vars->quote, $this->_vars->fields, $import_mapping, $this->storage->get('charset'), $this->storage->get('crlf')));
             $this->storage->set('map');
             return Horde_Data::IMPORT_MAPPED;
         default:
             return parent::nextStep($action, $param);
     }
 }
Example #3
0
 /**
  * Takes all necessary actions for the given import step, parameters and
  * form values and returns the next necessary step.
  *
  * @param integer $action  The current step. One of the IMPORT_* constants.
  * @param array $param     An associative array containing needed
  *                         parameters for the current step.
  *
  * @return mixed  Either the next step as an integer constant or imported
  *                data set after the final step.
  * @throws Horde_Data_Exception
  */
 public function nextStep($action, array $param = array())
 {
     switch ($action) {
         case Horde_Data::IMPORT_FILE:
             parent::nextStep($action, $param);
             $f_data = $this->importFile($_FILES['import_file']['tmp_name']);
             $data = array();
             foreach ($f_data as $record) {
                 $turbaHash = array();
                 foreach ($this->_turbaAttr as $value) {
                     switch ($value) {
                         case 'homeAddress':
                             // These are the keys we're interested in.
                             $keys = array('homeStreet', 'mozillaHomeStreet2', 'mozillaHomeLocalityName', 'mozillaHomeState', 'mozillaHomePostalCode', 'mozillaHomeCountryName');
                             // Grab all of them that exist in $record.
                             $values = array_intersect_key($record, array_flip($keys));
                             // Special handling for State if both State
                             // and Locality Name are set.
                             if (isset($values['mozillaHomeLocalityName']) && isset($values['mozillaHomeState'])) {
                                 $values['mozillaHomeLocalityName'] .= ', ' . $values['mozillaHomeState'];
                                 unset($values['mozillaHomeState']);
                             }
                             if ($values) {
                                 $turbaHash[$value] = implode("\n", $values);
                             }
                             break;
                         case 'workAddress':
                             // These are the keys we're interested in.
                             $keys = array('street', 'mozillaWorkStreet2', 'l', 'st', 'postalCode', 'c');
                             // Grab all of them that exist in $record.
                             $values = array_intersect_key($record, array_flip($keys));
                             // Special handling for "st" if both "st" and
                             // "l" are set.
                             if (isset($values['l']) && isset($values['st'])) {
                                 $values['l'] .= ', ' . $values['st'];
                                 unset($values['st']);
                             }
                             if ($values) {
                                 $turbaHash[$value] = implode("\n", $values);
                             }
                             break;
                         default:
                             if (isset($record[$this->_turbaMozillaMap[$value]])) {
                                 $turbaHash[$value] = $record[$this->_turbaMozillaMap[$value]];
                             }
                             break;
                     }
                 }
                 $data[] = $turbaHash;
             }
             $this->storage->set('data', null);
             return $data;
         default:
             return parent::nextStep($action, $param);
     }
 }
Example #4
0
 /**
  * Takes all necessary actions for the given import step,
  * parameters and form values and returns the next necessary step.
  *
  * @param integer $action  The current step. One of the IMPORT_* constants.
  * @param array $param     An associative array containing needed
  *                         parameters for the current step.
  *
  * @return mixed  Either the next step as an integer constant or imported
  *                data set after the final step.
  * @throws Horde_Data_Exception
  */
 public function nextStep($action, array $param = array())
 {
     switch ($action) {
         case Horde_Data::IMPORT_FILE:
             parent::nextStep($action, $param);
             $this->importFile($_FILES['import_file']['tmp_name']);
             return $this->_iCal->getComponents();
         case Horde_Data::IMPORT_URL:
             parent::nextStep($action, $param);
             $this->importUrl($this->_vars->import_url);
             return $this->_iCal->getComponents();
     }
     return parent::nextStep($action, $param);
 }
Example #5
0
 /**
  * Takes all necessary actions for the given import step, parameters and
  * form values and returns the next necessary step.
  *
  * @param integer $action  The current step. One of the IMPORT_* constants.
  * @param array $param     An associative array containing needed
  *                         parameters for the current step.
  *
  * @return mixed  Either the next step as an integer constant or imported
  *                data set after the final step.
  * @throws Horde_Data_Exception
  */
 public function nextStep($action, array $param = array())
 {
     switch ($action) {
         case Horde_Data::IMPORT_FILE:
             parent::nextStep($action, $param);
             $format = $this->storage->get('format');
             if (in_array($format, array('mulberry', 'pine'))) {
                 $filedata = $this->importFile($_FILES['import_file']['tmp_name']);
                 switch ($format) {
                     case 'mulberry':
                         $appKeys = array('alias', 'name', 'email', 'company', 'workAddress', 'workPhone', 'homePhone', 'fax', 'notes');
                         $dataKeys = array(0, 1, 2, 3, 4, 5, 6, 7, 9);
                         break;
                     case 'pine':
                         $appKeys = array('alias', 'name', 'email', 'notes');
                         $dataKeys = array(0, 1, 2, 4);
                         break;
                 }
                 foreach ($appKeys as $key => $app) {
                     $map[$dataKeys[$key]] = $app;
                 }
                 $data = array();
                 foreach ($filedata as $row) {
                     $hash = array();
                     switch ($format) {
                         case 'mulberry':
                             if (preg_match("/^Grp:/", $row[0]) || empty($row[1])) {
                                 continue;
                             }
                             $row[1] = preg_replace('/^([^,"]+),\\s*(.*)$/', '$2 $1', $row[1]);
                             foreach ($dataKeys as $key) {
                                 if (array_key_exists($key, $row)) {
                                     $hash[$key] = stripslashes(preg_replace('/\\\\r/', "\n", $row[$key]));
                                 }
                             }
                             break;
                         case 'pine':
                             if (count($row) < 3 || preg_match("/^#DELETED/", $row[0]) || preg_match("/[()]/", $row[2])) {
                                 continue;
                             }
                             $row[1] = preg_replace('/^([^,"]+),\\s*(.*)$/', '$2 $1', $row[1]);
                             /* Address can be a full RFC822 address */
                             $addr_ob = new Horde_Mail_Rfc822_Address($row[2]);
                             if (!$addr_ob->valid) {
                                 continue;
                             }
                             $row[2] = $addr_ob->bare_address;
                             if (empty($row[1]) && !is_null($addr_ob->personal)) {
                                 $row[1] = $addr_ob->personal;
                             }
                             foreach ($dataKeys as $key) {
                                 if (array_key_exists($key, $row)) {
                                     $hash[$key] = $row[$key];
                                 }
                             }
                             break;
                     }
                     $data[] = $hash;
                 }
                 $this->storage->set('data', $data);
                 $this->storage->set('map', $map);
                 return $this->nextStep(Horde_Data::IMPORT_DATA, $param);
             }
             /* Store uploaded file data so that we can read it again in the
              * next step after the user gives some format details. */
             try {
                 $this->_browser->wasFileUploaded('import_file', Horde_Data_Translation::t("TSV file"));
             } catch (Horde_Browser_Exception $e) {
                 throw new Horde_Data_Exception($e);
             }
             $file_name = $_FILES['import_file']['tmp_name'];
             if (($file_data = file_get_contents($file_name)) === false) {
                 throw new Horde_Data_Exception(Horde_Data_Translation::t("The uploaded file could not be saved."));
             }
             $this->storage->set('file_data', $file_data);
             /* Read the file's first two lines to show them to the user. */
             $first_lines = '';
             if ($fp = @fopen($file_name, 'r')) {
                 $line_no = 1;
                 while ($line_no < 3 && ($line = fgets($fp))) {
                     $newline = Horde_String::length($line) > 100 ? "\n" : '';
                     $first_lines .= substr($line, 0, 100) . $newline;
                     ++$line_no;
                 }
             }
             $this->storage->set('first_lines', $first_lines);
             return Horde_Data::IMPORT_TSV;
         case Horde_Data::IMPORT_TSV:
             $file_name = Horde_Util::getTempFile('import');
             file_put_contents($file_name, $this->storage->get('file_data'));
             $this->storage->set('header', $this->_vars->header);
             $this->storage->set('data', $this->importFile($file_name, $this->storage->get('header')));
             $this->storage->set('map');
             return Horde_Data::IMPORT_MAPPED;
     }
     return parent::nextStep($action, $param);
 }