<?php 
//file-procesor upload,delete,modify file 

    class file_processor
    {
        function upload_file($inputName)
        {
            include 'helpers/clive.php';
            $clive = new clive();
            //add file to regisrty then add the file into documents folder 
            $file_name = $_FILES[$inputName]['name'];
            $location = $_FILES[$inputName]['tmp_name'];
            $file_type = $_FILES[$inputName]['type'];
            $file_size = $_FILES[$inputName]['size'];
            //add file to registry 
            $dataslave = new dataslave();
            $dbn= $dataslave->install_pdo_docs();
            $token = base64_decode($_POST['token']);

            $tk_data = explode(';',$token);
            $client_id =$tk_data[0];
            $lw_id = $tk_data[1];
            $convo_id = $tk_data[2];
           $extension = explode('/',$file_type);
           
           $registry_query = $dbn->prepare('INSERT INTO documents_registry (doc_type,doc_name,doc_size,client_id,lw_id)
            VALUES
           (:dType,:dName,:dSize,:cId,:lId)
           ');

           $registry_query->execute(array(':dType'=>$file_type,
                                        ':dName'=>$file_name,
                                        'dSize'=>$file_size,
                                        ':cId'=>$client_id,
                                        ':lId'=>$lw_id
        ));
        //move uploaded file to upload folder 
        $file_desitnation ='documents/'.$lw_id.'_'.$file_name;
        
        move_uploaded_file($_FILES[$inputName]['tmp_name'],$file_desitnation);

        //add file as msg
        $dbn_messenger = $dataslave->install_pdo_messenger();

        $file_msg = $dbn_messenger->prepare("INSERT INTO msgs (msg_body,msg_type,msg_file,msg_file_format,conversation_id,sender_email) VALUES(
            :m_body,
            :m_type,
            :m_file,
            :format,
            :cID,
            :semail)");

            $file_msg->execute(array(':m_body'=>$file_name,
                                    ':m_type'=>'file',
                                    ':m_file'=>$file_desitnation,
                                    ':format'=>$file_type,
                                    ':cID'=>$convo_id,
                                    ':semail'=>$clive->get_lw_email($lw_id) ));
        
        }

        function download_file($file_url)
        {
            include 'documents/'.$file_url;
        }

        function single_direct_upload($inputName)
        {   
            $dataslave = new dataslave();
            $enc = new encryption();
            $dbn= $dataslave->install_pdo_docs();
            $dbn_main = $dataslave->install_pdo_main();
            //file destination 
            $lw_id="13";
            $file_name = $_FILES[$inputName]['name'];
            $location = $_FILES[$inputName]['tmp_name'];
            $file_type = $_FILES[$inputName]['type'];
            $file_size = $_FILES[$inputName]['size'];
            //get lw id from current session later user cookies instead of ip address 
           
            $session_data = $_POST['cookie'];
            $session_data = $enc->decrypt($session_data);
            $session_data = explode(";",$session_data);
            $lw_id = $session_data[3];
            
            $registry_query = $dbn->prepare('INSERT INTO documents_registry (doc_type,doc_name,doc_size,lw_id)
            VALUES
           (:dType,:dName,:dSize,:lId)
           ');

           $registry_query->execute(array(':dType'=>$file_type,
                                        ':dName'=>$file_name,
                                        'dSize'=>$file_size,
                                        
                                        ':lId'=>$lw_id
        ));
            //move uploaded file 
            $file_desitnation ='documents/'.$lw_id.'_'.$file_name;
        
            move_uploaded_file($_FILES[$inputName]['tmp_name'],$file_desitnation);
        }
    }
?>