diff options
author | Georg Ehrke <developer@georgehrke.com> | 2014-05-21 12:14:10 +0200 |
---|---|---|
committer | Georg Ehrke <developer@georgehrke.com> | 2014-05-31 12:17:54 +0200 |
commit | 2bcfd8e084b27ed89cf6e62bc9ab2c681d5a8361 (patch) | |
tree | bff130f767536eca5892511e0de26bab317b9697 /lib | |
parent | b9f059bd3ee75247058bd5eb9908f6c33ffa08e2 (diff) | |
download | nextcloud-server-2bcfd8e084b27ed89cf6e62bc9ab2c681d5a8361.tar.gz nextcloud-server-2bcfd8e084b27ed89cf6e62bc9ab2c681d5a8361.zip |
make it possible to update shipped apps via the appstore
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/app.php | 250 | ||||
-rw-r--r-- | lib/private/installer.php | 282 |
2 files changed, 358 insertions, 174 deletions
diff --git a/lib/private/app.php b/lib/private/app.php index 2f55b54b328..e672df7b32a 100644 --- a/lib/private/app.php +++ b/lib/private/app.php @@ -3,8 +3,13 @@ * ownCloud * * @author Frank Karlitschek + * @copyright 2012 Frank Karlitschek <frank@owncloud.org> + * * @author Jakob Sack - * @copyright 2012 Frank Karlitschek frank@owncloud.org + * @copyright 2012 Jakob Sack <mail@jakobsack.de> + * + * @author Georg Ehrke + * @copyright 2014 Georg Ehrke <georg@ownCloud.com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE @@ -211,48 +216,50 @@ class OC_App{ */ public static function enable( $app ) { self::$enabledAppsCache = array(); // flush - if(!OC_Installer::isInstalled($app)) { - // check if app is a shipped app or not. OCS apps have an integer as id, shipped apps use a string - 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']!='') { - // Replace spaces in download link without encoding entire URL - $download['downloadlink'] = str_replace(' ', '%20', $download['downloadlink']); - $info = array('source'=>'http', 'href'=>$download['downloadlink'], 'appdata'=>$appdata); - $app=OC_Installer::installApp($info); - } - } + if (!OC_Installer::isInstalled($app)) { + $app = self::installApp($app); } - $l = OC_L10N::get('core'); - if($app!==false) { - // check if the app is compatible with this version of ownCloud - $info=OC_App::getAppInfo($app); - $version=OC_Util::getVersion(); - if(!isset($info['require']) or !self::isAppVersionCompatible($version, $info['require'])) { - throw new \Exception( - $l->t("App \"%s\" can't be installed because it is not compatible with this version of ownCloud.", - array($info['name']) - ) - ); - }else{ - OC_Appconfig::setValue( $app, 'enabled', 'yes' ); - if(isset($appdata['id'])) { - OC_Appconfig::setValue( $app, 'ocsid', $appdata['id'] ); - } - \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); - } - }else{ - throw new \Exception($l->t("No app name specified")); + + OC_Appconfig::setValue( $app, 'enabled', 'yes' ); + } + + /** + * @param string $app + * @return int + */ + public static function downloadApp($app) { + $appdata=OC_OCSClient::getApplication($app); + $download=OC_OCSClient::getApplicationDownload($app, 1); + if(isset($download['downloadlink']) and $download['downloadlink']!='') { + // Replace spaces in download link without encoding entire URL + $download['downloadlink'] = str_replace(' ', '%20', $download['downloadlink']); + $info = array('source'=>'http', 'href'=>$download['downloadlink'], 'appdata'=>$appdata); + $app=OC_Installer::installApp($info); } + return $app; + } + + /** + * @param string $app + * @return bool + */ + public static function removeApp($app) { + if (self::isShipped($app)) { + return false; + } + + $disable = self::disable($app); + if (!$disable) { + return false; + } + + return OC_Installer::removeApp($app); } /** * @brief disables an app * @param string $app app - * @return boolean|null + * @return null * * This function set an app as disabled in appconfig. */ @@ -261,11 +268,6 @@ class OC_App{ // check if app is a shipped app or not. if not delete \OC_Hook::emit('OC_App', 'pre_disable', array('app' => $app)); OC_Appconfig::setValue( $app, 'enabled', 'no' ); - - // check if app is a shipped app or not. if not delete - if(!OC_App::isShipped( $app )) { - OC_Installer::removeApp( $app ); - } } /** @@ -446,18 +448,50 @@ class OC_App{ } - protected static function findAppInDirectories($appid) { + /** + * search for an app in all app-directories + * @param $appId + * @return mixed (bool|string) + */ + protected static function findAppInDirectories($appId) { static $app_dir = array(); - if (isset($app_dir[$appid])) { - return $app_dir[$appid]; + + if (isset($app_dir[$appId])) { + return $app_dir[$appId]; } + + $possibleApps = array(); foreach(OC::$APPSROOTS as $dir) { - if(file_exists($dir['path'].'/'.$appid)) { - return $app_dir[$appid]=$dir; + if(file_exists($dir['path'].'/'.$appId)) { + $possibleApps[] = $dir; } } - return false; + + if (count($possibleApps) === 0) { + return false; + } elseif(count($possibleApps) === 1) { + reset($possibleApps); + $dir = current($possibleApps); + $app_dir[$appId] = $dir; + return $dir; + } else { + $versionToLoad = array(); + foreach($possibleApps as $possibleApp) { + $version = self::getAppVersionByPath($possibleApp['path']); + if (empty($versionToLoad) || version_compare($version, $versionToLoad['version'], '>')) { + $versionToLoad = array( + 'dir' => $possibleApp, + 'version' => $version, + ); + } + } + $app_dir[$appId] = $versionToLoad['dir']; + return $versionToLoad['dir']; + //TODO - write test + } } + + /** * Get the directory for the given app. * If the app is defined in multiple directories, the first one is taken. (false if not found) @@ -471,6 +505,18 @@ class OC_App{ return false; } + + /** + * check if an app's directory is writable + * @param $appid + * @return bool + */ + public static function isAppDirWritable($appid) { + $path = self::getAppPath($appid); + return is_writable($path); + } + + /** * Get the path for the given app on the access * If the app is defined in multiple directories, the first one is taken. (false if not found) @@ -490,15 +536,28 @@ class OC_App{ * @return string */ public static function getAppVersion($appid) { - $file= self::getAppPath($appid).'/appinfo/version'; - if(is_file($file) && $version = trim(file_get_contents($file))) { + $file = self::getAppPath($appid); + return self::getAppVersionByPath($file); + } + + + /** + * get app's version based on it's path + * @param string $path + * @return string + */ + public static function getAppVersionByPath($path) { + $versionFile = $path . '/appinfo/version'; + $infoFile = $path . '/appinfo/info.xml'; + if(is_file($versionFile) && $version = trim(file_get_contents($versionFile))) { return $version; }else{ - $appData=self::getAppInfo($appid); + $appData=self::getAppInfo($infoFile, true); return isset($appData['version'])? $appData['version'] : ''; } } + /** * @brief Read all app metadata from the info.xml file * @param string $appid id of the app or the path of the info.xml file @@ -513,7 +572,7 @@ class OC_App{ if(isset(self::$appInfo[$appid])) { return self::$appInfo[$appid]; } - $file= self::getAppPath($appid).'/appinfo/info.xml'; + $file = self::getAppPath($appid) . '/appinfo/info.xml'; } $data=array(); $content=@file_get_contents($file); @@ -602,7 +661,6 @@ class OC_App{ } } - /** * get the forms for either settings, admin or personal */ @@ -726,14 +784,14 @@ class OC_App{ $info['internal']=true; $info['internallabel']='Internal App'; $info['internalclass']=''; - $info['update']=false; } else { $info['internal']=false; $info['internallabel']='3rd Party'; $info['internalclass']='externalapp'; - $info['update']=OC_Installer::isUpdateAvailable($app); } + $info['update'] = OC_Installer::isUpdateAvailable($app); + $info['preview'] = OC_Helper::imagePath('settings', 'trans.png'); $info['version'] = OC_App::getAppVersion($app); $appList[] = $info; @@ -828,17 +886,29 @@ class OC_App{ // rating img - if($app['score']>=0 and $app['score']<5) $img=OC_Helper::imagePath( "core", "rating/s1.png" ); - elseif($app['score']>=5 and $app['score']<15) $img=OC_Helper::imagePath( "core", "rating/s2.png" ); - elseif($app['score']>=15 and $app['score']<25) $img=OC_Helper::imagePath( "core", "rating/s3.png" ); - elseif($app['score']>=25 and $app['score']<35) $img=OC_Helper::imagePath( "core", "rating/s4.png" ); - elseif($app['score']>=35 and $app['score']<45) $img=OC_Helper::imagePath( "core", "rating/s5.png" ); - elseif($app['score']>=45 and $app['score']<55) $img=OC_Helper::imagePath( "core", "rating/s6.png" ); - elseif($app['score']>=55 and $app['score']<65) $img=OC_Helper::imagePath( "core", "rating/s7.png" ); - elseif($app['score']>=65 and $app['score']<75) $img=OC_Helper::imagePath( "core", "rating/s8.png" ); - elseif($app['score']>=75 and $app['score']<85) $img=OC_Helper::imagePath( "core", "rating/s9.png" ); - elseif($app['score']>=85 and $app['score']<95) $img=OC_Helper::imagePath( "core", "rating/s10.png" ); - elseif($app['score']>=95 and $app['score']<100) $img=OC_Helper::imagePath( "core", "rating/s11.png" ); + if ($app['score'] >= 0 && $app['score'] < 5) { + $img = OC_Helper::imagePath( "core", "rating/s1.png" ); + } elseif ($app['score'] >= 5 && $app['score'] < 15) { + $img = OC_Helper::imagePath( "core", "rating/s2.png" ); + } elseif($app['score'] >= 15 && $app['score'] < 25) { + $img = OC_Helper::imagePath( "core", "rating/s3.png" ); + } elseif($app['score'] >= 25 && $app['score'] < 35) { + $img = OC_Helper::imagePath( "core", "rating/s4.png" ); + } elseif($app['score'] >= 35 && $app['score'] < 45) { + $img = OC_Helper::imagePath( "core", "rating/s5.png" ); + } elseif($app['score'] >= 45 && $app['score'] < 55) { + $img = OC_Helper::imagePath( "core", "rating/s6.png" ); + } elseif($app['score'] >= 55 && $app['score'] < 65) { + $img = OC_Helper::imagePath( "core", "rating/s7.png" ); + } elseif($app['score'] >= 65 && $app['score'] < 75) { + $img = OC_Helper::imagePath( "core", "rating/s8.png" ); + } elseif($app['score'] >= 75 && $app['score'] < 85) { + $img = OC_Helper::imagePath( "core", "rating/s9.png" ); + } elseif($app['score'] >= 85 && $app['score'] < 95) { + $img = OC_Helper::imagePath( "core", "rating/s10.png" ); + } elseif($app['score'] >= 95 && $app['score'] < 100) { + $img = OC_Helper::imagePath( "core", "rating/s11.png" ); + } $app1[$i]['score'] = '<img src="'.$img.'"> Score: '.$app['score'].'%'; $i++; @@ -959,9 +1029,55 @@ class OC_App{ return $versions; } + + /** + * @param mixed $app + * @return bool + */ + public static function installApp($app) { + $l = OC_L10N::get('core'); + $appdata=OC_OCSClient::getApplication($app); + + // check if app is a shipped app or not. OCS apps have an integer as id, shipped apps use a string + if(!is_numeric($app)) { + $shippedVersion=self::getAppVersion($app); + if($appdata && version_compare($shippedVersion, $appdata['version'], '<')) { + $app = self::downloadApp($app); + } else { + $app = OC_Installer::installShippedApp($app); + } + }else{ + $app = self::downloadApp($app); + } + + if($app!==false) { + // check if the app is compatible with this version of ownCloud + $info = self::getAppInfo($app); + $version=OC_Util::getVersion(); + if(!isset($info['require']) or !self::isAppVersionCompatible($version, $info['require'])) { + throw new \Exception( + $l->t("App \"%s\" can't be installed because it is not compatible with this version of ownCloud.", + array($info['name']) + ) + ); + }else{ + OC_Appconfig::setValue( $app, 'enabled', 'yes' ); + if(isset($appdata['id'])) { + OC_Appconfig::setValue( $app, 'ocsid', $appdata['id'] ); + } + \OC_Hook::emit('OC_App', 'post_enable', array('app' => $app)); + } + }else{ + throw new \Exception($l->t("No app name specified")); + } + + return $app; + } + /** * update the database for the app and call the update script * @param string $appid + * @return bool */ public static function updateApp($appid) { if(file_exists(self::getAppPath($appid).'/appinfo/preupdate.php')) { @@ -972,7 +1088,7 @@ class OC_App{ OC_DB::updateDbFromStructure(self::getAppPath($appid).'/appinfo/database.xml'); } if(!self::isEnabled($appid)) { - return; + return false; } if(file_exists(self::getAppPath($appid).'/appinfo/update.php')) { self::loadApp($appid); @@ -989,6 +1105,8 @@ class OC_App{ } self::setAppTypes($appid); + + return true; } /** diff --git a/lib/private/installer.php b/lib/private/installer.php index 64e8e3a5e7a..e8ea162eb2f 100644 --- a/lib/private/installer.php +++ b/lib/private/installer.php @@ -5,6 +5,9 @@ * @author Robin Appelman * @copyright 2012 Frank Karlitschek frank@owncloud.org * + * @author Georg Ehrke + * @copytight 2014 Georg Ehrke georg@ownCloud.com + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE * License as published by the Free Software Foundation; either @@ -24,6 +27,7 @@ * This class provides the functionality needed to install, update and remove plugins/apps */ class OC_Installer{ + /** * * This function installs an app. All information needed are passed in the @@ -60,6 +64,157 @@ class OC_Installer{ public static function installApp( $data = array()) { $l = \OC_L10N::get('lib'); + list($extractDir, $path) = self::downloadApp($data); + $info = self::checkAppsIntegrity($data, $extractDir, $path); + + $basedir=OC_App::getInstallPath().'/'.$info['id']; + //check if the destination directory already exists + if(is_dir($basedir)) { + OC_Helper::rmdirr($extractDir); + if($data['source']=='http') { + unlink($path); + } + throw new \Exception($l->t("App directory already exists")); + } + + if(isset($data['pretent']) and $data['pretent']==true) { + return false; + } + + //copy the app to the correct place + if(@!mkdir($basedir)) { + OC_Helper::rmdirr($extractDir); + if($data['source']=='http') { + unlink($path); + } + throw new \Exception($l->t("Can't create app folder. Please fix permissions. %s", array($basedir))); + } + + $extractDir .= '/' . $info['id']; + OC_Helper::copyr($extractDir, $basedir); + + //remove temporary files + OC_Helper::rmdirr($extractDir); + + //install the database + if(is_file($basedir.'/appinfo/database.xml')) { + if (OC_Appconfig::getValue($info['id'], 'installed_version') === null) { + OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); + } else { + OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml'); + } + } + + //run appinfo/install.php + if((!isset($data['noinstall']) or $data['noinstall']==false) and file_exists($basedir.'/appinfo/install.php')) { + include $basedir.'/appinfo/install.php'; + } + + //set the installed version + OC_Appconfig::setValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'])); + OC_Appconfig::setValue($info['id'], 'enabled', 'no'); + + //set remote/public handelers + foreach($info['remote'] as $name=>$path) { + OCP\CONFIG::setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); + } + foreach($info['public'] as $name=>$path) { + OCP\CONFIG::setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); + } + + OC_App::setAppTypes($info['id']); + + return $info['id']; + } + + /** + * @brief checks whether or not an app is installed + * @param string $app app + * @returns bool + * + * Checks whether or not an app is installed, i.e. registered in apps table. + */ + public static function isInstalled( $app ) { + return (OC_Appconfig::getValue($app, "installed_version") !== null); + } + + /** + * @brief Update an application + * + * This function installs an app. All information needed are passed in the + * associative array $data. + * The following keys are required: + * - source: string, can be "path" or "http" + * + * One of the following keys is required: + * - path: path to the file containing the app + * - href: link to the downloadable file containing the app + * + * The following keys are optional: + * - pretend: boolean, if set true the system won't do anything + * - noupgrade: boolean, if true appinfo/upgrade.php won't be loaded + * + * This function works as follows + * -# fetching the file + * -# removing the old files + * -# unzipping new file + * -# including appinfo/upgrade.php + * -# setting the installed version + * + * upgrade.php can determine the current installed version of the app using + * "OC_Appconfig::getValue($appid, 'installed_version')" + */ + public static function updateApp( $app ) { + $appdata = OC_OCSClient::getApplication($app); + $download = OC_OCSClient::getApplicationDownload($app, 1); + + if (array_key_exists('downloadlink', $download) && trim($download['downloadlink']) !== '') { + $download['downloadlink'] = str_replace(' ', '%20', $download['downloadlink']); + $info = array( + 'source' => 'http', + 'href' => $download['downloadlink'], + 'appdata' => $appdata + ); + } else { + throw new \Exception('Could fetch app info!'); + } + + list($extractDir, $path) = self::downloadApp($info); + $info = self::checkAppsIntegrity($info, $extractDir, $path); + + $currentDir = OC_App::getAppPath($info['id']); + $basedir = OC_App::getInstallPath(); + $basedir .= '/'; + $basedir .= $info['id']; + + if($currentDir !== null && is_writable($currentDir)) { + $basedir = $currentDir; + } + if(is_dir($basedir)) { + OC_Helper::rmdirr($basedir); + } + + $appInExtractDir = $extractDir; + if (substr($extractDir, -1) !== '/') { + $appInExtractDir .= '/'; + } + + $appInExtractDir .= $info['id']; + OC_Helper::copyr($appInExtractDir, $basedir); + OC_Helper::rmdirr($extractDir); + + return OC_App::updateApp($info['id']); + } + + + /** + * @param array $data + * @return array + * @throws Exception + */ + public static function downloadApp($data = array()) { + $l = \OC_L10N::get('lib'); + if(!isset($data['source'])) { throw new \Exception($l->t("No source specified when installing app")); } @@ -104,6 +259,21 @@ class OC_Installer{ throw new \Exception($l->t("Failed to open archive when installing app")); } + return array( + $extractDir, + $path + ); + } + + /** + * check an app's integrity + * @param array $data + * @param string $extractDir + * @return array + * @throws \Exception + */ + public static function checkAppsIntegrity($data = array(), $extractDir, $path) { + $l = \OC_L10N::get('lib'); //load the info.xml file of the app if(!is_file($extractDir.'/appinfo/info.xml')) { //try to find it in a subdir @@ -160,115 +330,12 @@ class OC_Installer{ throw new \Exception($l->t("App can't be installed because the version in info.xml/version is not the same as the version reported from the app store")); } - $basedir=OC_App::getInstallPath().'/'.$info['id']; - //check if the destination directory already exists - if(is_dir($basedir)) { - OC_Helper::rmdirr($extractDir); - if($data['source']=='http') { - unlink($path); - } - throw new \Exception($l->t("App directory already exists")); - } - - if(isset($data['pretent']) and $data['pretent']==true) { - return false; - } - - //copy the app to the correct place - if(@!mkdir($basedir)) { - OC_Helper::rmdirr($extractDir); - if($data['source']=='http') { - unlink($path); - } - throw new \Exception($l->t("Can't create app folder. Please fix permissions. %s", array($basedir))); - } - OC_Helper::copyr($extractDir, $basedir); - - //remove temporary files - OC_Helper::rmdirr($extractDir); - - //install the database - if(is_file($basedir.'/appinfo/database.xml')) { - if (OC_Appconfig::getValue($info['id'], 'installed_version') === null) { - OC_DB::createDbFromStructure($basedir.'/appinfo/database.xml'); - } else { - OC_DB::updateDbFromStructure($basedir.'/appinfo/database.xml'); - } - } - - //run appinfo/install.php - if((!isset($data['noinstall']) or $data['noinstall']==false) and file_exists($basedir.'/appinfo/install.php')) { - include $basedir.'/appinfo/install.php'; - } - - //set the installed version - OC_Appconfig::setValue($info['id'], 'installed_version', OC_App::getAppVersion($info['id'])); - OC_Appconfig::setValue($info['id'], 'enabled', 'no'); - - //set remote/public handelers - foreach($info['remote'] as $name=>$path) { - OCP\CONFIG::setAppValue('core', 'remote_'.$name, $info['id'].'/'.$path); - } - foreach($info['public'] as $name=>$path) { - OCP\CONFIG::setAppValue('core', 'public_'.$name, $info['id'].'/'.$path); - } - - OC_App::setAppTypes($info['id']); - - return $info['id']; - } - - /** - * @brief checks whether or not an app is installed - * @param string $app app - * @returns true/false - * - * Checks whether or not an app is installed, i.e. registered in apps table. - */ - public static function isInstalled( $app ) { - - if( null == OC_Appconfig::getValue( $app, "installed_version" )) { - return false; - } - - return true; - } - - /** - * @brief Update an application - * - * This function installs an app. All information needed are passed in the - * associative array $data. - * The following keys are required: - * - source: string, can be "path" or "http" - * - * One of the following keys is required: - * - path: path to the file containing the app - * - href: link to the downloadable file containing the app - * - * The following keys are optional: - * - pretend: boolean, if set true the system won't do anything - * - noupgrade: boolean, if true appinfo/upgrade.php won't be loaded - * - * This function works as follows - * -# fetching the file - * -# removing the old files - * -# unzipping new file - * -# including appinfo/upgrade.php - * -# setting the installed version - * - * upgrade.php can determine the current installed version of the app using - * "OC_Appconfig::getValue($appid, 'installed_version')" - */ - public static function updateApp( $app ) { - $ocsid=OC_Appconfig::getValue( $app, 'ocsid'); - OC_App::disable($app); - OC_App::enable($ocsid); - return(true); + return $info; } /** - * @brief Check if an update for the app is available + * Check if an update for the app is available + * @param string $app * @return string|false false or the version number of the update * * The function will check if an update for a version is available @@ -424,14 +491,13 @@ class OC_Installer{ return $info['id']; } - /** * check the code of an app with some static code checks * @param string $folder the folder of the app to check * @return boolean true for app is o.k. and false for app is not o.k. */ public static function checkCode($appname, $folder) { - + return true; $blacklist=array( 'exec(', 'eval(', |