Location: PHPKode > projects > ContentBBcode Parser > contentbbcode-parser/content_bbcode.php
<?PHP
class contentbb_code
	{
	public function __construct($login, $pass, $cf_db = 'config.db', $prefix = '')
		{
		$this->prefix = $prefix;
		$this->config_db = new sqlite_db();
		IF(!is_file($cf_db))
			{
			$this->config_db->connect($cf_db);
			contentbb_code::create_config_tables($this->prefix);
			}
		else
			{
			$this->config_db->connect($cf_db);
			}
		$this->login = $login;
		$this->pass = $pass;
		unset($login);
		unset($pass);
		unset($cf_db);
		unset($prefix);
		IF($_GET['config'] == 'true')
			{
			echo contentbb_code::cfg();
			contentbb_code::__destruct();
			exit();
			}
		}
	public function __destruct()
		{
		unset($this->config_db);
		unset($this->prefix);
		unset($this->login);
		unset($this->pass);
		}
	private function create_config_tables($prefix)
		{
		$this->config_db->query("CREATE TABLE ".$prefix."rk_cbc (cbc_id INTEGER NOT NULL , cbc_node varchar(20) default NULL, cbc_code text, cbc_desc text, cbc_active char(1) NOT NULL default '1', PRIMARY KEY  (cbc_id))");
		$this->config_db->query("INSERT INTO ".$prefix."rk_cbc (cbc_node, cbc_code, cbc_desc, cbc_active) VALUES ('google', 'Zm9yZWFjaCgkZ29vZ2xlIGFzICR2YWwpCgl7CglvYl9zdGFydCgpOwoJSUYoaXNzZXQoJF9QT1NUW1wnc2VhcmNoXCddKSBhbmQgc3RybGVuKCRfUE9TVFtcJ3NlYXJjaFwnXSkgPiAyKQoJCXsKCQlJRighaXNzZXQoJHZhbFsxXVtcJ3NpdGVcJ10pKQoJCQl7CgkJCSRkYXRhID0gZmlsZV9nZXRfY29udGVudHMoXCdodHRwOi8vd3d3Lmdvb2dsZS5jb20vc2VhcmNoP3E9XCcuJF9QT1NUW1wnc2VhcmNoXCddLlwnJmllPVVURi04Jm9lPVVURi04XCcpOwoJCQl9CgkJZWxzZQoJCQl7CgkJCSRkYXRhID0gZmlsZV9nZXRfY29udGVudHMoXCdodHRwOi8vd3d3Lmdvb2dsZS5jb20vc2VhcmNoP3E9XCcuJF9Q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Gdlc2hpLT5zZXRfbGlua19zdHlsZXMoR0VTSElfTElOSywgXCdjb2xvcjogIzAwMDA2MDtcJyk7CgkJJGdlc2hpLT5zZXRfbGlua19zdHlsZXMoR0VTSElfSE9WRVIsIFwnYmFja2dyb3VuZC1jb2xvcjogI2YwZjAwMDtcJyk7CgkJCUlGKCFpc3NldCgkZ2VzaGkyKSkKCQkJewoJCQkkZ2VzaGkyID0gdHJ1ZTsKCQkJZWNobyBcJzxzdHlsZSB0eXBlPVwidGV4dC9jc3NcIj48IS0tXCc7CgkJCWVjaG8gJGdlc2hpLT5nZXRfc3R5bGVzaGVldCgpOwoJCQllY2hvIFwnCgkJCS0tPgoJCQk8L3N0eWxlPlwnOwoJCQl9CgkJCgkJZWNobyAkZ2VzaGktPnBhcnNlX2NvZGUoKTsKCQkkZm9vID0gb2JfZ2V0X2NvbnRlbnRzKCk7CgkJb2JfZW5kX2NsZWFuKCk7CgkJJHRleHQgPSBzdHJfcmVwbGFjZSgkdmFsWzBdLCAkZm9vLCAkdGV4dCk7CgkJfQoJfQ==', 'PEI+R0VTSEk8L0I+OiBbY29kZV08cmtfZ2VzaGkgbGFuZz0iTEFORyI+Q29kZTwvcmtfZ2VzaGk+Wy9jb2RlXSAtIFdpbGwgZGlzcGxheSBoaWdobGlnaHRlZCBjb2RlIHdpdGggbGluZSBudW1iZXJzLiBMQU5HIC0gaXMgdGhlIGdlc2hpIGxhbmcgZmlsZSB3aXRob3V0ICoucGhwLiA8Qj5Ob3RlPC9CPjogWW91IG5lZWQgdG8gZ2V0IGdlc2hpIGFuZCBleHRyYWN0IGl0IHRvIC9leHRlcm5hbC4=', '1')");
		}
	#######################################
	#######################################
	public function parse_content_bbcode($text)
		{
		preg_match_all('#<rk_([a-z_0-9]*) (.*?)>(.*?)</rk_(\\1)>#es', $text, $array1);
		preg_match_all('#<rk:([a-z_0-9]*) (.*?) />#es', $text, $array2);
		IF(count($array1[0]) < 1 and count($array2[0]) < 1)
			{
			return $text;
			}
		elseIF(count($array1[0]) >= 1 and count($array2[0]) >= 1)
			{
			$cbc = array_merge($array1[1], $array2[1]);
			foreach($array2[2] as $key => $line)
				{
				$x = explode('" ', $line);
				foreach($x as $foo)
					{
					$foo = str_replace('"', '', $foo);
					$foo = explode('=', $foo);
					IF(!is_array($array2[2][$key]))
						{
						$array2[2][$key] = array();
						}
					$array2[2][$key][$foo[0]] = $foo[1];
					}
				}
			foreach($array2[1] as $key => $ar)
				{
				IF(!is_array($$ar))
					{
					$$ar = array();
					}
				array_push($$ar, array($array2[0][$key], $array2[2][$key], $array2[3][$key]));
				}
			
			foreach($array1[2] as $key => $line)
				{
				$x = explode('" ', $line);
				foreach($x as $foo)
					{
					$foo = str_replace('"', '', $foo);
					$foo = explode('=', $foo);
					IF(!is_array($array1[2][$key]))
						{
						$array1[2][$key] = array();
						}
					$array1[2][$key][$foo[0]] = $foo[1];
					}
				}
			foreach($array1[1] as $key => $ar)
				{
				IF(!is_array($$ar))
					{
					$$ar = array();
					}
				array_push($$ar, array($array1[0][$key], $array1[2][$key], $array1[3][$key]));
				}
			}
		elseIF(count($array1[0]) >= 1)
			{
			$cbc = $array1[1];
			foreach($array1[2] as $key => $line)
				{
				$x = explode('" ', $line);
				foreach($x as $foo)
					{
					$foo = str_replace('"', '', $foo);
					$foo = explode('=', $foo);
					IF(!is_array($array1[2][$key]))
						{
						$array1[2][$key] = array();
						}
					$array1[2][$key][$foo[0]] = $foo[1];
					}
				}
			foreach($array1[1] as $key => $ar)
				{
				IF(!is_array($$ar))
					{
					$$ar = array();
					}
				array_push($$ar, array($array1[0][$key], $array1[2][$key], $array1[3][$key]));
				}
			}
		elseIF(count($array2[0]) >= 1)
			{
			$cbc = $array2[1];
			foreach($array2[2] as $key => $line)
				{
				$x = explode('" ', $line);
				foreach($x as $foo)
					{
					$foo = str_replace('"', '', $foo);
					$foo = explode('=', $foo);
					IF(!is_array($array2[2][$key]))
						{
						$array2[2][$key] = array();
						}
					$array2[2][$key][$foo[0]] = $foo[1];
					}
				}
			foreach($array2[1] as $key => $ar)
				{
				IF(!is_array($$ar))
					{
					$$ar = array();
					}
				array_push($$ar, array($array2[0][$key], $array2[2][$key], $array2[3][$key]));
				}
			}
		unset($array1);
		unset($array2);
		$cbc = implode('\' OR cbc_node=\'', $cbc);
		$cbc = 'cbc_node=\''.$cbc.'\'';
		$query = $this->config_db->query_select("SELECT cbc_code, cbc_active FROM ".$this->prefix."rk_cbc WHERE ".$cbc);
		unset($cbc);
		IF(is_array($query))
			{
			foreach($query as $res)
				{
				IF($res['cbc_active'] == 1)
					{
					$res['cbc_code'] = stripslashes(base64_decode($res['cbc_code']));
					eval($res['cbc_code']);
					}
				}
			}
		return $text;
		}
	##################################
	############## CFG ################
	public function cfg()
		{
		ob_start();
		echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>
				<head>
				<title>Diamanda</title>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
				<META name="robots" content="noindex, nofollow">
				<META name="Author" content="Riklaunim">
				<link rel="stylesheet" href="cbc.css" type="text/css">
				</head><body>';
		IF($_SERVER['PHP_AUTH_USER'] != $this->login || $_SERVER['PHP_AUTH_PW'] != $this->pass)
			{
			header("WWW-Authenticate: Basic realm=Login");
			header("HTTP/1.0 401 Unauthorized");
			echo 'Login Error';
			exit();
			}
		echo '<center><b><a href="'.$_SERVER['PHP_SELF'].'?config=true&mod=cbc&act=edit">Managa ContentBBcode</a> | <a href="'.$_SERVER['PHP_SELF'].'">Site Index</a></b></center>';
		IF($_GET['mod'] == 'cbc' and $_GET['act'] == 'add')
			{
			echo contentbb_code::cbc_add();
			}
		elseIF($_GET['mod'] == 'cbc' and $_GET['act'] == 'edit')
			{
			echo contentbb_code::cbc_edit();
			}
		echo '</body></html>';
		$wynik = ob_get_contents();
		ob_end_clean();
		return $wynik;
		}
	private function cbc_add()
		{
		ob_start();
		IF (isset($_POST['desc']) and isset($_POST['code']))
			{
			$_POST = array_map("stripslashes", $_POST);
			$_POST['code'] = base64_encode(addslashes($_POST['code']));
			$_POST['desc'] = base64_encode($_POST['desc']);
			$this->config_db->query("INSERT INTO ".$this->prefix."rk_cbc (cbc_node, cbc_code, cbc_desc, cbc_active) VALUES ('".$_POST['node']."', '".$_POST['code']."', '".$_POST['desc']."', '".$_POST['active']."')");
			echo 'CBC Definition added';
			echo '<META HTTP-EQUIV="Refresh" CONTENT="1; URL='.$_SERVER['PHP_SELF'].'?config=true&mod=cbc&act=edit">';
			unset($_POST);
			}
		else
			{
			$fr = new form();
			$fr->start_form(''.$_SERVER['PHP_SELF'].'?config=true&mod=cbc&act=add', 'Add CBC Definition');
			$fr->element('title', 'ID', 'node', '', '60', '');
			$fr->element('textarea', 'Description', 'desc', '', '70', '6');
			$fr->element('textarea', 'Code', 'code', '', '70', '20');
			$fr->element('select', 'Status', 'active', array(1, 0), '', '');
			$fr->end_form('Send', '<B>Status</B> - 1 for Active, 0 - unactive.');
			unset($fr);
			}
		$wynik = ob_get_contents();
		ob_end_clean();
		return $wynik;
		}
	private function cbc_edit()
		{
		ob_start();
			IF (isset($_GET['infed']) and is_numeric($_GET['infed']) and $_GET['ak']=='zapis')
				{
				$_POST = array_map("stripslashes", $_POST);
				$_POST['code'] = base64_encode(addslashes($_POST['code']));
				$_POST['desc'] = base64_encode($_POST['desc']);
				$this->config_db->query("UPDATE ".$this->prefix."rk_cbc SET cbc_node = '".$_POST['node']."', cbc_code = '".$_POST['code']."', cbc_desc = '".$_POST['desc']."', cbc_active = '".$_POST['active']."' WHERE cbc_id = '".$_GET['infed']."'");
				echo 'Definition updated';
				echo '<META HTTP-EQUIV="Refresh" CONTENT="1; URL='.$_SERVER['PHP_SELF'].'?config=true&mod=cbc&act=edit">';
				}
			elseIF (isset($_GET['infed']) and is_numeric($_GET['infed']) and $_GET['to']=='del')
				{
				IF ($_GET['conf'] == 'yes')
					{
					$this->config_db->query("DELETE FROM ".$this->prefix."rk_cbc WHERE cbc_id = '".$_GET['infed']."'");
					echo 'Definition Deleted';
					echo '<META HTTP-EQUIV="Refresh" CONTENT="1; URL='.$_SERVER['PHP_SELF'].'?config=true&mod=cbc&act=edit">';
					}
				else
					{
					echo '<center>Delete definition ?<BR><B><a href="'.$_SERVER['PHP_SELF'].'?config=true&mod=cbc&act=edit&infed='.$_GET['infed'].'&to=del&conf=yes">YES</a></B></ceter>';
					}
				}
			elseIF (isset($_GET['infed']) and is_numeric($_GET['infed']))
				{
				$queryx = $this->config_db->query_select("SELECT * FROM ".$this->prefix."rk_cbc WHERE cbc_id = '".$_GET['infed']."'");
				IF(is_array($queryx))
					{
					foreach ($queryx as $res)
						{
						$res['cbc_code'] = stripslashes(base64_decode($res['cbc_code']));
						$res['cbc_code'] = str_replace('</textarea>', '< /textarea>', $res['cbc_code']);
						$res['cbc_desc'] = base64_decode($res['cbc_desc']);
						
						$fr = new form();
						$fr->start_form(''.$_SERVER['PHP_SELF'].'?config=true&mod=cbc&act=edit&infed='.$res['cbc_id'].'&ak=zapis', 'Edit CBC Definition');
						$fr->element('title', 'ID', 'node', $res['cbc_node'], '60', '');
						$fr->element('textarea', 'Description', 'desc', $res['cbc_desc'], '70', '6');
						$fr->element('textarea', 'Code', 'code', $res['cbc_code'], '70', '20');
						$fr->element('select', 'Status', 'active', array($res['cbc_active'], 1, 0), '', '');
						$fr->end_form('Send', '');
						unset($fr);
						unset($queryx);
						}
					}
				}
			else
				{
				echo '<center><B>[<a href="'.$_SERVER['PHP_SELF'].'?config=true&mod=cbc&act=add">Add CBC Definition</a>]</B></center>';
				$query = $this->config_db->query_select("SELECT cbc_id, cbc_desc, cbc_node, cbc_active FROM ".$this->prefix."rk_cbc ORDER BY cbc_id DESC");
				IF (is_array($query))
					{
					echo '<center><table width="95%" border="1" cellspacing="0" cellpadding="5">';
					foreach ($query as $res)
						{
						IF($res['cbc_active'] != 1)
							{
							$res['cbc_node'] = '<font color="brown">'.$res['cbc_node'].'</font>';
							}
						$res['cbc_desc'] = base64_decode($res['cbc_desc']);
						$res['cbc_desc'] = preg_replace('#\[code\](.*?)\[/code\]#es', "contentbb_code::code('\\1')", $res['cbc_desc']);
						echo '<tr><td><LI><b>'.$res['cbc_node'].'</b></td><td>'.$res['cbc_desc'].'</td><td><center>[<a href="'.$_SERVER['PHP_SELF'].'?config=true&mod=cbc&act=edit&infed='.$res['cbc_id'].'">Edit</a>]</td><td><center>[<a href="'.$_SERVER['PHP_SELF'].'?config=true&mod=cbc&act=edit&infed='.$res['cbc_id'].'&to=del">Delete</a>]</td></tr>';
						}
					echo '</table></center>';
					unset($query);
					}
				else
					{
					echo 'No Definitions';
					}
				}
		$wynik = ob_get_contents();
		ob_end_clean();
		return $wynik;
		}
	private function code($text)
		{
		$text = stripslashes($text);
			ob_start();
				highlight_string($text);
			$code = ob_get_contents();
			ob_end_clean();
		return '<B>'.$code.'</B>';
		}
	}
#########################################
############	DATABASE	################
#########################################
class sqlite_db
	{
	public function connect($dbname)
		{
		try
			{
			$this->sqlite = new SqliteDatabase($dbname);
			}
		catch (SQLiteException $error)
			{
			$this->error = true;
			throw new Exception('SQLite ERROR - '.$error->getMessage());
			}
		$this->sqlite->query('BEGIN;');
		}
	public function query($query)
		{
		IF(!ereg('SELECT', $query) and !$this->error)
			{
			IF(!$this->sqlite->query($query))
				{
				$this->error = true;
				throw new Exception('SQLite ERROR - '.$query.'<BR> -  '.sqlite_error_string($this->sqlite->lastError()));
				}
			}
		}
	public function query_select($query)
		{
		$result = $this->sqlite->arrayQuery($query, SQLITE_ASSOC);
		IF(!is_array($result))
			{
			$this->error = true;
			throw new Exception('SQLite ERROR - '.$query.'<BR> -  '.sqlite_error_string($this->sqlite->lastError()));
			}
		IF(count($result) == 0)
			{
			$result = '';
			}
		return $result;
		}
	public function insert_id()
		{
		return $this->sqlite->lastInsertRowid();
		}
	public function escape($string)
		{
		return  $this->sqlite->escapeString($string);
		}
	function __destruct()
		{
		IF(!$this->error)
			{
			$this->sqlite->query('COMMIT;');
			}
		else
			{
			$this->sqlite->query('ROLLBACK;');
			}
		unset($this->sqlite);
		unset($this->error);
		}
	}
########################################
########################################
class form
{
public function start_form($action, $title)
	{
	echo '<B><center><u>'.$title.'</u></center></B><form action="'.$action.'" method="POST"><center><table border="0" cellspacing="0" cellpadding="5">';
	}
public function end_form($button, $end_msg)
	{
	echo '</table><BR /><input type="submit" value="'.$button.'"></center></form>'.$end_msg.'<BR />';
	}
public function element($type, $title, $name, $value, $wid, $hei)
	{
	global $bbcode, $link;
	switch ($type)
		{
		case 'title':
		echo '<tr><td class="table1"><B><UL><LI>'.$title.'</LI></UL></B></td><td class="table2"><input type="title" size="'.$wid.'" name="'.$name.'" value="'.$value.'"></td></tr>';
		break;
		###################
		case 'textarea':
		echo '<tr><td class="table1"><B><UL><LI>'.$title.'</LI></UL></B></td><td class="table2"><textarea cols="'.$wid.'" rows="'.$hei.'" name="'.$name.'">'.$value.'</textarea></td></tr>';
		break;
		###################
		case 'select':
		echo '<tr><td class="table1"><B><UL><LI>'.$title.'</LI></UL></B></td><td class="table2"><select name="'.$name.'">';
		foreach ($value as $bar)
			{
			echo '<option>'.$bar.'</option>';
			}
		echo '</select></td></tr>';
		break;
		###################
		default:
		return 'form error';
		break;
		}
	}
}
?>
Return current item: ContentBBcode Parser