Commit 016d0b08 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Misc: add central getLastErrorAsString(). Refactor from exec() to qfqExec()

parent 682bc904
Pipeline #1038 passed with stage
in 2 minutes and 11 seconds
......@@ -1434,6 +1434,7 @@ class QuickFormQuery {
* @throws CodeException
* @throws DbException
* @throws UserFormException
* @throws UserReportException
*/
public function saveReport() {
$uid = $this->store->getVar(T3DATA_UID, STORE_SIP . STORE_ZERO, SANITIZE_ALLOW_DIGIT);
......
......@@ -797,7 +797,7 @@ class Save {
$overwrite = isset($formElement[FE_FILE_REPLACE_MODE]) && $formElement[FE_FILE_REPLACE_MODE] == FE_FILE_REPLACE_MODE_ALWAYS;
Support::moveFile($srcFile, $pathFileName, $overwrite, $chmodDir);
// If given, get chmodFile. Needs to be prefixed with a 0 (=octal) - it should not be quoted! Symbolic mode is not allowed. E.g.: 0660, or 01777
// get chmodFile
if (empty($formElement[FE_FILE_CHMOD_FILE])) {
$chmodFile = false;
} else {
......@@ -841,7 +841,10 @@ class Save {
$cmd = $this->evaluate->parse($cmd);
// Do 'autoOrient' command
$rc = exec($cmd);
$output = Support::qfqExec($cmd, $rc);
if ($rc != 0) {
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'copy failed', ERROR_MESSAGE_SUPPORT => "[cmd=$cmd]$output"]), ERROR_IO_COPY);
}
}
/**
......@@ -906,9 +909,15 @@ class Save {
// Create temporary directory
$tempDir = Support::createTempDir();
$newSrc = $tempDir . DIRECTORY_SEPARATOR . QFQ_TEMP_SOURCE;
$rc = copy($pathFileNameSrc, $newSrc);
if (false === copy($pathFileNameSrc, $newSrc)) {
$msg = HelperFile::getLastErrorAsString() . " - copy $pathFileNameSrc $newSrc";
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'copy failed', ERROR_MESSAGE_SUPPORT => $msg]), ERROR_IO_COPY);
}
$rc = chdir($tempDir);
if (false === chdir($tempDir)) {
$msg = HelperFile::getLastErrorAsString() . " - chdir($tempDir)";
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'copy failed', ERROR_MESSAGE_SUPPORT => $msg]), ERROR_IO_COPY);
}
// Split destination.
$pathParts = pathinfo($fileDestinationSplit);
......@@ -920,7 +929,11 @@ class Save {
$fileNameDest = $pathParts['basename'];
// Split PDF
$rc = exec('pdf2svg "' . $newSrc . '" "' . $fileNameDest . '" all');
$cmd = 'pdf2svg "' . $newSrc . '" "' . $fileNameDest . '" all';
$output = Support::qfqExec($cmd, $rc);
if ($rc != 0) {
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'copy failed', ERROR_MESSAGE_SUPPORT => "[$cwd][cmd=$cmd]$output"]), ERROR_IO_COPY);
}
// Array of created filenames.
$files = scandir('.');
......
......@@ -238,4 +238,17 @@ class HelperFile {
}
}
/**
* @return string
*/
public static function getLastErrorAsString() {
if (NULL === ($errors = error_get_last())) {
return '';
}
return $errors['type'] . ' - ' . $errors['message'];
}
}
\ No newline at end of file
......@@ -44,7 +44,8 @@ class Logger {
if (!$handle = fopen($filename, $mode)) {
throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'Error: cannot open file',
ERROR_MESSAGE_SUPPORT => "Error - cannot open. File: " . $filename . " ( CWD: " . getcwd() . ") - " . error_get_last()]),
ERROR_MESSAGE_SUPPORT => "Error - cannot open. File: " . $filename .
" ( CWD: " . getcwd() . ") - " . HelperFile::getLastErrorAsString()]),
ERROR_IO_OPEN);
}
}
......@@ -52,7 +53,8 @@ class Logger {
if (fwrite($handle, $msg . PHP_EOL) === false) {
throw new UserFormException(
json_encode([ERROR_MESSAGE_TO_USER => 'Error: cannot write file',
ERROR_MESSAGE_SUPPORT => "Error - cannot open. File: " . $filename . " ( CWD: " . getcwd() . ") - " . error_get_last()]),
ERROR_MESSAGE_SUPPORT => "Error - cannot open. File: " . $filename .
" ( CWD: " . getcwd() . ") - " . HelperFile::getLastErrorAsString()]),
ERROR_IO_WRITE);
}
......
......@@ -1310,8 +1310,7 @@ class Support {
/**
*
*/
public
static function createTempDir() {
public static function createTempDir() {
return exec("mktemp -d --tmpdir " . QFQ_TEMP_FILE_PATTERN);
......@@ -1324,8 +1323,7 @@ class Support {
*
* @return string
*/
public
static function falseEmptyToZero($val) {
public static function falseEmptyToZero($val) {
return ($val == '' || $val == false) ? '0' : $val;
}
......@@ -1336,8 +1334,7 @@ class Support {
* @param string $key
* @return bool true: if $key exists and a) is empty or b) =='1'
*/
public
static function isEnabled(array $arr, $key) {
public static function isEnabled(array $arr, $key) {
if (!array_key_exists($key, $arr)) {
return false;
}
......@@ -1358,8 +1355,7 @@ class Support {
*
* @return string
*/
public
static function handleEscapeSpaceComment($str) {
public static function handleEscapeSpaceComment($str) {
$str = trim($str);
if ($str == '') {
......@@ -1397,8 +1393,7 @@ class Support {
*
* @return string
**/
public
static function ldap_escape($subject, $ignore = '', $flags = 0) {
public static function ldap_escape($subject, $ignore = '', $flags = 0) {
if (function_exists('ldap_escape')) {
......@@ -1470,8 +1465,7 @@ class Support {
* @return string
* @throws UserFormException
*/
public
static function htmlEntityEncodeDecode($mode, $data) {
public static function htmlEntityEncodeDecode($mode, $data) {
switch ($mode) {
case MODE_ENCODE:
......@@ -1522,16 +1516,15 @@ class Support {
* @param int $rc
* @return string The content that is displayed on the website
*/
public
static function qfqExec($cmd, &$rc = 0) {
public static function qfqExec($cmd, &$rc = 0) {
exec($cmd, $arr, $rc);
$output = implode('<br>', $arr);
if ($rc != 0) {
$output = $rc . " - " . $output;
$output = "[rc=$rc] $output";
}
return ($output);
return $output;
}
}
\ No newline at end of file
......@@ -506,6 +506,7 @@ class Download {
*
* @return string
* @throws CodeException
* @throws DbException
* @throws DownloadException
* @throws UserFormException
* @throws UserReportException
......
......@@ -966,18 +966,18 @@ class Report {
default :
if ($flagControl) {
$flagOutput = false;
$token= isset($columnName[0])? $columnName[0]:'';
switch($token){
$token = isset($columnName[0]) ? $columnName[0] : '';
switch ($token) {
case COLUMN_WRAP_TOKEN:
if(isset($columnName[1])){
if (isset($columnName[1])) {
$content = Support::wrapTag('<' . substr($columnName, 1) . '>', $columnValue);
$flagOutput = true;
}
break;
case COLUMN_STORE_USER:
if(isset($columnName[1])) {
$this->store::setVar(substr($columnName, 1),$columnValue, STORE_USER);
if (isset($columnName[1])) {
$this->store::setVar(substr($columnName, 1), $columnValue, STORE_USER);
}
break;
......@@ -986,7 +986,6 @@ class Report {
}
} else {
// No special Columnname: just add the column value.
$content .= $columnValue;
......
......@@ -225,19 +225,18 @@ class SendMail {
$cmd = $sendEmail . ' ' . implode(' ', $args);
$cmd = str_replace('`', '\`', $cmd); // escape backticks (escapeshellcmd would be too thorough)
exec($cmd, $arr, $rc);
$output = Support::qfqExec($cmd, $rc);
if ($rc != 0) {
// After first installation of QFQ extension, the PERL script is not executable: is this the problem here?
$perms = fileperms($sendEmail);
if (!($perms & 0x0040)) {
chmod($sendEmail, 0755);
exec($cmd, $arr, $rc); // Give it a second try.
// exec($cmd, $arr, $rc); // Give it a second try.
$output = Support::qfqExec($cmd, $rc);
}
if ($rc != 0) {
$output = $rc . " - " . implode('<br>', $arr) . " - " . $cmd;
throw new UserFormException("Error sendmail failed: " . $output, ERROR_SENDMAIL);
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => '"Error sendmail failed', ERROR_MESSAGE_SUPPORT => "[cmd=$cmd]$output"]), ERROR_SENDMAIL);
}
}
......
......@@ -447,7 +447,7 @@ class Store {
* @param string $foundInStore Returns the name of the store where $key has been found. If $key is not found,
* return ''.
*
* @return string a) if found: value, b) false
* @return string|array a) if found: value, b) false. STORE_EXTRA returns an array for the given key.
* @throws CodeException
* @throws UserFormException
*/
......
......@@ -93,19 +93,19 @@ class BuildFormPlainTest extends AbstractDatabaseTest {
$formElement[FE_MAX] = 10;
$result = $build->buildInput($formElement, 'name:1', '', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="number" value="" min="1" max="10" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
$formElement[FE_MIN] = 1;
$formElement[FE_MAX] = '';
$result = $build->buildInput($formElement, 'name:1', '', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" min="1" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
$formElement[FE_MIN] = '';
$formElement[FE_MAX] = 10;
$result = $build->buildInput($formElement, 'name:1', '', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" max="10" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
// reset so they don't interfere with next tests
$formElement[FE_MIN] = '';
$formElement[FE_MAX] = '';
......@@ -115,19 +115,19 @@ class BuildFormPlainTest extends AbstractDatabaseTest {
$formElement[FE_CHECK_PATTERN] = '^[a-z]*$';
$result = $build->buildInput($formElement, 'name:1', '', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^[a-z]*$" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
$formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_DIGIT;
$formElement[FE_CHECK_PATTERN] = '';
$result = $build->buildInput($formElement, 'name:1', '', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^[\d]*$" data-pattern-error="Allowed characters: 0...9" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
$formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_EMAIL;
$formElement[FE_CHECK_PATTERN] = '';
$result = $build->buildInput($formElement, 'name:1', '', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" data-pattern-error="Requested format: string@domain" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
$formElement[FE_CHECK_TYPE] = SANITIZE_ALLOW_ALL;
$formElement[FE_CHECK_PATTERN] = '';
......@@ -137,7 +137,7 @@ class BuildFormPlainTest extends AbstractDatabaseTest {
$formElement[FE_DECIMAL_FORMAT] = '5,2';
$result = $build->buildInput($formElement, 'name:1', '', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="255" type="input" value="" pattern="^-?[0-9]{0,3}(\.[0-9]{0,2})?$" data-pattern-error="Requested decimal format (mantis,decimal): 5,2" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
$formElement[FE_DECIMAL_FORMAT] = '';
......@@ -147,13 +147,13 @@ class BuildFormPlainTest extends AbstractDatabaseTest {
$formElement['maxLength'] = 40;
$result = $build->buildInput($formElement, 'name:1', '', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="40" type="input" size="40" value="" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
// maxlength bigger than physical spec:
$formElement['maxLength'] = 1000;
$result = $build->buildInput($formElement, 'name:1', '', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" maxlength="1000" type="input" size="40" value="" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => '', API_ELEMENT_UPDATE => $label], $json);
// no size, no maxlength and column not in primary table
$formElement2 = $formElement;
......@@ -188,19 +188,19 @@ class BuildFormPlainTest extends AbstractDatabaseTest {
$result = $build->buildInput($formElement2, 'name:1', 'Hello World', $json);
$this->assertEquals('<input id="123" name="name:1" class="form-control" type="input" value="Hello World" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-required="no" ><div class="help-block with-errors hidden"></div>', $result);
$label['123'][API_ELEMENT_ATTRIBUTE] = ['value' => 'Hello World'];
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', API_ELEMENT_UPDATE => $label], $json);
// textarea
$formElement2['size'] = '40,10';
$result = $build->buildInput($formElement2, 'name:1', 'Hello World', $json);
$this->assertEquals('<textarea id="123" name="name:1" class="form-control" cols="40" rows="10" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-required="no" >Hello World</textarea><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', API_ELEMENT_UPDATE => $label], $json);
$formElement2['size'] = ' 40 , 10 ';
$result = $build->buildInput($formElement2, 'name:1', 'Hello World', $json);
$this->assertEquals('<textarea id="123" name="name:1" class="form-control" cols="40" rows="10" placeholder="Please type a name" title="Nice Tooltip" data-hidden="no" data-required="no" >Hello World</textarea><div class="help-block with-errors hidden"></div>', $result);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', 'disabled' => false, API_ELEMENT_UPDATE => $label], $json);
$this->assertEquals(['disabled' => false, FE_MODE_REQUIRED => '', 'form-element' => 'name:1', 'value' => 'Hello World', API_ELEMENT_UPDATE => $label], $json);
}
/**
......
......@@ -238,9 +238,6 @@ class StoreTest extends TestCase {
$this->store->appendToStore(null, STORE_RECORD);
$this->assertEquals($param, $this->store->getStore(STORE_RECORD));
$this->store->appendToStore('', STORE_RECORD);
$this->assertEquals($param, $this->store->getStore(STORE_RECORD));
$this->store->appendToStore(array(), STORE_RECORD);
$this->assertEquals($param, $this->store->getStore(STORE_RECORD));
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment