Location: PHPKode > scripts > HTML Editor > html-editor/html_editor.php
<?php
class Html_Editor {
    
    var $body   = array();
    var $title  = array();
    var $meta   = array();
    var $meta_http   = array();
    var $link   = array();
    var $css    = array();
    var $docID  = 0;
    var $body_attr;
    
    function __construct(){
        //if($title) { $this->newDocument($title); }
    }
    
    function newDocument($title){
        $this->docID++;
        $this->addTitle($this->docID, $title);
        return $this->docID;
    }

    function addTitle($doc, $title){
        $this->title[$doc] = $title;
    }
    
    function addMeta($doc, $data){
        $this->meta[$doc] = $data;
    }

    function addMetaHttp($doc, $data){
        $this->meta_http[$doc] = $data;
    }
    
    function linkCss($doc, $file, $attr = FALSE){
        $atributes = array(
            'rel'   => 'stylesheet',
            'media' => 'screen',
            'type'  => 'text/css'
        );
        if($attr) {
            if(is_array($attr)) {$atributes = $attr;}
        }
        $atributes = $this->fetchAtr($atributes);
        $this->link[$doc][] = sprintf('<LINK href="%s"%s>', $file, $atributes);
    }
    
    function addHeadAdditions($doc, $add){
        $this->head_additions[$doc][] = $add;
    }
    
    
    function highlightText($tag, $text2highlight, $text, $attr = ''){
        if($attr && is_array($attr)){$attributes = $this->fetchAtr($attr);}
        return str_replace($text2highlight, "<{$tag}{$attributes}>{$text2highlight}</{$tag}>", $text); 
    }

    function makeImg($src, $options = ''){
        if($options && is_array($options)){
            $attributes = $this->fetchAtr($options);
        }
        if(!file_exists($src)){
            //return false;
        }
        $size = @getimagesize($path);
        $img = sprintf(
            "<img src=\"%s\" align=\"absmiddle\" %s%s>",
            $src, $size[3], $attributes
        );
        return $img;        
    }
    
    function makeLink($href, $label, $options = ''){
        if($options && is_array($options)){
            $attributes = $this->fetchAtr($options);
        }

        $lnk = sprintf(
            "<a href=\"%s\" onfocus=\"this.blur()\"%s>%s</a>",
            $href, $attributes, $label
        );
        return $lnk;

    }
    function writeHtml($doc, $tag, $str, $attr = ''){
        $this->body[$doc] .= $this->getHtml($tag, $str, $attr);
    }
    
    function getHtml($tag, $str, $attr = ''){
        $attributes = '';
        
        if($attr && is_array($attr)){$attributes = $this->fetchAtr($attr);}
        $out = sprintf('<%s%s>%s</%s>',$tag, $attributes, $str, $tag);
        return $out;
    }
    
    function docGet($doc){
        $document = sprintf(
            '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">'.
            '<html>'.
            '<head>'.
            '<title>%s</title>'.
            '%s'. // meta
            '%s'. // links
            '%s'. // css
            '%s'. // addition
            '</head>'.
            '<body%s>'.
            '%s'. // body
            '</body>'.
            '</html>',
            $this->title[$doc],
            $this->getMeta($doc),
            implode('\n', $this->link[$doc]),
            $this->css[$doc] ? $this->getHtml('style',$this->css[$doc]) : FALSE,
            implode('',$this->head_additions[$doc]),
            $this->fetchAtr($this->body_attr[$doc]),
            $this->body[$doc]
        );
        //$tidy = tidy_parse_string($document, dirname(__FILE__).'/tidy.cfg'); 
        //tidy_clean_repair($tidy); 
        //return $tidy;
        return $document;
    }
    
    function docSave($doc, $path){
        $fp = fopen($path, 'w');
        fwrite($fp, $this->docGet($doc));
        fclose($fp);
    }
    
    function docShow($doc){
        echo $this->docGet($doc);
    }
    
    
    function setBodyAttr($doc, $attr){
        if(is_array($attr))$this->body_attr[$doc] = $attr;    
    }
    
    function getMeta($doc){
        $out = '';
        foreach($this->meta_http[$doc] as $k => $v){
            $out .= sprintf('<meta http-equiv="%s" content="%s">',$k,$v);
        }
        foreach($this->meta[$doc] as $k => $v){
            $out .= sprintf('<meta name="%s" content="%s">',$k,$v);
        }
        return $out;
    }
    
    function fetchAtr($attr){
        $attributes = '';
        if(!$attr){return false;}
        if(is_array($attr)){
            foreach($attr AS $a => $v){
                $attributes .= sprintf(' %s="%s"', $a, $v);
            }
        } else {
            $this->error('The $attr value is not an array'); 
        }
        return $attributes;
    }
    
    function error($msg){
    
    }
}

class CSS_Editor {
    
    var $elementID = 0;
    var $element;
    var $properties;
    
    function setElement($element){
        $elementID++;
        $this->element[$elementID] = $element;
        return $elementID;
    }
    
    function setProperties($elementID, $properties){
        if(!is_array($properties)) {return false;}
        $this->properties[$elementID] = $properties;
    }
    
    function getCss(){
        $out = '';
        foreach($this->element AS $id => $name){
            $out .= sprintf("%s {\n", $name);
            foreach($this->properties[$id] as $atr => $val){
                $out .= sprintf("    %s:%s;\n", $atr, $val);
            }
            $out .= "}\n\n";
        }
        return $out;
    }
    
    function saveCss($path){
        $fp = fopen($path, 'w');
        fwrite($fp, $this->getCss());
        fclose($fp);
    }
}
?>
Return current item: HTML Editor