From b2a7cfb9b152dcad1333b92dde76af3a164af8f6 Mon Sep 17 00:00:00 2001 From: jesse Date: Fri, 27 May 2011 11:57:02 -0700 Subject: Got image posting finished. --- htdocs/new-post.php | 25 +++++++++------ htdocs/postimages.php | 55 ++++++++++++++++++++++++++++++++ htdocs/postings.php | 9 +++++- htdocs/src/Post.inc.php | 69 +++++++++++++++++++++++++++++++++++------ htdocs/src/PostIterator.inc.php | 2 +- htdocs/src/config.inc.php | 5 +-- 6 files changed, 142 insertions(+), 23 deletions(-) create mode 100644 htdocs/postimages.php (limited to 'htdocs') diff --git a/htdocs/new-post.php b/htdocs/new-post.php index b4dd6ad..7112a8c 100644 --- a/htdocs/new-post.php +++ b/htdocs/new-post.php @@ -138,7 +138,8 @@ function finish_post() { ); $error = ''; - $values = array(); + $values = array('title' => '', 'description' => '', + 'email' => '', 'email2' => ''); foreach ($required as $field => $desc) { if (!isset($_POST[$field]) or trim($_POST[$field]) == '') { $error .= "

$desc is a required field.

"; @@ -188,9 +189,11 @@ function handle_images() { function finish_images() { $post = $_SESSION['newpost']; - if (isset($_FILES['images']) and is_array($_FILES['images'])) { - foreach ($_FILES['images'] as $file) { - $post->addImage($file['tmp_name']); + if (isset($_FILES['images'])) { + foreach ($_FILES["images"]["error"] as $key => $error) { + if ($error == UPLOAD_ERR_OK) { + $post->addImage($_FILES['images']['tmp_name'][$key]); + } } } @@ -223,21 +226,25 @@ require_once "src/footer.inc.php"; function render_form($error="") { - global $values; + + $title = isset($_POST['title']) ? $_POST['title'] : ''; + $description = isset($_POST['description']) ? $_POST['description'] : ''; + $email = isset($_POST['email']) ? $_POST['email'] : ''; + $email2 = isset($_POST['email2']) ? $_POST['email2'] : ''; if ($error != '') { echo "
$error
"; } - echo "

"; + echo "

"; echo "

"; echo "

"; + . " cols=\"80\">$description

"; - echo "

"; - echo "

" . "

Your email address will only be visible to our moderators.

"; diff --git a/htdocs/postimages.php b/htdocs/postimages.php new file mode 100644 index 0000000..375e8d7 --- /dev/null +++ b/htdocs/postimages.php @@ -0,0 +1,55 @@ + + * + */ + +require_once "src/base.inc.php"; + +// Make sure we had a path info +if (!isset($_SERVER['PATH_INFO'])) { + errorNotFound(); +} + +// Clean up the id in the path info. +$id = substr($_SERVER['PATH_INFO'], 1); + +if (!is_numeric($id)) { + errorNotFound(); +} + +// Get the post. +$post = Post::getByImage($id); + +if (!$post or + (!isset($_SESSION['currentUser']) and $post->getStage() != 'approved')) { + errorNotFound(); +} + +// Check if file exists. +$file = $CONFIG['uploads'] . "/$id"; + +if (!file_exists($file)) { + echo $file; + errorNotFound(); +} + +// Output the file +$info = getimagesize($file); +header('Content-Type: ' . $info['mime']); +header('Content-Transfer-Encoding: binary'); +header('Content-Length: ' . filesize($file)); +readfile($file); +exit; + +function errorNotFound() { + header("HTTP/1.0 404 Not Found"); + exit; +} + +?> + diff --git a/htdocs/postings.php b/htdocs/postings.php index 5c3dd55..7550e18 100644 --- a/htdocs/postings.php +++ b/htdocs/postings.php @@ -50,9 +50,16 @@ if (isset($_GET['moderate'])) { echo "

". $post->getName() ."

"; -echo "

". $post->getDescription() ."

"; +echo "

Date: ". date('r', $post->getTimestamp()) ."

"; +echo "

". + str_replace("\n", '
', $post->getDescription()) + ."

"; +foreach ($post->getImages() as $imgid) { + echo "

"; +} require_once "src/footer.inc.php"; diff --git a/htdocs/src/Post.inc.php b/htdocs/src/Post.inc.php index 34230b2..c05cb7e 100644 --- a/htdocs/src/Post.inc.php +++ b/htdocs/src/Post.inc.php @@ -13,6 +13,7 @@ require_once "base.inc.php"; class Post { private $info; private $indatabase = false; + private $images; public function __construct($info=null) { @@ -24,6 +25,8 @@ class Post { } else { $this->indatabase = false; } + + $images = null; } public static function getById($id) { @@ -38,6 +41,12 @@ class Post { return Post::getPost($where); } + public static function getByImage($imgid) { + $where = "id=(SELECT post_id FROM image WHERE id='$imgid')"; + + return Post::getPost($where); + } + private static function getPost($where) { $query = "SELECT *, UNIX_TIMESTAMP(created) AS createdts FROM post WHERE $where"; @@ -150,6 +159,10 @@ class Post { return $this->info['created']; } + public function getTimestamp() { + return $this->info['createdts']; + } + public function getEmail() { return $this->info['email']; } @@ -186,24 +199,60 @@ class Post { return $this->info['location']; } - public function addImage($file) { - // TODO: Verify file type + public function getImages() { + if ($this->images == null) { + $this->loadImages(); + } - // TODO: Unique name for file. - $newfile = $GLOBALS['CONFIG']['uploads']; + return $this->images; + } - if (move_uploaded_file($file, $newfile)) { - return true; + public function addImage($file) { + // Verify file type + $info = @getimagesize($file); - } else { + if (!$info) { return false; } + + // TODO Verify image dimensions? + + // Get image id + $db = getDatabase(); + try { + $id = $db->insert('image', array('post_id' => $this->getId())); + $newfile = $GLOBALS['CONFIG']['uploads'] . "/$id"; + + if (move_uploaded_file($file, $newfile)) { + // Invalidate the image cache + $this->images = null; + + return true; + } + + } catch (Cif_Database_Exception $e) { + + } + + return false; + } + + private function loadImages() { + $query = "SELECT id FROM image WHERE post_id='". $this->getId() ."'"; + + $db = getDatabase(); + $imgs = $db->fetchAssocRows($query); + + $this->images = array(); + foreach ($imgs as $img) { + $this->images[] = $img['id']; + } } public function sendValidation() { $email = new Email($this->getEmail()); - $email->setSubject($GLOBAL['CONFIG']['sitetitle'] . " Email Validation"); + $email->setSubject($GLOBALS['CONFIG']['sitetitle'] . " Email Validation"); $url = $GLOBALS['CONFIG']['urlroot'] . '/validate.php?id=' . $this->getSecretId(); @@ -216,7 +265,7 @@ class Post { public function sendAcceptance() { $email = new Email($this->getEmail()); - $email->setSubject($GLOBAL['CONFIG']['sitetitle'] . " Posting Approved"); + $email->setSubject($GLOBALS['CONFIG']['sitetitle'] . " Posting Approved"); $email->appendMessage("Your posting titled ". $this->getName() ." has been approved by our moderation team.\n\n"); @@ -231,7 +280,7 @@ class Post { public function sendRejection($message='') { $email = new Email($this->getEmail()); - $email->setSubject($GLOBAL['CONFIG']['sitetitle'] . " Posting Rejected"); + $email->setSubject($GLOBALS['CONFIG']['sitetitle'] . " Posting Rejected"); $email->appendMessage("Your posting titled ". $this->getName() ." has been rejected by our moderation team.\n\n"); diff --git a/htdocs/src/PostIterator.inc.php b/htdocs/src/PostIterator.inc.php index cced79b..43c1a05 100644 --- a/htdocs/src/PostIterator.inc.php +++ b/htdocs/src/PostIterator.inc.php @@ -42,7 +42,7 @@ class PostIterator implements Iterator { } public function limit($limit) { - $this->limit = limit; + $this->limit = $limit; } public function rewind() { diff --git a/htdocs/src/config.inc.php b/htdocs/src/config.inc.php index 211a798..829c632 100644 --- a/htdocs/src/config.inc.php +++ b/htdocs/src/config.inc.php @@ -19,11 +19,12 @@ $CONFIG = array( 'sitetitle' => 'Foursquare Community', 'email_from' => 'community@myfoursquarechurch.com', - 'urlroot' => 'http://localhost/~jesse/p4s/community/htdocs', + 'urlroot' => '/~jesse/p4s/community/htdocs', 'root' => '/home/jesse/Development/p4square/community/htdocs', + 'uploads' => '/home/jesse/Development/p4square/community/uploads', 'debug' => true, - 'production' => false, + 'production' => true, ); set_include_path(get_include_path() . PATH_SEPARATOR . $CONFIG['root'].'/src'); -- cgit v1.2.3