1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
<?php
/**
* @package framework
*
* @author Pierre-Emmanuel Périllon
* @license GPL2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
**/
/**
* @see __autoload()
* */
require_once('autoload.inc.php');
/**
* @package framework
* */
class SourceCode
{
const patternRequire = '#require(_once)?[ \t\n]*\(?[ \t\n]*\'(.*)\'[ \t\n]*\)?#';
const patternInclude = '#include(_once)?[ \t\n]*\(?[ \t\n]*\'(.*)\'[ \t\n]*\)?#';
const patternClass = '#(new[ \t\n]+([A-Za-z0-9_]+)[ \t\n]*\()|(([A-Za-z0-9_]+)::)|(extends[ \t\n]+([A-Za-z0-9_]+)[ \t\n]*)|(implements[ \t\n]+([ \t\n,A-Za-z0-9_]+)[ \t\n]*)|(catch[ \t\n]*\([ \t\n]*([A-Za-z0-9_]+))#';
const patternFunction ='#(abstract|final|static|protected|public|private|)[ \t]*(abstract|final|static|protected|public|private|)[ \t]*(abstract|final|static|protected|public|private|)[ \t]*(abstract|final|static|protected|public|private|)[ \t]*function[ \t\n]+(&?[a-z0-9_]+)#i';
const patternException = '#(.+)exception#i';
static $ignore = array('self', 'parent', 'static');
static $param = 'getSourceCode';
static $dir = array('' => '', 'class' => 'include/','interface' => '.int.php' );
static $ext = array('' => '', 'class' => '.class.php', 'interface' => '.int.php' ) ;
static $docUrl = 'http://php.net/manual-lookup.php?pattern=';
static $charset = 'UTF-8';
/**
* merci à Brice qui m'a aidé à écrire la premiere version de cette
* fonction
* @param sourceFileName c'est le nom du fichier
* @uses SourceCode::convertirListeSymboleVersLien()
* @uses SourceCode::patternRequire
* @uses SourceCode::patternInclude
* @uses SourceCode::patternClass
* @static
**/
static function BriceA($source)
{
//detection des requires
preg_match_all (self::patternRequire, $source, $matches);
$liste =self::convertirListeSymboleVersLien( $matches[2], '' );
//detection des includes
preg_match_all (self::patternInclude, $source, $matches);
$liste = array_merge( $liste , self::convertirListeSymboleVersLien($matches[2],'' ) ) ;
//detection des classes
preg_match_all (self::patternClass,$source,$matches);
//new
$liste = array_merge( $liste , self::convertirListeSymboleVersLien($matches[2], 'class') );
//static
$liste = array_merge( $liste , self::convertirListeSymboleVersLien($matches[4], 'class') );
//extends
$liste = array_merge( $liste , self::convertirListeSymboleVersLien($matches[6], 'class') );
//interface
$interface = array();
foreach( $matches[8] as $int )
{
$tmp = explode(',',str_replace( array("\n\t\0 "), '', trim($int)));
$interface = array_merge($interface, $tmp );
}
$liste = array_merge( $liste , self::convertirListeSymboleVersLien( $interface, 'interface' ) );
//catch
$liste = array_merge( $liste , self::convertirListeSymboleVersLien($matches[10], 'class') );
// print_r($matches);
$liste = array_unique($liste );
$suite = NULL;
echo '
<ul style="text-align:left;float:left">';
foreach( $liste as $symbole => $filename)
{
if ($filename === null )
{
echo "\n",'<li>Citation: ', $symbole, '</li>';
}
else if ( strpos($filename,'http://') === 0 )
{
$suite[$symbole] = $filename;
}
else
{
echo "\n",'<li><a href="', self::uri2url($filename),'">', $symbole, '</a></li>';
}
}
echo '</ul>';
if ( $suite !== null )
{
echo '
<ul style="text-align:left;float:left">';
foreach( $suite as $symbole => $filename)
{
echo "\n",'<li><a href="', self::uri2url($filename),'">', $symbole, '</a></li>';
}
echo '</ul>';
}
}
/***
* replace un nom de fichier local en ce qu'il devrait être sur le web
* pet'te que ça marche pas.
* @param filename $file
* @return string
* */
static protected function uri2url( $file )
{
$hide[] = $_SERVER['DOCUMENT_ROOT'];
return str_replace($hide,'http://'.$_SERVER['SERVER_NAME'], $file);
}
/**
* affiche le code source.
* utilise un tableau pour sparer le code et les numros: plus simple.
* on ne peut par couper la chaine obtenue par ligne avec par exemple un explode('<br />',$str)
* parce que sinon on nique la coloration et avec la non fermeture des spans.
* @param string $filename le nom du fichier (pathname afficher)
* @return void affiche a tous les coups et continue
* @uses SourceCode::afficherNavigation()
* @uses SourceCode::BriceA
* @uses SourceCode::afficherCss()
* @uses SourceCode::afficherCode()
* @uses SourceCode::afficherListeFonction()
* @uses SourceCode::$charset
* @static
*/
static function afficher($filename)
{
$file = basename($filename);
$dir = dirname( $filename);
$fileContent= file_get_contents($file);
header('Content-type: text/html ; charset='.self::$charset.';');
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content=" content=; charset=',self::$charset,'" />
<title>Code du script / serveur = ',$_SERVER['SERVER_NAME'],$dir,'/',$file,'</title>
<style type="text/css">';
self::afficherCss();
echo '
</style>
</head>
<body id="top">
<div class="boite">
<h1>',$filename,'</h1>
<p>derniere modification: <span id="lastModified">',date('r',filemtime($file)),'</span>.</p>';
self::afficherNavigation($filename);
self::BriceA($fileContent);
echo '
<div style="clear:both"></div>
</div>';
/* on prépare le code. */
self::afficherCode($file);
echo '
<hr />
<div >licence: GPL 2+, author: Pierre-Emmanuel Périllon.</div>';
self::afficherListeFonction($file);
include 'traceursJavascript.inc.php' ;
echo '
</body>
</html>';
}
/**
* @param filename $file
* @static
* */
static function afficherCode($file)
{
$fileContent= file_get_contents($file);
$max = substr_count ($fileContent,"\n")+1;
$recherche = array('<br />'/*, '</font>', '</span>'*/);
$remplace = array("<br />\n"/*, "</font>\n", "</span>\n"*/);
$str = highlight_string($fileContent,true);
$html= str_replace($recherche, $remplace, $str);
echo '
<table>
<tr>
<th> ? </th>
<th> Code </th>
</tr>
<tr>
<td title="Numéro de ligne" ><code>';
/* afficher les numeros dans une cellule*/
for ( $i=1; $i <= $max ; $i++)
{
echo '
<a id="line',$i,'" href="#line',$i,'">',$i,'</a><br/>';
}
echo '
</code></td>
<td title="le code php" >',
/* le reste dans une autre cellule */
$html,'
</td>
</tr>
</table>';
}
/**
* @static
* */
static function afficherCss()
{
echo '
/*<![CDATA[*/
body{font-size:10pt}
a{text-decoration:none}
a:hover{font-weight:bolder}
.boite{border:black 2px solid;text-align: center}
.boite a{text-decoration:underline}
code{font-size:10pt}
code a{color:gray;text-decoration:none}
code a:hover{text-decoration:underline}
h1{font-family:monospace}
.toolbox{position:fixed;bottom:0;right:0;width:30ex}
.toolbox>.resume{text-align:center;border:1px black solid;background:whitesmoke;margin:0;padding:2pt}
.liste{margin:0;padding:0;}
.liste>li{margin:0;padding:1px;}
.toolbox>.liste{list-style-type:none;height:300px;overflow:scroll;background-color:rgba(245,255,250,0.9);display:none;font-size:smaller;border-style:none;}
.toolbox:hover>.liste{display:block}
.abstract{text-decoration:line-through}
.private{color:red}
.protected{color:blue}
.public{color:green}
.final{background-color:yellow}
/*]]>*/
';
}
/**
* @param filename $file
* @static
* @uses SourceCode::listerFonction()
* @uses SourceCode::afficherPrototype()
* */
static function afficherListeFonction ($filename)
{
$t = self::listerFonction($filename);
$c = count ($t);
echo '
<div class="toolbox">';
if ( $c > 0 )
{
echo '<ul class="liste" >';
foreach( $t as $proto)
{
self::afficherPrototype($proto);
}
echo '</ul>';
}
echo '
<div class="resume" ><a href="#top" title="top">',$c,' méthodes/fonctions</a></div>
</div>';
}
/**
* @param filename $file
* @static
* @uses SourceCode::listerFichiers()
* */
static function afficherNavigation($filename)
{
$t = self::listerFichiers($filename);
echo '<ul style="text-align:left;float:left;width:20em">
<li><a href="',$t[0],'?',self::$param,'">fichier précédant</a></li>
<li><a href="',$t[1],'?',self::$param,'">fichier suivant</a></li>';
if ( file_exists('index.php') )
{
echo '<li><a href="?',self::$param,'">index</a>.</li>';
}
if ( file_exists('../index.php') )
{
echo '
<li><a href="..?',self::$param,'">parent</a></li>';
}
if ( isset($_SERVER['HTTP_REFERER']) )
{
echo '
<li><a href="',htmlspecialchars($_SERVER['HTTP_REFERER']),'" title="',htmlspecialchars($_SERVER['HTTP_REFERER']),'">referer</a></li>';
}
echo '
</ul>';
}
/**
* @static
* @param array $p
* */
static function afficherPrototype($p)
{
echo '<li>',(empty($p['static'])?'->':'::');
echo '<a class="',$p['abstract'],' ',$p['visibility'],' ',$p['final'],
'" href="#line',$p['line'],
'" title="ligne ',
$p['line'],': ',$p['abstract'],' ',$p['visibility'],'" >',$p['function'],'()</a></li>';
}
/**
* liste les fichiers
* @param filename $file
* @static
* @return array
* */
static function listerFichiers($filename)
{
$dir = dirname($filename);
$ref = basename($filename);
$tab = glob($_SERVER['DOCUMENT_ROOT'].$dir.'/*.php');
// print_r($tab);
$r[0] = $r[1] = $ref;
foreach ( $tab as $file )
{
$script = basename($file);
if ( $script === $ref)
{
$r[1] = basename( current ($tab) );
}
$r[0] = $script;
}
return $r;
}
/***
* @param filename $filename
* @static
* @uses SourceCode::simplierPrototype()
* @uses SourceCode::patternFunction
* @return array
* */
static function listerFonction($filename)
{
$f = file ($filename);
$r = array();
foreach( $f as $n => $line )
{
if ( preg_match(self::patternFunction, $line, $matches ) )
{
$x =self::simplierPrototype($matches, $n+1);
// on peut avoir deux fonctions du meme nom dans un meme fichier
// mais tres difficilitement dans une meme ligne
$r[$x['function'].'@'.$x['line']]=$x;
}
}
ksort($r);
return $r;
}
/**
* afficher le code source d'une page au cas ou on en a besoin.
* @return void affiche et termine ou ne fait rien
* @uses SourceCode::afficher()
* @static
*/
static final function obtenir()
{
if( isset($_GET[self::$param]))
{
self::afficher($_SERVER['PHP_SELF']);
exit;
} // if( isset($_GET['getSourceCode']))
}
/**
* @access private
* @param array $m
* @param integer $line
* @return array
* @static
* */
static private function simplierPrototype($m, $line)
{
$r['function'] = $m[5];
$r['line'] = $line;
$r['visibility'] = 'public';
$r['static'] = '';
$r['abstract'] = '';
$r['final'] = '';
$m[0] = null;
$m[5] = null;
foreach( $m as $word )
{
$word = strtolower($word);
if ( $word === 'protected' or $word === 'private' )
{
$r['visibility'] = $word;
}
else if ( $word === 'abstract' )
{
$r['abstract'] = 'abstract';
}
else if ( $word === 'static' )
{
$r['static'] = 'static';
}
else if ( $word === 'final' )
{
$r['final'] = 'final';
}
}
return $r;
}
/**
* @access protected
* @static
* @uses SourceCode::$dir
* @uses SourceCode::$ext
* @uses SourceCode::patternException
* @uses SourceCode::$param
* @uses SourceCode::chercherDocumentation()
* */
static protected function convertirSymboleVersLien($objet, $type)
{
if ( empty( $type ) and file_exists ($objet) )
{
return htmlspecialchars($objet).'?'.self::$param;
}
else if ( ($theFile = loadClassAndInterface($objet, false) ) !== False )
{
return htmlspecialchars($theFile).'?'.self::$param;
}
else
{
if ( preg_match(self::patternException,$objet,$matches) )
{
//si c'est une exception, on s'en contrefiche.
return null;
}
else
{
return self::chercherDocumentation($objet);
}
}
}
/***
* trouve l'url associée à une classe ou interface...
* @access protected
* @static
* @param string $type
* @param array $array
* @return array
* @uses SourceCode::convertirSymboleVersLien()
* */
static protected function convertirListeSymboleVersLien($array,$type)
{
// $array = array_unique($array);
$l = array();
foreach( $array as $objet )
{
$objet = trim($objet);
if ( empty($objet) or in_array( $objet , self::$ignore ) )
{
continue;
}
$l[$type.' '.$objet]= self::convertirSymboleVersLien($objet, $type);
//traiter les
}
return $l;
}
/***
* @static
* @uses SourceCode::$docUrl
*
* */
static function chercherDocumentation($symbole)
{
return self::$docUrl.urlencode($symbole);
}
}//class |
Partager