<?php
//Creado con Ardora - www.webardora.net
//bajo licencia Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)
//para otros usos contacte con el autor
session_cache_limiter("nocache,private");session_start();$slogin_noauthpage = 0;$slogin_pagetitle = "";include_once ("cfg2.php");include_once("cfg.php");include_once("para.php");include_once ("../".profUname.uName."/".uName."_lib.php");$folderN="../".xmlDir;$folUser=$slogin_Username;date_default_timezone_set("Europe/Madrid");
if($_POST["action"]=="logout"){$_SESSION["Username"]=NULL;session_destroy();echo json_encode(array("status" => "logout"));}
if($_GET["action"]=="login"){echo json_encode(array("usu"=>$slogin_Username,"nom"=>dameValor($slogin_Username, "NOM"),"cur"=>dameValor($slogin_Username, "CUR"),"gru"=>dameValor($slogin_Username, "GRU"),"typ"=>dameValor($slogin_Username, "TIP")));}
if($_POST["action"]=="new"){$userNumUpload=0;$itemsUpload=0;$canSave=true;if ($_POST["type"]=="0"){$xmlName=$slogin_Username;}else {$xmlName=$_POST["nameUser"];}
$files = glob($folderN."*xml");if (is_array($files)) {foreach($files as $filename) {$xml_file=new DomDocument("1.0","UTF-8");$xml_file->load($filename, LIBXML_NOBLANKS);$tot=$xml_file->getElementsByTagName("item")->length;$itemsUpload=$itemsUpload+$tot;}if ($itemsUpload>=itemMaxUpload){$canSave=false;}}
if ($canSave){$xml=new DomDocument("1.0","UTF-8");$xml->formatOut=true;if(!file_exists($folderN.$xmlName.".xml")){$initial=$xml->createElement($xmlName);$initial=$xml->appendChild($initial);$form=$xml->createElement("item");$form=$initial->appendChild($form);$userNumUpload=1;} else {
$xml->load($folderN.$xmlName.".xml", LIBXML_NOBLANKS);$userNumUpload=$xml->getElementsByTagName("item")->length;$userNumUpload++;if ($userNumUpload<=userMaxUpload){$root = $xml->documentElement;$form=$xml->createElement("item");$form=$root->appendChild($form);}}
if ($userNumUpload<=userMaxUpload){$dat=$xml->createElement("dat",date("d-m-Y"));$dat=$form->appendChild($dat);$tit=$xml->createElement("tit",$_POST["tit"]);$tit=$form->appendChild($tit);$des=$xml->createElement("des",$_POST["des"]);$des=$form->appendChild($des);
$cbr=$xml->createElement("cBo",$_POST["cBorder"]);$cbr=$form->appendChild($cbr);$cba=$xml->createElement("cBa",$_POST["cBack"]);$cba=$form->appendChild($cba);$strings_xml=$xml->saveXML();
if($xml->save($folderN.$xmlName.".xml")){$arr=array("status" => "Ok");}else{$arr=array("status" => "Error");}} else{$arr=array("status" => "UpMaxUpload");}} else{$arr=array("status" => "ItemMaxUpload");}echo json_encode($arr);}
if ($_POST["action"] == "updateItem") {$xml = new DOMDocument("1.0","utf-8");$xml->formatOutput = true;$xml->preserveWhiteSpace = false;$xml->load($folderN.$_POST["nameUser"].".xml");$oldItem = trim($_POST["oldItem"]);$titulo = trim($_POST["item"]);$descricion = trim($_POST["com"]);$corfondo = trim($_POST["cBack"]);$corborde = trim($_POST["cBorder"]);$fecha = trim(strftime("%d/%m/%Y", time()));$nodes=$xml->getElementsByTagName("item");
for ($i=0;$i<$nodes->length; $i++) {$back=$xml->getElementsByTagName("item")->item($i);$item=$back->getElementsByTagName("tit")->item(0);$com=$back->getElementsByTagName("des")->item(0);$colorBack=$back->getElementsByTagName("cBa")->item(0);$colorBorder=$back->getElementsByTagName("cBo")->item(0);$date=$back->getElementsByTagName("dat")->item(0);$actualItem=(string)$item->nodeValue;$arr=array("act" =>$oldItem);
if ($actualItem===$oldItem) {$item->nodeValue=$titulo;$com->nodeValue=$descricion;$colorBack->nodeValue=$corfondo;$colorBorder->nodeValue=$corborde;$date->nodeValue=$fecha;$xml->save($folderN.$_POST["nameUser"].".xml");}}echo json_encode($arr);}
if($_POST["action"]=="canSave"){$userNumUpload=0;$itemsUpload=0;$canSave=true;$files = glob($folderN."*xml");
if (is_array($files)) {foreach($files as $filename) {$xml_file=new DomDocument("1.0","UTF-8");$xml_file->load($filename, LIBXML_NOBLANKS);$tot=$xml_file->getElementsByTagName("item")->length;$itemsUpload=$itemsUpload+$tot;}
if ($itemsUpload>=itemMaxUpload){$canSave=false;}else{if ($_POST["type"]=="0"){$xmlName=$slogin_Username;}else {$xmlName=$_POST["nameUser"];}
$xml=new DomDocument("1.0","UTF-8");$xml->formatOut=true;if(file_exists($folderN.$xmlName.".xml")){$xml->load($folderN.$xmlName.".xml", LIBXML_NOBLANKS);$userNumUpload=$xml->getElementsByTagName("item")->length;if ($userNumUpload>=userMaxUpload){$canSave=false;}}}}
if ($canSave){$arr=array("status" => "Ok");}else{$arr=array("status" => "No");}echo json_encode($arr);}
if($_POST["action"]=="getUser"){if (file_exists($folderN.$slogin_Username.".xml")){$xml = new DOMDocument("1.0", "utf-8");$xml->formatOutput = true;$xml->preserveWhiteSpace = false;$xml->load($folderN.$slogin_Username.".xml");$form=$xml->getElementsByTagName("form")->item(0);
$paragrafo2=$form->getElementsByTagName("paragrafo2")->item(0);
echo json_encode(array("tiUser"=>"1","paragrafo2"=>stripslashes($paragrafo2->nodeValue),"userType"=>dameValor($slogin_Username,"TIP")));
}else{echo json_encode(array("tiUser"=>"0","userType"=>dameValor($slogin_Username,"TIP")));}}
if($_POST["action"]=="delete"){if(unlink($folderN.$slogin_Username.".xml")){$arr=array("status" => "Ok");} else{ $arr=array("status" => "Error"); }; echo json_encode($arr);}
if($_POST["action"]=="getUserData"){if (file_exists($folderN.$_POST["nameUser"].".xml")){$xml = new DOMDocument("1.0", "utf-8");$xml->formatOutput = true;$xml->preserveWhiteSpace = false;$xml->load($folderN.$_POST["nameUser"].".xml");$form=$xml->getElementsByTagName("form")->item(0);
$paragrafo2=$form->getElementsByTagName("paragrafo2")->item(0);
echo json_encode(array("tiUser"=>"1","paragrafo2"=>stripslashes($paragrafo2->nodeValue),"userType"=>dameValor($slogin_Username,"TIP")));
}else{echo json_encode(array("tiUser"=>"0","userType"=>dameValor($slogin_Username,"TIP")));}}
if($_POST["action"]=="deleteJob"){if(unlink($folderN.$_POST["nameUser"].".xml")){$arr=array("status" => "Ok");} else{$arr=array("status" => "Error");};echo json_encode($arr);}
if ($_POST["action"] == "deleteItem") {$xml = new DOMDocument("1.0", "utf-8");$xml->formatOutput = true;$xml->preserveWhiteSpace = false;$xml->load($folderN.$_POST["nameUser"].".xml");$titulo = trim($_POST["tit"]);$autor = trim($_POST["nameUser"]);$nodes=$xml->getElementsByTagName("item");
for ($i=0;$i<$nodes->length;$i++) {$back=$xml->getElementsByTagName("item")->item($i);$item=$back->getElementsByTagName("tit")->item(0);$actualItem=(string)$item->nodeValue;if ($actualItem===$titulo) {$back->parentNode->removeChild($back);$xml->save($folderN.$_POST["nameUser"].".xml");}}echo json_encode(array("status"=>$nodes->length));}
if ($_POST["action"]=="indexUsers") {$orderName=scandir("../".folderWork."/");for ($i = 0; $i < count($orderName); $i++) {if (strcmp ($orderName[$i],".")==0 || strcmp ($orderName[$i],"..")==0){$orderData[$i]="";} else {$orderData[$i]=dameValor(basename($orderName[$i],".xml"),"NOM");}};
for ($i = 0; $i < count($orderName); $i++) {if (strcmp ($orderName[$i],".")!=0 && strcmp ($orderName[$i],"..")!=0){$resultArray[$i]=array($orderName[$i],$orderData[$i]);}} echo json_encode($resultArray);}
if($_POST["action"]=="getJob"){$jobs=array();$files = glob($folderN."*xml");if (is_array($files)) {foreach($files as $filename) {$xml_file=new DomDocument("1.0","UTF-8");$xml_file->load($filename, LIBXML_NOBLANKS);$items=$xml_file->getElementsByTagName("item");
foreach($items as $item){$dat=$item->getElementsByTagName("dat")->item(0)->nodeValue;$tit=$item->getElementsByTagName("tit")->item(0)->nodeValue;$des=$item->getElementsByTagName("des")->item(0)->nodeValue;$cBo=$item->getElementsByTagName("cBo")->item(0)->nodeValue;$cBa=$item->getElementsByTagName("cBa")->item(0)->nodeValue;$jobs[]=array("dat"=>$dat,"tit"=>$tit,"des"=>$des,"cBo"=>$cBo,"cBa"=>$cBa,"usu"=>basename($filename,".xml"));}}};
echo json_encode($jobs);}
?>
