1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 04:38:27 +01:00
php-e107/e107_handlers/secure_img_handler.php

426 lines
9.8 KiB
PHP
Raw Normal View History

2006-12-02 04:36:16 +00:00
<?php
/*
2009-11-12 15:11:17 +00:00
* e107 website system
*
2009-11-18 01:06:08 +00:00
* Copyright (C) 2008-2009 e107 Inc (e107.org)
2009-11-12 15:11:17 +00:00
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
*
2011-07-07 13:13:47 +00:00
* $URL$
* $Id$
2009-11-12 15:11:17 +00:00
*/
2006-12-02 04:36:16 +00:00
if (!defined('e107_INIT')) { exit; }
2011-06-30 11:10:34 +00:00
class secure_image
{
2011-06-30 11:10:34 +00:00
public $random_number;
protected $HANDLERS_DIRECTORY;
protected $IMAGES_DIRECTORY;
2013-04-26 18:19:18 -07:00
protected $FONTS_DIRECTORY;
2011-06-30 11:10:34 +00:00
protected $MYSQL_INFO;
protected $THIS_DIR;
protected $BASE_DIR;
public $FONT_COLOR = "90,90,90";
2006-12-02 04:36:16 +00:00
function secure_image()
{
/*
if ($user_func = e107::getOverride()->check($this,'secure_image'))
{
return call_user_func($user_func);
}
* */
2006-12-02 04:36:16 +00:00
list($usec, $sec) = explode(" ", microtime());
$this->random_number = str_replace(".", "", $sec.$usec);
2011-06-30 11:10:34 +00:00
$imgp = dirname(__FILE__);
2011-06-30 11:10:34 +00:00
if (substr($imgp,-1,1) != DIRECTORY_SEPARATOR) $imgp .= DIRECTORY_SEPARATOR;
$imgp = str_replace('/', DIRECTORY_SEPARATOR, $imgp);
@include($imgp.'..'.DIRECTORY_SEPARATOR.'e107_config.php');
if(!isset($mySQLserver))
{
if(defined('e_DEBUG'))
{
2011-06-30 11:10:34 +00:00
echo "FAILED TO LOAD e107_config.php in secure_img_handler.php";
}
exit;
}
// FIX - new prefered configuration format - $E107_CONFIG
if(isset($E107_CONFIG))
{
extract($E107_CONFIG);
}
2011-06-30 11:10:34 +00:00
$this->THIS_DIR = $imgp;
2011-06-30 11:10:34 +00:00
$this->BASE_DIR = realpath($imgp.'..'.DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
2011-07-07 12:24:32 +00:00
$this->HANDLERS_DIRECTORY = $HANDLERS_DIRECTORY;
2013-04-26 18:19:18 -07:00
$this->FONTS_DIRECTORY = isset($CORE_DIRECTORY) ? $CORE_DIRECTORY."fonts/" : "e107_core/fonts/";
// $this->IMAGES_DIRECTORY = str_replace('/', DIRECTORY_SEPARATOR, $IMAGES_DIRECTORY);
$this->IMAGES_DIRECTORY = $IMAGES_DIRECTORY;
2011-06-30 11:10:34 +00:00
$this->MYSQL_INFO = array('db' => $mySQLdefaultdb, 'server' => $mySQLserver, 'user' => $mySQLuser, 'password' => $mySQLpassword, 'prefix' => $mySQLprefix);
2006-12-02 04:36:16 +00:00
}
function create_code()
{
if ($user_func = e107::getOverride()->check($this,'create_code'))
{
return call_user_func($user_func);
}
2006-12-02 04:36:16 +00:00
$pref = e107::getPref();
$sql = e107::getDb();
2006-12-02 04:36:16 +00:00
mt_srand ((double)microtime() * 1000000);
$maxran = 1000000;
$rand_num = mt_rand(0, $maxran);
$datekey = date("r");
$rcode = hexdec(md5($_SERVER['HTTP_USER_AGENT'] . serialize($pref). $rand_num . $datekey));
$code = substr($rcode, 2, 6);
$recnum = $this->random_number;
$del_time = time()+1200;
2011-06-30 11:10:34 +00:00
$sql->db_Insert("tmp", "'{$recnum}',{$del_time},'{$code}'");
2006-12-02 04:36:16 +00:00
return $recnum;
}
/* Return TRUE if code is valid, otherwise return FALSE
*
*/
2011-06-30 11:10:34 +00:00
function verify_code($rec_num, $checkstr)
{
if ($user_func = e107::getOverride()->check($this,'verify_code'))
{
return call_user_func($user_func,$rec_num,$checkstr);
}
$sql = e107::getDb();
$tp = e107::getParser();
2011-06-30 11:10:34 +00:00
2006-12-02 04:36:16 +00:00
if ($sql->db_Select("tmp", "tmp_info", "tmp_ip = '".$tp -> toDB($rec_num)."'")) {
$row = $sql->db_Fetch();
$sql->db_Delete("tmp", "tmp_ip = '".$tp -> toDB($rec_num)."'");
2011-06-30 11:10:34 +00:00
//list($code, $path) = explode(",", $row['tmp_info']);
2011-07-10 23:21:09 +00:00
$code = intval($row['tmp_info']);
2006-12-02 04:36:16 +00:00
return ($checkstr == $code);
}
return FALSE;
}
// Return an Error message (true) if check fails, otherwise return false.
function invalidCode($rec_num,$checkstr)
{
if ($user_func = e107::getOverride()->check($this,'invalidCode'))
{
return call_user_func($user_func,$rec_num,$checkstr);
}
if($this->verify_code($rec_num,$checkstr))
{
return false;
}
else
{
return LAN_INVALID_CODE;
}
return true;
}
//XXX Discuss - Add more posibilities for themers? e_CAPTCHA_BGIMAGE, e_CAPTCH_WIDTH, e_CAPTCHA_HEIGHT?
function r_image()
{
if ($user_func = e107::getOverride()->check($this,'r_image'))
{
return call_user_func($user_func);
}
if(defined('e_CAPTCHA_FONTCOLOR'))
{
$color = str_replace("#","", e_CAPTCHA_FONTCOLOR);
}
else
{
$color = 'cccccc';
}
2006-12-02 04:36:16 +00:00
$code = $this->create_code();
return "<img src='".e_HTTP.$this->IMAGES_DIRECTORY."secimg.php?id={$code}&clr={$color}' class='icon secure-image' alt='Missing Code' style='max-width:100%' />";
2006-12-02 04:36:16 +00:00
}
function renderImage() // Alias of r_image
{
return $this->r_image();
}
2011-06-30 11:10:34 +00:00
function hex2rgb($hex)
{
$hex = str_replace("#", "", $hex);
if(strlen($hex) == 3)
{
$r = hexdec(substr($hex,0,1).substr($hex,0,1));
$g = hexdec(substr($hex,1,1).substr($hex,1,1));
$b = hexdec(substr($hex,2,1).substr($hex,2,1));
}
else
{
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
}
$rgb = array($r, $g, $b);
return implode(",", $rgb);
}
2011-06-30 11:10:34 +00:00
function renderInput()
{
if ($user_func = e107::getOverride()->check($this,'renderInput'))
{
return call_user_func($user_func);
}
$frm = e107::getForm();
return $frm->hidden("rand_num", $this->random_number).$frm->text("code_verify", "", 20, array("size"=>20,"title"=> LAN_ENTER_CODE,'required'=>1, 'placeholder'=>LAN_ENTER_CODE));
}
function renderLabel()
{
if ($user_func = e107::getOverride()->check($this,'renderLabel'))
{
return call_user_func($user_func);
}
return LAN_ENTER_CODE;
}
2011-06-26 20:09:45 +00:00
/**
2011-06-30 11:10:34 +00:00
* Render the generated Image. Called without class2 environment (standalone).
2011-06-26 20:09:45 +00:00
*/
function render($qcode, $color='')
2011-06-26 20:09:45 +00:00
{
if($color)
{
$this->FONT_COLOR = $this->hex2rgb($color);
}
// echo "COLOR: ".$this->FONT_COLOR;
require_once($this->BASE_DIR.$this->HANDLERS_DIRECTORY."override_class.php");
$over = new override;
if ($user_func = $over->check($this,'render'))
{
return call_user_func($user_func,$qcode);
}
2011-06-30 11:10:34 +00:00
if(!is_numeric($qcode)){ exit; }
$recnum = preg_replace('#\D#',"",$qcode);
2011-06-26 20:09:45 +00:00
2013-04-26 18:19:18 -07:00
$imgtypes = array('png'=>"png",'gif'=>"gif",'jpg'=>"jpeg",);
2011-06-30 11:10:34 +00:00
@mysql_connect($this->MYSQL_INFO['server'], $this->MYSQL_INFO['user'], $this->MYSQL_INFO['password']) || die('db connection failed');
@mysql_select_db($this->MYSQL_INFO['db']);
$result = mysql_query("SELECT tmp_info FROM {$this->MYSQL_INFO['prefix']}tmp WHERE tmp_ip = '{$recnum}'");
if(!$result || !($row = mysql_fetch_array($result, MYSQL_ASSOC)))
2011-06-26 20:09:45 +00:00
{
2011-11-26 01:45:10 +00:00
// echo "Render Failed";
// echo "SELECT tmp_info FROM {$this->MYSQL_INFO['prefix']}tmp WHERE tmp_ip = '{$recnum}'";
2011-06-26 20:09:45 +00:00
exit;
}
2011-06-30 11:10:34 +00:00
$code = intval($row['tmp_info']); // new value
2011-06-26 20:09:45 +00:00
$type = "none";
2011-06-30 11:10:34 +00:00
2011-06-26 20:09:45 +00:00
foreach($imgtypes as $k=>$t)
{
if(function_exists("imagecreatefrom".$t))
{
$ext = ".".$k;
$type = $t;
break;
}
}
2011-06-30 11:10:34 +00:00
2013-04-26 18:19:18 -07:00
$path = $this->BASE_DIR.$this->IMAGES_DIRECTORY;
$fontpath = $this->BASE_DIR.$this->IMAGES_DIRECTORY;
$secureimg = array();
2011-06-30 11:10:34 +00:00
if(is_readable($path."secure_image_custom.php"))
2011-06-26 20:09:45 +00:00
{
2011-06-30 11:10:34 +00:00
require_once($path."secure_image_custom.php");
/* Example secure_image_custom.php file:
$secureimg['image'] = "code_bg_custom"; // filename excluding the .ext
$secureimg['size'] = "15";
$secureimg['angle'] = "0";
$secureimg['x'] = "6";
$secureimg['y'] = "22";
$secureimg['font'] = "imagecode.ttf";
$secureimg['color'] = "90,90,90"; // red,green,blue
*/
2011-06-26 20:09:45 +00:00
// var_dump($secureimg);
if(isset($secureimg['font']) && !is_readable($path.$secureimg['font']))
{
echo "Font missing"; // for debug only. translation not necessary.
exit;
}
if(!is_readable($path.$secureimg['image'].$ext))
{
echo "Missing Background-Image: ".$secureimg['image'].$ext; // for debug only. translation not necessary.
exit;
}
2011-06-26 20:09:45 +00:00
}
else
{
$fontpath = $this->BASE_DIR.$this->FONTS_DIRECTORY;
$secureimg['image'] = "generic/code_bg";
$secureimg['angle'] = "0";
$secureimg['color'] = $this->FONT_COLOR; // red,green,blue
$secureimg['x'] = "1";
$secureimg['y'] = "21";
2013-04-26 18:19:18 -07:00
$num = rand(1,3);
switch ($num)
{
case 1:
$secureimg['font'] = "chaostimes.ttf";
$secureimg['size'] = "19";
break;
case 2:
$secureimg['font'] = "crazy_style.ttf";
$secureimg['size'] = "18";
break;
case 3:
$secureimg['font'] = "puchakhonmagnifier3.ttf";
$secureimg['size'] = "19";
break;
}
2013-04-26 18:19:18 -07:00
}
if(isset($secureimg['font']) && !is_readable($fontpath.$secureimg['font']))
2013-04-26 18:19:18 -07:00
{
echo "Font missing"; // for debug only. translation not necessary.
exit;
2011-06-26 20:09:45 +00:00
}
if(isset($secureimg['image']) && !is_readable($path.$secureimg['image'].$ext))
2013-04-26 18:19:18 -07:00
{
echo "Missing Background-Image: ".$secureimg['image'].$ext; // for debug only. translation not necessary.
exit;
}
$bg_file = $secureimg['image'];
2013-04-26 18:19:18 -07:00
2011-06-26 20:09:45 +00:00
switch($type)
{
2013-04-26 18:19:18 -07:00
case "png": // preferred
$image = imagecreatefrompng($path.$bg_file.".png");
imagealphablending($image, true);
break;
2011-06-26 20:09:45 +00:00
case "gif":
2013-04-26 18:19:18 -07:00
$image = imagecreatefromgif($path.$bg_file.".gif");
imagealphablending($image, true);
break;
case "jpeg":
$image = imagecreatefromjpeg($path.$bg_file.".jpg");
break;
2011-06-26 20:09:45 +00:00
}
2011-06-30 11:10:34 +00:00
2013-04-26 18:19:18 -07:00
// removing the black from the placeholder
$image = $this->imageCreateTransparent(100,35); //imagecreatetruecolor(100, 35);
2011-06-30 11:10:34 +00:00
2011-06-26 20:09:45 +00:00
if(isset($secureimg['color']))
{
$tmp = explode(",",$secureimg['color']);
2013-04-26 18:19:18 -07:00
$text_color = imagecolorallocate($image,$tmp[0],$tmp[1],$tmp[2]);
2011-06-26 20:09:45 +00:00
}
else
{
2013-04-26 18:19:18 -07:00
$text_color = imagecolorallocate($image, 90, 90, 90);
2011-06-26 20:09:45 +00:00
}
2011-06-26 20:09:45 +00:00
header("Content-type: image/{$type}");
2011-06-30 11:10:34 +00:00
2013-04-26 18:19:18 -07:00
if(isset($secureimg['font']) && is_readable($fontpath.$secureimg['font']))
2011-06-26 20:09:45 +00:00
{
2013-04-26 18:19:18 -07:00
imagettftext($image, $secureimg['size'],$secureimg['angle'], $secureimg['x'], $secureimg['y'], $text_color,$fontpath.$secureimg['font'], $code);
2011-06-26 20:09:45 +00:00
}
else
{
imagestring ($image, 5, 12, 2, $code, $text_color);
}
2011-06-30 11:10:34 +00:00
imagesavealpha($image, true);
2011-06-26 20:09:45 +00:00
switch($type)
{
case "jpeg":
imagejpeg($image);
break;
case "png":
imagepng($image);
break;
case "gif":
imagegif($image);
break;
}
2011-06-30 11:10:34 +00:00
2011-06-26 20:09:45 +00:00
}
2011-06-30 11:10:34 +00:00
function imageCreateTransparent($x, $y)
{
$imageOut = imagecreatetruecolor($x, $y);
$backgroundColor = imagecolorallocatealpha($imageOut, 0, 0, 0, 127);
imagefill($imageOut, 0, 0, $backgroundColor);
return $imageOut;
}
2011-06-30 11:10:34 +00:00
2006-12-02 04:36:16 +00:00
}
2011-06-26 20:09:45 +00:00
?>