<?php

/*
    This file is part of osmdatamanager.

    osmdatamanager is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, only GPLv2.

    osmdatamanager is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with osmdatamanager.  If not, see <http://www.gnu.org/licenses/>.
*/
include_once "application.php";
include_once "directoryfactory.php";
include_once "JSON.php";
if (application_userisvalid()) {
    $usr = application_getvaliduser();
    if ($usr != null) {
        $df = new DirectoryFactory();
        $df->uploadFiles($usr);
    }
}
if ($action == msg_chklogin) {
    $usr = application_getvaliduser();
    if ($usr != null) {
        echo application_getMessage($usr);
    } else {
        echo application_getMessage(msg_loginfailed);
    }
}
//msg_registeruser
if ($action == msg_registeruser) {
    $userfactory = new Userfactory();
    if (!$userfactory->userNameExists(trim($username))) {
        if ($userfactory->registerUser($username, $password, $email)) {
            $usr = application_loginuser($username, $password);
            if ($usr != null) {
                $df = new DirectoryFactory();
                $df->login();
                $df->createDirectory("trf_" . $usr->getUid());
                $df->logout();
                $userfactory->createExampleGroup($usr);
                $userfactory->createExamplePoi($usr);
                echo application_getMessage($usr);
                return;
            }
        } else {
            echo application_getMessage(msg_registerfailed);
            return;
        }
    } else {
        echo application_getMessage(msg_userexists);
    }
    You should have received a copy of the GNU General Public License
    along with osmdatamanager.  If not, see <http://www.gnu.org/licenses/>.
*/
include_once "application.php";
include_once "JSON.php";
include_once "directoryfactory.php";
$action = $_REQUEST['action'];
if (isset($action)) {
    //add other variables here
} else {
    //uncomment for debug
    /* 
    $action  		= $_GET['action'];
    */
}
if (application_userisvalid()) {
    $usr = application_getvaliduser();
    if ($usr != null) {
        //msg_getftpfiles
        if ($action == msg_getftpfiles) {
            $df = new DirectoryFactory();
            $geslist = $df->listFiles_Ftp($usr);
            if ($geslist != null) {
                echo application_getMessage($geslist);
            } else {
                echo application_getMessage(msg_failed);
            }
        }
    }
}
    global $gl_loglevel;
} else {
    //uncomment for debug
    /*
    $action  		= $_GET['action'];
    $filename		= $_GET['filename'];
    $description    = $_GET['description'];	
    
    global $gl_loglevel;
    $gl_loglevel 	= 1;
    */
}
if (application_userisvalid()) {
    $usr = application_getvaliduser();
    if ($usr != null) {
        $df = new DirectoryFactory();
        $ff = new FileFactory();
        global $gl_filedir;
        //msg_updatefilelist
        //TODO delete file from db if the file does not exist in directory
        if ($action == msg_updatefilelist) {
            //$ff->updateFiles($usr->getUid());
            $ff->setInvalid($usr->getUid());
            $lst1 = $df->listFiles_Dir($usr->getUid(), $gl_filedir, array("gpx", "xml"));
            if ($lst1 != null) {
                for ($i = 0; $i < count($lst1); $i++) {
                    $fn = $lst1[$i];
                    if (!$ff->fileExists($usr->getUid(), basename($fn))) {
                        $path = dirname($fn) . "/";
                        $filename = basename($fn);
                        $ff->createFile($usr->getUid(), $path, $filename, NULL);
 /** //TODO checken
  * deletes a user an all his data
  * @return 
  * @param $username Object
  */
 function deleteUser($username)
 {
     $usr = $this->getUser($username);
     if ($usr != null) {
         $usrid = $usr->getUid();
         if ($usrid != null && $usrid != "") {
             $delquery = "DELETE FROM `tab_file` WHERE (usrid = {$usrid})";
             $this->executeQuery($delquery);
             $delquery = "DELETE FROM `tab_grp` WHERE (usrid = {$usrid})";
             $this->executeQuery($delquery);
             $delquery = "DELETE FROM `tab_grp_file` WHERE (usrid = {$usrid})";
             $this->executeQuery($delquery);
             $delquery = "DELETE FROM `tab_grp_poi` WHERE (usrid = {$usrid})";
             $this->executeQuery($delquery);
             $delquery = "DELETE FROM `tab_poi` WHERE (usrid = {$usrid})";
             $this->executeQuery($delquery);
             $delquery = "DELETE FROM `tab_usr` WHERE (itemid = {$usrid})";
             $this->executeQuery($delquery);
             $this->addLogMessage("User {$usrid}-{$username} deleted", "INFO");
             $df = new DirectoryFactory();
             $df->login();
             $df->deleteUserDir($usrid);
             $df->logout();
         }
     }
 }