<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
/**
     * Import survey in a known format (RPC function)
     *
     * Allow importing lss, csv, xls or survey zip archive in BASE 64 encoded.
     *
     * Failure status: Invalid session key, No permission, The import error
     *
     * @access public
     * @param string $sSessionKey Auth Credentials
     * @param string $sImportData String containing the BASE 64 encoded data of a lss, csv, txt or survey lsa archive
     * @param string $sImportDataType lss, csv, txt or lsa
     * @param string $sNewSurveyName (optional) The optional new name of the survey
     * @param integer $DestSurveyID  (optional) This is the new ID of the survey - if already used a random one will be taken instead
     * @return int|array The ID of the new survey in case of success
     */
	 
// without composer this line can be used
// require_once 'path/to/your/rpcclient/jsonRPCClient.php';
// with composer support just add the autoloader
function importSurvey($SurveyID, $strFilename)
{
//1:
echo "<br>1: File exists: "; var_dump(file_exists($file));
if(!file_exists($file))
{
return "File not found @ ". $file;
}

$hFile = fopen($file,"r");

$data = fread($hFile, filesize($file));
$data = base64_encode($data);
fclose($hFile);

if($data == null)
return "Error reading file content: ". $file;

 
require_once './jsonrpcphp/JsonRPCClient.php';
 
 
$rpcUrl="http://localhost:801/limesurvey/admin/remotecontrol"; // en 2018, et qui plus est avec le RGPD, le site est bien sûr chiffré en SSL ;)
 
$rpcUser="admin"; // à priori celui défini comme propriétaire du questionnaire dans LimeSurvey ?
$rpcPassword="admin";
 
 
$lsJSONRPCClient = new jsonRPCClient($rpcUrl);
//2:
$sessionKey= $lsJSONRPCClient->get_session_key($rpcUser,$rpcPassword );
//~ If an error happen
if(is_array($sessionKey))
{
    header("Content-type: application/json");
    echo json_encode($sessionKey);
    die();
}
//$response=$lsJSONRPCClient->list_surveys($sessionKey,null); // pour avoir la liste des questionnaires
//$response=$lsJSONRPCClient->list_questions($sessionKey,315627,null); // si vous voulez plutôt la liste des questions d'un questionnaire en particulier, ici le numéro 315627
//$response=$lsJSONRPCClient->list_participants($sessionKey,947376,0,50); // si vous voulez plutôt la liste des participants d'un questionnaire en particulier, ici le numéro 947376
//$response=$lsJSONRPCClient->list_users($sessionKey,null); // si vous voulez plutôt la liste des utilisateurs de limesurvey

//3:
$response = $lsJSONRPCClient->import_survey($sessionKey, $data, "lss", null, $SurveyID);
 
header("Content-type: application/json");
//~ For big array : base64 encoded
if(is_array($response)){
    echo json_encode($response);
} else {
    print_r(base64_decode($response), null );
}
//4:
//~ release the session key
$lsJSONRPCClient->release_session_key( $sessionKey );
return $SurveyID;
} 
?>