From a27f92a17ae12d2d1ff48b26aadfecd7c221c589 Mon Sep 17 00:00:00 2001
From: Frank Karlitschek <frank@owncloud.org>
Date: Mon, 21 Jan 2013 20:40:23 +0100
Subject: [PATCH] we are getting closer. updating is not working yet.

---
 lib/app.php                 | 16 +++++-
 lib/installer.php           | 99 ++++++++++++++++++++++++++++++++++---
 lib/ocsclient.php           |  2 +
 settings/ajax/apps/ocs.php  |  1 +
 settings/ajax/updateapp.php | 17 +++++++
 settings/apps.php           |  5 +-
 settings/js/apps.js         | 30 +++++++++++
 settings/routes.php         |  2 +
 settings/templates/apps.php |  4 +-
 9 files changed, 167 insertions(+), 9 deletions(-)
 create mode 100644 settings/ajax/updateapp.php

diff --git a/lib/app.php b/lib/app.php
index e60bce2a20..13f54d6cf7 100644
--- a/lib/app.php
+++ b/lib/app.php
@@ -142,6 +142,8 @@ class OC_App{
 	 * check if app is shipped
 	 * @param string $appid the id of the app to check
 	 * @return bool
+	 *
+	 * Check if an app that is installed is a shipped app or installed from the appstore.
 	 */
 	public static function isShipped($appid){
 		$info = self::getAppInfo($appid);
@@ -197,9 +199,10 @@ class OC_App{
 			if(!is_numeric($app)) {
 				$app = OC_Installer::installShippedApp($app);
 			}else{
+				$appdata=OC_OCSClient::getApplication($app);
 				$download=OC_OCSClient::getApplicationDownload($app, 1);
 				if(isset($download['downloadlink']) and $download['downloadlink']!='') {
-					$app=OC_Installer::installApp(array('source'=>'http', 'href'=>$download['downloadlink']));
+					$app=OC_Installer::installApp(array('source'=>'http', 'href'=>$download['downloadlink'],'appdata'=>$appdata));
 				}
 			}
 		}
@@ -212,6 +215,7 @@ class OC_App{
 				return false;
 			}else{
 				OC_Appconfig::setValue( $app, 'enabled', 'yes' );
+				if(isset($appdata['id'])) OC_Appconfig::setValue( $app, 'ocsid', $appdata['id'] );
 				return true;
 			}
 		}else{
@@ -229,6 +233,14 @@ class OC_App{
 	public static function disable( $app ) {
 		// check if app is a shiped app or not. if not delete
 		OC_Appconfig::setValue( $app, 'enabled', 'no' );
+
+		// check if app is a shiped app or not. if not delete
+		if(!OC_App::isShipped( $app )){
+//			error_log($app.' not shipped');	
+			OC_Installer::removeApp( $app );
+		}else{
+//			error_log($app.' shipped');	
+		}		
 	}
 
 	/**
@@ -609,6 +621,8 @@ class OC_App{
 				$app1[$i]['author'] = $app['personid'];
 				$app1[$i]['ocs_id'] = $app['id'];
 				$app1[$i]['internal'] = $app1[$i]['active'] = 0;
+				$app1[$i]['update'] = false;
+
 
 				// rating img
 				if($app['score']>=0     and $app['score']<5)	$img=OC_Helper::imagePath( "core", "rating/s1.png" );
diff --git a/lib/installer.php b/lib/installer.php
index 7dc8b0cef8..f4094a5d4c 100644
--- a/lib/installer.php
+++ b/lib/installer.php
@@ -141,6 +141,20 @@ class OC_Installer{
 			return false;
 		}
 
+		// check if shipped tag is set which is only allowed for apps that are shipped with ownCloud
+		if(isset($info['shipped']) and ($info['shipped']=='true')) {
+			OC_Log::write('core', 'App can\'t be installed because it contains the <shipped>true</shippe> tag which is not allowed for non shipped apps', OC_Log::ERROR);
+			OC_Helper::rmdirr($extractDir);
+			return false;
+		}
+
+		// check if the ocs version is the same as the version in info.xml/version
+		if(!isset($info['version']) or ($info['version']<>$data['appdata']['version'])) {
+			OC_Log::write('core', 'App can\'t be installed because the version in info.xml/version is not the same as the version reported from the app store', OC_Log::ERROR);
+			OC_Helper::rmdirr($extractDir);
+			return false;
+		}
+
 		//check if an app with the same id is already installed
 		if(self::isInstalled( $info['id'] )) {
 			OC_Log::write('core', 'App already installed', OC_Log::WARN);
@@ -226,7 +240,6 @@ class OC_Installer{
 	/**
 	 * @brief Update an application
 	 * @param $data array with all information
-	 * @returns integer
 	 *
 	 * This function installs an app. All information needed are passed in the
 	 * associative array $data.
@@ -250,11 +263,57 @@ class OC_Installer{
 	 *
 	 * upgrade.php can determine the current installed version of the app using "OC_Appconfig::getValue($appid, 'installed_version')"
 	 */
-	public static function upgradeApp( $data = array()) {
-		// TODO: write function
-		return true;
+	public static function updateApp( $app ) {
+		error_log('updater!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
+		return(true);
+        	if(OC_Installer::isDownloaded( $name )) { 
+		}
 	}
 
+        /**
+         * @brief Check if an update for the app is available
+         * @param $name name of the application
+         * @returns emptry string is no update available or the version number of the update
+         *
+         * The function will check if an update for a version is available
+         */
+        public static function isUpdateAvailable( $app ) {
+		//debug
+		return('1.1');
+
+		$ocsid=OC_Appconfig::getValue( $app, 'ocsid', '');
+
+		if($ocsid<>''){
+		
+			$ocsdata=OC_OCSClient::getApplication($ocsid);
+			$ocsversion=$ocsdata['version'];
+			$currentversion=OC_App::getAppVersion($app);
+
+//error_log('bb'.$app.' '.$ocsversion);
+			return($ocsversion);
+
+		}else{
+			return('');
+		}
+
+        }
+
+        /**
+         * @brief Check if app is already downloaded
+         * @param $name name of the application to remove
+         * @returns true/false
+         *
+         * The function will check if the app is already downloaded in the apps repository
+         */
+        public static function isDownloaded( $name ) {
+
+		$downloaded=false;
+		foreach(OC::$APPSROOTS as $dir) {
+                	if(is_dir($dir['path'].'/'.$name)) $downloaded=true;
+		}
+		return($downloaded);
+        }
+
 	/**
 	 * @brief Removes an app
 	 * @param $name name of the application to remove
@@ -276,8 +335,36 @@ class OC_Installer{
 	 * this has to be done by the function oc_app_uninstall().
 	 */
 	public static function removeApp( $name, $options = array()) {
-		// TODO: write function
-		return true;
+
+		if(isset($options['keeppreferences']) and $options['keeppreferences']==false ){
+			// todo
+			// remove preferences
+		}
+
+		if(isset($options['keepappconfig']) and $options['keepappconfig']==false ){
+			// todo
+			// remove app config
+		}
+
+		if(isset($options['keeptables']) and $options['keeptables']==false ){
+			// todo
+			// remove app database tables
+		}
+
+		if(isset($options['keepfiles']) and $options['keepfiles']==false ){
+			// todo
+			// remove user files
+		}
+
+        	if(OC_Installer::isDownloaded( $name )) { 
+			$appdir=OC_App::getInstallPath().'/'.$name;
+			OC_Helper::rmdirr($appdir);
+
+		}else{
+			OC_Log::write('core', 'can\'t remove app '.$name.'. It is not installed.', OC_Log::ERROR);
+
+		}
+
 	}
 
 	/**
diff --git a/lib/ocsclient.php b/lib/ocsclient.php
index 24081425f1..3693078877 100644
--- a/lib/ocsclient.php
+++ b/lib/ocsclient.php
@@ -123,6 +123,7 @@ class OC_OCSClient{
 			$app=array();
 			$app['id']=(string)$tmp[$i]->id;
 			$app['name']=(string)$tmp[$i]->name;
+			$app['version']=(string)$tmp[$i]->version;
 			$app['type']=(string)$tmp[$i]->typeid;
 			$app['typename']=(string)$tmp[$i]->typename;
 			$app['personid']=(string)$tmp[$i]->personid;
@@ -162,6 +163,7 @@ class OC_OCSClient{
 		$app=array();
 		$app['id']=$tmp->id;
 		$app['name']=$tmp->name;
+		$app['version']=$tmp->version;
 		$app['type']=$tmp->typeid;
 		$app['typename']=$tmp->typename;
 		$app['personid']=$tmp->personid;
diff --git a/settings/ajax/apps/ocs.php b/settings/ajax/apps/ocs.php
index 1ffba26ad1..6e09785d23 100644
--- a/settings/ajax/apps/ocs.php
+++ b/settings/ajax/apps/ocs.php
@@ -54,6 +54,7 @@ if(is_array($catagoryNames)) {
 				'preview'=>$pre,
 				'internal'=>false,
 				'internallabel'=>'3rd Party App',
+				'update'=>false,
 			);
 		}
 	}
diff --git a/settings/ajax/updateapp.php b/settings/ajax/updateapp.php
new file mode 100644
index 0000000000..68c2bbf7f0
--- /dev/null
+++ b/settings/ajax/updateapp.php
@@ -0,0 +1,17 @@
+<?php
+
+OC_JSON::checkAdminUser();
+OCP\JSON::callCheck();
+
+$appid = $_POST['appid'];
+
+$result = OC_Installer::updateApp($appid);
+if($result !== false) {
+	OC_JSON::success(array('data' => array('appid' => $appid)));
+} else {
+	$l = OC_L10N::get('settings');	
+	OC_JSON::error(array("data" => array( "message" => $l->t("Could update app. ") )));
+}
+
+
+
diff --git a/settings/apps.php b/settings/apps.php
index 99a3094399..a2015801e5 100644
--- a/settings/apps.php
+++ b/settings/apps.php
@@ -68,13 +68,16 @@ foreach ( $installedApps as $app ) {
 			$info['internal']=true;
 			
 			$info['internallabel']='Internal App';
+
+			$info['update']=false;
 		
 		}else{
 		
 			$info['internal']=false;
 			
 			$info['internallabel']='3rd Party App';
-		
+
+			$info['update']=OC_Installer::isUpdateAvailable($app);
 		}
 		
 		$info['preview'] = OC_Helper::imagePath('settings', 'trans.png');
diff --git a/settings/js/apps.js b/settings/js/apps.js
index c4c36b4bb1..8d214bd114 100644
--- a/settings/js/apps.js
+++ b/settings/js/apps.js
@@ -24,6 +24,14 @@ OC.Settings.Apps = OC.Settings.Apps || {
 		page.find('span.author').text(app.author);
 		page.find('span.licence').text(app.licence);
 
+		if (app.update != false) {
+			page.find('input.update').show();
+			page.find('input.update').data('appid', app.id);
+			page.find('input.update').attr('value',t('settings', 'Update to ')+app.update);
+		} else {
+			page.find('input.update').hide();
+		}
+
 		page.find('input.enable').show();
 		page.find('input.enable').val((app.active) ? t('settings', 'Disable') : t('settings', 'Enable'));
 		page.find('input.enable').data('appid', app.id);
@@ -44,6 +52,7 @@ OC.Settings.Apps = OC.Settings.Apps || {
 		appData = appitem.data('app');
 		appData.active = !active;
 		appitem.data('app', appData);
+		element.val(t('settings','Please wait....'));
 		if(active) {
 			$.post(OC.filePath('settings','ajax','disableapp.php'),{appid:appid},function(result) {
 				if(!result || result.status!='success') {
@@ -70,6 +79,20 @@ OC.Settings.Apps = OC.Settings.Apps || {
 			$('#leftcontent li[data-id="'+appid+'"]').addClass('active');
 		}
 	},
+	updateApp:function(appid, element) {
+		console.log('updateApp:', appid, element);
+		element.val(t('settings','Updateing....'));
+		$.post(OC.filePath('settings','ajax','updateapp.php'),{appid:appid},function(result) {
+			if(!result || result.status!='success') {
+				OC.dialogs.alert('Error while updating app','Error');
+			}
+			else {
+				element.val(t('settings','Updated'));
+				element.hide();
+			}
+		},'json');
+	},
+
 	insertApp:function(appdata) {
 		var applist = $('#leftcontent li');
 		var app =
@@ -154,6 +177,13 @@ $(document).ready(function(){
 			OC.Settings.Apps.enableApp(appid, active, element);
 		}
 	});
+	$('#rightcontent input.update').click(function(){
+		var element = $(this);
+		var appid=$(this).data('appid');
+		if(appid) {
+			OC.Settings.Apps.updateApp(appid, element);
+		}
+	});
 
 	if(appid) {
 		var item = $('#leftcontent li[data-id="'+appid+'"]');
diff --git a/settings/routes.php b/settings/routes.php
index 9b5bf80923..fa78f56652 100644
--- a/settings/routes.php
+++ b/settings/routes.php
@@ -51,6 +51,8 @@ $this->create('settings_ajax_enableapp', '/settings/ajax/enableapp.php')
 	->actionInclude('settings/ajax/enableapp.php');
 $this->create('settings_ajax_disableapp', '/settings/ajax/disableapp.php')
 	->actionInclude('settings/ajax/disableapp.php');
+$this->create('settings_ajax_updateapp', '/settings/ajax/updateapp.php')
+	->actionInclude('settings/ajax/updateapp.php');
 $this->create('settings_ajax_navigationdetect', '/settings/ajax/navigationdetect.php')
 	->actionInclude('settings/ajax/navigationdetect.php');
 // admin
diff --git a/settings/templates/apps.php b/settings/templates/apps.php
index 179ce9c540..8654547ecb 100644
--- a/settings/templates/apps.php
+++ b/settings/templates/apps.php
@@ -7,7 +7,7 @@
 	var appid = '<?php echo $_['appid']; ?>';
 </script>
 <div id="controls">
-	<a class="button" target="_blank" href="http://owncloud.org/dev/apps/getting-started/"><?php echo $l->t('Add your App');?></a>
+	<a class="button" target="_blank" href="http://owncloud.org/dev"><?php echo $l->t('Add your App');?></a>
 	<a class="button" target="_blank" href="http://apps.owncloud.com"><?php echo $l->t('More Apps');?></a>
 </div>
 <ul id="leftcontent" class="applist">
@@ -31,5 +31,7 @@
 	<p class="appslink hidden"><a href="#" target="_blank"><?php echo $l->t('See application page at apps.owncloud.com');?></a></p>
     <p class="license hidden"><?php echo $l->t('<span class="licence"></span>-licensed by <span class="author"></span>');?></p>
 	<input class="enable hidden" type="submit" />
+	<?php //if(isset($app['update']) ) echo('<input class="update " type="submit" value="'.$l->t('Update').' '.$app['update'].'" />');  ?>
+	<input class="update hidden" type="submit" value="<?php echo($l->t('Update')); ?>" />
 	</div>
 </div>
-- 
GitLab