-
-
Save jlengstorf/5165959 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function retrieveEntries($db, $page, $url=NULL) | |
{ | |
/* | |
* If an entry ID was supplied, load the associated entry */ | |
var_dump($page); | |
var_dump($url); | |
if(isset($url)) { | |
// Load specified entry | |
$sql = "SELECT id,page, title, entry | |
FROM entries | |
WHERE url=? | |
LIMIT 1"; | |
$stmt = $db->prepare($sql); | |
$stmt->execute(array($url)); | |
// Save the returned entry array | |
$e = $stmt->fetch(); | |
// Set the fulldisp flag for a single entry | |
$fulldisp = 1; | |
} | |
/* | |
* If no entry ID was supplied, load all entry titles for the page | |
*/ | |
else | |
{ | |
// Load all entry titles | |
$sql = "SELECT id, page, title, entry, url | |
FROM entries | |
WHERE page=? | |
ORDER BY created DESC"; | |
$stmt = $db->prepare($sql); | |
$stmt->execute(array($page)); | |
$e = NULL; // Declare the variable to avoid errors | |
// Loop through returned results and store as an array | |
while($row = $stmt->fetch()) { | |
if($page=='blog') { | |
$e[] = $row; | |
} | |
else { | |
$e = $row; | |
$fulldisp = 1; | |
} | |
} | |
} | |
/* | |
* If no entries were returned, display a default | |
* message and set the fulldisp flag to display a | |
* single entry */ | |
if(!is_array($e)) { | |
$fulldisp = 1; | |
$e = array( | |
'title' => 'No Entries Yet', | |
'entry' => 'This page does not have an entry yet!' ); | |
} | |
} | |
// Add the $fulldisp flag to the end of the array | |
array_push($e, $fulldisp); | |
return $e; | |
} | |
function adminLinks($page, $url) { | |
// Format the link to be followed for each option | |
$editURL = "/cap_6/admin/$page/$url"; | |
$deleteURL = "/cap_6/admin/delete/$url"; | |
// Make a hyperlink and add it to an array | |
$admin['edit'] = "<a href=\"$editURL\">edit</a>"; | |
$admin['delete'] = "<a href=\"$deleteURL\">delete</a>"; | |
return $admin; | |
} | |
function sanitizeData($data) { | |
// If $data is not an array, run strip_tags() | |
if(!is_array($data)) | |
{ | |
// Remove all tags except <a> tags | |
return strip_tags($data, "<a>"); | |
} | |
// If $data is an array, process each element | |
else | |
{ | |
// Call sanitizeData recursively for each array element | |
return array_map('sanitizeData', $data); | |
} | |
} | |
function makeUrl($title) | |
{ | |
$patterns = array( '/\s+/','/(?!-)\W+/' ); | |
$replacements = array('-', ''); | |
return preg_replace($patterns, $replacements, strtolower($title)); | |
} | |
function confirmDelete($db, $url) | |
{ | |
$e = retrieveEntries($db, '', $url); | |
return<<<FORM | |
<form action="/cap_6/admin.php" method="post"> | |
<fieldset> | |
<legend>Are You Sure?</legend> | |
<p>Are you sure you want to delete the entry "$e[title]"?</p> | |
<input type="submit" name="submit" value="Yes" /> | |
<input type="submit" name="submit" value="No" /> | |
<input type="hidden" name="action" value="delete" /> | |
<input type="hidden" name="url" value="$url" /> | |
</fieldset> | |
</form> | |
FORM; | |
} | |
function deleteEntry($db, $url) { | |
$sql = "DELETE FROM entries WHERE url=? | |
LIMIT 1"; | |
$stmt = $db->prepare($sql); | |
return $stmt->execute(array($url)); } | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Include the necessary files */ | |
include_once 'inc/functions.inc.php'; | |
include_once 'inc/db.inc.php'; | |
// Open a database connection | |
$db = new PDO(DB_INFO, DB_USER, DB_PASS); | |
/* | |
* Figure out what page is being requested (default is blog) | |
* Perform basic sanitization on the variable as well | |
*/ | |
// Figure out what page is being requested (default is blog) | |
if(isset($_GET['page'])) | |
{ | |
$page = htmlentities(strip_tags($_GET['page'])); | |
} | |
else | |
{ | |
$page = 'blog'; | |
} | |
// Determine if an entry URL was passed | |
$url = (isset($_GET['url'])) ? $_GET['url'] : NULL; | |
// Load the entries | |
$e = retrieveEntries($db, $page, $url); | |
// Get the fulldisp flag and remove it from the array | |
$fulldisp = array_pop($e); | |
// Sanitize the entry data | |
$e = sanitizeData($e); | |
?> | |
<!DOCTYPE html | |
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" | |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" | |
content="text/html;charset=utf-8" /> | |
<link rel="stylesheet" href="/cap_6/default.css" type="text/css" /> | |
<title> Simple Blog </title> | |
</head> | |
<body> | |
<h1> Simple Blog Application </h1> | |
<ul id="menu"> | |
<li><a href="/cap_6/blog/">Blog</a></li> | |
<li><a href="/cap_6/about/">About the Author</a></li> | |
</ul> | |
<div id="entries"> | |
<?php | |
// If the full display flag is set, show the entry | |
if($fulldisp==1) | |
{ | |
// Get the URL if one wasn't passed | |
$url = (isset($url)) ? $url : $e['url']; | |
// Build the admin links | |
$admin = adminLinks($page, $url); | |
?> | |
<h2> <?php echo $e['title'] ?> </h2> | |
<p> <?php echo $e['entry'] ?> </p> | |
<p> | |
<?php echo $admin['edit'] ?> | |
<?php if($page=='blog') echo $admin['delete'] ?> | |
</p> | |
<?php if($page=='blog'): ?> | |
<p class="backlink"> | |
<a href="./">Back to Latest Entries</a> | |
</p> | |
<?php endif; ?> | |
<?php | |
} // End the if statement | |
// If the full display flag is 0, format linked entry titles | |
else | |
{ | |
// Loop through each entry | |
foreach($e as $entry) { | |
?> | |
<p> | |
<a href="/cap_6/<?php echo $entry['page'] ?>/<?php echo $entry['url'] ?>"> | |
<?php echo $entry['title'] ?> | |
</a> | |
</p> | |
<?php | |
} // End the foreach loop | |
} // End the else | |
?> | |
<p class="backlink"> | |
<?php if($page=='blog'): ?> | |
<a href="/cap_6/admin/<?php echo $page ?>"> | |
Post a New Entry | |
</a> | |
<?php endif; ?> | |
</p> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment