<?php
	//this function adds a comment to either the english or german comments database
	function AddComment($type)
	{
		$posts_dbs = array( 'news' => 'news_posts',
											'requests' => 'requests_posts',
											'big_requests' => 'bigrequests_posts',
											'great_stuff' => 'greatstuff_posts',
											'austria_trip' => 'austriatrip_posts'
										);
		$comments_dbs = array( 'news' => 'news_comments',
														'requests' => 'requests_comments',
														'big_requests' => 'bigrequests_comments',
														'great_stuff' => 'greatstuff_comments',
														'austria_trip' => 'austriatrip_comments'
													);
		
		$_POST['author'] = addslashes($_POST['author']); //make this form data database safe
		$_POST['authorweb'] = addslashes($_POST['authorweb']);
		$_POST['content'] = addslashes($_POST['content']);
		
		//insert this new comment into the news_comments table
		mysql_query("INSERT INTO {$comments_dbs[$type]} (PostID,Date,Author,AuthorWeb,Content) VALUES ({$_GET['id']},UNIX_TIMESTAMP(NOW()),'{$_POST['author']}','{$_POST['authorweb']}','{$_POST['content']}');");
		
		//this gets the current comment count for the news post, increments it, then updates the value in the news_posts table
		$num_comments = mysql_query("SELECT NumComments FROM {$posts_dbs[$type]} WHERE ID = {$_GET['id']};");
		$num_comments = mysql_fetch_assoc( $num_comments );
		$num_comments['NumComments'] = $num_comments['NumComments'] + 1;
		mysql_query("UPDATE {$posts_dbs[$type]} SET NumComments = {$num_comments['NumComments']} WHERE ID = {$_GET['id']};");
	}
	
	//this function adds an entry to the guestbook database
	function AddGuestbookEntry()
	{
		$_POST['author'] = addslashes($_POST['author']); //make this form data database safe
		$_POST['authorweb'] = addslashes($_POST['authorweb']);
		$_POST['content'] = addslashes($_POST['content']);
		
		//insert this new guestbook entry into the guestbook table
		mysql_query("INSERT INTO guestbook (Date,Author,AuthorWeb,Content) VALUES (UNIX_TIMESTAMP(NOW()),'{$_POST['author']}','{$_POST['authorweb']}','{$_POST['content']}');");
	}
	
	//opens a connection to the database to be used by the rest of the script
	function InitializeDB()
	{
		$db_host = 'mysql17.powweb.com';
		$db_user = 'smramey';
		$db_pass = '7274j3';
		
		//open the connection to the ria database
		$rlinks_db = mysql_connect($db_host, $db_user, $db_pass);

		//make sure a db connection was made, if so, select the ria database
		if( !$rlinks_db ) { die( 'Could not connect to database: ' . mysql_error() ); }
		else { mysql_select_db('resourcelinks') or die( 'Could not select database: ' . mysql_error()); }
	}
	
	//gets the latest news posts and returns an html-ized version of them
	function GetPosts($type,$page)
	{
		$posts_dbs = array( 'news' => 'news_posts',
											'requests' => 'requests_posts',
											'big_requests' => 'bigrequests_posts',
											'great_stuff' => 'greatstuff_posts',
											'austria_trip' => 'austriatrip_posts'
										);
		$files = array( 'news' => 'indextest.php',
											'requests' => 'prayer_updates.php',
											'big_requests' => 'prayer_updates.php',
											'great_stuff' => 'great_stuff.php',
											'austria_trip' => 'austria_trip.php'
										);
		
		$max_posts = 7;
		$one_past_max = $max_posts + 1;
		$num_posts = 1;
		$start_post = $page ? ($page * $max_posts) : 0;
		$q_posts = mysql_query("SELECT * FROM {$posts_dbs[$type]} ORDER BY ID DESC LIMIT $start_post,$one_past_max;");
		 
		while( $num_posts <= $max_posts and $post = mysql_fetch_assoc($q_posts) )
		{ 
		 	$title = stripslashes($post['Title']);
		 	$author = stripslashes($post['Author']);
		 	$content = stripslashes($post['Content']);
		 	$date = date( "D, F d, Y \a\\t h:ia", $post['Date'] );
		 	$num_comments = $post['NumComments'];
		 	$id = $post['ID'];
		 	
		 	$html .="
		 		<div align='left' class='item'>
		 			<div class='item_title'>$title</div>
		 			<div class='item_date'>
		 				by $author on $date
		 			</div>
		 			<div class='item_content'>
		 				$content
		 				<br /> <br />
		 				+ <a href='{$files[$type]}?type=$type&id=$id'>comments [$num_comments]</a>
		 			</div>
		 		</div>
		 	";
		 	
		 	$num_posts++;
		}
		
		if( $start_post > 0 or ($start_post == 0 and $post = mysql_fetch_assoc($q_posts)) )
		{
			if($start_post > 0) { $post = mysql_fetch_assoc($q_posts); }
			
			$html .= "
			<div align='center' class='news_nav'>
			<table cellpadding='4' cellspacing='0' border='0' width='100%'>
				<tr>
			";
	
			$newer_news = $page - 1;
			$older_news = $page + 1;
			
			if($start_post != 0)
			{ $html .= "<td align='left'><a href='{$files[$type]}?type=$type&page=$newer_news'>next</a></td>"; }
			if( $post )
			{ $html .= "<td align='right'><a href='{$files[$type]}?type=$type&page=$older_news'>previous</a></td>"; }
		
			$html .= "
				</tr>
			</table>
			</div>
			";
		}
		
		return($html);
	}
	
	//gets the specified news post and display the comments
	function GetSinglePost($type,$id)
	{
		$posts_dbs = array( 'news' => 'news_posts',
											'requests' => 'requests_posts',
											'big_requests' => 'bigrequests_posts',
											'great_stuff' => 'greatstuff_posts',
											'austria_trip' => 'austriatrip_posts'
										);
		$files = array( 'news' => 'indextest.php',
											'requests' => 'prayer_updates.php',
											'big_requests' => 'prayer_updates.php',
											'great_stuff' => 'great_stuff.php',
											'austria_trip' => 'austria_trip.php'
										);
		$comments_dbs = array( 'news' => 'news_comments',
														'requests' => 'requests_comments',
														'big_requests' => 'bigrequests_comments',
														'great_stuff' => 'greatstuff_comments',
														'austria_trip' => 'austriatrip_comments'
													);
		
		$q_posts = mysql_query("SELECT * FROM {$posts_dbs[$type]} WHERE ID = $id LIMIT 1;");
		$q_comments = mysql_query("SELECT * FROM {$comments_dbs[$type]} WHERE PostID = $id;");
		
		$post = mysql_fetch_assoc($q_posts);
	
	 	$title = stripslashes($post['Title']);
	 	$author = stripslashes($post['Author']);
	 	$content = stripslashes($post['Content']);
	 	$date = date( "D, F d, Y \a\\t h:ia", $post['Date'] );
	 	$num_comments = $post['NumComments'];
	 	$id = $post['ID'];
	 	
	 	$html .="
	 		<div align='left' class='item'>
	 			<div class='item_title'>$title || <a href='{$files[$type]}'>return</a></div>
	 			<div class='item_date'>
	 				by $author on $date
	 			</div>
	 			<div class='item_content'>
	 				$content
	 				<br />
	 				<div class='sub_title'>comments</div>
	 	";

	 	while( $comment = mysql_fetch_assoc($q_comments) )
	 	{
		 	$c_website = stripslashes($comment['AuthorWeb']);
		 	$c_author = stripslashes($comment['Author']);
		 	$c_content = stripslashes($comment['Content']);
		 	$c_date = date( "F d, Y \a\\t h:ia", $comment['Date'] );
		 	
		 	if( $c_website != "" && $c_website != "http://" )
		 	{ $c_author = "<a href='$c_website'>$c_author</a>"; }
		 	
		 	$html .= "
							<div class='comment'>
								<div class='comment_author'>comment by $c_author on $c_date</div>
								<div class='comment_content'>$c_content</div>
							</div>
			";
	 	}

	 	$html .= "
	 	<div class='sub_title'>add a comment</div>
				<form action='{$files[$type]}?type=$type&id=$id' method='post' onsubmit=\"return Check_GB_Fields('author','content');\">
					<input type='hidden' name='cmd' id='cmd' value='addcomment' />
					name:<br />
					<input type='text' size='20' name='author' id='author' />
					<br />
					homepage:<br />
					<input type='text' size='40' name='authorweb' id='authorweb' value='http://' />
					<br />
					comment:
					<br /><textarea rows='4' cols='30' name='content' id='content'></textarea>
					<br />
					<input name='submit' type='submit' id='submit' value='submit' class='submit_button'
						onmouseover=\"javascript:this.style.background = '#999999';\"
						onmouseout=\"javascript:this.style.background = '#333333';\" />
				</form>
	 			</div>
	 		</div>
	 	";
		
		return($html);
	}
	
	//gets guestbook entries from database
	function GetGuestbook()
	{
		$max_entries = 7;
		$one_past_max = $max_entries + 1;
		$num_entries = 1;
		$start_entry = $_GET['page'] ? ($_GET['page'] * $max_entries) : 0;
		$q_entries = mysql_query("SELECT * FROM guestbook ORDER BY ID DESC LIMIT $start_entry,$one_past_max;");
		
		$html = "
			<div class='item' align='left'>
	 			<div class='sub_title'>add a guestbook entry</div>
				<form action='guestbook.php' method='post' onsubmit=\"return Check_GB_Fields('author','content');\">
					<input type='hidden' name='cmd' id='cmd' value='addentry' />
					name:<br />
					<input type='text' size='20' name='author' id='author' />
					<br />
					homepage:<br />
					<input type='text' size='40' name='authorweb' id='authorweb' value='http://' />
					<br />
					message:
					<br /><textarea rows='4' cols='30' name='content' id='content'></textarea>
					<br />
					<input name='submit' type='submit' id='submit' value='submit' class='submit_button'
						onmouseover=\"javascript:this.style.background = '#999999';\"
						onmouseout=\"javascript:this.style.background = '#333333';\" />
				</form>
			</div>
	 		<div class='sub_title'>guestbook entries</div>
		";
		 
		while( $num_entries <= $max_entries and $entry = mysql_fetch_assoc($q_entries) )
		{
		 	$author = stripslashes($entry['Author']);
		 	$authorweb = stripslashes($entry['AuthorWeb']);
		 	$content = stripslashes($entry['Content']);
		 	$date = date( "F d, Y \a\\t h:ia", $entry['Date'] );
		 	
		 	if( $authorweb != "" && $authorweb != "http://" )
		 	{ $author = "<a href='$authorweb'>$author</a>"; }
		 	
		 	$html .="
							<div class='comment'>
								<div class='comment_author'>comment by $author on $date</div>
								<div class='comment_content'>$content</div>
							</div>
		 	";
		 	
		 	$num_entries++;
		}
		
		if( $start_entry > 0 or ($start_post == 0 and $entry = mysql_fetch_assoc($q_entries)) )
		{
			if($start_entry > 0) { $entry = mysql_fetch_assoc($q_entries); }
			
			$html .= "
			<div align='center' class='guestbook_nav'>
			<table cellpadding='4' cellspacing='0' border='0' width='100%'>
				<tr>
			";
	
			$newer_entries = $_GET['page'] - 1;
			$older_entries = $_GET['page'] + 1;
			
			if($start_entry != 0)
			{ $html .= "<td align='left'><a href='guestbook.php?page=$newer_entries'>newer entries</a></td>"; }
			if( $entry )
			{ $html .= "<td align='right'><a href='guestbook.php?page=$older_entries'>older entries</a></td>"; }
		
			$html .= "
				</tr>
			</table>
			</div>
			";
		}
		
		return($html);
	}
	
	//displays a single album
	function DisplayAlbum()
	{
		$root = '../pictures_root';
		$description = addslashes(file_get_contents("$root/{$_GET['album']}/description.txt"));
		
		$album = array(	'Folder' => "{$_GET['album']}",
										'Title' => "{$_GET['album']}",
										'Description' => "$description"
										);
		
		$picture_counter = 0;
		$first_picture = "";
		$thumb_prefix = "thumb_";
		
		print <<<END
				
				<div class='item_title'>{$album['Title']} || <a href='through_the-lens.php'>return to album index</a></div>
				<div class='item_content'>
					{$album['Description']}
					<script language='javascript' type='text/javascript'>
					<!--
						function Change_Pic( picture )
						{
							var location = "$root/{$album['Folder']}";
							if( /long_/.test(picture) )
							{
								Landscape_Picture( location + '/' + picture );
							}
							else
							{
								var main_image = 'main_image';
								var larger_image_HTML = "+ <a href=\"javascript:News_Picture('" + location + "/" + picture + "');\">enlarge picture</a> +";

								document.getElementById("loading_blinker").style.visibility = "visible";
								document.getElementById("larger_picture").innerHTML = larger_image_HTML;
								document[ main_image ].src = location + "/" + picture;
							}
						}
						function Change_Negative( set )
						{
							var new_innerhtml = "<table cellpadding=\"0\" cellspacing=\"10\">\\n<tr>";
							var start_at = set * 10 - 10;
							set = set * 10 - 1;
																
							for( start_at=start_at; start_at<=set; start_at++ )
							{
								if( image[start_at] )
								{
									var temp_img = image[start_at].replace("$thumb_prefix", "" );
									new_innerhtml += "<td><a href=\"javascript:Change_Pic( '" + temp_img + "' );\"><img src=\"$root/{$album['Folder']}/" + image[start_at] + "\" border=\"0\" height=\"60\" /></a></td>";
								}
							}
							
							new_innerhtml += "</tr>\\n</table>";
							
							document.getElementById( "negative" ).innerHTML = new_innerhtml;
							document.getElementById( "current_strip" ).innerHTML = (set + 1) / 10;
						}
						
						var image = new Array();
END;
		
		$album_folder = opendir("$root/{$album['Folder']}");
		while( ($picture = readdir($album_folder)) !== false )
		{
			if( stripos($picture, "$thumb_prefix") !== false )
			{
				print "\n\t\t\t\t\t\t\t\t image[{$picture_counter}] = '{$picture}';";
				$picture_counter++;
			}
			if( $picture_counter == 0 ) { $first_picture = $picture; }
		}
		closedir($album_folder);
		
		print <<<END
						
						//-->
					</script>
					<div class='negative_index' id='negative_index' align='center'>
END;
	
		$num_negatives = ceil($picture_counter / 10);
		$this_negative = 1;
		while( $this_negative < $num_negatives )
		{
			print "
						<a href=\"javascript:Change_Negative('{$this_negative}');\">{$this_negative}</a> -";
				
			$this_negative++;
		}


		print <<<END
		
						<a href="javascript:Change_Negative('{$this_negative}');">{$this_negative}</a>
						| page: <span id='current_strip'>1</span>
					</div>
					<div class='negative' id='negative'>
						<table cellpadding='0' cellspacing='10'>
							<tr>
END;
					
		print <<<END
		
							</tr>
						</table>
					</div>
					<br />
					<span id='loading_blinker' style='color:#990000;'>&gt;&gt;&gt; Loading picture...</span>
					<div align='center' id='larger_picture'>+ <a href="javascript:News_Picture('');">enlarge picture</a> +</div>
					<div align='center'><img src='' name='main_image' class='main_image' id='main_image' onload="document.getElementById('loading_blinker').style.visibility='hidden';" /></div>
					<script language='javascript' type='text/javascript'>
					<!--
						Change_Negative('1');
						Change_Pic(image[0].replace("$thumb_prefix", "" ));
					//-->
					</script>
					
				</div>
END;
	}
	
	//displays all folders as albums in a certain root folder
	function DisplayAlbumIndex()
	{
		$root = '../pictures_root';
		
		$albums = scandir($root);	//get contents of root folder, holding all album folders
		if($albums)
		{
			$album_counter = 0;
			
			print "<table cellpadding='10' cellspacing='0' border='0'>\n";
			print "<tr>\n";
			
			foreach($albums as $folder)	//step through root folder
			{
				if( $folder == '.' or $folder == '..' )
				{ continue; }
				
				if($album_counter == 2)
				{
					print "</tr>\n<tr>\n";
					$album_counter = 0;
				}
				
				$pictures = array();
				$h_album = opendir("$root/$folder");	//open album folder and get two pictures
				if($h_album)
				{
					$c = 0;
					while( ($c < 2) && (false !== ($picture = readdir($h_album))) )
					{
						if(0 === stripos($picture,'thumb_'))
						{
							$pictures[$c] = $picture;
							$c++;
						}
					}
					
					closedir($h_album);
				}
				
				print "<td>\n";
				print "<div class='item_title'>$folder</div>\n";
				print "<div class='item_content'>\n";
				print "<img src='$root/$folder/{$pictures[0]}' height='60' /> <img src='$root/$folder/{$pictures[1]}' height='60' /><br />\n";
				print "<a href='through_the-lens.php?album=$folder'>+ view album</a>\n";
				print "</div>";
				print "</td>\n";
				
				$album_counter++;
			}
			
			print "</tr>\n";
			print "</table>\n\n";
		}
	}
	
	//this function traverses a folder and returns html describing what it finds
	function TraverseFolders( $p_path )
	{
		$rootfolder = opendir( $p_path ); //open folder passed to function
		$back_paths = explode("/",$p_path);
		$folderhtml = "<div class='item_title'>";		//variable that will hold all the HTML about the folder
		$cum_path = '../projects_folders';
		
		foreach($back_paths as $path)
		{
			if( $path == '..' ) { continue; }
			if( $path != 'projects_folders' )
			{ $cum_path .= "/$path"; }
			
			if( $cum_path != $p_path )
			{ $folderhtml .= " :: <a href='studies_projects.php?folder=$cum_path'>$path</a>"; }
		}
		
		$folderhtml .= "</div>\n<div class='item_content'>"; 						
		
			while( $element = readdir( $rootfolder ) ) //one by one, set $element to a file or folder inside the passed folder
			{
				if( ($element != '.') and ($element != '..') ) //ignore current and parent folders
				{
					if( is_dir("$p_path/$element") ) //if $element is a folder, call this function recursively and create HTML for that folder
					{
						clearstatcache();	//make sure each is_dir call is not using cached information from last is_dir check
						$folderhtml .= "
							<div class='sub_title'>
									<img src='../php_graphics/folder.gif'  border='0' />
									<br />
									<a href='studies_projects.php?folder=$p_path/$element'>$element</a>
							</div>\n";
					}
					else
					{ $folderhtml .= "+ <a href='$p_path/$element'>$element</a>\n<br />\n"; }
				}
			}
		
		closedir( $rootfolder );
		
		$folderhtml .= "
			</div>";
		
		return $folderhtml;
	}
?>