protected function readCodeFromFile($numWords = 1)
    {
        $strtolower_func = 'strtolower';
        $mb_support      = false;

        if (!empty($this->wordlist_file_encoding)) {
            if (!extension_loaded('mbstring')) {
                trigger_error("wordlist_file_encoding option set, but PHP does not have mbstring support", E_USER_WARNING);
                return false;
            }

            // emits PHP warning if not supported
            $mb_support = mb_internal_encoding($this->wordlist_file_encoding);

            if (!$mb_support) {
                return false;
            }

            $strtolower_func = 'mb_strtolower';
        }

        $fp = fopen($this->wordlist_file, 'rb');
        if (!$fp) return false;

        $fsize = filesize($this->wordlist_file);
        if ($fsize < 128) return false; // too small of a list to be effective

        if ((int)$numWords < 1 || (int)$numWords > 5) $numWords = 1;

        $words = array();
        $i = 0;
        do {
            fseek($fp, mt_rand(0, $fsize - 128), SEEK_SET); // seek to a random position of file from 0 to filesize-128
            $data = fread($fp, 128); // read a chunk from our random position

            if ($mb_support !== false) {
                $data = mb_ereg_replace("\r?\n", "\n", $data);
            } else {
                $data = preg_replace("/\r?\n/", "\n", $data);
            }

            $start = @$this->strpos($data, "\n", mt_rand(0, 56)) + 1; // random start position
            $end   = @$this->strpos($data, "\n", $start);          // find end of word

            if ($start === false) {
                // picked start position at end of file
                continue;
            } else if ($end === false) {
                $end = $this->strlen($data);
            }

            $word = $strtolower_func($this->substr($data, $start, $end - $start)); // return a line of the file

            if ($mb_support) {
                // convert to UTF-8 for imagettftext
                $word = mb_convert_encoding($word, 'UTF-8', $this->wordlist_file_encoding);
            }

            $words[] = $word;
        } while (++$i < $numWords);

        fclose($fp);

        if ($numWords < 2) {
            return $words[0];
        } else {
            return $words;
        }
    }
protected function readCodeFromFile($numWords = 1)
    {
        $strtolower_func = 'strtolower';
        $mb_support      = false;

        if (!empty($this->wordlist_file_encoding)) {
            if (!extension_loaded('mbstring')) {
                trigger_error("wordlist_file_encoding option set, but PHP does not have mbstring support", E_USER_WARNING);
                return false;
            }

            // emits PHP warning if not supported
            $mb_support = mb_internal_encoding($this->wordlist_file_encoding);

            if (!$mb_support) {
                return false;
            }

            $strtolower_func = 'mb_strtolower';
        }

        $fp = fopen($this->wordlist_file, 'rb');
        if (!$fp) return false;

        $fsize = filesize($this->wordlist_file);
        if ($fsize < 128) return false; // too small of a list to be effective

        if ((int)$numWords < 1 || (int)$numWords > 5) $numWords = 1;

        $words = array();
        $i = 0;
        do {
            fseek($fp, mt_rand(0, $fsize - 128), SEEK_SET); // seek to a random position of file from 0 to filesize-128
            $data = fread($fp, 128); // read a chunk from our random position

            if ($mb_support !== false) {
                $data = mb_ereg_replace("\r?\n", "\n", $data);
            } else {
                $data = preg_replace("/\r?\n/", "\n", $data);
            }

            $start = @$this->strpos($data, "\n", mt_rand(0, 56)) + 1; // random start position
            $end   = @$this->strpos($data, "\n", $start);          // find end of word

            if ($start === false) {
                // picked start position at end of file
                continue;
            } else if ($end === false) {
                $end = $this->strlen($data);
            }

            $word = $strtolower_func($this->substr($data, $start, $end - $start)); // return a line of the file

            if ($mb_support) {
                // convert to UTF-8 for imagettftext
                $word = mb_convert_encoding($word, 'UTF-8', $this->wordlist_file_encoding);
            }

            $words[] = $word;
        } while (++$i < $numWords);

        fclose($fp);

        if ($numWords < 2) {
            return $words[0];
        } else {
            return $words;
        }
    }
protected function readCodeFromFile ( $ numWords = 1 ) { $ strtolower_func = 'strtolower' ; $ mb_support = false ; if ( ! empty ( $ this -> wordlist_file_encoding ) ) { if ( ! extension_loaded ( 'mbstring' ) ) { trigger_error ( "wordlist_file_encoding option set, but PHP does not have mbstring support" , E_USER_WARNING ) ; return false ; } // emits PHP warning if not supported $ mb_support = mb_internal_encoding ( $ this -> wordlist_file_encoding ) ; if ( ! $ mb_support ) { return false ; } $ strtolower_func = 'mb_strtolower' ; } $ fp = fopen ( $ this -> wordlist_file , 'rb' ) ; if ( ! $ fp ) return false ; $ fsize = filesize ( $ this -> wordlist_file ) ; if ( $ fsize < 128 ) return false ; // too small of a list to be effective if ( ( int ) $ numWords < 1 || ( int ) $ numWords > 5 ) $ numWords = 1 ; $ words = array ( ) ; $ i = 0 ; do { fseek ( $ fp , mt_rand ( 0 , $ fsize - 128 ) , SEEK_SET ) ; // seek to a random position of file from 0 to filesize-128 $ data = fread ( $ fp , 128 ) ; // read a chunk from our random position if ( $ mb_support !== false ) { $ data = mb_ereg_replace ( "\r?\n" , "\n" , $ data ) ; } else { $ data = preg_replace ( "/\r?\n/" , "\n" , $ data ) ; } $ start = @ $ this -> strpos ( $ data , "\n" , mt_rand ( 0 , 56 ) ) + 1 ; // random start position $ end = @ $ this -> strpos ( $ data , "\n" , $ start ) ; // find end of word if ( $ start === false ) { // picked start position at end of file continue ; } else if ( $ end === false ) { $ end = $ this -> strlen ( $ data ) ; } $ word = $ strtolower_func ( $ this -> substr ( $ data , $ start , $ end - $ start ) ) ; // return a line of the file if ( $ mb_support ) { // convert to UTF-8 for imagettftext $ word = mb_convert_encoding ( $ word , 'UTF-8' , $ this -> wordlist_file_encoding ) ; } $ words [ ] = $ word ; } while ( ++ $ i < $ numWords ) ; fclose ( $ fp ) ; if ( $ numWords < 2 ) { return $ words [ 0 ] ; } else { return $ words ; } }
Gets a captcha code from a file containing a list of words.

Seek to a random offset in the file and reads a block of data and returns a line from the file.

@param int $numWords Number of words (lines) to read from the file
@return string|array|bool  Returns a string if only one word is to be read, or an array of words
Gets a captcha code from a file containing a list of words .