Esempio n. 1
0
 /**
  * VQModObject::applyMod()
  *
  * @param array $mods Array of search add nodes
  * @param string $data File contents to be altered
  * @return null
  * @description Applies all modifications to the text data
  */
 public function applyMod($mods, &$data)
 {
     if ($this->_skip) {
         return;
     }
     $tmp = $data;
     foreach ($mods as $mod) {
         VQMod::$fileModding = $mod['fileToMod'] . '(' . $mod['opIndex'] . ')';
         if (!empty($mod['ignoreif'])) {
             if ($mod['ignoreif']->regex == 'true') {
                 if (preg_match($mod['ignoreif']->getContent(), $tmp)) {
                     continue;
                 }
             } else {
                 if (strpos($tmp, $mod['ignoreif']->getContent()) !== false) {
                     continue;
                 }
             }
         }
         $indexCount = 0;
         $tmp = $this->_explodeData($tmp);
         $lineMax = count($tmp) - 1;
         switch ($mod['search']->position) {
             case 'top':
                 $tmp[$mod['search']->offset] = $mod['add']->getContent() . $tmp[$mod['search']->offset];
                 break;
             case 'bottom':
                 $offset = $lineMax - $mod['search']->offset;
                 if ($offset < 0) {
                     $tmp[-1] = $mod['add']->getContent();
                 } else {
                     $tmp[$offset] .= $mod['add']->getContent();
                 }
                 break;
             default:
                 $changed = false;
                 foreach ($tmp as $lineNum => $line) {
                     if (strlen($mod['search']->getContent()) == 0) {
                         if ($mod['error'] == 'log' || $mod['error'] == 'abort') {
                             VQMod::$log->write('VQModObject::applyMod - EMPTY SEARCH CONTENT ERROR', $this);
                         }
                         break;
                     }
                     if ($mod['search']->regex == 'true') {
                         $pos = @preg_match($mod['search']->getContent(), $line);
                         if ($pos === false) {
                             if ($mod['error'] == 'log' || $mod['error'] == 'abort') {
                                 VQMod::$log->write('VQModObject::applyMod - INVALID REGEX ERROR - ' . $mod['search']->getContent(), $this);
                             }
                             continue 2;
                         } elseif ($pos == 0) {
                             $pos = false;
                         }
                     } else {
                         $pos = strpos($line, $mod['search']->getContent());
                     }
                     if ($pos !== false) {
                         $indexCount++;
                         $changed = true;
                         if (!$mod['search']->indexes() || $mod['search']->indexes() && in_array($indexCount, $mod['search']->indexes())) {
                             switch ($mod['search']->position) {
                                 case 'before':
                                     $offset = $lineNum - $mod['search']->offset < 0 ? -1 : $lineNum - $mod['search']->offset;
                                     $tmp[$offset] = empty($tmp[$offset]) ? $mod['add']->getContent() : $mod['add']->getContent() . "\n" . $tmp[$offset];
                                     break;
                                 case 'after':
                                     $offset = $lineNum + $mod['search']->offset > $lineMax ? $lineMax : $lineNum + $mod['search']->offset;
                                     $tmp[$offset] = $tmp[$offset] . "\n" . $mod['add']->getContent();
                                     break;
                                 case 'ibefore':
                                     $tmp[$lineNum] = str_replace($mod['search']->getContent(), $mod['add']->getContent() . $mod['search']->getContent(), $line);
                                     break;
                                 case 'iafter':
                                     $tmp[$lineNum] = str_replace($mod['search']->getContent(), $mod['search']->getContent() . $mod['add']->getContent(), $line);
                                     break;
                                 default:
                                     if (!empty($mod['search']->offset)) {
                                         for ($i = 1; $i <= $mod['search']->offset; $i++) {
                                             if (isset($tmp[$lineNum + $i])) {
                                                 $tmp[$lineNum + $i] = '';
                                             }
                                         }
                                     }
                                     if ($mod['search']->regex == 'true') {
                                         $tmp[$lineNum] = preg_replace($mod['search']->getContent(), $mod['add']->getContent(), $line);
                                     } else {
                                         $tmp[$lineNum] = str_replace($mod['search']->getContent(), $mod['add']->getContent(), $line);
                                     }
                                     break;
                             }
                         }
                     }
                 }
                 if (!$changed) {
                     $skip = $mod['error'] == 'skip' || $mod['error'] == 'log' ? ' (SKIPPED)' : ' (ABORTING MOD)';
                     if ($mod['error'] == 'log' || $mod['error'] == 'abort') {
                         VQMod::$log->write('VQModObject::applyMod - SEARCH NOT FOUND' . $skip . ': ' . $mod['search']->getContent(), $this);
                     }
                     if ($mod['error'] == 'abort') {
                         $this->_skip = true;
                         return;
                     }
                 }
                 break;
         }
         ksort($tmp);
         $tmp = $this->_implodeData($tmp);
     }
     VQMod::$fileModding = false;
     $data = $tmp;
 }
Esempio n. 2
0
 /**
  * VQModObject::_parseMods()
  *
  * @param DOMNode $node <modification> node to be parsed
  * @return null
  * @description Parses modifications in preparation for the applyMod method to work
  */
 private function _parseMods(DOMNode $node)
 {
     $files = $node->getElementsByTagName('file');
     $replaces = VQMod::$replaces;
     foreach ($files as $file) {
         $path = $file->getAttribute('path') ? $file->getAttribute('path') : '';
         $filesToMod = explode(',', $file->getAttribute('name'));
         foreach ($filesToMod as $filename) {
             $fileToMod = $path . $filename;
             if (!empty($replaces)) {
                 foreach ($replaces as $r) {
                     if (count($r) == 2) {
                         $fileToMod = preg_replace($r[0], $r[1], $fileToMod);
                     }
                 }
             }
             $error = $file->hasAttribute('error') ? $file->getAttribute('error') : 'log';
             $fullPath = VQMod::_realpath($fileToMod);
             if (!$fullPath || !file_exists($fullPath)) {
                 if (strpos($fileToMod, '*') !== false) {
                     $fullPath = VQMod::getCwd() . $fileToMod;
                 } else {
                     if ($error == 'log' || $error == 'abort') {
                         $skip = $error == 'log' ? ' (SKIPPED)' : ' (ABORTING MOD)';
                         VQMod::$log->write('VQModObject::parseMods - Could not resolve path for [' . $fileToMod . ']' . $skip, $this);
                     }
                     if ($error == 'log' || $error == 'skip') {
                         continue;
                     } elseif ($error == 'abort') {
                         return false;
                     }
                 }
             }
             $operations = $file->getElementsByTagName('operation');
             foreach ($operations as $opIndex => $operation) {
                 VQMod::$fileModding = $fileToMod . '(' . $opIndex . ')';
                 $skipOperation = false;
                 $error = $operation->hasAttribute('error') ? $operation->getAttribute('error') : 'abort';
                 $ignoreif = $operation->getElementsByTagName('ignoreif')->item(0);
                 if ($ignoreif) {
                     $ignoreif = new VQSearchNode($ignoreif);
                 } else {
                     $ignoreif = false;
                 }
                 $search = $operation->getElementsByTagName('search')->item(0);
                 $add = $operation->getElementsByTagName('add')->item(0);
                 if (!$search) {
                     VQMod::$log->write('Operation <search> tag missing', $this);
                     $skipOperation = true;
                 }
                 if (!$add) {
                     VQMod::$log->write('Operation <add> tag missing', $this);
                     $skipOperation = true;
                 }
                 if (!$skipOperation) {
                     $this->mods[$fullPath][] = array('search' => new VQSearchNode($search), 'add' => new VQAddNode($add), 'ignoreif' => $ignoreif, 'error' => $error, 'fileToMod' => $fileToMod, 'opIndex' => $opIndex);
                 }
             }
             VQMod::$fileModding = false;
         }
     }
 }