All pastes #421576 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-04-02 22:10 UTC
rendered paste body
<?php
  define('WEB_ROOT,      "/home/rhodel/htdocs/problems/class12-1");
  define('GALLERY_ROOT',  WEB_ROOT . "/gallery");
  $domain = "russet.wccnet.org";

  // Check to see if user just submitted comments
  //
  if (get_var('submit') == 'Save Comments') {
    // Loop thru the POSt variables looking for -long and -brief     
    foreach ($_POST as $key => $val) {
      $cmd = "";

      $cmd = preg_replace('/^.*-(long|brief)$/', '1', $key);
      if (($cmd == "long") || ($cmd == "brief")) {
        $key = preg_replace('/.jpg-(long|brief)$/', '.txt', $key);

        $val = trim($val); // REmove blank spaces at end of string
        if ($val) {
          $new_data[$key][$cmd] = $val;
        }
      }
    }

    $dir = GALLERY_ROOT . "/" . get_var('dir');


    // Go thru each image and save comments to file.
    foreach ($new_data as $key => $val) {
      $handle = fopen("$dir/$key", "w");
      if ($handle) {
          if (fwrite($handle, $new_data[$key]['brief'] . "\n") == FALSE) {
             print "Failed to write to file: $dir/$key<br>";
          } else if (fwrite($handle, $new_data[$key]['long'] . "\n") == FALSE) {
             print "Failed to write to file: $dir/$key<br>";
          }
      } else {
          print "Failed to open file: $dir/$key<br>";
      }
    }
  }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
      <HEAD>
      </HEAD>
      <BODY BGCOLOR="#ffffff" LEFTMARGIN="0" MARGINHEIGHT="0" MARGINWIDTH="0" TOPMARGIN="0">
         <table><tr>
            <?php Display_Gallery(); ?> 
         </TR></TABLE>
      </BODY>
</HTML>

<?php

############################################
################    get_var
############################################
## Purpose
##    Used to get form data from either a GET or POST
##
function get_var($var, $default = '') {
    if (@$_REQUEST[$var]) return @$_REQUEST[$var];
    return $default;
}

############################################
################    Display_Gallery
############################################
## Purpose
##    
##
function Display_Gallery() {
  $root = GALLERY_ROOT;
  $dir = get_var("dir", ".");
  $image = get_var("image");
  $comment = get_var("comment);
  $path = "$root/$dir";
  $arrow_img = "<img src='images/little_arrow.gif'>";
  $cur_user = @$_SERVER['REMOTE_USER'];
  $is_owner = 0;

        if (in_array(strtolower($cur_user), get_owners($dir))) {
           $is_owner = 1;
        } else {
           $comment = "";
        }

    if ($image) {
      Display_Image("$dir/$image", $dir);
        exit;
    }
    
    if ($dir == ".") {
            print "<P>Welcome to the gallery page.  </P>";
    }
    
    $title = @file("$path/title.txt");
    if (!$title) {
       $title[0] = preg_replace("|^.*/([^/]*)$|", "$1", $dir);
    }

    # Remove the leading "./" in the directory variable
        $dir = preg_replace("|^./|", "", $dir);

    # Space out the "/" in the directory variable for cosmetic purposes
        $title_dir = preg_replace("|/|", " :: ", $dir);

    if ($dir != ".") {
        $title_dir = "gallery :: $title_dir";
    }

    $title_dir_arr = preg_split("/ :: /", $title_dir);

    $num = count($title_dir_arr);
    $i = 0;
    $url = "/gallery.php?comment=$comment&dir=.";
    while ($i < $num-1) {
        if ($i > 0) {
            $url = "$url%2F" . urlencode($title_dir_arr[$i]);
        }

        $title_dir_arr[$i] = "<a href='$url'>$title_dir_arr[$i]</a>";
        $i++;
    }

    $title_dir = implode(" $arrow_img ", $title_dir_arr);

    # Only display the title dir path if we are not at the root
    if ($dir != ".") {
        print "<font size='2'>$title_dir</font><br/><br/>";
    }

       if ($is_owner) {
          if ($comment == 'yes') {
             $comment_link = " <a href='?comment=no&dir=" . urlencode($dir) . "'>[Stop Commenting]</a>";
          } else {
             $comment_link = " <a href='?comment=yes&dir=" . urlencode($dir) . "'>[Add Comments]</a>";
          }
       } else {
         $comment_link = "";
       }

        $dir_array = array();

    print "<font size='5'><strong>$title[0]</strong></font> $comment_link<br/><br/>";
    
        if (is_dir($path)) {
           if ($dh = opendir($path)) {
              while (($file = readdir($dh)) !== false) {
         if (($file[0] != ".") && (is_dir("$path/$file"))) {
              $dir_array[] = $file;
         }
              }
        
              if (count($dir_array) > 0) {
                 sort($dir_array);
                  print "<div class='gallery'>";
                 foreach ($dir_array as $file) {
                    $date = get_lastupdate("$path/$file");
                    $lastmod = date("M d, Y", $date);
                    print "<li><a href='gallery.php?comment=$comment&dir=" . urlencode("$dir/$file") . "'>$file</a> <strong>[$lastmod]</strong>";
                    if ($date > time()-432000) {
                        print " <img align='absmiddle' src='images/new.gif'>";
                    }
                 } // FOREACH
                    print "</div>";
              } // IF COUNT
           } // IF $DH
        } else {
           print "<p>There was an error displaying the gallery.  Please contact Ray Hodel. ";
           print "This is anot a directory: $path</p>;
           exit;
        } // IF IS_DIR

    Display_Thumbs($path, $dir, $comment);


############################################
################    Display_Thumbs
############################################
## Purpose
##    
##
function Display_Thumbs($path, $dir, $comment) {
  global $domain;
  $owners = get_owners($dir);
  $i = 0;
  $columns = 6;
  $align = 'center';

  if ($comment == 'yes') { 
      $columns = 1; 
      $align = 'left';
  }

  $file_array = array();

  if (@is_dir($path)) {
    if ($dh = @opendir($path)) {
      while (($file = @readdir($dh)) !== false) {
    if (($file[0] != ".") && (!is_dir("$path/$file"))) {
       $file_array[] = $file;
    }
      }

      if (count($file_array)) {
        sort($file_array);
      }

      if (count($file_array) > 0) {
        $file_array = preg_grep("/\.gif$/", $file_array);
    sort($file_array);
        if ($comment == 'yes') {
            print "<form method='POST' action='gallery.php'>";
        }
    print "<table width='100%'><tr>";
          foreach ($file_array as $file) {
        $thumb = $file;
                if ($comment == 'yes') {
                  $brief_comment = get_brief_comment($dir, $file, 0);
                  $long_comment = get_long_comment($dir, $file, 0);
                } else {
                  $brief_comment = get_brief_comment($dir, $file, 1);
                  $long_comment = get_long_comment($dir, $file, 1);
                }

                $file = preg_replace("/(.+)gif$/", "$1jpg", $file);

                ### print translate_uri($dir) . "/" . $file . "<br>";
        if (($i++ % $columns) == 0) { print "</tr><tr align='center'>"; }            
        print "<td width='20%' align='$align'>";
                if ($comment == 'yes') {
                   print "<table><tr><td>";
                   print "<img class='thumb' src='gallery/" . translate_uri("$dir/$thumb") . "' alt='$long_comment'>";
                   print "</td><td>";
                   print "<font size='2'>Thumbnail image comment</font><br>";
                   print "<input class='box' name='${file}-brief' value='$brief_comment'><br>";
                   print "<font size='2'>Large image comment</font><br>";
                   print "<input class='box' name='${file}-long' size='80' value='$long_comment'>";
                   print "</td></tr></table>";
                } else {
                   print "<img class='thumb' src='gallery/" . translate_uri("$dir/$thumb") . "' alt='$long_comment'>";
                   print "<div class='brief_comment'>$brief_comment</div></td>\n";
                }
        } // FOREACH
    print "</tr></table>";
        if ($comment == 'yes') {
           print "<input type='hidden' name='dir' value=\"$dir\">";
           print "<input type='submit' value='Save Comments' name='submit'>";
           print "</form>";
        }

      } // IF COUNT
    } // IF $DH
  } else {
    print "<p>There was an error displaying the gallery.  Please contact Ray Hodel. ";
    print "This is anot a directory: $path</p>";
    exit;
  } // IF IS_DIR

}


############################################
################    Display_Image
############################################
## Purpose
##    
##
function Display_Image($image, $dir) {

  $arrow_img = "<img src='/images/little_arrow.gif'>";

  # Space out the "/" in the directory variable for cosmetic purposes
  $title_dir = preg_replace("|/|", " :: ", $dir);

    $title_dir_arr = preg_split("/ :: /", $title_dir);

    $num = count($title_dir_arr);
    $i = 0;
    $url = "http://www.hodel.com/gallery.php?dir=.";
    while ($i < $num) {
        if ($i > 0) {
            $url = "$url%2F" . urlencode($title_dir_arr[$i]);
        }

        $title_dir_arr[$i] = "<a href='$url'>$title_dir_arr[$i]</a>";
        $i++;
    }

    $title_dir = implode(" $little_arrow ", $title_dir_arr);


  $image_fn = preg_replace("|^.*/([^/]*)$|", "$1", $image);

  if ($dir != ".") {
    print "<font size='3'>$title_dir $little_arrow $image_fn</font>";
  }
  print "<p><font size='5'><strong>$title[0]</strong></font></p>";

  print "<table width='100%'>";
  print "<tr><td align='center'><img class='thumb' src='$image' alt='$image'></td></tr>";
  $image = preg_replace("|$dir/|", "", $image);
  $image = preg_replace("|.jpg$|", "", $image);
  print "<tr><td align='center'>$image</td></tr>";
  print "</table>";
}

############################################
################    get_owners
############################################
## Purpose
##    
##
function get_owners($dir) {         
   $dir_arr = preg_split("/\//", $dir);             

   $owner_file = GALLERY_ROOT . "/" . $dir_arr[0] . "/owner.txt";
   $owners = split("\n", chop(@file_get_contents($owner_file)));

   return $owners;
}

############################################
################    get_brief_comment
############################################
## Purpose
##    
##
function get_brief_comment($dir, $file, $display_only=0) {
   $comment_file = preg_replace("/(.+)gif$/", "$1txt", $file);
   $comment_file = GALLERY_ROOT . "/$dir/$comment_file";
   $comment = array();

   if (file_exists($comment_file)) {
      $comment = split("\n", chop(@file_get_contents($comment_file)));
   } else {
      if ($display_only) {
         $comment = split("\.", $file);
      }
   }                               

   if (@$comment[0]) {
      return $comment[0];      
   }                

   return @$comment[1];
}


############################################
################    get_long_comment
############################################
## Purpose
##    
##
function get_long_comment($dir, $file, $display_only=0) {
   $comment_file = preg_replace("/(.+)...$/", "$1txt", $file);
   $comment_file = GALLERY_ROOT . "/$dir/$comment_file";
   $comment = "";

   if (file_exists($comment_file)) {
      $comment = split("\n", chop(@file_get_contents($comment_file)));
      if ($comment[1]) {
        $comment[0] = ""; # Remove the brief comment    
      }

      $comment = implode(" ", $comment);
   } else {
      if ($display_only) {
         $comment = split("\.", $file);
         $comment = $comment[0];
      }                            
   }

   return $comment;            
} 

############################################
################    get_lastupdate
############################################
## Purpose
##    
##
function get_lastupdate($dir) {
  $filelist = Array();
  $path = getcwd();

  if (is_dir($dir)) {
    # System command to get all the directories within a certain directory
    $a = `find "$dir" -type d';
    $filelist = explode ("\n", $a); # Break up the lines and put into an array
  } else if (is_file($dir)) {
    $filelist[] = $dir;
  }

  $lastmod = 0;
  for($i=0;$i<count($filelist);$i++){
    # We don't want a blank file
    if ($filelist[$i] == "") { continue; }

    $date = filemtime($filelist[$i]);
    $lastmod = ($date > $lastmod) ? $date : $lastmod;
    clearstatcache();
  }

  #print "The last modification was on ". date("M d, Y", $lastmod) . "<br>";
  return $lastmod;
}


############################################
################    translate_uri
############################################
## Purpose
##
##
function translate_uri($uri) {
   $parts = explode('/', $uri);    
   for ($i = 0; $i < count($parts); $i++) {
     $parts[$i] = rawurlencode($parts[$i]);
   }             
   return implode('/', $parts);
}   

?>