User Tools

Site Tools


cern:index

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
cern:index [2020/09/18 18:03] iwncern:index [2023/09/29 17:29] iwn
Line 3: Line 3:
 <file bash index.php> <file bash index.php>
 <?php <?php
-$pwd = preg_replace("|^\/((\w)\w+)\/|","/eos/user/$2/$1/www/",$_SERVER["REQUEST_URI"]); +preg_match("|^(\w+)\.|",$_SERVER["SERVER_NAME"],$usr); // get username from hostname $USER.web.cern.ch 
-$pwd = preg_replace("(\?.*)","",$pwd); +$pwd = preg_replace("|^((\w)\w+)|","/eos/user/$2/$1/www/",$usr[1]); // get base path 
-preg_match("|^\/(\w+)\/|",$_SERVER["REQUEST_URI"],$usr); +$pwd = $pwd.preg_replace("|^(?:\/$usr[1]\/)?([^\?]*)(?:\?.*)?$|","$1",$_SERVER["REQUEST_URI"]); // get subdirectory 
-$pwdshort = preg_replace("(.*www/)","$usr[1]/",$pwd); +//$pwd = $pwd.preg_replace("|(?<=\/)\?.*|","",$_SERVER["REQUEST_URI"]); // remove question mark 
-chdir($pwd);+$pwdshort = preg_replace("(.*www\/)","$usr[1]/",$pwd); 
 +if(!chdir($pwd)) { // try to go to directory 
 +  $msg = "Could not find ".$_SERVER["REQUEST_URI"]."..."; 
 +  $msg .= "Could not find $pwd..."; 
 +  print_r($_SERVER); 
 +  echo $msg; throw new Exception($msg); 
 +}
 ?> ?>
 <html> <html>
Line 40: Line 46:
     width: 320px;     width: 320px;
     min-height: 330px;     min-height: 330px;
-    max-height: 380px;+    max-height: 360px;
   }   }
   h1 { color: rgb(40,40,80); }   h1 { color: rgb(40,40,80); }
Line 108: Line 114:
 <div> <div>
 <?php <?php
-$displayed = array(); 
  
 +function matches($filename,$keywords,$regex) { // match filename to filter
 +  if(isset($_GET['match'])){
 +    foreach($keywords as $keyword){
 +      if($regex){
 +        if(!preg_match('/.*'.$keyword.'.*/', $filename))
 +          return false;
 +      }else{
 +        if(!fnmatch('*'.$keyword.'*', $filename))
 +          return false;
 +      }
 +    }
 +  }
 +  return true;
 +}
 +
 +function is_animated($filename) { // check if gif is animated
 +  // https://www.php.net/manual/en/function.imagecreatefromgif.php#104473
 +  if(!($fh = @fopen($filename,'rb')))
 +    return false;
 +  $count = 0;
 +  while(!feof($fh) && $count < 2) {
 +    $chunk = fread($fh, 1024 * 100); //read 100kb at a time
 +    $count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00(\x2C|\x21)#s', $chunk, $matches);
 +  }
 +  fclose($fh);
 +  return $count > 1;
 +}
 +
 +$displayed = array();
 if($_GET['noplots']){ if($_GET['noplots']){
     print "Plots will not be displayed.\n";     print "Plots will not be displayed.\n";
 }else{ }else{
-    $other_exts = array('.pdf','.jpg','.jpeg','.jpg','.cxx','.eps','.root','.txt','.tex','.log','.dir','.info','.psd'); +     
-    $filenames = glob("*.png"); natsort($filenames);+    // Get images to be diplayed 
 +    $disp_filenames = array(); // images to be displayed 
 +    $png_filenames = glob("*.png");
     $keywords = explode(" ",$_GET['match']);     $keywords = explode(" ",$_GET['match']);
-    foreach($filenames as $filename){ +    $regex = isset($_GET['regexp']) && $_GET['regexp']
-        if(isset($_GET['match'])){ +    foreach(glob("*.gif"as $filename){ // get animated GIFs 
-          $matched = True+        if(!matches($filename,$keywords,$regex)continue; 
-          foreach($keywords as $keyword){ +        //$png_filename = str_replace('.gif','.png',$filename); 
-            if(isset($_GET['regexp']) && $_GET['regexp']){ +        $png_filename = preg_replace("~\.gif$~",'.png',$filename); 
-              if(!preg_match('/.*'.$keyword.'.*/', $filename)){ +        if(is_animated($filename))
-                $matched = False; +          if(in_array($png_filename,$png_filenames)) // remove PNG 
-                continue; +            $png_filenames array_diff($png_filenames,array($png_filename))
-              } +        }else if(in_array($png_filename,$png_filenames)){ // give preference to PNG 
-            }else+          continue;
-              if(!fnmatch('*'.$keyword.'*', $filename)){ +
-                $matched False+
-                continue; +
-              } +
-            } +
-          } +
-          if(!$matched) continue;+
         }         }
 +        array_push($disp_filenames,$filename);
 +    }
 +    foreach($png_filenames as $filename){ // get PNGs
 +        if(!matches($filename,$keywords,$regex)) continue;
 +        array_push($disp_filenames,$filename);
 +    }
 +    foreach(glob("*.jpg") as $filename){ // get JPGs
 +        if(!matches($filename,$keywords,$regex)) continue;
 +        $png_filename = preg_replace("~\.jpg$~",'.png',$filename);
 +        $gif_filename = preg_replace("~\.jpg$~",'.gif',$filename);
 +        if(!in_array($png_filename,$disp_filenames) and
 +           !in_array($gif_filename,$disp_filenames)){ // give preference to PNG or animated GIF
 +          array_push($disp_filenames,$filename);
 +        }
 +    }
 +    
 +    // Display, and include other extensions
 +    $other_exts = array('.pdf','.jpg','.png','.gif','.jpeg','.jpg','.eps','.root','.C','.cc','.cpp','.cxx','.txt','.tex','.log','.dir','.info','.psd');
 +    natsort($disp_filenames); // natural sorting (for numbers)
 +    foreach($disp_filenames as $filename){ // get PNGs
         array_push($displayed, $filename);         array_push($displayed, $filename);
         $brfname = str_replace("_","_<wbr>",$filename); //&shy;         $brfname = str_replace("_","_<wbr>",$filename); //&shy;
         print "<div class='pic'>\n";         print "<div class='pic'>\n";
         print "<h3><a href=\"$filename\">$brfname</a></h3>";         print "<h3><a href=\"$filename\">$brfname</a></h3>";
-        print "<a href=\"$filename\"><img src=\"$filename\" style=\"border: none; max-width: 300px; max-height: 360px; \"></a>";+        print "<a href=\"$filename\"><img src=\"$filename\" style=\"border: none; max-width: 300px; max-height: 320px; \"></a>";
         $others = array();         $others = array();
         foreach($other_exts as $ex){         foreach($other_exts as $ex){
-            $other_filename = str_replace('.png', $ex, $filename); +            //$other_filename = str_replace('.png', $ex, $filename); 
-            if(file_exists($other_filename)){ +            $other_filename = preg_replace("~\.(png|gif)$~",$ex,$filename); 
-              array_push($others, "<a class=\"file\" href=\"$other_filename\">[" . $ex . "]</a>"); +            if(strcmp($other_filename,$filename)==0) continue; 
-              if($ex != '.txt') array_push($displayed, $other_filename); +            if(!file_exists($other_filename)) continue; 
-            }+            array_push($others, "<a class=\"file\" href=\"$other_filename\">[" . $ex . "]</a>"); 
 +            if($ex != '.txt') array_push($displayed, $other_filename);
         }         }
         if($others) print "<p>Also as ".implode(', ',$others)."</p>";         if($others) print "<p>Also as ".implode(', ',$others)."</p>";
cern/index.txt · Last modified: 2023/10/04 16:10 by iwn