Chris@76: $memID)); Chris@76: Chris@76: $id_folder = !empty($modSettings['currentAttachmentUploadDir']) ? $modSettings['currentAttachmentUploadDir'] : 1; Chris@76: $avatar_hash = empty($modSettings['custom_avatar_enabled']) ? getAttachmentFilename($destName, false, null, true) : ''; Chris@76: $smcFunc['db_insert']('', Chris@76: '{db_prefix}attachments', Chris@76: array( Chris@76: 'id_member' => 'int', 'attachment_type' => 'int', 'filename' => 'string-255', 'file_hash' => 'string-255', 'fileext' => 'string-8', 'size' => 'int', Chris@76: 'id_folder' => 'int', Chris@76: ), Chris@76: array( Chris@76: $memID, empty($modSettings['custom_avatar_enabled']) ? 0 : 1, $destName, $avatar_hash, $ext, 1, Chris@76: $id_folder, Chris@76: ), Chris@76: array('id_attach') Chris@76: ); Chris@76: $attachID = $smcFunc['db_insert_id']('{db_prefix}attachments', 'id_attach'); Chris@76: // Retain this globally in case the script wants it. Chris@76: $modSettings['new_avatar_data'] = array( Chris@76: 'id' => $attachID, Chris@76: 'filename' => $destName, Chris@76: 'type' => empty($modSettings['custom_avatar_enabled']) ? 0 : 1, Chris@76: ); Chris@76: Chris@76: $destName = (empty($modSettings['custom_avatar_enabled']) ? (is_array($modSettings['attachmentUploadDir']) ? $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']] : $modSettings['attachmentUploadDir']) : $modSettings['custom_avatar_dir']) . '/' . $destName . '.tmp'; Chris@76: Chris@76: // Resize it. Chris@76: if (!empty($modSettings['avatar_download_png'])) Chris@76: $success = resizeImageFile($url, $destName, $max_width, $max_height, 3); Chris@76: else Chris@76: $success = resizeImageFile($url, $destName, $max_width, $max_height); Chris@76: Chris@76: // Remove the .tmp extension. Chris@76: $destName = substr($destName, 0, -4); Chris@76: Chris@76: if ($success) Chris@76: { Chris@76: // Walk the right path. Chris@76: if (!empty($modSettings['currentAttachmentUploadDir'])) Chris@76: { Chris@76: if (!is_array($modSettings['attachmentUploadDir'])) Chris@76: $modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']); Chris@76: $path = $modSettings['attachmentUploadDir'][$modSettings['currentAttachmentUploadDir']]; Chris@76: } Chris@76: else Chris@76: $path = $modSettings['attachmentUploadDir']; Chris@76: Chris@76: // Remove the .tmp extension from the attachment. Chris@76: if (rename($destName . '.tmp', empty($avatar_hash) ? $destName : $path . '/' . $attachID . '_' . $avatar_hash)) Chris@76: { Chris@76: $destName = empty($avatar_hash) ? $destName : $path . '/' . $attachID . '_' . $avatar_hash; Chris@76: list ($width, $height) = getimagesize($destName); Chris@76: $mime_type = 'image/' . $ext; Chris@76: Chris@76: // Write filesize in the database. Chris@76: $smcFunc['db_query']('', ' Chris@76: UPDATE {db_prefix}attachments Chris@76: SET size = {int:filesize}, width = {int:width}, height = {int:height}, Chris@76: mime_type = {string:mime_type} Chris@76: WHERE id_attach = {int:current_attachment}', Chris@76: array( Chris@76: 'filesize' => filesize($destName), Chris@76: 'width' => (int) $width, Chris@76: 'height' => (int) $height, Chris@76: 'current_attachment' => $attachID, Chris@76: 'mime_type' => $mime_type, Chris@76: ) Chris@76: ); Chris@76: return true; Chris@76: } Chris@76: else Chris@76: return false; Chris@76: } Chris@76: else Chris@76: { Chris@76: $smcFunc['db_query']('', ' Chris@76: DELETE FROM {db_prefix}attachments Chris@76: WHERE id_attach = {int:current_attachment}', Chris@76: array( Chris@76: 'current_attachment' => $attachID, Chris@76: ) Chris@76: ); Chris@76: Chris@76: @unlink($destName . '.tmp'); Chris@76: return false; Chris@76: } Chris@76: } Chris@76: Chris@76: function createThumbnail($source, $max_width, $max_height) Chris@76: { Chris@76: global $modSettings; Chris@76: Chris@76: $destName = $source . '_thumb.tmp'; Chris@76: Chris@76: // Do the actual resize. Chris@76: if (!empty($modSettings['attachment_thumb_png'])) Chris@76: $success = resizeImageFile($source, $destName, $max_width, $max_height, 3); Chris@76: else Chris@76: $success = resizeImageFile($source, $destName, $max_width, $max_height); Chris@76: Chris@76: // Okay, we're done with the temporary stuff. Chris@76: $destName = substr($destName, 0, -4); Chris@76: Chris@76: if ($success && @rename($destName . '.tmp', $destName)) Chris@76: return true; Chris@76: else Chris@76: { Chris@76: @unlink($destName . '.tmp'); Chris@76: @touch($destName); Chris@76: return false; Chris@76: } Chris@76: } Chris@76: Chris@76: function reencodeImage($fileName, $preferred_format = 0) Chris@76: { Chris@76: // There is nothing we can do without GD, sorry! Chris@76: if (!checkGD()) Chris@76: return false; Chris@76: Chris@76: if (!resizeImageFile($fileName, $fileName . '.tmp', null, null, $preferred_format)) Chris@76: { Chris@76: if (file_exists($fileName . '.tmp')) Chris@76: unlink($fileName . '.tmp'); Chris@76: Chris@76: return false; Chris@76: } Chris@76: Chris@76: if (!unlink($fileName)) Chris@76: return false; Chris@76: Chris@76: if (!rename($fileName . '.tmp', $fileName)) Chris@76: return false; Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: function checkImageContents($fileName, $extensiveCheck = false) Chris@76: { Chris@76: $fp = fopen($fileName, 'rb'); Chris@76: if (!$fp) Chris@76: fatal_lang_error('attach_timeout'); Chris@76: Chris@76: $prev_chunk = ''; Chris@76: while (!feof($fp)) Chris@76: { Chris@76: $cur_chunk = fread($fp, 8192); Chris@76: Chris@76: // Though not exhaustive lists, better safe than sorry. Chris@76: if (!empty($extensiveCheck)) Chris@76: { Chris@76: // Paranoid check. Some like it that way. Chris@76: if (preg_match('~(iframe|\\<\\?|\\<%|html|eval|body|script\W|[CF]WS[\x01-\x0C])~i', $prev_chunk . $cur_chunk) === 1) Chris@76: { Chris@76: fclose($fp); Chris@76: return false; Chris@76: } Chris@76: } Chris@76: else Chris@76: { Chris@76: // Check for potential infection Chris@76: if (preg_match('~(iframe|html|eval|body|script\W|[CF]WS[\x01-\x0C])~i', $prev_chunk . $cur_chunk) === 1) Chris@76: { Chris@76: fclose($fp); Chris@76: return false; Chris@76: } Chris@76: } Chris@76: $prev_chunk = $cur_chunk; Chris@76: } Chris@76: fclose($fp); Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: function checkGD() Chris@76: { Chris@76: global $gd2; Chris@76: Chris@76: // Check to see if GD is installed and what version. Chris@76: if (($extensionFunctions = get_extension_funcs('gd')) === false) Chris@76: return false; Chris@76: Chris@76: // Also determine if GD2 is installed and store it in a global. Chris@76: $gd2 = in_array('imagecreatetruecolor', $extensionFunctions) && function_exists('imagecreatetruecolor'); Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: function resizeImageFile($source, $destination, $max_width, $max_height, $preferred_format = 0) Chris@76: { Chris@76: global $sourcedir; Chris@76: Chris@76: // Nothing to do without GD Chris@76: if (!checkGD()) Chris@76: return false; Chris@76: Chris@76: static $default_formats = array( Chris@76: '1' => 'gif', Chris@76: '2' => 'jpeg', Chris@76: '3' => 'png', Chris@76: '6' => 'bmp', Chris@76: '15' => 'wbmp' Chris@76: ); Chris@76: Chris@76: require_once($sourcedir . '/Subs-Package.php'); Chris@76: @ini_set('memory_limit', '90M'); Chris@76: Chris@76: $success = false; Chris@76: Chris@76: // Get the image file, we have to work with something after all Chris@76: $fp_destination = fopen($destination, 'wb'); Chris@76: if ($fp_destination && substr($source, 0, 7) == 'http://') Chris@76: { Chris@76: $fileContents = fetch_web_data($source); Chris@76: Chris@76: fwrite($fp_destination, $fileContents); Chris@76: fclose($fp_destination); Chris@76: Chris@76: $sizes = @getimagesize($destination); Chris@76: } Chris@76: elseif ($fp_destination) Chris@76: { Chris@76: $sizes = @getimagesize($source); Chris@76: Chris@76: $fp_source = fopen($source, 'rb'); Chris@76: if ($fp_source !== false) Chris@76: { Chris@76: while (!feof($fp_source)) Chris@76: fwrite($fp_destination, fread($fp_source, 8192)); Chris@76: fclose($fp_source); Chris@76: } Chris@76: else Chris@76: $sizes = array(-1, -1, -1); Chris@76: fclose($fp_destination); Chris@76: } Chris@76: // We can't get to the file. Chris@76: else Chris@76: $sizes = array(-1, -1, -1); Chris@76: Chris@76: // Gif? That might mean trouble if gif support is not available. Chris@76: if ($sizes[2] == 1 && !function_exists('imagecreatefromgif') && function_exists('imagecreatefrompng')) Chris@76: { Chris@76: // Download it to the temporary file... use the special gif library... and save as png. Chris@76: if ($img = @gif_loadFile($destination) && gif_outputAsPng($img, $destination)) Chris@76: $sizes[2] = 3; Chris@76: } Chris@76: Chris@76: // A known and supported format? Chris@76: if (isset($default_formats[$sizes[2]]) && function_exists('imagecreatefrom' . $default_formats[$sizes[2]])) Chris@76: { Chris@76: $imagecreatefrom = 'imagecreatefrom' . $default_formats[$sizes[2]]; Chris@76: if ($src_img = @$imagecreatefrom($destination)) Chris@76: { Chris@76: resizeImage($src_img, $destination, imagesx($src_img), imagesy($src_img), $max_width === null ? imagesx($src_img) : $max_width, $max_height === null ? imagesy($src_img) : $max_height, true, $preferred_format); Chris@76: $success = true; Chris@76: } Chris@76: } Chris@76: Chris@76: return $success; Chris@76: } Chris@76: Chris@76: function resizeImage($src_img, $destName, $src_width, $src_height, $max_width, $max_height, $force_resize = false, $preferred_format = 0) Chris@76: { Chris@76: global $gd2, $modSettings; Chris@76: Chris@76: // Without GD, no image resizing at all. Chris@76: if (!checkGD()) Chris@76: return false; Chris@76: Chris@76: $success = false; Chris@76: Chris@76: // Determine whether to resize to max width or to max height (depending on the limits.) Chris@76: if (!empty($max_width) || !empty($max_height)) Chris@76: { Chris@76: if (!empty($max_width) && (empty($max_height) || $src_height * $max_width / $src_width <= $max_height)) Chris@76: { Chris@76: $dst_width = $max_width; Chris@76: $dst_height = floor($src_height * $max_width / $src_width); Chris@76: } Chris@76: elseif (!empty($max_height)) Chris@76: { Chris@76: $dst_width = floor($src_width * $max_height / $src_height); Chris@76: $dst_height = $max_height; Chris@76: } Chris@76: Chris@76: // Don't bother resizing if it's already smaller... Chris@76: if (!empty($dst_width) && !empty($dst_height) && ($dst_width < $src_width || $dst_height < $src_height || $force_resize)) Chris@76: { Chris@76: // (make a true color image, because it just looks better for resizing.) Chris@76: if ($gd2) Chris@76: { Chris@76: $dst_img = imagecreatetruecolor($dst_width, $dst_height); Chris@76: Chris@76: // Deal nicely with a PNG - because we can. Chris@76: if ((!empty($preferred_format)) && ($preferred_format == 3)) Chris@76: { Chris@76: imagealphablending($dst_img, false); Chris@76: if (function_exists('imagesavealpha')) Chris@76: imagesavealpha($dst_img, true); Chris@76: } Chris@76: } Chris@76: else Chris@76: $dst_img = imagecreate($dst_width, $dst_height); Chris@76: Chris@76: // Resize it! Chris@76: if ($gd2) Chris@76: imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); Chris@76: else Chris@76: imagecopyresamplebicubic($dst_img, $src_img, 0, 0, 0, 0, $dst_width, $dst_height, $src_width, $src_height); Chris@76: } Chris@76: else Chris@76: $dst_img = $src_img; Chris@76: } Chris@76: else Chris@76: $dst_img = $src_img; Chris@76: Chris@76: // Save the image as ... Chris@76: if (!empty($preferred_format) && ($preferred_format == 3) && function_exists('imagepng')) Chris@76: $success = imagepng($dst_img, $destName); Chris@76: elseif (!empty($preferred_format) && ($preferred_format == 1) && function_exists('imagegif')) Chris@76: $success = imagegif($dst_img, $destName); Chris@76: elseif (function_exists('imagejpeg')) Chris@76: $success = imagejpeg($dst_img, $destName); Chris@76: Chris@76: // Free the memory. Chris@76: imagedestroy($src_img); Chris@76: if ($dst_img != $src_img) Chris@76: imagedestroy($dst_img); Chris@76: Chris@76: return $success; Chris@76: } Chris@76: Chris@76: function imagecopyresamplebicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) Chris@76: { Chris@76: $palsize = imagecolorstotal($src_img); Chris@76: for ($i = 0; $i < $palsize; $i++) Chris@76: { Chris@76: $colors = imagecolorsforindex($src_img, $i); Chris@76: imagecolorallocate($dst_img, $colors['red'], $colors['green'], $colors['blue']); Chris@76: } Chris@76: Chris@76: $scaleX = ($src_w - 1) / $dst_w; Chris@76: $scaleY = ($src_h - 1) / $dst_h; Chris@76: Chris@76: $scaleX2 = (int) $scaleX / 2; Chris@76: $scaleY2 = (int) $scaleY / 2; Chris@76: Chris@76: for ($j = $src_y; $j < $dst_h; $j++) Chris@76: { Chris@76: $sY = (int) $j * $scaleY; Chris@76: $y13 = $sY + $scaleY2; Chris@76: Chris@76: for ($i = $src_x; $i < $dst_w; $i++) Chris@76: { Chris@76: $sX = (int) $i * $scaleX; Chris@76: $x34 = $sX + $scaleX2; Chris@76: Chris@76: $color1 = imagecolorsforindex($src_img, imagecolorat($src_img, $sX, $y13)); Chris@76: $color2 = imagecolorsforindex($src_img, imagecolorat($src_img, $sX, $sY)); Chris@76: $color3 = imagecolorsforindex($src_img, imagecolorat($src_img, $x34, $y13)); Chris@76: $color4 = imagecolorsforindex($src_img, imagecolorat($src_img, $x34, $sY)); Chris@76: Chris@76: $red = ($color1['red'] + $color2['red'] + $color3['red'] + $color4['red']) / 4; Chris@76: $green = ($color1['green'] + $color2['green'] + $color3['green'] + $color4['green']) / 4; Chris@76: $blue = ($color1['blue'] + $color2['blue'] + $color3['blue'] + $color4['blue']) / 4; Chris@76: Chris@76: $color = imagecolorresolve($dst_img, $red, $green, $blue); Chris@76: if ($color == -1) Chris@76: { Chris@76: if ($palsize++ < 256) Chris@76: imagecolorallocate($dst_img, $red, $green, $blue); Chris@76: $color = imagecolorclosest($dst_img, $red, $green, $blue); Chris@76: } Chris@76: Chris@76: imagesetpixel($dst_img, $i + $dst_x - $src_x, $j + $dst_y - $src_y, $color); Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@76: if (!function_exists('imagecreatefrombmp')) Chris@76: { Chris@76: function imagecreatefrombmp($filename) Chris@76: { Chris@76: global $gd2; Chris@76: Chris@76: $fp = fopen($filename, 'rb'); Chris@76: Chris@76: $errors = error_reporting(0); Chris@76: Chris@76: $header = unpack('vtype/Vsize/Vreserved/Voffset', fread($fp, 14)); Chris@76: $info = unpack('Vsize/Vwidth/Vheight/vplanes/vbits/Vcompression/Vimagesize/Vxres/Vyres/Vncolor/Vcolorimportant', fread($fp, 40)); Chris@76: Chris@76: if ($header['type'] != 0x4D42) Chris@76: false; Chris@76: Chris@76: if ($gd2) Chris@76: $dst_img = imagecreatetruecolor($info['width'], $info['height']); Chris@76: else Chris@76: $dst_img = imagecreate($info['width'], $info['height']); Chris@76: Chris@76: $palette_size = $header['offset'] - 54; Chris@76: $info['ncolor'] = $palette_size / 4; Chris@76: Chris@76: $palette = array(); Chris@76: Chris@76: $palettedata = fread($fp, $palette_size); Chris@76: $n = 0; Chris@76: for ($j = 0; $j < $palette_size; $j++) Chris@76: { Chris@76: $b = ord($palettedata{$j++}); Chris@76: $g = ord($palettedata{$j++}); Chris@76: $r = ord($palettedata{$j++}); Chris@76: Chris@76: $palette[$n++] = imagecolorallocate($dst_img, $r, $g, $b); Chris@76: } Chris@76: Chris@76: $scan_line_size = ($info['bits'] * $info['width'] + 7) >> 3; Chris@76: $scan_line_align = $scan_line_size & 3 ? 4 - ($scan_line_size & 3) : 0; Chris@76: Chris@76: for ($y = 0, $l = $info['height'] - 1; $y < $info['height']; $y++, $l--) Chris@76: { Chris@76: fseek($fp, $header['offset'] + ($scan_line_size + $scan_line_align) * $l); Chris@76: $scan_line = fread($fp, $scan_line_size); Chris@76: Chris@76: if (strlen($scan_line) < $scan_line_size) Chris@76: continue; Chris@76: Chris@76: if ($info['bits'] == 32) Chris@76: { Chris@76: $x = 0; Chris@76: for ($j = 0; $j < $scan_line_size; $x++) Chris@76: { Chris@76: $b = ord($scan_line{$j++}); Chris@76: $g = ord($scan_line{$j++}); Chris@76: $r = ord($scan_line{$j++}); Chris@76: $j++; Chris@76: Chris@76: $color = imagecolorexact($dst_img, $r, $g, $b); Chris@76: if ($color == -1) Chris@76: { Chris@76: $color = imagecolorallocate($dst_img, $r, $g, $b); Chris@76: Chris@76: // Gah! Out of colors? Stupid GD 1... try anyhow. Chris@76: if ($color == -1) Chris@76: $color = imagecolorclosest($dst_img, $r, $g, $b); Chris@76: } Chris@76: Chris@76: imagesetpixel($dst_img, $x, $y, $color); Chris@76: } Chris@76: } Chris@76: elseif ($info['bits'] == 24) Chris@76: { Chris@76: $x = 0; Chris@76: for ($j = 0; $j < $scan_line_size; $x++) Chris@76: { Chris@76: $b = ord($scan_line{$j++}); Chris@76: $g = ord($scan_line{$j++}); Chris@76: $r = ord($scan_line{$j++}); Chris@76: Chris@76: $color = imagecolorexact($dst_img, $r, $g, $b); Chris@76: if ($color == -1) Chris@76: { Chris@76: $color = imagecolorallocate($dst_img, $r, $g, $b); Chris@76: Chris@76: // Gah! Out of colors? Stupid GD 1... try anyhow. Chris@76: if ($color == -1) Chris@76: $color = imagecolorclosest($dst_img, $r, $g, $b); Chris@76: } Chris@76: Chris@76: imagesetpixel($dst_img, $x, $y, $color); Chris@76: } Chris@76: } Chris@76: elseif ($info['bits'] == 16) Chris@76: { Chris@76: $x = 0; Chris@76: for ($j = 0; $j < $scan_line_size; $x++) Chris@76: { Chris@76: $b1 = ord($scan_line{$j++}); Chris@76: $b2 = ord($scan_line{$j++}); Chris@76: Chris@76: $word = $b2 * 256 + $b1; Chris@76: Chris@76: $b = (($word & 31) * 255) / 31; Chris@76: $g = ((($word >> 5) & 31) * 255) / 31; Chris@76: $r = ((($word >> 10) & 31) * 255) / 31; Chris@76: Chris@76: // Scale the image colors up properly. Chris@76: $color = imagecolorexact($dst_img, $r, $g, $b); Chris@76: if ($color == -1) Chris@76: { Chris@76: $color = imagecolorallocate($dst_img, $r, $g, $b); Chris@76: Chris@76: // Gah! Out of colors? Stupid GD 1... try anyhow. Chris@76: if ($color == -1) Chris@76: $color = imagecolorclosest($dst_img, $r, $g, $b); Chris@76: } Chris@76: Chris@76: imagesetpixel($dst_img, $x, $y, $color); Chris@76: } Chris@76: } Chris@76: elseif ($info['bits'] == 8) Chris@76: { Chris@76: $x = 0; Chris@76: for ($j = 0; $j < $scan_line_size; $x++) Chris@76: imagesetpixel($dst_img, $x, $y, $palette[ord($scan_line{$j++})]); Chris@76: } Chris@76: elseif ($info['bits'] == 4) Chris@76: { Chris@76: $x = 0; Chris@76: for ($j = 0; $j < $scan_line_size; $x++) Chris@76: { Chris@76: $byte = ord($scan_line{$j++}); Chris@76: Chris@76: imagesetpixel($dst_img, $x, $y, $palette[(int) ($byte / 16)]); Chris@76: if (++$x < $info['width']) Chris@76: imagesetpixel($dst_img, $x, $y, $palette[$byte & 15]); Chris@76: } Chris@76: } Chris@76: else Chris@76: { Chris@76: // Sorry, I'm just not going to do monochrome :P. Chris@76: } Chris@76: } Chris@76: Chris@76: fclose($fp); Chris@76: Chris@76: error_reporting($errors); Chris@76: Chris@76: return $dst_img; Chris@76: } Chris@76: } Chris@76: Chris@76: function gif_loadFile($lpszFileName, $iIndex = 0) Chris@76: { Chris@76: // The classes needed are in this file. Chris@76: loadClassFile('Class-Graphics.php'); Chris@76: $gif = new gif_file(); Chris@76: Chris@76: if (!$gif->loadFile($lpszFileName, $iIndex)) Chris@76: return false; Chris@76: Chris@76: return $gif; Chris@76: } Chris@76: Chris@76: function gif_outputAsPng($gif, $lpszFileName, $background_color = -1) Chris@76: { Chris@76: if (!isset($gif) || @get_class($gif) != 'cgif' || !$gif->loaded || $lpszFileName == '') Chris@76: return false; Chris@76: Chris@76: $fd = $gif->get_png_data($background_color); Chris@76: if (strlen($fd) <= 0) Chris@76: return false; Chris@76: Chris@76: if (!($fh = @fopen($lpszFileName, 'wb'))) Chris@76: return false; Chris@76: Chris@76: @fwrite($fh, $fd, strlen($fd)); Chris@76: @fflush($fh); Chris@76: @fclose($fh); Chris@76: Chris@76: return true; Chris@76: } Chris@76: Chris@76: // Create the image for the visual verification code. Chris@76: function showCodeImage($code) Chris@76: { Chris@76: global $settings, $user_info, $modSettings; Chris@76: Chris@76: /* Chris@76: Note: The higher the value of visual_verification_type the harder the verification is - from 0 as disabled through to 4 as "Very hard". Chris@76: */ Chris@76: Chris@76: // What type are we going to be doing? Chris@76: $imageType = $modSettings['visual_verification_type']; Chris@76: // Special case to allow the admin center to show samples. Chris@76: if ($user_info['is_admin'] && isset($_GET['type'])) Chris@76: $imageType = (int) $_GET['type']; Chris@76: Chris@76: // Some quick references for what we do. Chris@76: // Do we show no, low or high noise? Chris@76: $noiseType = $imageType == 3 ? 'low' : ($imageType == 4 ? 'high' : ($imageType == 5 ? 'extreme' : 'none')); Chris@76: // Can we have more than one font in use? Chris@76: $varyFonts = $imageType > 3 ? true : false; Chris@76: // Just a plain white background? Chris@76: $simpleBGColor = $imageType < 3 ? true : false; Chris@76: // Plain black foreground? Chris@76: $simpleFGColor = $imageType == 0 ? true : false; Chris@76: // High much to rotate each character. Chris@76: $rotationType = $imageType == 1 ? 'none' : ($imageType > 3 ? 'low' : 'high'); Chris@76: // Do we show some characters inversed? Chris@76: $showReverseChars = $imageType > 3 ? true : false; Chris@76: // Special case for not showing any characters. Chris@76: $disableChars = $imageType == 0 ? true : false; Chris@76: // What do we do with the font colors. Are they one color, close to one color or random? Chris@76: $fontColorType = $imageType == 1 ? 'plain' : ($imageType > 3 ? 'random' : 'cyclic'); Chris@76: // Are the fonts random sizes? Chris@76: $fontSizeRandom = $imageType > 3 ? true : false; Chris@76: // How much space between characters? Chris@76: $fontHorSpace = $imageType > 3 ? 'high' : ($imageType == 1 ? 'medium' : 'minus'); Chris@76: // Where do characters sit on the image? (Fixed position or random/very random) Chris@76: $fontVerPos = $imageType == 1 ? 'fixed' : ($imageType > 3 ? 'vrandom' : 'random'); Chris@76: // Make font semi-transparent? Chris@76: $fontTrans = $imageType == 2 || $imageType == 3 ? true : false; Chris@76: // Give the image a border? Chris@76: $hasBorder = $simpleBGColor; Chris@76: Chris@76: // Is this GD2? Needed for pixel size. Chris@76: $testGD = get_extension_funcs('gd'); Chris@76: $gd2 = in_array('imagecreatetruecolor', $testGD) && function_exists('imagecreatetruecolor'); Chris@76: unset($testGD); Chris@76: Chris@76: // The amount of pixels inbetween characters. Chris@76: $character_spacing = 1; Chris@76: Chris@76: // What color is the background - generally white unless we're on "hard". Chris@76: if ($simpleBGColor) Chris@76: $background_color = array(255, 255, 255); Chris@76: else Chris@76: $background_color = isset($settings['verification_background']) ? $settings['verification_background'] : array(236, 237, 243); Chris@76: Chris@76: // The color of the characters shown (red, green, blue). Chris@76: if ($simpleFGColor) Chris@76: $foreground_color = array(0, 0, 0); Chris@76: else Chris@76: { Chris@76: $foreground_color = array(64, 101, 136); Chris@76: Chris@76: // Has the theme author requested a custom color? Chris@76: if (isset($settings['verification_foreground'])) Chris@76: $foreground_color = $settings['verification_foreground']; Chris@76: } Chris@76: Chris@76: if (!is_dir($settings['default_theme_dir'] . '/fonts')) Chris@76: return false; Chris@76: Chris@76: // Get a list of the available fonts. Chris@76: $font_dir = dir($settings['default_theme_dir'] . '/fonts'); Chris@76: $font_list = array(); Chris@76: $ttfont_list = array(); Chris@76: while ($entry = $font_dir->read()) Chris@76: { Chris@76: if (preg_match('~^(.+)\.gdf$~', $entry, $matches) === 1) Chris@76: $font_list[] = $entry; Chris@76: elseif (preg_match('~^(.+)\.ttf$~', $entry, $matches) === 1) Chris@76: $ttfont_list[] = $entry; Chris@76: } Chris@76: Chris@76: if (empty($font_list)) Chris@76: return false; Chris@76: Chris@76: // For non-hard things don't even change fonts. Chris@76: if (!$varyFonts) Chris@76: { Chris@76: $font_list = array($font_list[0]); Chris@76: // Try use Screenge if we can - it looks good! Chris@76: if (in_array('Screenge.ttf', $ttfont_list)) Chris@76: $ttfont_list = array('Screenge.ttf'); Chris@76: else Chris@76: $ttfont_list = empty($ttfont_list) ? array() : array($ttfont_list[0]); Chris@76: Chris@76: } Chris@76: Chris@76: // Create a list of characters to be shown. Chris@76: $characters = array(); Chris@76: $loaded_fonts = array(); Chris@76: for ($i = 0; $i < strlen($code); $i++) Chris@76: { Chris@76: $characters[$i] = array( Chris@76: 'id' => $code{$i}, Chris@76: 'font' => array_rand($font_list), Chris@76: ); Chris@76: Chris@76: $loaded_fonts[$characters[$i]['font']] = null; Chris@76: } Chris@76: Chris@76: // Load all fonts and determine the maximum font height. Chris@76: foreach ($loaded_fonts as $font_index => $dummy) Chris@76: $loaded_fonts[$font_index] = imageloadfont($settings['default_theme_dir'] . '/fonts/' . $font_list[$font_index]); Chris@76: Chris@76: // Determine the dimensions of each character. Chris@76: $total_width = $character_spacing * strlen($code) + 20; Chris@76: $max_height = 0; Chris@76: foreach ($characters as $char_index => $character) Chris@76: { Chris@76: $characters[$char_index]['width'] = imagefontwidth($loaded_fonts[$character['font']]); Chris@76: $characters[$char_index]['height'] = imagefontheight($loaded_fonts[$character['font']]); Chris@76: Chris@76: $max_height = max($characters[$char_index]['height'] + 5, $max_height); Chris@76: $total_width += $characters[$char_index]['width']; Chris@76: } Chris@76: Chris@76: // Create an image. Chris@76: $code_image = $gd2 ? imagecreatetruecolor($total_width, $max_height) : imagecreate($total_width, $max_height); Chris@76: Chris@76: // Draw the background. Chris@76: $bg_color = imagecolorallocate($code_image, $background_color[0], $background_color[1], $background_color[2]); Chris@76: imagefilledrectangle($code_image, 0, 0, $total_width - 1, $max_height - 1, $bg_color); Chris@76: Chris@76: // Randomize the foreground color a little. Chris@76: for ($i = 0; $i < 3; $i++) Chris@76: $foreground_color[$i] = mt_rand(max($foreground_color[$i] - 3, 0), min($foreground_color[$i] + 3, 255)); Chris@76: $fg_color = imagecolorallocate($code_image, $foreground_color[0], $foreground_color[1], $foreground_color[2]); Chris@76: Chris@76: // Color for the dots. Chris@76: for ($i = 0; $i < 3; $i++) Chris@76: $dotbgcolor[$i] = $background_color[$i] < $foreground_color[$i] ? mt_rand(0, max($foreground_color[$i] - 20, 0)) : mt_rand(min($foreground_color[$i] + 20, 255), 255); Chris@76: $randomness_color = imagecolorallocate($code_image, $dotbgcolor[0], $dotbgcolor[1], $dotbgcolor[2]); Chris@76: Chris@76: // Some squares/rectanges for new extreme level Chris@76: if ($noiseType == 'extreme') Chris@76: { Chris@76: for ($i = 0; $i < rand(1, 5); $i++) Chris@76: { Chris@76: $x1 = rand(0, $total_width / 4); Chris@76: $x2 = $x1 + round(rand($total_width / 4, $total_width)); Chris@76: $y1 = rand(0, $max_height); Chris@76: $y2 = $y1 + round(rand(0, $max_height / 3)); Chris@76: imagefilledrectangle($code_image, $x1, $y1, $x2, $y2, mt_rand(0, 1) ? $fg_color : $randomness_color); Chris@76: } Chris@76: } Chris@76: Chris@76: // Fill in the characters. Chris@76: if (!$disableChars) Chris@76: { Chris@76: $cur_x = 0; Chris@76: foreach ($characters as $char_index => $character) Chris@76: { Chris@76: // Can we use true type fonts? Chris@76: $can_do_ttf = function_exists('imagettftext'); Chris@76: Chris@76: // How much rotation will we give? Chris@76: if ($rotationType == 'none') Chris@76: $angle = 0; Chris@76: else Chris@76: $angle = mt_rand(-100, 100) / ($rotationType == 'high' ? 6 : 10); Chris@76: Chris@76: // What color shall we do it? Chris@76: if ($fontColorType == 'cyclic') Chris@76: { Chris@76: // Here we'll pick from a set of acceptance types. Chris@76: $colors = array( Chris@76: array(10, 120, 95), Chris@76: array(46, 81, 29), Chris@76: array(4, 22, 154), Chris@76: array(131, 9, 130), Chris@76: array(0, 0, 0), Chris@76: array(143, 39, 31), Chris@76: ); Chris@76: if (!isset($last_index)) Chris@76: $last_index = -1; Chris@76: $new_index = $last_index; Chris@76: while ($last_index == $new_index) Chris@76: $new_index = mt_rand(0, count($colors) - 1); Chris@76: $char_fg_color = $colors[$new_index]; Chris@76: $last_index = $new_index; Chris@76: } Chris@76: elseif ($fontColorType == 'random') Chris@76: $char_fg_color = array(mt_rand(max($foreground_color[0] - 2, 0), $foreground_color[0]), mt_rand(max($foreground_color[1] - 2, 0), $foreground_color[1]), mt_rand(max($foreground_color[2] - 2, 0), $foreground_color[2])); Chris@76: else Chris@76: $char_fg_color = array($foreground_color[0], $foreground_color[1], $foreground_color[2]); Chris@76: Chris@76: if (!empty($can_do_ttf)) Chris@76: { Chris@76: // GD2 handles font size differently. Chris@76: if ($fontSizeRandom) Chris@76: $font_size = $gd2 ? mt_rand(17, 19) : mt_rand(18, 25); Chris@76: else Chris@76: $font_size = $gd2 ? 18 : 24; Chris@76: Chris@76: // Work out the sizes - also fix the character width cause TTF not quite so wide! Chris@76: $font_x = $fontHorSpace == 'minus' && $cur_x > 0 ? $cur_x - 3 : $cur_x + 5; Chris@76: $font_y = $max_height - ($fontVerPos == 'vrandom' ? mt_rand(2, 8) : ($fontVerPos == 'random' ? mt_rand(3, 5) : 5)); Chris@76: Chris@76: // What font face? Chris@76: if (!empty($ttfont_list)) Chris@76: $fontface = $settings['default_theme_dir'] . '/fonts/' . $ttfont_list[mt_rand(0, count($ttfont_list) - 1)]; Chris@76: Chris@76: // What color are we to do it in? Chris@76: $is_reverse = $showReverseChars ? mt_rand(0, 1) : false; Chris@76: $char_color = function_exists('imagecolorallocatealpha') && $fontTrans ? imagecolorallocatealpha($code_image, $char_fg_color[0], $char_fg_color[1], $char_fg_color[2], 50) : imagecolorallocate($code_image, $char_fg_color[0], $char_fg_color[1], $char_fg_color[2]); Chris@76: Chris@76: $fontcord = @imagettftext($code_image, $font_size, $angle, $font_x, $font_y, $char_color, $fontface, $character['id']); Chris@76: if (empty($fontcord)) Chris@76: $can_do_ttf = false; Chris@76: elseif ($is_reverse) Chris@76: { Chris@76: imagefilledpolygon($code_image, $fontcord, 4, $fg_color); Chris@76: // Put the character back! Chris@76: imagettftext($code_image, $font_size, $angle, $font_x, $font_y, $randomness_color, $fontface, $character['id']); Chris@76: } Chris@76: Chris@76: if ($can_do_ttf) Chris@76: $cur_x = max($fontcord[2], $fontcord[4]) + ($angle == 0 ? 0 : 3); Chris@76: } Chris@76: Chris@76: if (!$can_do_ttf) Chris@76: { Chris@76: // Rotating the characters a little... Chris@76: if (function_exists('imagerotate')) Chris@76: { Chris@76: $char_image = $gd2 ? imagecreatetruecolor($character['width'], $character['height']) : imagecreate($character['width'], $character['height']); Chris@76: $char_bgcolor = imagecolorallocate($char_image, $background_color[0], $background_color[1], $background_color[2]); Chris@76: imagefilledrectangle($char_image, 0, 0, $character['width'] - 1, $character['height'] - 1, $char_bgcolor); Chris@76: imagechar($char_image, $loaded_fonts[$character['font']], 0, 0, $character['id'], imagecolorallocate($char_image, $char_fg_color[0], $char_fg_color[1], $char_fg_color[2])); Chris@76: $rotated_char = imagerotate($char_image, mt_rand(-100, 100) / 10, $char_bgcolor); Chris@76: imagecopy($code_image, $rotated_char, $cur_x, 0, 0, 0, $character['width'], $character['height']); Chris@76: imagedestroy($rotated_char); Chris@76: imagedestroy($char_image); Chris@76: } Chris@76: Chris@76: // Sorry, no rotation available. Chris@76: else Chris@76: imagechar($code_image, $loaded_fonts[$character['font']], $cur_x, floor(($max_height - $character['height']) / 2), $character['id'], imagecolorallocate($code_image, $char_fg_color[0], $char_fg_color[1], $char_fg_color[2])); Chris@76: $cur_x += $character['width'] + $character_spacing; Chris@76: } Chris@76: } Chris@76: } Chris@76: // If disabled just show a cross. Chris@76: else Chris@76: { Chris@76: imageline($code_image, 0, 0, $total_width, $max_height, $fg_color); Chris@76: imageline($code_image, 0, $max_height, $total_width, 0, $fg_color); Chris@76: } Chris@76: Chris@76: // Make the background color transparent on the hard image. Chris@76: if (!$simpleBGColor) Chris@76: imagecolortransparent($code_image, $bg_color); Chris@76: if ($hasBorder) Chris@76: imagerectangle($code_image, 0, 0, $total_width - 1, $max_height - 1, $fg_color); Chris@76: Chris@76: // Add some noise to the background? Chris@76: if ($noiseType != 'none') Chris@76: { Chris@76: for ($i = mt_rand(0, 2); $i < $max_height; $i += mt_rand(1, 2)) Chris@76: for ($j = mt_rand(0, 10); $j < $total_width; $j += mt_rand(1, 10)) Chris@76: imagesetpixel($code_image, $j, $i, mt_rand(0, 1) ? $fg_color : $randomness_color); Chris@76: Chris@76: // Put in some lines too? Chris@76: if ($noiseType != 'extreme') Chris@76: { Chris@76: $num_lines = $noiseType == 'high' ? mt_rand(3, 7) : mt_rand(2, 5); Chris@76: for ($i = 0; $i < $num_lines; $i++) Chris@76: { Chris@76: if (mt_rand(0, 1)) Chris@76: { Chris@76: $x1 = mt_rand(0, $total_width); Chris@76: $x2 = mt_rand(0, $total_width); Chris@76: $y1 = 0; $y2 = $max_height; Chris@76: } Chris@76: else Chris@76: { Chris@76: $y1 = mt_rand(0, $max_height); Chris@76: $y2 = mt_rand(0, $max_height); Chris@76: $x1 = 0; $x2 = $total_width; Chris@76: } Chris@76: imagesetthickness($code_image, mt_rand(1, 2)); Chris@76: imageline($code_image, $x1, $y1, $x2, $y2, mt_rand(0, 1) ? $fg_color : $randomness_color); Chris@76: } Chris@76: } Chris@76: else Chris@76: { Chris@76: // Put in some ellipse Chris@76: $num_ellipse = $noiseType == 'extreme' ? mt_rand(6, 12) : mt_rand(2, 6); Chris@76: for ($i = 0; $i < $num_ellipse; $i++) Chris@76: { Chris@76: $x1 = round(rand(($total_width / 4) * -1, $total_width + ($total_width / 4))); Chris@76: $x2 = round(rand($total_width / 2, 2 * $total_width)); Chris@76: $y1 = round(rand(($max_height / 4) * -1, $max_height + ($max_height / 4))); Chris@76: $y2 = round(rand($max_height / 2, 2 * $max_height)); Chris@76: imageellipse($code_image, $x1, $y1, $x2, $y2, mt_rand(0, 1) ? $fg_color : $randomness_color); Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@76: // Show the image. Chris@76: if (function_exists('imagegif')) Chris@76: { Chris@76: header('Content-type: image/gif'); Chris@76: imagegif($code_image); Chris@76: } Chris@76: else Chris@76: { Chris@76: header('Content-type: image/png'); Chris@76: imagepng($code_image); Chris@76: } Chris@76: Chris@76: // Bail out. Chris@76: imagedestroy($code_image); Chris@76: die(); Chris@76: } Chris@76: Chris@76: // Create a letter for the visual verification code. Chris@76: function showLetterImage($letter) Chris@76: { Chris@76: global $settings; Chris@76: Chris@76: if (!is_dir($settings['default_theme_dir'] . '/fonts')) Chris@76: return false; Chris@76: Chris@76: // Get a list of the available font directories. Chris@76: $font_dir = dir($settings['default_theme_dir'] . '/fonts'); Chris@76: $font_list = array(); Chris@76: while ($entry = $font_dir->read()) Chris@76: if ($entry[0] !== '.' && is_dir($settings['default_theme_dir'] . '/fonts/' . $entry) && file_exists($settings['default_theme_dir'] . '/fonts/' . $entry . '.gdf')) Chris@76: $font_list[] = $entry; Chris@76: Chris@76: if (empty($font_list)) Chris@76: return false; Chris@76: Chris@76: // Pick a random font. Chris@76: $random_font = $font_list[array_rand($font_list)]; Chris@76: Chris@76: // Check if the given letter exists. Chris@76: if (!file_exists($settings['default_theme_dir'] . '/fonts/' . $random_font . '/' . $letter . '.gif')) Chris@76: return false; Chris@76: Chris@76: // Include it! Chris@76: header('Content-type: image/gif'); Chris@76: include($settings['default_theme_dir'] . '/fonts/' . $random_font . '/' . $letter . '.gif'); Chris@76: Chris@76: // Nothing more to come. Chris@76: die(); Chris@76: } Chris@76: Chris@76: ?>