0
votes

I tried to install the Captcha (https://github.com/kolanos/kohana-captcha) to Kohana 3.1. Module installed, but still screwed, there were questions on which to find the answer was not able to. If you specifically something I couldn't understand how it works here's the code:

/**
    * Returns the img html element or outputs the image to the browser.
    *
    * @param boolean $html Output as HTML
    * @return mixed HTML, string or void
    */
   public function image_render($html)
   {
      // Output html element
      if ($html === TRUE)
         return '<img src="'.url::site('captcha/'.Captcha::$config['group']).'" width="'.Captcha::$config['width'].'" height="'.Captcha::$config['height'].'" alt="Captcha" class="captcha" />';

      // Send the correct HTTP header
        Request::instance()->headers['Content-Type'] = 'image/'.$this->image_type;
        Request::instance()->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0';
        Request::instance()->headers['Pragma'] = 'no-cache';
        Request::instance()->headers['Connection'] = 'close';

      // Pick the correct output function
      $function = 'image'.$this->image_type;
      $function($this->image);

      // Free up resources
      imagedestroy($this->image);
   }

... that's out of this class http://prog-school.ru/forum/go.php?https://github.com/kolanos/kohana-captcha/blob/master/classes/captcha.php

questions:

  1. In the variable $html, when you call this method(config by default) is true. Therefore is the return and the underlying code should not be executed, but the debugger says the opposite... how does it work?

  2. A little later in the variable $function is passed a string by concatenating "image" and $thos->image_type (as seen above = "png"). It turns out the line with the name of a function which gives an image in png format("imagepng"). And the following line is used obscure syntax: $function($this->image); What these lines do?

I hope someone will help me to understand how it works.

1

1 Answers

0
votes
  1. There is no html value set anywhere in the config. The return will only work if $html is TRUE i.e. image_render(TRUE). It will not execute if you call image_render(1) or image_render('some string') because of === operator used in if statement. Check here to learn more converting to boolean type.
  2. The first line evaluates function name (eg. imagepng), second calls this function. See variable functions for more details.