Location: PHPKode > projects > PapyrusBB > PapyrusBB-0.6/include/tool/Save.php
<?php
defined('WikyBlog') or die("Not an entry point...");

class bbSave{
	function newPost($postId='forum'){
		
		if( $_SESSION['userlevel'] < 2 ){
			$_SESSION['lastSave'] = array($postId=>time());
		}
	}

	
	function postData(){
		global $wbParser;
		$query = '';
		
		//formatting
		if( $_POST['formatting'] == 'wiki'){
			globalFromPost('text');
			
			initiateParser();
			$wbParser->parse($_POST['text'],true,$this); //$this refers to the forumListing object
			if( $wbParser->foundUnsafe ){
				//message('unsafe');
				$flags = '';
				//$flags = str_replace('safe','',$flags);
			}else{
				$flags = 'safe';
			}
			
		}else{
			$_POST['formatting'] = 'none';
			$flags = '';
		}
		
		$query .= ' `text` = "'. wbDB::escape($_POST['text']) .'" ';
		$query .= ', `formatting` = "'. wbDB::escape($_POST['formatting']) .'" ';
		$query .= ', `flags` = "'. wbDB::escape($flags) .'" ';
		
		return $query;
	}
	
	
	//similar to links() function in SavePage.php of WikyBlog package
	//	needed custom function to keep track of links post by post
	function all_links($post_id){
		global $wbTables,$dbInfo,$wbLinkPrefixStor;
		
		$cleanedLinks = array();
		$oldLinks = array();
		
		//clean the links
		if( is_array($this->inLinks) ){
			foreach($this->inLinks as $rev => $link){
				
				if( $hash = strpos($link,'#') ){
					$link = substr($link,0,$hash);
					$rev = substr($rev,0,$hash);
				}
				if($rev == $this->uniqStorage ){ //don't save link to self
					continue;
				}
				$this->inLinks[$rev] = toStorage($link); 					//make sure spaces are translated
				$cleanedLinks[$rev] = wbStrtolower($this->inLinks[$rev]); 	//to compare with the database values
			}
		}
		
		
		//Get the links saved for this post
		$query = 'SELECT LOWER(`to_link`) as `to_link` ';
		$query .= ' FROM '.$wbTables['bb_links'];
		$query .= ' WHERE `file_id` = "'.$this->file_id.'" ';
		$query .= ' AND `post_id` = "'.$post_id.'" ';
		$result = wbDB::runQuery($query);
		$oldLinks = array();
		while($row = mysql_fetch_assoc($result) ){
			$rev = array_search($row['to_link'],$cleanedLinks);
			if( $rev ){
				unset($cleanedLinks[$rev]);
			}else{
				$oldLinks[] = ' ('.$wbTables['bb_links'].'.`to_link` = "'.wbDB::escape($row['to_link']).'") ';
			}
		}
		
		
		
		//Delete Removed Links
		if( count($oldLinks) > 0 ){
			
			//delete from bb_links
			$query = 'DELETE ';
			$query .= ' FROM ';
			$query .= $wbTables['bb_links'];
			$query .= ' WHERE ';
			$query .= ' `file_id` = "'.$this->file_id.'" ';
			$query .= ' AND `post_id` = "'.$post_id.'" ';
			$query .= ' AND ( ';
			$query .= implode(' OR ',$oldLinks);
			$query .= ' )';
			wbDB::runQuery($query);
			
			
			//delete from all_links according to remaining links
			$query = 'DELETE '.$wbTables['all_links'].' FROM ';
			$query .= $wbTables['all_links'].' LEFT JOIN '.$wbTables['bb_links'];
			$query .= ' USING (`file_id`,`to_link` ) ';
			$query .= ' WHERE ';
			$query .= $wbTables['all_links'].'.`file_id` = "'.$this->file_id.'" ';
			$query .= ' AND '.$wbTables['bb_links'].'.`file_id` IS NULL';
			wbDB::runQuery($query);


		}
		
		//only continue if links have been added.
		if( count($cleanedLinks) < 1){
			return;
		}
		
		//this will only be new links
		foreach($cleanedLinks as $rev => $link){
			
			$link = $this->inLinks[$rev];
			
			$pathArray = interpretPath($link);
			if( !isset($dbInfo[$pathArray['type']]['dbTable']) ){
				continue;
			}
			
			if( count($pathArray['path']) < 1){
				$link = str_replace('//','/',$link.'/Home');
			}
			
			$space =& $pathArray['type'];
			
			//get to_id
			$query = 'SELECT '.$dbInfo[$space]['dbTable'].'.`file_id` ';
			$query .= ' FROM '.$dbInfo[$space]['dbTable'];
			$query .= ' INNER JOIN '.$wbTables['all_files'];
			$query .= ' USING(`file_id`) ';
			//might be able to use keys instead of uniqLink
			$query .= ' WHERE '.wbData::dbInfo($space,'uniqLink').' = "'.wbDB::escape($link).'" '; 
			$query .= ' LIMIT 1 OFFSET 0 ';
			$result = wbDB::runQuery($query);
			$to_id = false;
			if( $row = mysql_fetch_assoc($result) ){
				$to_id = $row['file_id'];
			}
			
			//all_links
			$query = 'INSERT IGNORE INTO '.$wbTables['all_links'].' SET ';
			$query .= ' `file_id` = "'.$this->file_id.'" ';
			$query .= ', `to_link` = "'.wbDB::escape($link).'" ';
			$query .= ', `to_owner` = "'.$pathArray['owner'].'" ';
			if( $to_id ){
				$query .= ', `to_id` ="'.$to_id.'" ';
			}
			wbDB::runQuery($query);
			
			//bb_links
			$query = 'INSERT IGNORE INTO '.$wbTables['bb_links'].' SET ';
			$query .= ' `file_id` = "'.$this->file_id.'" ';
			$query .= ', `to_link` = "'.wbDB::escape($link).'" ';
			$query .= ', `post_id` = "'.wbDB::escape($post_id).'" ';
			wbDB::runQuery($query);
		}
				
	}
	
	
	function captcha($postId){
		
		if( isset($_SESSION['captcha']) ){
			while( count($_SESSION['captcha']) > 6){
				array_shift($_SESSION['captcha']);
			}
		}else{
			$_SESSION['captcha'] = array();
		}
		$rand = rand(0,10000);
		unset($_SESSION['captcha'][$postId]);
		$_SESSION['captcha'][$postId] = $rand;
		return $rand;
	}
	
	function checkPoster($postId,$captcha=false){
		global $wbConfig,$wbUniq;
		
		
		if( $captcha ){
			if( empty($_POST['captcha']) ){
				message('INCORRECT_CAPTCHA');
				return false;
			}
			
			//prep the text
			$r = $_SESSION['captcha'][$postId];
			$text = md5($wbUniq.$r);
			$text = str_replace(array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'),array(''),$text);
			$text = substr($text,1,6);

			if( $text !== $_POST['captcha'] ){
				$i = 0;
				while($i < strlen($text) ){
					$a = $text{$i};
					$b = $_POST['captcha']{$i};
					$i++;
				}
				message('INCORRECT_CAPTCHA');
				return false;
			}
		}
		
		
		
		
		if( isset($_SESSION['lastSave']) ){
			reset($_SESSION['lastSave']); //key() doesn't work right for all php versions
		
			if( isset($wbConfig['floodInterval']) && $wbConfig['floodInterval'] > 0
			&&  $_SESSION['userlevel'] < 3 ){ //using level 3 instead of 2 here
				
				
				$timeDiff = time()-current($_SESSION['lastSave']);
				if( $timeDiff < $wbConfig['floodInterval'] ){
					message('FLOOD_WARN',$wbConfig['floodInterval'],$wbConfig['floodInterval']-$timeDiff);
					return false;
				}
			}
		}		
		return true;
	}
}

////////////////////////////////////////////////////////////////////////////////////
//
//	All Search Functions
//

class bbSearch{
	
	
	//update the whole topic
	function update($file_id){
		global $wbTables;
		
		
		//Get it started with bb_topics
		$query = 'INSERT INTO '.$wbTables['all_search'];
		$query .= ' (`file_id`,`all_title`,`all_content`) ';
		$query .= ' SELECT ';
		$query .= ' "'.wbDB::escape($file_id).'" ';
		$query .= ', `title` ';
		$query .= ', "" ';
		$query .= ' FROM '.$wbTables['bb_topics'];
		$query .= ' WHERE `file_id` = "'.wbDB::escape($file_id).'" ';
		$query .= ' ON DUPLICATE KEY UPDATE `all_title` = `title` ';
		$query .= ' , `all_content` = "" ';
		wbDB::runQuery($query);
		if( !wbDB::runQuery($query) ){
			trigger_error('bbSearch::update()');
		}
		
		//add all the posts
		$query = 'INSERT INTO '.$wbTables['all_search'];
		$query .= ' (`file_id`,`all_title`,`all_content`) ';
		$query .= ' SELECT ';
		$query .= ' "'.wbDB::escape($file_id).'" ';
		$query .= ' , "", "" ';
		$query .= ' FROM '.$wbTables['bb_posts'];
		$query .= ' WHERE '.$wbTables['bb_posts'].'.`file_id` = "'.wbDB::escape($file_id).'" ';
		$query .= ' ORDER BY `id` ASC ';
		$query .= ' ON DUPLICATE KEY UPDATE ';
		$query .= ' `all_content` = CONCAT_WS(" ", `all_content`, `text` )';
		
		if( !wbDB::runQuery($query) ){
			trigger_error('bbSearch::update()');
		}
	}
	
	
	//just append the newest post
	function addPost($file_id){
		global $wbTables;
		
		
		$query = 'INSERT INTO '.$wbTables['all_search'];
		$query .= ' (`file_id`,`all_title`, `all_content`) ';
		$query .= ' SELECT ';
		$query .= ' "'.wbDB::escape($file_id).'" ';
		$query .= ', "", "" ';
		$query .= ' FROM '.$wbTables['bb_posts'];
		$query .= ' WHERE '.$wbTables['bb_posts'].'.`file_id` = "'.wbDB::escape($file_id).'" ';
		$query .= ' ORDER BY `id` DESC ';
		$query .= ' LIMIT 1 OFFSET 0 '; //only adds the newest post!
		$query .= ' ON DUPLICATE KEY UPDATE ';
		$query .= ' `all_content` = CONCAT_WS(" ", `all_content`, `text` )';
		
		
		if( !wbDB::runQuery($query) ){
			trigger_error('bbSearch::update()');
		}		
		
	}
	
}

Return current item: PapyrusBB