Esempio n. 1
0
 /**
  * main class method; Create the image and return the url to the image file;
  * @param string relative path to site root
  * @return mixt image url or error
  */
 function getImageURL($relpath)
 {
     $this->garbageCollector();
     // check write permissions on captcha folder
     $fld = new KT_folder();
     $fld->createFolder(KT_CAPTCHA_TEMP_FOLDER);
     if (!$fld->checkRights(KT_CAPTCHA_TEMP_FOLDER, 'write')) {
         if ($GLOBALS['tNG_debug_mode'] == 'DEVELOPMENT') {
             $error = KT_getResource('FOLDER_ERROR_D', 'Captcha', array(KT_CAPTCHA_TEMP_FOLDER));
         } else {
             $error = KT_getResource('FOLDER_ERROR', 'Captcha');
         }
         return $this->formatError($error);
     }
     // with gd
     if ($this->lib == "gd") {
         $arr["GD Version"] = 'GD not available';
         if (function_exists('gd_info')) {
             $arr = gd_info();
             preg_match("/(2)\\.[\\d]+/i", $arr["GD Version"], $matches);
         }
         if (!isset($arr) || !isset($matches[1]) || (int) $matches[1] < 2) {
             $error = KT_getResource('PHP_GD_VERSION_ERROR', 'Captcha', array($arr["GD Version"]));
             return $this->formatError($error);
         }
         $im = imagecreatefrompng(dirname(__FILE__) . '/captcha.png') or $error = KT_getResource('PHP_GD_ERROR', 'Captcha', array());
         if (isset($error)) {
             return $this->formatError($error);
         }
         $string = $this->getTextCaptcha();
         $font = imageloadfont(dirname(__FILE__) . "/fonts/Courier.gdf");
         if ($font === false) {
             $font = 5;
         }
         $fontFileName = dirname(__FILE__) . '/fonts/MyriadWebPro.ttf';
         $wFont = 24;
         $hFont = 24;
         // write the letters
         for ($i = 0; $i < strlen($string); $i++) {
             $color1 = rand(0, 64);
             $color2 = rand(0, 64);
             $color3 = rand(0, 64);
             $text_color = imagecolorallocate($im, $color1, $color2, $color3);
             $okttf = false;
             if (function_exists('imagettftext')) {
                 $okttf = @imagettftext($im, 14, rand(-25, 25), 10 + $i * $wFont, $hFont + rand(4, 26), $text_color, $fontFileName, $string[$i]);
             }
             if ($okttf === false) {
                 $fim = imagecreatetruecolor($wFont + 9, $hFont + 9);
                 $back = imagecolorallocate($fim, 255, 255, 255);
                 imagefilledrectangle($fim, 0, 0, $wFont + 8, $hFont + 8, $back);
                 $transparent2 = imagecolorallocate($fim, 255, 255, 255);
                 $text_color = imagecolorallocate($fim, $color1, $color2, $color3);
                 imagestring($fim, $font, 4, 4, $string[$i], $text_color);
                 if (function_exists("imagerotate")) {
                     $fim = imagerotate($fim, rand(-25, 25), $transparent2);
                 }
                 $iTranspa2 = imagecolortransparent($fim, $transparent2);
                 imagecopymerge($im, $fim, 0 + $i * $wFont, rand(4, 26), 0, 0, $wFont + 9, $hFont + 9, 80);
                 imagedestroy($fim);
             }
         }
         imagepng($im, KT_CAPTCHA_TEMP_FOLDER . $this->filename);
         imagedestroy($im);
         // with imagemagick
     } else {
         $sourceFileName = dirname(__FILE__) . '/captcha.png';
         $fontFileName = dirname(__FILE__) . '/fonts/MyriadWebPro.ttf';
         $destFileName = KT_CAPTCHA_TEMP_FOLDER . $this->filename;
         $arrCommands = array($GLOBALS['KT_prefered_imagemagick_path'] . 'convert');
         $shell = new KT_shell();
         $direction = rand(0, 10);
         if ($direction % 2 == 0) {
             $textRend = -rand(8, 11) . 'x0+' . (5 + (8 - strlen($this->text)) * 20) . '+' . (70 - (8 - strlen($this->text)) * 5);
         } else {
             $textRend = rand(8, 11) . 'x0+' . (5 + (8 - strlen($this->text)) * 20) . '+' . (35 + (8 - strlen($this->text)) * 5);
         }
         $arrArguments = array('-font', $fontFileName, '-pointsize', '34', '-fill', 'rgb(' . rand(0, 32) . ',' . rand(0, 32) . ',' . rand(0, 32) . ')', '-annotate', $textRend, $this->text, '-wave', '3x50', '-region', '100x70+' . rand(0, 100) . '+0', '-swirl', '25', '-region', '100x70+' . rand(0, 100) . '+0', '-swirl', '-25', $sourceFileName, $destFileName);
         $shell->execute($arrCommands, $arrArguments);
         if ($shell->hasError()) {
             $arr = $shell->getError();
             $ret = $this->formatError($arr[0]);
             return $ret;
         }
     }
     return $relpath . KT_CAPTCHA_TEMP_URL . $this->filename;
 }
Esempio n. 2
0
 /**
  * Check if the folder exists and has write permissions.
  * If the folder does not exists, try to create it.
  * If the folder does not have write permissions or if could not create it, set error.	
  * @param string $path the path
  * @param string $right the right to check
  * @param string $from  from what function is called
  * @return boolean true if is installed or false if not;
  * @access private
  */
 function checkFolder($path, $right, $from)
 {
     if (strtolower(substr(PHP_OS, 0, 1)) == 'w') {
         $path = str_replace('/', '\\', $path);
     }
     if (preg_match("/\\./ims", $path)) {
         $arr = split("[/\\]", $path);
         array_pop($arr);
         $path = implode(DIRECTORY_SEPARATOR, $arr);
     }
     if (is_file($path)) {
         $arr = explode(DIRECTORY_SEPARATOR, $path);
         array_pop($arr);
         $path = implode(DIRECTORY_SEPARATOR, $arr);
     }
     $folder = new KT_folder();
     $folder->createFolder($path);
     if ($right != '') {
         $res = $folder->checkRights($path, $right);
         if ($res !== true) {
             $this->setError('PHP_IMAGE_CHECK_FOLDER_ERROR', array($from), array($from, $path, $right));
         }
     }
     if ($folder->hasError()) {
         $arr = $folder->getError();
         $this->setError('PHP_IMAGE_FOLDER_ERROR', array($from, $arr[0]), array($from, $arr[1]));
     }
 }
Esempio n. 3
0
 /**
  * Check if the uploaded folder exists and has write permissions.
  * If the folder does not exists, try to create it.
  * If the folder does not have write permissions or if could not create it, set error.
  * @return nothing;
  * @access public
  */
 function checkFolder()
 {
     if ($this->fileExists) {
         $folder = new KT_folder();
         $folder->createFolder($this->folder);
         $right = $folder->checkRights($this->folder, 'write');
         if ($folder->hasError()) {
             $arr = $folder->getError();
             $this->setError('PHP_UPLOAD_FOLDER_ERROR', array($arr[0]), array($arr[1]));
         }
         if ($right !== true) {
             $this->setError('PHP_UPLOAD_CHECK_FOLDER_ERROR', array(), array($this->folder));
         }
     }
 }
Esempio n. 4
0
 /**
  * verify the rights on the folder of the given file;
  * @param string $file the absolute path of the file to be checked;
  * @param string $mode the right to be checked: read/write:
  * @param string $from the function that needs the check;
  * @return nothing;
  * @access private
  */
 function checkFolder($file, $mode, $from)
 {
     $folderName = $this->getFolder($file);
     $folder = new KT_folder();
     $folder->createFolder($folderName);
     switch ($mode) {
         case 'read':
             $right = $folder->checkRights($folderName, 'read');
             break;
         case 'write':
         default:
             $right = $folder->checkRights($folderName, 'write');
             break;
     }
     if ($folder->hasError()) {
         $arr = $folder->getError();
         $this->setError('PHP_FILE_FOLDER_ERROR', array($from, $arr[0]), array($from, $arr[1]));
     }
     if ($right !== true) {
         $this->setError('PHP_FILE_CHECK_FOLDER_ERROR', array($from), array($from, $mode, $folderName));
     }
 }