Beispiel #1
0
 /**
  * @return array
  */
 public function getColumns()
 {
     if ($this->columns === null) {
         $this->csvFile->fseek(0);
         $this->columns = $this->csvFile->fgetcsv();
     }
     return $this->columns;
 }
 /**
  * @return \SplFileObject
  */
 protected function getFile()
 {
     if (!$this->file instanceof \SplFileObject) {
         $this->file = $this->fileInfo->openFile();
         $this->file->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::DROP_NEW_LINE);
         $this->file->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
         if ($this->firstLineIsHeader && !$this->header) {
             $this->header = $this->file->fgetcsv();
         }
     }
     return $this->file;
 }
 /**
  * @param string       $code
  * @param PyStringNode $csv
  *
  * @Then /^exported file of "([^"]*)" should contain:$/
  *
  * @throws ExpectationException
  * @throws \Exception
  */
 public function exportedFileOfShouldContain($code, PyStringNode $csv)
 {
     $config = $this->getFixturesContext()->getJobInstance($code)->getRawConfiguration();
     $path = $this->getMainContext()->getSubcontext('job')->getJobInstancePath($code);
     if (!is_file($path)) {
         throw $this->getMainContext()->createExpectationException(sprintf('File "%s" doesn\'t exist', $path));
     }
     $delimiter = isset($config['delimiter']) ? $config['delimiter'] : ';';
     $enclosure = isset($config['enclosure']) ? $config['enclosure'] : '"';
     $escape = isset($config['escape']) ? $config['escape'] : '\\';
     $csvFile = new \SplFileObject($path);
     $csvFile->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
     $csvFile->setCsvControl($delimiter, $enclosure, $escape);
     $expectedLines = [];
     foreach ($csv->getLines() as $line) {
         if (!empty($line)) {
             $expectedLines[] = explode($delimiter, str_replace($enclosure, '', $line));
         }
     }
     $actualLines = [];
     while ($data = $csvFile->fgetcsv()) {
         if (!empty($data)) {
             $actualLines[] = array_map(function ($item) use($enclosure) {
                 return str_replace($enclosure, '', $item);
             }, $data);
         }
     }
     $expectedCount = count($expectedLines);
     $actualCount = count($actualLines);
     assertSame($expectedCount, $actualCount, sprintf('Expecting to see %d rows, found %d', $expectedCount, $actualCount));
     if (md5(json_encode($actualLines[0])) !== md5(json_encode($expectedLines[0]))) {
         throw new \Exception(sprintf('Header in the file %s does not match expected one: %s', $path, implode(' | ', $actualLines[0])));
     }
     unset($actualLines[0]);
     unset($expectedLines[0]);
     foreach ($expectedLines as $expectedLine) {
         $originalExpectedLine = $expectedLine;
         $found = false;
         foreach ($actualLines as $index => $actualLine) {
             // Order of columns is not ensured
             // Sorting the line values allows to have two identical lines
             // with values in different orders
             sort($expectedLine);
             sort($actualLine);
             // Same thing for the rows
             // Order of the rows is not reliable
             // So we generate a hash for the current line and ensured that
             // the generated file contains a line with the same hash
             if (md5(json_encode($actualLine)) === md5(json_encode($expectedLine))) {
                 $found = true;
                 // Unset line to prevent comparing it twice
                 unset($actualLines[$index]);
                 break;
             }
         }
         if (!$found) {
             throw new \Exception(sprintf('Could not find a line containing "%s" in %s', implode(' | ', $originalExpectedLine), $path));
         }
     }
 }
 /**
  * Returns a line form the CSV file and advances the pointer to the next one
  *
  * @param Model $Model
  * @param SplFileObject $handle CSV file handler
  * @return array list of attributes fetched from the CSV file
  */
 protected function _getCSVLine(Model &$Model, SplFileObject $handle)
 {
     if ($handle->eof()) {
         return false;
     }
     return $handle->fgetcsv($this->settings[$Model->alias]['delimiter'], $this->settings[$Model->alias]['enclosure']);
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $values = $form_state->getValues();
     $file = File::load($values['file'][0]);
     // Load File entity.
     $read_file = new \SplFileObject($file->url());
     // Create file handler.
     $lines = 1;
     $in_queue = 0;
     $queue = \Drupal::queue('eventninja');
     // Load queue
     while (!$read_file->eof()) {
         $data = $read_file->fgetcsv(';');
         if ($lines > 1) {
             // skip headers
             $user = user_load_by_mail($data[1]);
             if ($user === false) {
                 // Verify if user with specified email does not exist.
                 $queue->createItem($data);
                 $in_queue++;
             } else {
                 $this->logger('eventninja')->log(RfcLogLevel::NOTICE, 'User {mail} hasn\'t been created.', ['mail' => $data[1]]);
             }
         }
         $lines++;
     }
     if ($lines > 1) {
         drupal_set_message($this->t('@num records was scheduled for import', array('@num' => $in_queue)), 'success');
     } else {
         drupal_set_message($this->t('File contains only headers'), 'error');
     }
 }
Beispiel #6
0
 /**
  * Reads a single element from the source
  *    then return a Object instance
  *
  * @return \StdClass|false object instance
  */
 public function readElement()
 {
     $csv_line_data = $this->spl_file_object->fgetcsv();
     if ($csv_line_data) {
         $csv_line_data[0] = $this->convertToUtf8($csv_line_data);
         // questo controllo è per quando si esporta da excel
         if (!$csv_line_data[0]) {
             return false;
         }
         $csv_line = array_combine($this->columns_name, $csv_line_data);
         // we cast it to StdClass
         return (object) $csv_line;
     } else {
         return false;
     }
 }
 /**
  * Parses a file of data into a php array.
  *
  * @param \SplFileObject $file
  *
  * @return array
  */
 public function parse(\SplFileObject $file)
 {
     $delimiter = $this->options['delimiter'];
     $enclosure = $this->options['enclosure'];
     $escape = $this->options['escape'];
     $header = $file->fgetcsv($delimiter, $enclosure, $escape);
     $result = [];
     while (!$file->eof()) {
         $line = $file->fgetcsv($delimiter, $enclosure, $escape);
         // If row is not a blank line.
         if (!empty($line[0])) {
             $result[] = array_combine($header, $line);
         }
     }
     return $result;
 }
 /**
  * {@inheritdoc}
  */
 public function read()
 {
     if (null === $this->csv) {
         if (mime_content_type($this->filePath) === 'application/zip') {
             $this->extractZipArchive();
         }
         $this->csv = new \SplFileObject($this->filePath);
         $this->csv->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
         $this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
         $this->fieldNames = $this->csv->fgetcsv();
     }
     $data = $this->csv->fgetcsv();
     if (false !== $data) {
         if ($data === array(null) || $data === null) {
             return null;
         }
         if ($this->stepExecution) {
             $this->stepExecution->incrementSummaryInfo('read');
         }
         if (count($this->fieldNames) !== count($data)) {
             throw new InvalidItemException('pim_base_connector.steps.csv_reader.invalid_item_columns_count', $data, array('%totalColumnsCount%' => count($this->fieldNames), '%itemColumnsCount%' => count($data), '%csvPath%' => $this->csv->getRealPath(), '%lineno%' => $this->csv->key()));
         }
         $data = array_combine($this->fieldNames, $data);
     } else {
         throw new \RuntimeException('An error occured while reading the csv.');
     }
     return $data;
 }
 /**
  * Parse les pleins et remplit le tableau $pleins.
  *
  * @param \SplFileObject $file Fichier CSV fourni par MyCars.
  */
 public function parse(\SplFileObject $file)
 {
     // Repository
     $vehiculeRepository = $this->em->getRepository('ComptesBundle:Vehicule');
     // Configuration du handler
     $configuration = $this->configuration['mycars.csv'];
     // Tableau de correspondance entre le nom du véhicule dans MyCars et l'objet Vehicule
     $vehicules = array();
     foreach ($configuration['vehicules'] as $vehiculeLabel => $vehiculeID) {
         $vehicules[$vehiculeLabel] = $vehiculeRepository->find($vehiculeID);
     }
     // Lignes du fichier CSV qui représentent des pleins
     $refuels = array();
     // Les en-têtes de colonnes
     $headers = array();
     // Numéros de ligne
     $currentLine = 0;
     $headersLine = false;
     while (($cols = $file->fgetcsv()) !== null) {
         // Recherche de la ligne d'en-têtes
         if ($cols[0] == "#entity: refuel") {
             $headersLine = $currentLine + 1;
         }
         // Si la ligne d'en-têtes a été trouvée et qu'on l'a dépassée
         if ($headersLine !== false && $currentLine > $headersLine) {
             // La ligne en cours est un plein
             $refuel = array_combine($headers, $cols);
             $refuels[] = $refuel;
         } elseif ($currentLine == $headersLine) {
             $headers = $cols;
         }
         $currentLine++;
     }
     foreach ($refuels as $refuel) {
         $plein = new Plein();
         // Véhicule
         $vehiculeName = (string) $refuel['##car_name'];
         $vehicule = $vehicules[$vehiculeName];
         $plein->setVehicule($vehicule);
         // Date
         $date = \DateTime::createFromFormat('Y-m-d G:i', (string) $refuel['refuelDate']);
         $plein->setDate($date);
         // Distance parcourue
         $distanceParcourue = (string) $refuel['distance'];
         $plein->setDistanceParcourue($distanceParcourue);
         // Montant
         $montant = (string) $refuel['price'] * (string) $refuel['quantity'];
         $plein->setMontant($montant);
         // Prix au litre
         $prixLitre = (string) $refuel['price'];
         $plein->setPrixLitre($prixLitre);
         // Quantité
         $quantite = (string) $refuel['quantity'];
         $plein->setQuantite($quantite);
         // Classification
         $classification = $this->getClassification($plein);
         $this->classify($plein, $classification);
     }
 }
Beispiel #10
0
 public function __construct($csvFile)
 {
     $file = new SplFileObject($csvFile);
     while (!$file->eof()) {
         $csv[] = $file->fgetcsv();
     }
     $this->File = $csv;
 }
 /**
  * Initialize read process by extracting zip if needed, setting CSV options
  * and settings field names.
  */
 protected function initializeRead()
 {
     if (mime_content_type($this->filePath) === 'application/zip') {
         $this->extractZipArchive();
     }
     $this->csv = new \SplFileObject($this->filePath);
     $this->csv->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
     $this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
     $this->fieldNames = $this->csv->fgetcsv();
 }
 /**
  * Parses a file of data into a php array.
  *
  * @param \SplFileObject $file
  *
  * @return array
  */
 public function parse(\SplFileObject $file)
 {
     $result = [];
     $delimiter = $this->options['delimiter'];
     $enclosure = $this->options['enclosure'];
     $escape = $this->options['escape'];
     while (!$file->eof()) {
         $this->formatLine($result, $file->fgetcsv($delimiter, $enclosure, $escape));
     }
     return $result;
 }
 /**
  * Initialize read process by extracting zip if needed, setting CSV options
  * and settings field names.
  */
 protected function initializeRead()
 {
     // TODO mime_content_type is deprecated, use Symfony\Component\HttpFoundation\File\MimeTypeMimeTypeGuesser?
     if ('application/zip' === mime_content_type($this->filePath)) {
         $this->extractZipArchive();
     }
     $this->csv = new \SplFileObject($this->filePath);
     $this->csv->setFlags(\SplFileObject::READ_CSV | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY | \SplFileObject::DROP_NEW_LINE);
     $this->csv->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
     $this->fieldNames = $this->csv->fgetcsv();
 }
 /**
  * Parse les mouvements et remplit les tableaux de classification du handler.
  *
  * @param \SplFileObject $file Fichier CSV fourni par le CIC.
  */
 public function parse(\SplFileObject $file)
 {
     // Repository
     $compteRepository = $this->em->getRepository('ComptesBundle:Compte');
     // Configuration du handler
     $configuration = $this->configuration['cic.csv'];
     // Le compte bancaire dans lequel importer les mouvements
     $compteID = $configuration['compte'];
     $compte = $compteRepository->find($compteID);
     // Lignes du fichier CSV qui représentent des mouvements
     $rows = array();
     // Les en-têtes de colonnes
     $headers = array('date_operation', 'date_valeur', 'debit', 'credit', 'libelle', 'solde');
     // Numéros de ligne
     $currentLine = 0;
     $headersLine = 0;
     while (($cols = $file->fgetcsv(';')) !== null) {
         // Si on a dépassé la ligne d'en-têtes
         if ($currentLine > $headersLine) {
             // Si la date est valide et sans month shifting
             $date = \DateTime::createFromFormat('d/m/Y', $cols[0]);
             $isValidDate = $date !== false && !array_sum($date->getLastErrors());
             // Alors la ligne en cours est un mouvement
             if ($isValidDate) {
                 $row = array_combine($headers, $cols);
                 $rows[] = $row;
             }
         }
         $currentLine++;
     }
     foreach ($rows as $row) {
         $mouvement = new Mouvement();
         // Date
         $date = \DateTime::createFromFormat('d/m/Y', (string) $row['date_operation']);
         $mouvement->setDate($date);
         // Compte
         $mouvement->setCompte($compte);
         // Montant
         $montant = $row['debit'] !== '' ? $row['debit'] : $row['credit'];
         $montant = str_replace(',', '.', $montant);
         $montant = sprintf('%0.2f', $montant);
         $mouvement->setMontant($montant);
         // Description
         $description = $row['libelle'];
         $mouvement->setDescription($description);
         // Classification
         $classification = $this->getClassification($mouvement);
         $this->classify($mouvement, $classification);
     }
 }
Beispiel #15
0
 /**
  * @param \Closure $callback
  */
 protected function parseFile(\Closure $callback)
 {
     while (!$this->file->eof()) {
         if (strlen($this->delim) == 1) {
             $data = $this->file->fgetcsv($this->delim, $this->enclosure);
         } else {
             $data = explode($this->delim, $this->file->fgets());
             $data = array_map(function ($row) {
                 return mb_convert_encoding(trim($row, $this->enclosure), "UTF-8", "Windows-1252,ISO-8859-15");
             }, $data);
             if ($this->debug) {
                 break;
             }
             /*
              $enclosure = $this->enclosure;
              array_walk($data, function(&$val) use ($enclosure) {
                 return trim($val, $enclosure);
             });
             */
         }
         $callback($data);
     }
 }
 /**
  * @test
  */
 public function dump()
 {
     $root = vfsStream::setup('root');
     vfsStream::newFile('directives.csv')->at($root);
     $dumper = new FileDumper(vfsStream::url('root/directives.csv'));
     $values = [['add', 'file1', 'file1'], ['add', 'file2', 'file2']];
     $dumper->dump($values);
     $reader = new \SplFileObject(vfsStream::url('root/directives.csv'));
     $actual = [];
     while ($reader->eof() === false) {
         $actual[] = $reader->fgetcsv();
     }
     $this->assertEquals($values, $actual);
 }
Beispiel #17
0
 /**
  *
  * @return array
  */
 private function all($resource)
 {
     try {
         $file = new \SplFileObject($resource, 'rb');
     } catch (\RuntimeException $e) {
         throw new \InvalidArgumentException(sprintf('Error opening file "%s".', $resource));
     }
     $file->setFlags(\SplFileObject::SKIP_EMPTY | \SplFileObject::READ_CSV);
     $file->setCsvControl(';');
     $lines = array();
     // iterate over the file's rows
     // fgets increments file descriptor to next line
     while ($data = $file->fgetcsv()) {
         $lines[] = $data;
     }
     return $lines;
 }
function processCsv($file, $omitTitles = true)
{
    $csv = new SplFileObject($file);
    $records = 0;
    if ($omitTitles) {
        // Get the first line but do nothing with it
        $csv->getCurrentLine();
    }
    while (!$csv->eof()) {
        // Get the current line
        $line = $csv->fgetcsv();
        // Skip blank lines that return a single null element
        if (count($line) > 1 && !is_null($line[0])) {
            (yield $line);
            $records++;
        }
    }
    return "{$records} records processed from {$file}";
}
Beispiel #19
0
 function __construct(\Tracker_Definition $definition, $fileName)
 {
     $file = new \SplFileObject($fileName, 'r');
     $headers = $file->fgetcsv();
     $schema = new Schema($definition);
     foreach ($headers as $header) {
         if (preg_match(Schema\Column::HEADER_PATTERN, $header, $parts)) {
             list($full, $pk, $field, $mode) = $parts;
             $schema->addColumn($field, $mode);
             if ($pk === '*') {
                 $schema->setPrimaryKey($field);
             }
         } else {
             // Column without definition, add fake entry to skip column
             $schema->addNew('ignore', 'ignore')->setReadOnly(true);
         }
     }
     $this->source = new CsvSource($schema, $fileName);
 }
Beispiel #20
0
 /**
  * Loads the record/model data from the persistent storage file.
  *
  * NOTE: THE FIRST ROW OF THE FILE MUST CONTAIN COLUMN NAMES MATCHING THE
  * LIST OF COLUMNS ON THE MODEL ASSOCIATED WITH THIS CLASS.
  *
  * @return void
  */
 protected function loadFromFile()
 {
     $columns = [];
     while (!$this->file->eof()) {
         $row = (array) $this->file->fgetcsv();
         if (empty($columns)) {
             $columns = $row;
             continue;
         }
         $row = array_filter($row);
         if (empty($row)) {
             continue;
         }
         $attributes = array_fill_keys($columns, null);
         foreach ($row as $index => $value) {
             $attributes[$columns[$index]] = $value;
         }
         $model = $this->model->newInstance($attributes, true);
         $this->records[$model->id] = $model;
     }
 }
 /**
  * @test
  */
 public function testExecuteFlowWithIgnores()
 {
     $app = new Application();
     $app->add(new GenerateCreateCommand());
     $root = vfsStream::setup('root');
     vfsStream::newFile('directives.csv')->at($root);
     $testDir = vfsStream::newDirectory('test')->at($root);
     vfsStream::newFile('testfile1')->at($testDir);
     vfsStream::newFile('testfile2')->at($testDir);
     vfsStream::newFile('testfile3')->at($testDir);
     $command = $app->find('diff:create');
     $tester = new CommandTester($command);
     $tester->execute(array('command' => $command->getName(), 'folder' => vfsStream::url('root/test'), 'directives' => vfsStream::url('root/directives.csv'), 'ignores' => 'testfile1,testfile2'));
     $this->assertEquals(0, $tester->getStatusCode());
     $this->assertContains('Directives are dumped successfully!', $tester->getDisplay());
     $reader = new \SplFileObject(vfsStream::url('root/directives.csv'));
     $actual = [];
     while ($reader->eof() === false) {
         $actual[] = $reader->fgetcsv();
     }
     $this->assertEquals([['add', 'testfile3', 'testfile3']], $actual);
 }
<?php

$file = new SplFileObject('data.csv');
while ($array = $file->fgetcsv()) {
    var_export($array);
}
exit;
// Кастомизируем чтение csv файла
class CSVFileObject extends SPLFileInfo implements Iterator, SeekableIterator
{
    protected $map, $fp, $currentLine;
    public function __construct($filename, $mode = 'r', $inc = false, $ctx = null)
    {
        parent::__construct($filename);
        if (isset($context)) {
            $this->fp = fopen($filename, $mode, $inc, $ctx);
        } else {
            $this->fp = fopen($filename, $mode, $inc);
        }
        if (!$this->fp) {
            throw new Exception("Не могу прочитать файл!");
        }
        //
        $this->map = $this->fgetcsv();
        $this->currentLine = 0;
    }
    function fgetcsv($delimiter = ',', $enclosure = '"')
    {
        return fgetcsv($this->fp, 0, $delimiter, $enclosure);
    }
    function key()
 function getCurrentLine()
 {
     echo __METHOD__ . "\n";
     return parent::fgetcsv(',', '"');
 }
<?php

$fp = fopen('SplFileObject_fgetcsv_basic.csv', 'w+');
fputcsv($fp, array('field1', 'field2', 'field3', 5));
fclose($fp);
$fo = new SplFileObject('SplFileObject_fgetcsv_basic.csv');
var_dump($fo->fgetcsv());
unlink('SplFileObject_fgetcsv_basic.csv');
<?php

$fp = fopen('SplFileObject__fgetcsv4.csv', 'w+');
fputcsv($fp, array('field1', 'field2', 'field3', 5), ',', '"');
fclose($fp);
$fo = new SplFileObject('SplFileObject__fgetcsv4.csv');
var_dump($fo->fgetcsv(',', '"'));
function acui_import_users($file, $form_data, $attach_id = 0, $is_cron = false)
{
    ?>
	<div class="wrap">
		<h2>Importing users</h2>	
		<?php 
    set_time_limit(0);
    add_filter('send_password_change_email', '__return_false');
    global $wpdb;
    global $wp_users_fields;
    global $wp_min_fields;
    if (is_plugin_active('wp-access-areas/wp-access-areas.php')) {
        $wpaa_labels = WPAA_AccessArea::get_available_userlabels();
    }
    $buddypress_fields = array();
    if (is_plugin_active('buddypress/bp-loader.php')) {
        $profile_groups = BP_XProfile_Group::get(array('fetch_fields' => true));
        if (!empty($profile_groups)) {
            foreach ($profile_groups as $profile_group) {
                if (!empty($profile_group->fields)) {
                    foreach ($profile_group->fields as $field) {
                        $buddypress_fields[] = $field->name;
                    }
                }
            }
        }
    }
    $users_registered = array();
    $headers = array();
    $headers_filtered = array();
    $role = $form_data["role"];
    $empty_cell_action = $form_data["empty_cell_action"];
    if (empty($form_data["activate_users_wp_members"])) {
        $activate_users_wp_members = "no_activate";
    } else {
        $activate_users_wp_members = $form_data["activate_users_wp_members"];
    }
    if (empty($form_data["allow_multiple_accounts"])) {
        $allow_multiple_accounts = "not_allowed";
    } else {
        $allow_multiple_accounts = $form_data["allow_multiple_accounts"];
    }
    echo "<h3>Ready to registers</h3>";
    echo "<p>First row represents the form of sheet</p>";
    $row = 0;
    $positions = array();
    ini_set('auto_detect_line_endings', TRUE);
    $delimiter = acui_detect_delimiter($file);
    $manager = new SplFileObject($file);
    while ($data = $manager->fgetcsv($delimiter)) {
        if (empty($data[0])) {
            continue;
        }
        if (count($data) == 1) {
            $data = $data[0];
        }
        foreach ($data as $key => $value) {
            $data[$key] = trim($value);
        }
        for ($i = 0; $i < count($data); $i++) {
            $data[$i] = acui_string_conversion($data[$i]);
        }
        if ($row == 0) {
            // check min columns username - email
            if (count($data) < 2) {
                echo "<div id='message' class='error'>File must contain at least 2 columns: username and email</div>";
                break;
            }
            $i = 0;
            $password_position = false;
            foreach ($wp_users_fields as $wp_users_field) {
                $positions[$wp_users_field] = false;
            }
            foreach ($data as $element) {
                $headers[] = $element;
                if (in_array(strtolower($element), $wp_users_fields)) {
                    $positions[strtolower($element)] = $i;
                }
                if (!in_array(strtolower($element), $wp_users_fields) && !in_array($element, $wp_min_fields) && !in_array($element, $buddypress_fields)) {
                    $headers_filtered[] = $element;
                }
                $i++;
            }
            $columns = count($data);
            update_option("acui_columns", $headers_filtered);
            ?>
					<h3>Inserting and updating data</h3>
					<table>
						<tr><th>Row</th><?php 
            foreach ($headers as $element) {
                echo "<th>" . $element . "</th>";
            }
            ?>
</tr>
					<?php 
            $row++;
        } else {
            if (count($data) != $columns) {
                // if number of columns is not the same that columns in header
                echo '<script>alert("Row number: ' . $row . ' has no the same columns than header, we are going to skip");</script>';
                continue;
            }
            $username = $data[0];
            $email = $data[1];
            $user_id = 0;
            $problematic_row = false;
            $password_position = $positions["password"];
            $password = "";
            $created = true;
            if ($password_position === false) {
                $password = wp_generate_password();
            } else {
                $password = $data[$password_position];
            }
            if (username_exists($username)) {
                // if user exists, we take his ID by login, we will update his mail if it has changed
                $user_object = get_user_by("login", $username);
                $user_id = $user_object->ID;
                if (!empty($password)) {
                    wp_set_password($password, $user_id);
                }
                $updateEmailArgs = array('ID' => $user_id, 'user_email' => $email);
                wp_update_user($updateEmailArgs);
                $created = false;
            } elseif (email_exists($email) && $allow_multiple_accounts == "not_allowed") {
                // if the email is registered, we take the user from this and we don't allow repeated emails
                $user_object = get_user_by("email", $email);
                $user_id = $user_object->ID;
                $data[0] = "User already exists as: " . $user_object->user_login . "<br/>(in this CSV file is called: " . $username . ")";
                $problematic_row = true;
                if (!empty($password)) {
                    wp_set_password($password, $user_id);
                }
                $created = false;
            } elseif (email_exists($email) && $allow_multiple_accounts == "allowed") {
                // if the email is registered and repeated emails are allowed
                if (empty($password)) {
                    // if user not exist and password is empty but the column is set, it will be generated
                    $password = wp_generate_password();
                }
                $hacked_email = acui_hack_email($email);
                $user_id = wp_create_user($username, $password, $hacked_email);
                acui_hack_restore_remapped_email_address($user_id, $email);
            } else {
                if (empty($password)) {
                    // if user not exist and password is empty but the column is set, it will be generated
                    $password = wp_generate_password();
                }
                $user_id = wp_create_user($username, $password, $email);
            }
            if (is_wp_error($user_id)) {
                // in case the user is generating errors after this checks
                $error_string = $user_id->get_error_message();
                echo '<script>alert("Problems with user: '******', we are going to skip. \\r\\nError: ' . $error_string . '");</script>';
                continue;
            }
            $users_registered[] = $user_id;
            $user_object = new WP_User($user_id);
            if (!(in_array("administrator", acui_get_roles($user_id), FALSE) || is_multisite() && is_super_admin($user_id))) {
                $default_roles = $user_object->roles;
                foreach ($default_roles as $default_role) {
                    $user_object->remove_role($default_role);
                }
                if (is_array($role)) {
                    foreach ($role as $single_role) {
                        $user_object->add_role($single_role);
                    }
                } else {
                    $user_object->add_role($role);
                }
            }
            // WP Members activation
            if ($activate_users_wp_members == "activate") {
                update_user_meta($user_id, "active", true);
            }
            if ($columns > 2) {
                for ($i = 2; $i < $columns; $i++) {
                    if (!empty($data)) {
                        if (strtolower($headers[$i]) == "password") {
                            // passwords -> continue
                            continue;
                        } else {
                            if (in_array($headers[$i], $wp_users_fields)) {
                                // wp_user data
                                if (empty($data[$i]) && $empty_cell_action == "leave") {
                                    continue;
                                } else {
                                    wp_update_user(array('ID' => $user_id, $headers[$i] => $data[$i]));
                                }
                            } elseif (strtolower($headers[$i]) == "wp-access-areas" && is_plugin_active('wp-access-areas/wp-access-areas.php')) {
                                // wp-access-areas
                                $active_labels = array_map('trim', explode("#", $data[$i]));
                                foreach ($wpaa_labels as $wpa_label) {
                                    if (in_array($wpa_label->cap_title, $active_labels)) {
                                        acui_set_cap_for_user($wpa_label->capability, $user_object, true);
                                    } else {
                                        acui_set_cap_for_user($wpa_label->capability, $user_object, false);
                                    }
                                }
                            } elseif (in_array($headers[$i], $buddypress_fields)) {
                                // buddypress
                                xprofile_set_field_data($headers[$i], $user_id, $data[$i]);
                            } else {
                                // wp_usermeta data
                                if (empty($data[$i])) {
                                    if ($empty_cell_action == "delete") {
                                        delete_user_meta($user_id, $headers[$i]);
                                    } else {
                                        continue;
                                    }
                                } else {
                                    update_user_meta($user_id, $headers[$i], $data[$i]);
                                }
                            }
                        }
                    }
                }
            }
            $styles = "";
            if ($problematic_row) {
                $styles = "background-color:red; color:white;";
            }
            echo "<tr style='{$styles}' ><td>" . ($row - 1) . "</td>";
            foreach ($data as $element) {
                echo "<td>{$element}</td>";
            }
            echo "</tr>\n";
            flush();
            $mail_for_this_user = false;
            if ($created) {
                $mail_for_this_user = true;
            } else {
                if (!$is_cron && isset($form_data["send_email_updated"]) && $form_data["send_email_updated"]) {
                    $mail_for_this_user = true;
                } else {
                    if ($is_cron && get_option("acui_send_mail_cron")) {
                        $mail_for_this_user = true;
                    }
                }
            }
            // send mail
            if (isset($form_data["sends_email"]) && $form_data["sends_email"] && $mail_for_this_user) {
                $body_mail = get_option("acui_mail_body");
                $subject = get_option("acui_mail_subject");
                $body_mail = str_replace("**loginurl**", "<a href='" . home_url() . "/wp-login.php" . "'>" . home_url() . "/wp-login.php" . "</a>", $body_mail);
                $body_mail = str_replace("**username**", $username, $body_mail);
                if (empty($password) && !$created) {
                    $password = "******";
                }
                $body_mail = str_replace("**password**", $password, $body_mail);
                $body_mail = str_replace("**email**", $email, $body_mail);
                foreach ($wp_users_fields as $wp_users_field) {
                    if ($positions[$wp_users_field] != false && $wp_users_field != "password") {
                        $body_mail = str_replace("**" . $wp_users_field . "**", $data[$positions[$wp_users_field]], $body_mail);
                    }
                }
                for ($i = 0; $i < count($headers); $i++) {
                    $body_mail = str_replace("**" . $headers[$i] . "**", $data[$i], $body_mail);
                }
                add_filter('wp_mail_content_type', 'set_html_content_type');
                if (get_option("acui_settings") == "plugin") {
                    add_action('phpmailer_init', 'acui_mailer_init');
                    add_filter('wp_mail_from', 'acui_mail_from');
                    add_filter('wp_mail_from_name', 'acui_mail_from_name');
                    wp_mail($email, $subject, $body_mail);
                    remove_filter('wp_mail_from', 'acui_mail_from');
                    remove_filter('wp_mail_from_name', 'acui_mail_from_name');
                    remove_action('phpmailer_init', 'acui_mailer_init');
                } else {
                    wp_mail($email, $subject, $body_mail);
                }
                remove_filter('wp_mail_content_type', 'set_html_content_type');
            }
        }
        $row++;
    }
    if ($attach_id != 0) {
        wp_delete_attachment($attach_id);
    }
    // delete all users that have not been imported
    if ($is_cron && get_option("acui_cron_delete_users")) {
        $all_users = get_users(array('fields' => array('ID')));
        foreach ($all_users as $user) {
            if (!in_array($user->ID, $users_registered)) {
                wp_delete_user($user->ID);
            }
        }
    }
    ?>
			</table>
			<br/>
			<p>Process finished you can go <a href="<?php 
    echo get_admin_url() . '/users.php';
    ?>
">here to see results</a></p>
			<?php 
    ini_set('auto_detect_line_endings', FALSE);
    add_filter('send_password_change_email', '__return_true');
    ?>
	</div>
<?php 
}
    Capsule::schema()->create('gamertags', function ($table) {
        $table->increments('id');
        $table->string('gamertag', 255)->index()->unique();
        $table->string('xuid', 255)->nullable()->index()->unique();
        $table->text('error')->nullable();
        $table->timestamps();
    });
}
// Setup our relationship models
class Gamertag extends Model
{
    protected $table = 'gamertags';
    protected $hidden = [];
    protected $fillable = ['gamertag', 'xuid', 'error'];
}
if ($createSchema === true) {
    $dir = realpath(__DIR__ . '/../');
    $file = "{$dir}/" . GAMERTAGS_CSV;
    try {
        $csv = new SplFileObject($file, 'r');
    } catch (RuntimeException $e) {
        printf("Error opening .csv: %s\n", $e->getMessage());
        exit;
    }
    while (!$csv->eof() && ($row = $csv->fgetcsv()) && $row[0] !== null) {
        print "Importing gamertag: {$row[0]}\n";
        Gamertag::create(['gamertag' => $row[0]]);
    }
    $count = Gamertag::all()->count();
    print "\nImported {$count} gamertags\n\n";
}
<?php

$fp = fopen('SplFileObject__fgetcsv8.csv', 'w+');
fwrite($fp, '"aaa","b""bb","ccc"');
fclose($fp);
$fo = new SplFileObject('SplFileObject__fgetcsv8.csv');
var_dump($fo->fgetcsv(',', '"', 'invalid'));
Beispiel #29
0
 /**
  * @param $objectId
  */
 public function importAction($objectId = 0, $ignorePost = false)
 {
     //Auto detect line endings for the file to work around MS DOS vs Unix new line characters
     ini_set('auto_detect_line_endings', true);
     /** @var \Mautic\LeadBundle\Model\LeadModel $model */
     $model = $this->factory->getModel('lead');
     $session = $this->factory->getSession();
     if (!$this->factory->getSecurity()->isGranted('lead:leads:create')) {
         return $this->accessDenied();
     }
     // Move the file to cache and rename it
     $forceStop = $this->request->get('cancel', false);
     $step = $forceStop ? 1 : $session->get('mautic.lead.import.step', 1);
     $cacheDir = $this->factory->getSystemPath('cache', true);
     $username = $this->factory->getUser()->getUsername();
     $fileName = $username . '_leadimport.csv';
     $fullPath = $cacheDir . '/' . $fileName;
     $complete = false;
     if (!file_exists($fullPath)) {
         // Force step one if the file doesn't exist
         $step = 1;
         $session->set('mautic.lead.import.step', 1);
     }
     $progress = $session->get('mautic.lead.import.progress', array(0, 0));
     $stats = $session->get('mautic.lead.import.stats', array('merged' => 0, 'created' => 0, 'ignored' => 0));
     $action = $this->generateUrl('mautic_lead_action', array('objectAction' => 'import'));
     switch ($step) {
         case 1:
             // Upload file
             if ($forceStop) {
                 $this->resetImport($fullPath);
             }
             $session->set('mautic.lead.import.headers', array());
             $form = $this->get('form.factory')->create('lead_import', array(), array('action' => $action));
             break;
         case 2:
             // Match fields
             /** @var \Mautic\LeadBundle\Model\FieldModel $addonModel */
             $fieldModel = $this->factory->getModel('lead.field');
             $leadFields = $fieldModel->getFieldList(false, false);
             $importFields = $session->get('mautic.lead.import.importfields', array());
             $form = $this->get('form.factory')->create('lead_field_import', array(), array('action' => $action, 'lead_fields' => $leadFields, 'import_fields' => $importFields));
             break;
         case 3:
             // Just show the progress form
             $session->set('mautic.lead.import.step', 4);
             break;
         case 4:
             ignore_user_abort(true);
             $inProgress = $session->get('mautic.lead.import.inprogress', false);
             $checks = $session->get('mautic.lead.import.progresschecks', 1);
             if (!$inProgress || $checks > 5) {
                 $session->set('mautic.lead.import.inprogress', true);
                 $session->set('mautic.lead.import.progresschecks', 1);
                 // Batch process
                 $defaultOwner = $session->get('mautic.lead.import.defaultowner', null);
                 $defaultList = $session->get('mautic.lead.import.defaultlist', null);
                 $headers = $session->get('mautic.lead.import.headers', array());
                 $importFields = $session->get('mautic.lead.import.fields', array());
                 $file = new \SplFileObject($fullPath);
                 if ($file !== false) {
                     $lineNumber = $progress[0];
                     if ($lineNumber > 0) {
                         $file->seek($lineNumber);
                     }
                     $config = $session->get('mautic.lead.import.config');
                     $batchSize = $config['batchlimit'];
                     while ($batchSize && !$file->eof()) {
                         $data = $file->fgetcsv($config['delimiter'], $config['enclosure'], $config['escape']);
                         if ($lineNumber === 0) {
                             $lineNumber++;
                             continue;
                         }
                         // Increase progress count
                         $progress[0]++;
                         // Decrease batch count
                         $batchSize--;
                         if (is_array($data) && count($headers) === count($data)) {
                             $data = array_combine($headers, $data);
                             if (empty($data)) {
                                 $stats['ignored']++;
                             } else {
                                 $merged = $model->importLead($importFields, $data, $defaultOwner, $defaultList);
                                 if ($merged) {
                                     $stats['merged']++;
                                 } else {
                                     $stats['created']++;
                                 }
                             }
                         }
                     }
                     $session->set('mautic.lead.import.stats', $stats);
                 }
                 // Close the file
                 $file = null;
                 // Clear in progress
                 if ($progress[0] >= $progress[1]) {
                     $progress[0] = $progress[1];
                     $this->resetImport($fullPath);
                     $complete = true;
                 } else {
                     $complete = false;
                     $session->set('mautic.lead.import.inprogress', false);
                     $session->set('mautic.lead.import.progress', $progress);
                 }
                 break;
             } else {
                 $checks++;
                 $session->set('mautic.lead.import.progresschecks', $checks);
             }
     }
     ///Check for a submitted form and process it
     if (!$ignorePost && $this->request->getMethod() == 'POST') {
         if (isset($form) && !($cancelled = $this->isFormCancelled($form))) {
             $valid = $this->isFormValid($form);
             switch ($step) {
                 case 1:
                     if ($valid) {
                         if (file_exists($fullPath)) {
                             unlink($fullPath);
                         }
                         $fileData = $form['file']->getData();
                         if (!empty($fileData)) {
                             try {
                                 $fileData->move($cacheDir, $fileName);
                                 $file = new \SplFileObject($fullPath);
                                 $config = $form->getData();
                                 unset($config['file']);
                                 unset($config['start']);
                                 foreach ($config as $key => &$c) {
                                     $c = htmlspecialchars_decode($c);
                                     if ($key == 'batchlimit') {
                                         $c = (int) $c;
                                     }
                                 }
                                 $session->set('mautic.lead.import.config', $config);
                                 if ($file !== false) {
                                     // Get the headers for matching
                                     $headers = $file->fgetcsv($config['delimiter'], $config['enclosure'], $config['escape']);
                                     // Get the number of lines so we can track progress
                                     $file->seek(PHP_INT_MAX);
                                     $linecount = $file->key();
                                     if (!empty($headers) && is_array($headers)) {
                                         $session->set('mautic.lead.import.headers', $headers);
                                         sort($headers);
                                         $headers = array_combine($headers, $headers);
                                         $session->set('mautic.lead.import.step', 2);
                                         $session->set('mautic.lead.import.importfields', $headers);
                                         $session->set('mautic.lead.import.progress', array(0, $linecount));
                                         return $this->importAction(0, true);
                                     }
                                 }
                             } catch (\Exception $e) {
                             }
                         }
                         $form->addError(new FormError($this->factory->getTranslator()->trans('mautic.lead.import.filenotreadable', array(), 'validators')));
                     }
                     break;
                 case 2:
                     // Save matched fields
                     $matchedFields = $form->getData();
                     if (empty($matchedFields)) {
                         $this->resetImport($fullPath);
                         return $this->importAction(0, true);
                     }
                     $owner = $matchedFields['owner'];
                     unset($matchedFields['owner']);
                     $list = $matchedFields['list'];
                     unset($matchedFields['list']);
                     foreach ($matchedFields as $k => $f) {
                         if (empty($f)) {
                             unset($matchedFields[$k]);
                         }
                     }
                     if (empty($matchedFields)) {
                         $form->addError(new FormError($this->factory->getTranslator()->trans('mautic.lead.import.matchfields', array(), 'validators')));
                     } else {
                         $defaultOwner = $owner ? $owner->getId() : null;
                         $session->set('mautic.lead.import.fields', $matchedFields);
                         $session->set('mautic.lead.import.defaultowner', $defaultOwner);
                         $session->set('mautic.lead.import.defaultlist', $list);
                         $session->set('mautic.lead.import.step', 3);
                         return $this->importAction(0, true);
                     }
                     break;
                 default:
                     // Done or something wrong
                     $this->resetImport($fullPath);
                     break;
             }
         } else {
             $this->resetImport($fullPath);
             return $this->importAction(0, true);
         }
     }
     if ($step === 1 || $step === 2) {
         $contentTemplate = 'MauticLeadBundle:Import:form.html.php';
         $viewParameters = array('form' => $form->createView());
     } else {
         $contentTemplate = 'MauticLeadBundle:Import:progress.html.php';
         $viewParameters = array('progress' => $progress, 'stats' => $stats, 'complete' => $complete);
     }
     if (!$complete && $this->request->query->has('importbatch')) {
         // Ajax request to batch process so just return ajax response unless complete
         return new JsonResponse(array('success' => 1, 'ignore_wdt' => 1));
     } else {
         return $this->delegateView(array('viewParameters' => $viewParameters, 'contentTemplate' => $contentTemplate, 'passthroughVars' => array('activeLink' => '#mautic_lead_index', 'mauticContent' => 'leadImport', 'route' => $this->generateUrl('mautic_lead_action', array('objectAction' => 'import')), 'step' => $step, 'progress' => $progress)));
     }
 }
Beispiel #30
0
 /**
  * @param int $limit
  *
  * @return Task[]
  *
  * @throws \RuntimeException
  */
 protected function fetchTasks($limit)
 {
     $logger = Logger::getLogger('file-queue');
     $statusFileObject = $this->getStatusFileObject();
     if (!$statusFileObject->flock(LOCK_EX)) {
         throw new \RuntimeException("Can't get lock for status file {$this->getStatusFile()}");
     }
     $seek = $statusFileObject->fgets();
     if ($seek === false) {
         throw new \RuntimeException("Can't get seek from status file {$this->getStatusFile()}");
     }
     if (!$seek) {
         $seek = 0;
     }
     $seek = (int) $seek;
     $tasksFileObject = new \SplFileObject($this->getTasksFile(), 'r');
     if ($tasksFileObject->fseek($seek) !== 0) {
         throw new \RuntimeException("Can't seek to {$seek} in tasks file {$this->getTasksFile()}");
     }
     $logger->debug("Current seek {$seek}");
     $tasks = [];
     $taskNumber = 0;
     while ($taskNumber < $limit && !$tasksFileObject->eof()) {
         $this->getProcessor()->getSignalHandler()->dispatch();
         $row = $tasksFileObject->fgetcsv();
         if ($row === false) {
             throw new \RuntimeException("Can't get row from file {$this->getTasksFile()}");
         }
         // пропускаем пустые строки
         if (!$row || $row === [null]) {
             continue;
         }
         $seek = $tasksFileObject->ftell();
         if ($seek === false) {
             throw new \RuntimeException("Can't get seek from file {$this->getTasksFile()}");
         }
         if (count($row) < 2) {
             $eventId = $row[0];
             $data = null;
         } else {
             list($eventId, $data) = $row;
         }
         $tasks[] = new Task($eventId, $data);
         ++$taskNumber;
     }
     if ($statusFileObject->fseek(0) !== 0) {
         throw new \RuntimeException("Can't seek to 0 in status file {$this->getStatusFile()}");
     }
     if (!$statusFileObject->ftruncate(0)) {
         throw new \RuntimeException("Can't truncate status file {$this->getStatusFile()}");
     }
     if (!$statusFileObject->fwrite($seek)) {
         throw new \RuntimeException("Can't write new seek {$seek} to status file {$this->getStatusFile()}");
     }
     $logger->debug("New seek {$seek}");
     if (!$statusFileObject->flock(LOCK_UN)) {
         $logger->warn("Can't release lock");
     }
     return $tasks;
 }