<?php
    $path = "../libraries/"; 
	require_once $path."configuration.php";
    $data       = eF_getTableData("configuration", "value", "name='api'");                                        //Read current values
    $api = $data[0]['value'];
    if ($api == 1){
        if (isset($_GET['action'])){
            $action = $_GET['action'];
            switch($_GET['action']){
                case 'token':
                    $token = createToken(30);
                    if (strlen($token) ==30){
                        $insert['token'] = $token;
                        $insert['status'] = "unlogged";
                        $insert['expired'] = 0;
                        $insert['create_timestamp'] = time();
                        ef_insertTableData("tokens", $insert);
                        echo "<token>".$token."</token>";    
                    }
                    break;
                 case 'efrontlogin':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        $token = $_GET['token'];
                        $creds = ef_getTableData("tokens t, users u", "u.login, u.password, u.user_type", "t.users_LOGIN = u.LOGIN and t.token='$token'");
                        if (sizeof($creds) == 0){
                            echo "<status>error</status>";                                
                            echo "<message>Invalid username</message>";
                            break;
                        }
    
                        if (isset($_SESSION['s_login'])) {
							$user = EfrontUserFactory :: factory($_SESSION['s_login']);
							$user -> logout();
                        }                
                        $login = $creds[0]['login'];
                        $password  = $creds[0]['password'];
                        
                        $user = EfrontUserFactory :: factory($login);
                        $ok = $user -> login($password, true);
                        if ($ok){
                            echo "<status>ok</status>";     
                        }     
                        else{
                            echo "<status>error</status>";                                   
                            echo "<message>Unable to update log</message>";
                        }                                                         
                        break;
                    }   
                    else{
                        echo "<status>error</status>";                                
                        echo "<message>Invalid token</message>";
                        break;
                    }
                 }
                 case 'login':{
                    if (isset($_GET['username']) && isset($_GET['password']) && isset($_GET['token']) ){
                        $login = $_GET['username'];
                        $password = md5($_GET['password'].(G_MD5KEY));
                        $token = $_GET['token'];
                        $tmp = ef_getTableData("tokens","token","status='unlogged'");
                        if (sizeof($tmp) > 0){
                            if (eF_checkParameter($login, 'login')) {    
                                $tmp = ef_getTableData("users", "password","login='$login'");
                                $pwd = $tmp[0]['password'];                            
                                if ($pwd == $password){
                                    $update['status'] = "logged";
                                    $update['users_LOGIN'] = $login;
                                    ef_updateTableData("tokens",$update,"token='$token'");
                                    echo "<status>ok</status>";                         
                                }
                                else{
                                    echo "<status>error</status>";                                   
                                    echo "<message>Invalid password</message>";
                                }
                            }
                            else{
                                echo "<status>error</status>";                                
                                echo "<message>Invalid username</message>";
                            }
                        }
                        else{
                            echo "<status>error</status>";                            
                            echo "<message>Invalid token</message>";
                        }
                    }  
                    else{
                        echo "<status>error</status>";                        
                        echo "<message>No username/password/token provided</message>";
                    }
                    break;           
                }
                case 'create_user':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        if (isset($_GET['login']) && isset($_GET['password']) && isset($_GET['email']) && isset($_GET['languages']) && isset($_GET['name']) && isset($_GET['surname'])){
                            $insert['login'] = $_GET['login'];
                            $insert['password'] = md5($_GET['password'].(G_MD5KEY));
                            $insert['email'] = $_GET['email'];
                            $insert['languages_NAME'] = $_GET['languages'];
                            $insert['name'] = $_GET['name'];
                            $insert['surname'] = $_GET['surname'];
                            if (ef_InsertTableData("users", $insert))
                            {
                                echo "<status>ok</status>";                            
                            }
                            else
                            {
                                echo "<status>error</status>";                               
                                echo "<message>User exists</message>";
                            }
                        }
                        else
                        {
                            echo "<status>error</status>";                           
                            echo "<message>Incomplete arguments</message>";                            
                        }
                            
                    }
                    else{
                        echo "<status>error</status>";                       
                        echo "<message>Invalid token</message>";
                    }                    
                    
                    break;    
                }
                case 'update_user':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        if (isset($_GET['login']) && isset($_GET['password']) && isset($_GET['email']) && isset($_GET['name']) && isset($_GET['surname'])){
                            $fields['password'] = md5($_GET['password'].(G_MD5KEY));
                            $fields['email'] = $_GET['email'];
                            $fields['name'] = $_GET['name'];
                            $fields['surname'] = $_GET['surname'];
                            if (ef_UpdateTableData("users", $fields, "login='".$_GET['login']."'"))
                            {
                                echo "<status>ok</status>";                            
                            }
                            else
                            {
                                echo "<status>error</status>";                               
                                echo "<message>User exists</message>";
                            }
                        }
                        else
                        {
                            echo "<status>error</status>";                           
                            echo "<message>Incomplete arguments</message>";                            
                        }
                            
                    }
                    else{
                        echo "<status>error</status>";                       
                        echo "<message>Invalid token</message>";
                    }                    
                    
                    break;
                }
                case 'deactivate_user':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        if (isset($_GET['login'])){
                            $update['active'] = '0';
                            if (ef_updateTableData("users",$update, "login='$login'")){
                                echo "<status>ok</status>";
                            }
                            else{
                                echo "<status>error</status>";                                
                                echo "<message>User doesn't exist</message>";
                            }
                        }
                        else{
                            echo "<status>error</status>";                            
                            echo "<message>Incomplete arguments</message>"; 
                        }
                        
                    }
                    else{
                        echo "<status>error</status>";                       
                        echo "<message>Invalid token</message>";
                    }        
                    break;            
                }
                case 'activate_user':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        if (isset($_GET['login'])){
                            $update['active'] = '1';
                            if (ef_updateTableData("users",$update, "login='$login'")){
                                echo "<status>ok</status>";
                            }
                            else{
                                echo "<status>error</status>";                                
                                echo "<message>User doesn't exist</message>";
                            }
                        }
                        else{
                            echo "<status>error</status>";                            
                            echo "<message>Incomplete arguments</message>"; 
                        }
                        
                    }
                    else{
                        echo "<status>error</status>";                        
                        echo "<message>Invalid token</message>";
                    }           
                    break;         
                }
                case 'remove_user':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        if (isset($_GET['login'])){
                            $user = EfrontUserFactory :: factory($_GET['login']);
                            $user -> delete();
                            echo "<status>ok</status>";
                        }
                        else{
                            echo "<status>error</status>";                           
                            echo "<message>Incomplete arguments</message>"; 
                        }
                    }
                    else{
                        echo "<status>error</status>";                       
                        echo "<message>Invalid token</message>";
                    }         
                    break;  
                }
                case 'lesson_to_user':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        if (isset($_GET['login']) && isset($_GET['lesson'])){
                            $insert['users_LOGIN'] = $_GET['login'];
                            $insert['lessons_ID'] = $_GET['lesson'];
                            $insert['active'] = '1';
                            $insert['from_timestamp'] = time();
                            $res = ef_getTableData("users_to_lessons", "*", "users_LOGIN='".$_GET['login']."' and lessons_ID=".$_GET['lesson']);
                            if (sizeof($res) == 0){
                                ef_insertTableData("users_to_lessons",$insert);
                                echo "<status>ok</status>";
                            }
                            else{
                                echo "<status>error</status>";                                
                                echo "<message>Assignment already exists</message>";
                            }
                        }
                        else{
                            echo "<status>error</status>";                           
                            echo "<message>Incomplete arguments</message>"; 
                        }
                        
                    }
                    else{
                        echo "<status>error</status>";                       
                        echo "<message>Invalid token</message>";
                    }         
                    break;           
                }
                case 'lesson_from_user':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        if (isset($_GET['login']) && isset($_GET['lesson'])){
                            $res = ef_deleteTableData("users_to_lessons", "users_LOGIN='".$_GET['login']."' and lessons_ID=".$_GET['lesson']);
                            echo "<status>ok</status>";
                        }
                        else{
                            echo "<status>error</status>";                           
                            echo "<message>Incomplete arguments</message>"; 
                        }
                        
                    }
                    else{
                        echo "<status>error</status>";                       
                        echo "<message>Invalid token</message>";
                    }         
                    break;           
                }
                case 'user_lessons':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        if (isset($_GET['login'])){
                            $lessons = ef_getTableData("users_to_lessons ul, lessons l", "l.name", "ul.lessons_ID = l.ID and ul.users_LOGIN='".$_GET['login']."'");
                            echo "<xml>";
                            for ($i=0; $i<sizeof($lessons); $i++){
                                echo "<lesson>".$lessons[$i]['name']."</lesson>";
                            }
                            echo "</xml>";
                        }
                        else{
                            echo "<status>error</status>";
                            echo "<br>";
                            echo "<message>Incomplete arguments</message>"; 
                        }
                    }
                    else{
                        echo "<status>error</status>";
                        echo "<br>";
                        echo "<message>Invalid token</message>";
                    }         
                    break; 

                }
                case 'lesson_info':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        if (isset($_GET['lesson'])){
                            try{
                                $lesson = new EfrontLesson($_GET['lesson']);
                                $info = $lesson -> getStatisticInformation();
                                echo "<xml>";
                                echo "<general_info>";
                                echo "<name>".$lesson -> lesson['name']."</name>";
                                echo "<direction>".$info['direction']."</direction>";
                                echo "<price>".$info['price_string']."</price>";
                                echo "<language>".$info['language']."</language>";
                                echo "</general_info>";
                                echo "</xml>";    
                            }
                            catch (Exception $e){
                                echo "<status>error</status>";                            
                                echo "<message>Lesson doesn't exist</message>"; 
                            }
                            
                        }
                        else{
                            echo "<status>error</status>";                            
                            echo "<message>Incomplete arguments</message>"; 
                        }
                        
                    }
                    else{
                        echo "<status>error</status>";                       
                        echo "<message>Invalid token</message>";
                    }         
                    break;           
                }
                case 'user_info':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        if (isset($_GET['login'])){
                            try{
                                $user = EfrontUserFactory :: factory($_GET['login']);
                                echo "<xml>";
                                echo "<general_info>";
                                echo "<name>".$user -> user['name']." ".$user -> user['surname']."</name>";
                                echo "<active>".$user -> user['active']."</active>";
                                echo "<user_type>".$user -> user['user_type']."</user_type>";                            
                                echo "</general_info>";
                                echo "</xml>";
                            }
                            catch (Exception $e){
                                echo "<status>error</status>";                            
                                echo "<message>User doesn't exist</message>"; 
                            }
                        }
                        else{
                            echo "<status>error</status>";                            
                            echo "<message>Incomplete arguments</message>"; 
                        }
                        
                    }
                    else{
                        echo "<status>error</status>";                        
                        echo "<message>Invalid token</message>";
                    }         
                    break;           
                }
                case 'lessons':{
                    if (isset($_GET['token']) && checkToken($_GET['token'])){
                        $lessons = ef_getTableData("lessons","id, name");
                        echo "<xml>";
                        echo "<lessons>";
                        for ($i=0; $i < sizeof($lessons);$i++){
                            echo "<lesson>";
                            echo "<id>".$lessons[$i]['id']."</id>";
                            echo "<name>".$lessons[$i]['name']."</name>";
                            echo "</lesson>";
                        }
                        echo "</lessons>";
                        echo "</xml>";
                    }
                    else{
                        echo "<status>error</status>";                       
                        echo "<message>Invalid token</message>";
                    }         
                    break;  
                }
                case 'logout':{
                    if (isset($_GET['token'])){
                        $token = $_GET['token'];
                        ef_deleteTableData("tokens","token='$token'");
                        echo "<status>ok</status>";        
                    }
                    else{
                        echo "<status>error</status>";                        
                        echo "<message>No token provided</message>";
                    }
                    break;
                }
                default:break;            
            }    
        }
    }
    else{
        echo "<status>error</status>";        
        echo "<message>The api module is disabled</message>";
    }
?>