<?php

defined('_JEXEC') or die('Restricted access');
jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');	

class Status {
	var $STATUS_FAIL = 'Failed';
	var $STATUS_SUCCESS = 'Success';
	var $infomsg = array();
	var $errmsg = array();
	var $status;
}

$database = JFactory::getDBO();

$plg_return = gmplugin_uninstall($this, "plugmaccess", "system", $database);
$install_status['plugin_gmaccess'] = $plg_return;
if ($plg_return->status == $plg_return->STATUS_FAIL) {
	return false;
}
	$admingmajax="";
	$sitegmajax="";
	if (JFolder::exists(JPATH_SITE.DS."administrator/components/com_gmajax")){
		JFolder::delete(JPATH_SITE.DS."administrator/components/com_gmajax");
		$admingmajax="REUSSI";
	}
	if (JFolder::exists(JPATH_SITE.DS."components/com_gmajax")){
		JFolder::delete(JPATH_SITE.DS."components/com_gmajax");
		$sitegmajax="REUSSI";
	}
	if (JFolder::exists(JPATH_SITE.DS."administrator/gmbackup/administrator")){
		$restoreadministrator = restore(JPATH_SITE.DS."administrator/gmbackup/administrator", JPATH_SITE.DS."administrator");
	}
	if (JFolder::exists(JPATH_SITE.DS."administrator/gmbackup/components")){
		$restorecomponents = restore(JPATH_SITE.DS."administrator/gmbackup/components", JPATH_SITE.DS."components");
	}
	if (JFolder::exists(JPATH_SITE.DS."administrator/gmbackup/language")){
		$restorelanguage = restore(JPATH_SITE.DS."administrator/gmbackup/language", JPATH_SITE.DS."language");
	}
	if (JFolder::exists(JPATH_SITE.DS."administrator/gmbackup/libraries")){
		$restorelibraries = restore(JPATH_SITE.DS."administrator/gmbackup/libraries", JPATH_SITE.DS."libraries");
	}
	if (JFolder::exists(JPATH_SITE.DS."administrator/gmbackup/modules")){
		$restoremodules = restore(JPATH_SITE.DS."administrator/gmbackup/modules", JPATH_SITE.DS."modules");
	}
	if (JFolder::exists(JPATH_SITE.DS."administrator/gmbackup/plugins")){
		$restoreplugins = restore(JPATH_SITE.DS."administrator/gmbackup/plugins", JPATH_SITE.DS."plugins");
	}
	if (JFolder::exists(JPATH_SITE.DS."administrator/gmbackup/templates")){
		$restoretemplates = restore(JPATH_SITE.DS."administrator/gmbackup/templates", JPATH_SITE.DS."templates");
	}
	if (JFolder::exists(JPATH_SITE.DS."administrator/gmbackup")){
			JFolder::delete(JPATH_SITE.DS."administrator/gmbackup");
	}


function gmplugin_uninstall($installer, $pluginname, $group='sytem', $db) { 	
	$status = new Status();
	$status->status = $status->STATUS_FAIL;
	
	// Set the plugin root path
	$installer->parent->setPath('extension_root', JPATH_ROOT.DS.'plugins'.DS.$group);
	
	// Delete the module in the #__modules table
	$query = 'DELETE FROM #__plugins WHERE element = '.$db->Quote($pluginname).' AND folder = '.$db->Quote($group);
	$db->setQuery($query);
	if (!$db->query()) {
	    $status->errmsg[]=JText::_('Unable to delete from plugin table').': '.$db->stderr(true);
		return $status;
	}
	
	// Set the installation path
	$element =& $installer->manifest->getElementByPath($pluginname.'/files');
	if (is_a($element, 'JSimpleXMLElement') && count($element->children())) {
	    $installer->parent->removeFiles($element, -1);
	}
	// If the folder is empty, let's delete it
	$files = JFolder::files($installer->parent->getPath('extension_root'));
	if (!count($files)) {
	    JFolder::delete($installer->parent->getPath('extension_root'));
	}
	$status->status = $status->STATUS_SUCCESS;
	return $status;
}
function restore($src, $dest, $path = '', $force = false)
	{
		// Initialize variables
		jimport('joomla.client.helper');
		$FTPOptions = JClientHelper::getCredentials('ftp');

		if ($path) {
			$src = JPath::clean($path.DS.$src);
			$dest = JPath::clean($path.DS.$dest);
		}

		// Eliminate trailing directory separators, if any
		$src = rtrim($src, DS);
		$dest = rtrim($dest, DS);

		if (!JFolder::exists($src)) {
			return JError::raiseError(-1, JText::_('Cannot find source folder'));
		}
		/*if (JFolder::exists($dest) && !$force) {
			return JError::raiseError(-1, JText::_('Folder already exists'));
		}

		// Make sure the destination exists
		if (! JFolder::create($dest)) {
			return JError::raiseError(-1, JText::_('Unable to create target folder'));
		}*/

		if ($FTPOptions['enabled'] == 1)
		{
			// Connect the FTP client
			jimport('joomla.client.ftp');
			$ftp = & JFTP::getInstance($FTPOptions['host'], $FTPOptions['port'], null, $FTPOptions['user'], $FTPOptions['pass']);

			if(! ($dh = @opendir($src))) {
				return JError::raiseError(-1, JText::_('Unable to open source folder'));
			}
			// Walk through the directory copying files and recursing into folders.
			while (($file = readdir($dh)) !== false) {
				$sfid = $src . DS . $file;
				$dfid = $dest . DS . $file;
				switch (filetype($sfid)) {
					case 'dir':
						if ($file != '.' && $file != '..') {
							$ret = restore($sfid, $dfid, null, $force);
							if ($ret !== true) {
								return $ret;
							}
						}
						break;

					case 'file':
						//Translate path for the FTP account
						$dfid = JPath::clean(str_replace(JPATH_ROOT, $FTPOptions['root'], $dfid), '/');
						if (! $ftp->store($sfid, $dfid)) {
							return JError::raiseError(-1, JText::_('Copy failed'));
						}
						break;
				}
			}
		} else {
			if(! ($dh = @opendir($src))) {
				return JError::raiseError(-1, JText::_('Unable to open source folder'));
			}
			// Walk through the directory copying files and recursing into folders.
			while (($file = readdir($dh)) !== false) {
				$sfid = $src.DS.$file;
				$dfid = $dest.DS.$file;

				switch (filetype($sfid)) {
					case 'dir':
						if ($file != '.' && $file != '..') {
							$ret = restore($sfid, $dfid, null, $force);
							if ($ret !== true) {
								return $ret;
							}
						}
						break;

					case 'file':
						if (!@ copy($sfid, $dfid)) {
							return JError::raiseError(-1, JText::_('Copy failed'));
						}
						break;
				}
			}
		}
		return true;
	}
?>
<h1>GMAccess D&eacute;sinstallation</h1>
<table class="adminlist">
	<thead>
		<tr>
			<th class="title"><?php echo JText::_('Sous-composants'); ?></th>
			<th width="60%"><?php echo JText::_('Status'); ?></th>
		</tr>
	</thead>
	<tfoot>
		<tr>
			<td colspan="2">&nbsp;</td>
		</tr>
	</tfoot>
	<tbody>
	<?php
		$i=0;
		foreach ( $install_status as $component => $status ) {?>
		<tr class="row<?php echo $i; ?>">
			<td class="key"><?php echo $component; ?></td>
			<td>D&eacute;sinstallation : 
				<?php echo ($status->status == $status->STATUS_SUCCESS)? ' <strong><font color="#00FF00">'.JText::_('REUSSI').'</strong>' : ' <em><font color="#FF0000">'.JText::_('ECHEC').'</font></em>'?>
				<?php if (count($status->errmsg) > 0 ) {
						foreach ( $status->errmsg as $errmsg ) {
       						echo '<br/>Error: ' . $errmsg;
						}
				} ?>
				<?php if (count($status->infomsg) > 0 ) {
						foreach ( $status->infomsg as $infomsg ) {
       						echo '<br/>Info: ' . $infomsg;
						}
				} ?>
			</td>
		</tr>	
	<?php
			if ($i=0){ $i=1;} else {$i = 0;}; 
		}?>
		<tr class="row<?php echo $i; ?>">
			<td class="key">Admin com_gmajax</td>
			<td>D&eacute;sinstallation : 
				<?php echo ($admingmajax)? ' <strong><font color="#00FF00">'.JText::_('REUSSI').'</strong>' : ' <em><font color="#FF0000">'.JText::_('ECHEC').'</font></em>'?>
			</td>
		</tr>
        <?php
			if ($i=0){ $i=1;} else {$i = 0;}; 
		?>
		<tr class="row<?php echo $i; ?>">
			<td class="key">Site com_gmajax</td>
			<td>D&eacute;sinstallation : 
				<?php echo ($sitegmajax)? ' <strong><font color="#00FF00">'.JText::_('REUSSI').'</strong>' : ' <em><font color="#FF0000">'.JText::_('ECHEC').'</font></em>'?>
			</td>
		</tr>
      
          <?php
			if ($i=0){ $i=1;} else {$i = 0;};
			if ( $restoreadministrator && $restorecomponents && $restorelanguage && $restorelibraries && $restoremodules && $restoreplugins && $restoretemplates ) {
		?>
		<tr class="row<?php echo $i; ?>">
			<td class="key">Restauration fichiers joomla</td>
			<td>Restauration : 
				<?php echo ' <strong><font color="#00FF00">'.JText::_('REUSSI').'</strong>'?>
			</td>
		</tr>
           <?php
			} else {
		?>
		<tr class="row<?php echo $i; ?>">
			<td class="key">Restauration fichiers joomla</td>
			<td>Restauration : 
				<?php echo ' <em><font color="#FF0000">'.JText::_('ECHEC').'</font></em>'?>
			</td>
		</tr>
        <?php
			}
		?>		
	</tbody>
</table>
