ruạṛ
<?php /** * Basic page object */ class page { /** * define variables matched to to database fields * may not have identical names, see $this->load_from_data for conversions */ var $page_id , $parent_id , $position , $type , $display_content , $path , $content , $title , $keywords , $description , $active , $menu , $menu_text , $menu_id , $homepage , $errorpage , $external , $link , $slideshow , $visited ; /** * @var array $subpages */ var $subpages = array(); /** * object construction function * @param int $id unique database record id */ function __construct($id=0) { /* * set miminum visibility */ $this->page_id = 0; $this->active = 0; $this->menu = 0; $this->menu_id = 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 page_data where page_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; */ function load_from_data($d=array()) { if(!empty($d)) { $this->page_id = $d['page_id']; $this->parent_id = $d['parent_id']; $this->position = $d['position']; $this->type = $d['page_type']; $this->display_content = $d['display_content']; $this->name = $d['name']; $this->path = $d['path']; $this->content = $d['content']; $this->title = $d['title']; $this->keywords = $d['keywords']; $this->description = $d['description']; $this->active = $d['active']; $this->menu = $d['menu']; $this->menu_text = $d['menu_text']; $this->menu_id = $d['menu_id']; $this->homepage = $d['index_page']; $this->errorpage = $d['error_page']; $this->external = $d['external']; $this->link = $d['external_path']; $this->slideshow = $d['has_slideshow']; $this->visited = $d['visited']; } $this->images = array(); //empty $images = mysql_query("select * from image_data where image_type = 'page-slideshow' and container_id='".$this->page_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; } /** * load subpages * @param bool $all all or active only * @param bool $vis all or on menu only */ function load_subpages($all=false,$vis=true) { $this->subpages = array(); //empty; /** * @var int $id this page id * @var string $query constructed mysql query * @var resource $subs mysql_resource set */ $id = $this->page_id; $query = "select page_id from page_data where parent_id = '$id'"; $query .= ($all) ? '' : " and active = '1'"; $query .= (!$vis) ? '' : " and menu = '1'"; $query .= ' order by position'; $subs = mysql_query($query); while($s = mysql_fetch_assoc($subs)) { $this->subpages[] = new page($s['page_id']); } unset($s); unset($subs); // clean up return; } /** * add record to the database and create stub file * @return bool */ function create() { global $message; /** * get from POST */ $this->parent_id = $parent_id = clean_plain_data($_POST['parent']); $this->menu_id = $menu_id = clean_plain_data($_POST['menu']); $this->name = $name = clean_plain_data($_POST['page_name']); $this->title = $title = clean_plain_data($_POST['title']); $this->keywords = $keywords = clean_plain_data($_POST['keywords']); $this->description = $description = clean_plain_data($_POST['description']); $this->content = $content = clean_html_data($_POST['content']); $this->position = $position = clean_plain_data($_POST['position']); if(!$position) { $this->position = $position = select_one('page_data','max(position)','parent_id',$parent_id)+10; } /** * validate required fields */ $m = ''; if($name==''){ $m .= 'Please enter a page name <br />'; } if($parent_id != 0 && (!is_numeric_id($parent_id, false) || !select_one('page_data', 'page_id', 'page_id', $parent_id) ) ) { $m .= 'Invalid parent page'; } else if($parent_id != 0) { $page = new page($parent_id); $this->menu_id = $menu_id = $page->menu_id; } else { $this->menu_id = $menu_id = 1; } if($m != '') { $message .= $m; return false; } /** * assemble directory path * @var string $path * @var string $parent_path */ $path = dir_name($name); if($parent_id > 0) { $parent_path = select_one('page_data', 'path', 'page_id', $parent_id); } else { $parent_path = '/'; } $this->path = $path = $parent_path.$path; /** * add to database * @var string $fields * @var string $values */ $date_added = time(); $fields = 'parent_id , name , title , keywords , description , content , path , menu_text , menu_id , position '; $values = "'$parent_id' , '$name' , '$title' , '$keywords' , '$description' , '$content' , '$path/' , '$name' , '$menu_id' , '$position' "; $query = "insert into page_data ( $fields ) values ( $values )"; if(!mysql_query($query)) { $message .= mysql_error(); return false; } $message .= 'Page record created <br />'; $this->page_id = $page_id = mysql_insert_id(); /** * create stub file * there will be a page_id because we checked for the record getting $parent_path */ $contents = array( 'page_id' => $page_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/page/'.$page_id; if(!mkdir($dir, 0755)) { $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 .= 'Page created successfully.<br />'; return true; } /** * update record in the database - content * @return bool */ function update() { global $message; /** * san check */ if(!$this->page_id) { $message .= 'Invalid page <br />'; return false; } /** * get from POST */ $title = clean_plain_data($_POST['title']); $keywords = clean_plain_data($_POST['keywords']); $description = clean_plain_data($_POST['description']); $content = clean_html_data($_POST['content']); /** * validate required fields */ $m = ''; if($menu_text==''){ //$m .= 'Please enter a page name <br />'; $menu_text = $this->name; } if($m != '') { $message .= $m; return false; } /** * @var string $query update query */ $query = "update page_data set title='$title' , keywords='$keywords' , description='$description' , content='$content' where page_id = '$this->page_id'"; $update = mysql_query($query); if(!$update) { $message .= mysql_error(); return false; } $message .= 'Page record updated <br />'; return true; } /** * update record in the database - misc / menu options * @return bool */ function update_options() { global $message; /** * san check */ if(!$this->page_id) { $message .= 'Invalid page <br />'; return false; } $menu_text = clean_plain_data($_POST['menu_text']); $index_page = ($_POST['index_page']) ? 1 : 0; $error_page = ($_POST['error_page']) ? 1 : 0; $external = ($_POST['external']) ? 1 : 0; $link = clean_plain_data($_POST['link']); if($menu_text==''){ //$m .= 'Please enter a page name <br />'; $menu_text = $this->name; } /** * @var string $query update query */ $query = "update page_data set menu_text = '$menu_text' , index_page='$index_page' , error_page='$error_page' , external = '$external' , external_path = '$link' where page_id = '$this->page_id'"; $update = mysql_query($query); if(!$update) { $message .= mysql_error(); return false; } if($index_page) { mysql_query("update page_data set index_page='0' where index_page='1' and page_id!='$this->page_id'"); } if($error_page) { mysql_query("update page_data set error_page='0' where error_page='1' and page_id!='$this->page_id'"); } $message .= 'Page record updated <br />'; return true; } /** * update slideshow (database settings and images) */ function update_slideshow() { global $message; /** * san check */ if(!$this->page_id) { $message .= 'Invalid page <br />'; return false; } if(!PAGE_HAS_SLIDESHOW) { $message .= 'Module not enabled <br />'; return false; } //else /** * settings * @var int $has_slideshow (bool) * @var string $query */ $has_slideshow = ($_POST['has_slideshow']) ? 1 : 0; $query = "update page_data set has_slideshow = '$has_slideshow' where page_id = '$this->page_id'"; $update = mysql_query($query); if(!$update) { $message .= mysql_error(); return false; } /** * images */ // $message .= 'Init slideshow <br />'; $this->add_slideshow_image('slideshow_image'); // $message .= 'Update slideshow <br />'; $this->update_slideshow_images(); } /** * 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 */ function add_slideshow_image($upload) { global $message; /** * san check */ if(!$upload || !is_numeric_id($this->page_id)) { return false; } /** * make sure we have a product image directory */ $path = '/resources/images/page/'.$this->page_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 */ $link = clean_plain_data($_POST[$upload.'_link']); $caption = clean_plain_data($_POST[$upload.'_caption']); $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)",'page-slideshow:'.$this->page_id)+10; } /** * @var array $dim config for image function * @var int $img_id */ $dim = array('w'=>PAGE_SLIDESHOW_WIDTH,'h'=>PAGE_SLIDESHOW_HEIGHT, 'tw'=>0, 'th'=>0, '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 ('page-slideshow', '$this->page_id', '$path/', '$position', '$caption', '$link', '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 = 'slide'.$img_id.$suff; $message .= upload_image($upload,$dim,$dir,$filename,false); //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 slideshow * @return bool */ function update_slideshow_images() { global $message; $message .= 'Updating images<br />'; //testing; /** * san check */ if(!is_numeric_id($this->page_id)) { return false; } /** * make sure we have a product image directory */ $path = '/resources/images/page/'.$this->page_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'=>PAGE_SLIDESHOW_WIDTH,'h'=>PAGE_SLIDESHOW_HEIGHT, 'tw'=>0, 'th'=>0, 'orient'=>''); $img_id = 0; //valid img_id which will match no records for position update /** * replacement images */ if(isset($_FILES['slideshow'])) { foreach($_FILES['slideshow']['name'] as $slide => $filename) { if($filename!='') { $sid = ltrim($slide,'slide'); 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['slideshow']['tmp_name'][$slide]); //$message .= IMAGETYPE_JPEG."Current: $currentType ; New: $newType <br />"; //debugging upload_slideshow_image($slide,$dim,$dir,'',false); if($newType && file_exists(DOC_ROOT.$image_path.$slide.$newType)) { $message .= 'file'; if($newType != $currentType) { $message .= 'update'; mysql_query("update image_data set image_filename = '$slide$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.$slide.$newType); mysql_query("update image_data set width = '$w', height = '$h' where image_id = '$sid'"); } } } } /** * slideshow captions */ if(isset($_POST['slidetitle']) && is_array($_POST['slidetitle'])){ foreach($_POST['slidetitle'] as $slide => $caption){ $sid = ltrim($slide,'title'); $caption = clean_plain_data($caption); mysql_query("update image_data set title = '$caption' where image_id = '$sid'"); } } /** * slideshow links */ if(isset($_POST['slidelink']) && is_array($_POST['slidelink'])){ foreach($_POST['slidelink'] as $slide => $link){ $sid = ltrim($slide,'link'); $link = clean_plain_data($link); mysql_query("update image_data set link = '$link' where image_id = '$sid'"); } } /** * image order */ if(isset($_POST['slideposition']) && is_array($_POST['slideposition'])){ $p = $_POST['slideposition']; asort($p); $i = 10; foreach($p as $id => $pos){ //actual entered value ($pos) is irrelevant if($id=='new'){$id=$img_id;} mysql_query("update image_data set image_position = '$i' where image_id = '$id'"); $i+=10; } } return true; } /** * remove all images * @param bool $rmDir remove image directory as well * @return bool */ function remove_images($rm_dir = false) { global $message; /** * san check */ if(!is_numeric_id($this->page_id,0)) { $message .= 'Unable to remove product images: invalid id <br />'; return false; } /* * images */ // files $path = DOC_ROOT.'/resources/images/page/'.$this->page_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 'page%' and container_id = '$this->page_id'"); $message .= $this->name.' images deleted <br />'; return true; } /** * remove record from database * @return bool */ function remove_from_database() { global $message; /** * san check */ if(!is_numeric_id($this->page_id,0)) { $message .= 'Unable to remove page from database: invalid id <br />'; return false; } /** * delete record * @var string $query delete query * @return bool */ $query = "delete from page_data where page_id = '$this->page_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 records deleted <br />'; return true; } /** * delete subpages. Use with care. * load subpages before calling this function (this allows the list to be customised) * @return bool */ function delete_subpages() { global $message; foreach($this->subpages as $s) { if(!$s->delete(true)) { $message .= 'Error: unable to delete '.$this->name.' -> '.$s->name.'<br />'; return false; } //else $message .= 'Deleted '.$this->name.' => '.$s->name.'<br />'; } return true; } /** * remove everything * @param $subpage flag to say if this is being called from $this->delete_subpages (toggles message) * @return bool */ function delete($subpage = false) { global $message; /** * san check */ if(!is_numeric_id($this->page_id,0)) { $message .= 'Unable to delete page: invalid id <br />'; return false; } /** * delete subcategories (recursive) */ $this->load_subpages(true,false); if(!$this->delete_subpages()) { 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; } /* * make backup */ mysql_query("insert into deleted_pages (select * from page_data where page_id='$this->page_id')"); /** * remove (delete) from database */ if(!$this->remove_from_database()) { return false; } //else if(!$subpage) { $message .= 'Deleted '.$this->name.' <br />'; } return true; } } ?>
cải xoăn