Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
    if (!function_exists('file_put_contents')) {
3
        function file_put_contents($filename, $content) {
4
            if (!($file = fopen($filename, 'w'))) {
5
                return false;
6
            }
7
            $n = fwrite($file, $content);
8
            fclose($file);
9
            return $n ? $n : false;
10
        }
11
    }
12
 
13
    // Start PHP session support
14
    session_start();
15
 
16
    $ok = false;
17
 
18
    $msg = 'Please enter the text in the image in the field below!';
19
 
20
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
21
 
22
        if (isset($_POST['phrase']) && is_string($_POST['phrase']) && isset($_SESSION['phrase']) &&
23
            strlen($_POST['phrase']) > 0 && strlen($_SESSION['phrase']) > 0 &&
24
            $_POST['phrase'] == $_SESSION['phrase']) {
25
            $msg = 'OK!';
26
            $ok = true;
27
            unset($_SESSION['phrase']);
28
        } else {
29
            $msg = 'Please try again!';
30
        }
31
 
32
        unlink(sha1(session_id()) . '.png');
33
 
34
    }
35
 
36
    print "<p>$msg</p>";
37
 
38
    if (!$ok) {
39
 
40
        require_once 'Text/CAPTCHA.php';
41
 
42
        // Set CAPTCHA image options (font must exist!)
43
        $imageOptions = array(
44
            'font_size'        => 24,
45
            'font_path'        => './',
46
            'font_file'        => 'COUR.TTF',
47
            'text_color'       => '#DDFF99',
48
            'lines_color'      => '#CCEEDD',
49
            'background_color' => '#555555'
50
        );
51
 
52
        // Set CAPTCHA options
53
        $options = array(
54
            'width' => 200,
55
            'height' => 80,
56
            'output' => 'png',
57
            'imageOptions' => $imageOptions,
58
            'phraseOptions' => array('unpronounceable', 'alphanumeric')
59
        );
60
 
61
        // Generate a new Text_CAPTCHA object, Image driver
62
        $c = Text_CAPTCHA::factory('Image');
63
        $retval = $c->init($options);
64
        if (PEAR::isError($retval)) {
65
            printf('Error initializing CAPTCHA: %s!',
66
                $retval->getMessage());
67
            exit;
68
        }
69
 
70
        // Get CAPTCHA secret passphrase
71
        $_SESSION['phrase'] = $c->getPhrase();
72
 
73
        // Get CAPTCHA image (as PNG)
74
        $png = $c->getCAPTCHA();
75
        if (PEAR::isError($png)) {
76
            printf('Error generating CAPTCHA: %s!',
77
                $png->getMessage());
78
            exit;
79
        }
80
        file_put_contents(sha1(session_id()) . '.png', $png);
81
 
82
        echo '<form method="post">' .
83
             '<img src="' . sha1(session_id()) . '.png?' . time() . '" />' .
84
             '<input type="text" name="phrase" />' .
85
             '<input type="submit" /></form>';
86
    }
87
?>