/*!
Generates PHP code for the command \a $command.
\param $charsetName The name of the charset the text will be in,
this can be used to generate different code for different charsets.
\return A string containing PHP code or \c false if not supported.
964536
function generateCommandCode( $command, $charsetName )
    {
        if ( $command['command'] == 'url_cleanup_iri' )
        {
            $charsetNameTxt = var_export( $charsetName, true );
            $code = "\$text = eZCharTransform::commandUrlCleanupIRI( \$text, $charsetNameTxt );\n";
            return $code;
        }
        else if ( $command['command'] == 'url_cleanup' )
        {
            $charsetNameTxt = var_export( $charsetName, true );
            $code = "\$text = eZCharTransform::commandUrlCleanup( \$text, $charsetNameTxt );\n";
            return $code;
        }
        else if ( $command['command'] == 'url_cleanup_compat' )
        {
            $charsetNameTxt = var_export( $charsetName, true );
            $code = "\$text = eZCharTransform::commandUrlCleanupCompat( \$text, $charsetNameTxt );\n";
            return $code;
        }
        else if ( $command['command'] == 'identifier_cleanup' )
        {
            $code = ( "\$text = strtolower( \$text );\n" .
                      "\$text = preg_replace( array( \"#[^a-z0-9_ ]#\",\n" .
                      "                             \"/ /\",\n" .
                      "                             \"/__+/\",\n" .
                      "                             \"/^_|_$/\" ),\n" .
                      "                      array( \" \",\n" .
                      "                             \"_\",\n" .
                      "                             \"_\",\n" .
                      "                             \"\" ),\n" .
                      "                      \$text );\n" );
            return $code;
        }
        else if ( $command['command'] == 'search_cleanup' )
        {
            $code = '';
            $nonCJKCharsets = $this->nonCJKCharsets();
            if ( !in_array( $charsetName, $nonCJKCharsets ) )
            {
                $code .= ( '// add N-Gram(N=2)  chinese / japanese / korean multibyte characters' . "\n" .
                           '$codec = eZTextCodec::instance( false, \'unicode\' );' . "\n" .
                           "\n" .
                           '$unicodeValueArray = $codec->convertString( $text );' . "\n" .
                           "\n" .
                           '$normalizedTextArray = array();' . "\n" .
                           '$bFlag = false;' . "\n" .
                          'foreach ( array_keys( $unicodeValueArray ) as $valueKey )' . "\n" .
                           '{' . "\n" .
                           '    // Check for word characters that should be broken up for search' . "\n" .
                           '    if ( ( $unicodeValueArray[$valueKey] >= 12289 and' . "\n" .
                           '           $unicodeValueArray[$valueKey] <= 12542 ) or' . "\n" .
                           '         ( $unicodeValueArray[$valueKey] >= 13312 and' . "\n" .
                           '           $unicodeValueArray[$valueKey] <= 40863 ) or' . "\n" .
                           '         ( $unicodeValueArray[$valueKey] >= 44032 and' . "\n" .
                           '           $unicodeValueArray[$valueKey] <= 55203 ) )' . "\n" .
                           '    {' . "\n" .
                           '        if ( $bFlag )' . "\n" .
                           '        {' . "\n" .
                           '            $normalizedTextArray[] = $unicodeValueArray[$valueKey];' . "\n" .
                           '        }' . "\n" .
                           '        $normalizedTextArray[] = 32; // A space' . "\n" .
                           '        $normalizedTextArray[] = $unicodeValueArray[$valueKey];' . "\n" .
                           '        $bFlag = true;' . "\n" .
                           '    }' . "\n" .
                           '    else' . "\n" .
                           '    {' . "\n" .
                           '        if ( $bFlag )' . "\n" .
                           '        {' . "\n" .
                           '            $normalizedTextArray[] = 32; // A space' . "\n" .
                           '        }' . "\n" .
                           '        $normalizedTextArray[] = $unicodeValueArray[$valueKey];' . "\n" .
                           '        $bFlag = false;' . "\n" .
                           '    }' . "\n" .
                           '}' . "\n" .
                           'if ( $bFlag )' . "\n" .
                           '{' . "\n" .
                           '    $normalizedTextArray[count($normalizedTextArray)-1]=32;' . "\n" .
                           '}' . "\n" .
                           '$revCodec = eZTextCodec::instance( \'unicode\', false ); // false means use internal charset' . "\n" .
                           '$text = $revCodec->convertString( $normalizedTextArray );' . "\n" );
            }
            $code .= ( '$text = preg_replace( array( "#(\.){2,}#",' . "\n" .
                       '                             "#^\.#",' . "\n" .
                       '                             "#\s\.#",' . "\n" .
                       '                             "#\.\s#",' . "\n" .
                       '                             "#\.$#",' . "\n" .
                       '                             "#([^0-9])%#" ),' . "\n" .
                       '                      array( " ",' . "\n" .
                       '                             " ",' . "\n" .
                       '                             " ",' . "\n" .
                       '                             " ",' . "\n" .
                       '                             " ",' . "\n" .
                       '                             " " ),' . "\n" .
                       '                      $text );' . "\n" .
                       '$ini = eZINI::instance();' . "\n" .
                       'if ( $ini->variable( \'SearchSettings\', \'EnableWildcard\' ) != \'true\' )' . "\n" .
                       '{' . "\n" .
                       '    $text = str_replace( "*", " ", $text );' . "\n" .
                       '}' . "\n" .
                       '$charset = eZTextCodec::internalCharset();' . "\n" .
                       '$hasUTF8 = ( $charset == "utf-8" );' . "\n" .
                       "\n" .
                       'if ( $hasUTF8 )' . "\n" .
                       '{' . "\n" .
                       '    $text = preg_replace( "#(\s+)#u", " ", $text );' . "\n" .
                       '}' . "\n" .
                       'else' . "\n" .
                       '{' . "\n" .
                       '    $text = preg_replace( "#(\s+)#", " ", $text );' . "\n" .
                       '}' );

            return $code;
        }
        else
        {
            $ini = eZINI::instance( 'transform.ini' );
            $commands = $ini->variable( 'Extensions', 'Commands' );
            if ( isset( $commands[$command['command']] ) )
            {
                list( $path, $className ) = explode( ':', $commands[$command['command']], 2 );
                if ( file_exists( $path ) )
                {
                    $charsetNameTxt = var_export( $charsetName, true );
                    $commandTxt     = var_export( $command['command'], true );
                    $pathTxt        = var_export( $path, true );
                    $code = "include_once( $pathTxt );\n\$text = $className::executeCommand( \$text, $commandTxt, $charsetNameTxt );\n";
                    return $code;
                }
                else
                {
                    eZDebug::writeError( "Could not locate include file '$path' for transformation '" . $command['command'] . "'" );
                }
            }
        }
        return false;
    }
532387
function SetCharSet($charset_name)
	{
		$this->GetCharSet();
		if ($this->charSet !== $charset_name) {
			$if = pg_set_client_encoding($this->_connectionID, $charset_name);
			if ($if == "0" & $this->GetCharSet() == $charset_name) {
				return true;
			} else return false;
		} else return true;
	}
1064029
function decodeCommand( $name, $parameters )
    {
        $names = $this->ruleNames();
        $rules = array();
        switch ( $name )
        {
            // Special code handlers
            case 'url_cleanup_iri':
            case 'url_cleanup':
            case 'url_cleanup_compat':
            case 'identifier_cleanup':
            {
            } break;

            case 'normalize':
            case 'search_normalize':
            case 'decompose':
            case 'diacritical':
            case 'lowercase':
            case 'uppercase':
            case 'search_cleanup':
            {
                if ( count( $parameters ) == 0 )
                {
                    // Include all normalize rules
                    foreach ( $names as $rule )
                    {
                        if ( preg_match( '#_'. $name . '$#', $rule ) )
                            $rules[] = $rule;
                    }
                }
                else
                {
                    foreach ( $parameters as $parameter )
                    {
                        $rule = $parameter . '_' . $name;
                        if ( in_array( $rule, $names ) )
                            $rules[] = $rule;
                    }
                }
            } break;

            case 'transform':
            case 'transliterate':
            {
                $dividers = array( 'transform' => '_to_',
                                   'transliterate' => '_transliterate_' );
                $divider = $dividers[$name];
                if ( count( $parameters ) == 0 )
                {
                    // Include all transformation rules
                    foreach ( $names as $rule )
                    {
                        if ( preg_match( '#^[a-zA-Z][a-zA-Z0-9-]+'. $divider . '[a-zA-Z][a-zA-Z0-9-]+$#', $rule ) )
                            $rules[] = $rule;
                    }
                }
                else if ( count( $parameters ) == 2 )
                {
                    $rule = $parameters[0] . $divider . $parameters[1];
                    if ( in_array( $rule, $names ) )
                        $rules[] = $rule;
                }
            } break;

            default:
            {
                $ini = eZINI::instance( 'transform.ini' );
                $commands = $ini->variable( 'Extensions', 'Commands' );
                if ( isset( $commands[$name] ) )
                {
                    break;
                }
                eZDebug::writeError( "Unknown command '$name'", __METHOD__ );
            } break;
        }
        return $rules;
    }
687109
function tableCharsetName( $charset )
    {
        $charset = eZCharsetInfo::realCharsetCode( $charset );
        // Convert charset names into something MySQL will understand
        $charsetMapping = array( 'iso-8859-1' => 'latin1',
                                 'iso-8859-2' => 'latin2',
                                 'iso-8859-8' => 'hebrew',
                                 'iso-8859-7' => 'greek',
                                 'iso-8859-9' => 'latin5',
                                 'iso-8859-13' => 'latin7',
                                 'windows-1250' => 'cp1250',
                                 'windows-1251' => 'cp1251',
                                 'windows-1256' => 'cp1256',
                                 'windows-1257' => 'cp1257',
                                 'utf-8' => 'utf8',
                                 'koi8-r' => 'koi8r',
                                 'koi8-u' => 'koi8u' );
        $charset = strtolower( $charset );
        if ( isset( $charsetMapping[$charset] ) )
            return $charsetMapping[$charset];
        return $charset;
    }
472919
public function charset($charset = null)
    {
        if (!func_num_args()) {
            return $this->_stream->charset();
        }
        $this->_stream->charset($charset);
        return $this;
    }
1297955
private function mail_head_param_encode(string $string_to_encode, string $charset = 'utf-8')
    {
        $end = '?=';
        $start = '=?' . $charset . '?B?';

        $spacer = $end . "\r\n " . $start;

        $length = 75 - strlen($start) - strlen($end);
        $length -= ($length % 4);

        $string_to_encode = base64_encode($string_to_encode);
        $string_to_encode = chunk_split($string_to_encode, $length, $spacer);

        $spacer = preg_quote($spacer, '');

        $string_to_encode = preg_replace('/' . $spacer . '$/', NULL, $string_to_encode);
        $string_to_encode = $start . $string_to_encode . $end;

        return $string_to_encode;
    }
217834
protected function getCommandByString($name, $setName = false)
  {
    if(stristr($name, '.'))
    {
      $parts = explode(' ', ucwords(str_replace('.', ' ', $name)));
      $class = '\\' . implode('\\', $parts);
    }
    else
    {
      $class = $name;
    }

    foreach($this->_searchPatterns as $pattern)
    {
      $attempt = str_replace(['.', '%s'], ['\\', $class], $pattern);

      if(!class_exists($attempt))
      {
        continue;
      }

      $command = $setName ? new $attempt($name) : new $attempt();
      if($command instanceof Command)
      {
        return $this->_prepareCommand($command);
      }
    }

    return null;
  }
1165050
public function build($length = null, $charset = null)
    {
        if ($length !== null) {
            $this->length = $length;
        }
        if ($charset !== null) {
            $this->charset = $charset;
        }

        $phrase = '';
        $chars = str_split($this->charset);

        for ($i = 0; $i < $this->length; $i++) {
            $phrase .= $chars[array_rand($chars)];
        }

        return $phrase;
    }
1335286
public function save($filename, $charset = null)
    {
        $data = $this->getData();

        /*
        if ($charset === null) {
            $charset = $this->options['charset'];
        }

        */
        // UTF-8 : file_put_contents("file.txt", "\xEF\xBB\xBF" . $data);

        /*

            $data = file_get_contents($npath);
            $data = mb_convert_encoding($data, 'UTF-8', 'OLD-ENCODING');
            file_put_contents('tempfolder/'.$a, $data);

            Or alternatively, with PHP's stream filters:

            $fd = fopen($file, 'r');
            stream_filter_append($fd, 'convert.iconv.UTF-8/OLD-ENCODING');
            stream_copy_to_stream($fd, fopen($output, 'w'));
         *
         * mb_convert_encoding($data, 'UTF-8', 'auto');
         * mb_convert_encoding($data, 'UTF-8', mb_detect_encoding($data));
         */

        $ret = file_put_contents($filename, $data);
        if (!$ret) {
            throw new \Exception("Filename $filename cannot be written");
        }
    }
1094130
public String get(String command) throws PHPException {
		try {
			String s = php.execGet(command);
			flushContent();
			return s;
		} catch (IOException e) {
			LOGGER.error("IO Error with PhpDriver : " + e.toString());
			throw new IOErrorException(flushContentAndDropIOException());
		}
	}
913371
public function setOutputCharset($charset)
    {
        $flag = true;
        
        $charset  = strtolower($charset);
        $charsets = array('utf-8', 'windows-1256', 'cp1256', 'iso-8859-6');
        
        if (in_array($charset, $charsets)) {
            if ($charset == 'windows-1256') {
                $charset = 'cp1256';
            }
            $this->_outputCharset = $charset;
        } else {
            $flag = false;
        }
        
        return $flag;
    }
1361488
public function process($value = '', $format = 'html', $charset = null)
    {
        if ($charset === null) {
            $charset = $this->core->getCharset();
        }

        switch ($format) {
            case 'html':
                return htmlspecialchars((string)$value, ENT_QUOTES, $charset);
            case 'htmlall':
                return htmlentities((string)$value, ENT_QUOTES, $charset);
            case 'url':
                return rawurlencode((string)$value);
            case 'urlpathinfo':
                return str_replace('%2F', '/', rawurlencode((string)$value));
            case 'quotes':
                return preg_replace("#(?<!\\\\)'#", "\\'", (string)$value);
            case 'hex':
                $out = '';
                $cnt = strlen((string)$value);
                for ($i = 0; $i < $cnt; ++ $i) {
                    $out .= '%' . bin2hex((string)$value[$i]);
                }

                return $out;
            case 'hexentity':
                $out = '';
                $cnt = strlen((string)$value);
                for ($i = 0; $i < $cnt; ++ $i) {
                    $out .= '&#x' . bin2hex((string)$value[$i]) . ';';
                }

                return $out;
            case 'javascript':
            case 'js':
                return strtr((string)$value,
                    array(
                        '\\' => '\\\\',
                        "'"  => "\\'",
                        '"'  => '\\"',
                        "\r" => '\\r',
                        "\n" => '\\n',
                        '</' => '<\/'
                    ));
            case 'mail':
                return str_replace(array(
                    '@',
                    '.'
                ),
                    array(
                        '&nbsp;(AT)&nbsp;',
                        '&nbsp;(DOT)&nbsp;'
                    ),
                    (string)$value);
            default:
                $this->core->triggerError('Escape\'s format argument must be one of : html, htmlall, url, urlpathinfo, hex, hexentity, javascript, js or mail, "' . $format . '" given.',
                    E_USER_WARNING);
        }
    }
747062
function execute($command) {
        $output = array();
        $result = null;

        // Redirect standard error to stdout so we can do something with
        // it.
        $command .= " 2>&1";

        exec($command, $output, $result);

        if ($result !== 0) {
            if ($result === self::RESULT_CODE_COMMAND_NOT_FOUND) {
                throw new CommandNotFoundError(implode("\t", $output), $result);
            }
            else {
                throw new ExecutionError(implode("\t", $output), $result);
            }
        }
        return $output;
    }
801815
public static function generate(string $text = '', ?int $charCount = null): string
    {
        $text = StripTags::strip($text);
        $text = \htmlspecialchars_decode((string)$text, ENT_COMPAT | ENT_HTML5);
        $text = \trim(\preg_replace('/\s+/', ' ', $text));

        if ($charCount === null || \mb_strlen($text, 'UTF-8') <= $charCount) {
            return $text;
        }

        $expand = Tags::span(static::getConstant('TXT_EXPANDABLE_EXPAND'), ['class' => 'expand-link']);
        $collapse = Tags::span(static::getConstant('TXT_EXPANDABLE_COLLAPSE'), ['class' => 'collapse-link']);
        $short = Shortener::shorten($text, $charCount, true, '');
        $short = Tags::span(\sprintf($short.'...'.$expand), ['class' => 'short']);
        $full = Tags::span(\sprintf($text.$collapse), ['class' => 'full']);

        return Tags::span($short.$full, ['class' => 'expandable']);
    }
521854
public function setName($name): \Symfony\Component\Console\Command\Command
    {
        if (!$this->namespace) {
            return parent::setName($name);
        }
        return parent::setName(sprintf('%s:%s', $this->namespace, $name));
    }
393439
public function help($command = 'help')
    {
        $output = $this->getInfo().
        Colors::YELLOW."Description:".PHP_EOL.
        Colors::WHITE."  Displays help for a command".PHP_EOL.
        PHP_EOL.
        Colors::YELLOW."Usage:".PHP_EOL.
        Colors::WHITE."  help [options] [--] [<command_name>]".PHP_EOL.
        PHP_EOL.
        Colors::YELLOW."Arguments:".PHP_EOL.
        Colors::GREEN."  command ".Colors::WHITE."\tThe command to execute".PHP_EOL.
        Colors::GREEN."  command_name ".Colors::WHITE."\tThe command name ".Colors::YELLOW."[default: \"help\"]".PHP_EOL.
        PHP_EOL.
        Colors::YELLOW."Options:".PHP_EOL.
        Colors::GREEN."  -h, --help".Colors::WHITE."\t\tDisplay this help message".PHP_EOL.
        Colors::GREEN."  -V, --version".Colors::WHITE."\t\tDisplay this application version".PHP_EOL.
        PHP_EOL.
        Colors::YELLOW."Help:".PHP_EOL.
        Colors::WHITE."  The ".Colors::GREEN."help".Colors::WHITE." command displays help for a given command:".PHP_EOL.
        PHP_EOL.
        Colors::GREEN."    php jarvis help".PHP_EOL.
        PHP_EOL.
        Colors::WHITE."  To display the list of available commands, please use the ".Colors::GREEN."list".Colors::WHITE." command.".PHP_EOL.
        Colors::BG_BLACK.Colors::WHITE;
        return $output;
    }
162422
public function charset($charset, $convert = true)
    {
        $oldcharset = $this->_charset;
        $this->_charset = Horde_String::upper($charset);

        if (!$convert || ($oldcharset == $this->_charset)) {
            return;
        }

        foreach (array('and', 'or') as $item) {
            if (isset($this->_search[$item])) {
                foreach ($this->_search[$item] as &$val) {
                    $val->charset($charset, $convert);
                }
            }
        }

        foreach (array('header', 'text') as $item) {
            if (isset($this->_search[$item])) {
                foreach ($this->_search[$item] as $key => $val) {
                    $new_val = Horde_String::convertCharset($val['text'], $oldcharset, $this->_charset);
                    if (Horde_String::convertCharset($new_val, $this->_charset, $oldcharset) != $val['text']) {
                        throw new Horde_Imap_Client_Exception_SearchCharset($this->_charset);
                    }
                    $this->_search[$item][$key]['text'] = $new_val;
                }
            }
        }
    }