<?php
global $smf_settings, $smf_user_info;

if( !defined( 'MEDIAWIKI' ) )
        die( -1 );

/**
 * Inherit main code from SkinTemplate, set the CSS and template filter.
 * @todo document
 * @ingroup Skins
 */
class SkinGCL3 extends SkinTemplate {
        /** Using GCL3. */
        var $skinname = 'GCL3', $stylename = 'GCL3',
                $template = 'GCL3Template', $useHeadElement = true;

        function setupSkinUserCss( OutputPage $out ) {
                global $wgHandheldStyle;

                parent::setupSkinUserCss( $out );

                // Append to the default screen common & print styles...
                $out->addStyle( 'GCL3/main.css', 'screen' );
                $out->addStyle( 'GCL3/resources/print.css', 'print' );
                if( $wgHandheldStyle ) {
                        // Currently in testing... try 'chick/main.css'
                        $out->addStyle( $wgHandheldStyle, 'handheld' );
                }

                $out->addStyle( 'GCL3/IE50Fixes.css', 'screen', 'lt IE 5.5000' );
                $out->addStyle( 'GCL3/IE55Fixes.css', 'screen', 'IE 5.5000' );
                $out->addStyle( 'GCL3/IE60Fixes.css', 'screen', 'IE 6' );
                $out->addStyle( 'GCL3/IE70Fixes.css', 'screen', 'IE 7' );

                $out->addStyle( 'GCL3/rtl.css', 'screen', '', 'rtl' );

        }
}

/**
 * @todo document
 * @ingroup Skins
 */
class GCL3Template extends QuickTemplate {
        var $skin;

    function head() {
        ob_start();
        $this->html( 'headelement' );
        $mw_head = ob_get_contents();
        ob_end_clean();

        ob_start();
        template_html_above();
        $smf_head = ob_get_contents();
        ob_end_clean();

        // merge mediawiki and smf heads together
        $smf_head = str_replace('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>',"",$smf_head);
        $mw_head = str_replace("</head>",$smf_head,$mw_head);

        echo $mw_head;
    }
        /**
         * Template filter callback for GCL3 skin.
         * Takes an associative array of data set from a SkinTemplate-based
         * class, and a wrapper for MediaWiki's localization database, and
         * outputs a formatted page.
         *
         * @access private
         */
        function execute() {
                global $wgRequest, $wgScriptPath;

                $this->skin = $skin = $this->data['skin'];
                $action = $wgRequest->getText( 'action' );

                // Suppress warnings to prevent notices about missing indexes in $this->data
                wfSuppressWarnings();

                //$this->html( 'headelement' );
        $this->head();
?><div id="globalWrapper">
<div id="column-content"><div id="content" <?php $this->html("specialpageattributes") ?>>
        <a id="top"></a>
<?php
    if(!function_exists("template_main_above")) {
       //template_html_above();
       template_body_above();
    } else {
       template_main_above();
    }
?>
        <?php if($this->data['sitenotice']) { ?><div id="siteNotice"><?php $this->html('sitenotice') ?></div><?php } ?>

<table id="metapod-table">
    <tr>
        <td id="metapod-menu">
<?php
                $sidebar = $this->data['sidebar'];
                if ( !isset( $sidebar['SEARCH'] ) ) $sidebar['SEARCH'] = true;
                if ( !isset( $sidebar['TOOLBOX'] ) ) $sidebar['TOOLBOX'] = true;
                if ( !isset( $sidebar['LANGUAGES'] ) ) $sidebar['LANGUAGES'] = true;
                foreach ($sidebar as $boxName => $cont) {
                        if ( $boxName == 'SEARCH' ) {
                                $this->searchBox();
                        } elseif ( $boxName == 'TOOLBOX' ) {
                                $this->toolbox();
                        } elseif ( $boxName == 'LANGUAGES' ) {
                                $this->languageBox();
                        } else {
                                $this->customBox( $boxName, $cont );
                        }
                }
?>
        </td>
        <td id="metapod-content">
        <!--<h1 id="firstHeading" class="firstHeading"><?php $this->html('title') ?></h1>-->
        <?php
        $linx = array();
        $page_url = array_values($this->data['content_actions'])[0]['href'];

        $page_url_pattern = '/' . str_replace(array("$1", '/'), array("(.*)", '\/'), $this->data['articlepath']) . '/';
        $page_url_matches = array();
        preg_match($page_url_pattern, $page_url, $page_url_matches);
        $page_path = $page_url_matches[1];
        $new_title = "<a href='$page_url'>" . $this->data['title'] . "</a>";

        if($page_path) {
            $tbits = explode("/",$this->data['title']);
            $urlbits = explode("/",$page_path);

            $linkzor2 = array();
            $linkTarget = "";
            for($i = 0; $i < count($tbits); $i++) {
                $linkTarget .= str_replace(" ","_", $linkTarget ? "/${urlbits[$i]}" : $urlbits[$i]);
                $linkzor2[] = "<a href=\"" . str_replace("$1", $linkTarget, $this->data['articlepath']) . "\">${tbits[$i]}</a>";
            }
            $new_title = implode("/",$linkzor2);
        }

        foreach($this->data['content_actions'] as $key => $tab) {
            $linx[] = '<a href="'.htmlspecialchars($tab['href']).'"'.Linker::tooltipAndAccesskeyAttribs('ca-'.$key).'>'.htmlspecialchars($tab['text']).'</a>';
        }
        ?>
        <?php begin_box3($this,$new_title,implode($linx," | ")); ?>
        <div id="bodyContent">
                <h3 id="siteSub"><?php $this->msg('tagline') ?></h3>
                <div id="contentSub"<?php $this->html('userlangattributes') ?>><?php $this->html('subtitle') ?></div>
<?php if($this->data['undelete']) { ?>
                <div id="contentSub2"><?php $this->html('undelete') ?></div>
<?php } ?><?php if($this->data['newtalk'] ) { ?>
                <div class="usermessage"><?php $this->html('newtalk')  ?></div>
<?php } ?><?php if($this->data['showjumplinks']) { ?>
                <div id="jump-to-nav"><?php $this->msg('jumpto') ?> <a href="#column-one"><?php $this->msg('jumptonavigation') ?></a>, <a href="#searchInput"><?php $this->msg('jumptosearch') ?></a></div>
<?php } ?>

                <!-- start content -->
<?php $this->html('bodytext') ?>
                <?php if($this->data['catlinks']) { $this->html('catlinks'); } ?>
                <!-- end content -->
        <?php end_box(); ?>
                <?php if($this->data['dataAfterContent']) { $this->html ('dataAfterContent'); } ?>
                <div class="visualClear"></div>
        </div>
</div></div>

    </td>
    </tr>
</table>
<!-- <div id="column-one"<?php $this->html('userlangattributes')  ?>>

        <div id="p-cactions" class="portlet">
                <h5><?php $this->msg('views') ?></h5>
                <div class="pBody">
                        <ul><?php
                                foreach($this->data['content_actions'] as $key => $tab) {
                                        echo '
                                 <li id="' . Sanitizer::escapeId( "ca-$key" ) . '"';
                                        if( $tab['class'] ) {
                                                echo ' class="'.htmlspecialchars($tab['class']).'"';
                                        }
                                        echo '><a href="'.htmlspecialchars($tab['href']).'"';
                                        # We don't want to give the watch tab an accesskey if the
                                        # page is being edited, because that conflicts with the
                                        # accesskey on the watch checkbox.  We also don't want to
                                        # give the edit tab an accesskey, because that's fairly su-
                                        # perfluous and conflicts with an accesskey (Ctrl-E) often
                                        # used for editing in Safari.
                                        if( in_array( $action, array( 'edit', 'submit' ) )
                                        && in_array( $key, array( 'edit', 'watch', 'unwatch' ))) {
                                                echo Linker::tooltip( "ca-$key" );
                                        } else {
                                                echo Linker::tooltipAndAccesskeyAttribs( "ca-$key" );
                                        }
                                        echo '>'.htmlspecialchars($tab['text']).'</a></li>';
                                } ?>

                        </ul>
                </div>
        </div>
        <div class="portlet" id="p-personal">
                <h5><?php $this->msg('personaltools') ?></h5>
                <div class="pBody">
                        <ul<?php $this->html('userlangattributes') ?>>
<?php                   foreach($this->data['personal_urls'] as $key => $item) { ?>
                                <li id="<?php echo Sanitizer::escapeId( "pt-$key" ) ?>"<?php
                                        if ($item['active']) { ?> class="active"<?php } ?>><a href="<?php
                                echo htmlspecialchars($item['href']) ?>"<?php echo Linker::tooltipAndAccesskeyAttribs('pt-'.$key) ?><?php
                                if(!empty($item['class'])) { ?> class="<?php
                                echo htmlspecialchars($item['class']) ?>"<?php } ?>><?php
                                echo htmlspecialchars($item['text']) ?></a></li>
<?php                   } ?>
                        </ul>
                </div>
        </div>
        <div class="portlet" id="p-logo">
                <a style="background-image: url(<?php $this->text('logopath') ?>);" <?php
                        ?>href="<?php echo htmlspecialchars($this->data['nav_urls']['mainpage']['href'])?>"<?php
                        echo Linker::tooltipAndAccesskeyAttribs('p-logo') ?>></a>
        </div>
        <script type="<?php $this->text('jsmimetype') ?>"> if (window.isMSIE55) fixalpha(); </script>

</div>--><!-- end of the left (by default at least) column -->
<div class="visualClear"></div>
<div id="footer"<?php $this->html('userlangattributes') ?>>
<?php
if($this->data['poweredbyico']) { ?>
        <div id="f-poweredbyico"><?php $this->html('poweredbyico') ?></div>
<?php }
if($this->data['copyrightico']) { ?>
        <div id="f-copyrightico"><?php $this->html('copyrightico') ?></div>
<?php }

                // Generate additional footer links
                $footerlinks = array(
                        'lastmod', 'viewcount', 'numberofwatchingusers', 'credits', 'copyright',
                        'privacy', 'about', 'disclaimer', 'tagline',
                );
                $validFooterLinks = array();
                foreach( $footerlinks as $aLink ) {
                        if( isset( $this->data[$aLink] ) && $this->data[$aLink] ) {
                                $validFooterLinks[] = $aLink;
                        }
                }
                if ( count( $validFooterLinks ) > 0 ) {
?>      <ul id="f-list">
<?php
                        foreach( $validFooterLinks as $aLink ) {
                                if( isset( $this->data[$aLink] ) && $this->data[$aLink] ) {
?>              <li id="<?php echo $aLink ?>"><?php $this->html($aLink) ?></li>
<?php                   }
                        }
?>
        </ul>
<?php   }
?>
</div>
</div>
<?php $this->html('bottomscripts'); /* JS call to runBodyOnloadHook */ ?>
<?php $this->html('reporttime') ?>
<?php if ( $this->data['debug'] ): ?>
<!-- Debug output:
<?php $this->text( 'debug' ); ?>

-->
<?php endif; ?>
</body></html>
<?php
        wfRestoreWarnings();
        } // end of execute() method

        /*************************************************************************************************/
        function searchBox() {
                global $wgUseTwoButtonsSearchForm;
?>
        <?php begin_box("Search Wiki"); ?>
        <div id="p-search" class="portlet">
                <div id="searchBody" class="pBody">
                        <form action="<?php $this->text('wgScript') ?>" id="searchform">
                                <input type='hidden' name="title" value="<?php $this->text('searchtitle') ?>"/>
                                <?php
                echo Html::input( 'search',
                        isset( $this->data['search'] ) ? $this->data['search'] : '', 'search',
                        array(
                                'id' => 'searchInput',
                                'title' => Linker::titleAttrib( 'search' ),
                                'accesskey' => Linker::accesskey( 'search' )
                        ) ); ?>

                                <input type='submit' name="go" class="searchButton" id="searchGoButton" value="<?php $this->msg('searcharticle') ?>"<?php echo Linker::tooltipAndAccesskeyAttribs( 'search-go' ); ?> /><?php if ($wgUseTwoButtonsSearchForm) { ?>&nbsp;
                                <input type='submit' name="fulltext" class="searchButton" id="mw-searchButton" value="<?php $this->msg('searchbutton') ?>"<?php echo Linker::tooltipAndAccesskeyAttribs( 'search-fulltext' ); ?> /><?php } else { ?>

                                <div><a href="<?php $this->text('searchaction') ?>" rel="search"><?php $this->msg('powersearch-legend') ?></a></div><?php } ?>

                        </form>
                </div>
        </div>
    <?php end_box(); ?>
<?php
        }

        /*************************************************************************************************/
        function toolbox() {
?>
        <?php begin_box("Toolbox"); ?>
        <div class="portlet" id="p-tb">
                <div class="pBody">
                        <ul>
<?php
                if($this->data['notspecialpage']) { ?>
                                <li id="t-whatlinkshere"><a href="<?php
                                echo htmlspecialchars($this->data['nav_urls']['whatlinkshere']['href'])
                                ?>"<?php echo Linker::tooltipAndAccesskeyAttribs('t-whatlinkshere') ?>><?php $this->msg('whatlinkshere') ?></a></li>
<?php
                        if( $this->data['nav_urls']['recentchangeslinked'] ) { ?>
                                <li id="t-recentchangeslinked"><a href="<?php
                                echo htmlspecialchars($this->data['nav_urls']['recentchangeslinked']['href'])
                                ?>"<?php echo Linker::tooltipAndAccesskeyAttribs('t-recentchangeslinked') ?>><?php $this->msg('recentchangeslinked-toolbox') ?></a></li>
<?php           }
                }
                if( isset( $this->data['nav_urls']['trackbacklink'] ) && $this->data['nav_urls']['trackbacklink'] ) { ?>
                        <li id="t-trackbacklink"><a href="<?php
                                echo htmlspecialchars($this->data['nav_urls']['trackbacklink']['href'])
                                ?>"<?php echo Linker::tooltipAndAccesskeyAttribs('t-trackbacklink') ?>><?php $this->msg('trackbacklink') ?></a></li>
<?php   }
                if($this->data['feeds']) { ?>
                        <li id="feedlinks"><?php foreach($this->data['feeds'] as $key => $feed) {
                                        ?><a id="<?php echo Sanitizer::escapeId( "feed-$key" ) ?>" href="<?php
                                        echo htmlspecialchars($feed['href']) ?>" rel="alternate" type="application/<?php echo $key ?>+xml" class="feedlink"<?php echo Linker::tooltipAndAccesskeyAttribs('feed-'.$key) ?>><?php echo htmlspecialchars($feed['text'])?></a>&nbsp;
                                        <?php } ?></li><?php
                }

                foreach( array('contributions', 'log', 'blockip', 'emailuser', 'upload', 'specialpages') as $special ) {

                        if($this->data['nav_urls'][$special]) {
                                ?><li id="t-<?php echo $special ?>"><a href="<?php echo htmlspecialchars($this->data['nav_urls'][$special]['href'])
                                ?>"<?php echo Linker::tooltipAndAccesskeyAttribs('t-'.$special) ?>><?php $this->msg($special) ?></a></li>
<?php           }
                }

                if(!empty($this->data['nav_urls']['print']['href'])) { ?>
                                <li id="t-print"><a href="<?php echo htmlspecialchars($this->data['nav_urls']['print']['href'])
                                ?>" rel="alternate"<?php echo Linker::tooltipAndAccesskeyAttribs('t-print') ?>><?php $this->msg('printableversion') ?></a></li><?php
                }

                if(!empty($this->data['nav_urls']['permalink']['href'])) { ?>
                                <li id="t-permalink"><a href="<?php echo htmlspecialchars($this->data['nav_urls']['permalink']['href'])
                                ?>"<?php echo Linker::tooltipAndAccesskeyAttribs('t-permalink') ?>><?php $this->msg('permalink') ?></a></li><?php
                } elseif ($this->data['nav_urls']['permalink']['href'] === '') { ?>
                                <li id="t-ispermalink"<?php echo Linker::tooltip('t-ispermalink') ?>><?php $this->msg('permalink') ?></li><?php
                }

                Hooks::run( 'GCL3TemplateToolboxEnd', array( &$this ) );
                Hooks::run( 'SkinTemplateToolboxEnd', array( &$this ) );
?>
                        </ul>
                </div>
        </div>
    <?php end_box(); ?>
<?php
        }

        /*************************************************************************************************/
        function languageBox() {
                if( $this->data['language_urls'] ) {
?>
        <?php begin_box("Languages"); ?>
        <div id="p-lang" class="portlet">
                <div class="pBody">
                        <ul>
<?php           foreach($this->data['language_urls'] as $langlink) { ?>
                                <li class="<?php echo htmlspecialchars($langlink['class'])?>"><?php
                                ?><a href="<?php echo htmlspecialchars($langlink['href']) ?>"><?php echo $langlink['text'] ?></a></li>
<?php           } ?>
                        </ul>
                </div>
        </div>
    <?php end_box(); ?>
<?php
                }
        }

        /*************************************************************************************************/
        function customBox( $bar, $cont ) {
?>
    <?php begin_box($bar); ?>
        <div class='generated-sidebar portlet' id='<?php echo Sanitizer::escapeId( "p-$bar" ) ?>'<?php echo Linker::tooltip('p-'.$bar) ?>>
                <div class='pBody'>
<?php   if ( is_array( $cont ) ) { ?>
                        <ul>
<?php                   foreach($cont as $key => $val) { ?>
                                <li id="<?php echo Sanitizer::escapeId($val['id']) ?>"<?php
                                        if ( $val['active'] ) { ?> class="active" <?php }
                                ?>><a href="<?php echo htmlspecialchars($val['href']) ?>"<?php echo Linker::tooltipAndAccesskeyAttribs($val['id']) ?>><?php echo htmlspecialchars($val['text']) ?></a></li>
<?php                   } ?>
                        </ul>
<?php   } else {
                        # allow raw HTML block to be defined by extensions
                        print $cont;
                }
?>
                </div>
        </div>
    <?php end_box(); ?>
<?php
        }
} // end of class

function begin_box($title) {
global $theme;
echo <<<HERE
<table cellspacing="1" cellpadding="4" border="0" width="100%" align="center" class="bordercolor">
<tr>
<td class="catbg"><strong>$title</strong></td>
</tr>
<tr>
<td class="windowbg2">
HERE;
}

function begin_box2($temp,$text,$sub="") {
global $theme;
echo <<<HERE
<table cellspacing="1" cellpadding="4" border="0" width="100%" align="center"  class="bordercolor">
<tr>
<td class="catbg"><strong>
HERE;
$temp->text($text);
echo <<<HERE
</strong></td>
</tr>
HERE;
if($sub) echo '<tr><td class="titlebg">&nbsp;'.$sub.'</td></tr>';
echo <<<HERE
<tr>
<td class="windowbg2">
HERE;
}

function begin_box3($temp,$text,$sub="") {
global $theme;
echo <<<HERE
<table cellspacing="1" cellpadding="4" border="0" width="100%" align="center"  class="bordercolor">
<tr>
<td class="catbg"><strong>$text</strong></td>
</tr>
HERE;
if($sub) echo '<tr><td class="titlebg">&nbsp;'.$sub.'</td></tr>';
echo <<<HERE
<tr>
<td class="windowbg2">
HERE;
}

function end_box() {
echo <<<THERE
</td></tr></table><br />
THERE;
}
