scanPhpFiles(); // Étape 2: Analyser includes et $src $this->analyzeIncludesAndSrc(); // Étape 3: Analyser les liens href et JavaScript $this->analyzeLinks(); // Étape 4: Trouver parents, grands-parents et aïeuls $this->findParentRelationships(); // Étape 5: Générer propositions de liens $this->generateLinkPropositions(); // Générer fichier Excel $this->generateExcel(); } private function scanPhpFiles() { $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($this->rootPath, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST ); foreach ($iterator as $file) { if ($file->isFile() && $file->getExtension() === 'php') { $filePath = $file->getPathname(); $fileName = $file->getFilename(); $relativePath = str_replace($this->rootPath, './', $filePath); // Exclure gallerie.php et le dossier Outillage if ($fileName !== 'gallerie.php' && strpos($filePath, $this->excludeDir) === false) { $this->tableau1[] = [ 'cheminComplet' => $relativePath ]; } } } } private function analyzeIncludesAndSrc() { foreach ($this->tableau1 as $file) { $filePath = $this->rootPath . substr($file['cheminComplet'], 2); if (!is_readable($filePath)) { continue; } $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $relativePath = str_replace($this->rootPath, './', $filePath); foreach ($lines as $lineNumber => $line) { // Recherche des includes if (preg_match('/include\s*[\'"]?([^\'"]+\.php)[\'"]?/i', $line, $matches)) { $includedFile = basename($matches[1]); $this->tableau2[] = [ 'fichierContenant' => $relativePath, 'fichierIncludé' => $includedFile, 'numeroLigne' => $lineNumber + 1 ]; } // Recherche des $src= if (preg_match('/\$src\s*=\s*[\'"]?([^\'"]+\.php)[\'"]?/i', $line, $matches)) { $srcFile = basename($matches[1]); $this->tableau2[] = [ 'fichierContenant' => $relativePath, 'fichierIncludé' => $srcFile, 'numeroLigne' => $lineNumber + 1 ]; } } } } private function analyzeLinks() { foreach ($this->tableau1 as $file) { $filePath = $this->rootPath . substr($file['cheminComplet'], 2); if (!is_readable($filePath)) { continue; } $lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $relativePath = str_replace($this->rootPath, './', $filePath); $fileName = basename($filePath); foreach ($lines as $lineNumber => $line) { // Recherche des href if (preg_match('/href\s*=\s*[\'"]([^\'"]+)[\'"]/i', $line, $matches)) { $link = trim($matches[1]); if ($this->isValidLink($link)) { $this->tableau3[] = [ 'cheminDufichierComplet' => $relativePath, 'fichierContenantLeLien' => $fileName, 'Lien' => $link, 'N°Ligne' => $lineNumber + 1, 'fichierParent' => '', 'grandParent' => '', 'ailleul' => '', 'proposition' => '' ]; } } // Recherche des location JavaScript if (preg_match('/(?:document|top|self|parent)\.location\.(?:href\s*=\s*[\'"]([^\'"]+)[\'"])/i', $line, $matches)) { $link = trim($matches[1]); if ($this->isValidLink($link)) { $this->tableau3[] = [ 'cheminDufichierComplet' => $relativePath, 'fichierContenantLeLien' => $fileName, 'Lien' => $link, 'N°Ligne' => $lineNumber + 1, 'fichierParent' => '', 'grandParent' => '', 'ailleul' => '', 'proposition' => '' ]; } } } } } private function isValidLink($link) { return !empty($link) && !preg_match('/^(http|#|mailto|tel)/i', $link); } private function findParentRelationships() { // Première passe: trouver fichierParent foreach ($this->tableau3 as &$row) { $fileToFind = $row['fichierContenantLeLien']; foreach ($this->tableau2 as $include) { if ($fileToFind === $include['fichierIncludé']) { $row['fichierParent'] = $include['fichierContenant']; break; } } } // Deuxième passe: trouver grandParent $updated = true; while ($updated) { $updated = false; foreach ($this->tableau3 as &$row) { if (!empty($row['fichierParent']) && empty($row['grandParent'])) { $fileToFind = basename($row['fichierParent']); foreach ($this->tableau2 as $include) { if ($fileToFind === $include['fichierIncludé']) { $row['grandParent'] = $include['fichierContenant']; $updated = true; break; } } } } } // Troisième passe: trouver ailleul $updated = true; while ($updated) { $updated = false; foreach ($this->tableau3 as &$row) { if (!empty($row['grandParent']) && empty($row['ailleul'])) { $fileToFind = basename($row['grandParent']); foreach ($this->tableau2 as $include) { if ($fileToFind === $include['fichierIncludé']) { $row['ailleul'] = $include['fichierContenant']; $updated = true; break; } } } } } } private function generateLinkPropositions() { foreach ($this->tableau3 as &$row) { $referencePath = !empty($row['ailleul']) ? $row['ailleul'] : (!empty($row['grandParent']) ? $row['grandParent'] : (!empty($row['fichierParent']) ? $row['fichierParent'] : '')); if (empty($referencePath)) { continue; } $link = $row['Lien']; $linkSlashCount = substr_count($link, '/'); $refSlashCount = substr_count($referencePath, '/'); if ($linkSlashCount === $refSlashCount) { $row['proposition'] = './'; } else { $diff = $linkSlashCount - $refSlashCount; $prefix = $diff > 0 ? str_repeat('../', $diff) : './'; $row['proposition'] = $prefix . ltrim($link, '/'); } } } private function generateExcel() { $spreadsheet = new Spreadsheet(); // Tableau 1 $sheet1 = $spreadsheet->getActiveSheet(); $sheet1->setTitle('Tableau 1'); $sheet1->fromArray(['Chemin Complet'], null, 'A1'); $sheet1->fromArray($this->tableau1, null, 'A2'); $sheet1->freezePane('A2'); $sheet1->getColumnDimension('A')->setAutoSize(true); // Tableau 2 $sheet2 = $spreadsheet->createSheet(); $sheet2->setTitle('Tableau 2'); $sheet2->fromArray(['Fichier Contenant', 'Fichier Includé', 'N° Ligne'], null, 'A1'); $sheet2->fromArray($this->tableau2, null, 'A2'); $sheet2->freezePane('A2'); foreach (range('A', 'C') as $col) { $sheet2->getColumnDimension($col)->setAutoSize(true); } // Tableau 3 $sheet3 = $spreadsheet->createSheet(); $sheet3->setTitle('Tableau 3'); $headers = [ 'Chemin Du Fichier Complet', 'Fichier Contenant Le Lien', 'Lien', 'N° Ligne', 'Fichier Parent', 'Grand Parent', 'Ailleul', 'Proposition' ]; $sheet3->fromArray($headers, null, 'A1'); $sheet3->fromArray($this->tableau3, null, 'A2'); $sheet3->freezePane('A2'); foreach (range('A', 'H') as $col) { $sheet3->getColumnDimension($col)->setAutoSize(true); } $writer = new Xls($spreadsheet); $writer->save('analyseDesLiensGrok.xls'); } } try { $analyzer = new LinkAnalyzer(); $analyzer->runAnalysis(); } catch (Exception $e) { echo "Erreur: " . $e->getMessage(); } ?>