/**
 * displays a pane slider with all dated extensions and update information
 *
 * @param   object instance of JPane to use
 * @param   array dated extensions provided by Joom_CheckUpdate
 */
function Joom_ShowDatedExtensions($pane, $extensions = null)
{
    if (is_null($extensions)) {
        $extensions = Joom_CheckUpdate();
    }
    $entries = count($extensions);
    if (!$entries) {
        return;
    }
    echo $pane->startPanel(JText::_('JGA_UPDATECHECK_TITLE'), 'cpanel-panel-joom_update');
    $entry = 1;
    foreach ($extensions as $name => $extension) {
        ?>
  <div style="padding:10px;">
    <div style="color:red;"><?php 
        echo sprintf(JText::_('JGA_EXTENSION_NOT_UPTODATE'), '<span style="font-weight:bold;">' . $name . '</span>');
        ?>
</div>
    <div style="margin-left:10px;">
      <table>
        <tr>
          <td><?php 
        echo JText::_('JGA_YOUR_VERSION');
        ?>
</td>
          <td><?php 
        echo $extension['installed_version'];
        ?>
</td>
        </tr>
        <tr>
          <td><?php 
        echo JText::_('JGA_CURRENT_VERSION');
        ?>
</td>
          <td><?php 
        echo $extension['version'];
        ?>
</td>
<?php 
        if (isset($extension['releaselink'])) {
            ?>
          <td><a href="<?php 
            echo $extension['releaselink'];
            ?>
" target="_blank"><?php 
            echo JText::_('JGA_MORE_INFO_LINK');
            ?>
</a></td>
<?php 
        }
        ?>
        </tr>
      </table>
    </div>
<?php 
        if (isset($extension['downloadlink'])) {
            ?>
    <div style="font-weight:bold;"><a href="<?php 
            echo $extension['downloadlink'];
            ?>
" target="_blank"><?php 
            echo JText::_('JGA_DOWNLOAD_UPDATE_LINK');
            ?>
</a></div>
<?php 
        }
        if (isset($extension['updatelink'])) {
            //check which method we will use to get the update zip
            if (@ini_get('allow_url_fopen')) {
                ?>
    <div style="margin:5px 0px;"><?php 
                echo JText::_('JGA_AUTOUPDATE_TEXT');
                ?>
      <form enctype="multipart/form-data" action="index.php" method="post" name="JoomUpdateForm<?php 
                echo $entry;
                ?>
">
        <input type="hidden" name="installtype" value="url" />
        <input type="hidden" name="install_url" value="<?php 
                echo $extension['updatelink'];
                ?>
" />
        <input type="hidden" name="task" value="doInstall" />
        <input type="hidden" name="option" value="com_installer" />
        <?php 
                echo JHTML::_('form.token');
                ?>
      </form>
    </div>
    <div style="font-weight:bold;">
      <a href="javascript:document.JoomUpdateForm<?php 
                echo $entry;
                ?>
.submit();"><?php 
                echo JText::_('JGA_AUTOUPDATE_LINK');
                ?>
</a></div>
<?php 
            } else {
                if (extension_loaded('curl')) {
                    ?>
    <div style="margin:5px 0px;"><?php 
                    echo JText::_('JGA_AUTOUPDATE_TEXT');
                    ?>
</div>
    <div style="font-weight:bold;">
      <a href="index.php?option=<?php 
                    echo _JOOM_OPTION;
                    ?>
&amp;task=autoupdate&amp;extension=<?php 
                    echo $name;
                    ?>
"><?php 
                    echo JText::_('JGA_AUTOUPDATE_LINK');
                    ?>
</a></div>
<?php 
                } else {
                    ?>
    <div style="margin:5px 0px;"><?php 
                    echo JText::_('JGA_AUTOUPDATE_NOT_POSSIBLE');
                    ?>
</div>
<?php 
                }
            }
        }
        if ($entry < $entries) {
            $entry++;
            ?>
    <hr />
<?php 
        }
        ?>
  </div>
<?php 
    }
    echo $pane->endPanel();
}
/**
 * fetches an update zip file from JoomGallery server and extracts it
 */
function Joom_AutoUpdate()
{
    $extension = Joom_mosGetParam('extension', 0, 'get');
    $extensions = Joom_CheckUpdate();
    if (!isset($extensions[$extension]['updatelink']) || !extension_loaded('curl')) {
        $mainframe =& JFactory::getApplication('administrator');
        $mainframe->redirect('index.php?option=' . _JOOM_OPTION, 'Could not fetch update zip', 'error');
    }
    //create curl resource
    $ch = curl_init($extensions[$extension]['updatelink']);
    //some settings for curl
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //create the zip file
    jimport('joomla.filesystem.file');
    $config = Joom_getConfig();
    $output = curl_exec($ch);
    JFile::write(JPath::clean(JPATH_ROOT . DS . $config->jg_pathtemp . 'update.zip'), $output);
    //close curl resource to free up system resources
    curl_close($ch);
    //extract the zip file
    include JPATH_ADMINISTRATOR . DS . 'includes' . DS . 'pcl' . DS . 'pclzip.lib.php';
    $zipfile = new PclZip(JPath::clean(JPATH_ROOT . DS . $config->jg_pathtemp . 'update.zip'));
    $folder = JPath::clean(JPATH_ROOT . DS . $config->jg_pathtemp . 'update');
    $zipfile->extract(PCLZIP_OPT_PATH, $folder);
    if ($zipfile->error_code != 1) {
        $mainframe =& JFactory::getApplication('administrator');
        $mainframe->redirect('index.php?option=' . _JOOM_OPTION, $zipfile->errorInfo(), 'error');
    }
    JError::raiseNotice('301', JText::_('JGA_REDIRECT_NOTE'));
    //let's ask Joomla! to do the rest
    ?>
  <form action="index.php" method="post" name="JoomUpdateForm">
    <input type="hidden" name="installtype" value="folder" />
    <input type="hidden" name="install_directory" value="<?php 
    echo $folder;
    ?>
" />
    <input type="hidden" name="task" value="doInstall" />
    <input type="hidden" name="option" value="com_installer" />
    <?php 
    echo JHTML::_('form.token');
    ?>
  </form>
  <script type="text/javascript">
    document.JoomUpdateForm.submit();
  </script>
<?php 
}