jargon

/ Shared :: "Linedancer 2/Parse Comments.php"

Oct 27th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.37 KB | Gaming | 0 0
  1. <?php
  2. // Shared :: "Linedancer 2/Parse Comments.php"
  3.  
  4. // Define the regular expressions for path and short containers
  5.  
  6. $pathPattern = '/(\/\*)?(\<\!\-\-)? \{\{ \#(?<capture>[^\r\n]+) \}\} (?(1)\*\/|(?(2)\-\-\>|(*F)))/';
  7.  
  8. $shortPattern = '/(\/\*)?(\<\!\-\-)? \{\{ \#(?<capture>\/\/ [^\r\n]+ :: [^\r\n]+\([^\r\n]+\)\.(css|csv|html|js|json|meta|php|regex)) \}\} (?(1)\*\/|(?(2)\-\-\>|(*F)))/';
  9.  
  10. $filePattern = '/(?<release_root>\/(?<release_name>[^\/]+))(?<project_root>\/(?<project_name>[^\/]+)|)\/(?<sub>scripts)\/(?<format>css|csv|html|js|json|meta|php|regex)\/(?<file>[^\.]+)\.(?<extension>(?!\.|\/|\n|$)[\w]+)/m';
  11.  
  12. $release = $_GET['release'] ?? false;
  13. $game = $_GET['game'] ?? false;
  14.  
  15. // Debugging output for release and game
  16. // echo "Release: " . htmlspecialchars($release) . "<br>";
  17. // echo "Game: " . htmlspecialchars($game) . "<br>";
  18.  
  19. // Blacklist for releases and games
  20. $releaseBlacklist = ['Shared', 'cgi-bin', 'Junk'];
  21. $gameBlacklist = ['Shared', 'Junk'];  // Example of blacklisted games
  22.  
  23. // Initial placeholders for Linedancer's embed state
  24. $placeholders = [
  25.    
  26.     '{{ release root }}' => rtrim("/" . ltrim($release, '/'), '/'),  // Ensure single leading slash
  27.     '{{ project root }}'  => rtrim("/" . ltrim($game, '/'), '/'),   // Ensure single leading slash
  28.     '{{ release name }}'  => $release,                // Example release name
  29.     '{{ project name }}'  => $game,                   // Example project name
  30.     '{{ title }}'         => fancyTitle($release,$game),                      // Placeholder for title to be dynamically generated
  31.     '{{ fav icon }}'      => fancyIcon($release,$game),                      // Placeholder for base64 icon URL
  32. ];
  33.  
  34. $validReleases = [];
  35. $validGames = [];
  36.  
  37. // Reset valid games for each release to avoid duplication
  38. validation($validReleases, $validGames, $releaseBlacklist, $gameBlacklist);
  39.  
  40. function validation(&$validReleases, &$validGames, $releaseBlacklist, $gameBlacklist) {
  41.    
  42.     // Get all valid releases from the document root
  43.     $validReleases = array_filter(array_map('basename', glob("{$_SERVER['DOCUMENT_ROOT']}/*", GLOB_ONLYDIR)), function($release) use ($releaseBlacklist) {
  44.         // Filter out blacklisted releases
  45.         return !in_array($release, $releaseBlacklist);
  46.     });
  47.  
  48.     // Loop through each valid release and get valid games
  49.     foreach ($validReleases as $validRelease) {
  50.         $validGames[$validRelease] = array_filter(array_map('basename', glob("{$_SERVER['DOCUMENT_ROOT']}/{$validRelease}/*", GLOB_ONLYDIR)), function($game) use ($gameBlacklist) {
  51.             // Filter out blacklisted games
  52.             return !in_array($game, $gameBlacklist);
  53.         });
  54.     }
  55. }
  56.  
  57. // Define the isValidCombo function to check valid release/game combinations
  58. function isValidCombo($release, $game, $validReleases, $validGames) {
  59.     // Check if release and game are valid
  60.     return isset($validGames[$release]) && in_array($game, $validGames[$release]);
  61. }
  62.  
  63. // Check if the release and game combination is valid
  64. if (!isValidCombo($release, $game, $validReleases, $validGames)) {
  65.     // Display menu for invalid combo
  66.     echo displayMenu($validReleases, $validGames);
  67.     exit;
  68. }
  69.  
  70. // Continue with the rest of the script for valid combo...
  71.  
  72. // Generate title with fancy project/release names and host
  73.  
  74. // Parse and embed referenced files recursively
  75. function processLines(&$lines, &$index, $depth) {
  76.    
  77.     global $pathPattern, $shortPattern, $filePattern, $placeholders;
  78.  
  79.     $depth++; // Increase depth for each recursion
  80.    
  81.     while ($index < count($lines)) {
  82.        
  83.         $lines[$index] = swapPlaceholders($lines[$index], $placeholders);
  84.        
  85.         $file = null;
  86.         $matches = [];
  87.  
  88.         if (preg_match_all($pathPattern, $lines[$index], $matches, PREG_SET_ORDER) > 0) {
  89.            
  90.             foreach($matches as $match){
  91.                 $file = $match['capture'] ?? null; // Use first match, access by named group if available
  92.             }      
  93.        
  94.         } elseif (preg_match_all($shortPattern, $lines[$index], $matches, PREG_SET_ORDER) > 0) {
  95.            
  96.             foreach($matches as $match){
  97.                 $file = $match['capture'] ?? null; // Use first match, access by named group if available
  98.             }
  99.        
  100.         } else {
  101.        
  102.             $index++;
  103.             continue;
  104.         }
  105.  
  106.         // If $file was successfully captured, proceed to process it
  107.                
  108.         if ($file === null){
  109.             $index++;
  110.             continue;          
  111.         }
  112.            
  113.         if(!is_file("{$_SERVER['DOCUMENT_ROOT']}{$file}")){
  114.            
  115.             notification($file, $extension, $base, $in, $out, $bad, $depth);   
  116.             array_splice(
  117.                 $lines,
  118.                 $index,
  119.                 1,
  120.                 explode("\r\n", $bad)
  121.             );
  122.            
  123.             $index++;
  124.             continue;
  125.         }
  126.                
  127.         // $index++;
  128.                        
  129.         notification($file, $extension, $base, $in, $out, $bad, $depth);
  130.        
  131.         // Insert the included file's contents
  132.         array_splice(
  133.             $lines,
  134.             $index,
  135.             1,
  136.             explode("\r\n", "{$in}\r\n" . file_get_contents("{$_SERVER['DOCUMENT_ROOT']}{$file}") . "\r\n{$out}")
  137.         );
  138.  
  139.         // Recursively process the included file, increasing the index to continue past the inserted content
  140.         processLines($lines, $index, $depth);
  141.        
  142.         $index++;          
  143.     }
  144. }
  145.  
  146. function notification($file,&$extension,&$base,&$in,&$out,&$bad,$depth){
  147.        
  148.    
  149.     global $filePattern;
  150.    
  151.     $fileShort = "{$file}";
  152.    
  153.     if(preg_match_all($filePattern,$file,$matches,PREG_SET_ORDER)>0){
  154.        
  155.         foreach($matches as $match){
  156.            
  157.             // LabDemo2Js (Beta 3 A) :: "Fade Audio.js"
  158.  
  159.             $fileShort = "// ";
  160.             $fileShort .= convertNumbersToRoman($match['project_name']);
  161.             $fileShort .= " (";
  162.             $fileShort .= convertNumbersToRoman($match['release_name']);
  163.             $fileShort .= ") :: \"";
  164.             $fileShort .= $match['file'];
  165.             $fileShort .= ".";
  166.             $fileShort .= $match['extension'];
  167.             $fileShort .= "\"";
  168.            
  169.         }
  170.     }
  171.    
  172.     $extension = pathinfo($file, PATHINFO_EXTENSION);
  173.     $base = basename($file, $extension);
  174.    
  175.     $template = "{{ open }} {{ build }}: {{ file }} (Depth: {{ depth }}) {{ close }}";
  176.    
  177.     $in = $template;
  178.     $out = $template;
  179.     $bad = $template;
  180.    
  181.     $in = str_replace("{{ build }}","Included",$in);
  182.     $out = str_replace("{{ build }}","Concluded",$out);
  183.     $bad = str_replace("{{ build }}","Invalid",$bad);
  184.    
  185.     $in = str_replace("{{ depth }}","{$depth}",$in);
  186.     $out = str_replace("{{ depth }}","{$depth}",$out);
  187.     $bad = str_replace("{{ depth }}","{$depth}",$bad);
  188.    
  189.     $in = str_replace("{{ file }}","{$fileShort}",$in);
  190.     $out = str_replace("{{ file }}","{$fileShort}",$out);
  191.     $bad = str_replace("{{ file }}","{$fileShort}",$bad);
  192.    
  193.     switch($extension){
  194.    
  195.     case '.css':
  196.     case '.js':
  197.                
  198.         $in = str_replace("{{ open }}","/*",$in);
  199.         $out = str_replace("{{ open }}","/*",$out);
  200.         $bad = str_replace("{{ open }}","/*",$bad);
  201.        
  202.         $in = str_replace("{{ close }}","*/",$in);
  203.         $out = str_replace("{{ close }}","*/",$out);
  204.         $bad = str_replace("{{ close }}","*/",$bad);
  205.        
  206.         break;
  207.        
  208.     case '.meta':
  209.     case '.html':
  210.     default:
  211.        
  212.         $in = str_replace("{{ open }}","<!--",$in);
  213.         $out = str_replace("{{ open }}","<!--",$out);
  214.         $bad = str_replace("{{ open }}","<!--",$bad);
  215.        
  216.         $in = str_replace("{{ close }}","-->",$in);
  217.         $out = str_replace("{{ close }}","-->",$out);
  218.         $bad = str_replace("{{ close }}","-->",$bad);
  219.        
  220.         break;
  221.     }
  222.    
  223. }
  224.  
  225. // Swap placeholders in content
  226. function swapPlaceholders($content, &$placeholders) {
  227.     foreach ($placeholders as $placeholder => $replacement) {
  228.         // Normalize slashes to avoid double slashes (//)
  229.         $replacement = rtrim($replacement, '/');  // Remove trailing slashes from replacement
  230.         $content = str_replace($placeholder, $replacement, $content);
  231.     }
  232.     return $content;
  233. }
  234.  
  235. // Initialize title and fav icon
  236.  
  237. $rawtext = ($release!==null?"{$release}":"").($game!==null?"/{$game}":"");
  238.  
  239. if(strpos($rawtext,"/")){
  240.     $release = explode("/",$rawtext)[0];
  241.     $game = explode("/",$rawtext)[1];
  242. }else{
  243.     $release = $rawtext;
  244.     $game = "";
  245. }
  246.  
  247. $placeholders['{{ title }}'] = fancyTitle($release,$game);
  248.  
  249. $placeholders['{{ fav icon }}'] = fancyIcon($release,$game);
  250.  
  251. $placeholders['{{ release name }}'] = $release;
  252. $placeholders['{{ project name }}'] = $game;
  253.  
  254. $placeholders['{{ release root }}'] = "/{$release}";
  255. $placeholders['{{ project root }}'] = "/{$game}";
  256.  
  257. $scriptContent = "<!-- {{ #{{ release root }}/Shared/scripts/html/main.html }} -->";
  258.  
  259. // Array to track already embedded files
  260. $embeddedFiles = [];
  261.  
  262. // Call parser to embed files and replace placeholders
  263.  
  264. $lines = explode("\r\n",$scriptContent);
  265. $index = 0;
  266. $depth = 0;
  267.  
  268. processLines($lines, $index, $depth);
  269.  
  270. $processedContent = implode("\r\n",$lines);
  271.  
  272. // Output the processed content
  273. // echo $processedContent;
  274.  
  275.  
  276. echo $processedContent;
  277.  
  278. ?>
  279.  
Add Comment
Please, Sign In to add comment