eZ Publish  [4.0]
ezcheckphptag.php
Go to the documentation of this file.
00001 #!/usr/bin/env php
00002 <?php
00003 //
00004 // Created on: <19-Mar-2004 09:51:56 amos>
00005 //
00006 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00007 // SOFTWARE NAME: eZ Publish
00008 // SOFTWARE RELEASE: 4.0.x
00009 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00010 // SOFTWARE LICENSE: GNU General Public License v2.0
00011 // NOTICE: >
00012 //   This program is free software; you can redistribute it and/or
00013 //   modify it under the terms of version 2.0  of the GNU General
00014 //   Public License as published by the Free Software Foundation.
00015 //
00016 //   This program is distributed in the hope that it will be useful,
00017 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //   GNU General Public License for more details.
00020 //
00021 //   You should have received a copy of version 2.0 of the GNU General
00022 //   Public License along with this program; if not, write to the Free
00023 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00024 //   MA 02110-1301, USA.
00025 //
00026 //
00027 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00028 //
00029 
00030 //include_once( 'lib/ezutils/classes/ezcli.php' );
00031 //include_once( 'kernel/classes/ezscript.php' );
00032 
00033 require 'autoload.php';
00034 
00035 $cli = eZCLI::instance();
00036 $script = eZScript::instance( array( 'description' => ( "eZ Publish PHP tag checker\n\n" .
00037                                                          "Checks for characters before the PHP start tag and after the PHP end tag\n" .
00038                                                          "and sets exit code based on the result\n" .
00039                                                          "PATH can either be a file or a directory\n" .
00040                                                          "\n" .
00041                                                          "ezcheckphptag.php lib" ),
00042                                       'use-session' => false,
00043                                       'use-modules' => true,
00044                                       'use-extensions' => true ) );
00045 
00046 $script->startup();
00047 
00048 $options = $script->getOptions( "[no-print]",
00049                                 "[path+]",
00050                                 array( 'no-print' => "Do not print path for bad files"
00051                                        ) );
00052 $script->initialize();
00053 
00054 if ( count( $options['arguments'] ) < 1 )
00055 {
00056     $script->shutdown( 1, "No files to check" );
00057 }
00058 
00059 $print = true;
00060 if ( $options['no-print'] )
00061     $print = false;
00062 
00063 $ini = eZINI::instance();
00064 
00065 $pathList = $options['arguments'];
00066 $error = false;
00067 $badFiles = array();
00068 
00069 $shellTag = '#!';
00070 $startTag = '<?php';
00071 $shortStartTag = '<?';
00072 $endTag = '?>';
00073 $endNewlineTag = "?>\n";
00074 
00075 foreach ( $pathList as $path )
00076 {
00077     $files = array();
00078     if ( is_dir( $path ) )
00079     {
00080         $files = eZDir::recursiveFindRelative( false, $path, '.php' );
00081     }
00082     else if ( is_file( $path ) )
00083     {
00084         $files[] = $path;
00085     }
00086     else if ( !file_exists( $path ) )
00087     {
00088         if ( $print )
00089         {
00090             $cli->output( $cli->stylize( 'file', $path ) . ": file does not exist" );
00091         }
00092     }
00093     foreach ( $files as $file )
00094     {
00095         $fd = fopen( $file, 'r' );
00096         if ( $fd )
00097         {
00098             $startText = fread( $fd, 5 );
00099             $hasCorrectStart = false;
00100             $hasCorrectEnd = false;
00101             $errorText = array();
00102             if ( substr( $startText, 0, 2 ) == $shellTag )
00103             {
00104                 $hasCorrectStart = true;
00105             }
00106             else if ( $startText == $startTag )
00107             {
00108                 $hasCorrectStart = true;
00109             }
00110             else if ( substr( $startText, 0, 2 ) == $shortStartTag )
00111             {
00112                 $errorText[] = "short start tag used";
00113             }
00114             else
00115             {
00116                 $errorText[] = "does not start with PHP tag";
00117             }
00118             fseek( $fd, filesize( $file ) - 4, SEEK_SET );
00119             $endText = fread( $fd, 4 );
00120             $endText = preg_replace( "/\r\n|\r|\n/", "\n", $endText );
00121             $endText = substr( $endText, strlen( $endText ) - 3, 3 );
00122             if ( substr( $endText, 1 ) == $endTag or $endText == $endNewlineTag )
00123             {
00124                 $hasCorrectEnd = true;
00125             }
00126             else
00127             {
00128                 $errorText[] = "does not end with PHP tag";
00129             }
00130             fclose( $fd );
00131             if ( !$hasCorrectStart or !$hasCorrectEnd )
00132             {
00133                 if ( $print )
00134                 {
00135                     $text = $cli->stylize( 'file', $file );
00136                     if ( count( $errorText ) > 0 )
00137                         $text .= ": " . implode( ", ", $errorText );
00138                     $cli->output( $text );
00139                 }
00140                 $badFiles[] = $file;
00141             }
00142         }
00143         else
00144         {
00145             if ( $print )
00146                 $cli->output( $cli->stylize( 'file', $file ) . ": could not open file" );
00147             $error = true;
00148         }
00149     }
00150 }
00151 
00152 if ( count( $badFiles ) > 0 or $error )
00153     $script->setExitCode( 1 );
00154 
00155 $script->shutdown();
00156 
00157 ?>