Skip to content

Instantly share code, notes, and snippets.

@hillbilly
Last active February 3, 2017 00:37
Embed
What would you like to do?
Taconite xml files from php
<?php
/**
* ----------------------------------------------------------------------------
*
* The Software shall be used for Good, not Evil... bad eval()!
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
DEFINE('TACO_VERSION', 4.03);
// --------------------------------------------------------------------
class Taco{
var $storage = array();
var $newline = "\r\n";
var $xml_encode = 'ISO-8859-1';
var $xml_vers = '1.0';
var $html = FALSE;
var $method = 'xml';
var $illegals = array('&'=>'&amp;','&nbsp;' => '&#160;');
// --------------------------------------------------------------------
function taco()
{
log_message('message','Taco library started');
}
// --------------------------------------------------------------------
function _san($string)
{
return strtr($string,$this->illegals);
}
// --------------------------------------------------------------------
function _camel($string)
{
if( strpos($string,'_') === FALSE ) return $string;
$s = split('_', $string );
return strtolower( trim( $s[0] ) ). ucwords( strtolower( trim( $s[1] ) ) );
}
// --------------------------------------------------------------------
function set($name = '', $attributes = NULL, $content = NULL)
{
if( is_string($attributes) and $attributes != '' )
{
/* */
switch(true)
{
/* self closing + css/class etc. etc. */
case( is_array($content) ):
foreach( $content as $k => $v)
{
$this->storage[] = '<'.$name.' select="'.$attributes.'" arg1="'.$this->_camel($k).'" arg2="'.$this->_camel($v).'" />';
}
break;
/* single + show or class */
case( $content == 'fast'):
case( $content == 'slow' ):
case( stristr($name, 'class') ):
$this->storage[] = '<'.$name.' select="'.$attributes.'" arg1="'.$content.'" />';
break;
/* self closing + special: eval */
case( ($name == 'eval' ) ):
$this->storage[] = '<eval><![CDATA['.$attributes.']]></eval>';
break;
/* content replace etc. etc. */
case( is_string($content) ):
$this->storage[] = '<'.$name.' select="'.$attributes.'">'.$this->_san($content). $this->newline .'</'.$name.'>';
break;
default:
$this->storage[] = '<error select="'.$attributes.'">['.$name.'] is not a valid command, yet!'. $this->newline .'</error>';
}
}
}
// --------------------------------------------------------------------
/* */
function output()
{
$storage = $this->storage;
if($storage != '' and is_array($storage) and count($storage) > 0 )
{
$xmlString = '';
foreach( $storage as $string )
{
$xmlString .= $this->newline . $string;
}
/*
* Validation of xmldoc is not available in php4 as far as I can tell...
* But IE7 & FF3 have xml validating so check your generated docs with them.
*/
if( $this->html == true )
{
header('Content-type: text/'.$this->method.';');
}
else
{
header('Content-type: text/'.$this->method.'; charset='.$this->xml_encode);
echo '<?xml version="'.$this->xml_vers.'" encoding="'.$this->xml_encode.'"?>'.$this->newline;
}
echo '<taconite>'.$xmlString.$this->newline.'</taconite>';
}
}
}/* <!-- Taco -->*/
?>
<?php
/**
* ----------------------------------------------------------------------------
*
* The Software shall be used for Good, not Evil... bad eval
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
DEFINE('TACO_VERSION', 5.01);
class Taconite{
private $storage = array();
private $newline = "\r\n";
private $xml_encode = 'ISO-8859-1';
private $xml_vers = '1.0';
private $html = FALSE;
private $method = 'xml';
private $file_size ='';
private $illegals = array('&'=>'&amp;','&nbsp;' => '&#160;');
// --------------------------------------------------------------------
public function __call($name, $arguments)
{
$element = $arguments[0];
$arguments = $arguments[1];
$this->set( $name, $element, $arguments );
}
// --------------------------------------------------------------------
private function _san($string)
{
return strtr($string,$this->illegals);
}
// --------------------------------------------------------------------
private function _camel($string)
{
if( strpos($string,'_') === FALSE ) return $string;
$s = split('_', $string );
return strtolower( trim( $s[0] ) ). ucwords( strtolower( trim( $s[1] ) ) );
}
// --------------------------------------------------------------------
private function set($name = '', $element = NULL, $content = NULL)
{
/* */
switch(true)
{
/* self closing + css/class etc. etc. */
case( is_array($content) ):
$args = '';
$count = count($content);
$args_count = 1;
for ($i = 0; $i < $count; $i++){
$args .= 'arg'. $args_count .'="'.$this->_camel($content[$i]).'" ';
$args_count++;
}
$this->storage[] = '<'.$name.' select="'.$element.'" '.$args.' />';
break;
/* single + show or class */
case( $content == 'fast'):
case( $content == 'slow' ):
case( stristr($name, 'class') ):
$this->storage[] = '<'.$name.' select="'.$element.'" arg1="'.$content.'" />';
break;
/* self closing + special: eval */
case( ($name == 'eval' ) ):
$this->storage[] = '<eval><![CDATA['.$element.']]></eval>';
break;
/* content replace etc. etc. */
case( is_string($content) ):
$this->storage[] = '<'.$name.' select="'.$element.'">'. $this->_san($content) . $this->newline .'</'.$name.'>';
break;
/* toogle. */
case( $content == NULL ):
$this->storage[] = '<'.$name.' select="'.$element.'"></'.$name.'>';
break;
default:
$this->storage[] = '<error select="'.$element.'">['.$name.'] is not a valid command, yet!'. $this->newline .'</error>';
}
}
// --------------------------------------------------------------------
/* */
function set_headers()
{
if( $this->html )
{
header('Content-type: text/'.$this->method.';');
}else{
header('Content-type: text/'.$this->method.'; charset='.$this->xml_encode);
}
}
// --------------------------------------------------------------------
/* */
function no_cache()
{
//no cache headers
header("Expires: Mon, 26 Jul 12012 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
}
// --------------------------------------------------------------------
/* */
function set_header_size()
{
//filesize headers
header('Content-Length: ' . $this->file_size );
}
// --------------------------------------------------------------------
/* */
function output()
{
$output = $this->export();
$this->set_headers();
$this->set_header_size();
echo $output;
exit();
}
// --------------------------------------------------------------------
/* */
function export()
{
$storage = $this->storage;
ob_start();
if( $storage != '' and is_array($storage) and count($storage) > 0 )
{
$xmlString = '';
foreach( $storage as $string )
{
$xmlString .= $this->newline . $string;
}
echo '<taconite>'.$xmlString.$this->newline.'</taconite>';
}else{
echo '<taconite><replaceContent select="error-notice">There is nothing to output</replaceContent></taconite>';
}
$this->file_size = ob_get_length();
return ob_get_clean();
}
}/*[=- End class jquery -=]*/
@hillbilly
Copy link
Author

hillbilly commented Jul 18, 2013

Version 5

Now supports the __call magic method
You can now create of elements like this:

$jquery->append('.some-class','This is really cool')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment