ruạṛ
<?php /** * Blog article / output object * @version 0.10.1 * @author Robert Urquhart <programmer@activatedesign.co.nz> * @package WEP-CMS */ class Blog { /** * define variables matched to to database fields * may not have identical names, see $this->load_from_data for conversions */ public $article_id , $page_id , $name , $path , $date , $title , $summary , $content , $active , $visited ; /** * from other database tables * @var string $topic name of container page * @var array $images holds single feature image */ public $topic; public $images = array(); /** * static functions for loading articles and outputting html */ /** * get all the blog posts for a particular page (topic) * * @param int $page_id or 0 to fetch all * @param int $limit maximum number to return * @param int $start where to start * @param int $active (bool) active flag or 0 for all * * @return array of Blog objects */ static public function from_page($page_id=0, $limit=ARTICLES_PER_PAGE, $start=0, $active=1) { $articles = array(); $query = "select * from blog where 1 = 1"; $query .= ($page_id) ? " and page_id = '$page_id'" : ''; $query .= ($active) ? " and active = '1'" : ''; $query .= " order by date desc"; $query .= ($limit) ? " limit $start, $limit" : ''; $result = mysql_query($query); if($result) { while($r = mysql_fetch_assoc($result)) { $a = new Blog; $a->load_from_data($r); $articles[$r['article_id']] = $a; unset($a); } } else { die($query . mysql_error()); //debugging } return $articles; } /** * output html for the most recent blog posts [for a particular page (topic)] * * * @param int $n maximum number to return * @param int $page_id or 0 to fetch all * * @return string */ static public function output_most_recent($n=RECENT_ARTICLES, $page_id=0, $start=0) { $articles = Blog::from_page($page_id, $n, $start); $html = '<div class="blog-summary">'."\n\t"; $i = $start; foreach($articles as $a) { $date = new datetime($a->date); //appending $i puts the target article at the top of the sidebar when loaded and ensures a fresh supply below it $href = $a->path.'?start='.$i; ; $i++; $html .= '<div>'."\n\t\t"; $html .= (BLOG_POST_IMAGE && !empty($a->images)) ? '<a href="'.$href.'"><img src="'.$a->images[0]['thumbnail'].'" width="'.BLOG_FEATURE_THUMBNAIL_WIDTH.'" height="'.BLOG_FEATURE_THUMBNAIL_HEIGHT.'" alt="'. $a->title .'" /></a>' . "\n\t\t" : ''; $html .= '<p class="blog-topic">'.$a->topic.'</p>' . "\n\t\t" . '<h3 class="blog-title"><a href="'.$href.'">'. $a->title .' <span class="blog-date">'.$date->format('M jS').'</span></a></h3>'."\n\t\t"; $html .= (BLOG_POST_SUMMARY) ? '<p class="blog-summary">'.nl2br($a->summary).'</p>'."\n\t\t" : ''; $html .= '</div>'."\n\t"; } $html.= "\n".'</div>'; return $html; } /** * Output html for a specific blog post * * @param int $article_id * * @return string */ static public function output_article($article_id, $page_id = 0) { $a = new Blog($article_id); if(!$a->article_id) { global $page_id; return '<p class="message">Sorry, the article you requested is unavailable.</p>'.Blog::displayTopic($page_id); } //else /** * @var datetime $date * @var string $articleNav repeated at top and bottom of page */ $date = new datetime($a->date); $articleNav = Blog::article_nav($a->date,$a->page_id); $html = "\t".'<div class="blog-article">'."\n\t\t" . $articleNav ."\n\t\t"; $html .= (BLOG_POST_IMAGE && !empty($a->images)) ? '<img class="blog-feature" src="'.$a->images[0]['image'].'" width="'.$a->images[0]['width'].'" height="'.$a->images[0]['height'].'" alt="'. $a->title .'" />' . "\n\t\t" : ''; $html .= '<h1>' . $a->title . "</h1>\n\t\t" . '<p class="blog-posted">'. $date->format('jS \o\f F\, Y') . '</p>' . "\n\t\t" . db_content($a->content) . "\n\t\t" . $articleNav . "\n" . '</div><!-- end blog-article -->' ."\n"; return $html; } /** * Output html for a specific blog post navigation * * @param string $date * * @return string */ static public function article_nav($date, $page_id=0) { if(!$page_id){ // try and get it from context global $page_id; } $query = "SELECT * " . "FROM blog " . "WHERE active = '1' " . "AND page_id = '" . $page_id . "' " . "AND date < '" . $date . "' " . "ORDER BY date DESC " . "LIMIT 1"; if(!$result = mysql_query($query)) { return 'Error retrieving navigation items'; } $html = ''; $html .= '<div class="articleNav">'."\n"; if(mysql_num_rows($result) > 0) { $article = mysql_fetch_assoc($result); $html .= '<div class="previous">'."\n"; $html .= '<a href="' . $article['path'] . '">' . $article['name'] . '</a>'."\n"; $html .= "</div>\n"; } $query = "SELECT * " . "FROM blog " . "WHERE active = 1 " . "AND page_id = " . $page_id . " " . "AND date > '" . $date . "' " . "ORDER BY date ASC " . "LIMIT 1"; if(!$result = mysql_query($query)) { return "Error retrieving articles"; } if(mysql_num_rows($result) > 0) { $article = mysql_fetch_assoc($result); $html .= '<div class="next">'."\n"; $html .= '<a href="' . $article['path'] . '">' . $article['name'] . "</a>\n"; $html .= "</div>\n"; } $html .= "</div>\n"; return $html; } /** * Output html for topic navigation * * @param string $date * * @return string */ static public function topic_nav($n = ARTICLES_PER_PAGE, $page_id=0, $start=0) { $query = "SELECT COUNT(*) AS count " . "FROM blog " . "WHERE active = '1'"; $query .= ($page_id) ? " AND page_id = '" . $page_id ."'" : ''; if(!$result = mysql_query($query)) { return 'Error retrieving page count'; } $count = mysql_fetch_assoc($result); $totalPages = (ceil($count['count'] / $n)); $currentPage = ($start) ? floor($n/$start)+1 : 1; $html = "\t".'<div class="pageNav">'."\n\t\t"; if($totalPages > 1 || $start > 0) { if($currentPage < $totalPages ) { $html .= '<div class="previous">'; $html .= '<a href="?start=' . ($start + $n) . '">Earlier Articles</a>'; $html .= "</div>\n\t\t"; } if($currentPage > 1 ) { $html .= '<div class="next">'; $html .= '<a href="?start=' . max(0, $start - $n) . '">Later Articles</a>'; $html .= "</div>\n\t\t"; } } $html .= "</div>\n\t"; return $html; } /** * alter visit count */ static public function increment_views($article_id = 0, $n=1) { $article_id = (int) $article_id; $n = (int) $n; mysql_query("update blog set visited = visited + $n where article_id = '$article_id'"); } /** * public record handling functions */ /** * object construction function * @param int $id unique database record id */ public function __construct($id=0) { /* * set miminum visibility */ $this->article_id = 0; $this->page_id = 0; $this->active = 0; /** * if no id supplied simply prepare product to be populated from dataset eg in category object */ if(!$id) { return; } /** * else get page data * assumes database connection already established at global level * @var resource $record mysql dataset */ $record = mysql_query("select * from blog where article_id = '$id' "); if($record && mysql_num_rows($record)==1) { $this->load_from_data(mysql_fetch_assoc($record)); mysql_free_result($record); // clean up } return; } /** * populate object - can be done on init or manually * @param array $d data; */ public function load_from_data($d=array()) { if(!empty($d)) { $this->article_id = $d['article_id']; $this->page_id = $d['page_id']; $this->name = $d['name']; $this->path = $d['path']; $this->date = $d['date']; $this->title = $d['title']; $this->summary = $d['summary']; $this->content = $d['content']; $this->active = $d['active']; $this->visited = $d['visited']; } $this->topic = select_one('page_data','name','page_id',$this->page_id); $this->images = array(); //empty $images = mysql_query("select * from image_data where image_type = 'blog-image' and container_id='".$this->article_id ."' order by image_position"); if($images && mysql_num_rows($images)>0){ while ($i = mysql_fetch_assoc($images)) { $i['image'] = $i['image_path'].$i['image_filename']; $i['thumbnail'] = $i['image_path'].THUMBNAIL_PREFIX.$i['image_filename']; $this->images[] = $i; } } return; } /** * add record to the database and create stub file * @return bool */ public function create() { global $message; /** * get from POST */ $name = $this->name = clean_plain_data($_POST['article_name']); $page_id = $this->page_id = (int) $_POST['page']; $parent_path = select_one('page_data', 'path', 'page_id', $page_id); $path = dir_name($name); /** * validate required fields */ $m = ''; if($name=='') { $m .= 'Please enter an article name <br />'; } if(!$page_id) { $m .= 'Please select a blog section <br />'; } if(!$parent_path) { getout('No blog section defined: ' . $_POST['page'], '../blog.php'); exit; } if($m != '') { $message .= $m; return false; } /** * assemble directory path */ $path = $this->path = $parent_path . $path; /** * add to database * @var string $fields * @var string $values */ $query = "INSERT INTO blog(name, date, title, summary, content, path, page_id) " . "VALUES('" . $name . "', NOW(), '" . $name . "', '', '', '" . $path . "/', " . $page_id . ")"; if(!mysql_query($query)) { $message .= 'There was an error creating the article.'; return false; } // else $article_id = $this->article_id = mysql_insert_id(); //$message .= 'Blog article record created <br />'; /** * create stub file * there will be a page_id because we checked for the record getting $parent_path */ $contents = array( 'page_id' => $page_id , 'article_id' => $article_id ); if(!create_stub_file($path, $contents)) { //message should have been added by create_stub_file() $this->remove_from_database(); return false; } /** * directory for images * @var string $dir */ $dir = DOC_ROOT.'/resources/images/blog/'.$article_id; if(!@mkdir($dir, 0755, true)) { $message .= 'There was an error creating the image path <br />'; $this->remove_from_database(); clean_dir(DOC_ROOT.$this->path, true); return false; } chmod($dir, DIR_PERMS); $message .= 'Article created successfully.<br />'; return true; } /** * update record in the database - content * @return bool */ public function update() { global $message; /** * san check */ if(!$this->article_id) { $message .= 'Invalid article <br />'; return false; } /** * get from POST */ $title = clean_plain_data($_POST['article_title']); $date = clean_plain_data($_POST['date']) . " " . clean_plain_data($_POST['time']); $summary = clean_plain_data($_POST['summary']); $content = clean_html_data($_POST['content']); $active = isset($_POST['publish']) ? 1 : 0; $query = "UPDATE blog " . "SET title = '" . $title . "', date = '" . $date . "', summary = '" . $summary . "', content = '" . $content . "', active = '" . $active . "' " . "WHERE article_id = '" . $this->article_id ."'"; if(!mysql_query($query)) { $message .= "There was an error updating the article. <br /> $query" . mysql_error(); return false; } //else $message = "Article updated successfully. <br />"; if(count($this->images)) { $this->update_feature_image(); } else { $this->add_feature_image('feature_image'); } return true; } /** * remove everything * @param $subpage flag to say if this is being called from $this->delete_subpages (toggles message) * @return bool */ public function delete() { global $message; /** * san check */ if(!is_numeric_id($this->article_id,0)) { $message .= 'Unable to delete article: invalid id <br />'; return false; } /** * remove (delete) images */ if(!$this->remove_images(true)) { $message .= 'Notice - did not remove images directory <br />'; //not a critical error - continue } /** * delete stub file and directory */ clean_dir(DOC_ROOT.$this->path,true); if(file_exists($this->path)) { $message .= 'Error: unable to remove file <br />'; return false; } /** * remove (delete) from database */ if(!$this->remove_from_database()) { return false; } return true; } /** * private record handling functions */ /** * add an image * @var string $upload form field * @return bool * @todo we probably need to tidy up all the image functions into one or two - move databasing into upload_image() function */ private function add_feature_image($upload) { global $message; /** * san check */ if(!$upload || !is_numeric_id($this->article_id)) { return false; } /** * make sure we have an image directory */ $path = '/resources/images/blog/'.$this->article_id; $dir = DOC_ROOT.$path; if(!is_dir($dir)) { mkdir($dir,0755,true); chmod($dir, DIR_PERMS); } $dir .= '/'; /** * auxilary data * @var string $link * @var string $caption * @var string $position */ $position = is_numeric_id($_POST[$upload.'_position'],false); if($position === false) { $position = select_one('image_data','max(image_position)',"concat_ws(':',image_type,container_id)",'blog-image:'.$this->article_id)+10; } /** * @var array $dim config for image function * @var int $img_id */ $dim = array('w'=>BLOG_FEATURE_IMAGE_WIDTH,'h'=>BLOG_FEATURE_IMAGE_HEIGHT, 'tw'=>BLOG_FEATURE_THUMBNAIL_WIDTH, 'th'=>BLOG_FEATURE_THUMBNAIL_HEIGHT, 'orient'=>''); $img_id = 0; //valid img_id which will match no records for position update if($_FILES[$upload]['name']) { if(!$insert = mysql_query("insert into image_data (image_type, container_id, image_path, image_position, title, link, active) values ('blog-image', '$this->article_id', '$path/', '$position', '', '', '1')")) { $message .= 'Image upload database error. '.mysql_error().'<br />'; return false; } //else $img_id = mysql_insert_id(); /** * @var string $suff * @var string $filename */ $suff = image_suffix($_FILES[$upload]['tmp_name']); $filename = 'feature'.$img_id.$suff; $message .= upload_image($upload,$dim,$dir,$filename,true); //upload and generate thumbnail if(!file_exists($dir.$filename)) { // appropriate message should have been generated by upload_image() //$message .= $dir.$filename.' File exists fail <br />'; mysql_query("delete from image_data where image_id = '$img_id'"); return false; } else { list($w, $h) = @getimagesize($dir.$filename); if(!$update = mysql_query("update image_data set image_filename = '$filename', width = '$w', height = '$h' where image_id = '$img_id'")) { $message .= 'There was a problem recording the image. Please try again.'.mysql_error().'<br />'; unlink(($dir.$filename)); mysql_query("delete from image_data where image_id = '$img_id'"); return false; } } //$message .= ucfirst(str_replace('_', ' ', $upload)).' uploaded <br />'; //included in upload_image() return true; } //else //$message .= 'File fail <br />'; return false; } /** * update image * @return bool */ private function update_feature_image() { global $message; //$message .= 'Updating images<br />'; //debugging; /** * san check */ if(!is_numeric_id($this->article_id)) { return false; } /** * make sure we have a product image directory */ $path = '/resources/images/blog/'.$this->article_id; $dir = DOC_ROOT.$path; if(!is_dir($dir)) { mkdir($dir,0755,true); chmod($dir, DIR_PERMS); } $dir .= '/'; /** * @var array $dim config for image function * @var int $img_id */ $dim = array('w'=>BLOG_FEATURE_IMAGE_WIDTH,'h'=>BLOG_FEATURE_IMAGE_HEIGHT, 'tw'=>BLOG_FEATURE_THUMBNAIL_WIDTH, 'th'=>BLOG_FEATURE_THUMBNAIL_HEIGHT, 'orient'=>''); $img_id = 0; //valid img_id which will match no records for position update /** * replacement images */ if(isset($_FILES['feature'])) { foreach($_FILES['feature']['name'] as $feature => $filename) { if($filename!='') { $sid = ltrim($feature,'feature'); list($image_path,$image_filename) = explode(':',select_one('image_data',"concat_ws(':',image_path,image_filename)",'image_id',$sid)); $currentType = image_suffix(DOC_ROOT.$image_path.$image_filename); $newType = image_suffix($_FILES['feature']['tmp_name'][$feature]); //$message .= IMAGETYPE_JPEG." Current: $currentType ; New: $newType Dir: $dir<br />"; //debugging upload_set_image('feature',$feature,$dim,$dir,'',true); //$message .= upload_set_image('feature',$feature,$dim,$dir,'',true); if($newType && file_exists(DOC_ROOT.$image_path.$feature.$newType)) { // $message .= 'file'; if($newType != $currentType) { // $message .= 'update'; mysql_query("update image_data set image_filename = '$feature$newType' where image_id = '$sid'"); @unlink(DOC_ROOT.$image_path.$image_filename); @unlink(DOC_ROOT.$image_path.THUMBNAIL_PREFIX.$image_filename); } list($w,$h) = getimagesize(DOC_ROOT.$image_path.$feature.$newType); mysql_query("update image_data set width = '$w', height = '$h' where image_id = '$sid'"); } } } } return true; } /** * remove all images * @param bool $rmDir remove image directory as well * @return bool */ private function remove_images($rm_dir = false) { global $message; /** * san check */ if(!is_numeric_id($this->article_id,0)) { $message .= 'Unable to remove article images: invalid id <br />'; return false; } /* * images */ // files $path = DOC_ROOT.'/resources/images/blog/'.$this->article_id; if(!clean_dir($path, $rm_dir)) { $message .= 'Unable to remove '.$this->name.' images: dir failed <br />'; return false; } //delete image records mysql_query("delete from image_data where image_type like 'blog%' and container_id = '$this->article_id'"); $message .= $this->name.' images deleted <br />'; return true; } /** * remove record from database * @return bool */ private function remove_from_database() { global $message; /** * san check */ if(!is_numeric_id($this->article_id,0)) { $message .= 'Unable to remove article from database: invalid id <br />'; return false; } /** * delete record * @var string $query delete query * @return bool */ $query = "DELETE FROM blog " . "WHERE article_id = " . $this->article_id; $result = mysql_query($query); if(!$result || mysql_affected_rows() != 1) { $message .= 'There was an error '.mysql_error().'<br />Affected rows: '.mysql_affected_rows(); return false; } // $message .= $this->name.' database record deleted <br />'; return true; } } ?>
cải xoăn