[Picfinity-commit] SF.net SVN: picfinity: [48] trunk
Status: Beta
Brought to you by:
espadav8
From: <esp...@us...> - 2007-08-18 20:36:08
|
Revision: 48 http://picfinity.svn.sourceforge.net/picfinity/?rev=48&view=rev Author: espadav8 Date: 2007-08-18 13:35:54 -0700 (Sat, 18 Aug 2007) Log Message: ----------- Add the first parts of the DB backend in (likely to change a fair bit) Modified Paths: -------------- trunk/index.php Added Paths: ----------- trunk/db.inc Added: trunk/db.inc =================================================================== --- trunk/db.inc (rev 0) +++ trunk/db.inc 2007-08-18 20:35:54 UTC (rev 48) @@ -0,0 +1,23 @@ +<?php + $db_host = "www.espadav8.co.uk"; + $db_user = "espadav8_picfin"; + $db_pass = "picfin_pass"; + $db_name = "espadav8_picfinity"; + + $db_connection = mysql_pconnect($db_host, $db_user, $db_pass); + + if (!$db_connection) + { + echo "Couldn't connect to the DB, disabling"; + die(); + } + else + { + $selected_table = mysql_select_db($db_name); + if (!$selected_table) + { + echo "Couldn't select table"; + die(); + } + } +?> Modified: trunk/index.php =================================================================== --- trunk/index.php 2007-08-18 20:34:19 UTC (rev 47) +++ trunk/index.php 2007-08-18 20:35:54 UTC (rev 48) @@ -1,14 +1,14 @@ <?php // site options $site_name = "Picfinity"; - + // here we either get the theme posted in // or if there's one stored in the cookie use that one // otherwise use the default one if ($_POST['theme']) { setcookie("theme", $_POST['theme']); - $theme = $_POST['theme']; + $theme = $_POST['theme']; } else if ($_COOKIE['theme']) { @@ -17,9 +17,9 @@ else { // set the name of the default theme here - $theme = "ajax" ; + $theme = "db" ; } - + if ($_GET['id']) { $post_id = $_GET['id']; @@ -28,40 +28,78 @@ { $post_id = 0; } - + // create thumbnails for images that don't have one already $create_thumbnails = true; - + // create a flat HTML version of the page $create_html = false; - + // create the gallery.xml file (for use by themes $create_xml = true; - + // use the PEAR module to create the XML $use_pear = false; - + // Allow the users to change the theme - // this needs the PHP version of the page + // this needs the PHP version of the page $enable_themes = true; - + // thumbnail sizes $thumbnail_width = 100; $thumbnail_height = 100; + // use the database + $use_db = true; + + if ($use_db) + { + require_once ('db.inc'); + if (!$db_connection) + { + $use_db = false; + } + else + { + if ($selected_table) + { + $result = mysql_query("SELECT image_md5 FROM images"); + if (!$result) + { + echo 'Invalid query: ' . mysql_error() . "\n"; + echo 'Whole query: ' . $query; + $use_db = false; + } + else + { + $current_images = array(); + while ($row = mysql_fetch_row($result)) + { + $current_images[] = $row[0]; + } + mysql_free_result($result); + } + } + else + { + $use_db = false; + } + } + } + // valid images to be shown $valid_image_formats = array('png', 'gif', 'jpg', 'jpeg', 'bmp'); - + // get the layout of the current dir $site_layout = create_folder_layout('.'); - + // create the XML of the layout // if we're not using the PEAR module then the XML will have already been returned if ($use_pear) $xml = array_to_xml($site_layout); else $xml = $site_layout; - + // create the XML file if we've been told to if ($create_xml) { @@ -69,10 +107,10 @@ fwrite($fh, $xml); fclose($fh); } - + // pass the XML to the theme and get the HTML back $html = xml_to_xhtml($xml, ".themes/$theme/$theme.xsl", array('title' => $site_name, 'post_id' => $post_id)); - + // create the flat version if we need to if ($create_html) { @@ -80,27 +118,29 @@ fwrite($fh, $html); fclose($fh); } - + + if ($use_db) + { + mysql_close($db_connection); + } + // echo the HTML to the screen echo $html; - + // this function takes a folder path and creates a layout of the folder and sub folders function create_folder_layout($folder) { - global $valid_image_formats; - global $create_thumbnails; - global $use_pear; - global $enable_themes; - + global $valid_image_formats, $create_thumbnails, $use_pear, $enable_themes, $use_db, $current_images; + // get the name of the folder or '' if it's the root $name = substr($folder, strrpos($folder, '/') + 1); - + // create a folder ID $folder_id = "id" . md5($folder); - + // add the folder to the Array (for use by the PEAR route) $layout = array('name' => $name, 'id' => $folder_id); - + // create the non-PEAR XML if ($name == "") $folder_layout_xml = "<?xml version=\"1.0\"?>" @@ -110,15 +150,15 @@ . "<layout id=\"$folder_id\" name=\"\">\n"; else $folder_layout_xml = "<folder id=\"$folder_id\" name=\"$name\">\n"; - + // get a listing of the files/folder $dh = opendir($folder); while (false !== ($filename = readdir($dh))) { $folder_contents[] = $filename; } - + sort($folder_contents); - + // for each entry while (list(,$folder_entry) = each($folder_contents)) { @@ -132,13 +172,13 @@ else $folder_layout_xml .= create_themes_layout(); } - + // else if it starts with a . ignore it else if (strpos($folder_entry, '.') === 0) { continue; } - + // if it's a directory and doesn't start with a . then create a folder layout for it else if (is_dir($folder . '/' . $folder_entry)) { @@ -148,12 +188,12 @@ else $folder_layout_xml .= create_folder_layout($folder . '/' . $folder_entry); } - + // otherwise check to see if it's an image else { $ext = strtolower(substr($folder_entry, strrpos($folder_entry, '.') + 1)); - + if (in_array($ext, $valid_image_formats)) { // create a thumbnail if we've been told to @@ -164,37 +204,47 @@ $image_id = "id" . md5($folder . '/' . $folder_entry); $layout['image'][] = array('id' => $image_id, 'file' => $folder_entry); $folder_layout_xml .= "\t<image id=\"$image_id\" file=\"$folder_entry\" />\n"; + + if (($use_db) && (!in_array($image_id, $current_images))) + { + $query = "INSERT INTO images (image_md5) VALUES ('$image_id')"; + $result = mysql_query($query); + if (!$result) + { + die (mysql_error()); + } + } } } } - + // close up the hand-coded XML if ($name == "") $folder_layout_xml .= "</layout>\n"; else $folder_layout_xml .= "</folder>\n"; - + // return the correct layout (XML or Array) if ($use_pear) return $layout; else return $folder_layout_xml; } - + function create_themes_layout() { global $use_pear; - + $themes_id = "id" . md5(".themes"); - + // create the themes node $layout = array('name' => '.themes', 'id' => $themes_id); $theme_xml = "<themes id=\"$themes_id\" name=\".themes\">\n"; - + // get the themes $dh = opendir(".themes"); while (false !== ($filename = readdir($dh))) { $folder_contents[] = $filename; } - + sort($folder_contents); - + // for each entry while (list(,$folder_entry) = each($folder_contents)) { @@ -203,7 +253,7 @@ { continue; } - // otherwise if it's a dir assume it's a theme and add it to the list + // otherwise if it's a dir assume it's a theme and add it to the list else if (is_dir(".themes/" . $folder_entry)) { $theme_id = md5(".themes/" . $folder_entry); @@ -211,24 +261,24 @@ $theme_xml .= "\t<theme id=\"$theme_id\" name=\"$folder_entry\" />\n"; } } - + $theme_xml .= "</themes>\n"; - + if ($use_pear) return $layout; else return $theme_xml; } - + function create_thumbnail($folder, $image, $ext) { global $thumbnail_width; global $thumbnail_height; - + if (!is_dir('.thumbs/'.$folder)) { MakeDirectory('.thumbs/'.$folder, 0777); chmod('.thumbs/'.$folder, 0777); } - + $src_img; switch ($ext) { @@ -249,10 +299,10 @@ return; break; } - + $old_x = imageSX($src_img); $old_y = imageSY($src_img); - + if ($old_x > $old_y) { $thumb_w = $thumbnail_width; $thumb_h = $old_y * ($thumbnail_height/$old_x); @@ -265,10 +315,10 @@ $thumb_w = $thumbnail_width; $thumb_h = $thumbnail_height; } - + $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y); - + switch ($ext) { case 'png': @@ -285,13 +335,13 @@ imagebmp($dst_img, '.thumbs/'.$folder.'/'.$image); break; } - + imagedestroy($dst_img); imagedestroy($src_img); - + return; } - + function array_to_xml(&$array, $tag_name = 'image', $headless = false) { error_reporting(E_ALL ^ E_NOTICE); @@ -330,7 +380,7 @@ // return the XML document return $Serializer->getSerializedData(); } - + function xml_to_xhtml($xml, $xsl_file, $params = array()) { $ver = explode( '.', PHP_VERSION ); @@ -362,7 +412,7 @@ return $xsl->transformToXML($doc); } } - + function MakeDirectory($dir, $mode = 0755) { if (is_dir($dir) || @mkdir($dir,$mode)) return TRUE; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |