<?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 ("cfg.php");include_once ("paraUsu.php");include_once ("../".profUname.uName."/".uName."_lib.php");$folderN="../".xmlDir;$folUser=$_SESSION["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=$_SESSION["Username"];}else {$xmlName=$_POST["nameUser"];}
//CONTAMOS O TOTAL DE ITEMS DE TODOS OS ALUMNOS
$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")){
//CREAMOS ARQUIVO NOVO
$initial=$xml->createElement($xmlName);$initial=$xml->appendChild($initial);$form=$xml->createElement("item");$form=$initial->appendChild($form);$userNumUpload=1;
} else {
//CONTAMOS OS ITEMS QUE XA TEN INTRODUCIDOS O USUARIO
$xml->load($folderN.$xmlName.".xml", LIBXML_NOBLANKS);$userNumUpload=$xml->getElementsByTagName("item")->length;$userNumUpload++;
if ($userNumUpload<=userMaxUpload){
//ENGADIMOS UN NOVO ITEM
$root = $xml->documentElement;$form=$xml->createElement("item");$form=$root->appendChild($form);}}
if ($userNumUpload<=userMaxUpload){$dat=$xml->createElement("dat",date("d-m-Y H:i:s"));$dat=$form->appendChild($dat);$tit=$xml->createElement("tit",$_POST["tit"]);$tit=$form->appendChild($tit);$des=$xml->createElement("des",$_POST["des"]);$des=$form->appendChild($des);
$ima=$xml->createElement("ima");$cdima=$xml->createCDATASection($_POST["ima"]);$ima->appendChild($cdima);$form->appendChild($ima);
$htm=$xml->createElement("htm");$cdhtm=$xml->createCDATASection($_POST["htm"]);$htm->appendChild($cdhtm);$form->appendChild($htm);
$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);}
//función que devolve OK se non se sobrepasou o total de items da clase e o individual
if($_POST["action"]=="canSave"){$userNumUpload=0;$itemsUpload=0;$canSave=true;
//CONTAMOS O TOTAL DE ITEMS DE TODOS OS ALUMNOS
$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{
//CONTAMOS OS ITEMS INDIVIDUAIS
if ($_POST["type"]=="0"){$xmlName=$_SESSION["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.$_SESSION["Username"].".xml")){$xml = new DOMDocument("1.0", "utf-8");$xml->formatOutput = true;$xml->preserveWhiteSpace = false;$xml->load($folderN.$_SESSION["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($_SESSION["Username"],"TIP")));
}else{echo json_encode(array("tiUser"=>"0","userType"=>dameValor($_SESSION["Username"],"TIP")));}}
if($_POST["action"]=="delete"){if(unlink($folderN.$_SESSION["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($_SESSION["Username"],"TIP")));
}else{echo json_encode(array("tiUser"=>"0","userType"=>dameValor($_SESSION["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"]=="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;
$ima=$item->getElementsByTagName("ima")->item(0)->nodeValue;
$htm=$item->getElementsByTagName("htm")->item(0)->nodeValue;
$jobs[]=array("dat"=>$dat,"tit"=>$tit,"des"=>$des,"ima"=>$ima,"htm"=>$htm,"usu"=>basename($filename,".xml"));
}}}; echo json_encode($jobs);}
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"] == "updateItem") {$xml=new DOMDocument("1.0","utf-8");$xml->formatOutput=true;$xml->preserveWhiteSpace=false;$xml->load($folderN.$_POST["usu"].".xml");$oldItem = trim($_POST["oldItem"]);
$nodes=$xml->getElementsByTagName("item");foreach($nodes as $node){$n=$node->getElementsByTagName("tit")->item(0)->nodeValue;$arr=array("status1" => $n,"status2" => trim($_POST["tit"]));
if ($n==trim($_POST["oldItem"])){$newItem = $xml->createElement("item");$newItem->appendChild($xml->createElement("dat",date("d-m-Y H:i:s")));$newItem->appendChild($xml->createElement("tit",$_POST["tit"]));$newItem->appendChild($xml->createElement("des",$_POST["des"]));
$ima=$newItem->appendChild($xml->createElement("ima"));if ($_POST["ima"]=="0"){$ima->appendChild($xml->createCDATASection($node->getElementsByTagName("ima")->item(0)->nodeValue));}else{$ima->appendChild($xml->createCDATASection($_POST["ima"]));}
$htm = $newItem->appendChild($xml->createElement("htm"));$htm->appendChild($xml->createCDATASection($_POST["htm"]));$node->parentNode->replaceChild($newItem,$node);$xml->save($folderN.$_POST["usu"].".xml");
}}echo json_encode($arr);}
?>
