#!/usr/bin/php -q regex=$regex; } public function accept() { return !preg_match($this->regex, $this->current()); } } /** * Filter file list using regular expression * */ class RegexFilter extends FilterIterator { protected $regex; public function __construct(Iterator $it, $regex) { parent::__construct($it); $this->regex=$regex; } public function accept() { return preg_match($this->regex, $this->current()); } } /** * Filter file list by depth * */ class DepthFilter extends FilterIterator { protected $depth; public function __construct(Iterator $it, $depth) { parent::__construct($it); $this->depth=$depth; } public function accept() { return $this->getInnerIterator()->getDepth()<$this->depth; } } /** * Fix new line characters in given file * Returns true if file was changed * * @param string $path relative or absolute path name to file * @param string $nl name of a constant that holds new line character (CRLF|CR|LF) * @return bool */ function fixFile($path, $nl) { $contents = file($path); $size = filesize($path); if ($contents === false) { echo "\rERROR: couldn't read the " . $path . " file". "\n"; return false; } $modified = false; $new_content = ""; $contents_len = sizeof($contents); if ($GLOBALS['eofstripwhite']) { $lines_processed=0; //iterate through lines, from the end of file for ($i=$contents_len-1; $i>=0; $i--) { $old_line = $contents[$i]; $contents[$i] = rtrim($contents[$i]); if ($old_line !== $contents[$i]) { if (!$GLOBALS['eofnewline'] || $old_line !== $contents[$i] . constant($nl) || $lines_processed>0) { $modified = true; } } if (empty($contents[$i])) { //we have an empty line at the end of file, just skip it unset($contents[$contents_len--]); } else { if ($GLOBALS['eofnewline']) { $contents[$i] .= constant($nl); if ($old_line !== $contents[$i]) { $modified = true; } } //we have found non-empty line, there is no need to go further break; } $lines_processed++; } } for ($i=0; $i<$contents_len; $i++) { $is_last_line = ($i == $contents_len-1); $line = $contents[$i]; switch ($nl) { case 'CRLF': if (substr($line, -2) !== CRLF) { if (substr($line, -1) === LF || substr($line, -1) === CR) { $line = substr($line, 0, -1) . CRLF; $modified = true; } elseif(strlen($line)) { if (!$is_last_line) { echo "\rERROR: wrong line ending: " . $path . "@line " . ($i+1) . "\n"; return false; } elseif(!$GLOBALS['eofstripwhite']) { $line = $line . CRLF; $modified = true; } } } break; case 'CR': if (substr($line, -1) !== CR) { if (substr($line, -1) === LF) { $line = substr($line, 0, -1) . CR; $modified = true; } elseif(strlen($line)) { if (!$is_last_line) { echo "\rERROR: wrong line ending: " . $path . "@line " . ($i+1) . "\n"; return false; } elseif(!$GLOBALS['eofstripwhite']) { $line = $line . CR; $modified = true; } } } break; case 'LF': if (substr($line, -2) === CRLF) { $line = substr($line, 0, -2) . LF; $modified = true; } elseif (substr($line, -1) !== LF) { if (substr($line, -1) === CR) { $line = substr($line, 0, -1) . LF; $modified = true; } elseif(strlen($line)) { if (!$is_last_line) { echo "\rERROR: wrong line ending: " . $path . "@line " . ($i+1) . "\n"; return false; } elseif(!$GLOBALS['eofstripwhite']) { $line = $line . LF; $modified = true; } } } break; } if ($GLOBALS['eolstripwhite']) { $before = strlen($line); $line = preg_replace("/(?:\x09|\x20)+((?:\r|\n)+)$/", "$1", $line); if (strlen($line) != $before) { $modified = true; } } $new_content .= $line; } if ($GLOBALS['fixbom']) { $before_fixing = $new_content; $ext = strtolower(substr($path, strrpos($path, ".") + 1)); $new_content = stripUtf8Bom( $new_content ); if (!empty($GLOBALS['bom'][$ext])) { $new_content = "\xEF\xBB\xBF" . $new_content; // BOM } if ($new_content != $before_fixing) $modified = true; } if ($modified) { $fp = fopen($path, "wb"); if (!$fp) { echo "\rERROR: couldn't open the " . $path . " file". "\n"; return false; } else { if (flock($fp, LOCK_EX)) { fwrite($fp, $new_content); flock($fp, LOCK_UN); echo "\rMODIFIED to " . $nl . ": " . $path ; if ($GLOBALS['eolstripwhite']) { $saved = $size - strlen($new_content); $GLOBALS['saved_bytes'] += $saved; if ($saved>0) { echo " (saved " . $saved . "B)"; } else if ($saved<0) { echo " (" . abs($saved) . "B added)"; } } echo "\n"; } else { echo "\rERROR: couldn't lock the " . $path . " file". "\n"; return false; } fclose($fp); } } return $modified; } /** * Strip BOM from a string * @param string $data */ function stripUtf8Bom( $data ) { if ( substr( $data, 0, 3 ) == "\xEF\xBB\xBF" ) return stripUtf8Bom(substr_replace( $data, '', 0, 3 )) ; return $data ; } /** * Fix ending lines in all files at given path * * @param string $path */ function fixPath($path) { if (is_file($path)) { $ext = strtolower(substr($path, strrpos($path, "."))); foreach (array('CRLF', 'LF', 'CR') as $nl) { //find out what's the correct line ending and fix file //no need to process further if (in_array($ext, $GLOBALS['extList'][$nl])) { echo "Fixing single file:\n"; fixFile($path, $nl); break; } } } else { $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), true); if ($GLOBALS['maxdepth'] > -1) { $dir = new DepthFilter($dir, $GLOBALS['maxdepth']+1); } $dir = new NegRegexFilter($dir, "/\/(\.svn|CVS)/"); if ($GLOBALS['excluderegex']) { $dir = new NegRegexFilter($dir, $GLOBALS['excluderegex']); } foreach (array('CRLF', 'LF', 'CR') as $nl) { $filtered_dir = new RegexFilter($dir, "/\.(".implode("|", $GLOBALS['extList'][$nl]).")$/i"); $extensions = array(); $j = 0; $progressbar = "|/-\\"; foreach ($filtered_dir as $file) { if (!is_dir($file)) { $basename = basename($file); //skip dot files if ($GLOBALS['nodotfiles']) { if (strpos($basename, ".") === 0) { continue; } } if ($GLOBALS['windows']) { $attribs = trim(substr(shell_exec("attrib " . $file), 0, 5)); //skip archive files if ($GLOBALS['noarchive'] && false !== strpos($attribs, "A")) { print "\r ".$progressbar[$j++ % 4]. " ". str_pad(basename($file), 35, " ", STR_PAD_RIGHT)." SKIPPED"; continue; } //skip hidden files if ($GLOBALS['nohidden'] && false !== strpos($attribs, "H")) { print "\r ".$progressbar[$j++ % 4]. " ". str_pad(basename($file), 35, " ", STR_PAD_RIGHT)." SKIPPED"; continue; } //skip system files if ($GLOBALS['nosystem'] && false !== strpos($attribs, "S")) { print "\r ".$progressbar[$j++ % 4]. " ". str_pad(basename($file), 35, " ", STR_PAD_RIGHT)." SKIPPED"; continue; } } fixFile($file, $nl); print "\r ".$progressbar[$j++ % 4]. " ". str_pad(basename($file), 35, " ", STR_PAD_RIGHT); } } } } } function printHelp() { $help = <<