Location: PHPKode > projects > Pindorama > pindorama/active/components/tools.php
<?php
/**
*                                                          
* @name    Pindorama Core Configuration                    
* @version 0.1
* @author  Guilherme Capilé <hide@address.com>           
*                                                          
*/
/*
    $pcom["tools.pin_docindex"]="/tools/docindex.dav";
    $pcom["tools.pin_xmlrpc_test"]="/tools/xmlrpc.dav";
    $pcom["tools.pin_clean_public"]="/tools/clean.dav";
    */


/**
*                                                          
* @name        pin_docindex
* @deprecated  
* @version     0.11
*
* @author      Guilherme Capilé <hide@address.com>
*
* @global      $arg
* @global      $param
* @global      $c
* @global      $l
* 
* @see         pin_send_message()
* @see         pin_redirect()
* @see         pin_docindex_pages()
* @see         pin_xml_array()
* @see         pin_xsl_schemes()
* @see         pin_docindex_update()
* @see         pin_copy_xpath()
*
* @return      
*/
function pin_docindex()
{
    global $l,$arg, $param, $c;
    $di_file = $c["index"]."docindex.inc";
    $arg["docindex_template"] = $arg["empty"];
    $arg["docindex_datasets"] = $arg["empty"];
    $arg["docindex_pages"] = $arg["empty"];
    if (is_file($di_file)) {
        $di = file_get_contents($di_file);
        $di = unserialize($di);
    }
    if ($_GET["docindex"] != "") {
        $id = $_GET["docindex"];
        if (!is_array($di[$id])) {
            pin_send_message($l["tools"]["nodocindex"]);
            pin_redirect($_SERVER["SCRIPT_NAME"]);
        }
        if (count($_POST) > 0) {
            //updating the model
            if (is_array($_POST["ixml"]) && count($_POST["ixml"]) > 0) {
                $di[$id]["template"] = $c["xmlpi"].pin_xml_array($_POST["ixml"], TRUE, TRUE);
            }
            if (preg_match("/\#([^\:]+)\:([^\s]+)/", $_POST["template"], $matches)) {
                $template = pin_xsl_schemes ("", $matches[1], $matches[2]);
                if ($template != $arg["empty"]) {
                    $di[$id]["template"] = str_replace ($matches[1].":".$matches[2], "[[@id]]", $template);
                } else {
                    $template = "";
                }
            }
            if ($_POST["title"] != "") {
                $di[$id]["title"] = $_POST["title"];
            }
            if ($_POST["active"] != "") {
                $di[$id]["active"] = $_POST["active"];
            }
            if ($_POST["overwrite"] != "") {
                $di[$id]["overwrite"] = $_POST["overwrite"];
            }
            if (pin_save_file($di_file, serialize($di))) {
                pin_send_message($l["tools"]["docindex_updated"]);
            }
        }
        if ($_GET["edit"] == "pages") {
            $arg["docindex_pages"] = pin_docindex_pages($di[$id]);
            if ($_GET["update"] == 1){
                pin_docindex_update($di[$id]);
                pin_redirect($_SERVER["SCRIPT_NAME"]."?docindex=$id&edit=pages");
            }
        }
        $param["title"] .= ": {$di[$id]['title']}";
        $arg["docindex_template"] = $di[$id]["template"];
        $arg["docindex_datasets"] = pin_copy_xpath("/xpml/resource[@ref='{$di[$id]['index']}']", "index:datasets", TRUE);
        $param["docindex_index"] = $di[$id]["index"];
        $param["docindex_title"] = $di[$id]["title"];
        $param["docindex_active"] = $di[$id]["active"];
        $param["docindex_overwrite"] = $di[$id]["overwrite"];
        if (!preg_match("/\<\/xpml\>\s*$/", $arg["docindex_template"]) || $_GET["delete"] == 1) {
            unset($di[$id]);
            if (pin_save_file($di_file, serialize($di))) {
                pin_send_message($l["tools"]["docindex_deleted"]);
            }
            redirect($_SERVER["SCRIPT_NAME"]);
        }
    } else if ($_POST["index"] != "" && $_POST["template"] != ""){
        //new docindex
        if (preg_match("/\#([^\:]+)\:([^\s]+)/", $_POST["template"], $matches)) {
            $template = pin_xsl_schemes ("", $matches[1], $matches[2]);
            if ($template != $arg["empty"]) {
                $template = str_replace ($matches[1].":".$matches[2], "[[@id]]", $template);
            } else {
                $template = "";
            }
        }
        if (preg_match("/^[^\:]+\:[^\#]+$/", $_POST["index"])) {
            $datasets = pin_xsl_schemes ("", "index", "datasets#".$_POST["index"]);
            if ($datasets != $arg["empty"]) {
                $index = $_POST["index"];
            } else {
                $datasets = "";
            }
        }
        if ($template != "" && $datasets != "") {
            //creating the docindex model
            $tmp = "";
            $tmp["template"] = $template;
            $tmp["index"] = $index;
            $tmp["title"] = $_POST["title"];
            $di[] = $tmp;
            if (pin_save_file($di_file, serialize($di))) {
                end($di);
                $id = key($di);
                pin_send_message($l["tools"]["docindex_created"]);
                pin_redirect($_SERVER["SCRIPT_NAME"]."?docindex=$id");
            }
        }
    }
        
    $arg["di"] = $c["xmlpi"]."<xpml>";
    if (is_array($di)) {
        foreach ($di as $k=>$v) {
            $arg["di"] .= "<ref id=\"$k\">{$v['title']}</ref>";
        }
    }
    $arg["di"] .= "</xpml>";
    pin_output_header ($param["script-name"], FALSE, "php");
    echo pin_xsl_process ("arg:empty", $c["stylesheets"]."dav/docindex.xsl");
} // docindex


/**
*                                                          
* @name        pin_docindex_pages
* @deprecated  
* @version     0.11
*
* @author      Guilherme Capilé <hide@address.com>
*
* @param       $di
*
* @global      $arg
* @global      $param
* @global      $c
* @global      $l
* 
* @see         pin_read_xpath()
* @see         pin_xsl_schemes()
* @see         pin_docindex_cleanurl()
*
* @return      
*/
function pin_docindex_pages($di)
{
    global $l,$arg, $param, $c;
    
    // get the URL schema in the template
    $arg["tmp"] = $di["template"];
    $urls = array();
    $arr = $arg["empty"];
    $url = pin_read_xpath("/xpml/meta/identifier[@type='url']", "arg:tmp");
    if (is_array($url) && count($url) == 1) {
        $url = $url[0];
    } else {
        return $arr;
    }
    // get the parts that are inserted by the template
    if (preg_match_all("/\[\[([^\]]+)\]\]/", $url, $matches)) {
        $parts = $matches[1];
        $rules = "";
        foreach ($parts as $tmp) {
            $rules .= "<xsl:for-each select=\"/xpml/resource/ref\">\$result[<xsl:value-of select=\"@id\"/>]['$tmp']='<xsl:value-of select=\"translate(.//$tmp/text(), \$from, \$to)\"/>';\n</xsl:for-each>";
        }
        $arg["xsl"] = <<<FIM
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="iso-8859-1" indent="no" method="xml" omit-xml-declaration="yes"/>
<xsl:variable name="from">'</xsl:variable>
<xsl:variable name="to">’</xsl:variable>
<xsl:template match="/">$rules</xsl:template>
</xsl:stylesheet>
FIM;
        eval(pin_xsl_process($di["index"], "arg:xsl"));
        if (is_array($result) && count($result) > 0) {
            // limpando os reultados vazios
            
            $res = array();
            foreach ($result as $k=>$v) {
                if (!in_array("", $v)) {
                    $res[$k] = $v;
                }
            }
            $arr = "<xpml>";
            foreach ($res as $k=>$v) {
                $temp = $url;
                foreach ($v as $k2=>$v2) {
                    $temp = str_replace("[[$k2]]", pin_docindex_cleanurl($v2), $temp);
                }
                if (!in_array($temp, $urls)) {
                    $urls[$k] = $temp;
                    $arr .= "<url id=\"$k\">$temp</url>";
                }
            }
            $arg["urls"] = $urls;
            $arr .= "</xpml>";
        }
    }
    return $arr;
}

/**
*                                                          
* @name        pin_docindex_cleanurl
* @deprecated  
* @version     0.11
*
* @author      Guilherme Capilé <hide@address.com>
*
* @param       $str
* 
* @return      String limpa
*/
function pin_docindex_cleanurl($str)
{
    $str =  strtr(strtolower(html_entity_decode($str)), " \$#@!\"+,\\/.()*%àáãâäéèêëíìîïóòõôöúùûüç&'", "________________aaaaaeeeeiiiiooooouuuuc_");
    return $str;
}


/**
*                                                          
* @name        pin_docindex_update
* @deprecated  
* @version     0.11
*
* @author      Guilherme Capilé <hide@address.com>
*
* @param       $di
*
* @global      $l
* @global      $arg
* @global      $param
* @global      $c
* 
* @see         pin_read_xpath()
* @see         pin_xsl_process()
* @see         pin_save_file()
* @see         pin_xsl_schemes_update()
* @see         pin_send_message()
* 
* @return      String limpa
*/
function pin_docindex_update($di)
{
    global $l,$arg, $param, $c;
    // get the URL schema in the template
    $pages = $arg["urls"];
    $template = $di["template"];
    if (!is_array($pages) || count($pages) <= 0){
        return false;
    }
    list($url) = pin_read_xpath("/xpml/meta/identifier[@type='url']", "arg:tmp");
    if (preg_match_all("/\[\[([^\]]+)\]\]/", $template, $matches)) {
        $parts = $matches[1];
        $rules = "";
        foreach ($parts as $tmp) {
            $rules .= "<xsl:for-each select=\"/xpml/resource/ref\">\$result[<xsl:value-of select=\"@id\"/>]['$tmp']='<xsl:value-of select=\"translate(.//$tmp/text(), \$from, \$to)\" disable-output-escaping=\"yes\"/>';\n</xsl:for-each>";
        }
        $arg["xsl"] = <<<FIM
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="iso-8859-1" indent="no" method="xml" omit-xml-declaration="yes"/>
<xsl:variable name="from">'</xsl:variable>
<xsl:variable name="to">’</xsl:variable>
<xsl:template match="/">$rules</xsl:template>
</xsl:stylesheet>
FIM;
        eval(pin_xsl_process($di["index"], "arg:xsl"));
    }
    // all the records in $result
    $arg["xsl"] = <<<FIM
<?xml version="1.0" encoding="iso-8859-1" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="iso-8859-1" indent="no" method="xml" omit-xml-declaration="yes"/>
<xsl:variable name="from">'</xsl:variable>
<xsl:variable name="to">’</xsl:variable>
<xsl:template match="/"><xsl:for-each select="/xpml/resource//uri">\$uris['<xsl:value-of select="@id"/>']='<xsl:value-of select="translate(., \$from, \$to)" disable-output-escaping="yes"/>';
</xsl:for-each></xsl:template>
</xsl:stylesheet>
FIM;
    eval(pin_xsl_process("index:uris", "arg:xsl"));
    $eval = "";
    $now = date("Y-m-d\TH:i:s");
    $scheme = "db:";
    $next = file_get_contents($c["db"]."next");
    // loop through all the pages, building the document and saving it.
    $upd = 0;
    $crt = 0;
    foreach($result as $k=>$v) {
        $eval = "\$doc = '".preg_replace("/\[\[([^\]]+)\]\]/", "'.docindex_cleanurl(\$v['\$1']).'", $url)."';\n";
        eval($eval);
        if ($doc == $url || preg_match("/[\/\.]{2}/", $doc)) {
            continue;
        }
        $v["@date-modified"] = $now;
        $v["@rid"] = $k;
        $exist = array_search($doc, $uris);
        if ($exist != false) {
            // exists
            if (!$di["overwrite"]) {
                continue;
            }
            $v["@id"] = $exist;
            $xmlfile = $c["db"].str_replace("db:", "", $exist).".xml";
            list($v["@date-created"]) = pin_read_xpath("/xpml/meta/date[@type='created']", $exist);
            $upd++;
        } else {
            // does not exist
            $v["@id"] = $scheme.$next;
            $xmlfile = $c["db"].$next.".xml";
            $next = substr($next, 0, strpos($next, "_n"))."_n".str_pad(((int)substr($next, 5) + 1), 5, "0", STR_PAD_LEFT);
            $v["@date-created"] = $now;
            $crt++;
        }
        $eval = "\$xml = '".preg_replace("/\[\[([^\]]+)\]\]/", "'.\$v['\$1'].'", str_replace($url, $doc, $arg["tmp"]))."';";
        eval($eval);
        $update_uris[$v["@id"]] = $doc;
        pin_save_file($xmlfile, $xml);
    }
    pin_save_file($c["db"]."next", $next);
    // after all documents, make a batch update in the indexes index:uris and index:sitemap?
    if (is_array($update_uris)) {
        $arg["index_update"] = $c["xmlpi"]."<resource name=\"uris\">";
        foreach ($update_uris as $k=>$v) {
            $arg["index_update"] .= "\n<uri id=\"$k\">$v</uri>";
        }
        $arg["index_update"] .= "</resource>";
        $tmp = $arg["index_update"];
        pin_xsl_schemes_update ("5000", "index", "uris");
        $arg["index_update"] = "";
    }
    $msg = "";
    if ($upd == 0 && $crt == 0) {
        pin_send_message($l["tools"]["docindex_noneupdated"]);
    } else {
        eval("\$msg = \"{$l['tools']['docindex_docupdated']}\";");
        pin_send_message($msg);
    }
    /*if (is_array($update_sitemap)) {
        $arg["index_update"] = $c["xmlpi"];
        $arg["index_update"] .= "<page ref=\"$xmlref\"><ref>".$info[1][0]."</ref><ref-type>".$info[2][0]."</ref-type></page>";
        xsl_schemes_update ($xmlref, "index", "sitemap");
        $arg["index_update"] = "";
    }*/
}


/**
*                                                          
* @name        pin_xmlrpc_test
* @deprecated  
* @version     0.11
*
* @author      Guilherme Capilé <hide@address.com>
*
* @global      $l
* @global      $arg
* @global      $param
* @global      $c
* @global      $xmlrpc_err
* 
* @see         pin_read_xpath()
* @see         pin_xsl_process()
* @see         pin_save_file()
* @see         pin_xsl_schemes_update()
* @see         pin_send_message()
* 
* @return      
*/
function pin_xmlrpc_test()
{
    global $l,$arg, $param, $c, $xmlrpc_err;
    $cmd = $c["base"]."/active/schedule > {$c['var']}log/schedule.log &";
    /*@header("Content-Type: text/plain");
    echo $cmd;
    echo passthru($cmd);
    echo "\nok";
    exit();*/
    require_once($c["components"]."publication.php");
    require_once($c["components"]."xmlrpc/client.php");
    $method = "ping.ping";
    $msg = "Hello";
    //@header("Content-Type: text/plain");
    foreach ($c["remote_server"] as $rs) {
        $xmlrpc_r = xmlrpc_request($rs, $method);
        if ($xmlrpc_err != "") {
            echo "erro conectando com {$rs}\n";
            print_r ($xmlrpc_err);
            break;
        } else {
            print_r ($xmlrpc_r);
        }
    }
    exit();
}


/**
*                                                          
* @name        pin_batch_publication
* @deprecated  
* @version     0.11
*
* @author      Guilherme Capilé <hide@address.com>
*
* @global      $l
* @global      $arg
* @global      $param
* @global      $c
* @global      $xmlrpc_err
* 
* @see         pin_output_header()
* @see         pin_xsl_process()
* 
* @return      
*/
function pin_batch_publication()
{
    global $l,$arg, $param, $c, $xmlrpc_err;
    $cmd = $c["base"]."/active/schedule > {$c['var']}log/schedule.log &";
    passthru($cmd);
    $arg["message"] = $c["xmlpi"]."<xpml><meta><title>{$l['tools']['batch_publish']}</title></meta><resource><p>{$l['tools']['batch_publish_start']}</p></resource></xpml>";
    pin_output_header ($param["script-name"], FALSE, "php");
    echo pin_xsl_process ("arg:message", $c["stylesheets"]."dav/msg.xsl");
}


/**
*                                                          
* @name        pin_clean_public
* @deprecated  
* @version     0.11
*
* @author      Guilherme Capilé <hide@address.com>
*
* @global      $l
* @global      $arg
* @global      $param
* @global      $c
* @global      $xmlrpc_err
* 
* @see         pin_read_rdir()
* @see         pin_check_host()
* @see         pin_output_header()
* @see         pin_xsl_process()
* 
* @return      
*/
function pin_clean_public()
{
    global $l,$arg, $param, $c, $xmlrpc_err;
    require_once($c["components"]."publication.php");
    $uris = file_get_contents($c["index"]."/uris.xml");
    $removed = "";
    if (is_array($c["remote_host"])) {
        foreach ($c["remote_host"] as $host=>$info){
            $root = ($info["document_root"] != "")?($info["document_root"]):($host);
            $root = $c["xmlrpc_s"]["document_root"]."/{$root}";
            $files = pin_read_rdir($root, "fullname name is_dir", "*", true);
            $files = array_reverse($files);
            foreach ($files as $file) {
                $url = substr($file["fullname"], strlen($root));
                if ($file["is_dir"]!=1) {
                    if (!pin_check_host($url, $info)) {
                        if (@unlink($file["fullname"])){
                            $removed .= "\n".$file["fullname"];
                        }
                        continue;
                    }
                    $valid = false;
                    if (!(strpos($uris, ">{$url}</uri>") > 0 || is_file($c["www"].$url))) {
                        if (@unlink($file["fullname"])){
                            $removed .= "\n".$file["fullname"];
                        }
                        continue;
                    }
                } else if (count(pin_read_rdir($file["fullname"])) == 0) {
                    if (@rmdir($file["fullname"])) {
                        $removed .= "\n".$file["fullname"];
                    }
                }
            }
        }
    } else if (is_dir($c["xmlrpc_s"]["document_root"])) {
        $root = $c["xmlrpc_s"]["document_root"];
        $files = pin_read_rdir($root, "fullname name is_dir", "*", true);
        $files = array_reverse($files);
        foreach ($files as $file) {
            $url = substr($file["fullname"], strlen($root));
            if ($file["is_dir"]!=1) {
                if (!(strpos($uris, ">{$url}</uri>") > 0 || is_file($c["www"].$url))) {
                    if (@unlink($file["fullname"])){
                        $removed .= "\n".$file["fullname"];
                    }
                    continue;
                }
            } else if (count(pin_read_rdir($file["fullname"])) == 0) {
                if (@rmdir($file["fullname"])) {
                    $removed .= "\n".$file["fullname"];
                }
            }
        }
    }
    $arg["message"] = ($removed == "")?($c["xmlpi"]."<xpml><meta><title>{$l['msg']['no_files_delete']}</title></meta><resource><p>{$l['msg']['no_files_delete']}</p></resource></xpml>"):("<xpml><meta><title>{$l['msg']['files_delete']}</title></meta><resource><p>{$l['msg']['files_delete']}</p><pre>$removed</pre></resource></xpml>");
    $param["title"] = $l["sitemap"]["title"];
    pin_output_header ($param["script-name"], FALSE, "php");
    echo pin_xsl_process ("arg:message", $c["stylesheets"]."dav/msg.xsl");
    //print_r(read_rdir($c["www"]));
}
?>
Return current item: Pindorama