<?php
/**
*
* @name Pindorama Core Configuration
* @version 0.1
* @author Guilherme Capilé <hide@address.com>
*
*/
/**
*
* @name pin_publish_dav
* @deprecated
* @version 0.11
*
* @author Guilherme Capilé <hide@address.com>
*
* @param $xmlref (Default = "")
*
* @global $arg
* @global $param
* @global $c
* @global $l
* @global $g
* @global $pcom
*
* @see pin_read_rdir()
* @see pin_redirect()
* @see pin_xsl_process()
* @see pin_output_header()
* @see pin_read_xpath()
* @see pin_delete_scheduled_export()
* @see pin_schedule_export()
* @see pin_send_message()
* @see pin_send_email()
* @see pin_process_todo()
* @see pin_parse_cvs()
*
* @return
*/
function pin_publish_dav ( $xmlref = "" )
{
global $c, $param, $l, $g, $pcom, $arg;
if ($_SERVER["REMOTE_USER"] != "") {
$user = $_SERVER["REMOTE_USER"];
} else if ($_SERVER["PHP_AUTH_USER"] != "") {
$user = $_SERVER["PHP_AUTH_USER"];
} else {
$user = $param["user"];
}
if (!is_array($g)) {
include_once($c["users"]."permissions.php");
}
if ($c["permissions"]) {
$perm = FALSE;
if (in_array($user, explode(" ", $g["editor"]))) {
$perm = TRUE;
}
}
if (!$perm) {
//send_message($l["msg"]["file"].$xmlref.$l["msg"]["file_locked"]);
$param["read-only"] = "all";
}
$filenames = array();
$today = date("Ymd");
$when = "";
$id = "";
$xpath = "";
if ($_GET["when"] != "" && preg_match("/^([0-9]{4})\-([0-9]{2})\-([0-9]{2})(T([0-9]{2})\:([0-9]{2}))?/", $_GET["when"], $matches)){
$when = $matches[1].$matches[2].$matches[3].$matches[5].$matches[6];
}
if (strlen($when) < 12) {
$when = str_pad($when, 12, "0");
}
if ($when < date("YmdHi")) {
$when = date("YmdHi");
}
$delete = array();
if ($_GET["start"] == 1){
if ($_GET["uris"] != "" || $_GET["ids"] != "") {
$uris = split(" ",$_GET["uris"]);
foreach ($uris as $uri) {
if ($uri != "" && is_file($c["www"].$uri)) {
$filenames[] = $uri;
} else if ($uri != "" && preg_match("/^(http\:\/\/[^\/]*)?\/*(\/[^\?\#]*)/", $uri, $matches)) {
$scriptname = $matches[2];
if (substr($scriptname, -1) == "/") {
$scriptname = substr($scriptname, 0, strlen($scriptname) -1);
}
if (preg_match("/([^\*]*)\*(.*)/", $scriptname, $matches)){
$xp = "";
$dir = preg_replace("/\/[^\/]*$/", "", $matches[1]);
$pat = preg_replace("/.*\/([^\/]*)$/", "\$1", $matches[1])."*";
$files = pin_read_rdir($c["www"].$dir, "fullname name is_dir", $pat);
if (is_array($files)) {
foreach ($files as $file) {
$filenames[]=str_replace($c["www"], "", $file["fullname"]);
}
}
if ($matches[1] != "") {
$xp .= "and starts-with(., '{$matches[1]}')";
}
if ($matches[2] != "") {
$xp .= "and contains(., '".str_replace("*", "') and contains(., '", $matches[2])."')";
}
$xpath[] = "/xpml/resource/uri[".substr($xp, 4)."]/@id";
} else {
$xpath[] = "/xpml/resource/uri[ . = '$scriptname']/@id";
foreach ($c["dir_index"] as $tmp) {
$xpath[] = "/xpml/resource/uri[ . = '$scriptname/$tmp']/@id";
}
}
}
}
$ids = split(" ",$_GET["ids"]);
foreach ($ids as $id) {
if ($id != "" && preg_match("/^[a-z0-9\:\.\/\-\_]+$/i", $id)) {
$xpath[] = "/xpml/resource/uri[@id = '$id']/@id";
if ($_GET["ancestors"] > 0){
$anc = (int)$_GET["ancestors"];
$xpath[] = "/xpml/resource/uri[@id = document('index:sitemap')//page[@ref='$id']/ancestor::page[position() <= $anc]/@ref]/@id";
}
if ($_GET["descendants"] > 0){
$desc = (int)$_GET["descendants"];
$xpath[] = "/xpml/resource/uri[@id = document('index:sitemap')//page[@ref='$id']/descendant::page[position() <= $desc]/@ref]/@id";
}
}
}
if (is_array($xpath)){
$id = pin_read_xpath($xpath, "index:uris");
}
}
$todo = "";
$results = array();
if (is_array($id)) {
$allids = array();
foreach ($id as $tmp){
$allids = array_merge($allids, $tmp);
}
foreach ($allids as $k=>$v){
$todo[$k] = "/xpml/resource/uri[@id = '{$v}']";
}
$todo = pin_read_xpath($todo, "index:uris");
if (is_array($todo)){
foreach ($todo as $k=>$v){
$results[pin_schedule_export($when, $allids[$k], $v[0], 1, $user)][] = $v[0];
}
}
}
if (is_array($filenames)){
foreach ($filenames as $tmp){
$results[pin_schedule_export($when, "none", $tmp, 0, $user)][] = $tmp;
}
}
//messaging and notification
$msg = "";
if (count($results[1]) > 0){
if (count($results[1]) == 1) {
$msg .=$l["publish"]["doc"].$results[1][0].$l["publish"]["doc_scheduled"]."\n\n";
} else {
$msg .=$l["publish"]["docs"].implode(", ", $results[1]).$l["publish"]["docs_scheduled"]."\n\n";
}
}
if (count($results[2]) > 0){
require_once($c["components"]."email.php");
$pages = implode("<br />", $results[2]);
$subject = $l["publish"]["email_subject"];
$message = $l["publish"]["email_message"]."<p>$pages</p>";
if (pin_send_email($user, $g["editor"], $subject, $message)) {
if (count($results[2]) == 1) {
$msg .=$l["publish"]["doc_notify"].$results[2][0].".\n\n";
} else {
$msg .=$l["publish"]["docs_notify"].implode(", ", $results[2]).".\n\n";
}
} else {
if (count($results[2]) == 1) {
$msg .=$l["publish"]["doc_cant_notify"].$results[2][0].$l["publish"]["doc_approval"]."\n\n";
} else {
$msg .=$l["publish"]["docs_cant_notify"].implode(", ", $results[2]).$l["publish"]["docs_approval"]."\n\n";
}
}
}
if (is_array($_GET["delete"])) {
$indexes = array("todo", "approval");
foreach ($_GET["delete"] as $k=>$v) {
if (in_array($k, $indexes) && $v != "" && preg_match("/^([a-z0-9\.\/\:\-\_]+)@([0-9]+)/i", $v, $matches)) {
$delete[$k]["when"] = $matches[2];
$delete[$k]["identifier"] = $matches[1];
}
}
}
if (is_array($delete)) {
foreach ($delete as $k=>$v) {
$msg .= pin_delete_scheduled_export($v["when"], $v["identifier"], $k);
}
}
if ($msg != "") {
pin_send_message(addcslashes(trim($msg), "\n"));
}
}
if ($_GET["start"] && ($_GET["uris"] != "" || $_GET["ids"] != "") || $_GET["delete"] != "") {
pin_redirect($_SERVER['PHP_SELF']);
exit();
}
if ($_GET["noupdate"] == "" && !$c["only_cron_publish"]) {
//require_once($c["components"]."cron.php");
pin_process_todo();
}
$arg["xml"] = $c["xmlpi"]."<xpml><meta><title>{$l['publish']['title']}</title></meta><resource type='cron'>".
$arg["xml"] .= "<todo>".pin_parse_cvs(@file_get_contents($c["index"]."todo"))."</todo>\n";
$arg["xml"] .= "<done>".pin_parse_cvs(@file_get_contents($c["index"]."done.$today"))."</done>\n";
$arg["xml"] .= "<approval>".pin_parse_cvs(@file_get_contents($c["index"]."approval"))."</approval>\n";
$arg["xml"] .= "</resource></xpml>";
pin_output_header ($param["script-name"], FALSE, "php");
$result = pin_xsl_process ("arg:xml", $c["stylesheets"]."dav/export.xsl");
if (!headers_sent()) {
header("Content-Length: ".strlen($result));
}
echo $result;
}
/**
*
* @name pin_parse_cvs
* @deprecated
* @version 0.11
*
* @author Guilherme Capilé <hide@address.com>
*
* @param $str
* @param $sep (Default = '(\t| )+')
*
* @return
*/
function pin_parse_cvs ($str, $sep = '(\t| )+') {
return "<line><value>".preg_replace(array("/$sep/", "/\r?\n/"), array("</value><value>", "</value></line><line><value>"), $str)."</value></line>";
}
/**
*
* @name pin_schedule_export
* @deprecated
* @version 0.11
*
* @author Guilherme Capilé <hide@address.com>
*
* @param $when
* @param $id
* @param $identifier
* @param $const
* @param $user
*
* @global $param
* @global $arg
* @global $c
* @global $g
*
* @see $pin_check_permission()
*
* @return
*/
function pin_schedule_export($when, $id, $identifier, $const, $user)
{
global $param, $arg, $c, $g;
if (pin_check_permission($id, "publish", $user)) {
$fp = fopen($c["index"]."todo", "a");
fwrite($fp, "$when\t$const\t$id\t$identifier\t$user\n");
fclose($fp);
//return "O arquivo $identifier foi agendado para publicação.\n";
return 1;
} else {
$fp = fopen($c["index"]."approval", "a");
fwrite($fp, "$when\t$const\t$id\t$identifier\t$user\n");
fclose($fp);
//return "O editor responsável foi notificado da publicação do arquivo $identifier.\n";
return 2;
}
}
/**
*
* @name pin_delete_scheduled_export
* @deprecated
* @version 0.11
*
* @author Guilherme Capilé <hide@address.com>
*
* @param $when
* @param $identifier
* @param $index
*
* @global $c
* @global $param
* @global $arg
*
* @see pin_check_permission()
*
* @return
*/
function pin_delete_scheduled_export($when, $identifier, $index)
{
global $param, $arg, $c;
$delete = FALSE;
$lines = file($c["index"].$index);
$contents = "";
foreach ($lines as $line) {
if (preg_match("/^([0-9]{12})\s+([0-9]{1})\s+([a-z0-9\.\:\/\-\_]+)\s+([a-z0-9\.\/\-\_]+)\s([a-z0-9\.\/\-\_]+)?/i", trim($line), $matches)){
if ($matches[1] == $when && $matches[4] == $identifier && @pin_check_permission($matches[3], "publish")) {
$delete = TRUE;
} else {
$contents .= $matches[1]."\t".$matches[2]."\t".$matches[3]."\t".$matches[4]."\t".$matches[5]."\n";
}
} else {
$contents .= $line;
}
}
if ($delete) {
$fp = @fopen($c["index"].$index, "w");
@fwrite($fp, $contents);
@fclose($fp);
return $l["publish"]["doc"].$identifier.$l["publish"]["doc_delete"]."\n\n";
} else {
return $l["publish"]["doc"].$identifier.$l["publish"]["doc_cant_delete"]."\n\n";
}
}
/**
*
* @name pin_remote_copy
* @deprecated
* @version 0.11
*
* @author Guilherme Capilé <hide@address.com>
*
* @param $files
* @param $contents
*
* @global $c
* @global $l
*
* @see xmlrpc_request()
* @see pin_send_message()
*
* @return
*/
function pin_remote_copy($files, $contents)
{
global $c, $l;
require_once($c["components"]."xmlrpc/client.php");
$method = "publication.publish_files";
foreach ($c["remote_server"] as $rs) {
$xmlrpc_r = xmlrpc_request( $rs, $method, array($files, $contents));
if (!is_array($xmlrpc_r) || !$xmlrpc_r["result"]) {
return false;
break;
} else {
return true;
pin_send_message($xmlrpc_r["msg"]."\n\n");
}
}
return true;
}
/**
*
* @name pin_local_copy
* @deprecated
* @version 0.11
*
* @author Guilherme Capilé <hide@address.com>
*
* @param $files
* @param $contents
*
* @global $c
* @global $l
*
* @see pin_save_file()
*
* @return
*/
function pin_local_copy($files, $contents)
{
global $c, $l;
for($i=0; $i < @count($files); $i++) {
if (is_array($c["remote_host"]) && is_array($contents[$i])) {
foreach ($c["remote_host"] as $host=>$info){
if ($contents[$i][$host] != "") {
$document_root = ($info["document_root"] != "")?($info["document_root"]):($host);
if (!pin_save_file($c["xmlrpc_s"]["document_root"]."/{$document_root}".$files[$i], $contents[$i][$host])) {
@error_log ("Error while publishing ".$files[$i], 3, $c["var"]."log/publication.log");
//return false;
}
}
}
} else if (!pin_save_file($c["xmlrpc_s"]["document_root"].$files[$i], $contents[$i])) {
@error_log ("Error while publishing ".$files[$i], 3, $c["var"]."log/publication.log");
//return false;
}
}
return true;
}
/**
*
* @name pin_process_todo
* @deprecated
* @version 0.11
*
* @author Guilherme Capilé <hide@address.com>
*
* @global $c
* @global $l
* @global $param
*
* @see pin_output_xml()
* @see pin_save_file()
* @see pin_check_host()
* @see pin_remote_copy()
* @see pin_local_copy()
* @see pin_send_message()
*
* @return
*/
function pin_process_todo()
{
global $c, $l, $param;
$todo = $c["index"]."todo";
//verifica a existência dos arquivos de publicação e notificação, e procura por registros recentes
$checkfile = $c["var"]."check.publish";
if (is_file($checkfile)) {
// checkfile exists -- there must be someone publishing some pages
// see if it's up for more than 20 min
$then = trim(file_get_contents($checkfile));
$then = (!is_numeric($then))?(filemtime($checkfile)):($then);
if (!is_numeric($then)) {
return "";
} else if (($then + 72000) < $now ) {
// if it is, delete the checkfile
unlink($checkfile);
} else {
// if it's not, come back later...
return "";
}
}
pin_save_file($checkfile, time());
if (@is_writable($todo)) {
$now = date("YmdHi");
$lines = file($todo);
$nyet = "";
$pages = 0;
$maxpages = ($c["publish_maxfiles"] > 0)?($c["publish_maxfiles"]):(10);
$timeout = ($c["publish_timeout"] > 0)?($c["publish_timeout"]):(10);
$size = 0;
$export = "";
$new_lines = "";
$old_lines = "";
$filenames = array();
$contents = array();
if ($param["shell"] == true) {
$maxpages = ($c["publish_maxfiles"] > 0)?($c["publish_maxfiles"]):(2000);
$timeout = ($c["publish_timeout"] > 0)?($c["publish_timeout"]):(1000);
}
$param["publishing"] = 1;
$scriptname = $param["script-name"];
$timestart = time();
foreach ($lines as $line) {
if (preg_match("/^([0-9]{12})\s+([0-9]{1})\s+([a-z0-9\.\:\/\-\_]+)\s+([a-z0-9\.\/\-\_]+)\s(.+)/i", trim($line), $matches)){
//matches = array([1] => date, [2] => const, [3] => id, [4] => url, [5] => user);
if ((time() - $timestart <= $timeout) && $now >= $matches[1] && $pages < $maxpages) {
if (in_array($matches[4], $filenames)) {
// already published url, do nothing
} else if ($matches[2] == 1) {
$filenames[] = $matches[4];
$param["script-name"] = $matches[4];
if (is_array($c["remote_host"]) && count($c["remote_host"]) > 0) {
$oldhost = $param["host"];
$tmp = "";
foreach ($c["remote_host"] as $host=>$info) {
$param["host"] = $host;
if (pin_check_host($matches[4],$info)) {
$tmp[$host] = @pin_output_xml($matches[3],FALSE, FALSE, FALSE);
}
}
$param["host"] = $oldhost;
} else {
$tmp = @pin_output_xml($matches[3],FALSE, FALSE, FALSE);
}
$contents[] = $tmp;
$size = $size + strlen($tmp);
$users[] = $matches[5];
$pages++;
$old_lines .= $now."\t".$matches[2]."\t".$matches[3]."\t".$matches[4]."\t".$matches[5]."\n";
} else if ($matches[2] == 0) {
$filenames[] = $matches[4];
$tmp = @file_get_contents($c["www"].$matches[4]);
if (is_array($c["remote_host"]) && count($c["remote_host"]) > 0) {
$xtmp = $tmp;
$tmp = "";
foreach ($c["remote_host"] as $host=>$info) {
if (pin_check_host($matches[4],$info)) {
$tmp[$host] = $xtmp;
}
}
}
$contents[] = $tmp;
$size = $size + strlen($tmp);
$users[] = $matches[5];
$old_lines .= $now."\t".$matches[2]."\t".$matches[3]."\t".$matches[4]."\t".$matches[5]."\n";
$pages++;
} else {
$new_lines .= $matches[1]."\t".$matches[2]."\t".$matches[3]."\t".$matches[4]."\t".$matches[5]."\n";
}
} else {
$new_lines .= $matches[1]."\t".$matches[2]."\t".$matches[3]."\t".$matches[4]."\t".$matches[5]."\n";
}
}
}
unset($param["publishing"]);
$param["script-name"] = $scriptname;
if (count($filenames) > 0) {
if ($c["publish_mode"] == "local_copy") {
if (!pin_local_copy($filenames, $contents)) {
unlink($checkfile);
return false;
}
} else if (!pin_remote_copy($filenames, $contents)) {
unlink($checkfile);
return false;
}
$i = 0;
$log = "";
if(function_exists("send_message")) {
if (count($filenames) == 1) {
pin_send_message($l["publish"]["doc"].$filenames[0].$l["publish"]["doc_published"]);
} else {
pin_send_message($l["publish"]["docs"].implode(", ", $filenames).$l["publish"]["docs_published"]);
}
}
/*foreach ($filenames as $filename) {
$log .= $param["date"]." login:".$users[$i++]." published: ".$filename."\n";
}*/
$today = date("Ymd");
$fp = @fopen($c["index"]."done.$today", "a");
@fputs($fp, $old_lines);
@fclose($fp);
$fp = @fopen($c["index"]."todo", "w");
@fwrite($fp, $new_lines);
@fclose($fp);
}
}
unlink($checkfile);
}
/**
*
* @name pin_check_host
* @deprecated
* @version 0.11
*
* @author Guilherme Capilé <hide@address.com>
*
* @param $url
* @param $info
*
* @return
*/
function pin_check_host($url, $info)
{
//checks if file can be published
$tobepublished = true;
if (is_array($info["restrict"])){
$tobepublished = false;
foreach ($info["restrict"] as $restrict) {
if(substr($url, 0, strlen($restrict)) == $restrict) {
$tobepublished = true;
break;
}
}
}
if (is_array($info["exclude"])){
foreach ($info["exclude"] as $exclude) {
if(substr($url, 0, strlen($exclude)) == $exclude) {
$tobepublished = false;
break;
}
}
}
return $tobepublished;
}
?>