Browse Source

Update Display::img() code to return an empty string in case no image path was provided
Avoid calls with empty parameters (suggested by #scrutinizer)

Yannick Warnier 8 years ago
parent
commit
6bf1017ef9

+ 2 - 2
main/exercice/exercise.class.php

@@ -3519,7 +3519,7 @@ class Exercise
                         // the loop will stop at the end of the text
                         while (1) {
                             // quits the loop if there are no more blanks (detect '[')
-                            if (($pos = api_strpos($temp, '[')) === false) {
+                            if ($temp == false || ($pos = api_strpos($temp, '[')) === false) {
                                 // adds the end of the text
                                 $answer = $temp;
                                 $real_text[] = $answer;
@@ -3734,7 +3734,7 @@ class Exercise
                     // the loop will stop at the end of the text
                     while (1) {
                         // quits the loop if there are no more blanks (detect '[')
-                        if (($pos = api_strpos($temp, '[')) === false) {
+                        if ($temp == false || ($pos = api_strpos($temp, '[')) === false) {
                             // adds the end of the text
                             $answer = $temp;
                             $realText[] = $answer;

+ 1 - 1
main/exercice/question.class.php

@@ -272,7 +272,7 @@ abstract class Question
             return api_get_path(WEB_COURSE_PATH) . $this->course['path'] . '/document/images/' . $this->picture;
         }
 
-        return false;
+        return '';
     }
 
     /**

+ 9 - 0
main/inc/lib/display.lib.php

@@ -818,6 +818,15 @@ class Display
      */
     public static function img($image_path, $alt_text = '', $additional_attributes = array(), $filterPath = true)
     {
+        if (empty($image_path)) {
+            // For some reason, the call to img() happened without a proper
+            // image. Log the error and return an empty string to avoid
+            // breaking the HTML
+            $trace = debug_backtrace();
+            $caller = $trace[1];
+            error_log('No image provided in Display::img(). Caller info: '.print_r($caller, 1));
+            return '';
+        }
         // Sanitizing the parameter $image_path
         if ($filterPath) {
             $image_path = Security::filter_img_path($image_path);