/** * Get Code from file * @param $path * @param $line * @param $numLines * @return array|null */ private function getCode($path, $line, $numLines) { if (empty($path) || empty($line) || !file_exists($path)) { return NULL; } try { // Get the number of lines in the file $file = new \SplFileObject($path); $file->seek(PHP_INT_MAX); $totalLines = $file->key() + 1; // Work out which lines we should fetch $start = max($line - floor($numLines / 2), 1); $end = $start + ($numLines - 1); if ($end > $totalLines) { $end = $totalLines; $start = max($end - ($numLines - 1), 1); } // Get the code for this range $code = array(); $file->seek($start - 1); while ($file->key() < $end) { $code[$file->key() + 1] = rtrim($file->current()); $file->next(); } return $code; } catch (RuntimeException $ex) { return null; } }
public function actionIndex() { Yii::import("application.models.*"); //get the latest idle cron job /* @var $latestidle JobQueue*/ $latestidle = JobQueue::model()->findByAttributes(array("status" => JobQueue::$JOBQUEUE_STATUS_IDLE)); if (!$latestidle) { echo "No file queued"; die; } //set status to on-going $latestidle->status = JobQueue::$JOBQUEUE_STATUS_ON_GOING; $latestidle->save(false); //retrieve file $queueFile = new SplFileObject($latestidle->filename); //read file $queueFile->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY); $queueFile->next(); // Total_records , $queueFile->seek(PHP_INT_MAX); $linesTotal = $queueFile->key(); $latestidle->total_records = $linesTotal; $latestidle->save(false); $index = 0; foreach ($queueFile as $currentLine) { //iterate content if ($queueFile->key() === 0) { continue; } //TODO: processed_record $latestidle->processed_record = ++$index; $latestidle->save(false); $currentMobile = $currentLine[0]; $newBlackListedmobile = new BlackListedMobile(); //cleaning time $currentMobile = trim($currentMobile); $currentMobile = rtrim($currentMobile); $currentMobile = ltrim($currentMobile); $newBlackListedmobile->mobile_number = $currentMobile; $newBlackListedmobile->queue_id = $latestidle->queue_id; //set queueid if ($newBlackListedmobile->save()) { //save content echo "{$newBlackListedmobile->mobile_number} : Saved \n"; } else { echo "{$newBlackListedmobile->mobile_number} : Failed \n"; } } //when done //set status to done $latestidle->status = JobQueue::$JOBQUEUE_STATUS_DONE; $latestidle->date_done = date("Y-m-d H:i:s"); //set done datetime to now() $latestidle->save(); echo "Queue DONE \n"; }
/** * {@inheritdoc} */ public function countLines(\SplFileObject $file) { // Refresh file size clearstatcache($file->getFilename()); $previous = $file->key(); $file->seek($file->getSize()); $count = $file->key(); $file->seek($previous); return $count; }
function test($name) { echo "==={$name}===\n"; $o = new SplFileObject(dirname(__FILE__) . '/' . $name); var_dump($o->key()); while (($c = $o->fgetc()) !== false) { var_dump($o->key(), $c, $o->eof()); } echo "===EOF?===\n"; var_dump($o->eof()); var_dump($o->key()); var_dump($o->eof()); }
protected function export($var, $return = false) { if ($var instanceof Closure) { /* dump anonymous function in to plain code.*/ $ref = new ReflectionFunction($var); $file = new SplFileObject($ref->getFileName()); $file->seek($ref->getStartLine() - 1); $result = ''; while ($file->key() < $ref->getEndLine()) { $result .= $file->current(); $file->next(); } $begin = strpos($result, 'function'); $end = strrpos($result, '}'); $result = substr($result, $begin, $end - $begin + 1); } elseif (is_object($var)) { /* dump object with construct function. */ $result = 'new ' . get_class($var) . '(' . $this->export(get_object_vars($var), true) . ')'; } elseif (is_array($var)) { /* dump array in plain array.*/ $array = array(); foreach ($var as $k => $v) { $array[] = var_export($k, true) . ' => ' . $this->export($v, true); } $result = 'array(' . implode(', ', $array) . ')'; } else { $result = var_export($var, true); } if (!$return) { print $result; } return $result; }
/** * @param \ReflectionFunctionAbstract $reflection */ public function setBodyFromReflection(\ReflectionFunctionAbstract $reflection) { /** @var $reflection \ReflectionMethod */ if (is_a($reflection, '\\ReflectionMethod') && $reflection->isAbstract()) { $this->_code = null; return; } $file = new \SplFileObject($reflection->getFileName()); $file->seek($reflection->getStartLine() - 1); $code = ''; while ($file->key() < $reflection->getEndLine()) { $code .= $file->current(); $file->next(); } $begin = strpos($code, 'function'); $code = substr($code, $begin); $begin = strpos($code, '{'); $end = strrpos($code, '}'); $code = substr($code, $begin + 1, $end - $begin - 1); $code = preg_replace('/^\\s*[\\r\\n]+/', '', $code); $code = preg_replace('/[\\r\\n]+\\s*$/', '', $code); if (!trim($code)) { $code = null; } $this->setCode($code); }
/** * {@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; }
/** * Evaluates the closure and the code in the reset of the file. This sets * the code for the closure, the namespace it is declared in (if any) and * any applicable namespace imports * @param ReflectionFunction The reflected function of the closure * @return String The code the closure runs */ private function evaluate(\ReflectionFunction $reflection) { $code = ''; $full = ''; $file = new \SplFileObject($reflection->getFileName()); while (!$file->eof()) { if ($file->key() >= $reflection->getStartLine() - 1 && $file->key() < $reflection->getEndLine()) { $code .= $file->current(); } $full .= $file->current(); $file->next(); } //@todo this assumes the function will be the only one on that line $begin = strpos($code, 'function'); //@todo this assumes the } will be the only one on that line $end = strrpos($code, '}'); $this->code = substr($code, $begin, $end - $begin + 1); $this->extractDetail($full); }
public function current() { if ($this->_columns) { if (false === ($combined = @array_combine($this->_columns, parent::current()))) { throw new Exception(sprintf("Column count did not match expected on line %d", parent::key())); } return $combined; } return parent::current(); }
public static function setUpBeforeClass() { $reflection = new ReflectionClass(__NAMESPACE__ . '\\TestClass'); self::$startLine = $reflection->getStartLine(); self::$endLine = $reflection->getEndLine(); self::$testClassCode = NULL; /* GetCode as String */ $file = new \SplFileObject($reflection->getFileName()); $file->seek(self::$startLine - 1); while ($file->key() < $reflection->getEndLine()) { self::$testClassCode .= $file->current(); $file->next(); } self::$testClassCode = rtrim(self::$testClassCode, "\n"); unset($file); }
private function getClosureCode(\Closure $closure) { $reflection = new \ReflectionFunction($closure); // Open file and seek to the first line of the closure $file = new \SplFileObject($reflection->getFileName()); $file->seek($reflection->getStartLine() - 1); // Retrieve all of the lines that contain code for the closure $code = ''; while ($file->key() < $reflection->getEndLine()) { $line = $file->current(); $line = ltrim($line); $code .= $line; $file->next(); } return $code; }
protected function _fetchCode() { // Open file and seek to the first line of the closure $file = new SplFileObject($this->reflection->getFileName()); $file->seek($this->reflection->getStartLine() - 1); // Retrieve all of the lines that contain code for the closure $code = ''; while ($file->key() < $this->reflection->getEndLine()) { $code .= $file->current(); $file->next(); } // Only keep the code defining that closure $begin = strpos($code, 'function'); $end = strrpos($code, '}'); $code = substr($code, $begin, $end - $begin + 1); return $code; }
/** * Get's the next string of hook code to process. * * @return string */ public function getHook() { foreach ($this->hooks as $value) { $hook = '<?php function ' . $value[1] . '(){'; $reflection = new \ReflectionFunction($value[2]); $file = new \SplFileObject($reflection->getFileName()); $file->seek($reflection->getStartLine() - 1); $code = ''; while ($file->key() < $reflection->getEndLine()) { $code .= $file->current(); $file->next(); } $begin = strpos($code, 'function') + 8; $begin = strrpos($code, '{', $begin) + 1; $end = strrpos($code, '}'); $hook .= substr($code, $begin, $end - $begin); $hook .= '}' . PHP_EOL; (yield $value[0] => $hook); } }
/** * {@inheritdoc} */ public function read() { if (null === $this->csv) { $this->initializeRead(); } $data = $this->csv->fgetcsv(); if (false !== $data) { if ([null] === $data || null === $data) { return null; } if ($this->stepExecution) { $this->stepExecution->incrementSummaryInfo('read_lines'); } if (count($this->fieldNames) !== count($data)) { throw new InvalidItemException('pim_connector.steps.csv_reader.invalid_item_columns_count', $data, ['%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 occurred while reading the csv.'); } return $data; }
public function parseFile($logFile, &$startId, $finish = false) { if (!$logFile || !file_exists($logFile)) { return $this->setError('Log file error'); } try { $file = new SplFileObject($logFile); $file->seek(!$startId ? 0 : $startId - 1); $counter = 0; $items = array(); $item = null; while ($file->valid()) { $row = trim($file->current()); $file->next(); if (!$row) { continue; } $item = Mage::getModel('mpbackup/backup_progress_item')->parse($row, $item); $items[] = $item; $counter += $item->getLength(); if (!$finish && $counter > Mageplace_Backup_Helper_Const::BACKUP_PROCESS_REQUEST_PERIOD * Mageplace_Backup_Helper_Const::BACKUP_PROCESS_RESPONSE_SIZE) { break; } } $startId = $file->key(); } catch (Exception $e) { Mage::logException($e); return $this->setError($e->getMessage()); } if (empty($items)) { if ($finish) { return $this->setError('Log is empty (' . print_r($items, true) . ') and log process is finished'); } else { return $this->setData(self::TEXT, self::TEXT_TREE_POINT)->setError(print_r($items, true)); } } return $this->setData(self::ITEMS, $items); }
private function LoadSourceLines(\ReflectionFunctionAbstract $Reflection) { if (!$Reflection->isUserDefined()) { throw new Functional\FunctionException('Cannot parse function: Function must be user defined'); } $FileName = $Reflection->getFileName(); if (!file_exists($FileName)) { throw new Functional\FunctionException('Cannot parse function: Function does not belong to a valid file (cannot be eval\'d code)'); } $SourceLines = []; $File = new \SplFileObject($Reflection->getFileName()); $StartLine = $Reflection->getStartLine() - 2; $File->seek($StartLine); $EndLine = $Reflection->getEndLine() - 2; while ($File->key() <= $EndLine) { $SourceLines[] = trim($File->fgets()); } unset($File); $FirstLine =& $SourceLines[0]; $FirstLine = substr($FirstLine, stripos($FirstLine, 'function')); $LastLine =& $SourceLines[count($SourceLines) - 1]; $LastLine = substr($LastLine, 0, strrpos($LastLine, '}') + 1); return array_filter($SourceLines); }
/** * create_generic() * * A function that creates an archive file * * The $excludes will be a list or relative path excludes if the $listmaker object is NULL otehrwise * will be absolute path excludes and relative path excludes can be had from the $listmaker object * * @param string $zip Full path & filename of ZIP Archive file to create * @param string $dir Full path of directory to add to ZIP Archive file * @parame array $excludes List of either absolute path exclusions or relative exclusions * @param string $tempdir [Optional] Full path of directory for temporary usage * @param object $listmaker The object from which we can get an inclusions list * @return bool True if the creation was successful, false otherwise * */ protected function create_generic($zip, $dir, $excludes, $tempdir, $listmaker) { $exitcode = 0; $output = array(); $zippath = ''; $command = ''; $temp_zip = ''; $excluding_additional = false; $exclude_count = 0; $exclusions = array(); $have_zip_errors = false; $zip_errors_count = 0; $zip_errors = array(); $have_zip_warnings = false; $zip_warnings_count = 0; $zip_warnings = array(); $have_zip_additions = false; $zip_additions_count = 0; $zip_additions = array(); $have_zip_debug = false; $zip_debug_count = 0; $zip_debug = array(); $have_zip_other = false; $zip_other_count = 0; $zip_other = array(); $zip_using_log_file = false; $logfile_name = ''; $zip_ignoring_symlinks = false; // The basedir must have a trailing directory separator $basedir = rtrim(trim($dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; if (empty($tempdir) || !@file_exists($tempdir)) { pb_backupbuddy::status('details', __('Temporary working directory must be available.', 'it-l10n-backupbuddy')); return false; } // Tell which zip version is being used $version = $this->get_zip_version(); if (true === is_array($version)) { 2 == $version['major'] && 0 == $version['minor'] ? $version['minor'] = 'X' : true; pb_backupbuddy::status('details', sprintf(__('Using zip version: %1$s.%2$s', 'it-l10n-backupbuddy'), $version['major'], $version['minor'])); } else { $version = array("major" => "X", "minor" => "Y"); pb_backupbuddy::status('details', sprintf(__('Using zip version: %1$s.%2$s', 'it-l10n-backupbuddy'), $version['major'], $version['minor'])); } // Get the command path for the zip command - should return a trimmed string $zippath = $this->get_command_path(self::COMMAND_ZIP_PATH); // Determine if we are using an absolute path if (!empty($zippath)) { pb_backupbuddy::status('details', __('Using absolute zip path: ', 'it-l10n-backupbuddy') . $zippath); } // Add the trailing slash if required $command = $this->slashify($zippath) . 'zip'; // Always do recursive operation $command .= ' -r'; // Check if the version of zip in use supports log file (which will help with memory usage for large sites) if (true === $this->get_zip_supports_log_file()) { // Choose to use log file so quieten stdout - we'll set up the log file later $command .= ' -q'; $zip_using_log_file = true; } // Check if we need to turn off compression by settings (faster but larger backup) if (true !== $this->get_compression()) { $command .= ' -0'; pb_backupbuddy::status('details', __('Zip archive creation compression disabled based on settings.', 'it-l10n-backupbuddy')); } else { pb_backupbuddy::status('details', __('Zip archive creation compression enabled based on settings.', 'it-l10n-backupbuddy')); } // Check if ignoring (not following) symlinks if (true === $this->get_ignore_symlinks()) { // Not all OS support this for command line zip but best to handle it late and just // indicate here it is requested but not supported by OS switch ($this->get_os_type()) { case self::OS_TYPE_NIX: // Want to not follow symlinks so set command option and set flag for later use $command .= ' -y'; $zip_ignoring_symlinks = true; pb_backupbuddy::status('details', __('Zip archive creation symbolic links will not be followed based on settings.', 'it-l10n-backupbuddy')); break; case self::OS_TYPE_WIN: pb_backupbuddy::status('details', __('Zip archive creation symbolic links requested to not be followed based on settings but this option is not supported on this operating system.', 'it-l10n-backupbuddy')); break; default: pb_backupbuddy::status('details', __('Zip archive creation symbolic links requested to not be followed based on settings but this option is not supported on this operating system.', 'it-l10n-backupbuddy')); } } else { pb_backupbuddy::status('details', __('Zip archive creation symbolic links will be followed based on settings.', 'it-l10n-backupbuddy')); } // Check if we are ignoring warnings - meaning can still get a backup even // if, e.g., some files cannot be read if (true === $this->get_ignore_warnings()) { // Note: warnings are being ignored but will still be gathered and logged pb_backupbuddy::status('details', __('Zip archive creation actionable warnings will be ignored based on settings.', 'it-l10n-backupbuddy')); } else { pb_backupbuddy::status('details', __('Zip archive creation actionable warnings will not be ignored based on settings.', 'it-l10n-backupbuddy')); } // Delete any existing zip file of same name - really this should never happen if (@file_exists($zip)) { pb_backupbuddy::status('details', __('Existing ZIP Archive file will be replaced.', 'it-l10n-backupbuddy')); @unlink($zip); } // Now we'll set up the logging to file if required - use full logging if (true === $zip_using_log_file) { $logfile_name = $tempdir . self::ZIP_LOG_FILE_NAME; $command .= " -lf '{$logfile_name}' -li"; } // Set temporary directory to store ZIP while it's being generated. $command .= " -b '{$tempdir}'"; // Specify where to place the finalized zip archive file // If warnings are being ignored we can tell zip to create the zip archive in the final // location - otherwise we must put it in a temporary location and move it later only // if there are no warnings. This copes with the case where (this) controlling script // gets timed out by the server and if the file were created in the final location with // warnings that should not be ignored we cannot prevent it being created. The -MM option // could be used but this prevents us catching such warnings and being able to report // them to the user in the case where the script hasn't been terminated. Additionally the // -MM option would bail out on the first encountered problem and so if there were a few // problems they would each not be found until the current one is fixed and try again. if (true === $this->get_ignore_warnings()) { $temp_zip = $zip; } else { $temp_zip = $tempdir . basename($zip); } $command .= " '{$temp_zip}' ."; // Now work out exclusions dependent on what we have been given if (is_object($listmaker) && (defined('USE_EXPERIMENTAL_ZIPBUDDY_INCLUSION') && true === USE_EXPERIMENTAL_ZIPBUDDY_INCLUSION)) { // We're doing an inclusion operation, but first we'll just show the exclusiosn // For zip we need relative rather than absolute exclusion spaths $exclusions = $listmaker->get_relative_excludes($basedir); if (count($exclusions) > 0) { pb_backupbuddy::status('details', __('Calculating directories to exclude from backup.', 'it-l10n-backupbuddy')); $excluding_additional = false; $exclude_count = 0; foreach ($exclusions as $exclude) { if (!strstr($exclude, 'backupbuddy_backups')) { // Set variable to show we are excluding additional directories besides backup dir. $excluding_additional = true; } pb_backupbuddy::status('details', __('Excluding', 'it-l10n-backupbuddy') . ': ' . $exclude); $exclude_count++; } } // Get the list of inclusions to process $inclusions = $listmaker->get_terminals(); // For each directory we need to put the "wildcard" on the end foreach ($inclusions as &$inclusion) { if (is_dir($inclusion)) { $inclusion .= DIRECTORY_SEPARATOR . "*"; } // Remove directory path prefix excluding leading slash to make relative (needed for zip) $inclusion = str_replace(rtrim($basedir, DIRECTORY_SEPARATOR), '', $inclusion); } // Now create the inclusions file in the tempdir // And update the command options $ifile = $tempdir . self::ZIP_INCLUSIONS_FILE_NAME; if (file_exists($ifile)) { @unlink($ifile); } file_put_contents($ifile, implode(PHP_EOL, $inclusions) . PHP_EOL . PHP_EOL); $command .= " -i@" . "'{$ifile}'"; } else { // We're doing an exclusion operation //$command .= "-i '*' "; // Not needed. Zip defaults to doing this. Removed July 10, 2012 for v3.0.41. // Since we had no $listmaker object or not using it get the standard relative excludes to process $exclusions = $excludes; if (count($exclusions) > 0) { // Handle exclusions by placing them in an exclusion text file. $exclusion_file = $tempdir . self::ZIP_EXCLUSIONS_FILE_NAME; $this->_render_exclusions_file($exclusion_file, $exclusions); pb_backupbuddy::status('details', sprintf(__('Using exclusion file `%1$s`', 'it-l10n-backupbuddy'), $exclusion_file)); $command .= ' -x@' . "'{$exclusion_file}'"; } } // If we can't use a log file but exec_dir isn't in use we can redirect stderr to stdout // If exec_dir is in use we cannot redirect because of command line escaping so cannot log errors/warnings if (false === $zip_using_log_file) { if (false === $this->get_exec_dir_flag()) { $command .= ' 2>&1'; } else { pb_backupbuddy::status('details', sprintf(__('Zip Errors/Warnings cannot not be logged with this version of zip and exec_dir active', 'it-l10n-backupbuddy'), true)); } } // Remember the current directory and change to the directory being added so that "." is valid in command $working_dir = getcwd(); chdir($dir); $command = self::OS_TYPE_WIN === $this->get_os_type() ? str_replace('\'', '"', $command) : $command; pb_backupbuddy::status('details', $this->get_method_tag() . __(' command', 'it-l10n-backupbuddy') . ': ' . $command); @exec($command, $output, $exitcode); // Set current working directory back to where we were chdir($working_dir); // Convenience for handling different scanarios $result = false; // If we used a log file then process the log file - else process output // Always scan the output/logfile for warnings, etc. and show warnings even if user has chosen to ignore them if (true === $zip_using_log_file) { try { $logfile = new SplFileObject($logfile_name, "rb"); while (!$logfile->eof()) { $line = $logfile->current(); $id = $logfile->key(); // Use the line number as unique key for later sorting $logfile->next(); if (preg_match('/^\\s*(zip warning:)/i', $line)) { // Looking for specific types of warning - in particular want the warning that // indicates a file couldn't be read as we want to treat that as a "skipped" // warning that indicates that zip flagged this as a potential problem but // created the zip file anyway - but it would have generated the non-zero exit // code of 18 and we key off that later. All other warnings are not considered // reasons to return a non-zero exit code whilst still creating a zip file so // we'll follow the lead on that and not have other warning types halt the backup. // So we'll try and look for a warning output that looks like it is file related... if (preg_match('/^\\s*(zip warning:)\\s*([^:]*:)\\s*(.*)/i', $line, $matches)) { // Matched to what looks like a file related warning so check particular cases switch (strtolower($matches[2])) { case "could not open for reading:": $zip_warnings[self::ZIP_WARNING_SKIPPED][$id] = trim($line); $zip_warnings_count++; break; case "name not matched:": $zip_other[self::ZIP_OTHER_GENERIC][$id] = trim($line); $zip_other_count++; break; default: $zip_warnings[self::ZIP_WARNING_GENERIC][$id] = trim($line); $zip_warnings_count++; } } else { // Didn't match to what would look like a file related warning so count it regardless $zip_warnings[self::ZIP_WARNING_GENERIC][$id] = trim($line); $zip_warnings_count++; } } elseif (preg_match('/^\\s*(zip error:)/i', $line)) { $zip_errors[$id] = trim($line); $zip_errors_count++; } elseif (preg_match('/^\\s*(adding:)/i', $line)) { // Currently not processing additions entried //$zip_additions[] = trim( $line ); //$zip_additions_count++; } elseif (preg_match('/^\\s*(sd:)/i', $line)) { $zip_debug[$id] = trim($line); $zip_debug_count++; } else { // Currently not processing other entries //$zip_other[] = trim( $line ); //$zip_other_count++; } } unset($logfile); @unlink($logfile_name); } catch (Exception $e) { // Something fishy - we should have been able to open the log file... $error_string = $e->getMessage(); pb_backupbuddy::status('details', sprintf(__('Log file could not be opened - error reported: %1$s', 'it-l10n-backupbuddy'), $error_string)); } } else { // TODO: $output could be large so if we parse it all into separate arrays then may want to shift // out each line and then discard it after copied to another array $id = 0; // Create a unique key (like a line number) for later sorting foreach ($output as $line) { if (preg_match('/^\\s*(zip warning:)/i', $line)) { // Looking for specific types of warning - in particular want the warning that // indicates a file couldn't be read as we want to treat that as a "skipped" // warning that indicates that zip flagged this as a potential problem but // created the zip file anyway - but it would have generated the non-zero exit // code of 18 and we key off that later. All other warnings are not considered // reasons to return a non-zero exit code whilst still creating a zip file so // we'll follow the lead on that and not have other warning types halt the backup. // So we'll try and look for a warning output that looks like it is file related... if (preg_match('/^\\s*(zip warning:)\\s*([^:]*:)\\s*(.*)/i', $line, $matches)) { // Matched to what looks like a file related warning so check particular cases switch (strtolower($matches[2])) { case "could not open for reading:": $zip_warnings[self::ZIP_WARNING_SKIPPED][$id++] = trim($line); $zip_warnings_count++; break; case "name not matched:": $zip_other[self::ZIP_OTHER_GENERIC][$id++] = trim($line); $zip_other_count++; break; default: $zip_warnings[self::ZIP_WARNING_GENERIC][$id++] = trim($line); $zip_warnings_count++; } } else { // Didn't match to what would look like a file related warning so count it regardless $zip_warnings[self::ZIP_WARNING_GENERIC][$id++] = trim($line); $zip_warnings_count++; } } elseif (preg_match('/^\\s*(zip error:)/i', $line)) { $zip_errors[$id++] = trim($line); $zip_errors_count++; } elseif (preg_match('/^\\s*(adding:)/i', $line)) { // Currently not processing additions entried //$zip_additions[] = trim( $line ); //$zip_additions_count++; $id++; } elseif (preg_match('/^\\s*(sd:)/i', $line)) { $zip_debug[$id++] = trim($line); $zip_debug_count++; } else { // Currently not processing other entries //$zip_other[] = trim( $line ); //$zip_other_count++; $id++; } } // Now free up the memory... unset($output); } // Set convenience flags $have_zip_warnings = 0 < $zip_warnings_count; $have_zip_errors = 0 < $zip_errors_count; $have_zip_additions = 0 < $zip_additions_count; $have_zip_debug = 0 < $zip_debug_count; $have_zip_other = 0 < $zip_other_count; // Always report the exit code regardless of whether we might ignore it or not pb_backupbuddy::status('details', __('Zip process exit code: ', 'it-l10n-backupbuddy') . $exitcode); // Always report the number of warnings - even just to confirm that we didn't have any pb_backupbuddy::status('details', sprintf(__('Zip process reported: %1$s warning%2$s', 'it-l10n-backupbuddy'), $zip_warnings_count, 1 == $zip_warnings_count ? '' : 's')); // Always report warnings regardless of whether user has selected to ignore them if (true === $have_zip_warnings) { $this->log_zip_reports($zip_warnings, self::$_warning_desc, "WARNING", self::MAX_WARNING_LINES_TO_SHOW, dirname(dirname($tempdir)) . DIRECTORY_SEPARATOR . 'pb_backupbuddy' . DIRECTORY_SEPARATOR . self::ZIP_WARNINGS_FILE_NAME); } // Always report other reports regardless if (true === $have_zip_other) { // Only report number of informationals if we have any as they are not that important pb_backupbuddy::status('details', sprintf(__('Zip process reported: %1$s information%2$s', 'it-l10n-backupbuddy'), $zip_other_count, 1 == $zip_other_count ? 'al' : 'als')); $this->log_zip_reports($zip_other, self::$_other_desc, "INFORMATION", self::MAX_OTHER_LINES_TO_SHOW, dirname(dirname($tempdir)) . DIRECTORY_SEPARATOR . 'pb_backupbuddy' . DIRECTORY_SEPARATOR . self::ZIP_OTHERS_FILE_NAME); } // See if we can figure out what happened - note that $exitcode could be non-zero for actionable warning(s) or error // if ( (no zip file) or (fatal exit code) or (not ignoring warnable exit code) ) // TODO: Handle condition testing with function calls based on mapping exit codes to exit type (fatal vs non-fatal) if (!@file_exists($temp_zip) || 0 != $exitcode && 18 != $exitcode || 18 == $exitcode && !$this->get_ignore_warnings()) { // If we have any zip errors reported show them regardless if (true === $have_zip_errors) { pb_backupbuddy::status('details', sprintf(__('Zip process reported: %1$s error%2$s', 'it-l10n-backupbuddy'), $zip_errors_count, 1 == $zip_errors_count ? '' : 's')); foreach ($zip_errors as $line) { pb_backupbuddy::status('details', __('Zip process reported: ', 'it-l10n-backupbuddy') . $line); } } // Report whether or not the zip file was created (whether that be in the final or temporary location) if (!@file_exists($temp_zip)) { pb_backupbuddy::status('details', __('Zip Archive file not created - check process exit code.', 'it-l10n-backupbuddy')); } else { pb_backupbuddy::status('details', __('Zip Archive file created but with errors/actionable-warnings so will be deleted - check process exit code and warnings.', 'it-l10n-backupbuddy')); } // The operation has failed one way or another. Note that as the user didn't choose to ignore errors the zip file // is always created in a temporary location and then only moved to final location on success without error or warnings. // Therefore if there is a zip file (produced but with warnings) it will not be visible and will be deleted when the // temporary directory is deleted below. $result = false; } else { // Got file with no error or warnings _or_ with warnings that the user has chosen to ignore if (false === $this->get_ignore_warnings()) { // Because not ignoring warnings the zip archive was built in temporary location so we need to move it pb_backupbuddy::status('details', __('Moving Zip Archive file to local archive directory.', 'it-l10n-backupbuddy')); // Make sure no stale file information clearstatcache(); @rename($temp_zip, $zip); if (@file_exists($zip)) { pb_backupbuddy::status('details', __('Zip Archive file moved to local archive directory.', 'it-l10n-backupbuddy')); pb_backupbuddy::status('message', __('Zip Archive file successfully created with no errors or actionable warnings.', 'it-l10n-backupbuddy')); $this->log_archive_file_stats($zip); $result = true; } else { pb_backupbuddy::status('details', __('Zip Archive file could not be moved to local archive directory.', 'it-l10n-backupbuddy')); $result = false; } } else { // Warnings were being ignored so built in final location so no need to move it if (@file_exists($zip)) { pb_backupbuddy::status('message', __('Zip Archive file successfully created with no errors (any actionable warnings ignored by user settings).', 'it-l10n-backupbuddy')); $this->log_archive_file_stats($zip); $result = true; } else { // Odd condition - file should be present but apparently not? pb_backupbuddy::status('details', __('Zip Archive file could not be found in local archive directory.', 'it-l10n-backupbuddy')); $result = false; } } } // Cleanup the temporary directory that will have all detritus and maybe incomplete zip file pb_backupbuddy::status('details', __('Removing temporary directory.', 'it-l10n-backupbuddy')); if (!$this->delete_directory_recursive($tempdir)) { pb_backupbuddy::status('details', __('Temporary directory could not be deleted: ', 'it-l10n-backupbuddy') . $tempdir); } return $result; }
/** * @param \Illuminate\Database\Eloquent\Model $model */ protected function getPropertiesFromMethods($model) { $methods = get_class_methods($model); if ($methods) { foreach ($methods as $method) { if (Str::startsWith($method, 'get') && Str::endsWith($method, 'Attribute') && $method !== 'getAttribute') { //Magic get<name>Attribute $name = Str::snake(substr($method, 3, -9)); if (!empty($name)) { $this->setProperty($name, null, true, null); } } elseif (Str::startsWith($method, 'set') && Str::endsWith($method, 'Attribute') && $method !== 'setAttribute') { //Magic set<name>Attribute $name = Str::snake(substr($method, 3, -9)); if (!empty($name)) { $this->setProperty($name, null, null, true); } } elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery') { //Magic set<name>Attribute $name = Str::camel(substr($method, 5)); if (!empty($name)) { $reflection = new \ReflectionMethod($model, $method); $args = $this->getParameters($reflection); //Remove the first ($query) argument array_shift($args); $this->setMethod($name, '\\Illuminate\\Database\\Query\\Builder|\\' . $reflection->class, $args); } } elseif (!method_exists('Eloquent', $method) && !Str::startsWith($method, 'get')) { //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php $reflection = new \ReflectionMethod($model, $method); $file = new \SplFileObject($reflection->getFileName()); $file->seek($reflection->getStartLine() - 1); $code = ''; while ($file->key() < $reflection->getEndLine()) { $code .= $file->current(); $file->next(); } $begin = strpos($code, 'function('); $code = substr($code, $begin, strrpos($code, '}') - $begin + 1); foreach (array('hasMany', 'belongsToMany', 'hasOne', 'belongsTo', 'morphTo', 'morphMany', 'morphToMany') as $relation) { $search = '$this->' . $relation . '('; if ($pos = stripos($code, $search)) { $code = substr($code, $pos + strlen($search)); $arguments = explode(',', substr($code, 0, strpos($code, ')'))); //Remove quotes, ensure 1 \ in front of the model $returnModel = $this->getClassName($arguments[0], $model); if ($relation === "belongsToMany" or $relation === 'hasMany' or $relation === 'morphMany' or $relation === 'morphToMany') { //Collection or array of models (because Collection is Arrayable) $this->setProperty($method, '\\Illuminate\\Database\\Eloquent\\Collection|' . $returnModel . '[]', true, null); } else { //Single model is returned $this->setProperty($method, $returnModel, true, null); } } } } } } }
/** * @param string $name * @param array $configRaw * @param integer $limit */ protected function callSync($name, $configRaw, $limit) { //normalize config $config = array('last' => isset($configRaw[$name . '_last']) ? $configRaw[$name . '_last'] : 0, 'rows' => isset($configRaw[$name . '_rows']) ? $configRaw[$name . '_rows'] : 0); $filePath = $this->iniConfiguration->getTempPath() . '/' . $name . '.data'; if ($config['rows'] == 0) { //create new request $response = $this->getWrapper()->request('data', $name, array('lastDownload' => $config['last'])); //analyze response switch ($this->getWrapper()->analyzeResponse($response)) { case 'url': //continue to download $this->getWrapper()->download($response->url, $filePath); break; case 'suspend': //suspend processing for time in response return $this->suspend($response->until); default: throw new Api\ApiException('Response from datadepo is unknow'); } } //open file and read header $file = new \SplFileObject($filePath); $header = json_decode($file->fgets(), TRUE); //check number of lines in downloaded file if ($config['rows'] == 0) { $file->seek(PHP_INT_MAX); $linesTotal = $file->key() + 1; if ($header['numRecords'] != $linesTotal - 1) { throw new Api\ApiException('Incompleted file downloaded in ' . $name . ' synchronizer'); } $file->rewind(); } $processCount = $this->iniConfiguration->get('limits', 'processCount'); $collector = $this->createCollector(); $count = 0; //skip line $file->seek($config['rows'] + 1); while ((!$file->eof() || $file->current() !== FALSE) && $count < $limit) { //add line to front $line = $this->wrapLine($file->current()); $collector->add($line); $count++; //process if ($count % $processCount === 0) { $this->processChunk($collector); $collector = $this->createCollector(); } $file->next(); } //sync rest if (count($collector) > 0) { $this->processChunk($collector); } //check num processed if ($count == $limit) { $this->dataStore->setConfig($name . '_rows', $config['rows'] + $count); } else { $this->dataStore->setConfig($name . '_rows', 0); $this->dataStore->setConfig($name . '_last', $header['generatedAt']); } return new Api\DataDepoResponse(Api\DataDepoResponse::CODE_OK, NULL, array('processed' => $count)); }
/** * @param \Illuminate\Database\Eloquent\Model $model */ protected function getPropertiesFromMethods($model) { $methods = get_class_methods($model); if ($methods) { foreach ($methods as $method) { if (Str::startsWith($method, 'get') && Str::endsWith($method, 'Attribute') && $method !== 'getAttribute') { //Magic get<name>Attribute $name = Str::snake(substr($method, 3, -9)); if (!empty($name)) { $this->setProperty($name, null, true, null); } } elseif (Str::startsWith($method, 'set') && Str::endsWith($method, 'Attribute') && $method !== 'setAttribute') { //Magic set<name>Attribute $name = Str::snake(substr($method, 3, -9)); if (!empty($name)) { $this->setProperty($name, null, null, true); } } elseif (Str::startsWith($method, 'scope') && $method !== 'scopeQuery') { //Magic set<name>Attribute $name = Str::camel(substr($method, 5)); if (!empty($name)) { $reflection = new \ReflectionMethod($model, $method); $args = $this->getParameters($reflection); //Remove the first ($query) argument array_shift($args); $this->setMethod($name, '\\Illuminate\\Database\\Query\\Builder|\\' . $reflection->class, $args); } } elseif (!method_exists('Illuminate\\Database\\Eloquent\\Model', $method) && !Str::startsWith($method, 'get')) { //Use reflection to inspect the code, based on Illuminate/Support/SerializableClosure.php $reflection = new \ReflectionMethod($model, $method); $file = new \SplFileObject($reflection->getFileName()); $file->seek($reflection->getStartLine() - 1); $code = ''; while ($file->key() < $reflection->getEndLine()) { $code .= $file->current(); $file->next(); } $code = trim(preg_replace('/\\s\\s+/', '', $code)); $begin = strpos($code, 'function('); $code = substr($code, $begin, strrpos($code, '}') - $begin + 1); foreach (array('hasMany', 'hasManyThrough', 'belongsToMany', 'hasOne', 'belongsTo', 'morphOne', 'morphTo', 'morphMany', 'morphToMany') as $relation) { $search = '$this->' . $relation . '('; if ($pos = stripos($code, $search)) { //Resolve the relation's model to a Relation object. $relationObj = $model->{$method}(); if ($relationObj instanceof Relation) { $relatedModel = '\\' . get_class($relationObj->getRelated()); $relations = ['hasManyThrough', 'belongsToMany', 'hasMany', 'morphMany', 'morphToMany']; if (in_array($relation, $relations)) { //Collection or array of models (because Collection is Arrayable) $this->setProperty($method, $this->getCollectionClass($relatedModel) . '|' . $relatedModel . '[]', true, null); } elseif ($relation === "morphTo") { // Model isn't specified because relation is polymorphic $this->setProperty($method, '\\Illuminate\\Database\\Eloquent\\Model|\\Eloquent', true, null); } else { //Single model is returned $this->setProperty($method, $relatedModel, true, null); } } } } } } } }
private function determinePotentialTokens(\ReflectionFunction $reflection) { // Load the file containing the code for the function. $fileName = $reflection->getFileName(); if (!is_readable($fileName)) { throw new ClosureAnalysisException("Cannot read the file containing the closure: \"{$fileName}\"."); } $code = ''; $file = new \SplFileObject($fileName); $file->seek($reflection->getStartLine() - 1); while ($file->key() < $reflection->getEndLine()) { $code .= $file->current(); $file->next(); } $code = trim($code); if (strpos($code, '<?php') !== 0) { $code = "<?php\n" . $code; } return token_get_all($code); }
/** * (PHP 5 >= 5.0.0)<br/> * Return the key of the current element * @link http://php.net/manual/en/iterator.key.php * @return mixed scalar on success, or null on failure. */ public function key() { return parent::key(); }
/** * {@inheritdoc} */ public function key() { return $this->file->key(); }
/** * Rollback file pointer * * @param \SplFileObject $file File * * @return void */ protected function rollbackFilePointer(\SplFileObject $file) { $file->seek($file->key() - 1); $this->position--; $this->importer->getOptions()->position = $this->importer->getOptions()->position - 1; }
/** * @param Closure $closure * @return string */ function closureHash(Closure $closure) { $ref = new ReflectionFunction($closure); $file = new SplFileObject($ref->getFileName()); $file->seek($ref->getStartLine() - 1); $content = ''; while ($file->key() < $ref->getEndLine()) { $content .= $file->current(); $file->next(); } $hash = md5(json_encode([$content, $ref->getStaticVariables()])); return $hash; }
function gravityCount() { //returns count of domains in blocklist. $NGC4889 = new \SplFileObject('/etc/pihole/gravity.list'); $NGC4889->seek($NGC4889->getSize()); $swallowed = $NGC4889->key(); return $swallowed; }
<?php //line 2 //line 3 //line 4 //line 5 $s = new SplFileObject(__FILE__); $s->seek(3); echo $s->key();
public function extract(callable $callback) { $ref = new \ReflectionFunction($callback); $file = new \SplFileObject($ref->getFileName()); $file->seek($ref->getStartLine() - 1); $content = ''; while ($file->key() < $ref->getEndLine()) { $content .= $file->current(); $file->next(); } if (fnmatch('*function*', $content)) { list($dummy, $code) = explode('function', $content, 2); $code = 'function' . $code; $tab = explode('}', $code); $last = end($tab); $code = str_replace('}' . $last, '}', $code); return $code; } return null; }
/** * @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))); } }
/** * @param SplFileObject $filesListObject * @param $p_add_dir * @param $p_remove_dir * * @return bool */ function _addList($filesListObject, $p_add_dir, $p_remove_dir) { $v_header = array(); if (!$this->_file) { $this->_addBackupProcessMessage('Invalid file descriptor', true); return false; } $filesListObject->seek((int) $this->getStepIndex()); while ($filesListObject->valid()) { $this->setStepIndex($filesListObject->key()); if ($this->timeIsUp()) { return true; } $filename = $filesListObject->current(); if (trim($filename) === '') { $filesListObject->next(); continue; } if ($filename == $this->_tarname) { $filesListObject->next(); continue; } if (!file_exists($filename) && !is_link($filename)) { $this->_addBackupProcessMessage('File "' . $filename . '" does not exist', Mageplace_Backup_Model_Backup::LOG_LEVEL_WARNING); $filesListObject->next(); continue; } if (!$this->_addFile($filename, $v_header, $p_add_dir, $p_remove_dir)) { return false; } if ($this->_timeIsUp) { return true; } $filesListObject->next(); } $this->setStepIndex(null); return true; }