<?php
/**
* Obtenir le contenu d'un projet internet
* Auteur BlackBear le 09/07/2014
* http://www.fobec.com/php5/1149/obtenir-contenu-projet-internet.html
*/
class ProjectStat {
public $filesinfo = array();
private $path_root = '';
const FILETYPE_PHP = 1; //prog
const FILETYPE_WEB = 2; //web
const FILETYPE_DATA = 3; //dat csv
const FILETYPE_OTHER = 5; // img
/**
* Lancer le scan d'un dossier
* @param type $path_root
* @param type $recursif
*/
public function scan($path_root, $recursif = FALSE) {
if (is_dir($path_root)) {
$this->path_root = $path_root;
$this->scan_internal($path_root, $recursif);
}
}
/**
* Lister le contenu d'un dossier
* @param string $path
* @param type $recursif
*/
private function scan_internal($path, $recursif) {
$dhandle = opendir($path);
if ($dhandle) {
$path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
while (($file = readdir($dhandle)) !== false) {
if ($file != '.' && $file != '..' && substr($file, -1) != '~') {
if (!is_dir($path . $file)) {
if ($this->file_isBinary($path . $file) == FALSE) {
$n = array('path' => $path, 'name' => $file,
'linecount' => $this->fileLineCount($path . $file),
'size' => @filesize($path . $file));
$file_type = $this->filetype($path . $file);
$n['type'] = $file_type;
} else {
$n = array('path' => $path, 'name' => $file,
'linecount' => 0, 'size' => @filesize($path . $file),
'type' => self::FILETYPE_OTHER);
}
$this->filesinfo[] = $n;
} //end is_dir
else if ($recursif == TRUE) {
$this->scan_internal($path . $file, $recursif);
}
}
}
closedir($dhandle);
}
}
/**
* Retourne les statistiques d'un projet internet
* @param type $asString
* @return type
*/
public function stat_type($asString = false) {
$nstat = array(self::FILETYPE_PHP => array('filecount' => 0, 'linecount' => 0, 'filesize' => 0),
self::FILETYPE_WEB => array('filecount' => 0, 'linecount' => 0, 'filesize' => 0),
self::FILETYPE_DATA => array('filecount' => 0, 'linecount' => 0, 'filesize' => 0),
self::FILETYPE_OTHER => array('filecount' => 0, 'linecount' => 0, 'filesize' => 0));
$size = 0;
$sub_dir = array();
$count_file = 0;
$count_code_line = 0;
$count_code_data = 0;
foreach ($this->filesinfo as $file) {
$nstat[$file['type']]['filecount']++;
$nstat[$file['type']]['linecount']+=$file['linecount'];
$nstat[$file['type']]['filesize']+=$file['size'];
$size+=$file['size'];
//String
$sub_dir[] = $file['path'];
$count_file++;
if ($file['type'] == self::FILETYPE_PHP) {
$count_code_line+=$file['linecount'];
} else if ($file['type'] == self::FILETYPE_DATA) {
$count_code_data+=$file['linecount'];
}
}
if ($asString == false) {
return $nstat;
}
//Function toString() de JAVA
else {
$count_subdir = count(array_unique($sub_dir)) - 1;
$buf = 'Analyse du dossier ' . $this->path_root . "n";
$buf.= $count_subdir . ' sous dossier ' . "n";
$buf.= $count_file . ' fichiers ' . "n";
$buf.= $count_code_line . ' lignes de code ' . "n";
$buf.= $count_code_data . ' lignes de donnee ' . "n";
echo $buf;
}
}
/**
* Lister le contenu d'un dossier et des sous-répertoires
* @return type
*/
public function stat_dir() {
$nstat = array();
foreach ($this->filesinfo as $file) {
if (isset($nstat[$file['path']])) {
$nstat[$file['path']]['count']++;
$nstat[$file['path']]['fnames'].="n" . $file['name'];
} else {
$nstat[$file['path']]['count'] = 1;
$nstat[$file['path']]['fnames'] = $file['name'];
}
}
return $nstat;
}
/**
* Compter le nombre de ligne dans un fichier
* @param type $file
* @return int
*/
private function fileLineCount($file) {
$fhandle = @fopen($file, 'rb');
$lines = 0;
if ($fhandle) {
while (!feof($fhandle)) {
$buf = fread($fhandle, 8192);
$lines += substr_count($buf, "n");
}
$lines+=1;
fclose($fhandle);
}
return $lines;
}
/**
* Déterminer le type de fichier à partir de son extension
* @param type $file
* @return type
*/
private function filetype($file) {
$ext = strtolower(strrchr($file, '.'));
//Fichier de programmation PHP
if (in_array($ext, array('.php', '.php3', '.php4', '.php5'))) {
return self::FILETYPE_PHP;
}
//Fichier de page internet
else if (in_array($ext, array('.htm', '.html', '.xml', '.css', '.js'))) {
return self::FILETYPE_WEB;
}
//Fichier de données
else if (in_array($ext, array('.dat', '.csv'))) {
return self::FILETYPE_DATA;
}
//Autre fichier par exemple image
else {
return self::FILETYPE_OTHER;
}
}
/**
* Savoir si le contenu d'un fichier est ASCII ou binaire
* @param type $file
* @return boolean
*/
private function file_isBinary($file) {
$buf = '';
if (file_exists($file)) {
$fhandle = @fopen($file, "r");
if ($fhandle) {
$buf = fread($fhandle, 512);
fclose($fhandle);
}
if (substr_count($buf, "x00") > 0) {
return TRUE;
}
}
return FALSE;
}
}
?>