<?php header('Access-Control-Allow-Origin: *'); ?>
<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/*

Run4Football v0.1 Ajax API
This file is part of Run4Football. (C)2019 Corrado Mulas - CM TLC - R&D Dept. - Ardara (SS) 

    Run4Football 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, either version 3 of the License, or
    (at your option) any later version.

    Run4Football 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 Run4Football.  If not, see <http://www.gnu.org/licenses/>.

*/        

/*##########NOTE################


*/
############FINE NOTE###########
/**
 * Description of r4fbApi
 *
 * @author corradomulas for Run PoliTo - Corso Duca degli Abruzzi 24, 10129 Torino (TO)
 */

require '/sda1/www/test.mulas.me/run4football/lib/r4fb.php';
require '/sda1/www/idp.runpolito.ml/vendor/autoload.php';
//require '/sda1/www/test.mulas.me/run4football/lib/sns.php';
require_once '/sda1/www/idp.runpolito.ml/lib/_autoload.php';
//session_start();
$auth = new \SimpleSAML\Auth\Simple('runpolito-sp');
SimpleSAML_Session::getSessionFromRequest()->cleanup();

if (!$auth->isAuthenticated()) 
{
   // SimpleSAML_Session::getSessionFromRequest()->cleanup();
    //401 e tu muto
    http_response_code(401);
}
else
{
    
    $guid = $auth->getAttributes()['guid'][0];

//inizio partita:
//inserire funzione recupero matchid da DB utilizzando id utente da Service Provider e DB
switch ($_GET['op'])
{
    case "start":
        if(isset($_GET['mid']))
        {
            if(!r4fb::startMatch($_GET['mid'], $guid)) http_response_code(500);
        }
        else
        {
            echo "<pre>Parametri mancanti.</pre>";
            http_response_code(400);
        }
        break;
    case "instor":
        //r4fb::insTorneo($tsinizio,$org,$loc,$man);
        break;
    case "insarb":
        
        break;
    case "insamm":
        if(isset($_GET['plid']) && isset($_GET['mid']) && isset($_GET['ta']))
        {
            r4fb::insAmm($_GET['plid'],$_GET['mid'],$_GET['ta'], $guid);
        }
        else
        {
            echo "<pre>Parametri mancanti.</pre>";
        }
        break;
    case "insq":
        
        break;
    case "inspl":
        
        break;
    case "insma":
        
        break;
    case "randma":
        if(isset($_GET['mid']) && isset($_GET['uid']))
        {
            r4fb::randStart($_GET['mid'], $_GET['uid']);
        }
        else
        {
            echo "<pre>Parametri mancanti.</pre>";
        }
        break;
    case "insgl":
        if(isset($_GET['plid']) && isset($_GET['mid']))
        {
            r4fb::insGoal($_GET['plid'], $_GET['mid'], $guid);
        }
        else
        {
            echo "<pre>Parametri mancanti.</pre>";
        }
        break;
    case "insfa":
        if(isset($_GET['sid']))
        {
            r4fb::insFallo($_GET['sid'], $guid);
        }
        else
        {
            echo "<pre>Parametri mancanti.</pre>";
        }
        break;
    case "getfa":
        if(isset($_GET['sid']))
        {
            r4fb::getFalli($_GET['sid']);
        }
        else
        {
            echo "<pre>Parametri mancanti.</pre>";
        }
        break;
    case "stop":
        r4fb::stopMatch(r4fb::curMatch($guid), $guid); 
        break;
    case "logout":
        $auth->logout(); 
        break;
    case "genguid":
        echo r4fb::guidv4();
        break;
    case "csms":
      //  snsrun::sendSMS($_GET['num'], $_GET['msg']);
        break;
    case "uniqid":
        echo str_pad(mt_rand(0, 9999), 4, '0', STR_PAD_LEFT);
        break;
    case "randstr":
        echo bin2hex(openssl_random_pseudo_bytes(4));
        break;
    case "updpt":
        echo r4fb::getPoints($_GET['mid'],$_GET['sn']);
        break;
    case "updfl":
        echo r4fb::getFalli($_GET['sid']);
        break;
    case "getpls":
        $pls = r4fb::fetchPlayers($squadra);
        
        $output = array(
            'status'  => 'ok',
            'content' => $pls,
            );

print_r(json_decode(json_encode($output)));
        break;
    case "opvt":
        if(!r4fb::toggleVote($_GET['trn'],1)) http_response_code(500);
        break;
    case "clvt":
        if(!r4fb::toggleVote($_GET['trn'],2)) http_response_code(500);
        break;

    case "rcmsg":
        
        $tipo = $_GET['tipo'];
        $msgs = r4fb::TTrcvdMsgs($_GET['uid'], $tipo);
        //0 tutti i messaggi
        //1 da leggere
        //2 letti
        switch($tipo)
        {
            case 0:
                $cat = "(tutti)";
            case 1:
                $cat = "(non letti)";
                break;
            case 2:
                $cat = "(letti)";
                break;
        }
        $max = count($msgs);
               echo "
            <table border='1' style='display:  inline-table'>
          <!--  <caption>Messaggi in arrivo</caption> -->
	
		<thead>
		
			<tr>
				<th scope='col' colspan='2'>Lista messaggi ".$cat."</th>
			</tr>

		</thead>
		
		
		<tbody align='center'>
		        <tr>
				<td>Inviato</td>
                                <td>Mittente</td>
                                <td>Oggetto</td>
                                <td>Urgente</td>
                                <td>Leggi</td>
			</tr>";
                    
                    for($j=0; $j<$max; $j++)
//                       if($classf[$j]['pt_gir'] != 0)
  //                     {
                        echo "<tr>
                                <td>".$msgs[$j]['ts_sent']."</td>
				<td>".$msgs[$j]['sender']."</td>
                                <td>".$msgs[$j]['subject']."</td>
				<td>".$msgs[$j]['prio']."</td>
                                <td><a href='#' onclick=\"readMsg('".$msgs[$j]['id']."')\">Leggi</a></td>
                             </tr>";
    //                   }
		echo "
		</tbody>
		
		<tfoot align='center'>
		
			<tr>
				<td colspan='5'></td>		
			</tr>
		
		</tfoot>

            </table>";
        break;
    default:
        //echo "<pre>puppa</pre>";
        http_response_code(408);
        break;    
}

}

?>
