Topic: Accessing the ConfTool REST interface from a PHP script to download all files  (Read 118154 times)

We would like to download all files from our ConfTool Pro installation from another server or system.
How can this be done?

ConfTool Pro has a simple but secure REST interface, that allows to access the export functions, validate users and also to download all or selected files from the system.

For general information about the REST interface, please have a look at:
https://www.conftool.net/ctforum/index.php/topic,280.0.htm


The PHP script below shows a simple example how to download all files from a ConfTool Pro installation via the REST interface.

The script requires an XML file with the information on all submissions and the available downloads as data source.
This file can be generated by use of the export function at:

Overview => Data Import and Export => Export Data

using "Export Contributions" and the options:
- Include information about uploaded files.
- Export format: XML

Alternatively, you can use a bash script, which you find here, to create the XML export:
https://www.conftool.net/ctforum/index.php/topic,281.0.html


Code: [Select]
<?php
/**
 * A simple example script on how to download files from ConfTool via the REST interface using PHP
 * ATTENTION: Existing files will be overwritten.
 *
 * Do NOT call this file using your web browser but via the command line, e.g.
 * php download_files.php
 *
 * by Harald Weinreich 2013
 */

// Allow only local calls!
if (php_sapi_name()!='cli') die();

global 
$password;
$password='**********'// Set the common password here...

$input 'abstracts.xml';

// load data from abstracts file (XML format, including file download parameters).
$papers simplexml_load_file($input);

// iterate over all papers within this XML file
for ($i=0$size=count($papers); $i $size$i++) {
  
$paper $papers->paper[$i];
  
#print_r($paper);
  // only process the download if the acceptance status is NOT on hold (0) and not rejected (-1)
  
if ($paper->acceptance_status < -|| $paper->acceptance_status >) { 
    
// Test for the up to 3 first uploads, if a file is present.
    
if ($paper->download_link_a) {
       echo 
"Paper $paper->paperID, 1st file: $paper->original_filename_a\n";
       
get_file_rest($paper->paperID1''$paper->original_filename_a$paper->download_link_a);
    }
    if (
$paper->download_link_b) {
       echo 
"Paper $paper->paperID, 2nd file: $paper->original_filename_b\n";
       
get_file_rest($paper->paperID2''$paper->original_filename_b$paper->download_link_b);
    }
    if (
$paper->download_link_c) {
       echo 
"Paper $paper->paperID, 3rd file: $paper->original_filename_c\n";
       
get_file_rest($paper->paperID3''$paper->original_filename_c$paper->download_link_c);
    }
    
// Now also test for the 3 final uploads, if a file is present.
    
if ($paper->download_final_link_a) {
       echo 
"Paper $paper->paperID, 1st final: $paper->original_filename_final_a\n";
       
get_file_rest($paper->paperID1'final'$paper->original_filename_final_a$paper->download_final_link_a);
    }
    if (
$paper->download_final_link_b) {
       echo 
"Paper $paper->paperID, 2nd final: $paper->original_filename_final_b\n";
       
get_file_rest($paper->paperID2'final'$paper->original_filename_final_b$paper->download_final_link_b);
    }
    if (
$paper->download_final_link_c) {
       echo 
"Paper $paper->paperID, 3rd final: $paper->original_filename_final_c\n";
       
get_file_rest($paper->paperID3'final'$paper->original_filename_final_c$paper->download_final_link_c);
    }
    echo 
"\n";
  }
}

/**
 * The code to generate the URL to access the REST interface and a file name for the local hard drive.
 *
 * @param int $id paper ID
 * @param int $index number of uploaded paper (1-3) for this sumbission
 * @param string $version either empty for the first upload or "final" for the final version.
 * @param string $original_name original file name by author
 * @param string $download_url  download url, if all downloads are public. The URL is only used to determine the REST url.
 */
function get_file_rest($id,$index,$version,$original_name,$download_url) {
    global 
$password// Access to password parameter for REST interface

    
sleep(1); // Sleep one second, to make sure that the nonce is new.

    // compose REST URL
    
$page='downloadPaper'// Name of page to access via REST.
    
$nonce=time();         // get nonce...
    
$passhash strtolower(hash('sha256',$nonce.$password));  // Generate passhash
    
$base_url substr($download_url0strrpos($download_url'/') + 1); // Get base URL of ConfTool installation from download link
    
$url $base_url."rest.php?page=$page&nonce=$nonce&passhash=$passhash&form_id=$id&form_index=$index";
    if (
$version$url.="&form_version=$version";

    
#echo $url."\n"; // Show URL...

    // Compose download file name
    
$extension substr($original_namestrrpos($original_name'.') + 1);
    
$download_name "Contribution_".$id;
    if (
$version$download_name.="_$version";
    
$download_name .= "_".$index.".".$extension;
    
// exec("wget --quiet -O '$download_name' '$url'");
    
if (wget($url$download_name))
        echo 
"File downloaded: $download_name\n";
    else
        echo 
"==========> Download ERROR: $download_name\n";

}

/**
 * Downloading a file from ConfTool using fopen
 *
 * @param string $url the REST URL
 * @param string $localfile the local file name
 * @return bool was the download successful?
 */
function wget($url$localfile) {
    
// Open remote file via REST
    
$rh fopen($url'rb');
    if (
$rh===false ) {
        @
fclose($rh);
        return 
false;
    }
    
// Open local file for write
    
$wh fopen($localfile'wb');
    if (
$wh===false) {
        @
fclose($wh);
        return 
false;
    }
    
// Now read the file until EOF
    
while (!feof($rh)) {
        if (
fwrite($whfread($rh8129)) === FALSE) {
            @
fclose($rh);
            @
fclose($wh);
            return 
false;
        }
    }
    
// Mission completed.
    
fclose($rh);
    
fclose($wh);
    return 
true;
}

?>