00001 <?php
00030 class zCaptcha extends zCore{
00031 var $canvas;
00032 var $baseColor;
00033 var $store = array();
00034 var $setup;
00035 var $fonts;
00036 var $orgStr;
00037 var $controller;
00038
00046 function captcha(&$controller, $len = null){
00047 $this->controller = $controller;
00048 $deff = parse_ini_file(ZIGROOT.DRS.Z_DIR_PROJECTS.DRS.Z_PROJECT_DIR.DRS.ZIGSETTINGSDIR.DRS.'captcha.ini.php', true);
00049 foreach($deff['sys'] as $key => $val){
00050 if(eregi('^on$', $val))$val = true;
00051 if(eregi('^off$', $val))$val = false;
00052 zDef::set($key, $val);
00053 }
00054 if($len == null)$len = zDef::get('Z_CAPTCHA_LENGTH');
00055 if(zDef::get('Z_CAPTCHA_USE_EXCLUSIVE_FONT'))$this->fonts = array_keys($deff['fonts']);
00056
00057 $this->canvas = imagecreate(zDef::get('Z_CAPTCHA_HEIGHT'), zDef::get('Z_CAPTCHA_WIDTH'));
00058 $_tmp_col = explode(',',zDef::get('Z_CAPTCHA_BASE_COLOR'));
00059 $this->baseColor = imagecolorallocate($this->canvas, (int)trim($_tmp_col[0]), (int)trim($_tmp_col[1]), (int)trim($_tmp_col[2]));
00060 if(eregi('on', zDef::get('Z_CAPTCHA_TRANSPARENT'))){
00061 $tmp_trans = imagecolorexact($this->canvas, (int)trim($_tmp_col[0]), (int)trim($_tmp_col[1]), (int)trim($_tmp_col[2]));
00062 imagecolortransparent($this->canvas, $tmp_trans);
00063 }
00064
00065 $this->makeImg($len);
00066 zSession::start();
00067 }
00071 function putText(){
00072 if(count($this->store) > 0){
00073 $next = max($this->store)+zDef::get('Z_CAPTCHA_TEXT_GAP');
00074 }else{
00075 $next = zDef::get('Z_CAPTCHA_TEXT_X_START');
00076 }
00077 $rand = rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_CHAR_RANGE'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_CHAR_RANGE'), 1));
00078 if(zDef::get('Z_CAPTCHA_USE_EXCLUSIVE_FONT')){
00079 $currentFont = ZIGROOT.DRS.Z_DIR_PROJECTS.DRS.Z_PROJECT_DIR.DRS.Z_DIR_FONT.DRS.$this->fonts[rand(0, count($this->fonts)-1)];
00080 imagefttext($this->canvas,
00081 rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_SIZE_RANGE'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_SIZE_RANGE'), 1)),
00082 rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_ANGLE_RANGE'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_ANGLE_RANGE'), 1)),
00083 $next,
00084 rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_Y_RANGE'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_Y_RANGE'), 1)),
00085 imagecolorallocate($this->canvas,
00086 rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_RED'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_RED'), 1)),
00087 rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_GRN'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_GRN'), 1)),
00088 rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_BLU'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_BLU'), 1))
00089 ),
00090 $currentFont,
00091 chr($rand)
00092 );
00093 }else{
00094 imagechar($this->canvas,
00095 zDef::get('Z_CAPTCHA_PLAIN_TEXT_SIZE'),
00096 $next,
00097 rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_Y_RANGE'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_Y_RANGE'), 1)),
00098 chr($rand),
00099 imagecolorallocate($this->canvas,
00100 rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_RED'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_RED'), 1)),
00101 rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_GRN'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_GRN'), 1)),
00102 rand($this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_BLU'), 0), $this->strSep(zDef::get('Z_CAPTCHA_TEXT_COL_RANGE_BLU'), 1))
00103 )
00104 );
00105 }
00106 $this->store[] = $next;
00107 $this->orgStr .= chr($rand);
00108 }
00112 function makeImg($length){
00113 for($i=0;$i<=$length-1;$i++){
00114 $this->putText();
00115 }
00116 }
00125 function output(){
00126 $this->controller->noAutoViewRender();
00127 $this->controller->noLayout();
00128 $this->controller->setContentType('image/png');
00129 imagepng($this->canvas);
00130 imagedestroy($this->canvas);
00131 $_SESSION['gd_str'] = $this->orgStr;
00132 }
00136 function strSep($str, $id = -100){
00137 foreach(explode(',', $str) as $k => $v){
00138 $ret[$k] = trim($v);
00139 }
00140 if($id == -100){
00141 return $ret;
00142 }else{
00143 return $ret[$id];
00144 }
00145 }
00146 }
00148 ?>