Skip to content

Commit

Permalink
New feature #12564 remote control: adds upload_file action (#795)
Browse files Browse the repository at this point in the history
Dev: adds another action, upload_file, to the remote control interface.
  • Loading branch information
pardo-bsso authored and LouisGac committed Aug 14, 2017
1 parent 0da07ff commit a6b9fe0
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions application/helpers/remotecontrol/remotecontrol_handle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2552,6 +2552,98 @@ public function update_response($sSessionKey, $iSurveyID, $aResponseData)
}
}

/**
* Uploads one file to be used later.
* Returns the metadata on success.
*
* @access public
* @param string $sSessionKey Auth credentials
* @param int $iSurveyID ID of the Survey to insert file
* @param string $sFieldName the Field to upload file
* @param string $sFileName the uploaded file name
* @param string $sFileContent the uploaded file content encoded as BASE64
* @return array The file metadata with final upload path or error description
*/
public function upload_file($sSessionKey, $iSurveyID, $sFieldName, $sFileName, $sFileContent)
{
if (!$this->_checkSessionKey($sSessionKey)) {
return array('status' => 'Invalid session key');
}

$oSurvey=Survey::model()->findByPk($iSurveyID);

if (is_null($oSurvey)) {
return array('status' => 'Error: Invalid survey ID');
}

if (Permission::model()->hasSurveyPermission($iSurveyID, 'responses', 'create')) {
if (!Yii::app()->db->schema->getTable('{{survey_' . $iSurveyID . '}}')) {
return array('status' => 'No survey response table');
}
}
else {
return array('status' => 'No permission');
}

$tempdir = Yii::app()->getConfig("tempdir");

$sTempUploadDir = $tempdir.'/upload/';
if (!file_exists($sTempUploadDir)) {
if (!mkdir($sTempUploadDir)) {
return array('status' => 'Can not make temporary upload directory');
}
}

$aFieldMap = createFieldMap($iSurveyID, 'short', false, false, Yii::app()->getConfig('defaultlang'));
if (!isset($aFieldMap[$sFieldName])) {
return array('status' => 'Can not obtain field map');
}
$aAttributes = getQuestionAttributeValues($aFieldMap[$sFieldName]['qid']);

$iFileUploadTotalSpaceMB = Yii::app()->getConfig('iFileUploadTotalSpaceMB');

$maxfilesize = (int) $aAttributes['max_filesize'];
$allowed_filetypes = $aAttributes['allowed_filetypes'];
$valid_extensions_array = explode(",", $allowed_filetypes);
$valid_extensions_array = array_map('trim', $valid_extensions_array);

$pathinfo = pathinfo($sFileName);
$ext = strtolower($pathinfo['extension']);

// check to see that this file type is allowed
if (!in_array($ext, $valid_extensions_array)) {
return array('status' => 'The extension ' . $ext . ' is not valid. Valid extensions are: ' . $allowed_filetypes);
}

// This also accounts for BASE64 overhead
$size = (0.001 * 3 * strlen($sFileContent)) / 4;

$randfilename = 'futmp_'.randomChars(15).'_'.$pathinfo['extension'];
$randfileloc = $sTempUploadDir . $randfilename;

if ($size > $maxfilesize) {
return array('status' => sprintf('Sorry, this file is too large. Only files up to %s KB are allowed.', $maxfilesize));
}

if ($iFileUploadTotalSpaceMB>0 && ((calculateTotalFileUploadUsage()+($size/1024/1024))>$iFileUploadTotalSpaceMB)) {
return array('status' => 'Not enough free space available');
}

$uploaded = file_put_contents($randfileloc, base64_decode($sFileContent));
if ($uploaded === FALSE) {
return array('status' => 'Unable to write file');
}

return array(
"success" => true,
"size" => $size,
"name" => rawurlencode(basename($filename)),
"ext" => $ext,
"filename" => $randfilename,
"msg" => gT("The file has been successfully uploaded.")
);
}

/**
* Export responses in base64 encoded string
*
Expand Down

2 comments on commit a6b9fe0

@olleharstedt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maziminke @pardo-bsso Please get in touch to discuss the functionality of this PR and any potential bugs.

@maziminke
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pardo-bsso Can you please look into this bug report related to your code adjustments: https://bugs.limesurvey.org/view.php?id=13738

It looks like the adjustment is missing backwards compatibility. Maybe we can implement this suggested fix:
....add (validateFiles = TRUE) to RPC method add_response that we can set to false to maintain backward compatibility ?

Please sign in to comment.