Skip to content

Instantly share code, notes, and snippets.

@Nub
Created September 28, 2011 06:36
Show Gist options
  • Select an option

  • Save Nub/1247153 to your computer and use it in GitHub Desktop.

Select an option

Save Nub/1247153 to your computer and use it in GitHub Desktop.
<?php
/*
myHtml by Zachry Buddy Thayer
Version 2.0
License: CC BY-SA (Crreative Commons Share Alike)
TODO 2.1:
*Parse tags in physical order (compared to in order by category)
*Embedded Loops
*/
class myHtml{
private $myHtmlVersion = "2.0";
private $mysqli;//msSQL handle
private $data;//source data
private $newData;//new data
private $table;//current table
private $row;//current row
private $column;//current column
private $arg;//extra SQL args
private $plugins = array();//myHtml plugins array
/*
Either init with a sql connection or do not.
Arguments are all or none.
*/
function __construct($host=NULL,$user=NULL,$pass=NULL,$database=NULL){
if($host != NULL && $database != NULL){
$this->mysqli = new mysqli($host, $user, $pass, $database);
if(!$this->mysqli){
die($this->mysqli->error());
}
}
}
function __destruct(){
$this->mysqli->close();
}
/*
Break down refrence into array of arguments
*/
private function readyArgs($refrence){
$refPattern = "/[@#\.&]+((?![@#\.&\]]).)+/";
preg_match_all($refPattern, $refrence, $r);
$this->interpretArgs($r[0]);
}
/*
Read refrence arguments and store them to local variables
*/
private function interpretArgs($args){
foreach($args as $arg){
switch(substr($arg, 0, 1)){
case "@":
$this->table = substr($arg, 1);
break;
case "#":
$this->row = substr($arg, 1);
break;
case ".":
$this->column = substr($arg, 1);
break;
case "&":
$this->arg = substr($arg, 1);
break;
}
}
}
/*
Find all loops and replace them with the proper looped structure
*/
private function parseLoop(&$data){
$refPattern = "/\[(((?![\[\]]).)*)\]{(.*)}/";
preg_match_all($refPattern, $data, $r);
$loops = $r[0];
$loopArgs = $r[1];
$loopStructure = $r[3];
foreach($loops as $key => $loop){
$data = str_replace($loop, $this->interpretLoop($loopArgs[$key],$loopStructure[$key]), $data);
}
return $data;
}
/*
Takes a loop refrence and builds the structure with the proper data
*/
private function interpretLoop($args, $structure){
$this->readyArgs($args);
$query = "SELECT `key` FROM `".$this->table;
if($this->arg != ""){
$query .= "` WHERE ".$this->mysqli->escape_string($this->arg);
$this->arg = "";
}else
$query .= "`";
$struct = $structure;
$ret = "";
if($result = $this->mysqli->query($query)){
while($row = $result->fetch_row()){
$this->row = $row[0];
$ret .= $this->parse($struct);
$struct = $structure;//reset structure
}
$result->free();
return $ret;
}
return "";
}
/*
Find all refrences and replace them with proper values
*/
private function parse(&$data){
$refPattern = "/\[((?![\[\]]).)*\]/";
preg_match_all($refPattern, $data, $r);
$refrences = $r[0];
foreach($refrences as $ref){
$data = str_replace($ref, $this->interpret($ref), $data);
}
return $data;
}
/*
Takes a refrence and fetches its value from a database
*/
private function interpret($refrence){
$this->readyArgs($refrence);
$query = "SELECT `".$this->column."` FROM `".$this->table."` WHERE ";
if(is_numeric($this->row))
$query .= "`id`=".$this->row;
else
$query .= "`key`='".$this->row."'";
if($this->arg != ""){
$query .= " AND ".$this->mysqli->escape_string($this->arg);
$this->arg = "";
}
if($result = $this->mysqli->query($query)){
$row = $result->fetch_row();
$result->free();
if($row && $row[0])
return $row[0];
}
return "";
}
/*
Look for a plugin refrence and invoke the plugin
*/
private function parsePlugins(&$data){
$refPattern = "/\[([A-Za-z]+)\(((.)*)\)\]/";
preg_match_all($refPattern, $data, $r);
$refrences = $r[0];
$plugins = $r[1];
$args = $r[2];
foreach($refrences as $key => $ref){
if($plugins[$key] == "myHtml"){
$earg = explode(",", $args[$key]);
$this->mysqli = new mysqli($earg[0],$earg[1], $earg[2], $earg[3]);
if(!$this->mysqli){
die($this->mysqli->error());
}
$data = str_replace($ref, "", $data);
}else if($this->plugins[$plugins[$key]]){
$output = $this->plugins[$plugins[$key]]->invoke($args[$key]);
$data = str_replace($ref, $output, $data);
}
}
return $data;
}
/*
Processes a string or a file depending upon the input
*/
function process($source){
if(is_file($source))
$this->data = file_get_contents($source);
else
$this->data = $source;
$this->parsePlugins($this->data);
$this->parseLoop($this->data);
$this->parse($this->data);
}
/*
Append plugin to array of plugins
*/
function addPlugin(&$plugin){
$plugin->myHtml($this);
$this->plugins[$plugin->name()] = $plugin;
}
/*
Fetch version
*/
function version(){
return $this->myHtmlVersion;
}
function __toString(){
if($this->data)
return $this->data;
else
return "myHtml has no data.";
}
/*
php5.5+
*/
function __invoke($source){
$this->process($source);
}
}
abstract class myHtmlPlugin{
abstract public function __construct();
/*
refrence to myHtml for argument evaluation
*/
protected $_myHtml;
/*
Set myHtml refrence
*/
function myHtml(&$myHtml){
$this->_myHtml = $myHtml;
}
/*
Plugin Function to be called when refrence is found
Return: string
*/
abstract public function invoke($args);
/*
Plugin Name
to be searched for in refrences
*/
protected $_name;
/*
Fetch name
*/
function name(){
return $this->_name;
}
/*
String version of plugin
*/
protected $_version;
/*
Fetch version
*/
function version(){
return $this->_version;
}
function __toString(){
return "Plugin: ".$this->_name." Version: ".$this->_version;
}
}
/*
Example plugin (DO NOT DELETE)
This creates the required credentials for the licensing
*/
class myHtmlLicense extends myHtmlPlugin{
function invoke($args){
return "Powered by myHtml Version ".$this->_myHtml->version()."<br/>
<a rel=\"cc:attributionURL\" property=\"cc:attributionName\" href=\"http://myhtml.inkincdesign.org\">By: Zachry Buddy Thayer</a> &nbsp; &nbsp;<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/3.0/us/\"><img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"http://i.creativecommons.org/l/by-sa/3.0/us/80x15.png\" /></a>";
}
public function __construct(){
$this->_name = "myHtmlLicense";
$this->_version = "1.0";
}
}
//Example use
$myHtmlInfo = new myHtmlLicense();
$myHtml = new myHtml();
$myHtml->addPlugin($myHtmlInfo);
$myHtml->process($_GET['parse']);
echo $myHtml;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment