★ wanayoo — archive 1999 http://www.webtechniques.com/sourcecode/1998/02/lerdorf/lerdorf.lstNouvelle recherche | Portail wanayoo

 
| home | current issue | archives | source code | events | demo express | search | editorial calendar | advertising | customer service | author guidelines | jobs | about

S O U R C E   C O D E / 1998 / 02 / lerdorf / lerdorf.lst

LISTING ONE



CREATE TABLE photos (
  filename varchar(60) NOT NULL,
  album varchar(60),
  text varchar(120),
  PRIMARY KEY (filename)
);

CREATE TABLE users (
        id varchar(16) NOT NULL,
        password varchar(13),
        PRIMARY KEY (id)
);



LISTING TWO


<? 
    $theight = 50;           /* height of thumbnails */

    mysql_pconnect("localhost","nobody","") 
       or die("Unable to connect to SQL server");
    mysql_select_db("rasmus") or die("Unable to select database");
?>
<html><head><title><?echo $title?></title>
</head>
<body bgcolor="#000000" text="#FFCA2F" link="#DEC000" vlink="#ffffff">
<FONT FACE="Verdana, Arial, Helvetica">
<h1 align=center><?echo $title?></h1>
<br>



LISTING THREE



<br>
<center>
<?
if($page!="albums") {
    echo "<a href=/old?u=http%3A%2F%2Fwww.webtechniques.com%2Fsourcecode%2F1998%2F02%2Flerdorf%2F%5C&y=1999"index.php3\">Album List</a>\n";
}
if($page!="upload") {
    echo "<a href=/old?u=http%3A%2F%2Fwww.webtechniques.com%2Fsourcecode%2F1998%2F02%2Flerdorf%2F%5C&y=1999"upload.php3\">Upload a File</a>\n";
}
?>
</center>
<hr>
<font size=-1><i>WebTechniques Online Photo Album</i></font>
</body></html>



LISTING FOUR


<?
    /* 
     * The thumbnail function uses the pnmscale program from
     * the popular Netpbm image manipulation tools package
     * to create a thumbnail of the given image.  If a thumbnail
     * already exists for the image, the function simply returns.
     */
    cfunction thumbnail($filename) {
        global $theight;
        /* Define where to find the various external binaries we need      */
        $djpeg = "/usr/local/bin/djpeg"; /* decompresses a jpeg to ppm     */ 
        $cjpeg = "/usr/local/bin/cjpeg"; /* compreses a ppm to jpeg format */
        $pnmscale = "/usr/local/netpbm/pnmscale"; /* scales a ppm image    */
        $giftopnm = "/usr/local/netpbm/giftopnm"; /* convert a gif to ppm  */
        $ppmtogif = "/usr/local/netpbm/ppmtogif"; /* convert a ppm to gif  */
        $ppmquant = "/usr/local/netpbm/ppmquant"; /* colour quantize a ppm */

        $tdir = dirname($filename)."/thumbnails"; /* thumbnail directory   */
        if(!filetype($tdir)) {
            if(!@mkdir($tdir,0777)) {
                echo "Unable to create $tdir dir - check permissions<br>\n";
                return;
            }
        }
        $tfile = $tdir."/".basename($filename);   /* thumbnail file        */
        if(!filesize($tfile)) {
            if(ereg("\.gif$",$filename)) {  /* Look for .gif extension     */
                exec("$giftopnm $filename | $pnmscale -height $theight ".
                     "| $ppmquant 256 | $ppmtogif -interlace > $tfile");
            } elseif(ereg("\.jpe?g",$filename)) { /* Look for .jpg or .jpeg */
                exec("$djpeg $filename | $pnmscale -height $theight | ". 
                     "$cjpeg -outfile $tfile");
            } else {  /* not a GIF or JPG file */
                return("");
            }
        }
        return($tfile);
    }

    /* If the album variable isn't set, show the album list */
    if(!$album) {
        $title = "Photo Albums";
        $page = "albums";
        include "include/header.inc";

        /* Get a list of the available albums */
        $albums = mysql_query("select filename,album,count(*) as num_albums".
                              " from photos group by album");
        echo "<table align=center>\n";

        /* Loop through each album */
        while($row=mysql_fetch_row($albums)) {
            $row[1] = stripslashes($row[1]);
            $thumb = thumbnail($row[0]);
            if(strlen($thumb)) {
                echo "<tr><td><a href=/old?u=http%3A%2F%2Fwww.webtechniques.com%2Fsourcecode%2F1998%2F02%2Flerdorf%2F%5C&y=1999"$PHP_SELF?album=".urlencode($row[1]);
                $size = getimagesize($thumb);
                echo "\"><img src=/old?u=http%3A%2F%2Fwww.webtechniques.com%2Fsourcecode%2F1998%2F02%2Flerdorf%2F%5C&y=1999"$thumb\" border=0 $size[3]></a></td>";
                echo " <td><font size=+1><b>".$row[1]."</b></font><br>";
                echo "$row[2] photos<br></td></tr>\n";
            }
        }
        echo "</table>\n";
        mysql_free_result($albums);
    } 
    /* Ok, an album was selected, show the thumbnails for this album */
    else {
        $title = stripslashes($album);
        $page = "thumbnails";
        include "include/header.inc";
        $pics = mysql_query("select filename,text from photos where ".
                            "album='$album'");
        $album=stripslashes($album);
        echo "<center>\n";
        while($row=mysql_fetch_row($pics)) {
            if($photo==$row[0]) {
                $desc=stripslashes($row[1]);
            }
            $thumb = thumbnail($row[0]);
            if(strlen($thumb)) {
                echo "<a href=/old?u=http%3A%2F%2Fwww.webtechniques.com%2Fsourcecode%2F1998%2F02%2Flerdorf%2F%5C&y=1999"$PHP_SELF?album=".urlencode($album);
                $size = getimagesize($thumb);
                echo "&photo=$row[0]\"><img src=/old?u=http%3A%2F%2Fwww.webtechniques.com%2Fsourcecode%2F1998%2F02%2Flerdorf%2F%24thumb&y=1999 border=0 $size[3]></a>\n";
            }
            flush(); /* Force web server to flush buffer */
        }    
        echo "</center>\n";

        /* If a picture was selected as well, show the picture */
        if($photo) {
            $size=getimagesize($photo);
            echo "<center><img src=/old?u=http%3A%2F%2Fwww.webtechniques.com%2Fsourcecode%2F1998%2F02%2Flerdorf%2F%5C&y=1999"$photo\" $size[3]><br>$desc</center>\n";
        }
        mysql_free_result($pics);
    }

    include "include/footer.inc";
?>



LISTING FIVE


<?
    cfunction authenticate() {
        Header("WWW-authenticate: basic realm=\"Photo Album\"");
        Header("HTTP/1.0 401 Unauthorized");
        $title="Invalid Login";
        include "include/header.inc";?>
        In order to proceed you will need a valid username/password.
        <?include "include/footer.inc";
        exit;
    }

    if(!isset($PHP_AUTH_USER)) {
        authenticate();
    } else {
        mysql_pconnect("localhost","nobody","") 
            or die("Unable to connect to SQL server");
        mysql_select_db("rasmus") or die("Unable to select database");
        $id=strtolower($PHP_AUTH_USER);
        $query = mysql_query("select * from users where id='$id'". 
                             " and password='$PHP_AUTH_PW'");
        if(!mysql_num_rows($query)) {
            authenticate();
        }
    }
?>



LISTING SIX


<?
    include "include/auth.inc";
    $title = "Upload a Picture";
    $page = "upload";
    include "include/header.inc";
?>
<?if(!$file) { ?>

<form action="/old?u=http%3A%2F%2Fwww.webtechniques.com%2Fsourcecode%2F1998%2F02%2Flerdorf%2Fupload.php3&y=1999" enctype="multipart/form-data" method=POST>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<table border=2>
<tr><th align=right>Album: </th>
<td><select name=album>
<option value="none"> — Create new album —
<?
    $albums = mysql_query("select distinct album from photos");
    while($row = mysql_fetch_row($albums)) {
        echo "<option>".stripslashes($row[0])."\n";
    }
    mysql_free_result($albums);
?>
</select> 
<input type=text name=new_album value="<new album name>"></td></tr>
<tr><th align=right>Filename: </th>
<td><input type=file name=file size=29></td></tr>
<tr><th align=right>Description: </th>
<td><input type=text name=desc size=42></td></tr>
</table>
<input type="submit" value=" Send File ">
</form>

<? } else {
    if(!filetype("uploads")) {
        if(!@mkdir("uploads",0777)) {
            echo "Unable to create uploads directory - check permissions<br>\n";
            exit;
        }
    }
    $dest = "$DOCUMENT_ROOT/".dirname($PHP_SELF)."/uploads/$file_name";
    if(@exec("cp $file $dest")!=0) {
        echo "Unable to create $dest - check permissions<br>\n";
        exit;
    }
    unlink($file);
    if($album=="none") {
        $album = $new_album;
    }
    if(mysql_query("insert into photos values ".
                   "('uploads/$file_name','$album','$desc')")) {
        $size = getimagesize("uploads/$file_name");
        echo "You uploaded this photo:<P>".
             "<img src=/old?u=http%3A%2F%2Fwww.webtechniques.com%2Fsourcecode%2F1998%2F02%2Flerdorf%2F%5C&y=1999"uploads/$file_name\" $size[3]><br>";
        echo stripslashes($desc);
        $page="received";
    } else {
        echo "Unable to insert the photo in the database<P>\n";
    }
}

include "include/footer.inc";
?>



LISTING SEVEN


#!/bin/sh
#
# A simple shell script to insert image entries into the database.
# First argument is the category to add the images to, followed
# by the relative path (relative to the photo album script files)
# of each of the image files.
#
# For example: ./ins.sh Family family/*
#
category=$1
shift
for i in $*; do
mysql db_name -e "insert into photos values ('$i','$category','');"
done




| home | current issue | archives | source code | demo express | events | search | editorial calendar | advertising | customer service | author guidelines | jobs | about




Entire contents copyright 1996-2000 CMP Media Inc.
Read our privacy policy.