MDL-43074 assignfeedback_editpdf: Use escapeshellarg for all args in exec command.
authorDamyon Wiese <damyon@moodle.com>
Mon, 9 Dec 2013 02:51:54 +0000 (10:51 +0800)
committerDamyon Wiese <damyon@moodle.com>
Mon, 9 Dec 2013 02:52:35 +0000 (10:52 +0800)
This is to fix a reported problem with windows compatibility (e.g. when ghostscript is installed in
a folder with spaces in the name).

mod/assign/feedback/editpdf/classes/pdf.php

index 1eb58aa..6286fe1 100644 (file)
@@ -426,14 +426,14 @@ class pdf extends \FPDI {
 
         if ($generate) {
             // Use ghostscript to generate an image of the specified page.
-            $gsexec = \get_config('assignfeedback_editpdf', 'gspath');
-            $imageres = 100;
-            $filename = $this->filename;
-            $pagenoinc = $pageno + 1;
+            $gsexec = \escapeshellarg(\get_config('assignfeedback_editpdf', 'gspath'));
+            $imageres = \escapeshellarg(100);
+            $imagefilearg = \escapeshellarg($imagefile);
+            $filename = \escapeshellarg($this->filename);
+            $pagenoinc = \escapeshellarg($pageno + 1);
             $command = "$gsexec -q -sDEVICE=png16m -dSAFER -dBATCH -dNOPAUSE -r$imageres -dFirstPage=$pagenoinc -dLastPage=$pagenoinc ".
-                "-dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sOutputFile=\"$imagefile\" \"$filename\"";
+                "-dGraphicsAlphaBits=4 -dTextAlphaBits=4 -sOutputFile=$imagefilearg $filename";
 
-            //$command = escapeshellcmd($command);
             $output = null;
             $result = exec($command, $output);
             if (!file_exists($imagefile)) {
@@ -484,9 +484,10 @@ class pdf extends \FPDI {
 
         $file->copy_content_to($tempsrc); // Copy the file.
 
-        $gsexec = \get_config('assignfeedback_editpdf', 'gspath');
-        $command = "$gsexec -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile=\"$tempdst\" \"$tempsrc\"";
-        //$command = escapeshellcmd($command);
+        $gsexec = \escapeshellarg(\get_config('assignfeedback_editpdf', 'gspath'));
+        $tempdstarg = \escapeshellarg($tempdst);
+        $tempsrcarg = \escapeshellarg($tempsrc);
+        $command = "$gsexec -q -sDEVICE=pdfwrite -dBATCH -dNOPAUSE -sOutputFile=$tempdstarg $tempsrcarg";
         exec($command);
         @unlink($tempsrc);
         if (!file_exists($tempdst)) {