public function loadDictionary(string $filename, $append = false, $regexp = "/^[a-z]{4,6}$/")
    {
        if (!file_exists($filename) || !is_readable($filename))
        {
            $orig = $filename;
            $filename = "/usr/share/dict/" . $filename;
            if (!file_exists($filename))
                throw new IOException("Cannot open file $orig and dictionary $filename does not exist");
        }

        $cnt = mime_content_type($filename);
        if (substr($cnt, 0, 4) !== "text")
            throw new IOException("$filename does not appear to contain text");

        $contents = file_get_contents($filename);
        $lines = explode("\n", $contents);
        if (!$append)
            $this->dictionary = array();

        $nwords = 0;

        $wrapper = new ErrorInterceptor('preg_match');
        $wrapper->registerError(E_WARNING, 'preg_match');

        foreach ($lines as $line)
        {
            if (empty($regexp) || $wrapper->execute($regexp, $line))
            {
                $this->dictionary[] = $line;
                ++$nwords;
            }

            if (count($wrapper->getInterceptedErrors()))
                throw new InvalidArgumentException("Invalid regexp: $regexp");
        }

        if ($nwords === 0)
            throw new InvalidArgumentException("File $filename contains no words that match $regexp");

        return $this;
    }
public function loadDictionary(string $filename, $append = false, $regexp = "/^[a-z]{4,6}$/")
    {
        if (!file_exists($filename) || !is_readable($filename))
        {
            $orig = $filename;
            $filename = "/usr/share/dict/" . $filename;
            if (!file_exists($filename))
                throw new IOException("Cannot open file $orig and dictionary $filename does not exist");
        }

        $cnt = mime_content_type($filename);
        if (substr($cnt, 0, 4) !== "text")
            throw new IOException("$filename does not appear to contain text");

        $contents = file_get_contents($filename);
        $lines = explode("\n", $contents);
        if (!$append)
            $this->dictionary = array();

        $nwords = 0;

        $wrapper = new ErrorInterceptor('preg_match');
        $wrapper->registerError(E_WARNING, 'preg_match');

        foreach ($lines as $line)
        {
            if (empty($regexp) || $wrapper->execute($regexp, $line))
            {
                $this->dictionary[] = $line;
                ++$nwords;
            }

            if (count($wrapper->getInterceptedErrors()))
                throw new InvalidArgumentException("Invalid regexp: $regexp");
        }

        if ($nwords === 0)
            throw new InvalidArgumentException("File $filename contains no words that match $regexp");

        return $this;
    }
public function loadDictionary ( string $ filename , $ append = false , $ regexp = "/^[a-z]{4,6}$/" ) { if ( ! file_exists ( $ filename ) || ! is_readable ( $ filename ) ) { $ orig = $ filename ; $ filename = "/usr/share/dict/" . $ filename ; if ( ! file_exists ( $ filename ) ) throw new IOException ( "Cannot open file $orig and dictionary $filename does not exist" ) ; } $ cnt = mime_content_type ( $ filename ) ; if ( substr ( $ cnt , 0 , 4 ) !== "text" ) throw new IOException ( "$filename does not appear to contain text" ) ; $ contents = file_get_contents ( $ filename ) ; $ lines = explode ( "\n" , $ contents ) ; if ( ! $ append ) $ this -> dictionary = array ( ) ; $ nwords = 0 ; $ wrapper = new ErrorInterceptor ( 'preg_match' ) ; $ wrapper -> registerError ( E_WARNING , 'preg_match' ) ; foreach ( $ lines as $ line ) { if ( empty ( $ regexp ) || $ wrapper -> execute ( $ regexp , $ line ) ) { $ this -> dictionary [ ] = $ line ; ++ $ nwords ; } if ( count ( $ wrapper -> getInterceptedErrors ( ) ) ) throw new InvalidArgumentException ( "Invalid regexp: $regexp" ) ; } if ( $ nwords === 0 ) throw new InvalidArgumentException ( "File $filename contains no words that match $regexp" ) ; return $ this ; }
Load a dictionary file for passphrase generation
@param string $filename The file to read. Can be a full path or a name
of a dictionary in /usr/share/dict
@param bool $append True to add to the list of words, false to replace
it. Defauls to false
@param string $regexp A regular expression to match each word to. Can be
empty. Defaults to allowing on lowercase latin alphabet.
@return Wedeto\Auth\PasswordGenerator Provides fluent interface
@throws InvalidArgumentException When an invalid regular expression is
given or no words match it.
@throws Wedeto\IOException When the file is not readable
Load a dictionary file for passphrase generation