<?php

get();

function connectToDatabase(){
    require_once('db.php');
    $connection = new database();
    return $connection;

}


function get(){
    
    $connection = connectToDatabase();
    
    $username = $_REQUEST['username'];
    $page = $_REQUEST['page'];
    
    if($username!="" && $connection->checkParameters($username)){
        
        $limit = 10 ;
        $skip = $page * $limit ;
        
        $output = array();
        $get_collection = $connection->q("select * from Messages where User='$username' or User='all' order by ID DESC limit ".$skip.",".$limit);
        while($get_collection_r = $connection->a($get_collection)){
            $record = array();
    		$record['ID'] = $get_collection_r['ID'];
    		$record['Text'] = $get_collection_r['Text'];
    		
    		$d1 = strtotime($get_collection_r['Date']);
			$d2 = ceil((time()-$d1)/60);
			$record['Date'] = $d2 ;
    		
    		$output[] = $record;
        }
        echo json_encode($output);
        
    }else {
        print "null";
    }
    
}



?>