Invision Power Services, Inc. * @copyright (c) Invision Power Services, Inc. * @license https://www.invisioncommunity.com/legal/standards/ * @package Invision Community * @since 9 Sept 2013 */ namespace IPSUtf8\Output\Browser; /** * Template Class */ class Template { public static function wrapper( $title="IPS Converter", $content="", $isError=false ) { $iniTime = @ini_get('max_execution_time'); $execTime = ( is_numeric( $iniTime ) ) ? $iniTime : 30; $url = \IPSUtf8\Output\Browser::$url; $title = ( empty( $title ) ) ? 'IPS Converter' : $title; $active = array( 'convert' => '', 'tools' => '', 'archive' => '' ); $hasArchive = \IPSUtf8\Session::i()->has_archive; $controller = preg_replace( "/[^a-zA-Z0-9]/", '', empty( \IPSUtf8\Request::i()->controller ) ? 'browser' : \IPSUtf8\Request::i()->controller ); $version = \IPSUtf8\Convert::VERSION_ID; $copyrightDate = date('Y'); if ( $controller == 'browser' ) { $active['convert'] = 'active'; } else if ( $controller === 'archive' ) { $active['archive'] = 'active'; } else { $active['tools'] = 'active'; } $html = << {$title}
EOFHTML; if ( ! $isError ) { $html .= <<
  • Convert
  • EOFHTML; if ( $hasArchive ) { $html .= <<Archive EOFHTML; } $html .= <<Tools EOFHTML; } $html .= <<IPS UTF8 Converter
    {$content}
    EOFHTML; return static::parse( $html ); } /** * Tools page */ public static function tools( $isUtf8=false, $msg ) { $html = <<

    Tools

    EOFHTML; if ( $msg !== null ) { $html .= <<
    {$msg}

     

    EOFHTML; } if ( ! isset( \IPSUtf8\Request::i()->do ) ) { if ( $isUtf8 === true ) { $html .= <<The database is set to UTF-8.

    Check and fix UTF-8 collation

    EOFHTML; } else { $html .= <<Please convert to UTF-8 first.

    EOFHTML; } } $html .= << EOFHTML; $code = implode( "
    ", \IPSUtf8\Convert::i()->getDebugString() ); $html .= <<

    Info

    {$code} EOFHTML; return static::parse( $html ); } /** * Error page */ public static function error( $msg ) { $html = <<

    Error

    {$msg}

    EOFHTML; return static::parse( $html ); } /** * Error page explaining about database being Invision Community already */ public static function errorAlreadyIPS4() { $html = << The target database is already running Invision Community. There is no conversion required and the UTF8 converter is locked.
    To override this and unlock the UTF8 converter, please create a file called "constants.php" and upload it to your /admin/convertutf8/ directory with the following contents:

    Please remember to remove the constants.php file when you are finished!

    EOFHTML; return static::parse( $html ); } /** * Welcome page */ public static function welcome( $isUtf8=false, $status, $percent=false, $controller='browser' ) { $convertClass = ( $controller === 'browser' ) ? '\IPSUtf8\Convert' : '\IPSUtf8\Convert\Archive'; $origUtf8Charset = \IPSUtf8\Db::i('utf8')->getCharset(); $canUseMb4 = ( version_compare( \IPSUtf8\Db::i()->server_info, '5.5.3', '>=' ) AND \IPSUtf8\Db::i('utf8')->set_charset('utf8mb4') !== FALSE ); \IPSUtf8\Db::i('utf8')->set_charset( $origUtf8Charset ); $badCollations = $convertClass::i()->getNonUtf8CollationTables(); $badTablesCnt = count( $convertClass::i()->getNonUtf8Tables() ); $path = ROOT_PATH; $dbCharSetIsCorrect = TRUE; $isUtf8mb4 = ( $isUtf8 === TRUE AND \IPSUtf8\Convert::i()->database_charset == 'utf8mb4' ); if ( file_exists( ROOT_PATH . '/conf_global.php' ) ) { require_once( ROOT_PATH . '/conf_global.php' ); if ( !isset( $INFO['sql_charset'] ) OR ! in_array( $INFO['sql_charset'], array( 'utf8', 'utf8mb4' ) ) ) { $dbCharSetIsCorrect = FALSE; } } if ( isset( \IPSUtf8\Request::i()->convert_anyway ) AND \IPSUtf8\Request::i()->convert_anyway == 1 ) { $badTablesCnt = \IPSUtf8\Session::i()->json['tableCount']; } $html = <<

    Welcome

    EOFHTML; if ( $controller === 'archive' ) { $html .= <<The converter has detected that you have an archive table in a different database.

    EOFHTML; } if ( $status === 'processing' ) { $html .= <<Conversion in progress ({$percent}% complete).

    EOFHTML; } else if ( $isUtf8 !== true ) { $html .= <<You have {$badTablesCnt} table(s) in this database that are not UTF-8.\nYou must convert these tables to UTF-8 before you can proceed with the upgrade.

    EOFHTML; } else if ( $isUtf8 === true ) { if ( count( $badCollations ) ) { $cnt = count( $badCollations ); $html .= << The database is set to UTF-8, however {$cnt} table(s) have incorrect collations and need fixing.

    Fix UTF-8 collations

    EOFHTML; } else { /* If the charset stored for the gb_char_set setting is not utf-8, then we'll hit an endless loop unless we fix it here */ \IPSUtf8\Db::i()->update( 'core_sys_conf_settings', array( 'conf_value' => 'utf-8' ), array( 'conf_key=?', 'gb_char_set' ) ); $html .= << The database tables are UTF-8, collations are correct and there is nothing to convert. You can proceed with the upgrade.

    EOFHTML; } } if ( ! $isUtf8 and $canUseMb4 ) { $buttonLabel = ( $isUtf8 ) ? "Convert anyway using 4-Byte UTF-8 (utf8mb4)" : "Start using 4-Byte UTF-8 (utf8mb4)"; $html .= <<
    {$buttonLabel}
    Some non-common symbols (such as historical scripts, music symbols and Emoji) require more space in the database to be stored. If you choose to convert using 4-Byte UTF-8, these characters will be able to be used, but the database will use more disk space.

    EOFHTML; } if ( ! $isUtf8 ) { $label = 'Start using UTF-8 (utf8)'; $selector = 'btn-success'; $more = <<
    Choose this if you don't wish to store non-common symbols (such as historical scripts, music symbols and Emoji) and wish to use less disk space.

    Please note that you should turn your community offline before starting any conversion on a live site.

    If you have a large community, or are experiencing browser timeouts during conversion, you may wish to consider running the conversion via the Command Line.
    cd {$path}/admin/convertutf8 php cli.php
    EOFHTML; } if ( $status === 'processing' ) { $label = 'Continue Conversion'; $selector = 'btn-warning'; $more = ''; } else if ( ! $isUtf8 ) { $label = "Convert as UTF-8 (utf8)"; $selector = 'btn-info'; } if ( $label and $selector ) { $html .= << {$label} {$more}

    EOFHTML; } if ( $status === 'processing' ) { $html .= <<Reset conversion and restart

    EOFHTML; } $html .= << EOFHTML; return static::parse( $html ); } /** * Process page */ public static function process( $status, $percent, $msg ) { $utfPrefix = \IPSUtf8\Db::i('utf8')->prefix . \IPSUtf8\Db::i()->prefix; $html = <<

    Converting

     

    {$msg}

    {$msg}

    EOFHTML; return static::parse( $html ); } /** * Completed page */ public static function completed( $timeTaken ) { $html = <<

    Almost there!

    The database has now been converted!

    Conversion took {$timeTaken}

     

    Click here to finish

    EOFHTML; return static::parse( $html ); } /** * Finish page */ public static function finished( $updated ) { $utfPrefix = \IPSUtf8\Db::i('utf8')->prefix; $normalPrefix = \IPSUtf8\Db::i()->prefix; $sessionData = \IPSUtf8\Session::i()->json; $sql_charset = ( array_key_exists( 'utf8mb4', $sessionData['charSets'] ) ) ? 'utf8mb4' : 'utf8'; /* Phil made me give him credit for some changes in this code */ $html = <<

    Finished!

    The database has now been converted and the tables correctly renamed!

    EOFHTML; if ( \IPSUtf8\Request::i()->controller === 'browser' ) { if( !$updated ) { $utf8mb4 = ( $sql_charset == 'utf8mb4' ) ? "
    \$INFO['sql_utf8mb4'] = true;" : ''; $html .= <<You can now complete the conversion by editing conf_global.php to add
    \$INFO['sql_charset'] = '{$sql_charset}';{$utf8mb4}.
    Afterwards, you can proceed with the upgrade.

    EOFHTML; } else { $html .= <<You can now
    proceed with the upgrade.

    EOFHTML; } } else if ( \IPSUtf8\Request::i()->controller === 'archive' ) { $html .= <<You can now complete the conversion by converting the
    main database if you've not already done so.

    EOFHTML; } return static::parse( $html ); } /** * Basic HTML parsing * * @param string $html Raw HTML * @return string */ protected static function parse( $html ) { /* Parse {plugin="foo"} tags */ $html = preg_replace_callback ( '/\{([a-z]+?=([\'"]).+?\\2 ?+)}/', function( $matches ) { /* Work out the plugin and the values to pass */ preg_match_all( '/(.+?)='.$matches[2].'(.+?)'.$matches[2].'\s?/', $matches[1], $submatches ); $plugin = array_shift( $submatches[1] ); $value = array_shift( $submatches[2] ); $options = array(); foreach ( $submatches[1] as $k => $v ) { $options[ $v ] = $submatches[2][ $k ]; } switch( $plugin ) { case 'url': return \IPSUtf8\Output\Browser::$url . $value; break; } }, $html ); return $html; } }