Ruzgfpegk

Chromagnon log to HTML

Nov 5th, 2020 (edited)
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1. <?php
  2. // Usage: php log2html.php SessionOrTabs.log
  3. // -> produces "SessionOrTabs.html" in the same folder
  4.  
  5. $log        = $argv[1];
  6. $dest       = preg_replace( '/\.log/', '.html', $log );
  7. $logContent = file_get_contents( $log );
  8.  
  9. // Uncomment the following if the Chromagnon's STDOUT was written to a file through PowerShell (UTF-16LE output, VS UTF-8 for cmd)
  10. //$logContent = iconv( 'UTF-16LE', 'UTF-8', $logContent );
  11.  
  12. $lines      = explode( PHP_EOL, $logContent );
  13.  
  14. $windows = [];
  15.  
  16. $activeWindow = 1; // Default window number (for Tabs, where no Window number is defined)
  17.  
  18. foreach ( $lines as $line ) {
  19.     $matches = [];
  20.    
  21.     if ( preg_match( '/^SetTabWindow - Window: (?<window>\d+?), Tab: (?<tab>\d+)$/D', $line, $matches ) ) {
  22.         // "Session" file
  23.         $activeWindow = $matches['window'];
  24.     } elseif ( preg_match( '/^UpdateTabNavigation - Tab: (?<tab>\d+?), Index: (?<index>\d+?), Url: (?<url>.*)$/D', $line, $matches ) ) {
  25.         // "Session" and "Tabs" files
  26.         // We only keep the latest tab URL (no history), so each new line in the "tab" would overwrite the previous
  27.         $windows[ $activeWindow ][ $matches['tab'] ] = $matches['url'];
  28.     }
  29. }
  30.  
  31.  
  32. // Preparing output
  33. $destFd = fopen( $dest, 'w' );
  34.  
  35. // Output callback setup
  36. function write_to_dest( $buffer ) {
  37.     global $destFd;
  38.     fwrite( $destFd, $buffer );
  39. }
  40.  
  41. // Redirect STDOUT to the file
  42. ob_start( 'write_to_dest' );
  43.  
  44. // Bookmark HTML format reference:
  45. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa753582(v=vs.85)?redirectedfrom=MSDN
  46.  
  47. echo <<<HEADER
  48. <!DOCTYPE NETSCAPE-Bookmark-file-1>
  49. <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
  50. <!-- This is an automatically generated file.
  51. It will be read and overwritten.
  52. Do Not Edit! -->
  53. <Title>Bookmarks</Title>
  54. <H1>Bookmarks</H1>
  55. <DL>
  56. HEADER;
  57.  
  58. foreach ( $windows as $window => $tabs ) {
  59.     echo '<DT><H3 FOLDED ADD_DATE="0">Window ID ' . $window . '</H3>' . PHP_EOL . '<DL><p>' . PHP_EOL;
  60.    
  61.     foreach ( $tabs as $tab => $url ) {
  62.         echo '<DT><A HREF="' . $url . '" ADD_DATE="0" LAST_VISIT="0" LAST_MODIFIED="0">' . $url . '</A>' . PHP_EOL;
  63.     }
  64.    
  65.     echo '</DL><p>' . PHP_EOL;
  66. }
  67.  
  68. echo '</DL>' . PHP_EOL;
  69.  
  70. // Redirect end
  71. ob_end_flush();
  72.  
Add Comment
Please, Sign In to add comment