<?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_start();$slogin_noauthpage=0;$slogin_pagetitle="";include_once ("paraUsu.php");include_once (profUname."../".uName."/".uName."_lib.php");include_once ("cfg.php");include_once ("ardoraFileManagerFunctions.php");session_cache_limiter("nocache,private");
if($_POST["action"]=="getIdUser"){$allUser=array();for($i=0; $i<200; $i++){if (!empty($slogin_user[$i]) && $slogin_profe[$i]!="admin" && $slogin_profe[$i]!="profe" && $slogin_profe[$i]===dameValor($_SESSION["Username"],"NOM")){$allUser[$slogin_user[$i]]=$slogin_name[$i];}}
if (dameValor($_SESSION["Username"],"TIP")=="profe" || dameValor($_SESSION["Username"],"TIP")=="admin"){if (!file_exists("../".profeFold.$_SESSION["Username"])){mkdir("../".profeFold.$_SESSION["Username"], 0777);
$file=fopen("../".profeFold.$_SESSION["Username"]."/index.php","a") or die();fputs($file,"<?php");fputs($file,"// Created by Ardora www.webardora.net");fputs($file,"//XARQUIVOS-dummy php");fputs($file,"?>");fclose($file);}
} else{ if (!file_exists("../".aluFold.$_SESSION["Username"])){mkdir("../".aluFold.$_SESSION["Username"], 0777);$file=fopen("../".aluFold.$_SESSION["Username"]."/index.php","a") or die();
fputs($file,"<?php");fputs($file,"// Created by Ardora www.webardora.net");fputs($file,"//XARQUIVOS-dummy php");fputs($file,"?>");fclose($file);}}
echo json_encode(array("allUser"=>$allUser,"userN"=>$_SESSION["Username"],"userType"=>dameValor($_SESSION["Username"],"TIP"),"userName"=>dameValor($_SESSION["Username"],"NOM"),"userCur"=>dameValor($_SESSION["Username"],"CUR"),"userGru"=>dameValor($_SESSION["Username"],"GRU")));}
if($_POST["action"]=="getStateFolder"){$state="";if ($_POST["fType"]==0){$useSize=sizeDir(".."."/".sharedFold);$sMax=sizeMax;$state=format(sizeDir(".."."/".sharedFold))." / ".format(sizeMax);}
if ($_POST["fType"]==1){$tipo=dameValor($_SESSION["Username"],"TIP");if (($tipo=="profe") || ($tipo=="admin")){$useSize=sizeDir(".."."/".profeFold);$sMax=sizeMaxProf;$state=format(sizeDir(".."."/".profeFold))." / ".format(sizeMaxProf);}
else{$useSize=sizeDir(".."."/".aluFold);$sMax=sizeMaxUser;$state=format(sizeDir(".."."/".aluFold))." / ".format(sizeMaxUser);}}echo json_encode(array("state"=>$state,"useSize"=>$useSize,"sMax"=>$sMax));}
if($_POST["action"]=="makeDir"){$newDir=$_POST["fName"];if ($_POST["fType"]==0){$newDir=".."."/".sharedFold.$newDir;}if ($_POST["fType"]==1){$tipo=dameValor($_SESSION["Username"],"TIP");
if (($tipo=="profe") || ($tipo=="admin")){$newDir=".."."/".profeFold.$_SESSION["Username"]."/".$newDir;} else{$newDir=".."."/".aluFold.$_SESSION["Username"]."/".$newDir;}} if ($_POST["fType"]==2){$newDir=".."."/".aluFold.$newDir;}if(!is_dir($newDir)){ mkdir($newDir, 0777);}}
if($_POST["action"]=="deleteDir"){$newDir=$_POST["fName"];if ($_POST["fType"]==0){$newDir=".."."/".sharedFold.$newDir;}if ($_POST["fType"]==1){$tipo=dameValor($_SESSION["Username"],"TIP");
if (($tipo=="profe") || ($tipo=="admin")){$newDir=".."."/".profeFold.$_SESSION["Username"]."/".$newDir;} else{ $newDir=".."."/".aluFold.$_SESSION["Username"]."/".$newDir;}}if ($_POST["fType"]==2){$newDir=".."."/".aluFold.$newDir;}
if(is_dir($newDir)){deleteDir($newDir);$xml = new DOMDocument("1.0", "utf-8");$xml->load("../".xmlFile);$files = $xml->getElementsByTagName("file");
foreach($files as $file) {$f=$file->getElementsByTagName("path")->item(0)->nodeValue;if (!file_exists($f)){$t=$file->getElementsByTagName("tab")->item(0)->nodeValue="5";}}$xml->save("../".xmlFile);
$xmlF = simplexml_load_file("../".xmlFile);$cadena='//tab[.="5"]/parent::*';$result = $xmlF->xpath($cadena);foreach ($result as $r) unset ($r[0]);$xmlF->asXML("../".xmlFile);}}
if($_POST["action"]=="addXml"){$newDir=normaliza($_POST["fName"].$_POST["fileName"]);if ($_POST["fType"]==0){$newDir=".."."/".sharedFold.$newDir;}
if ($_POST["fType"]==1){$tipo=dameValor($_SESSION["Username"],"TIP");if (($tipo=="profe") || ($tipo=="admin")){$newDir=".."."/".profeFold.$_SESSION["Username"]."/".$newDir;}else{$newDir=".."."/".aluFold.$_SESSION["Username"]."/".$newDir;}}
if ($_POST["fType"]==2){$newDir=".."."/".aluFold.$newDir;}$idNum=0;$theXmlFile=".."."/".xmlFile;$new=true;
if (file_exists($theXmlFile)){$xml = new DOMDocument("1.0", "utf-8");$xml->formatOutput = true;$xml->preserveWhiteSpace = false;$xml->load($theXmlFile);$query="//files/file[path='".$newDir."']";$xpath = new DomXpath($xml);
$tiFiles=$xpath->query($query);foreach($tiFiles as $item){$new=false;$newItem = $xml->createElement("file");$newItem->appendChild($xml->createElement("tab",$_POST["fType"]));$newItem->appendChild($xml->createElement("path",$newDir));$newItem->appendChild($xml->createElement("usr",$_SESSION["Username"]));$newItem->appendChild($xml->createElement("datetime",date("d-m-Y H:i:s")));
$com = $newItem->appendChild($xml->createElement("coment"));$com->appendChild($xml->createCDATASection($_POST["coment"]));$item->parentNode->replaceChild($newItem,$item);$xml->save($theXmlFile);}}
else{$xml=new XMLWriter();
touch($theXmlFile);$theXmlFile = realpath($theXmlFile);
$xml->openURI($theXmlFile);$xml->startDocument();$xml->startElement("files");$xml->endElement();$xml->endDocument();$xml->flush();}
if ($new){$xml = new DOMDocument("1.0", "utf-8");$xml->formatOutput = true;$xml->preserveWhiteSpace = false;$xml->load($theXmlFile);$newItem = $xml->createElement("file");
$newItem->appendChild($xml->createElement("tab",$_POST["fType"]));$newItem->appendChild($xml->createElement("path",$newDir));$newItem->appendChild($xml->createElement("usr",$_SESSION["Username"]));$newItem->appendChild($xml->createElement("datetime",date("d-m-Y H:i:s")));$com = $newItem->appendChild($xml->createElement("coment"));
$com->appendChild($xml->createCDATASection($_POST["coment"]));$xml->getElementsByTagName("files")->item(0)->insertBefore($newItem, $element);$xml->save($theXmlFile);}}
if($_GET["action"]=="download"){$ar=$_GET["Arquivo"];$ar="../".$ar;$separar=explode(".",basename($ar));$extension=strtolower($separar[1]);
$extensiones_permitidas=array("jpg","gif","png","bmp","svg","mp4","ogv","swf","txt","pdf","mp3","ogg","odt","ods","odp","sxw","sxc","sxi","zip","rar","tar","7z","gzip","bz2");if (in_array($extension,$extensiones_permitidas)) {
if(file_exists($ar)){$filepath = ($ar);header("HTTP/1.1 200 OK");header("Status: 200 OK");header("Content-Type: text/php");header('Content-Disposition: attachment; filename="'.basename($filepath).'"');header("Content-Length: ".filesize($filepath));readfile($filepath);}else{echo"El archivo: ".$ar." No existe";}
} else {echo "El archivo: ".$ar." No Puede ser descargado";}}
if($_POST["action"]=="deleteFile"){$file=$_POST["fName"];$file=str_replace("~", " ",$file);$file=str_replace("%20"," ",$file);$com=$file;if(is_writable("../".$file)){$com="escribible";
if(unlink("../".$file)){$xmlF = simplexml_load_file("../".xmlFile);$cadena='//path[.="../'.$file.'"]/parent::*';$result = $xmlF->xpath($cadena);foreach ($result as $r) unset ($r[0]);$xmlF->asXML("../".xmlFile);$com="borrado";}}}
if($_POST["action"]=="getInfoFile"){$file=$_POST["fName"];$file=str_replace("~", " ",$file);$file=str_replace("%20"," ",$file);$xml = new DOMDocument("1.0", "utf-8");$xml->load("../".xmlFile);
$query='//files/file[path="../'.$file.'"]';$xpath = new DOMXPath($xml);$result=$xpath->query($query);
if ($result->length>0){foreach ($result as $node) {$coment=$node->getElementsByTagName("coment")->item(0)->nodeValue;$usr=$node->getElementsByTagName("usr")->item(0)->nodeValue;$datetime=$node->getElementsByTagName("datetime")->item(0)->nodeValue;
$tab=$node->getElementsByTagName("tab")->item(0)->nodeValue;} echo json_encode(array("coment"=>$coment,"usr"=>$usr,"datetime"=>$datetime,"tab"=>$tab));}}
if($_POST["action"]=="getTxt"){$texto="";$handle=fopen("../".str_replace("%20"," ",$_POST["fName"]), "r");while (!feof($handle)) {$buffer = fgets($handle,4096);$texto=$texto.$buffer."<br/>";}
fclose($handle);$txt='<div align="justify" style="margin:5px;">'.utf8_encode($texto).'</div>';echo json_encode(array("txt"=>$txt));}
?>
