From 5507db9b15034c73d9a121596da4bf440206f173 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Fri, 3 Feb 2012 20:32:06 +0000 Subject: Initial migration code, and basic export for bookmarks --- lib/migrate.php | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/migrate/provider.php | 23 +++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 lib/migrate.php create mode 100644 lib/migrate/provider.php (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php new file mode 100644 index 00000000000..8f13d07f468 --- /dev/null +++ b/lib/migrate.php @@ -0,0 +1,66 @@ +. + * + */ + + +/** + * provides an interface to all search providers + */ +class OC_Migrate{ + static public $providers=array(); + + /** + * register a new migration provider + * @param OC_Migrate_Provider $provider + */ + public static function registerProvider($provider){ + self::$providers[]=$provider; + } + + /** + * export app data for a user + * @param string userid + * @return string xml of app data + */ + public static function export($uid){ + $xml = ''; + foreach(self::$providers as $provider){ + $xml .= ''; + $xml .= self::appInfoXML($provider->$appid); + $xml .= $provider->export($uid)); + $xml .= ''; + } + return $xml; + } + + /** + * generates the app info xml + * @param string appid + * @return string xml app info + */ + public static function appInfoXML($appid){ + $info = OC_App::getAppInfo($appid); + $xml = ''; + $zml .= 'INFO HERE'; + $xml .= ''; + return $xml; + } +} diff --git a/lib/migrate/provider.php b/lib/migrate/provider.php new file mode 100644 index 00000000000..920fde7db39 --- /dev/null +++ b/lib/migrate/provider.php @@ -0,0 +1,23 @@ + Date: Fri, 3 Feb 2012 20:48:32 +0000 Subject: fix syntax, add logging, debug xml output instead of zip --- apps/user_migrate/settings.php | 21 +++++++++++---------- lib/migrate.php | 4 +++- 2 files changed, 14 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index fbf190e37d0..5831bf54cb7 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -49,21 +49,22 @@ if (isset($_POST['user_migrate'])) { // adding owncloud system files OC_Log::write('user_migrate',"Adding app data to user export",OC_Log::INFO); // Call to OC_Migrate for the xml file. - $appdatafile = $tempdir . "/appdata.xml"; - $fh = fopen($appdatafile, 'w'); + //$appdatafile = $tempdir . "/appdata.xml"; + //$fh = fopen($appdatafile, 'w'); $appdata = OC_Migrate::export(OC_User::getUser()); - fwrite($fh, $appdata); - $zip->addFile($appdatafile, "appdata.xml"); - fclose($fh); + //fwrite($fh, $appdata); + //$zip->addFile($appdatafile, "appdata.xml"); + //fclose($fh); } $zip->close(); - header("Content-Type: application/zip"); - header("Content-Disposition: attachment; filename=" . basename($filename)); - header("Content-Length: " . filesize($filename)); - @ob_end_clean(); - readfile($filename); + //header("Content-Type: application/zip"); + //header("Content-Disposition: attachment; filename=" . basename($filename)); + //header("Content-Length: " . filesize($filename)); + //@ob_end_clean(); + echo $appdata; + //readfile($filename); unlink($filename); } else { // fill template diff --git a/lib/migrate.php b/lib/migrate.php index 8f13d07f468..b3345ccd1a6 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -41,11 +41,13 @@ class OC_Migrate{ * @return string xml of app data */ public static function export($uid){ + OC_Log::write('user_migrate','Starting user appdata export for: '.$uid,OC_Log::INFO); $xml = ''; foreach(self::$providers as $provider){ + OC_Log::write('user_migrate','Getting app data for app:'.$provider->appid,OC_Log::INFO); $xml .= ''; $xml .= self::appInfoXML($provider->$appid); - $xml .= $provider->export($uid)); + $xml .= $provider->export($uid); $xml .= ''; } return $xml; -- cgit v1.2.3 From ee88ded463b24a9b9cd8ba78a5df8657159eeef8 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Fri, 3 Feb 2012 21:00:12 +0000 Subject: more commenting out for debugging, added logging commands --- apps/bookmarks/appinfo/app.php | 4 ++++ apps/bookmarks/lib/migrate.php | 19 ++++++++----------- apps/user_migrate/settings.php | 2 +- lib/migrate.php | 5 +++-- lib/migrate/provider.php | 4 ++-- 5 files changed, 18 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/apps/bookmarks/appinfo/app.php b/apps/bookmarks/appinfo/app.php index 479d8ed4767..54812188418 100644 --- a/apps/bookmarks/appinfo/app.php +++ b/apps/bookmarks/appinfo/app.php @@ -17,3 +17,7 @@ OC_App::addNavigationEntry( array( 'id' => 'bookmarks_index', 'order' => 70, 'hr OC_App::registerPersonal('bookmarks', 'settings'); require_once('apps/bookmarks/lib/search.php'); OC_Util::addScript('bookmarks','bookmarksearch'); + +// Include the migration provider + +require_once('apps/bookmarks/lib/migrate.php'); \ No newline at end of file diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index 2e6581cd9fa..d5a6a75ca82 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -1,18 +1,15 @@ appid = 'bookmarks'; // Create the xml for the user supplied function export($uid){ - $xml = ''; - $query = OC_DB::prepare("SELECT * FROM *PREFIX*bookmarks WHERE *PREFIX*bookmakrs.user_id = ?"); - $bookmarks = $query->execute($uid); - OC_Log::write('user_migrate',print_r($bookmarks)); - foreach($bookmarks as $bookmark){ - $xml .= ''; - $xml .='DATA WILL BE HERE'; - $xml .= ''; - } + $xml = 'debugfrombookmarks'; + //$query = OC_DB::prepare("SELECT * FROM *PREFIX*bookmarks WHERE *PREFIX*bookmakrs.user_id = ?"); + //$bookmarks = $query->execute($uid); + //foreach($bookmarks as $bookmark){ + // $xml .= ''; + // $xml .='DATA WILL BE HERE'; + // $xml .= ''; + //} return $xml; } } diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 5831bf54cb7..4fa4d23a6fe 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -47,7 +47,7 @@ if (isset($_POST['user_migrate'])) { // Does the user want their app data? if (isset($_POST['user_appdata'])) { // adding owncloud system files - OC_Log::write('user_migrate',"Adding app data to user export",OC_Log::INFO); + OC_Log::write('user_migrate',"Adding app data to user export file",OC_Log::INFO); // Call to OC_Migrate for the xml file. //$appdatafile = $tempdir . "/appdata.xml"; //$fh = fopen($appdatafile, 'w'); diff --git a/lib/migrate.php b/lib/migrate.php index b3345ccd1a6..e3394c5a67a 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -25,7 +25,7 @@ * provides an interface to all search providers */ class OC_Migrate{ - static public $providers=array(); + static private $providers=array(); /** * register a new migration provider @@ -33,6 +33,7 @@ class OC_Migrate{ */ public static function registerProvider($provider){ self::$providers[]=$provider; + OC_Log::write('user_migrate','Provider registered',OC_Log::INFO); } /** @@ -46,7 +47,7 @@ class OC_Migrate{ foreach(self::$providers as $provider){ OC_Log::write('user_migrate','Getting app data for app:'.$provider->appid,OC_Log::INFO); $xml .= ''; - $xml .= self::appInfoXML($provider->$appid); + //$xml .= self::appInfoXML($provider->$appid); $xml .= $provider->export($uid); $xml .= ''; } diff --git a/lib/migrate/provider.php b/lib/migrate/provider.php index 920fde7db39..b604a010720 100644 --- a/lib/migrate/provider.php +++ b/lib/migrate/provider.php @@ -6,7 +6,7 @@ abstract class OC_Migrate_Provider{ public function __construct(){ OC_Migrate::registerProvider($this); } - public static $appid; + //public static $appid; /** * exports data for apps * @param string $uid @@ -19,5 +19,5 @@ abstract class OC_Migrate_Provider{ * @param string $query * @return array An array of OC_Search_Result's */ - abstract function import($data); + //abstract function import($data); } -- cgit v1.2.3 From 1133eaa679558a3f88a4678de977734533c29507 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Fri, 3 Feb 2012 21:28:58 +0000 Subject: load bookmarks provider class --- apps/bookmarks/lib/migrate.php | 1 + apps/user_migrate/settings.php | 2 +- lib/migrate.php | 7 +++---- lib/migrate/provider.php | 5 ++++- 4 files changed, 9 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index d5a6a75ca82..bfbe3fc5830 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -13,3 +13,4 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ return $xml; } } +new OC_Migrate_Provider_Bookmarks('bookmarks'); \ No newline at end of file diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 4fa4d23a6fe..7e3510d97ef 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -63,7 +63,7 @@ if (isset($_POST['user_migrate'])) { //header("Content-Disposition: attachment; filename=" . basename($filename)); //header("Content-Length: " . filesize($filename)); //@ob_end_clean(); - echo $appdata; + echo htmlspecialchars($appdata); //readfile($filename); unlink($filename); } else { diff --git a/lib/migrate.php b/lib/migrate.php index e3394c5a67a..a1b4c5019bc 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -33,7 +33,6 @@ class OC_Migrate{ */ public static function registerProvider($provider){ self::$providers[]=$provider; - OC_Log::write('user_migrate','Provider registered',OC_Log::INFO); } /** @@ -42,12 +41,12 @@ class OC_Migrate{ * @return string xml of app data */ public static function export($uid){ - OC_Log::write('user_migrate','Starting user appdata export for: '.$uid,OC_Log::INFO); + OC_Log::write('user_migrate','App data export started for user: '.$uid,OC_Log::INFO); $xml = ''; foreach(self::$providers as $provider){ OC_Log::write('user_migrate','Getting app data for app:'.$provider->appid,OC_Log::INFO); $xml .= ''; - //$xml .= self::appInfoXML($provider->$appid); + $xml .= self::appInfoXML($provider->appid); $xml .= $provider->export($uid); $xml .= ''; } @@ -62,7 +61,7 @@ class OC_Migrate{ public static function appInfoXML($appid){ $info = OC_App::getAppInfo($appid); $xml = ''; - $zml .= 'INFO HERE'; + $xml .= 'INFO HERE'; $xml .= ''; return $xml; } diff --git a/lib/migrate/provider.php b/lib/migrate/provider.php index b604a010720..9dd1b2f3897 100644 --- a/lib/migrate/provider.php +++ b/lib/migrate/provider.php @@ -3,7 +3,10 @@ * provides search functionalty */ abstract class OC_Migrate_Provider{ - public function __construct(){ + public $appid; + + public function __construct($appid){ + $this->appid = $appid; OC_Migrate::registerProvider($this); } //public static $appid; -- cgit v1.2.3 From 33c5b3a2efa8559a3e960e6d8256576aab604869 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Thu, 1 Mar 2012 19:41:14 +0000 Subject: Added replaceDB method in lib/db.php --- lib/db.php | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 74 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/db.php b/lib/db.php index 9d3c20e0145..f43f00a397b 100644 --- a/lib/db.php +++ b/lib/db.php @@ -316,8 +316,11 @@ class OC_DB { // read file $content = file_get_contents( $file ); - // Make changes and save them to a temporary file - $file2 = tempnam( get_temp_dir(), 'oc_db_scheme_' ); + // Make changes and save them to an in-memory file + $file2 = 'static://db_scheme'; + if($file2 == ''){ + die('could not create tempfile in get_temp_dir() - aborting'); + } $content = str_replace( '*dbname*', $CONFIG_DBNAME, $content ); $content = str_replace( '*dbprefix*', $CONFIG_DBTABLEPREFIX, $content ); if( $CONFIG_DBTYPE == 'pgsql' ){ //mysql support it too but sqlite doesn't @@ -328,7 +331,7 @@ class OC_DB { // Try to create tables $definition = self::$schema->parseDatabaseDefinitionFile( $file2 ); - // Delete our temporary file + //clean up memory unlink( $file2 ); // Die in case something went wrong @@ -368,8 +371,8 @@ class OC_DB { return false; } - // Make changes and save them to a temporary file - $file2 = tempnam( get_temp_dir(), 'oc_db_scheme_' ); + // Make changes and save them to an in-memory file + $file2 = 'static://db_scheme'; $content = str_replace( '*dbname*', $previousSchema['name'], $content ); $content = str_replace( '*dbprefix*', $CONFIG_DBTABLEPREFIX, $content ); if( $CONFIG_DBTYPE == 'pgsql' ){ //mysql support it too but sqlite doesn't @@ -378,7 +381,7 @@ class OC_DB { file_put_contents( $file2, $content ); $op = self::$schema->updateDatabase($file2, $previousSchema, array(), false); - // Delete our temporary file + //clean up memory unlink( $file2 ); if (PEAR::isError($op)) { @@ -389,6 +392,27 @@ class OC_DB { return true; } + /** + * @breif replaces the owncloud tables with a new set + */ + public static function replaceDB( $file ){ + + // Delete the old tables + self::removeDBStructure( '/home/tom/sites/secure.tomneedham.com/public_html/migration/db_structure.xml' ); + + $apps = OC_App::getAllApps(); + foreach($apps as $app){ + $path = '/apps/'.$app.'/appinfo/database.xml'; + if(file_exists($path)){ + self::removeDBStructure( $path ); + } + } + + // Create new tables + self::createDBFromStructure( $file ); + + } + /** * @brief connects to a MDB2 database scheme * @returns true/false @@ -482,6 +506,27 @@ class OC_DB { } } + /** + * @breif replaces the owncloud tables with a new set + */ + public static function replaceDB( $file ){ + + // Delete the old tables + self::removeDBStructure( OC::$DOCUMENTROOT . 'db_structure.xml' ); + + $apps = OC_App::getAllApps(); + foreach($apps as $app){ + $path = '/apps/'.$app.'/appinfo/database.xml'; + if(file_exists($path)){ + self::removeDBStructure( $path ); + } + } + + // Create new tables + self::createDBFromStructure( $file ); + + } + /** * Start a transaction */ @@ -505,6 +550,21 @@ class OC_DB { self::$connection->commit(); self::$inTransaction=false; } + + /** + * check if a result is an error, works with MDB2 and PDOException + * @param mixed $result + * @return bool + */ + public static function isError($result){ + if(!$result){ + return true; + }elseif(self::$backend==self::BACKEND_MDB2 and PEAR::isError($result)){ + return true; + }else{ + return false; + } + } } /** @@ -524,11 +584,15 @@ class PDOStatementWrapper{ public function execute($input=array()){ $this->lastArguments=$input; if(count($input)>0){ - $this->statement->execute($input); + $result=$this->statement->execute($input); + }else{ + $result=$this->statement->execute(); + } + if($result){ + return $this; }else{ - $this->statement->execute(); + return false; } - return $this; } /** @@ -567,3 +631,4 @@ class PDOStatementWrapper{ return $this->statement->fetchColumn($colnum); } } + -- cgit v1.2.3 From 9c79de4aa01697e6b2eece450a83ca2019eb9b1c Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Fri, 2 Mar 2012 21:47:20 +0000 Subject: removed duplicate function --- lib/db.php | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) (limited to 'lib') diff --git a/lib/db.php b/lib/db.php index f43f00a397b..2348da908ef 100644 --- a/lib/db.php +++ b/lib/db.php @@ -392,27 +392,6 @@ class OC_DB { return true; } - /** - * @breif replaces the owncloud tables with a new set - */ - public static function replaceDB( $file ){ - - // Delete the old tables - self::removeDBStructure( '/home/tom/sites/secure.tomneedham.com/public_html/migration/db_structure.xml' ); - - $apps = OC_App::getAllApps(); - foreach($apps as $app){ - $path = '/apps/'.$app.'/appinfo/database.xml'; - if(file_exists($path)){ - self::removeDBStructure( $path ); - } - } - - // Create new tables - self::createDBFromStructure( $file ); - - } - /** * @brief connects to a MDB2 database scheme * @returns true/false @@ -511,10 +490,11 @@ class OC_DB { */ public static function replaceDB( $file ){ + $apps = OC_App::getAllApps(); + // Delete the old tables self::removeDBStructure( OC::$DOCUMENTROOT . 'db_structure.xml' ); - $apps = OC_App::getAllApps(); foreach($apps as $app){ $path = '/apps/'.$app.'/appinfo/database.xml'; if(file_exists($path)){ -- cgit v1.2.3 From 86fed4c226f2ca20e3924e1b9483aeea4073fc3b Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Fri, 2 Mar 2012 22:19:06 +0000 Subject: Update database.xml locations. Fix dbexport.xml. --- apps/admin_export/settings.php | 7 ++++--- lib/db.php | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index 5eecd4f3a2f..5ba6f716a2b 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -62,8 +62,8 @@ if (isset($_POST['admin_export'])) { $createstring = "\n\n *dbname*\n true"; $dbexport = str_replace( $dbnamestring, "\n\n *dbname*", $dbexport ); - $dbexport = str_replace( $dbtableprefixstring, "\n\n *dbtableprefix*", $dbexport ); - $dbexport = str_replace( $createstring, "\n\n *dbname*\n false\n\n *dbprefix*", $dbexport ); + $dbexport = str_replace( $createstring, "\n\n *dbname*\n false", $dbexport ); // Write the new db export file file_put_contents( $dbfile, $dbexport ); @@ -134,7 +134,8 @@ if (isset($_POST['admin_export'])) { exit(); } - // TODO: Import db + // TODO: Import db + OC_DB::replaceDB( get_temp_dir() . '/' . $importname . '/dbexport.xml' ); } else { // fill template $tmpl = new OC_Template('admin_export', 'settings'); diff --git a/lib/db.php b/lib/db.php index 2348da908ef..07e58590966 100644 --- a/lib/db.php +++ b/lib/db.php @@ -493,10 +493,10 @@ class OC_DB { $apps = OC_App::getAllApps(); // Delete the old tables - self::removeDBStructure( OC::$DOCUMENTROOT . 'db_structure.xml' ); + self::removeDBStructure( OC::$SERVERROOT . '/db_structure.xml' ); foreach($apps as $app){ - $path = '/apps/'.$app.'/appinfo/database.xml'; + $path = OC::$SERVERROOT.'/apps/'.$app.'/appinfo/database.xml'; if(file_exists($path)){ self::removeDBStructure( $path ); } -- cgit v1.2.3 From 188a304625b8ddb597505f69bc34487806b5b18e Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 3 Mar 2012 13:26:01 +0000 Subject: Replace db on import. Update user_migration export function. --- apps/admin_export/settings.php | 3 --- apps/user_migrate/settings.php | 21 ++++++++++----------- lib/migrate.php | 34 +++++++++++++++++++++++----------- 3 files changed, 33 insertions(+), 25 deletions(-) (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index 5ba6f716a2b..5584181fbb9 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -59,11 +59,9 @@ if (isset($_POST['admin_export'])) { $dbnamestring = "\n\n " . OC_Config::getValue( "dbname", "owncloud" ); $dbtableprefixstring = "
\n\n " . OC_Config::getValue( "dbtableprefix", "_oc" ); - $createstring = "\n\n *dbname*\n true"; $dbexport = str_replace( $dbnamestring, "\n\n *dbname*", $dbexport ); $dbexport = str_replace( $dbtableprefixstring, "
\n\n *dbprefix*", $dbexport ); - $dbexport = str_replace( $createstring, "\n\n *dbname*\n false", $dbexport ); // Write the new db export file file_put_contents( $dbfile, $dbexport ); @@ -134,7 +132,6 @@ if (isset($_POST['admin_export'])) { exit(); } - // TODO: Import db OC_DB::replaceDB( get_temp_dir() . '/' . $importname . '/dbexport.xml' ); } else { // fill template diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 7e3510d97ef..7c00dace3c2 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -49,23 +49,22 @@ if (isset($_POST['user_migrate'])) { // adding owncloud system files OC_Log::write('user_migrate',"Adding app data to user export file",OC_Log::INFO); // Call to OC_Migrate for the xml file. - //$appdatafile = $tempdir . "/appdata.xml"; - //$fh = fopen($appdatafile, 'w'); + $appdatafile = $tempdir . "/appdata.xml"; + $appdata = OC_Migrate::export(OC_User::getUser()); - //fwrite($fh, $appdata); - //$zip->addFile($appdatafile, "appdata.xml"); - //fclose($fh); + file_put_contents($appdatafile, $appdata); + $zip->addFile($appdatafile, "appdata.xml"); + } $zip->close(); - //header("Content-Type: application/zip"); - //header("Content-Disposition: attachment; filename=" . basename($filename)); - //header("Content-Length: " . filesize($filename)); - //@ob_end_clean(); - echo htmlspecialchars($appdata); - //readfile($filename); + header("Content-Type: application/zip"); + header("Content-Disposition: attachment; filename=" . basename($filename)); + header("Content-Length: " . filesize($filename)); + readfile($filename); unlink($filename); + } else { // fill template $tmpl = new OC_Template('user_migrate', 'settings'); diff --git a/lib/migrate.php b/lib/migrate.php index a1b4c5019bc..c5def5b5830 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -41,16 +41,26 @@ class OC_Migrate{ * @return string xml of app data */ public static function export($uid){ + + $doc = new DOMDocument(); + $doc->formatOutput = true; + OC_Log::write('user_migrate','App data export started for user: '.$uid,OC_Log::INFO); - $xml = ''; + foreach(self::$providers as $provider){ + OC_Log::write('user_migrate','Getting app data for app:'.$provider->appid,OC_Log::INFO); - $xml .= ''; - $xml .= self::appInfoXML($provider->appid); - $xml .= $provider->export($uid); - $xml .= ''; + $app = $doc->createElement('app'); + $doc->appendChild($app); + // Append app info + $app = $doc->appendChild( self::appInfoXML( $provider->appid ) ); + + // Add the app data + $app->appendChild($provider->export($uid)); + } - return $xml; + + return $doc->saveXML(); } /** @@ -59,10 +69,12 @@ class OC_Migrate{ * @return string xml app info */ public static function appInfoXML($appid){ - $info = OC_App::getAppInfo($appid); - $xml = ''; - $xml .= 'INFO HERE'; - $xml .= ''; - return $xml; + $doc = new DOMDocument(); + $appinfo = $doc->createElement('appinfo'); + $appinfo = $doc->appendChild($appinfo); + $data = $doc->createTextNode($appid); + $appinfo->appendChild($data); + + return $appinfo; } } -- cgit v1.2.3 From 34f05ba180792afb40d953b6cf8595a4513eb972 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 3 Mar 2012 14:35:17 +0000 Subject: Udpdate bookmarks migration provider. App version included in export. --- apps/bookmarks/lib/migrate.php | 2 +- lib/migrate.php | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index 86a5b957257..c655154d41b 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -63,7 +63,7 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ } } - return $bookmarks; + return $doc; } diff --git a/lib/migrate.php b/lib/migrate.php index c5def5b5830..5179e431996 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -51,12 +51,17 @@ class OC_Migrate{ OC_Log::write('user_migrate','Getting app data for app:'.$provider->appid,OC_Log::INFO); $app = $doc->createElement('app'); - $doc->appendChild($app); + $app = $doc->appendChild($app); + $app->setAttribute('id',$provider->appid); // Append app info - $app = $doc->appendChild( self::appInfoXML( $provider->appid ) ); + $appinfo = $doc->importNode( self::appInfoXML( $provider->appid )->documentElement, true ); + $app->appendChild( $appinfo ); + $appdata = $doc->createElement('appdata'); + $appdata = $app->appendChild($appdata); // Add the app data - $app->appendChild($provider->export($uid)); + $appdatanode = $doc->importNode( $provider->export($uid)->documentElement, true ); + $appdata->appendChild( $appdatanode ); } @@ -69,12 +74,17 @@ class OC_Migrate{ * @return string xml app info */ public static function appInfoXML($appid){ + + $info = OC_App::getAppInfo($appid); + $doc = new DOMDocument(); $appinfo = $doc->createElement('appinfo'); $appinfo = $doc->appendChild($appinfo); - $data = $doc->createTextNode($appid); - $appinfo->appendChild($data); + $version = $doc->createElement('version'); + $appinfo->appendChild($version); + $versionval = $doc->createTextNode($info['version']); + $version->appendChild($versionval); - return $appinfo; + return $doc; } } -- cgit v1.2.3 From 691103acd5aad2673b6375726ba24fb56e88451b Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 3 Mar 2012 17:30:21 +0000 Subject: Use json for migration data --- apps/bookmarks/lib/migrate.php | 98 ++++++++++++------------- apps/user_migrate/settings.php | 4 +- lib/migrate.php | 157 +++++++++++++++++++++++++++++++---------- lib/migrate/provider.php | 9 +-- 4 files changed, 178 insertions(+), 90 deletions(-) (limited to 'lib') diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index c655154d41b..f50a8c46334 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -4,69 +4,71 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ // Create the xml for the user supplied function export($uid){ - $doc = new DOMDocument(); - $doc->formatOutput = true; - $bookmarks = $doc->createElement('bookmarks'); - $bookmarks = $doc->appendChild($bookmarks); - + $bookmarks = array(); + $query = OC_DB::prepare("SELECT * FROM *PREFIX*bookmarks WHERE *PREFIX*bookmarks.user_id = ?"); $bookmarksdata =& $query->execute(array($uid)); - - // Foreach bookmark while ($row = $bookmarksdata->fetchRow()) { - $bookmark = $doc->createElement('bookmark'); - $bookmark = $bookmarks->appendChild($bookmark); - - $attr = $doc->createElement('title'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['title']); - $attr->appendChild($value); - - $attr = $doc->createElement('url'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['url']); - $attr->appendChild($value); - - $attr = $doc->createElement('added'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['added']); - $attr->appendChild($value); - - $attr = $doc->createElement('lastmodified'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['lastmodified']); - $attr->appendChild($value); - - $attr = $doc->createElement('public'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['public']); - $attr->appendChild($value); - - $attr = $doc->createElement('clickcount'); - $attr = $bookmark->appendChild($attr); - $value = $doc->createTextNode($row['clickcount']); - $attr->appendChild($value); - - $attr = $doc->createElement('tags'); - $tags = $bookmark->appendChild($attr); + // Get the tags $query = OC_DB::prepare("SELECT * FROM *PREFIX*bookmarks_tags WHERE *PREFIX*bookmarks_tags.bookmark_id = ?"); $tagsdata =& $query->execute(array($row['id'])); + $tags = array(); // Foreach tag while ($row = $tagsdata->fetchRow()) { - $attr = $doc->createElement('tag'); - $attr = $tags->appendChild($attr); - $value = $doc->createTextNode($row['tag']); - $attr->appendChild($value); - } + $tags[] = $row['tag']; + } + + $bookmarks[] = array( + 'url' => $row['url'], + 'title' => $row['title'], + 'public' => $row['public'], + 'added' => $row['added'], + 'lastmodified' => $row['lastmodified'], + 'clickcount' => $row['clickcount'], + 'tags' => $tags + ); + } - return $doc; + return array('bookmarks' => $bookmarks); } + // Import function for bookmarks + function import($data,$uid){ + + // Different import code for different versions of the app + switch($data['info']['version']){ + default: + // Foreach bookmark + foreach($data['data']['bookmarks'] as $bookmark){ + + $query = OC_DB::prepare( "INSERT INTO `*PREFIX*bookmarks` ( `url`, `title`, `user_id`, `public`, `added`, `lastmodified`, `clickcount` ) VALUES( ?, ?, ?, ?, ?, ?, ? )" ); + $result = $query->execute( array( + $bookmark['url'], + $bookmark['title'], + $uid, + $bookmark['public'], + $bookmark['added'], + $bookmark['lastmodified'], + $bookmark['clickcount'] + ) ); + // Now add the tags + $id = OC_DB::insertid(); + foreach($bookmark['tags'] as $tag){ + $query = OC_DB::prepare( "INSERT INTO `*PREFIX*bookmarks_tags` ( `id`, `tag` ) VALUES( ?, ? )" ); + $result = $query->execute( array( $id, $tag)); + } + + } + break; + } + // Finished import + } + } new OC_Migrate_Provider_Bookmarks('bookmarks'); \ No newline at end of file diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 7c00dace3c2..ed08abe79d7 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -49,11 +49,11 @@ if (isset($_POST['user_migrate'])) { // adding owncloud system files OC_Log::write('user_migrate',"Adding app data to user export file",OC_Log::INFO); // Call to OC_Migrate for the xml file. - $appdatafile = $tempdir . "/appdata.xml"; + $appdatafile = $tempdir . "/userexport.json"; $appdata = OC_Migrate::export(OC_User::getUser()); file_put_contents($appdatafile, $appdata); - $zip->addFile($appdatafile, "appdata.xml"); + $zip->addFile($appdatafile, "userexport.json"); } diff --git a/lib/migrate.php b/lib/migrate.php index 5179e431996..f3dd3080d30 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -42,49 +42,134 @@ class OC_Migrate{ */ public static function export($uid){ - $doc = new DOMDocument(); - $doc->formatOutput = true; - - OC_Log::write('user_migrate','App data export started for user: '.$uid,OC_Log::INFO); - - foreach(self::$providers as $provider){ + // Only export database users, otherwise we get chaos + if(OC_User_Database::userExists($uid)){ + + $data = array(); + $data['userid'] = OC_User::getUser(); - OC_Log::write('user_migrate','Getting app data for app:'.$provider->appid,OC_Log::INFO); - $app = $doc->createElement('app'); - $app = $doc->appendChild($app); - $app->setAttribute('id',$provider->appid); - // Append app info - $appinfo = $doc->importNode( self::appInfoXML( $provider->appid )->documentElement, true ); - $app->appendChild( $appinfo ); + $query = OC_DB::prepare( "SELECT uid, password FROM *PREFIX*users WHERE uid LIKE ?" ); + $result = $query->execute( array( $uid)); + + $row = $result->fetchRow(); + if($row){ + $data['hash'] = $row['password']; + } else { + return false; + exit(); + } - $appdata = $doc->createElement('appdata'); - $appdata = $app->appendChild($appdata); - // Add the app data - $appdatanode = $doc->importNode( $provider->export($uid)->documentElement, true ); - $appdata->appendChild( $appdatanode ); - + foreach(self::$providers as $provider){ + + $data['apps'][$prodider->appid]['info'] = OC_App::getAppInfo($provider->appid); + $data['apps'][$provider->appid]['data'] = $provider->export($uid); + + } + + return self::indent(json_encode($data)); + + } else { + return false; } - - return $doc->saveXML(); + } /** - * generates the app info xml - * @param string appid - * @return string xml app info - */ - public static function appInfoXML($appid){ + * @breif imports a new user + * @param $data json data for the user + * @param $uid optional uid to use + * @return json reply + */ + public function import($data,$uid=null){ + + // Import the data + $data = json_decode($data); + if(is_null($data)){ + // TODO LOG + return false; + exit(); + } + + // Specified user or use original + $uid = !is_null($uid) ? $uid : $data['userid']; + + // Check if userid exists + if(OC_User::userExists($uid)){ + // TODO LOG + return false; + exit(); + } + + // Create the user + $query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" ); + $result = $query->execute( array( $uid, $data['hash'])); + if(!$result){ + // TODO LOG + return false; + exit(); + } + + foreach($data['app'] as $app){ + // Check if supports migration and is enabled + if(in_array($app, self::$providers)){ + if(OC_App::isEnabled($app)){ + $provider->import($data['app'][$app],$uid); + } + } + + } + + } + + private static function indent($json){ + + $result = ''; + $pos = 0; + $strLen = strlen($json); + $indentStr = ' '; + $newLine = "\n"; + $prevChar = ''; + $outOfQuotes = true; - $info = OC_App::getAppInfo($appid); + for ($i=0; $i<=$strLen; $i++) { - $doc = new DOMDocument(); - $appinfo = $doc->createElement('appinfo'); - $appinfo = $doc->appendChild($appinfo); - $version = $doc->createElement('version'); - $appinfo->appendChild($version); - $versionval = $doc->createTextNode($info['version']); - $version->appendChild($versionval); + // Grab the next character in the string. + $char = substr($json, $i, 1); - return $doc; - } + // Are we inside a quoted string? + if ($char == '"' && $prevChar != '\\') { + $outOfQuotes = !$outOfQuotes; + + // If this character is the end of an element, + // output a new line and indent the next line. + } else if(($char == '}' || $char == ']') && $outOfQuotes) { + $result .= $newLine; + $pos --; + for ($j=0; $j<$pos; $j++) { + $result .= $indentStr; + } + } + + // Add the character to the result string. + $result .= $char; + + // If the last character was the beginning of an element, + // output a new line and indent the next line. + if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { + $result .= $newLine; + if ($char == '{' || $char == '[') { + $pos ++; + } + + for ($j = 0; $j < $pos; $j++) { + $result .= $indentStr; + } + } + + $prevChar = $char; + } + + return $result; + } + } diff --git a/lib/migrate/provider.php b/lib/migrate/provider.php index 9dd1b2f3897..63b804b6202 100644 --- a/lib/migrate/provider.php +++ b/lib/migrate/provider.php @@ -13,14 +13,15 @@ abstract class OC_Migrate_Provider{ /** * exports data for apps * @param string $uid - * @return string xml data for that app + * @return array appdata to be exported */ abstract function export($uid); /** * imports data for the app - * @param string $query - * @return array An array of OC_Search_Result's + * @param $data array of data. eg: array('info'=> APPINFO, 'data'=>APPDATA ARRAY) + * @param $info array of info of the source install + * @return void */ - //abstract function import($data); + abstract function import($data,$uid); } -- cgit v1.2.3 From c3dfcc5b21620476b6e5bf356b42aee9f0da5874 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Fri, 9 Mar 2012 23:33:11 +0000 Subject: First basic implementation of migration.db. --- lib/migrate.php | 314 ++++++++++++++++++++++++++++++----------------- lib/migrate/provider.php | 15 +-- 2 files changed, 209 insertions(+), 120 deletions(-) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index f3dd3080d30..da7a5ea34ff 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -25,7 +25,10 @@ * provides an interface to all search providers */ class OC_Migrate{ + + static private $MDB2=false; static private $providers=array(); + static private $schema=false; /** * register a new migration provider @@ -36,140 +39,225 @@ class OC_Migrate{ } /** - * export app data for a user - * @param string userid - * @return string xml of app data + * @breif creates a migration.db in the users data dir with their app data in + * @param @uid string userid of the user to export for + * @return bool whether operation was successfull */ - public static function export($uid){ + public static function export( $uid ){ // Only export database users, otherwise we get chaos - if(OC_User_Database::userExists($uid)){ + if(!OC_User_Database::userExists( $uid )){ + return false; + } - $data = array(); - $data['userid'] = OC_User::getUser(); + // Foreach provider + foreach( $providers as $provider ){ + + self::createAppTables( $provider->id ); + // Run the export function + $provider->export( $uid ); - $query = OC_DB::prepare( "SELECT uid, password FROM *PREFIX*users WHERE uid LIKE ?" ); - $result = $query->execute( array( $uid)); + } + + return true; + + } + + /** + * @breif imports a new user + * @param $uid optional uid to use + * @return bool if the import succedded + */ + public static function import( $uid=null ){ + + self::$uid = $uid; + + // Connect to the db + if(!self::connectDB()){ + return false; + } + + // Create the user + if(!self::createUser($uid, $hash)){ + return false; + } + + // Now get the list of apps to import from migration.db + // Then check for migrate.php for these apps + // If present, run the import function for them. + + return treu; + + } - $row = $result->fetchRow(); - if($row){ - $data['hash'] = $row['password']; - } else { + // @breif connects to migration.db, or creates if not found + // @return bool whether the operation was successful + private static function connectDB(){ + + // Already connected + if(!self::$MDB2){ + require_once('MDB2.php'); + + $datadir = OC_Config::getValue( "datadirectory", "$SERVERROOT/data" ); + + // Prepare options array + $options = array( + 'portability' => MDB2_PORTABILITY_ALL & (!MDB2_PORTABILITY_FIX_CASE), + 'log_line_break' => '
', + 'idxname_format' => '%s', + 'debug' => true, + 'quote_identifier' => true + ); + $dsn = array( + 'phptype' => 'sqlite', + 'database' => $datadir.'/'.self::$uid.'/migration.db', + 'mode' => '0644' + ); + + // Try to establish connection + self::$MDB2 = MDB2::factory( $dsn, $options ); + + // Die if we could not connect + if( PEAR::isError( self::$MDB2 )){ + OC_Log::write('migration', 'Failed to create migration.db',OC_Log::FATAL); + OC_Log::write('migration',self::$MDB2->getUserInfo(),OC_Log::FATAL); + OC_Log::write('migration',self::$MDB2->getMessage(),OC_Log::FATAL); return false; - exit(); } - foreach(self::$providers as $provider){ - - $data['apps'][$prodider->appid]['info'] = OC_App::getAppInfo($provider->appid); - $data['apps'][$provider->appid]['data'] = $provider->export($uid); + // We always, really always want associative arrays + self::$MDB2->setFetchMode(MDB2_FETCHMODE_ASSOC); + } + return true; + + } - } + // @breif prepares the db + // @param $query the sql query to prepare + public static function prepareDB( $query ){ + + // Optimize the query + $query = self::processQuery( $query ); + + // Optimize the query + $query = self::$MDB2->prepare( $query ); + + // Die if we have an error (error means: bad query, not 0 results!) + if( PEAR::isError( $query )) { + $entry = 'DB Error: "'.$result->getMessage().'"
'; + $entry .= 'Offending command was: '.$query.'
'; + OC_Log::write('migration',$entry,OC_Log::FATAL); + return false; + } else { + return true; + } + + } - return self::indent(json_encode($data)); + // @breif processes the db query + // @param $query the query to process + // @return string of processed query + private static function processQuery( $query ){ + self::connectDB(); + $type = 'sqlite'; + $prefix = ''; + + $query = str_replace( '`', '\'', $query ); + $query = str_replace( 'NOW()', 'datetime(\'now\')', $query ); + $query = str_replace( 'now()', 'datetime(\'now\')', $query ); + + // replace table name prefix + $query = str_replace( '*PREFIX*', $prefix, $query ); + + return $query; + + } + + // @breif creates the tables in migration.db from an apps database.xml + // @param $appid string id of the app + // @return bool whether the operation was successful + private static function createAppTables( $appid ){ + $file = OC::$SERVERROOT.'/apps/'.$appid.'appinfo/database.xml'; + if(file_exists( $file )){ + // There is a database.xml file + $content = file_get_contents( $file ); + + $file2 = 'static://db_scheme'; + $content = str_replace( '*dbname*', 'migration', $content ); + $content = str_replace( '*dbprefix*', '', $content ); + + file_put_contents( $file2, $content ); + + // Try to create tables + $definition = self::$schema->parseDatabaseDefinitionFile( $file2 ); + + unlink( $file2 ); + + // Die in case something went wrong + if( $definition instanceof MDB2_Schema_Error ){ + OC_Log::write('migration','Failed to parse database.xml for: '.$appid,OC_Log::FATAL); + OC_Log::write('migration',$definition->getMessage().': '.$definition->getUserInfo(),OC_Log::FATAL); + return false; + } + + $definition['overwrite'] = true; + + $ret = self::$schema->createDatabase( $definition ); + // Die in case something went wrong + + if( $ret instanceof MDB2_Error ){ + OC_Log::write('migration','Failed to create tables for: '.$appid,OC_Log::FATAL); + OC_Log::write('migration',$ret->getMessage().': '.$ret->getUserInfo(),OC_Log::FATAL); + return false; + } + return true; + } else { + // No database.xml return false; } - } - /** - * @breif imports a new user - * @param $data json data for the user - * @param $uid optional uid to use - * @return json reply + + /** + * @brief connects to a MDB2 database scheme + * @returns true/false + * + * Connects to a MDB2 database scheme */ - public function import($data,$uid=null){ - - // Import the data - $data = json_decode($data); - if(is_null($data)){ - // TODO LOG - return false; - exit(); - } - - // Specified user or use original - $uid = !is_null($uid) ? $uid : $data['userid']; - - // Check if userid exists - if(OC_User::userExists($uid)){ - // TODO LOG - return false; - exit(); - } - - // Create the user - $query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" ); - $result = $query->execute( array( $uid, $data['hash'])); - if(!$result){ - // TODO LOG - return false; - exit(); + private static function connectScheme(){ + // We need a mdb2 database connection + self::connectDB(); + self::$MDB2->loadModule( 'Manager' ); + self::$MDB2->loadModule( 'Reverse' ); + + // Connect if this did not happen before + if( !self::$schema ){ + require_once('MDB2/Schema.php'); + self::$schema=MDB2_Schema::factory( self::$MDB2 ); } - - foreach($data['app'] as $app){ - // Check if supports migration and is enabled - if(in_array($app, self::$providers)){ - if(OC_App::isEnabled($app)){ - $provider->import($data['app'][$app],$uid); - } - } - - } - - } - - private static function indent($json){ - $result = ''; - $pos = 0; - $strLen = strlen($json); - $indentStr = ' '; - $newLine = "\n"; - $prevChar = ''; - $outOfQuotes = true; - - for ($i=0; $i<=$strLen; $i++) { - - // Grab the next character in the string. - $char = substr($json, $i, 1); - - // Are we inside a quoted string? - if ($char == '"' && $prevChar != '\\') { - $outOfQuotes = !$outOfQuotes; - - // If this character is the end of an element, - // output a new line and indent the next line. - } else if(($char == '}' || $char == ']') && $outOfQuotes) { - $result .= $newLine; - $pos --; - for ($j=0; $j<$pos; $j++) { - $result .= $indentStr; - } - } - - // Add the character to the result string. - $result .= $char; - - // If the last character was the beginning of an element, - // output a new line and indent the next line. - if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) { - $result .= $newLine; - if ($char == '{' || $char == '[') { - $pos ++; - } - - for ($j = 0; $j < $pos; $j++) { - $result .= $indentStr; - } - } - - $prevChar = $char; + return true; + } + + // @breif creates a new user in the database + // @param $uid string user_id of the user to be created + // @param $hash string hash of the user to be created + // @return bool result of user creation + private static function createUser( $uid, $hash ){ + + // Check if userid exists + if(OC_User::userExists( $uid )){ + return false; } - return $result; - } + // Create the user + $query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" ); + $result = $query->execute( array( $uid, $data['hash'])); + + return $result ? true : false; + + } } diff --git a/lib/migrate/provider.php b/lib/migrate/provider.php index 63b804b6202..9c03639b7c3 100644 --- a/lib/migrate/provider.php +++ b/lib/migrate/provider.php @@ -3,22 +3,23 @@ * provides search functionalty */ abstract class OC_Migrate_Provider{ - public $appid; - public function __construct($appid){ - $this->appid = $appid; - OC_Migrate::registerProvider($this); + public $id; + + public function __construct( $appid ){ + $this->id = $appid; + OC_Migrate::registerProvider( $this ); } - //public static $appid; + /** - * exports data for apps + * @breif exports data for apps * @param string $uid * @return array appdata to be exported */ abstract function export($uid); /** - * imports data for the app + * @breif imports data for the app * @param $data array of data. eg: array('info'=> APPINFO, 'data'=>APPDATA ARRAY) * @param $info array of info of the source install * @return void -- cgit v1.2.3 From 3ca76d24a9b27101661c454be84c7126315315d6 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 10 Mar 2012 15:52:38 +0000 Subject: Add OC_Migrate::copyRows() method --- apps/bookmarks/lib/migrate.php | 89 +++++++++++++++--------------------------- lib/migrate.php | 59 +++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 58 deletions(-) (limited to 'lib') diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index f50a8c46334..7d8ad8bfc53 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -2,73 +2,48 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ // Create the xml for the user supplied - function export($uid){ + function export( $uid ){ - $bookmarks = array(); - - $query = OC_DB::prepare("SELECT * FROM *PREFIX*bookmarks WHERE *PREFIX*bookmarks.user_id = ?"); - $bookmarksdata =& $query->execute(array($uid)); - // Foreach bookmark - while ($row = $bookmarksdata->fetchRow()) { - - // Get the tags - $query = OC_DB::prepare("SELECT * FROM *PREFIX*bookmarks_tags WHERE *PREFIX*bookmarks_tags.bookmark_id = ?"); - $tagsdata =& $query->execute(array($row['id'])); + $options = array( + 'table'=>'bookmarks', + 'matchcol'=>'user_id', + 'matchval'=>$uid, + 'idcol'=>'id' + ); + $ids = OC_Migrate::copyRows( $options ); - $tags = array(); - // Foreach tag - while ($row = $tagsdata->fetchRow()) { - $tags[] = $row['tag']; - } - - $bookmarks[] = array( - 'url' => $row['url'], - 'title' => $row['title'], - 'public' => $row['public'], - 'added' => $row['added'], - 'lastmodified' => $row['lastmodified'], - 'clickcount' => $row['clickcount'], - 'tags' => $tags - ); - - } + $options = array( + 'table'=>'bookmarks_tags', + 'matchcol'=>'id', + 'matchval'=>$ids + ); - return array('bookmarks' => $bookmarks); + // Export tags + OC_Migrate::copyRows( $options ); } // Import function for bookmarks - function import($data,$uid){ + function import( $data, $uid ){ + + // new id mapping + $newids = array(); + + // Import bookmarks + foreach($data['bookmarks'] as $bookmark){ + $bookmark['user_id'] = $uid; + // import to the db now + $newids[$bookmark['id']] = OC_DB::insertid(); + } - // Different import code for different versions of the app - switch($data['info']['version']){ - default: - // Foreach bookmark - foreach($data['data']['bookmarks'] as $bookmark){ - - $query = OC_DB::prepare( "INSERT INTO `*PREFIX*bookmarks` ( `url`, `title`, `user_id`, `public`, `added`, `lastmodified`, `clickcount` ) VALUES( ?, ?, ?, ?, ?, ?, ? )" ); - $result = $query->execute( array( - $bookmark['url'], - $bookmark['title'], - $uid, - $bookmark['public'], - $bookmark['added'], - $bookmark['lastmodified'], - $bookmark['clickcount'] - ) ); - // Now add the tags - $id = OC_DB::insertid(); - foreach($bookmark['tags'] as $tag){ - $query = OC_DB::prepare( "INSERT INTO `*PREFIX*bookmarks_tags` ( `id`, `tag` ) VALUES( ?, ? )" ); - $result = $query->execute( array( $id, $tag)); - } - - } - break; + // Import tags + foreach($data['bookmarks_tags'] as $tag){ + // Map the new ids + $tag['id'] = $newids[$tag['id']]; + // Import to the db now using OC_DB } - // Finished import } } -new OC_Migrate_Provider_Bookmarks('bookmarks'); \ No newline at end of file +new OC_Migrate_Provider_Bookmarks( 'bookmarks' ); \ No newline at end of file diff --git a/lib/migrate.php b/lib/migrate.php index da7a5ea34ff..b09626d11be 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -134,7 +134,7 @@ class OC_Migrate{ // @breif prepares the db // @param $query the sql query to prepare - public static function prepareDB( $query ){ + public static function prepare( $query ){ // Optimize the query $query = self::processQuery( $query ); @@ -174,12 +174,69 @@ class OC_Migrate{ } + // @brief copys rows to migration.db from the main database + // @param $options array of options. + // @return bool + public static function copyRows( $options ){ + if( !array_key_exists( 'table', $options ) ){ + return false; + } + + // Need to include 'where' in the query? + if( array_key_exists( 'matchval', $options ) && array_key_exists( 'matchcol', $options ) ){ + foreach( $options['matchval'] as $matchval ){ + // Run the query for this match value (where x = y value) + $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] . " WHERE " . $options['matchcol'] . " LIKE ?" ); + $results = $query->execute( array( $matchval ) ); + self::insertData( $results, $options ); + + } + + } else { + // Just get everything + $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] ); + $results = $query->execute(); + self::insertData( $results, $options ); + + } + + return true; + + } + + // @breif saves a sql data set into migration.db + // @param $data a sql data set returned from self::prepare()->query() + // @param $options array of copyRows options + // @return void + private static function insertData( $data, $options ){ + while( $data = $result->fetchRow() ){ + // Now save all this to the migration.db + foreach($row as $field=>$value){ + $fields[] = $field; + $values[] = $value; + } + + // Generate some sql + $sql = "INSERT INTO `*PREFIX*" . $options['table'] . '` ( `'; + $fieldssql = implode( '`, `', $fields ); + $sql .= $fieldssql . "` ) VALUES( "; + $valuessql = substr( str_repeat( '?, ', count( $fields ) ),0,-1 ); + $sql .= $valuessql . " )"; + // Make the query + $query = self::prepare( $sql ); + $query->execute( $values ); + } + } + // @breif creates the tables in migration.db from an apps database.xml // @param $appid string id of the app // @return bool whether the operation was successful private static function createAppTables( $appid ){ $file = OC::$SERVERROOT.'/apps/'.$appid.'appinfo/database.xml'; if(file_exists( $file )){ + + self::connectScheme(); + // There is a database.xml file $content = file_get_contents( $file ); -- cgit v1.2.3 From d712d7f52c667e70edefcbd64c036afada678863 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 10 Mar 2012 18:18:58 +0000 Subject: Lots of fixes, improve copyRows() method and update settings page. --- apps/bookmarks/lib/migrate.php | 4 +-- apps/user_migrate/settings.php | 19 ++++++------- lib/migrate.php | 64 ++++++++++++++++++++++++++++-------------- 3 files changed, 54 insertions(+), 33 deletions(-) (limited to 'lib') diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index 7d8ad8bfc53..9493f2cae8e 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -11,10 +11,10 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ 'idcol'=>'id' ); $ids = OC_Migrate::copyRows( $options ); - + $ids = array('1'); $options = array( 'table'=>'bookmarks_tags', - 'matchcol'=>'id', + 'matchcol'=>'bookmark_id', 'matchval'=>$ids ); diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index ed08abe79d7..c35d46b3511 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -25,7 +25,6 @@ OC_Util::checkAppEnabled('user_migrate'); if (isset($_POST['user_migrate'])) { // Looks like they want to migrate - $errors = array(); $root = OC::$SERVERROOT . "/"; $user = OC_User::getUser(); $zip = new ZipArchive(); @@ -49,21 +48,21 @@ if (isset($_POST['user_migrate'])) { // adding owncloud system files OC_Log::write('user_migrate',"Adding app data to user export file",OC_Log::INFO); // Call to OC_Migrate for the xml file. - $appdatafile = $tempdir . "/userexport.json"; - $appdata = OC_Migrate::export(OC_User::getUser()); - file_put_contents($appdatafile, $appdata); - $zip->addFile($appdatafile, "userexport.json"); + // Create migration.db + var_dump(OC_Migrate::export(OC_User::getUser())); + // Add export db to zip + $zip->addFile($root.'data/'.$user.'/migration.db', "migration.db"); } $zip->close(); - header("Content-Type: application/zip"); - header("Content-Disposition: attachment; filename=" . basename($filename)); - header("Content-Length: " . filesize($filename)); - readfile($filename); - unlink($filename); + //header("Content-Type: application/zip"); + //header("Content-Disposition: attachment; filename=" . basename($filename)); + //header("Content-Length: " . filesize($filename)); + //readfile($filename); + //unlink($filename); } else { // fill template diff --git a/lib/migrate.php b/lib/migrate.php index b09626d11be..a0d702b4e21 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -29,6 +29,7 @@ class OC_Migrate{ static private $MDB2=false; static private $providers=array(); static private $schema=false; + static private $uid=false; /** * register a new migration provider @@ -49,17 +50,27 @@ class OC_Migrate{ if(!OC_User_Database::userExists( $uid )){ return false; } - + + self::$uid = $uid; + self::connectDB(); + $ok = true; + $return = array(); + // Foreach provider - foreach( $providers as $provider ){ - - self::createAppTables( $provider->id ); - // Run the export function - $provider->export( $uid ); - + foreach( self::$providers as $provider ){ + // Check for database.xml + if(file_exists(OC::$SERVERROOT.'/apps/'.$provider->id.'/appinfo/database.xml')){ + if(!self::createAppTables( $provider->id )){ + $ok = false; + OC_Log::write('migration','failed to create migration tables for: '.$provider->id,OC_Log::INFO); + } + } + if($ok){ + $return[$provider->id]['success'] = $provider->export( $uid ); + } } - return true; + return $return; } @@ -86,7 +97,7 @@ class OC_Migrate{ // Then check for migrate.php for these apps // If present, run the import function for them. - return treu; + return true; } @@ -98,7 +109,7 @@ class OC_Migrate{ if(!self::$MDB2){ require_once('MDB2.php'); - $datadir = OC_Config::getValue( "datadirectory", "$SERVERROOT/data" ); + $datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ); // Prepare options array $options = array( @@ -109,22 +120,22 @@ class OC_Migrate{ 'quote_identifier' => true ); $dsn = array( - 'phptype' => 'sqlite', + 'phptype' => 'sqlite3', 'database' => $datadir.'/'.self::$uid.'/migration.db', 'mode' => '0644' ); // Try to establish connection self::$MDB2 = MDB2::factory( $dsn, $options ); - // Die if we could not connect if( PEAR::isError( self::$MDB2 )){ + die(self::$MDB2->getMessage()); OC_Log::write('migration', 'Failed to create migration.db',OC_Log::FATAL); OC_Log::write('migration',self::$MDB2->getUserInfo(),OC_Log::FATAL); OC_Log::write('migration',self::$MDB2->getMessage(),OC_Log::FATAL); return false; + } else { } - // We always, really always want associative arrays self::$MDB2->setFetchMode(MDB2_FETCHMODE_ASSOC); } @@ -149,7 +160,7 @@ class OC_Migrate{ OC_Log::write('migration',$entry,OC_Log::FATAL); return false; } else { - return true; + return $query; } } @@ -160,7 +171,6 @@ class OC_Migrate{ private static function processQuery( $query ){ self::connectDB(); - $type = 'sqlite'; $prefix = ''; $query = str_replace( '`', '\'', $query ); @@ -184,6 +194,12 @@ class OC_Migrate{ // Need to include 'where' in the query? if( array_key_exists( 'matchval', $options ) && array_key_exists( 'matchcol', $options ) ){ + + // If only one matchval, create an array + if(!is_array($options['matchval'])){ + $options['matchval'] = array( $options['matchval'] ); + } + foreach( $options['matchval'] as $matchval ){ // Run the query for this match value (where x = y value) $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] . " WHERE " . $options['matchcol'] . " LIKE ?" ); @@ -209,7 +225,7 @@ class OC_Migrate{ // @param $options array of copyRows options // @return void private static function insertData( $data, $options ){ - while( $data = $result->fetchRow() ){ + while( $row = $data->fetchRow() ){ // Now save all this to the migration.db foreach($row as $field=>$value){ $fields[] = $field; @@ -217,14 +233,18 @@ class OC_Migrate{ } // Generate some sql - $sql = "INSERT INTO `*PREFIX*" . $options['table'] . '` ( `'; + $sql = "INSERT INTO `" . $options['table'] . '` ( `'; $fieldssql = implode( '`, `', $fields ); $sql .= $fieldssql . "` ) VALUES( "; - $valuessql = substr( str_repeat( '?, ', count( $fields ) ),0,-1 ); + $valuessql = substr( str_repeat( '?, ', count( $fields ) ),0,-2 ); $sql .= $valuessql . " )"; // Make the query $query = self::prepare( $sql ); - $query->execute( $values ); + if(!$query){ + OC_Log::write('migration','Invalid sql produced: '.$sql,OC_Log::FATAL); + } else { + $query->execute( $values ); + } } } @@ -232,10 +252,12 @@ class OC_Migrate{ // @param $appid string id of the app // @return bool whether the operation was successful private static function createAppTables( $appid ){ - $file = OC::$SERVERROOT.'/apps/'.$appid.'appinfo/database.xml'; + $file = OC::$SERVERROOT.'/apps/'.$appid.'/appinfo/database.xml'; if(file_exists( $file )){ - self::connectScheme(); + if(!self::connectScheme()){ + return false; + } // There is a database.xml file $content = file_get_contents( $file ); -- cgit v1.2.3 From fa5a5649c6f7a08b319b15bd537d898e5e77ee98 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sun, 11 Mar 2012 22:09:16 +0000 Subject: Fix copyRows() and sqlite connection --- apps/bookmarks/lib/migrate.php | 4 +-- lib/migrate.php | 65 +++++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index 9493f2cae8e..6e0b5c4cc49 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -3,7 +3,7 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ // Create the xml for the user supplied function export( $uid ){ - + OC_Log::write('migration','starting export for bookmarks',OC_Log::INFO); $options = array( 'table'=>'bookmarks', 'matchcol'=>'user_id', @@ -11,7 +11,7 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ 'idcol'=>'id' ); $ids = OC_Migrate::copyRows( $options ); - $ids = array('1'); + $options = array( 'table'=>'bookmarks_tags', 'matchcol'=>'bookmark_id', diff --git a/lib/migrate.php b/lib/migrate.php index a0d702b4e21..28329a8170b 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -45,13 +45,20 @@ class OC_Migrate{ * @return bool whether operation was successfull */ public static function export( $uid ){ - + // Only export database users, otherwise we get chaos if(!OC_User_Database::userExists( $uid )){ return false; } self::$uid = $uid; + + if(empty(self::$uid)){ + OC_Log::write('migration','Invalid uid passed',OC_Log::FATAL); + return false; + exit(); + } + self::connectDB(); $ok = true; $return = array(); @@ -60,13 +67,16 @@ class OC_Migrate{ foreach( self::$providers as $provider ){ // Check for database.xml if(file_exists(OC::$SERVERROOT.'/apps/'.$provider->id.'/appinfo/database.xml')){ - if(!self::createAppTables( $provider->id )){ - $ok = false; - OC_Log::write('migration','failed to create migration tables for: '.$provider->id,OC_Log::INFO); - } + $ok = self::createAppTables( $provider->id ); } if($ok){ + // Run the export function provided by the providor $return[$provider->id]['success'] = $provider->export( $uid ); + } else { + // Log the error + OC_Log::write('migration','failed to create migration tables for: '.$provider->id,OC_Log::INFO); + $return[$provider->id]['success'] = 'false'; + $return[$provider->id]['message'] = 'failed to create the app tables'; } } @@ -79,10 +89,16 @@ class OC_Migrate{ * @param $uid optional uid to use * @return bool if the import succedded */ - public static function import( $uid=null ){ + public static function import( $uid=false ){ self::$uid = $uid; + if(!self::$uid){ + OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL); + return false; + exit(); + } + // Connect to the db if(!self::connectDB()){ return false; @@ -104,7 +120,12 @@ class OC_Migrate{ // @breif connects to migration.db, or creates if not found // @return bool whether the operation was successful private static function connectDB(){ - + OC_Log::write('migration','connecting to migration.db for user: '.self::$uid,OC_Log::INFO); + // Fail if no user is set + if(!self::$uid){ + OC_Log::write('migration','connectDB() called without self::$uid being set',OC_Log::INFO); + return false; + } // Already connected if(!self::$MDB2){ require_once('MDB2.php'); @@ -130,7 +151,7 @@ class OC_Migrate{ // Die if we could not connect if( PEAR::isError( self::$MDB2 )){ die(self::$MDB2->getMessage()); - OC_Log::write('migration', 'Failed to create migration.db',OC_Log::FATAL); + OC_Log::write('migration', 'Failed to create/connect to migration.db',OC_Log::FATAL); OC_Log::write('migration',self::$MDB2->getUserInfo(),OC_Log::FATAL); OC_Log::write('migration',self::$MDB2->getMessage(),OC_Log::FATAL); return false; @@ -191,7 +212,9 @@ class OC_Migrate{ if( !array_key_exists( 'table', $options ) ){ return false; } - + + $return = array(); + // Need to include 'where' in the query? if( array_key_exists( 'matchval', $options ) && array_key_exists( 'matchcol', $options ) ){ @@ -204,19 +227,19 @@ class OC_Migrate{ // Run the query for this match value (where x = y value) $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] . " WHERE " . $options['matchcol'] . " LIKE ?" ); $results = $query->execute( array( $matchval ) ); - self::insertData( $results, $options ); - + $return = self::insertData( $results, $options ); + //$return = array_merge( $return, $newreturns ); } } else { // Just get everything $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] ); $results = $query->execute(); - self::insertData( $results, $options ); + $return = self::insertData( $results, $options ); } - return true; + return $return; } @@ -225,8 +248,11 @@ class OC_Migrate{ // @param $options array of copyRows options // @return void private static function insertData( $data, $options ){ + $return = array(); while( $row = $data->fetchRow() ){ // Now save all this to the migration.db + $fields = array(); + $values = array(); foreach($row as $field=>$value){ $fields[] = $field; $values[] = $value; @@ -242,10 +268,21 @@ class OC_Migrate{ $query = self::prepare( $sql ); if(!$query){ OC_Log::write('migration','Invalid sql produced: '.$sql,OC_Log::FATAL); + return false; + exit(); } else { $query->execute( $values ); + // Do we need to return some values? + if( array_key_exists( 'idcol', $options ) ){ + // Yes we do + $return[] = $row[$options['idcol']]; + } else { + // Take a guess and return the first field :) + $return[] = reset($row); + } } } + return $return; } // @breif creates the tables in migration.db from an apps database.xml @@ -263,7 +300,7 @@ class OC_Migrate{ $content = file_get_contents( $file ); $file2 = 'static://db_scheme'; - $content = str_replace( '*dbname*', 'migration', $content ); + $content = str_replace( '*dbname*', self::$uid.'/migration', $content ); $content = str_replace( '*dbprefix*', '', $content ); file_put_contents( $file2, $content ); -- cgit v1.2.3 From a2d7e9c6e8b951848c53dc2b159e1ff4b92642c5 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sun, 11 Mar 2012 22:13:50 +0000 Subject: Merge returns from insertData() --- lib/migrate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index 28329a8170b..b8dbcc475ee 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -227,8 +227,8 @@ class OC_Migrate{ // Run the query for this match value (where x = y value) $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] . " WHERE " . $options['matchcol'] . " LIKE ?" ); $results = $query->execute( array( $matchval ) ); - $return = self::insertData( $results, $options ); - //$return = array_merge( $return, $newreturns ); + $newreturns = self::insertData( $results, $options ); + $return = array_merge( $return, $newreturns ); } } else { -- cgit v1.2.3 From 1cdb4396a4bc71ee564e73144bfdcd74a1a7493b Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Mon, 12 Mar 2012 18:40:14 +0000 Subject: Fix copyRows() return value. Generate app info and oc info on return --- apps/user_migrate/settings.php | 2 +- lib/migrate.php | 121 ++++++++++++++++++++++++----------------- 2 files changed, 72 insertions(+), 51 deletions(-) (limited to 'lib') diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index c35d46b3511..c1121f2ddf5 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -50,7 +50,7 @@ if (isset($_POST['user_migrate'])) { // Call to OC_Migrate for the xml file. // Create migration.db - var_dump(OC_Migrate::export(OC_User::getUser())); + OC_Migrate::export(OC_User::getUser()); // Add export db to zip $zip->addFile($root.'data/'.$user.'/migration.db', "migration.db"); diff --git a/lib/migrate.php b/lib/migrate.php index b8dbcc475ee..95da4514fcc 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -65,20 +65,42 @@ class OC_Migrate{ // Foreach provider foreach( self::$providers as $provider ){ - // Check for database.xml + + $failed = false; + + // Does this app use the database? if(file_exists(OC::$SERVERROOT.'/apps/'.$provider->id.'/appinfo/database.xml')){ - $ok = self::createAppTables( $provider->id ); + // Create some app tables + $tables = self::createAppTables( $provider->id ); + if( is_array( $tables ) ){ + // Save the table names + foreach($tables as $table){ + $return['app'][$provider->id]['tables'][] = $table; + } + } else { + // It failed to create the tables + $failed = true; + } } - if($ok){ - // Run the export function provided by the providor - $return[$provider->id]['success'] = $provider->export( $uid ); + + // Run the import function? + if( !$failed ){ + $return['app'][$provider->id]['success'] = $provider->export( $uid ); } else { - // Log the error - OC_Log::write('migration','failed to create migration tables for: '.$provider->id,OC_Log::INFO); - $return[$provider->id]['success'] = 'false'; - $return[$provider->id]['message'] = 'failed to create the app tables'; + $return['app'][$provider->id]['success'] = false; + $return['app'][$provider->id]['message'] = 'failed to create the app tables'; } + + // Now add some app info the the return array + $appinfo = OC_App::getAppInfo( $provider->id ); + $return['app'][$provider->id]['version'] = $appinfo['version']; + } + + + // Add some general info to the return array + $return['migrateinfo']['uid'] = $uid; + $return['migrateinfo']['ocversion'] = OC_Util::getVersionString(); return $return; @@ -289,50 +311,49 @@ class OC_Migrate{ // @param $appid string id of the app // @return bool whether the operation was successful private static function createAppTables( $appid ){ - $file = OC::$SERVERROOT.'/apps/'.$appid.'/appinfo/database.xml'; - if(file_exists( $file )){ - - if(!self::connectScheme()){ - return false; - } - - // There is a database.xml file - $content = file_get_contents( $file ); - - $file2 = 'static://db_scheme'; - $content = str_replace( '*dbname*', self::$uid.'/migration', $content ); - $content = str_replace( '*dbprefix*', '', $content ); - - file_put_contents( $file2, $content ); - - // Try to create tables - $definition = self::$schema->parseDatabaseDefinitionFile( $file2 ); - - unlink( $file2 ); - - // Die in case something went wrong - if( $definition instanceof MDB2_Schema_Error ){ - OC_Log::write('migration','Failed to parse database.xml for: '.$appid,OC_Log::FATAL); - OC_Log::write('migration',$definition->getMessage().': '.$definition->getUserInfo(),OC_Log::FATAL); - return false; - } - - $definition['overwrite'] = true; - - $ret = self::$schema->createDatabase( $definition ); - // Die in case something went wrong - if( $ret instanceof MDB2_Error ){ - OC_Log::write('migration','Failed to create tables for: '.$appid,OC_Log::FATAL); - OC_Log::write('migration',$ret->getMessage().': '.$ret->getUserInfo(),OC_Log::FATAL); - return false; - } - return true; - - } else { - // No database.xml + if(!self::connectScheme()){ return false; } + + // There is a database.xml file + $content = file_get_contents( OC::$SERVERROOT . '/apps/' . $appid . '/appinfo/database.xml' ); + + $file2 = 'static://db_scheme'; + $content = str_replace( '*dbname*', self::$uid.'/migration', $content ); + $content = str_replace( '*dbprefix*', '', $content ); + + $xml = new SimpleXMLElement($content); + foreach($xml->table as $table){ + $tables[] = (string)$table->name; + } + + file_put_contents( $file2, $content ); + + // Try to create tables + $definition = self::$schema->parseDatabaseDefinitionFile( $file2 ); + + unlink( $file2 ); + + // Die in case something went wrong + if( $definition instanceof MDB2_Schema_Error ){ + OC_Log::write('migration','Failed to parse database.xml for: '.$appid,OC_Log::FATAL); + OC_Log::write('migration',$definition->getMessage().': '.$definition->getUserInfo(),OC_Log::FATAL); + return false; + } + + $definition['overwrite'] = true; + + $ret = self::$schema->createDatabase( $definition ); + // Die in case something went wrong + + if( $ret instanceof MDB2_Error ){ + OC_Log::write('migration','Failed to create tables for: '.$appid,OC_Log::FATAL); + OC_Log::write('migration',$ret->getMessage().': '.$ret->getUserInfo(),OC_Log::FATAL); + return false; + } + return $tables; + } -- cgit v1.2.3 From d108bdc7c7940d23355c9a16f3f355387bbb66ef Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Mon, 12 Mar 2012 21:41:32 +0000 Subject: Improved import function. Added param to connectDB() to load the db from the import --- lib/migrate.php | 68 +++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 54 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index 95da4514fcc..075358c72b7 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -30,6 +30,7 @@ class OC_Migrate{ static private $providers=array(); static private $schema=false; static private $uid=false; + static private $database=false; /** * register a new migration provider @@ -108,40 +109,75 @@ class OC_Migrate{ /** * @breif imports a new user + * @param $db string path to migration.db + * @param $migrateinfo string path to the migration info json file * @param $uid optional uid to use * @return bool if the import succedded */ - public static function import( $uid=false ){ - - self::$uid = $uid; - + public static function import( $db, $migrateinfo, $uid=false ){ + if(!self::$uid){ OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL); return false; exit(); } - // Connect to the db - if(!self::connectDB()){ - return false; + // Check if the db exists + if( file_exists( $db ) ){ + // Connect to the db + if(!self::connectDB( $db )){ + return false; + exit(); + } + } else { + OC_Log::write('migration','Migration.db not found at: '.$db, OC_Log::FATAL ); + return false; + exit(); + } + + // Load the json info + if( file_exists( $migrateinfo ) ){ + + } else { + OC_Log::write( 'migration', 'Migration information file not found at: '.$migrateinfo, OC_Log::FATAL ); + return false; + exit(); } + // Process migration info + $info = file_get_contents( $migrateinfo ); + $info = json_decode( $info ); + + // Set the user id + self::$uid = !$uid : $info['migrateinfo']['uid'] ? $uid; + // Create the user if(!self::createUser($uid, $hash)){ return false; + exit(); + } + + $apps = $info['apps']; + + foreach( self::$providers as $provider){ + // Is the app in the export? + if( array_key_exists( $provider->id, $apps ) ){ + // Did it succeed? + if( $app[$provider->id] ){ + // Then do the import + $provider->import(); + } + } } - - // Now get the list of apps to import from migration.db - // Then check for migrate.php for these apps - // If present, run the import function for them. return true; } // @breif connects to migration.db, or creates if not found + // @param $db optional path to migration.db, defaults to user data dir // @return bool whether the operation was successful - private static function connectDB(){ + private static function connectDB( $db=null ){ OC_Log::write('migration','connecting to migration.db for user: '.self::$uid,OC_Log::INFO); // Fail if no user is set if(!self::$uid){ @@ -152,6 +188,8 @@ class OC_Migrate{ if(!self::$MDB2){ require_once('MDB2.php'); + self::$database = !is_null( $db ) ? $db : $datadir.'/'.self::$uid.'/migration.db'; + $datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ); // Prepare options array @@ -164,7 +202,7 @@ class OC_Migrate{ ); $dsn = array( 'phptype' => 'sqlite3', - 'database' => $datadir.'/'.self::$uid.'/migration.db', + 'database' => self::$database, 'mode' => '0644' ); @@ -392,7 +430,9 @@ class OC_Migrate{ // Create the user $query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" ); $result = $query->execute( array( $uid, $data['hash'])); - + if( !$result ){ + OC_Log::write('migration', 'Failed to create the new user "'.$uid.""); + } return $result ? true : false; } -- cgit v1.2.3 From 4d5646a59f813e09455c78d840d0f62397ec60ad Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 13 Mar 2012 16:21:17 +0000 Subject: Find migrate.php even for disabled apps. Improve ui for user and admin migrations --- apps/admin_export/settings.php | 17 +++--- apps/bookmarks/appinfo/app.php | 4 -- apps/bookmarks/lib/migrate.php | 1 + apps/user_migrate/settings.php | 91 +++++++++++++++++++------------- apps/user_migrate/templates/settings.php | 15 ++++-- lib/migrate.php | 49 ++++++++--------- 6 files changed, 99 insertions(+), 78 deletions(-) (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index 5584181fbb9..b60557f3503 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -74,15 +74,16 @@ if (isset($_POST['admin_export'])) { } if (isset($_POST['user_files'])) { - // needs to handle data outside of the default data dir. - // adding user files - $zip->addFile($root . '/data/.htaccess', "data/.htaccess"); - $zip->addFile($root . '/data/index.html', "data/index.html"); - foreach (OC_User::getUsers() as $i) { - OC_Log::write('admin_export',"Adding owncloud user files of $i to export",OC_Log::INFO); - zipAddDir($root . "data/" . $i, $zip, true, "/data/"); - } + // needs to handle data outside of the default data dir. + // adding user files + $zip->addFile($root . '/data/.htaccess', "data/.htaccess"); + $zip->addFile($root . '/data/index.html', "data/index.html"); + foreach (OC_User::getUsers() as $i) { + OC_Log::write('admin_export',"Adding owncloud user files of $i to export",OC_Log::INFO); + zipAddDir($root . "data/" . $i, $zip, true, "/data/"); + } } + $zip->close(); header("Content-Type: application/zip"); header("Content-Disposition: attachment; filename=" . basename($filename)); diff --git a/apps/bookmarks/appinfo/app.php b/apps/bookmarks/appinfo/app.php index 13d76e08179..b9c308ca053 100644 --- a/apps/bookmarks/appinfo/app.php +++ b/apps/bookmarks/appinfo/app.php @@ -18,8 +18,4 @@ OC_App::addNavigationEntry( array( 'id' => 'bookmarks_index', 'order' => 70, 'hr OC_App::registerPersonal('bookmarks', 'settings'); OC_Util::addScript('bookmarks','bookmarksearch'); -// Include the migration provider - -require_once('apps/bookmarks/lib/migrate.php'); - OC_Search::registerProvider('OC_Search_Provider_Bookmarks'); diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index 451699ec15f..8387f706038 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -54,4 +54,5 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ } +// Load the provider new OC_Migrate_Provider_Bookmarks( 'bookmarks' ); \ No newline at end of file diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index c1121f2ddf5..3b82e148b5c 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -3,6 +3,8 @@ /** * ownCloud - user_migrate * + * @author Thomas Schmidt + * @copyright 2011 Thomas Schmidt tom@opensuse.org * @author Tom Needham * @copyright 2012 Tom Needham tom@owncloud.com * @@ -22,52 +24,67 @@ */ OC_Util::checkAppEnabled('user_migrate'); +define('DS', '/'); -if (isset($_POST['user_migrate'])) { - // Looks like they want to migrate - $root = OC::$SERVERROOT . "/"; - $user = OC_User::getUser(); +if (isset($_POST['user_export'])) { + + // Setup the export $zip = new ZipArchive(); - $tempdir = get_temp_dir(); - $filename = $tempdir . "/" . $user . "_export_" . date("y-m-d_H-i-s") . ".zip"; - OC_Log::write('user_migrate',"Creating user export file at: " . $filename,OC_Log::INFO); - if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) { - exit("Cannot open <$filename>\n"); + $tmp = get_temp_dir(); + $user = OC_User::getUser(); + // Create owncoud dir + if( !file_exists( $tmp . '/owncloud' ) ){ + if( !mkdir( $tmp . '/owncloud' ) ){ + die('Failed to create the owncloud tmp directory'); + } } - - // Does the user want to include their files? - if (isset($_POST['user_files'])) { - // needs to handle data outside of the default data dir. - // adding user files - OC_Log::write('user_migrate',"Adding owncloud user files of $user to export",OC_Log::INFO); - zipAddDir($root . "data/" . $user, $zip, true, "files/"); + // Create the export dir + $exportdir = $tmp . '/owncloud' . '/export_' . $user . '_' . date("y-m-d_H-i-s"); + if( !file_exists( $exportdir ) ){ + if( !mkdir( $exportdir ) ){ + die('Failed to create the owncloud export directory'); + } } - - // Does the user want their app data? - if (isset($_POST['user_appdata'])) { - // adding owncloud system files - OC_Log::write('user_migrate',"Adding app data to user export file",OC_Log::INFO); - // Call to OC_Migrate for the xml file. - - // Create migration.db - OC_Migrate::export(OC_User::getUser()); - // Add export db to zip - $zip->addFile($root.'data/'.$user.'/migration.db', "migration.db"); - + $filename = $exportdir . '/owncloud_export_' . $user . '_' . date("y-m-d_H-i-s") . ".zip"; + OC_Log::write('user_migrate',"Creating export file at: " . $filename,OC_Log::INFO); + if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) { + exit("Cannot open <$filename>\n"); } + + // Migrate the app info + $info = OC_Migrate::export( $user ); + $infofile = $exportdir . '/exportinfo.json'; + if( !file_put_contents( $infofile, $info ) ){ + die('Failed to save the export info'); + } + $zip->addFile( $infofile, "exportinfo.json"); + $zip->addFile(OC::$SERVERROOT . '/data/' . $user . '/migration.db', "migration.db"); + // Add the data dir + zipAddDir(OC::$SERVERROOT . "/data/" . $user, $zip, true, "files/"); + + // Save the zip $zip->close(); - - //header("Content-Type: application/zip"); - //header("Content-Disposition: attachment; filename=" . basename($filename)); - //header("Content-Length: " . filesize($filename)); - //readfile($filename); - //unlink($filename); -} else { -// fill template + // Send the zip + header("Content-Type: application/zip"); + header("Content-Disposition: attachment; filename=" . basename($filename)); + header("Content-Length: " . filesize($filename)); + @ob_end_clean(); + readfile($filename); + // Cleanup + unlink($filename); + unlink($infofile); + rmdir($exportdir); + +} if( isset( $_POST['user_import'] ) ){ + // TODO +}else { + + // fill template $tmpl = new OC_Template('user_migrate', 'settings'); return $tmpl->fetchPage(); + } function zipAddDir($dir, $zip, $recursive=true, $internalDir='') { @@ -89,6 +106,6 @@ function zipAddDir($dir, $zip, $recursive=true, $internalDir='') { } closedir($dirhandle); } else { - OC_Log::write('admin_export',"Was not able to open directory: " . $dir,OC_Log::ERROR); + OC_Log::write('user_migrate',"Was not able to open directory: " . $dir,OC_Log::ERROR); } } diff --git a/apps/user_migrate/templates/settings.php b/apps/user_migrate/templates/settings.php index ece8f70e064..59a27a926d8 100644 --- a/apps/user_migrate/templates/settings.php +++ b/apps/user_migrate/templates/settings.php @@ -1,12 +1,17 @@
t('Export your user account');?> -

t('This will create a compressed file that contains the data of owncloud account. - Please choose which components should be included:');?> +

t('This will create a compressed file that contains your ownCloud account.');?>

-


-
+ +

+ +
+
+ t('Import user account');?> +

+

- +
diff --git a/lib/migrate.php b/lib/migrate.php index 075358c72b7..88c0e7cfc28 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -26,11 +26,16 @@ */ class OC_Migrate{ + // Holds the db object static private $MDB2=false; + // Array of OC_Migration_Provider objects static private $providers=array(); + // Schema db object static private $schema=false; + // User id of the user to import/export static private $uid=false; - static private $database=false; + // Path to the sqlite db + static private $dbpath=false; /** * register a new migration provider @@ -64,9 +69,18 @@ class OC_Migrate{ $ok = true; $return = array(); + // Find the providers + $apps = OC_App::getAllApps(); + + foreach($apps as $app){ + $path = OC::$SERVERROOT . '/apps/' . $app . '/lib/migrate.php'; + if( file_exists( $path ) ){ + include( $path ); + } + } + // Foreach provider foreach( self::$providers as $provider ){ - $failed = false; // Does this app use the database? @@ -110,7 +124,7 @@ class OC_Migrate{ /** * @breif imports a new user * @param $db string path to migration.db - * @param $migrateinfo string path to the migration info json file + * @param $migrateinfo array of migration ino * @param $uid optional uid to use * @return bool if the import succedded */ @@ -135,27 +149,14 @@ class OC_Migrate{ exit(); } - // Load the json info - if( file_exists( $migrateinfo ) ){ - - } else { - OC_Log::write( 'migration', 'Migration information file not found at: '.$migrateinfo, OC_Log::FATAL ); + if( !is_array( $migrateinfo ) ){ + OC_Log::write('migration','$migrateinfo is not an array', OC_Log::FATAL); return false; exit(); } - // Process migration info - $info = file_get_contents( $migrateinfo ); - $info = json_decode( $info ); - // Set the user id - self::$uid = !$uid : $info['migrateinfo']['uid'] ? $uid; - - // Create the user - if(!self::createUser($uid, $hash)){ - return false; - exit(); - } + self::$uid = $info['migrateinfo']['uid']; $apps = $info['apps']; @@ -177,7 +178,7 @@ class OC_Migrate{ // @breif connects to migration.db, or creates if not found // @param $db optional path to migration.db, defaults to user data dir // @return bool whether the operation was successful - private static function connectDB( $db=null ){ + private static function connectDB( $dbpath=null ){ OC_Log::write('migration','connecting to migration.db for user: '.self::$uid,OC_Log::INFO); // Fail if no user is set if(!self::$uid){ @@ -188,10 +189,10 @@ class OC_Migrate{ if(!self::$MDB2){ require_once('MDB2.php'); - self::$database = !is_null( $db ) ? $db : $datadir.'/'.self::$uid.'/migration.db'; - $datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ); + self::$dbpath = $datadir.'/'.self::$uid.'/migration.db';//!is_null( $dbpath ) ? $dbpath : $datadir.'/'.self::$uid.'/migration.db'; + // Prepare options array $options = array( 'portability' => MDB2_PORTABILITY_ALL & (!MDB2_PORTABILITY_FIX_CASE), @@ -202,10 +203,10 @@ class OC_Migrate{ ); $dsn = array( 'phptype' => 'sqlite3', - 'database' => self::$database, + 'database' => self::$dbpath, 'mode' => '0644' ); - + // Try to establish connection self::$MDB2 = MDB2::factory( $dsn, $options ); // Die if we could not connect -- cgit v1.2.3 From 0f3eebbbd95b75e3dfe8f9322d8fd486925ac54a Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 13 Mar 2012 17:18:42 +0000 Subject: added cleanUp() method to OC_Migrate --- apps/user_migrate/settings.php | 2 +- lib/migrate.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 00f46660cf5..c017ba226cf 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -70,8 +70,8 @@ if (isset($_POST['user_export'])) { readfile($filename); // Cleanup unlink($filename); - unlink($infofile); rmdir($exportdir); + OC_Migrate::cleanUp(); } if( isset( $_POST['user_import'] ) ){ // TODO diff --git a/lib/migrate.php b/lib/migrate.php index 88c0e7cfc28..1be229d8fdd 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -438,4 +438,19 @@ class OC_Migrate{ } + /** + * @breif removes migration.db and exportinfo.json from the users data dir + * @return void + */ + static public function cleanUp(){ + if( !self::$uid ){ + OC_Log::write('migration', 'Failed to cleanup after migration', OC_Log::ERROR); + return false; + } + // Remove migration.db + unlink( OC::$SERVERROOT . '/data/' . self::$uid . '/migration.db' ); + // Remove exportinfo.json + unlink( OC::$SERVERROOT . '/data/' . self::$uid . '/exportinfo.json' ); + return true; + } } -- cgit v1.2.3 From cd2f75fdad6629f850bb07f07cd45e04d75de97d Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 13 Mar 2012 17:27:47 +0000 Subject: Use data dir from config.php --- apps/admin_export/settings.php | 4 ++-- apps/user_migrate/settings.php | 22 +++++----------------- lib/migrate.php | 5 +++-- 3 files changed, 10 insertions(+), 21 deletions(-) (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index b60557f3503..73a4209d3f8 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -31,6 +31,7 @@ define('DS', '/'); if (isset($_POST['admin_export'])) { $root = OC::$SERVERROOT . "/"; + $datadir = OC_Config::getValue( 'datadirectory' ); $zip = new ZipArchive(); $tempdir = get_temp_dir(); $filename = $tempdir . "/owncloud_export_" . date("y-m-d_H-i-s") . ".zip"; @@ -70,7 +71,6 @@ if (isset($_POST['admin_export'])) { OC_Log::write('admin_export',"Adding owncloud config to export",OC_Log::INFO); zipAddDir($root . "config/", $zip, true, "/"); - $zip->addFile($root . '/data/.htaccess', "data/owncloud.db"); } if (isset($_POST['user_files'])) { @@ -80,7 +80,7 @@ if (isset($_POST['admin_export'])) { $zip->addFile($root . '/data/index.html', "data/index.html"); foreach (OC_User::getUsers() as $i) { OC_Log::write('admin_export',"Adding owncloud user files of $i to export",OC_Log::INFO); - zipAddDir($root . "data/" . $i, $zip, true, "/data/"); + zipAddDir($datadir . '/' . $i, $zip, true, "/data/"); } } diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index c017ba226cf..5e8ac9c21df 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -32,20 +32,9 @@ if (isset($_POST['user_export'])) { $zip = new ZipArchive(); $tmp = get_temp_dir(); $user = OC_User::getUser(); - // Create owncoud dir - if( !file_exists( $tmp . '/owncloud' ) ){ - if( !mkdir( $tmp . '/owncloud' ) ){ - die('Failed to create the owncloud tmp directory'); - } - } - // Create the export dir - $exportdir = $tmp . '/owncloud' . '/export_' . $user . '_' . date("y-m-d_H-i-s"); - if( !file_exists( $exportdir ) ){ - if( !mkdir( $exportdir ) ){ - die('Failed to create the owncloud export directory'); - } - } - $filename = $exportdir . '/owncloud_export_' . $user . '_' . date("y-m-d_H-i-s") . ".zip"; + + $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . $user; + $filename = $userdatadir . '/owncloud_export_' . $user . '_' . date("y-m-d_H-i-s") . ".zip"; OC_Log::write('user_migrate',"Creating export file at: " . $filename,OC_Log::INFO); if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) { exit("Cannot open <$filename>\n"); @@ -53,11 +42,11 @@ if (isset($_POST['user_export'])) { // Migrate the app info $info = json_encode( OC_Migrate::export( $user ) ); - $infofile = OC::$SERVERROOT . '/data/' . $user . '/exportinfo.json'; + $infofile = $userdatadir . '/exportinfo.json'; file_put_contents( $infofile, $info ); // Add the data dir (which includes migration.db and exportinfo.json) - zipAddDir(OC::$SERVERROOT . "/data/" . $user, $zip, true, "/"); + zipAddDir( $userdatadir, $zip, true, "/" ); // Save the zip $zip->close(); @@ -70,7 +59,6 @@ if (isset($_POST['user_export'])) { readfile($filename); // Cleanup unlink($filename); - rmdir($exportdir); OC_Migrate::cleanUp(); } if( isset( $_POST['user_import'] ) ){ diff --git a/lib/migrate.php b/lib/migrate.php index 1be229d8fdd..a6a6432d0de 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -447,10 +447,11 @@ class OC_Migrate{ OC_Log::write('migration', 'Failed to cleanup after migration', OC_Log::ERROR); return false; } + $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . self::$uid; // Remove migration.db - unlink( OC::$SERVERROOT . '/data/' . self::$uid . '/migration.db' ); + unlink( $userdatadir . '/migration.db' ); // Remove exportinfo.json - unlink( OC::$SERVERROOT . '/data/' . self::$uid . '/exportinfo.json' ); + unlink( $userdatadir . '/exportinfo.json' ); return true; } } -- cgit v1.2.3 From fa8b66ca4f266ebc72ca204e85cf0e69c4c4aa25 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 13 Mar 2012 21:28:53 +0000 Subject: Add getApps() method to return apps supporting migration --- lib/migrate.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index a6a6432d0de..863cf261f4e 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -121,6 +121,21 @@ class OC_Migrate{ } + /** + * @breif returns an array of apps that support migration + * @return array + */ + static public function getApps(){ + $allapps = OC_App::getAllApps(); + foreach($allapps as $app){ + $path = OC::$SERVERROOT . '/apps/' . $app . '/lib/migrate.php'; + if( file_exists( $path ) ){ + $supportsmigration[] = $app; + } + } + reutrn $supportsmigration; + } + /** * @breif imports a new user * @param $db string path to migration.db -- cgit v1.2.3 From 5a50144a16fa9b5d8caf9ee261e3c4a39eaa04bc Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 13 Mar 2012 21:29:31 +0000 Subject: typo :/ --- lib/migrate.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index 863cf261f4e..acc01ec7bbd 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -133,7 +133,7 @@ class OC_Migrate{ $supportsmigration[] = $app; } } - reutrn $supportsmigration; + return $supportsmigration; } /** -- cgit v1.2.3 From a310a81053c31205abd6d62491304705b1f565e2 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 13 Mar 2012 23:09:43 +0000 Subject: move zip creation inside OC_Migrate --- apps/user_migrate/settings.php | 77 ++++++---------------------- lib/migrate.php | 112 ++++++++++++++++++++++++++++++++++------- lib/migrate/provider.php | 5 +- 3 files changed, 111 insertions(+), 83 deletions(-) (limited to 'lib') diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 5e8ac9c21df..9fbb4da9e56 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -24,72 +24,27 @@ */ OC_Util::checkAppEnabled('user_migrate'); -define('DS', '/'); - if (isset($_POST['user_export'])) { - - // Setup the export - $zip = new ZipArchive(); - $tmp = get_temp_dir(); - $user = OC_User::getUser(); - - $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . $user; - $filename = $userdatadir . '/owncloud_export_' . $user . '_' . date("y-m-d_H-i-s") . ".zip"; - OC_Log::write('user_migrate',"Creating export file at: " . $filename,OC_Log::INFO); - if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) { - exit("Cannot open <$filename>\n"); - } - - // Migrate the app info - $info = json_encode( OC_Migrate::export( $user ) ); - $infofile = $userdatadir . '/exportinfo.json'; - file_put_contents( $infofile, $info ); - - // Add the data dir (which includes migration.db and exportinfo.json) - zipAddDir( $userdatadir, $zip, true, "/" ); - - // Save the zip - $zip->close(); - - // Send the zip - header("Content-Type: application/zip"); - header("Content-Disposition: attachment; filename=" . basename($filename)); - header("Content-Length: " . filesize($filename)); - @ob_end_clean(); - readfile($filename); - // Cleanup - unlink($filename); - OC_Migrate::cleanUp(); - + // Create the export zip + $user = OC_User::getUser(); + $path = OC_Config::getValue( 'datadirectory' ) . '/' . OC_User::getUser() . '/'; + if( OC_Migrate::createExportFile( $user, $path ) ){ + // Download it then + header("Content-Type: application/zip"); + header("Content-Disposition: attachment; filename=" . basename($path)); + header("Content-Length: " . filesize($path)); + @ob_end_clean(); + readfile($path); + OC_Migrate::cleanUp(); + } else { + die('error'); + } } if( isset( $_POST['user_import'] ) ){ // TODO }else { - // fill template - $tmpl = new OC_Template('user_migrate', 'settings'); - return $tmpl->fetchPage(); - + $tmpl = new OC_Template('user_migrate', 'settings'); + return $tmpl->fetchPage(); } -function zipAddDir($dir, $zip, $recursive=true, $internalDir='') { - $dirname = basename($dir); - $zip->addEmptyDir($internalDir . $dirname); - $internalDir.=$dirname.='/'; - if ($dirhandle = opendir($dir)) { - while (false !== ( $file = readdir($dirhandle))) { - - if (( $file != '.' ) && ( $file != '..' )) { - - if (is_dir($dir . '/' . $file) && $recursive) { - zipAddDir($dir . '/' . $file, $zip, $recursive, $internalDir); - } elseif (is_file($dir . '/' . $file)) { - $zip->addFile($dir . '/' . $file, $internalDir . $file); - } - } - } - closedir($dirhandle); - } else { - OC_Log::write('user_migrate',"Was not able to open directory: " . $dir,OC_Log::ERROR); - } -} diff --git a/lib/migrate.php b/lib/migrate.php index acc01ec7bbd..b4c4d635ff3 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -36,6 +36,10 @@ class OC_Migrate{ static private $uid=false; // Path to the sqlite db static private $dbpath=false; + // Holds the ZipArchive object + static private $zip=false; + // String path to export + static private $zippath=false; /** * register a new migration provider @@ -47,24 +51,10 @@ class OC_Migrate{ /** * @breif creates a migration.db in the users data dir with their app data in - * @param @uid string userid of the user to export for * @return bool whether operation was successfull */ - public static function export( $uid ){ - - // Only export database users, otherwise we get chaos - if(!OC_User_Database::userExists( $uid )){ - return false; - } - - self::$uid = $uid; - - if(empty(self::$uid)){ - OC_Log::write('migration','Invalid uid passed',OC_Log::FATAL); - return false; - exit(); - } - + private static function exportAppData( ){ + self::connectDB(); $ok = true; $return = array(); @@ -100,7 +90,7 @@ class OC_Migrate{ // Run the import function? if( !$failed ){ - $return['app'][$provider->id]['success'] = $provider->export( $uid ); + $return['app'][$provider->id]['success'] = $provider->export( self::$uid ); } else { $return['app'][$provider->id]['success'] = false; $return['app'][$provider->id]['message'] = 'failed to create the app tables'; @@ -114,13 +104,95 @@ class OC_Migrate{ // Add some general info to the return array - $return['migrateinfo']['uid'] = $uid; + $return['migrateinfo']['uid'] = self::$uid; $return['migrateinfo']['ocversion'] = OC_Util::getVersionString(); return $return; } + /** + * @breif creates a zip user export + * @param $uid string user id of the user to export + * @param $path string path to folder to create file in (with trailing slash) + * @return bool success + */ + static public function createExportFile( $uid, $path ){ + // Is a directory + if( !is_dir( $path ) ){ + OC_Log::write('migration', 'Path supplied to createExportFile() is not a directory', OC_Log::ERROR); + return false; + exit(); + } + // Is writeable + if( !is_writeable( $path ) ){ + OC_Log::write('migration', 'Path supplied to createExportFile() is not writeable', OC_Log::ERROR); + return false; + exit(); + } + // Is a database user? + if( !OC_User_Database::userExists( $uid ) ){ + OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR); + return false; + exit(); + } + + self::$uid = $uid; + self::$zip = new ZipArchive; + + // Get some info + $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . self::$uid; + self::$zippath = $path . 'owncloud_export_' . self::$uid . '_' . date("y-m-d_H-i-s") . ".zip"; + if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE ) !== TRUE ) { + OC_Log::write('migration','Cannot create a zip file at: '.self::$zippath, OC_Log::ERROR); + return false; + exit(); + } + + // Export the app info + $info = json_encode( self::exportAppData() ); + file_put_contents( $userdatadir . '/exportinfo.json', $info ); + + // Add the data dir (which includes migration.db and exportinfo.json) + self::addDirToZip( $userdatadir, '/' ); + + // All done! + if( !self::$zip->close() ){ + OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR); + return false; + exit(); + } else { + OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO); + return true; + } + + + } + + /** + * @breif adds a directory to the zip object + * @return void + */ + static private function addDirToZip( $dir, $recursive=true, $internalDir='' ){ + $dirname = basename($dir); + self::$zip->addEmptyDir($internalDir . $dirname); + $internalDir.=$dirname.='/'; + if ($dirhandle = opendir($dir)) { + while (false !== ( $file = readdir($dirhandle))) { + if (( $file != '.' ) && ( $file != '..' )) { + if (is_dir($dir . '/' . $file) && $recursive) { + self::addDirToZip($dir . '/' . $file, $recursive, $internalDir); + } elseif (is_file($dir . '/' . $file)) { + self::$zip->addFile($dir . '/' . $file, $internalDir . $file); + } + } + } + closedir($dirhandle); + } else { + OC_Log::write('migration',"Was not able to open directory: " . $dir,OC_Log::ERROR); + } + } + /** * @breif returns an array of apps that support migration * @return array @@ -143,7 +215,7 @@ class OC_Migrate{ * @param $uid optional uid to use * @return bool if the import succedded */ - public static function import( $db, $migrateinfo, $uid=false ){ + public static function importAppData( $db, $migrateinfo, $uid=false ){ if(!self::$uid){ OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL); @@ -467,6 +539,8 @@ class OC_Migrate{ unlink( $userdatadir . '/migration.db' ); // Remove exportinfo.json unlink( $userdatadir . '/exportinfo.json' ); + // Remove the zip + unlink(self::$zippath); return true; } } diff --git a/lib/migrate/provider.php b/lib/migrate/provider.php index 9c03639b7c3..e2e01b3b5aa 100644 --- a/lib/migrate/provider.php +++ b/lib/migrate/provider.php @@ -20,9 +20,8 @@ abstract class OC_Migrate_Provider{ /** * @breif imports data for the app - * @param $data array of data. eg: array('info'=> APPINFO, 'data'=>APPDATA ARRAY) - * @param $info array of info of the source install + * @param $info array of info including exportinfo.json * @return void */ - abstract function import($data,$uid); + abstract function import( $info ); } -- cgit v1.2.3 From c9be325af2707b256f83cafbda3f7e3713f97876 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Wed, 14 Mar 2012 16:43:06 +0000 Subject: Fix zip creation. Add param to cleanUp() method. Add defaults to createExportFile() method. --- apps/user_migrate/settings.php | 15 +++-- lib/migrate.php | 122 +++++++++++++++++++++++------------------ 2 files changed, 75 insertions(+), 62 deletions(-) (limited to 'lib') diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 9fbb4da9e56..04aca51f518 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -26,22 +26,21 @@ OC_Util::checkAppEnabled('user_migrate'); if (isset($_POST['user_export'])) { // Create the export zip - $user = OC_User::getUser(); - $path = OC_Config::getValue( 'datadirectory' ) . '/' . OC_User::getUser() . '/'; - if( OC_Migrate::createExportFile( $user, $path ) ){ - // Download it then + if( !$path = OC_Migrate::createExportFile() ){ + // Error + die('error'); + } else { + // Download it header("Content-Type: application/zip"); header("Content-Disposition: attachment; filename=" . basename($path)); header("Content-Length: " . filesize($path)); @ob_end_clean(); readfile($path); - OC_Migrate::cleanUp(); - } else { - die('error'); + OC_Migrate::cleanUp( $path ); } } if( isset( $_POST['user_import'] ) ){ // TODO -}else { +} else { // fill template $tmpl = new OC_Template('user_migrate', 'settings'); return $tmpl->fetchPage(); diff --git a/lib/migrate.php b/lib/migrate.php index b4c4d635ff3..728f15e1f6d 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -113,85 +113,100 @@ class OC_Migrate{ /** * @breif creates a zip user export - * @param $uid string user id of the user to export - * @param $path string path to folder to create file in (with trailing slash) - * @return bool success + * @param optional $uid string user id of the user to export (defaults to current) + * @param optional $path string path to folder to create file in (with trailing slash) (defaults to current user's data dir) + * @return false on failure | string path on success */ - static public function createExportFile( $uid, $path ){ - // Is a directory - if( !is_dir( $path ) ){ - OC_Log::write('migration', 'Path supplied to createExportFile() is not a directory', OC_Log::ERROR); - return false; - exit(); - } - // Is writeable - if( !is_writeable( $path ) ){ - OC_Log::write('migration', 'Path supplied to createExportFile() is not writeable', OC_Log::ERROR); - return false; - exit(); - } + static public function createExportFile( $uid=null, $path=null ){ + // User passed? + $uid = is_null( $uid ) ? OC_User::getUser() : $uid ; // Is a database user? if( !OC_User_Database::userExists( $uid ) ){ OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR); return false; exit(); } - + // Set the uid self::$uid = $uid; + // Create the zip object self::$zip = new ZipArchive; - - // Get some info - $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . self::$uid; - self::$zippath = $path . 'owncloud_export_' . self::$uid . '_' . date("y-m-d_H-i-s") . ".zip"; - if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE ) !== TRUE ) { - OC_Log::write('migration','Cannot create a zip file at: '.self::$zippath, OC_Log::ERROR); - return false; - exit(); + // Calculate users data dir + $user = OC_User::getUser(); + $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . $user . '/'; + // Calculate zip name + $zipname = "owncloud_userexport_" . $user . '_' . date("y-m-d_H-i-s") . ".zip"; + // Calculate destination + if( !is_null( $path ) ){ + // Path given + // Is a directory? + if( !is_dir( $path ) ){ + OC_Log::write('migration', 'Path supplied to createExportFile() is not a directory', OC_Log::ERROR); + return false; + exit(); + } + // Is writeable + if( !is_writeable( $path ) ){ + OC_Log::write('migration', 'Path supplied to createExportFile() is not writeable', OC_Log::ERROR); + return false; + exit(); + } + self::$zippath = $path . $zipname; + } else { + // Save in users data dir + self::$zippath = $userdatadir . $zipname; } - - // Export the app info + if (self::$zip->open(self::$zippath, ZIPARCHIVE::CREATE) !== TRUE) { + // TODO ADD LOGGING + exit("Cannot open <$filename>\n"); + } + // Export the app info $info = json_encode( self::exportAppData() ); file_put_contents( $userdatadir . '/exportinfo.json', $info ); - - // Add the data dir (which includes migration.db and exportinfo.json) - self::addDirToZip( $userdatadir, '/' ); - - // All done! + // Add the data dir to the zip + self::addDirToZip( $userdatadir ); + // All done! if( !self::$zip->close() ){ OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR); return false; exit(); } else { OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO); - return true; + //return true; } - - + return self::$zippath; } /** * @breif adds a directory to the zip object - * @return void + * @param $dir string path of the directory to add + * @param $recursive bool + * @param $internaldir string path of folder to add dir to in zip + * @return bool */ - static private function addDirToZip( $dir, $recursive=true, $internalDir='' ){ - $dirname = basename($dir); - self::$zip->addEmptyDir($internalDir . $dirname); - $internalDir.=$dirname.='/'; - if ($dirhandle = opendir($dir)) { + static private function addDirToZip($dir, $recursive=true, $internaldir='') { + $dirname = basename($dir); + self::$zip->addEmptyDir($internaldir . $dirname); + $internaldir.=$dirname.='/'; + + if ($dirhandle = opendir($dir)) { while (false !== ( $file = readdir($dirhandle))) { + if (( $file != '.' ) && ( $file != '..' )) { + if (is_dir($dir . '/' . $file) && $recursive) { - self::addDirToZip($dir . '/' . $file, $recursive, $internalDir); + self::addDirToZip($dir . '/' . $file, $recursive, $internaldir); } elseif (is_file($dir . '/' . $file)) { - self::$zip->addFile($dir . '/' . $file, $internalDir . $file); + self::$zip->addFile($dir . '/' . $file, $internaldir . $file); } } } closedir($dirhandle); - } else { - OC_Log::write('migration',"Was not able to open directory: " . $dir,OC_Log::ERROR); - } - } + } else { + OC_Log::write('admin_export',"Was not able to open directory: " . $dir,OC_Log::ERROR); + return false; + } + return true; + } /** * @breif returns an array of apps that support migration @@ -483,7 +498,7 @@ class OC_Migrate{ } - /** + /** * @brief connects to a MDB2 database scheme * @returns true/false * @@ -527,20 +542,19 @@ class OC_Migrate{ /** * @breif removes migration.db and exportinfo.json from the users data dir + * @param optional $path string path to the export zip to delete * @return void */ - static public function cleanUp(){ - if( !self::$uid ){ - OC_Log::write('migration', 'Failed to cleanup after migration', OC_Log::ERROR); - return false; - } + static public function cleanUp( $path=null ){ $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . self::$uid; // Remove migration.db unlink( $userdatadir . '/migration.db' ); // Remove exportinfo.json unlink( $userdatadir . '/exportinfo.json' ); // Remove the zip - unlink(self::$zippath); + if( !is_null( $path ) ){ + unlink( $path ); + } return true; } } -- cgit v1.2.3 From 50233d075c47c86a5a26d4f946f8aa09f703cb15 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Thu, 15 Mar 2012 20:52:43 +0000 Subject: Improve admin_export ui and move system export cde to OC_Migrate --- apps/admin_export/settings.php | 78 ++++------------ apps/admin_export/templates/settings.php | 11 +-- apps/user_migrate/settings.php | 2 +- lib/migrate.php | 150 ++++++++++++++++++++++++++----- 4 files changed, 151 insertions(+), 90 deletions(-) (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index 73a4209d3f8..9db1d75db96 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -28,70 +28,22 @@ OC_Util::checkAppEnabled('admin_export'); define('DS', '/'); - +// Export? if (isset($_POST['admin_export'])) { - $root = OC::$SERVERROOT . "/"; - $datadir = OC_Config::getValue( 'datadirectory' ); - $zip = new ZipArchive(); - $tempdir = get_temp_dir(); - $filename = $tempdir . "/owncloud_export_" . date("y-m-d_H-i-s") . ".zip"; - OC_Log::write('admin_export',"Creating export file at: " . $filename,OC_Log::INFO); - if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) { - exit("Cannot open <$filename>\n"); - } - - if (isset($_POST['owncloud_system'])) { - // adding owncloud system files - OC_Log::write('admin_export',"Adding owncloud system files to export",OC_Log::INFO); - zipAddDir($root, $zip, false); - foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dirname) { - zipAddDir($root . $dirname, $zip, true, "/"); - } - } - - if (isset($_POST['owncloud_config'])) { - // adding owncloud config - // todo: add database export - $dbfile = $tempdir . "/dbexport.xml"; - OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL'); - - // Now add in *dbname* and *dbtableprefix* - $dbexport = file_get_contents( $dbfile ); - - $dbnamestring = "\n\n " . OC_Config::getValue( "dbname", "owncloud" ); - $dbtableprefixstring = "
\n\n " . OC_Config::getValue( "dbtableprefix", "_oc" ); - - $dbexport = str_replace( $dbnamestring, "\n\n *dbname*", $dbexport ); - $dbexport = str_replace( $dbtableprefixstring, "
\n\n *dbprefix*", $dbexport ); - - // Write the new db export file - file_put_contents( $dbfile, $dbexport ); - - $zip->addFile($dbfile, "dbexport.xml"); - - OC_Log::write('admin_export',"Adding owncloud config to export",OC_Log::INFO); - zipAddDir($root . "config/", $zip, true, "/"); - } - - if (isset($_POST['user_files'])) { - // needs to handle data outside of the default data dir. - // adding user files - $zip->addFile($root . '/data/.htaccess', "data/.htaccess"); - $zip->addFile($root . '/data/index.html', "data/index.html"); - foreach (OC_User::getUsers() as $i) { - OC_Log::write('admin_export',"Adding owncloud user files of $i to export",OC_Log::INFO); - zipAddDir($datadir . '/' . $i, $zip, true, "/data/"); - } - } - - $zip->close(); - header("Content-Type: application/zip"); - header("Content-Disposition: attachment; filename=" . basename($filename)); - header("Content-Length: " . filesize($filename)); - @ob_end_clean(); - readfile($filename); - unlink($filename); - unlink($dbfile); + // Create the export zip + if( !$path = OC_Migrate::createSysExportFile( $_POST['export_type'] ) ){ + // Error + die('error'); + } else { + // Download it + header("Content-Type: application/zip"); + header("Content-Disposition: attachment; filename=" . basename($path)); + header("Content-Length: " . filesize($path)); + @ob_end_clean(); + readfile($path); + OC_Migrate::cleanUp( $path ); + } +// Import? } else if( isset($_POST['admin_import']) ){ $root = OC::$SERVERROOT . "/"; diff --git a/apps/admin_export/templates/settings.php b/apps/admin_export/templates/settings.php index 9f0845bf55f..15a19b7c633 100644 --- a/apps/admin_export/templates/settings.php +++ b/apps/admin_export/templates/settings.php @@ -2,12 +2,13 @@
t('Export this ownCloud instance');?>

t('This will create a compressed file that contains the data of this owncloud instance. - Please choose which components should be included:');?> -

-


-
- + Please choose the export type:');?>

+

What would you like to export?

+

+ ownCloud instance ( suitable for import )
+ ownCloud system files
+ Just user files

diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 04aca51f518..d862ac5a82a 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -26,7 +26,7 @@ OC_Util::checkAppEnabled('user_migrate'); if (isset($_POST['user_export'])) { // Create the export zip - if( !$path = OC_Migrate::createExportFile() ){ + if( !$path = OC_Migrate::createUserExportFile() ){ // Error die('error'); } else { diff --git a/lib/migrate.php b/lib/migrate.php index 728f15e1f6d..8f26ea7ae68 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -111,20 +111,120 @@ class OC_Migrate{ } + /** + * @breif creates an export file for the whole system + * @param optional $exporttype string export type ('instance','system' or 'userfiles') + * @param optional $path string path to zip destination (with trailing slash) + * @return path to the zip or false if there was a problem + */ + static public function createSysExportFile( $exporttype='instance', $path=null ){ + // Calculate zip name + $zipname = "owncloud_export_" . date("y-m-d_H-i-s") . ".zip"; + // Get the data dir + $datadir = OC_Config::getValue( 'datadirectory' ); + // Calculate destination + if( !is_null( $path ) ){ + // Path given + // Is a directory? + if( !is_dir( $path ) ){ + OC_Log::write('migration', 'Path supplied to createSysExportFile() is not a directory', OC_Log::ERROR); + return false; + } + // Is writeable + if( !is_writeable( $path ) ){ + OC_Log::write('migration', 'Path supplied to createSysExportFile() is not writeable', OC_Log::ERROR); + return false; + } + self::$zippath = $path . $zipname; + } else { + // Save in tmp dir + $structure = sys_get_temp_dir() . '/owncloudexports/'; + if( !file_exists( $structure ) ){ + if ( !mkdir( $structure ) ) { + OC_Log::write('migration', 'Could not create the temporary export at: '.$structure, OC_Log::ERROR); + return false; + } + } + self::$zippath = $structure . $zipname; + } + // Create the zip object + self::$zip = new ZipArchive; + // Try to create the zip + if( !self::createZip() ){ + return false; + } + // Handle export types + if( $exporttype == 'instance' ){ + // Creates a zip that is compatable with the import function + /* + $dbfile = self:: . "/dbexport.xml"; + OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL'); + + // Now add in *dbname* and *dbtableprefix* + $dbexport = file_get_contents( $dbfile ); + + $dbnamestring = "\n\n " . OC_Config::getValue( "dbname", "owncloud" ); + $dbtableprefixstring = "
\n\n " . OC_Config::getValue( "dbtableprefix", "_oc" ); + + $dbexport = str_replace( $dbnamestring, "\n\n *dbname*", $dbexport ); + $dbexport = str_replace( $dbtableprefixstring, "
\n\n *dbprefix*", $dbexport ); + + // Write the new db export file + file_put_contents( $dbfile, $dbexport ); + + $zip->addFile($dbfile, "dbexport.xml"); + */ + } else if( $exporttype == 'system' ){ + // Creates a zip with the owncloud system files + self::addDirToZip( OC::$SERVERROOT . '/', false); + foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dir) { + self::addDirToZip( OC::$SERVERROOT . '/' . $dir, true, "/"); + } + } else if ( $exporttype == 'userfiles' ){ + // Creates a zip with all of the users files + foreach(OC_User::getUsers() as $user){ + self::addDirToZip( $datadir . '/' . $user . '/', true, "/" . $user); + } + } else { + // Invalid export type supplied + OC_Log::write('migration', 'Invalid export type supplied to createSysExportFile() "'.$exporttype.'"', OC_Log::ERROR); + return false; + } + // Close the zip + if( !self::closeZip() ){ + return false; + } + return self::$zippath; + + } + + /** + * @breif tried to finalise the zip + * @return bool + */ + static private function closeZip(){ + if( !self::$zip->close() ){ + OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR); + return false; + } else { + OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO); + return true; + } + } + /** * @breif creates a zip user export * @param optional $uid string user id of the user to export (defaults to current) * @param optional $path string path to folder to create file in (with trailing slash) (defaults to current user's data dir) * @return false on failure | string path on success */ - static public function createExportFile( $uid=null, $path=null ){ + static public function createUserExportFile( $uid=null, $path=null ){ // User passed? $uid = is_null( $uid ) ? OC_User::getUser() : $uid ; // Is a database user? if( !OC_User_Database::userExists( $uid ) ){ OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR); return false; - exit(); } // Set the uid self::$uid = $uid; @@ -140,41 +240,53 @@ class OC_Migrate{ // Path given // Is a directory? if( !is_dir( $path ) ){ - OC_Log::write('migration', 'Path supplied to createExportFile() is not a directory', OC_Log::ERROR); + OC_Log::write('migration', 'Path supplied to createUserExportFile() is not a directory', OC_Log::ERROR); return false; - exit(); } // Is writeable if( !is_writeable( $path ) ){ - OC_Log::write('migration', 'Path supplied to createExportFile() is not writeable', OC_Log::ERROR); + OC_Log::write('migration', 'Path supplied to createUserExportFile() is not writeable', OC_Log::ERROR); return false; - exit(); } self::$zippath = $path . $zipname; } else { // Save in users data dir self::$zippath = $userdatadir . $zipname; } - if (self::$zip->open(self::$zippath, ZIPARCHIVE::CREATE) !== TRUE) { - // TODO ADD LOGGING - exit("Cannot open <$filename>\n"); + // Try to create the zip + if( !self::createZip() ){ + return false; } // Export the app info $info = json_encode( self::exportAppData() ); file_put_contents( $userdatadir . '/exportinfo.json', $info ); // Add the data dir to the zip self::addDirToZip( $userdatadir ); - // All done! - if( !self::$zip->close() ){ - OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR); - return false; - exit(); - } else { - OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO); - //return true; + // Close the zip + if( !self::closeZip() ){ + return false; } + // All good return self::$zippath; } + + /** + * @breif tries to create the zip + * @return bool + */ + static private function createZip(){ + // Check if properties are set + if( !self::$zip || !self::$zippath ){ + OC_Log::write('migration', 'createZip() called but $zip and/or $zippath have not been set', OC_Log::ERROR); + return false; + } + if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE ) !== TRUE ) { + OC_Log::write('migration', 'Failed to create the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR); + return false; + } else { + return true; + } + } /** * @breif adds a directory to the zip object @@ -235,7 +347,6 @@ class OC_Migrate{ if(!self::$uid){ OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL); return false; - exit(); } // Check if the db exists @@ -243,18 +354,15 @@ class OC_Migrate{ // Connect to the db if(!self::connectDB( $db )){ return false; - exit(); } } else { OC_Log::write('migration','Migration.db not found at: '.$db, OC_Log::FATAL ); return false; - exit(); } if( !is_array( $migrateinfo ) ){ OC_Log::write('migration','$migrateinfo is not an array', OC_Log::FATAL); return false; - exit(); } // Set the user id -- cgit v1.2.3 From c442a06a0217afa8ff284333b0560aeb87db7a55 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Fri, 16 Mar 2012 21:09:36 +0000 Subject: Fix export for admin and users. Added 3 admin export types --- apps/admin_export/settings.php | 9 +- apps/admin_export/templates/settings.php | 2 +- apps/user_migrate/settings.php | 2 +- lib/migrate.php | 144 ++++++++++++++++++------------- 4 files changed, 90 insertions(+), 67 deletions(-) (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index 9db1d75db96..33fca26630a 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -40,12 +40,12 @@ if (isset($_POST['admin_export'])) { header("Content-Disposition: attachment; filename=" . basename($path)); header("Content-Length: " . filesize($path)); @ob_end_clean(); - readfile($path); - OC_Migrate::cleanUp( $path ); + readfile( $path ); + unlink( $path ); } // Import? } else if( isset($_POST['admin_import']) ){ - + /* $root = OC::$SERVERROOT . "/"; $importname = "owncloud_import_" . date("y-m-d_H-i-s"); @@ -85,7 +85,8 @@ if (isset($_POST['admin_export'])) { exit(); } - OC_DB::replaceDB( get_temp_dir() . '/' . $importname . '/dbexport.xml' ); + OC_DB::replaceDB( get_temp_dir() . '/' . $importname . '/dbexport.xml' ); + */ } else { // fill template $tmpl = new OC_Template('admin_export', 'settings'); diff --git a/apps/admin_export/templates/settings.php b/apps/admin_export/templates/settings.php index 15a19b7c633..6d848048c45 100644 --- a/apps/admin_export/templates/settings.php +++ b/apps/admin_export/templates/settings.php @@ -8,7 +8,7 @@

ownCloud instance ( suitable for import )
ownCloud system files
- Just user files + Just user files
diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index d862ac5a82a..2d200c0f769 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -36,7 +36,7 @@ if (isset($_POST['user_export'])) { header("Content-Length: " . filesize($path)); @ob_end_clean(); readfile($path); - OC_Migrate::cleanUp( $path ); + unlink( $path ); } } if( isset( $_POST['user_import'] ) ){ // TODO diff --git a/lib/migrate.php b/lib/migrate.php index 8f26ea7ae68..522d8da843c 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -40,6 +40,10 @@ class OC_Migrate{ static private $zip=false; // String path to export static private $zippath=false; + // Stores the type of export + static private $exporttype=false; + // Array of temp files to be deleted after zip creation + static private $tmpfiles=array(); /** * register a new migration provider @@ -138,14 +142,7 @@ class OC_Migrate{ self::$zippath = $path . $zipname; } else { // Save in tmp dir - $structure = sys_get_temp_dir() . '/owncloudexports/'; - if( !file_exists( $structure ) ){ - if ( !mkdir( $structure ) ) { - OC_Log::write('migration', 'Could not create the temporary export at: '.$structure, OC_Log::ERROR); - return false; - } - } - self::$zippath = $structure . $zipname; + self::$zippath = sys_get_temp_dir() . '/' . $zipname; } // Create the zip object self::$zip = new ZipArchive; @@ -154,42 +151,48 @@ class OC_Migrate{ return false; } // Handle export types - if( $exporttype == 'instance' ){ - // Creates a zip that is compatable with the import function - /* - $dbfile = self:: . "/dbexport.xml"; - OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL'); - - // Now add in *dbname* and *dbtableprefix* - $dbexport = file_get_contents( $dbfile ); - - $dbnamestring = "\n\n " . OC_Config::getValue( "dbname", "owncloud" ); - $dbtableprefixstring = "

\n\n " . OC_Config::getValue( "dbtableprefix", "_oc" ); - - $dbexport = str_replace( $dbnamestring, "\n\n *dbname*", $dbexport ); - $dbexport = str_replace( $dbtableprefixstring, "
\n\n *dbprefix*", $dbexport ); - - // Write the new db export file - file_put_contents( $dbfile, $dbexport ); - - $zip->addFile($dbfile, "dbexport.xml"); - */ - } else if( $exporttype == 'system' ){ - // Creates a zip with the owncloud system files - self::addDirToZip( OC::$SERVERROOT . '/', false); - foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dir) { - self::addDirToZip( OC::$SERVERROOT . '/' . $dir, true, "/"); - } - } else if ( $exporttype == 'userfiles' ){ - // Creates a zip with all of the users files - foreach(OC_User::getUsers() as $user){ - self::addDirToZip( $datadir . '/' . $user . '/', true, "/" . $user); - } - } else { - // Invalid export type supplied - OC_Log::write('migration', 'Invalid export type supplied to createSysExportFile() "'.$exporttype.'"', OC_Log::ERROR); + $exporttypes = array( 'userfiles', 'instance', 'system' ); + self::$exporttype = in_array( $exporttype, $exporttypes ) ? $exporttype : false; + if( !self::$exporttype ){ + OC_Log::write( 'migration', 'Export type: '.$exporttype.' is not supported.', OC_Log::ERROR); return false; } + switch( self::$exporttype ){ + case 'instance': + // Creates a zip that is compatable with the import function + $dbfile = tempnam( "/tmp", "owncloud_export_data_" ); + OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL'); + + // Now add in *dbname* and *dbprefix* + $dbexport = file_get_contents( $dbfile ); + $dbnamestring = "\n\n " . OC_Config::getValue( "dbname", "owncloud" ); + $dbtableprefixstring = "
\n\n " . OC_Config::getValue( "dbtableprefix", "oc_" ); + $dbexport = str_replace( $dbnamestring, "\n\n *dbname*", $dbexport ); + $dbexport = str_replace( $dbtableprefixstring, "
\n\n *dbprefix*", $dbexport ); + // Write the new db export file + file_put_contents( $dbfile, $dbexport ); + self::$zip->addFile( $dbfile, "dbexport.xml" ); + // Add user data + foreach(OC_User::getUsers() as $user){ + self::addDirToZip( $datadir . '/' . $user . '/', true, "/userdata/" ); + } + break; + case 'userfiles': + // Creates a zip with all of the users files + foreach(OC_User::getUsers() as $user){ + self::addDirToZip( $datadir . '/' . $user . '/', true, "/" ); + } + break; + case 'system': + // Creates a zip with the owncloud system files + self::addDirToZip( OC::$SERVERROOT . '/', false, '/'); + foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dir) { + self::addDirToZip( OC::$SERVERROOT . '/' . $dir, true, "/"); + } + break; + } + // Add export info + self::addExportInfo(); // Close the zip if( !self::closeZip() ){ return false; @@ -198,6 +201,30 @@ class OC_Migrate{ } + /** + * @breif adds a json file with infomation on the export to the zips root (used on import) + * @return bool + */ + static private function addExportInfo(){ + $info = array( + 'ocversion' => OC_Util::getVersion(), + 'exporttime' => time(), + 'exportedby' => OC_User::getUser(), + 'exporttype' => self::$exporttype + ); + // Create json + $json = json_encode( $info ); + $tmpfile = tempnam("/tmp", "oc_export_info_"); + self::$tmpfiles[] = $tmpfile; + if( !file_put_contents( $tmpfile, $json ) ){ + return false; + } else { + self::$zip->addFile( $tmpfile, "export_info.json" ); + return true; + } + } + + /** * @breif tried to finalise the zip * @return bool @@ -205,13 +232,25 @@ class OC_Migrate{ static private function closeZip(){ if( !self::$zip->close() ){ OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR); + self::cleanup(); return false; } else { - OC_Log::write('migration', 'Created export file for: '.self::$uid, OC_Log::INFO); + OC_Log::write('migration', 'Export zip created ok', OC_Log::INFO); + self::cleanup(); return true; } } + /** + * @breif cleans up after the zip + */ + static private function cleanup(){ + // Delete tmp files + foreach(self::$tmpfiles as $i){ + unlink( $i ); + } + } + /** * @breif creates a zip user export * @param optional $uid string user id of the user to export (defaults to current) @@ -647,22 +686,5 @@ class OC_Migrate{ return $result ? true : false; } - - /** - * @breif removes migration.db and exportinfo.json from the users data dir - * @param optional $path string path to the export zip to delete - * @return void - */ - static public function cleanUp( $path=null ){ - $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . self::$uid; - // Remove migration.db - unlink( $userdatadir . '/migration.db' ); - // Remove exportinfo.json - unlink( $userdatadir . '/exportinfo.json' ); - // Remove the zip - if( !is_null( $path ) ){ - unlink( $path ); - } - return true; - } + } -- cgit v1.2.3 From 5332c319a2140563478d83047d9f717c0d3e179f Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Fri, 16 Mar 2012 22:50:35 +0000 Subject: Migration info is an object. Other fixes --- apps/bookmarks/lib/migrate.php | 6 +++--- lib/migrate.php | 12 ++++++------ 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index 4f11bc5bdea..46b68ad5baa 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -34,16 +34,16 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ // Import function for bookmarks function import( $info ){ - switch( $info['appversion'] ){ + switch( $info->appversion ){ default: // All versions of the app have had the same db structure, so all can use the same import function $query = OC_Migrate::prepare( "SELECT * FROM bookmarks WHERE user_id LIKE ?" ); - $results = $query->execute( array( $info['olduid'] ) ); + $results = $query->execute( array( $info->olduid ) ); $idmap = array(); while( $row = $data->fetchRow() ){ // Import each bookmark, saving its id into the map $query = OC_DB::prepare( "INSERT INTO *PREFIX*bookmarks(url, title, user_id, public, added, lastmodified) VALUES (?, ?, ?, ?, ?, ?)" ); - $query->execute( array( $row['url'], $row['title'], $info['newuid'], $row['public'], $row['added'], $row['lastmodified'] ) ); + $query->execute( array( $row['url'], $row['title'], $info->newuid, $row['public'], $row['added'], $row['lastmodified'] ) ); // Map the id $idmap[$row['id']] = OC_DB::insertid(); } diff --git a/lib/migrate.php b/lib/migrate.php index 522d8da843c..28c36e96167 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -377,11 +377,11 @@ class OC_Migrate{ /** * @breif imports a new user * @param $db string path to migration.db - * @param $migrateinfo array of migration ino + * @param $info array of migration ino * @param $uid optional uid to use * @return bool if the import succedded */ - public static function importAppData( $db, $migrateinfo, $uid=false ){ + public static function importAppData( $db, $info, $uid=false ){ if(!self::$uid){ OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL); @@ -399,15 +399,15 @@ class OC_Migrate{ return false; } - if( !is_array( $migrateinfo ) ){ + if( !is_array( $info ) ){ OC_Log::write('migration','$migrateinfo is not an array', OC_Log::FATAL); return false; } // Set the user id - self::$uid = $info['migrateinfo']['uid']; + self::$uid = $info->migrateinfo->uid; - $apps = $info['apps']; + $apps = $info->app; foreach( self::$providers as $provider){ // Is the app in the export? @@ -415,7 +415,7 @@ class OC_Migrate{ // Did it succeed? if( $app[$provider->id] ){ // Then do the import - $provider->import(); + $provider->import( $info ); } } } -- cgit v1.2.3 From 5234e66bab0ebc6fd7eeef8170cf9f61f035124d Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 17 Mar 2012 13:53:00 +0000 Subject: Add exportinfo to user exports. --- apps/user_migrate/admin.php | 10 ++++------ lib/migrate.php | 37 +++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 20 deletions(-) (limited to 'lib') diff --git a/apps/user_migrate/admin.php b/apps/user_migrate/admin.php index 56fe8875145..da2e53d2a15 100644 --- a/apps/user_migrate/admin.php +++ b/apps/user_migrate/admin.php @@ -1,10 +1,8 @@ open(get_temp_dir().'/'.$importname.'.zip') != TRUE) { - OC_Log::write('admin_export',"Failed to open zip file",OC_Log::INFO); + OC_Log::write('migration',"Failed to open zip file",OC_Log::INFO); exit(); } $zip->extractTo(get_temp_dir().'/'.$importname.'/'); diff --git a/lib/migrate.php b/lib/migrate.php index 28c36e96167..f5fb808f173 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -84,7 +84,7 @@ class OC_Migrate{ if( is_array( $tables ) ){ // Save the table names foreach($tables as $table){ - $return['app'][$provider->id]['tables'][] = $table; + $return['apps'][$provider->id]['tables'][] = $table; } } else { // It failed to create the tables @@ -94,22 +94,17 @@ class OC_Migrate{ // Run the import function? if( !$failed ){ - $return['app'][$provider->id]['success'] = $provider->export( self::$uid ); + $return['apps'][$provider->id]['success'] = $provider->export( self::$uid ); } else { - $return['app'][$provider->id]['success'] = false; - $return['app'][$provider->id]['message'] = 'failed to create the app tables'; + $return['apps'][$provider->id]['success'] = false; + $return['apps'][$provider->id]['message'] = 'failed to create the app tables'; } // Now add some app info the the return array $appinfo = OC_App::getAppInfo( $provider->id ); - $return['app'][$provider->id]['version'] = $appinfo['version']; + $return['apps'][$provider->id]['version'] = $appinfo['version']; } - - - // Add some general info to the return array - $return['migrateinfo']['uid'] = self::$uid; - $return['migrateinfo']['ocversion'] = OC_Util::getVersionString(); return $return; @@ -205,13 +200,27 @@ class OC_Migrate{ * @breif adds a json file with infomation on the export to the zips root (used on import) * @return bool */ - static private function addExportInfo(){ + static private function addExportInfo( $array=array() ){ $info = array( 'ocversion' => OC_Util::getVersion(), 'exporttime' => time(), 'exportedby' => OC_User::getUser(), 'exporttype' => self::$exporttype ); + // Add hash if user export + if( self::$exporttype = 'user' ){ + $query = OC_DB::prepare( "SELECT password FROM *PREFIX*users WHERE uid LIKE ?" ); + $result = $query->execute( array( self::$uid ) ); + $row = $result->fetchRow(); + $hash = $row ? $row['password'] : false; + if( !$hash ){ + OC_Log::write( 'migration', 'Failed to get the users password hash', OC_log::ERROR); + return false; + } + $info['hash'] = $hash; + } + // Merge in other data + $info = array_merge( $info, $array ); // Create json $json = json_encode( $info ); $tmpfile = tempnam("/tmp", "oc_export_info_"); @@ -297,8 +306,8 @@ class OC_Migrate{ return false; } // Export the app info - $info = json_encode( self::exportAppData() ); - file_put_contents( $userdatadir . '/exportinfo.json', $info ); + $exportinfo = json_encode( self::addExportInfo( self::exportAppData() ) ); + file_put_contents( $userdatadir . '/exportinfo.json', $exportinfo ); // Add the data dir to the zip self::addDirToZip( $userdatadir ); // Close the zip @@ -670,7 +679,7 @@ class OC_Migrate{ // @param $uid string user_id of the user to be created // @param $hash string hash of the user to be created // @return bool result of user creation - private static function createUser( $uid, $hash ){ + public static function createUser( $uid, $hash ){ // Check if userid exists if(OC_User::userExists( $uid )){ -- cgit v1.2.3 From 247b25e7a97fcbe8386c63b1318537e669d40480 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 17 Mar 2012 15:01:08 +0000 Subject: Fix structure of export zip --- lib/migrate.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index f5fb808f173..44d28297d48 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -228,7 +228,7 @@ class OC_Migrate{ if( !file_put_contents( $tmpfile, $json ) ){ return false; } else { - self::$zip->addFile( $tmpfile, "export_info.json" ); + self::$zip->addFile( $tmpfile, "/" . self::$uid . "/export_info.json" ); return true; } } @@ -278,6 +278,8 @@ class OC_Migrate{ self::$uid = $uid; // Create the zip object self::$zip = new ZipArchive; + // Set export type + self::$exporttype = 'user'; // Calculate users data dir $user = OC_User::getUser(); $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . $user . '/'; @@ -306,8 +308,9 @@ class OC_Migrate{ return false; } // Export the app info - $exportinfo = json_encode( self::addExportInfo( self::exportAppData() ) ); - file_put_contents( $userdatadir . '/exportinfo.json', $exportinfo ); + $appinfo = self::exportAppData(); + // Save the migration results + self::addExportInfo( $appinfo ); // Add the data dir to the zip self::addDirToZip( $userdatadir ); // Close the zip -- cgit v1.2.3 From bc085c3ff40bd7980bb28a20238bf18a754ffba2 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 17 Mar 2012 16:25:14 +0000 Subject: Create new user, create new data dir, copy files, import app data --- apps/admin_export/settings.php | 1 - apps/bookmarks/lib/migrate.php | 9 +++--- apps/user_migrate/admin.php | 27 +++++++++++------- lib/migrate.php | 65 +++++++++++++++++++++++------------------- lib/migrate/provider.php | 3 +- 5 files changed, 59 insertions(+), 46 deletions(-) (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index e7de74f7588..1c98bb552f1 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -144,7 +144,6 @@ function unlinkRecursive($dir, $deleteRootToo) return; } - function copy_r( $path, $dest ) { diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index 46b68ad5baa..ffc5e9f8387 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -32,18 +32,17 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ } // Import function for bookmarks - function import( $info ){ - - switch( $info->appversion ){ + function import( $app, $info ){ + switch( $app->version ){ default: // All versions of the app have had the same db structure, so all can use the same import function $query = OC_Migrate::prepare( "SELECT * FROM bookmarks WHERE user_id LIKE ?" ); - $results = $query->execute( array( $info->olduid ) ); + $results = $query->execute( array( $info['olduid'] ) ); $idmap = array(); while( $row = $data->fetchRow() ){ // Import each bookmark, saving its id into the map $query = OC_DB::prepare( "INSERT INTO *PREFIX*bookmarks(url, title, user_id, public, added, lastmodified) VALUES (?, ?, ?, ?, ?, ?)" ); - $query->execute( array( $row['url'], $row['title'], $info->newuid, $row['public'], $row['added'], $row['lastmodified'] ) ); + $query->execute( array( $row['url'], $row['title'], $info['newuid'], $row['public'], $row['added'], $row['lastmodified'] ) ); // Map the id $idmap[$row['id']] = OC_DB::insertid(); } diff --git a/apps/user_migrate/admin.php b/apps/user_migrate/admin.php index da2e53d2a15..6f3565788eb 100644 --- a/apps/user_migrate/admin.php +++ b/apps/user_migrate/admin.php @@ -62,14 +62,15 @@ if (isset($_POST['user_import'])) { // Get the user if( count($files) != 1 ){ OC_Log::write('migration', 'Invalid import file', OC_Log::ERROR); - die('invalid import'); + die('invalid import, no user included'); } - $user = reset($files); + $olduser = reset($files); // Check for dbexport.xml and export info and data dir - $files = scandir( $importdir . '/' . $user ); - $required = array( 'migration.db', 'exportinfo.json', 'files'); + $files = scandir( $importdir . '/' . $olduser ); + + $required = array( 'migration.db', 'export_info.json', 'files'); foreach($required as $require){ if( !in_array( $require, $files) ){ OC_Log::write('migration', 'Invlaid import file', OC_Log::ERROR); @@ -77,31 +78,37 @@ if (isset($_POST['user_import'])) { } } - $migrateinfo = $importdir . '/' . $user . '/exportinfo.json'; + $migrateinfo = $importdir . '/' . $olduser . '/export_info.json'; $migrateinfo = json_decode( file_get_contents( $migrateinfo ) ); - $olduid = $migrateinfo->migrateinfo->uid; // Check if uid is available - if( OC_User::UserExists( $olduid ) ){ + if( OC_User::UserExists( $olduser ) ){ OC_Log::write('migration','Username exists', OC_Log::ERROR); die('user exists'); } // Create the user - if( !OC_Migrate::createUser( $olduid, $migrateinfo->migrateinfo->hash ) ){ + if( !OC_Migrate::createUser( $olduser, $migrateinfo->hash ) ){ OC_Log::write('migration', 'Failed to create the new user', OC_Log::ERROR); die('coundlt create new user'); } $datadir = OC_Config::getValue( 'datadirectory' ); + // Make the new users data dir + $path = $datadir . '/' . $olduser . '/files/'; + if( !mkdir( $path, 0755, true ) ){ + OC_Log::write('migration','Failed to create users data dir: '.$path, OC_Log::ERROR); + die('failed to create users data dir'); + } + // Copy data - if( !copy_r( $importdir . '/files', $datadir . '/' ) ){ + if( !copy_r( $importdir . '/' . $olduser . '/files', $datadir . '/' . $olduser . '/files' ) ){ OC_Log::write('migration','Failed to copy user files to destination', OC_Log::ERROR); die('failed to copy user files'); } // Import user data - if( !OC_Migrate::importUser( $importdir . '/migration.db', $migrateinfo ) ){ + if( !OC_Migrate::importAppData( $importdir . '/' . $olduser . '/migration.db', $migrateinfo ) ){ OC_Log::write('migration','Failed to import user data', OC_Log::ERROR); die('failed to import user data'); } diff --git a/lib/migrate.php b/lib/migrate.php index 44d28297d48..415c33e5bea 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -53,6 +53,21 @@ class OC_Migrate{ self::$providers[]=$provider; } + /** + * @breif finds and loads the providers + */ + static private function findProviders(){ + // Find the providers + $apps = OC_App::getAllApps(); + + foreach($apps as $app){ + $path = OC::$SERVERROOT . '/apps/' . $app . '/lib/migrate.php'; + if( file_exists( $path ) ){ + include( $path ); + } + } + } + /** * @breif creates a migration.db in the users data dir with their app data in * @return bool whether operation was successfull @@ -64,14 +79,7 @@ class OC_Migrate{ $return = array(); // Find the providers - $apps = OC_App::getAllApps(); - - foreach($apps as $app){ - $path = OC::$SERVERROOT . '/apps/' . $app . '/lib/migrate.php'; - if( file_exists( $path ) ){ - include( $path ); - } - } + self::findProviders(); // Foreach provider foreach( self::$providers as $provider ){ @@ -217,7 +225,8 @@ class OC_Migrate{ OC_Log::write( 'migration', 'Failed to get the users password hash', OC_log::ERROR); return false; } - $info['hash'] = $hash; + $info['hash'] = $hash; + $info['exporteduser'] = self::$uid; } // Merge in other data $info = array_merge( $info, $array ); @@ -393,17 +402,15 @@ class OC_Migrate{ * @param $uid optional uid to use * @return bool if the import succedded */ - public static function importAppData( $db, $info, $uid=false ){ + public static function importAppData( $db, $info, $uid=null ){ - if(!self::$uid){ - OC_Log::write('migration','Tried to import without passing a uid',OC_Log::FATAL); - return false; - } + self::$uid = !is_null( $uid ) ? $uid : $info->exporteduser; // Check if the db exists if( file_exists( $db ) ){ // Connect to the db if(!self::connectDB( $db )){ + OC_Log::write('migration','Failed to connect to migration.db',OC_Log::ERROR); return false; } } else { @@ -411,25 +418,25 @@ class OC_Migrate{ return false; } - if( !is_array( $info ) ){ - OC_Log::write('migration','$migrateinfo is not an array', OC_Log::FATAL); - return false; - } - - // Set the user id - self::$uid = $info->migrateinfo->uid; - - $apps = $info->app; - + // Find providers + self::findProviders(); + + // Generate importinfo array + $importinfo = array( + 'olduid' => $info->exporteduser, + 'newuid' => self::$uid + ); + foreach( self::$providers as $provider){ // Is the app in the export? - if( array_key_exists( $provider->id, $apps ) ){ + $id = $provider->id; + if( isset( $info->apps->$id ) ){ // Did it succeed? - if( $app[$provider->id] ){ + if( $info->apps->$id->success ){ // Then do the import - $provider->import( $info ); + $provider->import( $info->apps->$id, $importinfo ); } - } + } } return true; @@ -691,7 +698,7 @@ class OC_Migrate{ // Create the user $query = OC_DB::prepare( "INSERT INTO `*PREFIX*users` ( `uid`, `password` ) VALUES( ?, ? )" ); - $result = $query->execute( array( $uid, $data['hash'])); + $result = $query->execute( array( $uid, $hash)); if( !$result ){ OC_Log::write('migration', 'Failed to create the new user "'.$uid.""); } diff --git a/lib/migrate/provider.php b/lib/migrate/provider.php index e2e01b3b5aa..7ac3cf97cad 100644 --- a/lib/migrate/provider.php +++ b/lib/migrate/provider.php @@ -20,8 +20,9 @@ abstract class OC_Migrate_Provider{ /** * @breif imports data for the app + * @param $appinfo object with the data that the app exported * @param $info array of info including exportinfo.json * @return void */ - abstract function import( $info ); + abstract function import( $appinfo, $info ); } -- cgit v1.2.3 From 77f6872ea4859e13637efbc6d051072a5085394f Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 17 Mar 2012 17:45:39 +0000 Subject: Shorten export zip names --- lib/migrate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index 415c33e5bea..84eafcd4cdc 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -126,7 +126,7 @@ class OC_Migrate{ */ static public function createSysExportFile( $exporttype='instance', $path=null ){ // Calculate zip name - $zipname = "owncloud_export_" . date("y-m-d_H-i-s") . ".zip"; + $zipname = "oc_export_" . date("y-m-d_H-i-s") . ".zip"; // Get the data dir $datadir = OC_Config::getValue( 'datadirectory' ); // Calculate destination @@ -293,7 +293,7 @@ class OC_Migrate{ $user = OC_User::getUser(); $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . $user . '/'; // Calculate zip name - $zipname = "owncloud_userexport_" . $user . '_' . date("y-m-d_H-i-s") . ".zip"; + $zipname = "oc_userexport_" . $user . '_' . date("y-m-d_H-i-s") . ".zip"; // Calculate destination if( !is_null( $path ) ){ // Path given -- cgit v1.2.3 From 145d6f35660669397eaee08988ffbad1b65daff0 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Mon, 19 Mar 2012 20:44:20 +0000 Subject: Add OC_Migration_Content class to help app devs. Restructure OC_Migrate. --- apps/admin_export/settings.php | 134 +------ apps/bookmarks/lib/migrate.php | 20 +- apps/user_migrate/settings.php | 2 +- lib/migrate.php | 773 +++++++++++++++-------------------------- lib/migrate/provider.php | 28 -- lib/migration/content.php | 239 +++++++++++++ lib/migration/provider.php | 49 +++ 7 files changed, 589 insertions(+), 656 deletions(-) delete mode 100644 lib/migrate/provider.php create mode 100644 lib/migration/content.php create mode 100644 lib/migration/provider.php (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index 1c98bb552f1..af8dd0dbf54 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -27,11 +27,10 @@ OC_Util::checkAppEnabled('admin_export'); define('DS', '/'); - // Export? if (isset($_POST['admin_export'])) { // Create the export zip - if( !$path = OC_Migrate::createSysExportFile( $_POST['export_type'] ) ){ + if( !$path = OC_Migrate::export( $_POST['export_type'] ) ){ // Error die('error'); } else { @@ -46,136 +45,11 @@ if (isset($_POST['admin_export'])) { // Import? } else if( isset($_POST['admin_import']) ){ - $root = OC::$SERVERROOT . "/"; - $importname = "owncloud_import_" . date("y-m-d_H-i-s"); - - // Save data dir for later - $datadir = OC_Config::getValue( 'datadirectory' ); - - // Copy the uploaded file - $from = $_FILES['owncloud_import']['tmp_name']; - $to = get_temp_dir().'/'.$importname.'.zip'; - if( !move_uploaded_file( $from, $to ) ){ - OC_Log::write('admin_export',"Failed to copy the uploaded file",OC_Log::INFO); - exit(); - } - - // Extract zip - $zip = new ZipArchive(); - if ($zip->open(get_temp_dir().'/'.$importname.'.zip') != TRUE) { - OC_Log::write('admin_export',"Failed to open zip file",OC_Log::INFO); - exit(); - } - $zip->extractTo(get_temp_dir().'/'.$importname.'/'); - $zip->close(); - - // Delete uploaded file - unlink( get_temp_dir() . '/' . $importname . '.zip' ); - - // Now we need to check if everything is present. Data and dbexport.xml - - - // Delete current data folder. - OC_Log::write('admin_export',"Deleting current data dir",OC_Log::INFO); - unlinkRecursive( $datadir, false ); - - // Copy over data - if( !copy_r( get_temp_dir() . '/' . $importname . '/data', $datadir ) ){ - OC_Log::write('admin_export',"Failed to copy over data directory",OC_Log::INFO); - exit(); - } - - OC_DB::replaceDB( get_temp_dir() . '/' . $importname . '/dbexport.xml' ); + // TODO + // OC_Migrate::import( $pathtozipfile ); } else { // fill template $tmpl = new OC_Template('admin_export', 'settings'); return $tmpl->fetchPage(); -} - -function zipAddDir($dir, $zip, $recursive=true, $internalDir='') { - $dirname = basename($dir); - $zip->addEmptyDir($internalDir . $dirname); - $internalDir.=$dirname.='/'; - - if ($dirhandle = opendir($dir)) { - while (false !== ( $file = readdir($dirhandle))) { - - if (( $file != '.' ) && ( $file != '..' )) { - - if (is_dir($dir . '/' . $file) && $recursive) { - zipAddDir($dir . '/' . $file, $zip, $recursive, $internalDir); - } elseif (is_file($dir . '/' . $file)) { - $zip->addFile($dir . '/' . $file, $internalDir . $file); - } - } - } - closedir($dirhandle); - } else { - OC_Log::write('admin_export',"Was not able to open directory: " . $dir,OC_Log::ERROR); - } -} - -function unlinkRecursive($dir, $deleteRootToo) -{ - if(!$dh = @opendir($dir)) - { - return; - } - while (false !== ($obj = readdir($dh))) - { - if($obj == '.' || $obj == '..') - { - continue; - } - - if (!@unlink($dir . '/' . $obj)) - { - unlinkRecursive($dir.'/'.$obj, true); - } - } - - closedir($dh); - - if ($deleteRootToo) - { - @rmdir($dir); - } - - return; -} - - function copy_r( $path, $dest ) - { - if( is_dir($path) ) - { - @mkdir( $dest ); - $objects = scandir($path); - if( sizeof($objects) > 0 ) - { - foreach( $objects as $file ) - { - if( $file == "." || $file == ".." ) - continue; - // go on - if( is_dir( $path.DS.$file ) ) - { - copy_r( $path.DS.$file, $dest.DS.$file ); - } - else - { - copy( $path.DS.$file, $dest.DS.$file ); - } - } - } - return true; - } - elseif( is_file($path) ) - { - return copy($path, $dest); - } - else - { - return false; - } - } +} \ No newline at end of file diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index ffc5e9f8387..36a08c0cf40 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -1,16 +1,16 @@ 'bookmarks', 'matchcol'=>'user_id', - 'matchval'=>$uid, + 'matchval'=>$this->uid, 'idcol'=>'id' ); - $ids = OC_Migrate::copyRows( $options ); + $ids = $this->content->copyRows( $options ); $options = array( 'table'=>'bookmarks_tags', @@ -19,7 +19,7 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ ); // Export tags - $ids2 = OC_Migrate::copyRows( $options ); + $ids2 = $this->content->copyRows( $options ); // If both returned some ids then they worked if( is_array( $ids ) && is_array( $ids2 ) ) @@ -32,17 +32,17 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ } // Import function for bookmarks - function import( $app, $info ){ - switch( $app->version ){ + function import( ){ + switch( $this->appinfo->version ){ default: // All versions of the app have had the same db structure, so all can use the same import function $query = OC_Migrate::prepare( "SELECT * FROM bookmarks WHERE user_id LIKE ?" ); - $results = $query->execute( array( $info['olduid'] ) ); + $results = $query->execute( array( $this->info['olduid'] ) ); $idmap = array(); while( $row = $data->fetchRow() ){ // Import each bookmark, saving its id into the map $query = OC_DB::prepare( "INSERT INTO *PREFIX*bookmarks(url, title, user_id, public, added, lastmodified) VALUES (?, ?, ?, ?, ?, ?)" ); - $query->execute( array( $row['url'], $row['title'], $info['newuid'], $row['public'], $row['added'], $row['lastmodified'] ) ); + $query->execute( array( $row['url'], $row['title'], $this->info['newuid'], $row['public'], $row['added'], $row['lastmodified'] ) ); // Map the id $idmap[$row['id']] = OC_DB::insertid(); } @@ -66,4 +66,4 @@ class OC_Migrate_Provider_Bookmarks extends OC_Migrate_Provider{ } // Load the provider -new OC_Migrate_Provider_Bookmarks( 'bookmarks' ); \ No newline at end of file +new OC_Migration_Provider_Bookmarks( 'bookmarks' ); \ No newline at end of file diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 62f5e3f20d7..38eee990b46 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -26,7 +26,7 @@ OC_Util::checkAppEnabled('user_migrate'); if (isset($_POST['user_export'])) { // Create the export zip - if( !$path = OC_Migrate::createUserExportFile() ){ + if( !$path = OC_Migrate::export() ){ // Error die('error'); } else { diff --git a/lib/migrate.php b/lib/migrate.php index 84eafcd4cdc..338d091af8b 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -22,28 +22,31 @@ /** - * provides an interface to all search providers + * provides an interface to migrate users and whole ownclouds */ class OC_Migrate{ - // Holds the db object - static private $MDB2=false; + // Array of OC_Migration_Provider objects static private $providers=array(); - // Schema db object - static private $schema=false; // User id of the user to import/export static private $uid=false; - // Path to the sqlite db - static private $dbpath=false; // Holds the ZipArchive object static private $zip=false; - // String path to export - static private $zippath=false; // Stores the type of export static private $exporttype=false; // Array of temp files to be deleted after zip creation static private $tmpfiles=array(); + // Holds the db object + static private $MDB2=false; + // Schema db object + static private $schema=false; + // Path to the sqlite db + static private $dbpath=false; + // Holds the path to the zip file + static private $zippath=false; + // Holds the OC_Migration_Content object + static private $content=false; /** * register a new migration provider @@ -68,49 +71,181 @@ class OC_Migrate{ } } + /** + * @breif exports a user, or owncloud instance + * @param ootional $type string type of export, defualts to user + * @param otional $path string path to zip output folder + * @param optional $uid string user id of user to export if export type is user, defaults to current + */ + public static function export( $type='user', $path=null, $uid=null ){ + $datadir = OC_Config::getValue( 'datadirectory' ); + // Validate export type + $types = array( 'user', 'instance', 'system', 'userfiles' ); + if( !in_array( $type, $types ) ){ + OC_Log::write( 'migration', 'Invalid export type', OC_Log::ERROR ); + return false; + } + self::$exporttype = $type; + // Userid? + if( self::$exporttype == 'user' ){ + // Check user exists + if( !is_null($uid) ){ + if( !OC_User_Database::userExists( $uid ) ){ + OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR); + return false; + } + self::$uid = $uid; + } else { + self::$uid = OC_User::getUser(); + } + } + // Calculate zipname + if( self::$exporttype == 'user' ){ + $zipname = 'oc_export_' . self::$uid . '_' . date("y-m-d_H-i-s") . '.zip'; + } else { + $zipname = 'oc_export_' . self::$exporttype . '_' . date("y-m-d_H-i-s") . '.zip'; + } + // Calculate path + if( self::$exporttype == 'user' ){ + self::$zippath = $datadir . '/' . self::$uid . '/' . $zipname; + } else { + if( !is_null( $path ) ){ + // Validate custom path + if( !file_exists( $path ) || !is_writeable( $path ) ){ + OC_Log::write( 'migration', 'Path supplied is invalid.', OC_Log::ERROR ); + return false; + } + self::$zippath = $path . $zipname; + } else { + // Default path + self::$zippath = get_temp_dir() . '/' . $zipname; + } + } + // Create the zip object + self::$zip = new ZipArchive; + if( !self::createZip() ){ + return false; + } + // Do the export + self::findProviders(); + $exportdata = array(); + switch( self::$exporttype ){ + case 'user': + // Connect to the db + self::$dbpath = $datadir . '/' . self::$uid . '/migration.db'; + if( !self::connectDB() ){ + return false; + } + self::$content = new OC_Migration_Content( self::$zip, self::$MDB2 ); + // Export the app info + $exportdata = self::exportAppData(); + // Add the data dir to the zip + self::$content->addDir( $datadir . '/' . self::$uid, true, '/' ); + break; + case 'instance': + self::$content = new OC_Migration_Content( self::$zip ); + // Creates a zip that is compatable with the import function + $dbfile = tempnam( "/tmp", "owncloud_export_data_" ); + OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL'); + + // Now add in *dbname* and *dbprefix* + $dbexport = file_get_contents( $dbfile ); + $dbnamestring = "\n\n " . OC_Config::getValue( "dbname", "owncloud" ); + $dbtableprefixstring = "
\n\n " . OC_Config::getValue( "dbtableprefix", "oc_" ); + $dbexport = str_replace( $dbnamestring, "\n\n *dbname*", $dbexport ); + $dbexport = str_replace( $dbtableprefixstring, "
\n\n *dbprefix*", $dbexport ); + // Add the export to the zip + self::$content->addFromString( $dbexport, "dbexport.xml" ); + // Add user data + foreach(OC_User::getUsers() as $user){ + self::$content->addDir( $datadir . '/' . $user . '/', true, "/userdata/" ); + } + break; + case 'userfiles': + self::$content = new OC_Migration_Content( self::$zip ); + // Creates a zip with all of the users files + foreach(OC_User::getUsers() as $user){ + self::$content->addDir( $datadir . '/' . $user . '/', true, "/" ); + } + break; + case 'system': + self::$content = new OC_Migration_Content( self::$zip ); + // Creates a zip with the owncloud system files + self::$content->addDir( OC::$SERVERROOT . '/', false, '/'); + foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dir) { + self::$content->addDir( OC::$SERVERROOT . '/' . $dir, true, "/"); + } + break; + } + if( !$info = self::getExportInfo( $exportdata ) ){ + return false; + } + // Add the export info json to the export zip + self::$content->addFromString( $info, 'export_info.json' ); + if( !self::$content->finish() ){ + return false; + } + return self::$zippath; + } + + /** + * @brief connects to a MDB2 database scheme + * @returns bool + */ + static private function connectScheme(){ + // We need a mdb2 database connection + self::$MDB2->loadModule( 'Manager' ); + self::$MDB2->loadModule( 'Reverse' ); + + // Connect if this did not happen before + if( !self::$schema ){ + require_once('MDB2/Schema.php'); + self::$schema=MDB2_Schema::factory( self::$MDB2 ); + } + + return true; + } + /** * @breif creates a migration.db in the users data dir with their app data in * @return bool whether operation was successfull */ private static function exportAppData( ){ - self::connectDB(); - $ok = true; + $success = true; $return = array(); - - // Find the providers - self::findProviders(); - + // Foreach provider foreach( self::$providers as $provider ){ - $failed = false; - + $success = true; // Does this app use the database? - if(file_exists(OC::$SERVERROOT.'/apps/'.$provider->id.'/appinfo/database.xml')){ + if( file_exists( OC::$SERVERROOT.'/apps/'.$provider->getID().'/appinfo/database.xml' ) ){ // Create some app tables - $tables = self::createAppTables( $provider->id ); + $tables = self::createAppTables( $provider->getID() ); if( is_array( $tables ) ){ // Save the table names foreach($tables as $table){ - $return['apps'][$provider->id]['tables'][] = $table; + $return['apps'][$provider->getID()]['tables'][] = $table; } } else { // It failed to create the tables - $failed = true; + $success = false; } } - // Run the import function? - if( !$failed ){ - $return['apps'][$provider->id]['success'] = $provider->export( self::$uid ); + // Run the export function? + if( $success ){ + // Set the provider properties + $provider->setData( self::$uid, self::$content ); + $return['apps'][$provider->getID()]['success'] = $provider->export(); } else { - $return['apps'][$provider->id]['success'] = false; - $return['apps'][$provider->id]['message'] = 'failed to create the app tables'; + $return['apps'][$provider->getID()]['success'] = false; + $return['apps'][$provider->getID()]['message'] = 'failed to create the app tables'; } // Now add some app info the the return array - $appinfo = OC_App::getAppInfo( $provider->id ); - $return['apps'][$provider->id]['version'] = $appinfo['version']; + $appinfo = OC_App::getAppInfo( $provider->getID() ); + $return['apps'][$provider->getID()]['version'] = $appinfo['version']; } @@ -118,97 +253,13 @@ class OC_Migrate{ } - /** - * @breif creates an export file for the whole system - * @param optional $exporttype string export type ('instance','system' or 'userfiles') - * @param optional $path string path to zip destination (with trailing slash) - * @return path to the zip or false if there was a problem - */ - static public function createSysExportFile( $exporttype='instance', $path=null ){ - // Calculate zip name - $zipname = "oc_export_" . date("y-m-d_H-i-s") . ".zip"; - // Get the data dir - $datadir = OC_Config::getValue( 'datadirectory' ); - // Calculate destination - if( !is_null( $path ) ){ - // Path given - // Is a directory? - if( !is_dir( $path ) ){ - OC_Log::write('migration', 'Path supplied to createSysExportFile() is not a directory', OC_Log::ERROR); - return false; - } - // Is writeable - if( !is_writeable( $path ) ){ - OC_Log::write('migration', 'Path supplied to createSysExportFile() is not writeable', OC_Log::ERROR); - return false; - } - self::$zippath = $path . $zipname; - } else { - // Save in tmp dir - self::$zippath = sys_get_temp_dir() . '/' . $zipname; - } - // Create the zip object - self::$zip = new ZipArchive; - // Try to create the zip - if( !self::createZip() ){ - return false; - } - // Handle export types - $exporttypes = array( 'userfiles', 'instance', 'system' ); - self::$exporttype = in_array( $exporttype, $exporttypes ) ? $exporttype : false; - if( !self::$exporttype ){ - OC_Log::write( 'migration', 'Export type: '.$exporttype.' is not supported.', OC_Log::ERROR); - return false; - } - switch( self::$exporttype ){ - case 'instance': - // Creates a zip that is compatable with the import function - $dbfile = tempnam( "/tmp", "owncloud_export_data_" ); - OC_DB::getDbStructure( $dbfile, 'MDB2_SCHEMA_DUMP_ALL'); - - // Now add in *dbname* and *dbprefix* - $dbexport = file_get_contents( $dbfile ); - $dbnamestring = "\n\n " . OC_Config::getValue( "dbname", "owncloud" ); - $dbtableprefixstring = "
\n\n " . OC_Config::getValue( "dbtableprefix", "oc_" ); - $dbexport = str_replace( $dbnamestring, "\n\n *dbname*", $dbexport ); - $dbexport = str_replace( $dbtableprefixstring, "
\n\n *dbprefix*", $dbexport ); - // Write the new db export file - file_put_contents( $dbfile, $dbexport ); - self::$zip->addFile( $dbfile, "dbexport.xml" ); - // Add user data - foreach(OC_User::getUsers() as $user){ - self::addDirToZip( $datadir . '/' . $user . '/', true, "/userdata/" ); - } - break; - case 'userfiles': - // Creates a zip with all of the users files - foreach(OC_User::getUsers() as $user){ - self::addDirToZip( $datadir . '/' . $user . '/', true, "/" ); - } - break; - case 'system': - // Creates a zip with the owncloud system files - self::addDirToZip( OC::$SERVERROOT . '/', false, '/'); - foreach (array(".git", "3rdparty", "apps", "core", "files", "l10n", "lib", "ocs", "search", "settings", "tests") as $dir) { - self::addDirToZip( OC::$SERVERROOT . '/' . $dir, true, "/"); - } - break; - } - // Add export info - self::addExportInfo(); - // Close the zip - if( !self::closeZip() ){ - return false; - } - return self::$zippath; - - } /** - * @breif adds a json file with infomation on the export to the zips root (used on import) - * @return bool - */ - static private function addExportInfo( $array=array() ){ + * @breif generates json containing export info, and merges any data supplied + * @param optional $array array of data to include in the returned json + * @return bool + */ + static private function getExportInfo( $array=array() ){ $info = array( 'ocversion' => OC_Util::getVersion(), 'exporttime' => time(), @@ -216,11 +267,12 @@ class OC_Migrate{ 'exporttype' => self::$exporttype ); // Add hash if user export - if( self::$exporttype = 'user' ){ + if( self::$exporttype == 'user' ){ $query = OC_DB::prepare( "SELECT password FROM *PREFIX*users WHERE uid LIKE ?" ); $result = $query->execute( array( self::$uid ) ); $row = $result->fetchRow(); $hash = $row ? $row['password'] : false; + die(var_dump($hash)); if( !$hash ){ OC_Log::write( 'migration', 'Failed to get the users password hash', OC_log::ERROR); return false; @@ -228,110 +280,122 @@ class OC_Migrate{ $info['hash'] = $hash; $info['exporteduser'] = self::$uid; } + if( !is_array( $array ) ){ + OC_Log::write( 'migration', 'Supplied $array was not an array in getExportInfo()', OC_Log::ERROR ); + } // Merge in other data - $info = array_merge( $info, $array ); + $info = array_merge( $info, (array)$array ); // Create json $json = json_encode( $info ); - $tmpfile = tempnam("/tmp", "oc_export_info_"); - self::$tmpfiles[] = $tmpfile; - if( !file_put_contents( $tmpfile, $json ) ){ - return false; - } else { - self::$zip->addFile( $tmpfile, "/" . self::$uid . "/export_info.json" ); - return true; - } - } - - - /** - * @breif tried to finalise the zip - * @return bool - */ - static private function closeZip(){ - if( !self::$zip->close() ){ - OC_Log::write('migration', 'Failed to save the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR); - self::cleanup(); - return false; - } else { - OC_Log::write('migration', 'Export zip created ok', OC_Log::INFO); - self::cleanup(); - return true; - } - } - - /** - * @breif cleans up after the zip - */ - static private function cleanup(){ - // Delete tmp files - foreach(self::$tmpfiles as $i){ - unlink( $i ); - } + return true; } /** - * @breif creates a zip user export - * @param optional $uid string user id of the user to export (defaults to current) - * @param optional $path string path to folder to create file in (with trailing slash) (defaults to current user's data dir) - * @return false on failure | string path on success - */ - static public function createUserExportFile( $uid=null, $path=null ){ - // User passed? - $uid = is_null( $uid ) ? OC_User::getUser() : $uid ; - // Is a database user? - if( !OC_User_Database::userExists( $uid ) ){ - OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR); + * @breif connects to migration.db, or creates if not found + * @param $db optional path to migration.db, defaults to user data dir + * @return bool whether the operation was successful + */ + static private function connectDB( $path=null ){ + // Has the dbpath been set? + self::$dbpath = !is_null( $path ) ? $path : self::$dbpath; + if( !self::$dbpath ){ + OC_Log::write( 'migration', 'connectDB() was called without dbpath being set', OC_Log::ERROR ); return false; } - // Set the uid - self::$uid = $uid; - // Create the zip object - self::$zip = new ZipArchive; - // Set export type - self::$exporttype = 'user'; - // Calculate users data dir - $user = OC_User::getUser(); - $userdatadir = OC_Config::getValue( 'datadirectory' ) . '/' . $user . '/'; - // Calculate zip name - $zipname = "oc_userexport_" . $user . '_' . date("y-m-d_H-i-s") . ".zip"; - // Calculate destination - if( !is_null( $path ) ){ - // Path given - // Is a directory? - if( !is_dir( $path ) ){ - OC_Log::write('migration', 'Path supplied to createUserExportFile() is not a directory', OC_Log::ERROR); - return false; - } - // Is writeable - if( !is_writeable( $path ) ){ - OC_Log::write('migration', 'Path supplied to createUserExportFile() is not writeable', OC_Log::ERROR); + // Already connected + if(!self::$MDB2){ + require_once('MDB2.php'); + + $datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ); + + // Prepare options array + $options = array( + 'portability' => MDB2_PORTABILITY_ALL & (!MDB2_PORTABILITY_FIX_CASE), + 'log_line_break' => '
', + 'idxname_format' => '%s', + 'debug' => true, + 'quote_identifier' => true + ); + $dsn = array( + 'phptype' => 'sqlite3', + 'database' => self::$dbpath, + 'mode' => '0644' + ); + + // Try to establish connection + self::$MDB2 = MDB2::factory( $dsn, $options ); + // Die if we could not connect + if( PEAR::isError( self::$MDB2 ) ){ + die( self::$MDB2->getMessage() ); + OC_Log::write( 'migration', 'Failed to create/connect to migration.db', OC_Log::FATAL ); + OC_Log::write( 'migration', self::$MDB2->getUserInfo(), OC_Log::FATAL ); + OC_Log::write( 'migration', self::$MDB2->getMessage(), OC_Log::FATAL ); return false; } - self::$zippath = $path . $zipname; - } else { - // Save in users data dir - self::$zippath = $userdatadir . $zipname; + // We always, really always want associative arrays + self::$MDB2->setFetchMode(MDB2_FETCHMODE_ASSOC); } - // Try to create the zip - if( !self::createZip() ){ - return false; - } - // Export the app info - $appinfo = self::exportAppData(); - // Save the migration results - self::addExportInfo( $appinfo ); - // Add the data dir to the zip - self::addDirToZip( $userdatadir ); - // Close the zip - if( !self::closeZip() ){ + return true; + + } + + /** + * @breif creates the tables in migration.db from an apps database.xml + * @param $appid string id of the app + * @return bool whether the operation was successful + */ + static private function createAppTables( $appid ){ + + if( !self::connectScheme() ){ return false; } - // All good - return self::$zippath; - } - + + // There is a database.xml file + $content = file_get_contents( OC::$SERVERROOT . '/apps/' . $appid . '/appinfo/database.xml' ); + + $file2 = 'static://db_scheme'; + // TODO get the relative path to migration.db from the data dir + // For now just cheat + $path = pathinfo( self::$dbpath ); + $content = str_replace( '*dbname*', self::$uid.'/migration', $content ); + $content = str_replace( '*dbprefix*', '', $content ); + + $xml = new SimpleXMLElement($content); + foreach($xml->table as $table){ + $tables[] = (string)$table->name; + } + + file_put_contents( $file2, $content ); + + // Try to create tables + $definition = self::$schema->parseDatabaseDefinitionFile( $file2 ); + + unlink( $file2 ); + + // Die in case something went wrong + if( $definition instanceof MDB2_Schema_Error ){ + OC_Log::write( 'migration', 'Failed to parse database.xml for: '.$appid, OC_Log::FATAL ); + OC_Log::write( 'migration', $definition->getMessage().': '.$definition->getUserInfo(), OC_Log::FATAL ); + return false; + } + + $definition['overwrite'] = true; + + $ret = self::$schema->createDatabase( $definition ); + + // Die in case something went wrong + if( $ret instanceof MDB2_Error ){ + OC_Log::write( 'migration', 'Failed to create tables for: '.$appid, OC_Log::FATAL ); + OC_Log::write( 'migration', $ret->getMessage().': '.$ret->getUserInfo(), OC_Log::FATAL ); + return false; + } + return $tables; + + } + /** * @breif tries to create the zip + * @param $path string path to zip destination * @return bool */ static private function createZip(){ @@ -340,45 +404,13 @@ class OC_Migrate{ OC_Log::write('migration', 'createZip() called but $zip and/or $zippath have not been set', OC_Log::ERROR); return false; } - if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE ) !== TRUE ) { + if ( self::$zip->open( self::$zippath, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE ) !== TRUE ) { OC_Log::write('migration', 'Failed to create the zip with error: '.self::$zip->getStatusString(), OC_Log::ERROR); return false; } else { return true; } } - - /** - * @breif adds a directory to the zip object - * @param $dir string path of the directory to add - * @param $recursive bool - * @param $internaldir string path of folder to add dir to in zip - * @return bool - */ - static private function addDirToZip($dir, $recursive=true, $internaldir='') { - $dirname = basename($dir); - self::$zip->addEmptyDir($internaldir . $dirname); - $internaldir.=$dirname.='/'; - - if ($dirhandle = opendir($dir)) { - while (false !== ( $file = readdir($dirhandle))) { - - if (( $file != '.' ) && ( $file != '..' )) { - - if (is_dir($dir . '/' . $file) && $recursive) { - self::addDirToZip($dir . '/' . $file, $recursive, $internaldir); - } elseif (is_file($dir . '/' . $file)) { - self::$zip->addFile($dir . '/' . $file, $internaldir . $file); - } - } - } - closedir($dirhandle); - } else { - OC_Log::write('admin_export',"Was not able to open directory: " . $dir,OC_Log::ERROR); - return false; - } - return true; - } /** * @breif returns an array of apps that support migration @@ -429,10 +461,17 @@ class OC_Migrate{ foreach( self::$providers as $provider){ // Is the app in the export? - $id = $provider->id; + $id = $provider->getID(); if( isset( $info->apps->$id ) ){ // Did it succeed? if( $info->apps->$id->success ){ + // Give the provider the content object + // TODO PASS THE PATH TO MIGRATION.DB + if( !self::connectDB() ){ + return false; + } + $content = new OC_Migration_Content( self::$zip, self::$db ); + $provider->setObject( $content ); // Then do the import $provider->import( $info->apps->$id, $importinfo ); } @@ -443,252 +482,12 @@ class OC_Migrate{ } - // @breif connects to migration.db, or creates if not found - // @param $db optional path to migration.db, defaults to user data dir - // @return bool whether the operation was successful - private static function connectDB( $dbpath=null ){ - OC_Log::write('migration','connecting to migration.db for user: '.self::$uid,OC_Log::INFO); - // Fail if no user is set - if(!self::$uid){ - OC_Log::write('migration','connectDB() called without self::$uid being set',OC_Log::INFO); - return false; - } - // Already connected - if(!self::$MDB2){ - require_once('MDB2.php'); - - $datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ); - - self::$dbpath = $datadir.'/'.self::$uid.'/migration.db';//!is_null( $dbpath ) ? $dbpath : $datadir.'/'.self::$uid.'/migration.db'; - - // Prepare options array - $options = array( - 'portability' => MDB2_PORTABILITY_ALL & (!MDB2_PORTABILITY_FIX_CASE), - 'log_line_break' => '
', - 'idxname_format' => '%s', - 'debug' => true, - 'quote_identifier' => true - ); - $dsn = array( - 'phptype' => 'sqlite3', - 'database' => self::$dbpath, - 'mode' => '0644' - ); - - // Try to establish connection - self::$MDB2 = MDB2::factory( $dsn, $options ); - // Die if we could not connect - if( PEAR::isError( self::$MDB2 )){ - die(self::$MDB2->getMessage()); - OC_Log::write('migration', 'Failed to create/connect to migration.db',OC_Log::FATAL); - OC_Log::write('migration',self::$MDB2->getUserInfo(),OC_Log::FATAL); - OC_Log::write('migration',self::$MDB2->getMessage(),OC_Log::FATAL); - return false; - } else { - } - // We always, really always want associative arrays - self::$MDB2->setFetchMode(MDB2_FETCHMODE_ASSOC); - } - return true; - - } - - // @breif prepares the db - // @param $query the sql query to prepare - public static function prepare( $query ){ - - // Optimize the query - $query = self::processQuery( $query ); - - // Optimize the query - $query = self::$MDB2->prepare( $query ); - - // Die if we have an error (error means: bad query, not 0 results!) - if( PEAR::isError( $query )) { - $entry = 'DB Error: "'.$result->getMessage().'"
'; - $entry .= 'Offending command was: '.$query.'
'; - OC_Log::write('migration',$entry,OC_Log::FATAL); - return false; - } else { - return $query; - } - - } - - // @breif processes the db query - // @param $query the query to process - // @return string of processed query - private static function processQuery( $query ){ - - self::connectDB(); - $prefix = ''; - - $query = str_replace( '`', '\'', $query ); - $query = str_replace( 'NOW()', 'datetime(\'now\')', $query ); - $query = str_replace( 'now()', 'datetime(\'now\')', $query ); - - // replace table name prefix - $query = str_replace( '*PREFIX*', $prefix, $query ); - - return $query; - - } - - // @brief copys rows to migration.db from the main database - // @param $options array of options. - // @return bool - public static function copyRows( $options ){ - if( !array_key_exists( 'table', $options ) ){ - return false; - } - - $return = array(); - - // Need to include 'where' in the query? - if( array_key_exists( 'matchval', $options ) && array_key_exists( 'matchcol', $options ) ){ - - // If only one matchval, create an array - if(!is_array($options['matchval'])){ - $options['matchval'] = array( $options['matchval'] ); - } - - foreach( $options['matchval'] as $matchval ){ - // Run the query for this match value (where x = y value) - $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] . " WHERE " . $options['matchcol'] . " LIKE ?" ); - $results = $query->execute( array( $matchval ) ); - $newreturns = self::insertData( $results, $options ); - $return = array_merge( $return, $newreturns ); - } - - } else { - // Just get everything - $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] ); - $results = $query->execute(); - $return = self::insertData( $results, $options ); - - } - - return $return; - - } - - // @breif saves a sql data set into migration.db - // @param $data a sql data set returned from self::prepare()->query() - // @param $options array of copyRows options - // @return void - private static function insertData( $data, $options ){ - $return = array(); - while( $row = $data->fetchRow() ){ - // Now save all this to the migration.db - $fields = array(); - $values = array(); - foreach($row as $field=>$value){ - $fields[] = $field; - $values[] = $value; - } - - // Generate some sql - $sql = "INSERT INTO `" . $options['table'] . '` ( `'; - $fieldssql = implode( '`, `', $fields ); - $sql .= $fieldssql . "` ) VALUES( "; - $valuessql = substr( str_repeat( '?, ', count( $fields ) ),0,-2 ); - $sql .= $valuessql . " )"; - // Make the query - $query = self::prepare( $sql ); - if(!$query){ - OC_Log::write('migration','Invalid sql produced: '.$sql,OC_Log::FATAL); - return false; - exit(); - } else { - $query->execute( $values ); - // Do we need to return some values? - if( array_key_exists( 'idcol', $options ) ){ - // Yes we do - $return[] = $row[$options['idcol']]; - } else { - // Take a guess and return the first field :) - $return[] = reset($row); - } - } - } - return $return; - } - - // @breif creates the tables in migration.db from an apps database.xml - // @param $appid string id of the app - // @return bool whether the operation was successful - private static function createAppTables( $appid ){ - - if(!self::connectScheme()){ - return false; - } - - // There is a database.xml file - $content = file_get_contents( OC::$SERVERROOT . '/apps/' . $appid . '/appinfo/database.xml' ); - - $file2 = 'static://db_scheme'; - $content = str_replace( '*dbname*', self::$uid.'/migration', $content ); - $content = str_replace( '*dbprefix*', '', $content ); - - $xml = new SimpleXMLElement($content); - foreach($xml->table as $table){ - $tables[] = (string)$table->name; - } - - file_put_contents( $file2, $content ); - - // Try to create tables - $definition = self::$schema->parseDatabaseDefinitionFile( $file2 ); - - unlink( $file2 ); - - // Die in case something went wrong - if( $definition instanceof MDB2_Schema_Error ){ - OC_Log::write('migration','Failed to parse database.xml for: '.$appid,OC_Log::FATAL); - OC_Log::write('migration',$definition->getMessage().': '.$definition->getUserInfo(),OC_Log::FATAL); - return false; - } - - $definition['overwrite'] = true; - - $ret = self::$schema->createDatabase( $definition ); - // Die in case something went wrong - - if( $ret instanceof MDB2_Error ){ - OC_Log::write('migration','Failed to create tables for: '.$appid,OC_Log::FATAL); - OC_Log::write('migration',$ret->getMessage().': '.$ret->getUserInfo(),OC_Log::FATAL); - return false; - } - return $tables; - - } - - - /** - * @brief connects to a MDB2 database scheme - * @returns true/false - * - * Connects to a MDB2 database scheme - */ - private static function connectScheme(){ - // We need a mdb2 database connection - self::connectDB(); - self::$MDB2->loadModule( 'Manager' ); - self::$MDB2->loadModule( 'Reverse' ); - - // Connect if this did not happen before - if( !self::$schema ){ - require_once('MDB2/Schema.php'); - self::$schema=MDB2_Schema::factory( self::$MDB2 ); - } - - return true; - } - - // @breif creates a new user in the database - // @param $uid string user_id of the user to be created - // @param $hash string hash of the user to be created - // @return bool result of user creation + /* + * @breif creates a new user in the database + * @param $uid string user_id of the user to be created + * @param $hash string hash of the user to be created + * @return bool result of user creation + */ public static function createUser( $uid, $hash ){ // Check if userid exists diff --git a/lib/migrate/provider.php b/lib/migrate/provider.php deleted file mode 100644 index 7ac3cf97cad..00000000000 --- a/lib/migrate/provider.php +++ /dev/null @@ -1,28 +0,0 @@ -id = $appid; - OC_Migrate::registerProvider( $this ); - } - - /** - * @breif exports data for apps - * @param string $uid - * @return array appdata to be exported - */ - abstract function export($uid); - - /** - * @breif imports data for the app - * @param $appinfo object with the data that the app exported - * @param $info array of info including exportinfo.json - * @return void - */ - abstract function import( $appinfo, $info ); -} diff --git a/lib/migration/content.php b/lib/migration/content.php new file mode 100644 index 00000000000..fe8a21a45b4 --- /dev/null +++ b/lib/migration/content.php @@ -0,0 +1,239 @@ +. + * + */ + + +/** + * provides methods to add and access data from the migration + */ +class OC_Migration_Content{ + + private $zip=false; + // Holds the MDB2 object + private $db=false; + // Holds an array of tmpfiles to delete after zip creation + private $tmpfiles=false; + + /** + * @breif sets up the + * @param $zip ZipArchive object + * @param optional $db a MDB2 database object (required for exporttype user) + * @return bool + */ + public function __construct( $zip, $db=false ){ + + $this->zip = $zip; + $this->db = $db; + + } + + // @breif prepares the db + // @param $query the sql query to prepare + public function prepare( $query ){ + + // Optimize the query + $query = $this->processQuery( $query ); + + // Optimize the query + $query = $this->MDB2->prepare( $query ); + + // Die if we have an error (error means: bad query, not 0 results!) + if( PEAR::isError( $query ) ) { + $entry = 'DB Error: "'.$result->getMessage().'"
'; + $entry .= 'Offending command was: '.$query.'
'; + OC_Log::write( 'migration', $entry, OC_Log::FATAL ); + return false; + } else { + return $query; + } + + } + + /** + * @breif processes the db query + * @param $query the query to process + * @return string of processed query + */ + private function processQuery( $query ){ + $query = str_replace( '`', '\'', $query ); + $query = str_replace( 'NOW()', 'datetime(\'now\')', $query ); + $query = str_replace( 'now()', 'datetime(\'now\')', $query ); + // remove table prefixes + $query = str_replace( '*PREFIX*', '', $query ); + return $query; + } + + /** + * @brief copys rows to migration.db from the main database + * @param $options array of options. + * @return bool + */ + public function copyRows( $options ){ + if( !array_key_exists( 'table', $options ) ){ + return false; + } + + $return = array(); + + // Need to include 'where' in the query? + if( array_key_exists( 'matchval', $options ) && array_key_exists( 'matchcol', $options ) ){ + + // If only one matchval, create an array + if(!is_array($options['matchval'])){ + $options['matchval'] = array( $options['matchval'] ); + } + + foreach( $options['matchval'] as $matchval ){ + // Run the query for this match value (where x = y value) + $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] . " WHERE " . $options['matchcol'] . " LIKE ?" ); + $results = $query->execute( array( $matchval ) ); + $newreturns = $this->insertData( $results, $options ); + $return = array_merge( $return, $newreturns ); + } + + } else { + // Just get everything + $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] ); + $results = $query->execute(); + $return = $this->insertData( $results, $options ); + + } + + return $return; + + } + + /** + * @breif saves a sql data set into migration.db + * @param $data a sql data set returned from self::prepare()->query() + * @param $options array of copyRows options + * @return void + */ + private function insertData( $data, $options ){ + $return = array(); + while( $row = $data->fetchRow() ){ + // Now save all this to the migration.db + foreach($row as $field=>$value){ + $fields[] = $field; + $values[] = $value; + } + + // Generate some sql + $sql = "INSERT INTO `" . $options['table'] . '` ( `'; + $fieldssql = implode( '`, `', $fields ); + $sql .= $fieldssql . "` ) VALUES( "; + $valuessql = substr( str_repeat( '?, ', count( $fields ) ),0,-2 ); + $sql .= $valuessql . " )"; + // Make the query + $query = $this->prepare( $sql ); + if( !$query ){ + OC_Log::write( 'migration', 'Invalid sql produced: '.$sql, OC_Log::FATAL ); + return false; + exit(); + } else { + $query->execute( $values ); + // Do we need to return some values? + if( array_key_exists( 'idcol', $options ) ){ + // Yes we do + $return[] = $row[$options['idcol']]; + } else { + // Take a guess and return the first field :) + $return[] = reset($row); + } + } + } + return $return; + } + + /** + * @breif adds a directory to the zip object + * @param $dir string path of the directory to add + * @param $recursive bool + * @param $internaldir string path of folder to add dir to in zip + * @return bool + */ + public function addDir( $dir, $recursive=true, $internaldir='' ) { + $dirname = basename($dir); + $this->zip->addEmptyDir($internaldir . $dirname); + $internaldir.=$dirname.='/'; + + if ($dirhandle = opendir($dir)) { + while (false !== ( $file = readdir($dirhandle))) { + + if (( $file != '.' ) && ( $file != '..' )) { + + if (is_dir($dir . '/' . $file) && $recursive) { + $this->addDir($dir . '/' . $file, $recursive, $internaldir); + } elseif (is_file($dir . '/' . $file)) { + $this->zip->addFile($dir . '/' . $file, $internaldir . $file); + } + } + } + closedir($dirhandle); + } else { + OC_Log::write('admin_export',"Was not able to open directory: " . $dir,OC_Log::ERROR); + return false; + } + return true; + } + + /** + * @breif adds a file to the zip from a given string + * @param $data string of data to add + * @param $path the relative path inside of the zip to save the file to + * @return bool + */ + public function addFromString( $data, $path ){ + // Create a temp file + $file = tempnam( get_temp_dir(). '/', 'oc_export_tmp_' ); + $this->tmpfiles[] = $file; + if( !file_put_contents( $file, $data ) ){ + OC_Log::write( 'migation', 'Failed to save data to a temporary file', OC_Log::ERROR ); + return false; + } + // Add file to the zip + $this->zip->addFile( $file, $path ); + return true; + } + + /** + * @breif closes the zip, removes temp files + * @return bool + */ + public function finish(){ + if( !$this->zip->close() ){ + OC_Log::write( 'migration', 'Failed to write the zip file with error: '.$this->zip->getStatusString(), OC_Log::ERROR ); + return false; + } + $this->cleanup(); + return true; + } + + /** + * @breif cleans up after the zip + */ + private function cleanup(){ + // Delete tmp files + foreach($this->tmpfiles as $i){ + unlink( $i ); + } + } +} \ No newline at end of file diff --git a/lib/migration/provider.php b/lib/migration/provider.php new file mode 100644 index 00000000000..b9e2c476203 --- /dev/null +++ b/lib/migration/provider.php @@ -0,0 +1,49 @@ +id = $appid; + OC_Migrate::registerProvider( $this ); + } + + /** + * @breif exports data for apps + * @return array appdata to be exported + */ + abstract function export( ); + + /** + * @breif imports data for the app + * @return void + */ + abstract function import( ); + + /** + * @breif sets the OC_Migration_Content object to $this->content + * @param $content a OC_Migration_Content object + */ + public function setData( $uid, $content, $info=false, $appinfo=false ){ + $this->content = $content; + $this->uid = $uid; + $this->info = $info; + $this->appinfo = $appinfo; + } + + /** + * @breif returns the appid of the provider + * @return string + */ + public function getID(){ + return $this->id; + } +} -- cgit v1.2.3 From 514c9ad8e7df1d7882adc33c42eb32a209537273 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 20 Mar 2012 20:19:21 +0000 Subject: Added unified import method. --- apps/admin_export/settings.php | 10 +-- apps/bookmarks/lib/migrate.php | 10 +-- apps/user_migrate/admin.php | 18 +--- lib/db.php | 7 +- lib/migrate.php | 188 ++++++++++++++++++++++++++++++++++++++--- lib/migration/content.php | 6 +- lib/migration/provider.php | 10 ++- 7 files changed, 206 insertions(+), 43 deletions(-) (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index af8dd0dbf54..cd85bedcd7d 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -25,8 +25,6 @@ OC_Util::checkAdminUser(); OC_Util::checkAppEnabled('admin_export'); -define('DS', '/'); - // Export? if (isset($_POST['admin_export'])) { // Create the export zip @@ -44,9 +42,11 @@ if (isset($_POST['admin_export'])) { } // Import? } else if( isset($_POST['admin_import']) ){ - - // TODO - // OC_Migrate::import( $pathtozipfile ); + $from = $_FILES['owncloud_import']['tmp_name']; + + if( !OC_Migrate::import( $from ) ){ + die('failed'); + } } else { // fill template diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php index 36a08c0cf40..02c96e59632 100644 --- a/apps/bookmarks/lib/migrate.php +++ b/apps/bookmarks/lib/migrate.php @@ -36,19 +36,19 @@ class OC_Migration_Provider_Bookmarks extends OC_Migration_Provider{ switch( $this->appinfo->version ){ default: // All versions of the app have had the same db structure, so all can use the same import function - $query = OC_Migrate::prepare( "SELECT * FROM bookmarks WHERE user_id LIKE ?" ); - $results = $query->execute( array( $this->info['olduid'] ) ); + $query = $this->content->prepare( "SELECT * FROM bookmarks WHERE user_id LIKE ?" ); + $results = $query->execute( array( $this->olduid ) ); $idmap = array(); - while( $row = $data->fetchRow() ){ + while( $row = $results->fetchRow() ){ // Import each bookmark, saving its id into the map $query = OC_DB::prepare( "INSERT INTO *PREFIX*bookmarks(url, title, user_id, public, added, lastmodified) VALUES (?, ?, ?, ?, ?, ?)" ); - $query->execute( array( $row['url'], $row['title'], $this->info['newuid'], $row['public'], $row['added'], $row['lastmodified'] ) ); + $query->execute( array( $row['url'], $row['title'], $this->uid, $row['public'], $row['added'], $row['lastmodified'] ) ); // Map the id $idmap[$row['id']] = OC_DB::insertid(); } // Now tags foreach($idmap as $oldid => $newid){ - $query = OC_Migrate::prepare( "SELECT * FROM bookmarks_tags WHERE user_id LIKE ?" ); + $query = $this->content->prepare( "SELECT * FROM bookmarks_tags WHERE user_id LIKE ?" ); $results = $query->execute( array( $oldid ) ); while( $row = $data->fetchRow() ){ // Import the tags for this bookmark, using the new bookmark id diff --git a/apps/user_migrate/admin.php b/apps/user_migrate/admin.php index 6f3565788eb..d54bd6965b5 100644 --- a/apps/user_migrate/admin.php +++ b/apps/user_migrate/admin.php @@ -36,24 +36,12 @@ if (isset($_POST['user_import'])) { $from = $_FILES['owncloud_import']['tmp_name']; $to = get_temp_dir().'/'.$importname.'.zip'; if( !move_uploaded_file( $from, $to ) ){ - OC_Log::write('migration',"Failed to copy the uploaded file",OC_Log::INFO); + OC_Log::write( 'user_migrate', "Failed to copy the uploaded file", OC_Log::ERROR ); exit(); } - // Extract zip - $zip = new ZipArchive(); - if ($zip->open(get_temp_dir().'/'.$importname.'.zip') != TRUE) { - OC_Log::write('migration',"Failed to open zip file",OC_Log::INFO); - exit(); - } - $zip->extractTo(get_temp_dir().'/'.$importname.'/'); - $zip->close(); - - $importdir = get_temp_dir() . '/' . $importname; - - // Delete uploaded file - unlink( $importdir . '.zip' ); - + OC_Migrate::import( $to, 'user', 'newuser' ); + die(); // Find folder $files = scandir( $importdir ); unset($files[0]); diff --git a/lib/db.php b/lib/db.php index 07e58590966..bfcff678699 100644 --- a/lib/db.php +++ b/lib/db.php @@ -487,6 +487,7 @@ class OC_DB { /** * @breif replaces the owncloud tables with a new set + * @param $file string path to the MDB2 xml db export file */ public static function replaceDB( $file ){ @@ -503,7 +504,11 @@ class OC_DB { } // Create new tables - self::createDBFromStructure( $file ); + if( self::createDBFromStructure( $file ) ){ + return true; + } else { + return false; + } } diff --git a/lib/migrate.php b/lib/migrate.php index 338d091af8b..0058de73919 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -122,7 +122,6 @@ class OC_Migrate{ } } // Create the zip object - self::$zip = new ZipArchive; if( !self::createZip() ){ return false; } @@ -188,6 +187,177 @@ class OC_Migrate{ return self::$zippath; } + /** + * @breif imports a user, or owncloud instance + * @param $path string path to zip + * @param optional $uid userid of new user + */ + public static function import( $path, $uid=null ){ + OC_Util::checkAdminUser(); + $datadir = OC_Config::getValue( 'datadirectory' ); + // Extract the zip + if( !$extractpath = self::extractZip( $path ) ){ + return false; + } + // Get export_info.json + $scan = scandir( $extractpath ); + // Check for export_info.json + if( !in_array( 'export_info.json', $scan ) ){ + OC_Log::write( 'migration', 'Invalid import file, export_info.json note found', OC_Log::ERROR ); + return false; + } + $json = json_decode( file_get_contents( $extractpath . 'export_info.json' ) ); + self::$exporttype = $json->exporttype; + + // Have we got a user if type is user + if( self::$exporttype == 'user' ){ + if( !$uid ){ + self::$uid = $json->exporteduser; + } else { + self::$uid = $uid; + } + } + + // Handle export types + switch( self::$exporttype ){ + case 'user': + // Check user availability + if( OC_User::userExists( self::$uid ) ){ + OC_Log::write( 'migration', 'User already exists', OC_Log::ERROR ); + return false; + } + // Create the user + if( !self::createUser( self::$uid, $json->hash ) ){ + return false; + } + // Make the new users data dir + $path = $datadir . '/' . self::$uid . '/files/'; + if( !mkdir( $path, 0755, true ) ){ + OC_Log::write( 'migration', 'Failed to create users data dir: '.$path, OC_Log::ERROR ); + return false; + } + // Copy data + if( !self::copy_r( $extractpath . $json->exporteduser . '/files', $datadir . '/' . self::$uid . '/files' ) ){ + return false; + } + // Import user app data + if( !self::importAppData( $extractpath . $json->exporteduser . '/migration.db', $json, self::$uid ) ){ + return false; + } + // All done! + if( !self::unlink_r( $extractpath ) ){ + OC_Log::write( 'migration', 'Failed to delete the extracted zip', OC_Log::ERROR ); + } + return true; + break; + case 'instance': + // Check for new data dir and dbexport before doing anything + // TODO + /* + // Delete current data folder. + OC_Log::write( 'migration', "Deleting current data dir", OC_Log::INFO ); + if( self::unlink_r( $datadir, false ) ){ + OC_Log::write( 'migration', 'Failed to delete the current data dir', OC_Log::ERROR ); + return false; + } + + // Copy over data + if( !self::copy_r( $extractname . 'data', $datadir ) ){ + OC_Log::write( 'migration', 'Failed to copy over data directory', OC_Log::ERROR ); + return false; + } + */ + // Import the db + if( !OC_DB::replaceDB( $extractpath . 'dbexport.xml' ) ){ + return false; + } + // Done + return true; + break; + } + + } + + /** + * @breif recursively deletes a directory + * @param $dir string path of dir to delete + * $param optional $deleteRootToo bool delete the root directory + * @return bool + */ + private static function unlink_r( $dir, $deleteRootToo=true ){ + if( !$dh = @opendir( $dir ) ){ + return false; + } + while (false !== ($obj = readdir($dh))){ + if($obj == '.' || $obj == '..') { + continue; + } + if (!@unlink($dir . '/' . $obj)){ + self::unlink_r($dir.'/'.$obj, true); + } + } + closedir($dh); + if ( $deleteRootToo ) { + @rmdir($dir); + } + return true; + } + + /** + * @breif copies recursively + * @param $path string path to source folder + * @param $dest string path to destination + * @return bool + */ + private static function copy_r( $path, $dest ){ + if( is_dir($path) ){ + @mkdir( $dest ); + $objects = scandir( $path ); + if( sizeof( $objects ) > 0 ){ + foreach( $objects as $file ){ + if( $file == "." || $file == ".." ) + continue; + // go on + if( is_dir( $path . '/' . $file ) ){ + self::copy_r( $path .'/' . $file, $dest . '/' . $file ); + } else { + copy( $path . '/' . $file, $dest . '/' . $file ); + } + } + } + return true; + } + elseif( is_file( $path ) ){ + return copy( $path, $dest ); + } else { + return false; + } + } + + /** + * @breif tries to extract the import zip + * @param $path string path to the zip + * @return string path to extract location (with a trailing slash) or false on failure + */ + static private function extractZip( $path ){ + self::$zip = new ZipArchive; + // Validate path + if( !file_exists( $path ) ){ + OC_Log::write( 'migration', 'Zip not found', OC_Log::ERROR ); + return false; + } + if ( self::$zip->open( $path ) != TRUE ) { + OC_Log::write( 'migration', "Failed to open zip file", OC_Log::ERROR ); + return false; + } + $to = get_temp_dir() . '/oc_import_' . self::$exporttype . '_' . date("y-m-d_H-i-s") . '/'; + if( !self::$zip->extractTo( $to ) ){ + return false; + } + self::$zip->close(); + return $to; + } + /** * @brief connects to a MDB2 database scheme * @returns bool @@ -272,7 +442,6 @@ class OC_Migrate{ $result = $query->execute( array( self::$uid ) ); $row = $result->fetchRow(); $hash = $row ? $row['password'] : false; - die(var_dump($hash)); if( !$hash ){ OC_Log::write( 'migration', 'Failed to get the users password hash', OC_log::ERROR); return false; @@ -287,7 +456,7 @@ class OC_Migrate{ $info = array_merge( $info, (array)$array ); // Create json $json = json_encode( $info ); - return true; + return $json; } /** @@ -399,8 +568,9 @@ class OC_Migrate{ * @return bool */ static private function createZip(){ + self::$zip = new ZipArchive; // Check if properties are set - if( !self::$zip || !self::$zippath ){ + if( !self::$zippath ){ OC_Log::write('migration', 'createZip() called but $zip and/or $zippath have not been set', OC_Log::ERROR); return false; } @@ -435,9 +605,6 @@ class OC_Migrate{ * @return bool if the import succedded */ public static function importAppData( $db, $info, $uid=null ){ - - self::$uid = !is_null( $uid ) ? $uid : $info->exporteduser; - // Check if the db exists if( file_exists( $db ) ){ // Connect to the db @@ -466,12 +633,11 @@ class OC_Migrate{ // Did it succeed? if( $info->apps->$id->success ){ // Give the provider the content object - // TODO PASS THE PATH TO MIGRATION.DB - if( !self::connectDB() ){ + if( !self::connectDB( $db ) ){ return false; } - $content = new OC_Migration_Content( self::$zip, self::$db ); - $provider->setObject( $content ); + $content = new OC_Migration_Content( self::$zip, self::$MDB2 ); + $provider->setData( self::$uid, $content, $info ); // Then do the import $provider->import( $info->apps->$id, $importinfo ); } diff --git a/lib/migration/content.php b/lib/migration/content.php index fe8a21a45b4..d25b5af293c 100644 --- a/lib/migration/content.php +++ b/lib/migration/content.php @@ -53,7 +53,7 @@ class OC_Migration_Content{ $query = $this->processQuery( $query ); // Optimize the query - $query = $this->MDB2->prepare( $query ); + $query = $this->db->prepare( $query ); // Die if we have an error (error means: bad query, not 0 results!) if( PEAR::isError( $query ) ) { @@ -174,7 +174,9 @@ class OC_Migration_Content{ $dirname = basename($dir); $this->zip->addEmptyDir($internaldir . $dirname); $internaldir.=$dirname.='/'; - + if( !file_exists( $dir ) ){ + return false; + } if ($dirhandle = opendir($dir)) { while (false !== ( $file = readdir($dirhandle))) { diff --git a/lib/migration/provider.php b/lib/migration/provider.php index b9e2c476203..d592ed67264 100644 --- a/lib/migration/provider.php +++ b/lib/migration/provider.php @@ -7,7 +7,7 @@ abstract class OC_Migration_Provider{ protected $id=false; protected $content=false; protected $uid=false; - protected $info=false; + protected $olduid=false; protected $appinfo=false; public function __construct( $appid ){ @@ -32,11 +32,13 @@ abstract class OC_Migration_Provider{ * @breif sets the OC_Migration_Content object to $this->content * @param $content a OC_Migration_Content object */ - public function setData( $uid, $content, $info=false, $appinfo=false ){ + public function setData( $uid, $content, $info=false ){ $this->content = $content; $this->uid = $uid; - $this->info = $info; - $this->appinfo = $appinfo; + $this->olduid = $info->exporteduser; + $id = $this->id; + $this->appinfo = $info->apps->$id; + } /** -- cgit v1.2.3 From 0fa5e196ef8d0b220e4af17b008fb4908b080445 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 20 Mar 2012 20:32:01 +0000 Subject: Try to use old uid when importing --- apps/user_migrate/admin.php | 65 ++------------------------------------- apps/user_migrate/appinfo/app.php | 1 - lib/migrate.php | 11 +++++-- 3 files changed, 11 insertions(+), 66 deletions(-) (limited to 'lib') diff --git a/apps/user_migrate/admin.php b/apps/user_migrate/admin.php index d54bd6965b5..c1afb0aed4f 100644 --- a/apps/user_migrate/admin.php +++ b/apps/user_migrate/admin.php @@ -40,69 +40,10 @@ if (isset($_POST['user_import'])) { exit(); } - OC_Migrate::import( $to, 'user', 'newuser' ); - die(); - // Find folder - $files = scandir( $importdir ); - unset($files[0]); - unset($files[1]); - - // Get the user - if( count($files) != 1 ){ - OC_Log::write('migration', 'Invalid import file', OC_Log::ERROR); - die('invalid import, no user included'); - } - - $olduser = reset($files); - - // Check for dbexport.xml and export info and data dir - $files = scandir( $importdir . '/' . $olduser ); - - $required = array( 'migration.db', 'export_info.json', 'files'); - foreach($required as $require){ - if( !in_array( $require, $files) ){ - OC_Log::write('migration', 'Invlaid import file', OC_Log::ERROR); - die('invalid import'); - } - } - - $migrateinfo = $importdir . '/' . $olduser . '/export_info.json'; - $migrateinfo = json_decode( file_get_contents( $migrateinfo ) ); - - // Check if uid is available - if( OC_User::UserExists( $olduser ) ){ - OC_Log::write('migration','Username exists', OC_Log::ERROR); - die('user exists'); - } - - // Create the user - if( !OC_Migrate::createUser( $olduser, $migrateinfo->hash ) ){ - OC_Log::write('migration', 'Failed to create the new user', OC_Log::ERROR); - die('coundlt create new user'); - } - - $datadir = OC_Config::getValue( 'datadirectory' ); - // Make the new users data dir - $path = $datadir . '/' . $olduser . '/files/'; - if( !mkdir( $path, 0755, true ) ){ - OC_Log::write('migration','Failed to create users data dir: '.$path, OC_Log::ERROR); - die('failed to create users data dir'); - } - - // Copy data - if( !copy_r( $importdir . '/' . $olduser . '/files', $datadir . '/' . $olduser . '/files' ) ){ - OC_Log::write('migration','Failed to copy user files to destination', OC_Log::ERROR); - die('failed to copy user files'); + if( !OC_Migrate::import( $to, 'user' ) ){ + die( 'failed to to import' ); } - - // Import user data - if( !OC_Migrate::importAppData( $importdir . '/' . $olduser . '/migration.db', $migrateinfo ) ){ - OC_Log::write('migration','Failed to import user data', OC_Log::ERROR); - die('failed to import user data'); - } - - // All done! - die('done'); + } else { // fill template diff --git a/apps/user_migrate/appinfo/app.php b/apps/user_migrate/appinfo/app.php index 18b97b93df1..4b9cbd1308a 100644 --- a/apps/user_migrate/appinfo/app.php +++ b/apps/user_migrate/appinfo/app.php @@ -22,7 +22,6 @@ */ OC_APP::registerPersonal('user_migrate','settings'); -OC_APP::registerAdmin('user_migrate','admin'); // add settings page to navigation $entry = array( diff --git a/lib/migrate.php b/lib/migrate.php index 0058de73919..8b0a2aa3f74 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -190,9 +190,10 @@ class OC_Migrate{ /** * @breif imports a user, or owncloud instance * @param $path string path to zip + * @param optional $type type of import (user or instance) * @param optional $uid userid of new user */ - public static function import( $path, $uid=null ){ + public static function import( $path, $type='user', $uid=null ){ OC_Util::checkAdminUser(); $datadir = OC_Config::getValue( 'datadirectory' ); // Extract the zip @@ -207,8 +208,12 @@ class OC_Migrate{ return false; } $json = json_decode( file_get_contents( $extractpath . 'export_info.json' ) ); - self::$exporttype = $json->exporttype; - + if( !$json->exporttype != $type ){ + OC_Log::write( 'migration', 'Invalid import file', OC_Log::ERROR ); + return false; + } + self::$exporttype = $type; + // Have we got a user if type is user if( self::$exporttype == 'user' ){ if( !$uid ){ -- cgit v1.2.3 From 892343c7c11e7ba967f69174820a501b3954badb Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Wed, 21 Mar 2012 16:30:59 +0000 Subject: Fix instance import --- apps/admin_export/settings.php | 2 +- lib/migrate.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index cd85bedcd7d..269147a3bc6 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -44,7 +44,7 @@ if (isset($_POST['admin_export'])) { } else if( isset($_POST['admin_import']) ){ $from = $_FILES['owncloud_import']['tmp_name']; - if( !OC_Migrate::import( $from ) ){ + if( !OC_Migrate::import( $from, 'instance' ) ){ die('failed'); } diff --git a/lib/migrate.php b/lib/migrate.php index 8b0a2aa3f74..718dc57b9f3 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -208,7 +208,7 @@ class OC_Migrate{ return false; } $json = json_decode( file_get_contents( $extractpath . 'export_info.json' ) ); - if( !$json->exporttype != $type ){ + if( $json->exporttype != $type ){ OC_Log::write( 'migration', 'Invalid import file', OC_Log::ERROR ); return false; } -- cgit v1.2.3 From 7ed8f3974417964df18b135b154fdfdbd4f1b6ca Mon Sep 17 00:00:00 2001 From: Nils Jansen Date: Fri, 23 Mar 2012 13:34:07 +0100 Subject: as preperation for the sgf viewer app i added a file icon for .sgf files (saved go games), and an according mimetype. --- core/img/filetypes/application-sgf.png | Bin 0 -> 725 bytes lib/mimetypes.fixlist.php | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 core/img/filetypes/application-sgf.png (limited to 'lib') diff --git a/core/img/filetypes/application-sgf.png b/core/img/filetypes/application-sgf.png new file mode 100644 index 00000000000..f171f5579e7 Binary files /dev/null and b/core/img/filetypes/application-sgf.png differ diff --git a/lib/mimetypes.fixlist.php b/lib/mimetypes.fixlist.php index 51f12dbcc29..a40fbd9e228 100644 --- a/lib/mimetypes.fixlist.php +++ b/lib/mimetypes.fixlist.php @@ -16,5 +16,6 @@ return array( 'xls'=>'application/msexcel', 'xlsx'=>'application/msexcel', 'ppt'=>'application/mspowerpoint', - 'pptx'=>'application/mspowerpoint' + 'pptx'=>'application/mspowerpoint', + 'sgf' => 'application/sgf' ); -- cgit v1.2.3 From b201e5152840406f0b5de9a403fd8f6ceedd3636 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 27 Mar 2012 20:43:44 +0000 Subject: Stop error on export --- lib/migration/provider.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/migration/provider.php b/lib/migration/provider.php index d592ed67264..98804ee91c9 100644 --- a/lib/migration/provider.php +++ b/lib/migration/provider.php @@ -32,13 +32,14 @@ abstract class OC_Migration_Provider{ * @breif sets the OC_Migration_Content object to $this->content * @param $content a OC_Migration_Content object */ - public function setData( $uid, $content, $info=false ){ + public function setData( $uid, $content, $info=null ){ $this->content = $content; $this->uid = $uid; - $this->olduid = $info->exporteduser; + if( !is_null( $info ) ){ + $this->olduid = $info->exporteduser; + $this->appinfo = $info->apps->$id; + } $id = $this->id; - $this->appinfo = $info->apps->$id; - } /** -- cgit v1.2.3 From 553f4533c081e38e65ffb9981063d944fc58f431 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 27 Mar 2012 20:45:37 +0000 Subject: look for migrate.php in appinfo folder --- apps/bookmarks/appinfo/migrate.php | 69 ++++++++++++++++++++++++++++++++++++++ apps/bookmarks/lib/migrate.php | 69 -------------------------------------- lib/migrate.php | 2 +- 3 files changed, 70 insertions(+), 70 deletions(-) create mode 100644 apps/bookmarks/appinfo/migrate.php delete mode 100644 apps/bookmarks/lib/migrate.php (limited to 'lib') diff --git a/apps/bookmarks/appinfo/migrate.php b/apps/bookmarks/appinfo/migrate.php new file mode 100644 index 00000000000..02c96e59632 --- /dev/null +++ b/apps/bookmarks/appinfo/migrate.php @@ -0,0 +1,69 @@ +'bookmarks', + 'matchcol'=>'user_id', + 'matchval'=>$this->uid, + 'idcol'=>'id' + ); + $ids = $this->content->copyRows( $options ); + + $options = array( + 'table'=>'bookmarks_tags', + 'matchcol'=>'bookmark_id', + 'matchval'=>$ids + ); + + // Export tags + $ids2 = $this->content->copyRows( $options ); + + // If both returned some ids then they worked + if( is_array( $ids ) && is_array( $ids2 ) ) + { + return true; + } else { + return false; + } + + } + + // Import function for bookmarks + function import( ){ + switch( $this->appinfo->version ){ + default: + // All versions of the app have had the same db structure, so all can use the same import function + $query = $this->content->prepare( "SELECT * FROM bookmarks WHERE user_id LIKE ?" ); + $results = $query->execute( array( $this->olduid ) ); + $idmap = array(); + while( $row = $results->fetchRow() ){ + // Import each bookmark, saving its id into the map + $query = OC_DB::prepare( "INSERT INTO *PREFIX*bookmarks(url, title, user_id, public, added, lastmodified) VALUES (?, ?, ?, ?, ?, ?)" ); + $query->execute( array( $row['url'], $row['title'], $this->uid, $row['public'], $row['added'], $row['lastmodified'] ) ); + // Map the id + $idmap[$row['id']] = OC_DB::insertid(); + } + // Now tags + foreach($idmap as $oldid => $newid){ + $query = $this->content->prepare( "SELECT * FROM bookmarks_tags WHERE user_id LIKE ?" ); + $results = $query->execute( array( $oldid ) ); + while( $row = $data->fetchRow() ){ + // Import the tags for this bookmark, using the new bookmark id + $query = OC_DB::prepare( "INSERT INTO *PREFIX*bookmarks_tags(bookmark_id, tag) VALUES (?, ?)" ); + $query->execute( array( $newid, $row['tag'] ) ); + } + } + // All done! + break; + } + + return true; + } + +} + +// Load the provider +new OC_Migration_Provider_Bookmarks( 'bookmarks' ); \ No newline at end of file diff --git a/apps/bookmarks/lib/migrate.php b/apps/bookmarks/lib/migrate.php deleted file mode 100644 index 02c96e59632..00000000000 --- a/apps/bookmarks/lib/migrate.php +++ /dev/null @@ -1,69 +0,0 @@ -'bookmarks', - 'matchcol'=>'user_id', - 'matchval'=>$this->uid, - 'idcol'=>'id' - ); - $ids = $this->content->copyRows( $options ); - - $options = array( - 'table'=>'bookmarks_tags', - 'matchcol'=>'bookmark_id', - 'matchval'=>$ids - ); - - // Export tags - $ids2 = $this->content->copyRows( $options ); - - // If both returned some ids then they worked - if( is_array( $ids ) && is_array( $ids2 ) ) - { - return true; - } else { - return false; - } - - } - - // Import function for bookmarks - function import( ){ - switch( $this->appinfo->version ){ - default: - // All versions of the app have had the same db structure, so all can use the same import function - $query = $this->content->prepare( "SELECT * FROM bookmarks WHERE user_id LIKE ?" ); - $results = $query->execute( array( $this->olduid ) ); - $idmap = array(); - while( $row = $results->fetchRow() ){ - // Import each bookmark, saving its id into the map - $query = OC_DB::prepare( "INSERT INTO *PREFIX*bookmarks(url, title, user_id, public, added, lastmodified) VALUES (?, ?, ?, ?, ?, ?)" ); - $query->execute( array( $row['url'], $row['title'], $this->uid, $row['public'], $row['added'], $row['lastmodified'] ) ); - // Map the id - $idmap[$row['id']] = OC_DB::insertid(); - } - // Now tags - foreach($idmap as $oldid => $newid){ - $query = $this->content->prepare( "SELECT * FROM bookmarks_tags WHERE user_id LIKE ?" ); - $results = $query->execute( array( $oldid ) ); - while( $row = $data->fetchRow() ){ - // Import the tags for this bookmark, using the new bookmark id - $query = OC_DB::prepare( "INSERT INTO *PREFIX*bookmarks_tags(bookmark_id, tag) VALUES (?, ?)" ); - $query->execute( array( $newid, $row['tag'] ) ); - } - } - // All done! - break; - } - - return true; - } - -} - -// Load the provider -new OC_Migration_Provider_Bookmarks( 'bookmarks' ); \ No newline at end of file diff --git a/lib/migrate.php b/lib/migrate.php index 718dc57b9f3..6b1497d1d0c 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -64,7 +64,7 @@ class OC_Migrate{ $apps = OC_App::getAllApps(); foreach($apps as $app){ - $path = OC::$SERVERROOT . '/apps/' . $app . '/lib/migrate.php'; + $path = OC::$SERVERROOT . '/apps/' . $app . '/appinfo/migrate.php'; if( file_exists( $path ) ){ include( $path ); } -- cgit v1.2.3 From 31d268fe929abefbbf14e76a96c02f18235451a8 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 27 Mar 2012 20:55:53 +0000 Subject: check for sqlite --- lib/migrate.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index 6b1497d1d0c..d7d1400e2fa 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -482,6 +482,12 @@ class OC_Migrate{ $datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ); + // DB type + if( !is_callable( 'sqlite_open' ) || !class_exists( 'SQLite3' ) ){ + OC_Log::write( 'migration', 'SQLite not found', OC_Log::ERROR ); + return false; + } + // Prepare options array $options = array( 'portability' => MDB2_PORTABILITY_ALL & (!MDB2_PORTABILITY_FIX_CASE), -- cgit v1.2.3 From ef33219e4f83652676c7a668b6126c3c1c0da34d Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 27 Mar 2012 21:21:14 +0000 Subject: import method returns each apps' import status --- apps/user_migrate/admin.php | 16 +++++++++++++++- lib/migrate.php | 40 ++++++++++++++++++++++++++-------------- lib/migration/provider.php | 2 +- 3 files changed, 42 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/apps/user_migrate/admin.php b/apps/user_migrate/admin.php index c1afb0aed4f..1c4894c0f22 100644 --- a/apps/user_migrate/admin.php +++ b/apps/user_migrate/admin.php @@ -40,8 +40,22 @@ if (isset($_POST['user_import'])) { exit(); } - if( !OC_Migrate::import( $to, 'user' ) ){ + if( !$appsstatus = OC_Migrate::import( $to, 'user' ) ){ die( 'failed to to import' ); + } else { + // Check import status + foreach( $appsstatus as $app => $status ){ + if( $status != 'true' ){ + // It failed for some reason + if( $status == 'notsupported' ){ + $notsupported[] = $app; + } else if( !$status ){ + $failed[] = $app; + } + } + } + die(print_r($notsupported)); + die( 'Some apps failed to import, or were not supported.' ); } diff --git a/lib/migrate.php b/lib/migrate.php index d7d1400e2fa..b16b84b2a4b 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -246,14 +246,14 @@ class OC_Migrate{ return false; } // Import user app data - if( !self::importAppData( $extractpath . $json->exporteduser . '/migration.db', $json, self::$uid ) ){ + if( !$appsimported = self::importAppData( $extractpath . $json->exporteduser . '/migration.db', $json, self::$uid ) ){ return false; } // All done! if( !self::unlink_r( $extractpath ) ){ OC_Log::write( 'migration', 'Failed to delete the extracted zip', OC_Log::ERROR ); } - return true; + return $appsimported; break; case 'instance': // Check for new data dir and dbexport before doing anything @@ -611,9 +611,9 @@ class OC_Migrate{ /** * @breif imports a new user * @param $db string path to migration.db - * @param $info array of migration ino + * @param $info object of migration info * @param $uid optional uid to use - * @return bool if the import succedded + * @return array of apps with import statuses, or false on failure. */ public static function importAppData( $db, $info, $uid=null ){ // Check if the db exists @@ -641,21 +641,33 @@ class OC_Migrate{ // Is the app in the export? $id = $provider->getID(); if( isset( $info->apps->$id ) ){ - // Did it succeed? - if( $info->apps->$id->success ){ - // Give the provider the content object - if( !self::connectDB( $db ) ){ - return false; + // Is the app installed + if( !OC_App::isEnabled( $id ) ){ + OC_Log::write( 'migration', 'App: ' . $id . ' is not installed, can\'t import data.', OC_Log::INFO ); + $appsstatus[$id] = 'notsupported'; + } else { + // Did it succeed on export? + if( $info->apps->$id->success ){ + // Give the provider the content object + if( !self::connectDB( $db ) ){ + return false; + } + $content = new OC_Migration_Content( self::$zip, self::$MDB2 ); + $provider->setData( self::$uid, $content, $info ); + // Then do the import + if( !$appsstatus[$id] = $provider->import( $info->apps->$id, $importinfo ) ){ + // Failed to import app + OC_Log::write( 'migration', 'Failed to import app data for user: ' . self::$uid . ' for app: ' . $id, OC_Log::ERROR ); + } + } else { + // Add to failed list + $appsstatus[$id] = false; } - $content = new OC_Migration_Content( self::$zip, self::$MDB2 ); - $provider->setData( self::$uid, $content, $info ); - // Then do the import - $provider->import( $info->apps->$id, $importinfo ); } } } - return true; + return $appsstatus; } diff --git a/lib/migration/provider.php b/lib/migration/provider.php index 98804ee91c9..feae29f1354 100644 --- a/lib/migration/provider.php +++ b/lib/migration/provider.php @@ -35,11 +35,11 @@ abstract class OC_Migration_Provider{ public function setData( $uid, $content, $info=null ){ $this->content = $content; $this->uid = $uid; + $id = $this->id; if( !is_null( $info ) ){ $this->olduid = $info->exporteduser; $this->appinfo = $info->apps->$id; } - $id = $this->id; } /** -- cgit v1.2.3 From 73eca66a892ade0894e35fb4b57d7b45a5e1c872 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Thu, 29 Mar 2012 10:17:08 +0000 Subject: Fix comments --- lib/migrate.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index b16b84b2a4b..fe5ac456005 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -75,7 +75,8 @@ class OC_Migrate{ * @breif exports a user, or owncloud instance * @param ootional $type string type of export, defualts to user * @param otional $path string path to zip output folder - * @param optional $uid string user id of user to export if export type is user, defaults to current + * @param optional $uid string user id of user to export if export type is user, defaults to current + * @return false on error, path to zip on success */ public static function export( $type='user', $path=null, $uid=null ){ $datadir = OC_Config::getValue( 'datadirectory' ); -- cgit v1.2.3 From 65eee1f69dbbbe86bb4bf0716a464dc9f2c66a67 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 30 Mar 2012 19:44:38 +0200 Subject: clean pre-path_hash fscache entries --- db_structure.xml | 3 +-- files/ajax/scan.php | 1 + lib/filecache.php | 8 ++++++++ 3 files changed, 10 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/db_structure.xml b/db_structure.xml index 82d2a731d4c..2df218d359c 100644 --- a/db_structure.xml +++ b/db_structure.xml @@ -67,8 +67,7 @@ path_hash text - - + true 32 diff --git a/files/ajax/scan.php b/files/ajax/scan.php index 565275911b4..db09b7d5c64 100644 --- a/files/ajax/scan.php +++ b/files/ajax/scan.php @@ -17,6 +17,7 @@ if($force or !OC_FileCache::inCache('')){ if(!$checkOnly){ OC_DB::beginTransaction(); OC_FileCache::scan('',$eventSource); + OC_FileCache::clean(); OC_DB::commit(); $eventSource->send('success',true); }else{ diff --git a/lib/filecache.php b/lib/filecache.php index a8c48e3f144..86d865ed9f3 100644 --- a/lib/filecache.php +++ b/lib/filecache.php @@ -637,6 +637,14 @@ class OC_FileCache{ self::fileSystemWatcherWrite(array('path'=>$path),$root); } } + + /** + * clean old pre-path_hash entries + */ + public static function clean(){ + $query=OC_DB::prepare('DELETE FROM *PREFIX*fscache WHERE LENGTH(path_hash)<30'); + $query->execute(); + } } //watch for changes and try to keep the cache up to date -- cgit v1.2.3 From 3a4521a012fe75d8ec3685e1eb87374e5abd9da2 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 30 Mar 2012 23:15:48 +0200 Subject: Add support for logging to syslog --- lib/log.php | 71 ++++++++++--------------------------------- lib/log/owncloud.php | 78 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/log/syslog.php | 37 +++++++++++++++++++++++ settings/ajax/getlog.php | 2 +- settings/log.php | 2 +- 5 files changed, 133 insertions(+), 57 deletions(-) create mode 100644 lib/log/owncloud.php create mode 100644 lib/log/syslog.php (limited to 'lib') diff --git a/lib/log.php b/lib/log.php index 4e450a027f5..8bb2839be66 100644 --- a/lib/log.php +++ b/lib/log.php @@ -1,78 +1,39 @@ . - * + * Copyright (c) 2012 Bart Visscher + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. */ /** - *logging utilities + * logging utilities * - * Log is saved at data/owncloud.log (on default) + * Log is saved by default at data/owncloud.log using OC_Log_Owncloud. + * Selecting other backend is done with a config option 'log_type'. */ -class OC_Log{ +class OC_Log { const DEBUG=0; const INFO=1; const WARN=2; const ERROR=3; const FATAL=4; + static protected $class = null; + /** * write a message in the log * @param string $app * @param string $message * @param int level */ - public static function write($app,$message,$level){ - $minLevel=OC_Config::getValue( "loglevel", 2 ); - if($level>=$minLevel){ - $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); - $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); - $entry=array('app'=>$app,'message'=>$message,'level'=>$level,'time'=>time()); - $fh=fopen($logFile,'a'); - fwrite($fh,json_encode($entry)."\n"); - fclose($fh); - } - } - - /** - * get entries from the log in reverse chronological order - * @param int limit - * @param int offset - * @return array - */ - public static function getEntries($limit=50,$offset=0){ - $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); - $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); - $entries=array(); - if(!file_exists($logFile)){ - return array(); - } - $contents=file($logFile); - if(!$contents){//error while reading log - return array(); - } - $end=max(count($contents)-$offset-1,0); - $start=max($end-$limit,0); - for($i=$end;$i>$start;$i--){ - $entries[]=json_decode($contents[$i]); + public static function write($app, $message, $level) { + if (!self::$class) { + self::$class = 'OC_Log_'.ucfirst(OC_Config::getValue('log_type', 'owncloud')); + call_user_func(array(self::$class, 'init')); } - return $entries; + $log_class=self::$class; + $log_class::write($app, $message, $level); } } diff --git a/lib/log/owncloud.php b/lib/log/owncloud.php new file mode 100644 index 00000000000..6df346e9b17 --- /dev/null +++ b/lib/log/owncloud.php @@ -0,0 +1,78 @@ +. + * + */ + +/** + * logging utilities + * + * Log is saved at data/owncloud.log (on default) + */ + +class OC_Log_Owncloud { + /** + * Init class data + */ + public static function init() { + } + + /** + * write a message in the log + * @param string $app + * @param string $message + * @param int level + */ + public static function write($app, $message, $level) { + $minLevel=OC_Config::getValue( "loglevel", 2 ); + if($level>=$minLevel){ + $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); + $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); + $entry=array('app'=>$app, 'message'=>$message, 'level'=>$level,'time'=>time()); + $fh=fopen($logFile, 'a'); + fwrite($fh, json_encode($entry)."\n"); + fclose($fh); + } + } + + /** + * get entries from the log in reverse chronological order + * @param int limit + * @param int offset + * @return array + */ + public static function getEntries($limit=50, $offset=0){ + $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); + $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); + $entries=array(); + if(!file_exists($logFile)) { + return array(); + } + $contents=file($logFile); + if(!$contents) {//error while reading log + return array(); + } + $end=max(count($contents)-$offset-1, 0); + $start=max($end-$limit,0); + for($i=$end;$i>$start;$i--) { + $entries[]=json_decode($contents[$i]); + } + return $entries; + } +} diff --git a/lib/log/syslog.php b/lib/log/syslog.php new file mode 100644 index 00000000000..d1fb28d8b0a --- /dev/null +++ b/lib/log/syslog.php @@ -0,0 +1,37 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class OC_Log_Syslog { + static protected $levels = array( + OC_Log::DEBUG => LOG_DEBUG, + OC_Log::INFO => LOG_INFO, + OC_Log::WARN => LOG_WARNING, + OC_Log::ERROR => LOG_ERR, + OC_Log::FATAL => LOG_CRIT, + ); + + /** + * Init class data + */ + public static function init() { + openlog('ownCloud', LOG_PID | LOG_CONS, LOG_USER); + // Close at shutdown + register_shutdown_function('closelog'); + } + + /** + * write a message in the log + * @param string $app + * @param string $message + * @param int level + */ + public static function write($app, $message, $level) { + $syslog_level = self::$levels[$level]; + syslog($syslog_level, '{'.$app.'} '.$message); + } +} diff --git a/settings/ajax/getlog.php b/settings/ajax/getlog.php index 600ebefcece..ed48b2cae1a 100644 --- a/settings/ajax/getlog.php +++ b/settings/ajax/getlog.php @@ -13,5 +13,5 @@ OC_JSON::checkAdminUser(); $count=(isset($_GET['count']))?$_GET['count']:50; $offset=(isset($_GET['offset']))?$_GET['offset']:0; -$entries=OC_Log::getEntries($count,$offset); +$entries=OC_Log_Owncloud::getEntries($count,$offset); OC_JSON::success(array("data" => $entries)); diff --git a/settings/log.php b/settings/log.php index 946f2b6f8e5..ddbf72c4433 100644 --- a/settings/log.php +++ b/settings/log.php @@ -28,7 +28,7 @@ OC_Util::addStyle( "settings", "settings" ); OC_Util::addScript( "settings", "apps" ); OC_App::setActiveNavigationEntry( "core_log" ); -$entries=OC_Log::getEntries(); +$entries=OC_Log_Owncloud::getEntries(); OC_Util::addScript('settings','log'); OC_Util::addStyle('settings','settings'); -- cgit v1.2.3 From a7438189f315288c5e57bbf3bfb59a37c896cd6c Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 30 Mar 2012 23:31:05 +0200 Subject: Move more from base init to separate functions --- lib/base.php | 64 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 29 deletions(-) (limited to 'lib') diff --git a/lib/base.php b/lib/base.php index b031572f177..e9788f54b66 100644 --- a/lib/base.php +++ b/lib/base.php @@ -229,6 +229,39 @@ class OC{ } } + public static function initTemplateEngine() { + // if the formfactor is not yet autodetected do the autodetection now. For possible forfactors check the detectFormfactor documentation + if(!isset($_SESSION['formfactor'])){ + $_SESSION['formfactor']=OC::detectFormfactor(); + } + // allow manual override via GET parameter + if(isset($_GET['formfactor'])){ + $_SESSION['formfactor']=$_GET['formfactor']; + } + + // Add the stuff we need always + OC_Util::addScript( "jquery-1.6.4.min" ); + OC_Util::addScript( "jquery-ui-1.8.16.custom.min" ); + OC_Util::addScript( "jquery-showpassword" ); + OC_Util::addScript( "jquery.infieldlabel.min" ); + OC_Util::addScript( "jquery-tipsy" ); + OC_Util::addScript( "oc-dialogs" ); + OC_Util::addScript( "js" ); + OC_Util::addScript( "eventsource" ); + OC_Util::addScript( "config" ); + //OC_Util::addScript( "multiselect" ); + OC_Util::addScript('search','result'); + OC_Util::addStyle( "styles" ); + OC_Util::addStyle( "multiselect" ); + OC_Util::addStyle( "jquery-ui-1.8.16.custom" ); + OC_Util::addStyle( "jquery-tipsy" ); + } + + public static function initSession() { + ini_set('session.cookie_httponly','1;'); + session_start(); + } + public static function init(){ // register autoloader spl_autoload_register(array('OC','autoload')); @@ -272,35 +305,8 @@ class OC{ self::checkSSL(); self::checkUpgrade(); - ini_set('session.cookie_httponly','1;'); - session_start(); - - // if the formfactor is not yet autodetected do the autodetection now. For possible forfactors check the detectFormfactor documentation - if(!isset($_SESSION['formfactor'])){ - $_SESSION['formfactor']=OC::detectFormfactor(); - } - // allow manual override via GET parameter - if(isset($_GET['formfactor'])){ - $_SESSION['formfactor']=$_GET['formfactor']; - } - - - // Add the stuff we need always - OC_Util::addScript( "jquery-1.6.4.min" ); - OC_Util::addScript( "jquery-ui-1.8.16.custom.min" ); - OC_Util::addScript( "jquery-showpassword" ); - OC_Util::addScript( "jquery.infieldlabel.min" ); - OC_Util::addScript( "jquery-tipsy" ); - OC_Util::addScript( "oc-dialogs" ); - OC_Util::addScript( "js" ); - OC_Util::addScript( "eventsource" ); - OC_Util::addScript( "config" ); - //OC_Util::addScript( "multiselect" ); - OC_Util::addScript('search','result'); - OC_Util::addStyle( "styles" ); - OC_Util::addStyle( "multiselect" ); - OC_Util::addStyle( "jquery-ui-1.8.16.custom" ); - OC_Util::addStyle( "jquery-tipsy" ); + self::initSession(); + self::initTemplateEngine(); $errors=OC_Util::checkServer(); if(count($errors)>0) { -- cgit v1.2.3 From 3300d6ea532a973e987c7aeeef1af63a60487c58 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 30 Mar 2012 23:33:36 +0200 Subject: checkUpgrade has to be after template initialization The error path of checkUpgrade uses the template --- lib/base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/base.php b/lib/base.php index e9788f54b66..22f7f4ea486 100644 --- a/lib/base.php +++ b/lib/base.php @@ -303,10 +303,10 @@ class OC{ self::checkInstalled(); self::checkSSL(); - self::checkUpgrade(); self::initSession(); self::initTemplateEngine(); + self::checkUpgrade(); $errors=OC_Util::checkServer(); if(count($errors)>0) { -- cgit v1.2.3 From 20fc23c82bbcaff56caafe6a6cc0ef15db9b2bf8 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 30 Mar 2012 23:40:16 +0200 Subject: Move logfile determination to init function --- lib/log/owncloud.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/log/owncloud.php b/lib/log/owncloud.php index 6df346e9b17..5e143205563 100644 --- a/lib/log/owncloud.php +++ b/lib/log/owncloud.php @@ -27,10 +27,14 @@ */ class OC_Log_Owncloud { + static protected $logFile; + /** * Init class data */ public static function init() { + $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); + self::$logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); } /** @@ -42,10 +46,8 @@ class OC_Log_Owncloud { public static function write($app, $message, $level) { $minLevel=OC_Config::getValue( "loglevel", 2 ); if($level>=$minLevel){ - $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); - $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); $entry=array('app'=>$app, 'message'=>$message, 'level'=>$level,'time'=>time()); - $fh=fopen($logFile, 'a'); + $fh=fopen(self::$logFile, 'a'); fwrite($fh, json_encode($entry)."\n"); fclose($fh); } @@ -58,10 +60,9 @@ class OC_Log_Owncloud { * @return array */ public static function getEntries($limit=50, $offset=0){ - $datadir=OC_Config::getValue( "datadirectory", OC::$SERVERROOT.'/data' ); - $logFile=OC_Config::getValue( "logfile", $datadir.'/owncloud.log' ); + self::init(); $entries=array(); - if(!file_exists($logFile)) { + if(!file_exists(self::$logFile)) { return array(); } $contents=file($logFile); -- cgit v1.2.3 From b72d7f1ab324aa5669a1cce69564ff7b10551084 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 31 Mar 2012 16:20:32 +0200 Subject: make sure output buffering is dissabled when using eventsource --- lib/eventsource.php | 1 + 1 file changed, 1 insertion(+) (limited to 'lib') diff --git a/lib/eventsource.php b/lib/eventsource.php index 523f72403c3..dc28616c2dd 100644 --- a/lib/eventsource.php +++ b/lib/eventsource.php @@ -32,6 +32,7 @@ class OC_EventSource{ private $fallBackId=0; public function __construct(){ + @ob_end_clean(); header('Cache-Control: no-cache'); $this->fallback=isset($_GET['fallback']) and $_GET['fallback']=='true'; if($this->fallback){ -- cgit v1.2.3 From 9d2f8aa717826c1db8f8ec5f49a4128af3651edb Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 31 Mar 2012 16:24:53 +0200 Subject: send more progress updates when scanning large folders --- lib/filecache.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/filecache.php b/lib/filecache.php index 86d865ed9f3..59b0fb1b503 100644 --- a/lib/filecache.php +++ b/lib/filecache.php @@ -469,6 +469,10 @@ class OC_FileCache{ * @param string root (optionak) */ public static function scan($path,$eventSource=false,&$count=0,$root=''){ + if($eventSource){ + $eventSource->send('scanning',array('file'=>$path,'count'=>$count)); + } + $lastSend=$count; if(!$root){ $view=OC_Filesystem::getView(); }else{ @@ -482,13 +486,15 @@ class OC_FileCache{ if($filename != '.' and $filename != '..'){ $file=$path.'/'.$filename; if($view->is_dir($file.'/')){ - if($eventSource){ - $eventSource->send('scanning',array('file'=>$file,'count'=>$count)); - } self::scan($file,$eventSource,$count,$root); }else{ $totalSize+=self::scanFile($file,$root); $count++; + if($count>$lastSend+25){ + if($eventSource){ + $eventSource->send('scanning',array('file'=>$path,'count'=>$count)); + } + } } } } -- cgit v1.2.3 From ebc7a6a0a6249213122bf97104f850d2ce6b7c1a Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 31 Mar 2012 16:28:22 +0200 Subject: dont send to much when scanning large folders --- lib/filecache.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/filecache.php b/lib/filecache.php index 59b0fb1b503..4a4183cbdb5 100644 --- a/lib/filecache.php +++ b/lib/filecache.php @@ -490,10 +490,9 @@ class OC_FileCache{ }else{ $totalSize+=self::scanFile($file,$root); $count++; - if($count>$lastSend+25){ - if($eventSource){ - $eventSource->send('scanning',array('file'=>$path,'count'=>$count)); - } + if($count>$lastSend+25 and $eventSource){ + $lastSend=$count; + $eventSource->send('scanning',array('file'=>$path,'count'=>$count)); } } } -- cgit v1.2.3 From 2f68b084919437013cc6d977c0f077e541cf83f9 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sat, 31 Mar 2012 16:40:42 +0200 Subject: fix eventsource for ie --- core/js/eventsource.js | 2 +- lib/eventsource.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/core/js/eventsource.js b/core/js/eventsource.js index 34bce60c24a..08259e02cae 100644 --- a/core/js/eventsource.js +++ b/core/js/eventsource.js @@ -68,7 +68,7 @@ OC.EventSource=function(src,data){ OC.EventSource.fallBackSources=[]; OC.EventSource.iframeCount=0;//number of fallback iframes OC.EventSource.fallBackCallBack=function(id,type,data){ - OC.EventSource.fallBackSources[id].fallBackCallBack(type,JSON.parse(data)); + OC.EventSource.fallBackSources[id].fallBackCallBack(type,data); } OC.EventSource.prototype={ typelessListeners:[], diff --git a/lib/eventsource.php b/lib/eventsource.php index dc28616c2dd..cf10660b94c 100644 --- a/lib/eventsource.php +++ b/lib/eventsource.php @@ -59,7 +59,7 @@ class OC_EventSource{ $type=null; } if($this->fallback){ - $response=''.PHP_EOL; + $response=''.PHP_EOL; echo $response; }else{ if($type){ -- cgit v1.2.3 From d20eea9761238e0569f538c6f8b1bb553068bf7b Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 31 Mar 2012 22:41:43 +0000 Subject: Use ajax to download file, OC_Dialogs for errors --- apps/user_migrate/ajax/export.php | 63 ++++++++++++++++++++++++++++++++ apps/user_migrate/appinfo/app.php | 1 + apps/user_migrate/js/export.js | 27 ++++++++++++++ apps/user_migrate/settings.php | 22 ++--------- apps/user_migrate/templates/settings.php | 14 +++---- lib/migrate.php | 4 +- 6 files changed, 102 insertions(+), 29 deletions(-) create mode 100644 apps/user_migrate/ajax/export.php create mode 100644 apps/user_migrate/js/export.js (limited to 'lib') diff --git a/apps/user_migrate/ajax/export.php b/apps/user_migrate/ajax/export.php new file mode 100644 index 00000000000..ef947c610f2 --- /dev/null +++ b/apps/user_migrate/ajax/export.php @@ -0,0 +1,63 @@ +. + * + */ +// Init owncloud +require_once('../../../lib/base.php'); + +// Check if we are a user +OC_JSON::checkLoggedIn(); +OC_Util::checkAppEnabled('user_migrate'); + OC_JSON::error(); + die(); +// Which operation +if( $_GET['operation']=='create' ){ +$uid = !empty( $_POST['uid'] ) ? $_POST['uid'] : OC_User::getUser(); +if( $uid != OC_User::getUser() ){ + // Needs to be admin to export someone elses account + OC_JSON::error(); + die(); +} +// Create the export zip +if( !$path = OC_Migrate::export( $uid ) ){ + // Error + OC_JSON::error(); + die(); +} else { + // Save path in session + $_SESSION['ocuserexportpath'] = $path; +} +OC_JSON::success(); +die(); +} else if( $_GET['operation']=='download' ){ + // Download the export + $path = isset( $_SESSION['ocuserexportpath'] ) ? $_SESSION['ocuserexportpath'] : false; + if( !$path ){ + die(); + } + header("Content-Type: application/zip"); + header("Content-Disposition: attachment; filename=" . basename($path)); + header("Content-Length: " . filesize($path)); + @ob_end_clean(); + readfile($path); + unlink( $path ); + $_SESSION['ocuserexportpath'] = ''; +} diff --git a/apps/user_migrate/appinfo/app.php b/apps/user_migrate/appinfo/app.php index 18ea8f52b2a..a59b6dd705c 100644 --- a/apps/user_migrate/appinfo/app.php +++ b/apps/user_migrate/appinfo/app.php @@ -23,6 +23,7 @@ OC_APP::registerPersonal( 'user_migrate', 'settings' ); OC_APP::registerAdmin( 'user_migrate', 'admin' ); +OC_Util::addScript( 'user_migrate', 'export'); // add settings page to navigation $entry = array( diff --git a/apps/user_migrate/js/export.js b/apps/user_migrate/js/export.js new file mode 100644 index 00000000000..0e1e396f65d --- /dev/null +++ b/apps/user_migrate/js/export.js @@ -0,0 +1,27 @@ +$(document).ready(function(){ + // Do the export + $('#exportbtn').click(function(){ + // Show loader + $('.loading').show(); + $.getJSON( + OC.filePath('user_migrate','ajax','export.php'), + {operation:'create'}, + function(result){ + if(result.status == 'success'){ + // Download the file + window.location = OC.filePath('user_migrate','ajax','export.php?operation=download') ; + $('.loading').hide(); + $('#exportbtn').val(t('user_migrate', 'Export')); + } else { + // Cancel loading + $('#exportbtn').html('Failed'); + // Show Dialog + OC.dialogs.alert(t('user_migrate', 'Something went wrong while the export file was being generated'), t('user_migrate', 'An error has occurred'), function(){ + $('#exportbtn').html(t('user_migrate', 'Export')+''); + }); + } + } + // End ajax + ); + }); +}); \ No newline at end of file diff --git a/apps/user_migrate/settings.php b/apps/user_migrate/settings.php index 38eee990b46..62f347b3557 100644 --- a/apps/user_migrate/settings.php +++ b/apps/user_migrate/settings.php @@ -24,22 +24,6 @@ */ OC_Util::checkAppEnabled('user_migrate'); -if (isset($_POST['user_export'])) { - // Create the export zip - if( !$path = OC_Migrate::export() ){ - // Error - die('error'); - } else { - // Download it - header("Content-Type: application/zip"); - header("Content-Disposition: attachment; filename=" . basename($path)); - header("Content-Length: " . filesize($path)); - @ob_end_clean(); - readfile($path); - unlink( $path ); - } -} else { - // fill template - $tmpl = new OC_Template('user_migrate', 'settings'); - return $tmpl->fetchPage(); -} \ No newline at end of file +// fill template +$tmpl = new OC_Template('user_migrate', 'settings'); +return $tmpl->fetchPage(); \ No newline at end of file diff --git a/apps/user_migrate/templates/settings.php b/apps/user_migrate/templates/settings.php index 389de563a6f..5f4857de5fa 100644 --- a/apps/user_migrate/templates/settings.php +++ b/apps/user_migrate/templates/settings.php @@ -1,8 +1,6 @@ -
-
- t('Export your user account');?> -

t('This will create a compressed file that contains your ownCloud account.');?> -

- -
- \ No newline at end of file +
+ t('Export your user account');?> +

t('This will create a compressed file that contains your ownCloud account.');?> +

+ +
diff --git a/lib/migrate.php b/lib/migrate.php index fe5ac456005..db3852fb832 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -73,12 +73,12 @@ class OC_Migrate{ /** * @breif exports a user, or owncloud instance + * @param optional $uid string user id of user to export if export type is user, defaults to current * @param ootional $type string type of export, defualts to user * @param otional $path string path to zip output folder - * @param optional $uid string user id of user to export if export type is user, defaults to current * @return false on error, path to zip on success */ - public static function export( $type='user', $path=null, $uid=null ){ + public static function export( $uid=null, $type='user', $path=null ){ $datadir = OC_Config::getValue( 'datadirectory' ); // Validate export type $types = array( 'user', 'instance', 'system', 'userfiles' ); -- cgit v1.2.3 From ce89ff15aaa971ec930196db64e7437c2fe47fa5 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 31 Mar 2012 23:20:08 +0000 Subject: Remove db tmp file after export creation --- lib/migration/content.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/migration/content.php b/lib/migration/content.php index d25b5af293c..a9fa05596b3 100644 --- a/lib/migration/content.php +++ b/lib/migration/content.php @@ -28,7 +28,7 @@ class OC_Migration_Content{ private $zip=false; // Holds the MDB2 object - private $db=false; + private $db=null; // Holds an array of tmpfiles to delete after zip creation private $tmpfiles=false; @@ -38,11 +38,18 @@ class OC_Migration_Content{ * @param optional $db a MDB2 database object (required for exporttype user) * @return bool */ - public function __construct( $zip, $db=false ){ + public function __construct( $zip, $db=null ){ $this->zip = $zip; $this->db = $db; + if( !is_null( $db ) ){ + // Get db path + $db = $this->db->getDatabase(); + $this->tmpfiles[] = $db; + OC_Log::write('user-migrate',$db, OC_Log::INFO); + } + } // @breif prepares the db -- cgit v1.2.3 From daf742c086fefab9d715be3308088e626de1216c Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 31 Mar 2012 23:55:41 +0000 Subject: Fix owncloud log --- apps/contacts/appinfo/migrate.php | 68 +++++++++++++++++++++++++++++++++++++++ lib/log/owncloud.php | 2 +- 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 apps/contacts/appinfo/migrate.php (limited to 'lib') diff --git a/apps/contacts/appinfo/migrate.php b/apps/contacts/appinfo/migrate.php new file mode 100644 index 00000000000..a6c6bc20fa4 --- /dev/null +++ b/apps/contacts/appinfo/migrate.php @@ -0,0 +1,68 @@ +'contacts_addressbooks', + 'matchcol'=>'userid', + 'matchval'=>$this->uid, + 'idcol'=>'id' + ); + $ids = $this->content->copyRows( $options ); + + $options = array( + 'table'=>'contacts_cards', + 'matchcol'=>'addressbookid', + 'matchval'=>$ids + ); + + // Export tags + $ids2 = $this->content->copyRows( $options ); + + // If both returned some ids then they worked + if( is_array( $ids ) && is_array( $ids2 ) ) + { + return true; + } else { + return false; + } + + } + + // Import function for bookmarks + function import( ){ + switch( $this->appinfo->version ){ + default: + // All versions of the app have had the same db structure, so all can use the same import function + $query = $this->content->prepare( "SELECT * FROM contacts_addressbooks WHERE userid LIKE ?" ); + $results = $query->execute( array( $this->olduid ) ); + $idmap = array(); + while( $row = $results->fetchRow() ){ + // Import each bookmark, saving its id into the map + $query = OC_DB::prepare( "INSERT INTO *PREFIX*contacts_addressbooks (`userid`, `displayname`, `uri`, `description`, `ctag`) VALUES (?, ?, ?, ?, ?)" ); + $query->execute( array( $this->uid, $row['displayname'], $row['uri'], $row['description'], $row['ctag'] ) ); + // Map the id + $idmap[$row['id']] = OC_DB::insertid(); + } + // Now tags + foreach($idmap as $oldid => $newid){ + $query = $this->content->prepare( "SELECT * FROM contacts_cards WHERE addressbookid LIKE ?" ); + $results = $query->execute( array( $oldid ) ); + while( $row = $results->fetchRow() ){ + // Import the tags for this bookmark, using the new bookmark id + $query = OC_DB::prepare( "INSERT INTO *PREFIX*contacts_cards (`addressbookid`, `fullname`, `carddata`, `uri`, `lastmodified`) VALUES (?, ?, ?, ?, ?)" ); + $query->execute( array( $newid, $row['fullname'], $row['carddata'], $row['uri'], $row['lastmodified'] ) ); + } + } + // All done! + break; + } + + return true; + } + +} + +// Load the provider +new OC_Migration_Provider_Contacts( 'contacts' ); \ No newline at end of file diff --git a/lib/log/owncloud.php b/lib/log/owncloud.php index 5e143205563..0ed30510134 100644 --- a/lib/log/owncloud.php +++ b/lib/log/owncloud.php @@ -65,7 +65,7 @@ class OC_Log_Owncloud { if(!file_exists(self::$logFile)) { return array(); } - $contents=file($logFile); + $contents=file(self::$logFile); if(!$contents) {//error while reading log return array(); } -- cgit v1.2.3 From ffbd72bbcf775ac31a43b958ec1e8eaddf0f8356 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sun, 1 Apr 2012 00:07:39 +0000 Subject: Fix user app data export --- lib/migration/content.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/migration/content.php b/lib/migration/content.php index a9fa05596b3..d304051f3e6 100644 --- a/lib/migration/content.php +++ b/lib/migration/content.php @@ -47,7 +47,6 @@ class OC_Migration_Content{ // Get db path $db = $this->db->getDatabase(); $this->tmpfiles[] = $db; - OC_Log::write('user-migrate',$db, OC_Log::INFO); } } @@ -110,7 +109,8 @@ class OC_Migration_Content{ foreach( $options['matchval'] as $matchval ){ // Run the query for this match value (where x = y value) - $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] . " WHERE " . $options['matchcol'] . " LIKE ?" ); + $sql = "SELECT * FROM *PREFIX*" . $options['table'] . " WHERE " . $options['matchcol'] . " LIKE ?"; + $query = OC_DB::prepare( $sql ); $results = $query->execute( array( $matchval ) ); $newreturns = $this->insertData( $results, $options ); $return = array_merge( $return, $newreturns ); @@ -118,7 +118,8 @@ class OC_Migration_Content{ } else { // Just get everything - $query = OC_DB::prepare( "SELECT * FROM *PREFIX*" . $options['table'] ); + $sql = "SELECT * FROM *PREFIX*" . $options['table']; + $query = OC_DB::prepare( $sql ); $results = $query->execute(); $return = $this->insertData( $results, $options ); @@ -136,6 +137,7 @@ class OC_Migration_Content{ */ private function insertData( $data, $options ){ $return = array(); + // Foreach row of data to insert while( $row = $data->fetchRow() ){ // Now save all this to the migration.db foreach($row as $field=>$value){ @@ -166,6 +168,8 @@ class OC_Migration_Content{ $return[] = reset($row); } } + $fields = ''; + $values = ''; } return $return; } -- cgit v1.2.3 From eba6a65908ab848741a29467a2c054818e3c740c Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sun, 1 Apr 2012 00:25:47 +0000 Subject: try to use transactions to replace db --- apps/admin_export/settings.php | 2 +- lib/db.php | 9 +++------ lib/migrate.php | 10 ++++++---- 3 files changed, 10 insertions(+), 11 deletions(-) (limited to 'lib') diff --git a/apps/admin_export/settings.php b/apps/admin_export/settings.php index 269147a3bc6..719bedb66e8 100644 --- a/apps/admin_export/settings.php +++ b/apps/admin_export/settings.php @@ -28,7 +28,7 @@ OC_Util::checkAppEnabled('admin_export'); // Export? if (isset($_POST['admin_export'])) { // Create the export zip - if( !$path = OC_Migrate::export( $_POST['export_type'] ) ){ + if( !$path = OC_Migrate::export( null, $_POST['export_type'] ) ){ // Error die('error'); } else { diff --git a/lib/db.php b/lib/db.php index a7b7ae75da0..9c46a40addb 100644 --- a/lib/db.php +++ b/lib/db.php @@ -489,7 +489,7 @@ class OC_DB { public static function replaceDB( $file ){ $apps = OC_App::getAllApps(); - + self::beginTransaction(); // Delete the old tables self::removeDBStructure( OC::$SERVERROOT . '/db_structure.xml' ); @@ -501,11 +501,8 @@ class OC_DB { } // Create new tables - if( self::createDBFromStructure( $file ) ){ - return true; - } else { - return false; - } + self::createDBFromStructure( $file ); + self::commit(); } diff --git a/lib/migrate.php b/lib/migrate.php index db3852fb832..815333b4d89 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -257,28 +257,30 @@ class OC_Migrate{ return $appsimported; break; case 'instance': + /* // Check for new data dir and dbexport before doing anything // TODO - /* + // Delete current data folder. OC_Log::write( 'migration', "Deleting current data dir", OC_Log::INFO ); - if( self::unlink_r( $datadir, false ) ){ + if( !self::unlink_r( $datadir, false ) ){ OC_Log::write( 'migration', 'Failed to delete the current data dir', OC_Log::ERROR ); return false; } // Copy over data - if( !self::copy_r( $extractname . 'data', $datadir ) ){ + if( !self::copy_r( $extractpath . 'userdata', $datadir ) ){ OC_Log::write( 'migration', 'Failed to copy over data directory', OC_Log::ERROR ); return false; } - */ + // Import the db if( !OC_DB::replaceDB( $extractpath . 'dbexport.xml' ) ){ return false; } // Done return true; + */ break; } -- cgit v1.2.3 From 6bb48b2731185dd93ad1a1edc52f33725cd99061 Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Sun, 1 Apr 2012 02:38:26 -0400 Subject: Check file handle exists before trying to read file --- lib/filesystemview.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/filesystemview.php b/lib/filesystemview.php index 39e47975b28..a3736f1976c 100644 --- a/lib/filesystemview.php +++ b/lib/filesystemview.php @@ -137,13 +137,16 @@ class OC_FilesystemView { } public function readfile($path){ $handle=$this->fopen($path,'r'); - $chunkSize = 1024*1024;// 1 MB chunks - while (!feof($handle)) { - echo fread($handle, $chunkSize); - @ob_flush(); - flush(); + if ($handle) { + $chunkSize = 1024*1024;// 1 MB chunks + while (!feof($handle)) { + echo fread($handle, $chunkSize); + @ob_flush(); + flush(); + } + return $this->filesize($path); } - return $this->filesize($path); + return false; } public function is_readable($path){ return $this->basicOperation('is_readable',$path); -- cgit v1.2.3 From 6545e4878715c10d85a72b1cdb3f7cef73d7e383 Mon Sep 17 00:00:00 2001 From: Frank Karlitschek Date: Sun, 1 Apr 2012 11:20:12 +0200 Subject: Show the different editions to the user. Used in the status call, on the personal settings page and in the updater to update to the next available version from the same edition. --- lib/updater.php | 1 + lib/util.php | 8 ++++++++ settings/templates/personal.php | 2 +- status.php | 2 +- 4 files changed, 11 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/updater.php b/lib/updater.php index 57623797ae5..196822ac35d 100644 --- a/lib/updater.php +++ b/lib/updater.php @@ -36,6 +36,7 @@ class OC_Updater{ $version['installed']=OC_Config::getValue('installedat'); $version['updated']=OC_Appconfig::getValue('core', 'lastupdatedat', OC_Config::getValue( 'lastupdatedat')); $version['updatechannel']='stable'; + $version['edition']=OC_Util::getEditionString(); $versionstring=implode('x',$version); //fetch xml data from updater diff --git a/lib/util.php b/lib/util.php index 529b6d958fb..722b7404d0c 100644 --- a/lib/util.php +++ b/lib/util.php @@ -77,6 +77,14 @@ class OC_Util { return '3'; } + /** + * get the current installed edition of ownCloud. There is the community edition that just returns an empty string and the enterprise edition that returns "Enterprise". + * @return string + */ + public static function getEditionString(){ + return ''; + } + /** * add a javascript file * diff --git a/settings/templates/personal.php b/settings/templates/personal.php index 57731d979d9..d40da7eb773 100644 --- a/settings/templates/personal.php +++ b/settings/templates/personal.php @@ -50,7 +50,7 @@ };?>

- ownCloud
+ ownCloud
developed by the ownCloud community

source code licensed freely under AGPL diff --git a/status.php b/status.php index 94c8cfce842..81f339fa53f 100644 --- a/status.php +++ b/status.php @@ -26,7 +26,7 @@ $RUNTIME_NOAPPS = TRUE; //no apps, yet require_once('lib/base.php'); if(OC_Config::getValue('installed')==1) $installed='true'; else $installed='false'; -$values=array('installed'=>$installed,'version'=>implode('.',OC_Util::getVersion()),'versionstring'=>OC_Util::getVersionString()); +$values=array('installed'=>$installed,'version'=>implode('.',OC_Util::getVersion()),'versionstring'=>OC_Util::getVersionString(),'edition'=>OC_Util::getEditionString()); echo(json_encode($values)); -- cgit v1.2.3 From b758725bf7fad960e971adfeb826596e02673244 Mon Sep 17 00:00:00 2001 From: Frank Karlitschek Date: Sun, 1 Apr 2012 17:02:32 +0200 Subject: Try to configure php to enable big file uploads. This doesn´t work always depending on the webserver and php configuration. Let´s try to overwrite some defaults anyways MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/base.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'lib') diff --git a/lib/base.php b/lib/base.php index 22f7f4ea486..a4a94e86968 100644 --- a/lib/base.php +++ b/lib/base.php @@ -277,6 +277,24 @@ class OC{ date_default_timezone_set('Europe/Berlin'); ini_set('arg_separator.output','&'); + //try to configure php to enable big file uploads. + //this doesn´t work always depending on the webserver and php configuration. + //Let´s try to overwrite some defaults anyways + + //try to set the maximum execution time to 60min + @set_time_limit(3600); + @ini_set('max_execution_time',3600); + @ini_set('max_input_time',3600); + + //try to set the maximum filesize to 10G + @ini_set('upload_max_filesize','10G'); + @ini_set('post_max_size','10G'); + @ini_set('file_uploads','50'); + + //try to set the session lifetime to 60min + @ini_set('gc_maxlifetime','3600'); + + //set http auth headers for apache+php-cgi work around if (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'], $matches)) { -- cgit v1.2.3 From 39e8981bc2ef098a020476a9d94a4b0cc4d9e1c9 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 1 Apr 2012 17:31:44 +0200 Subject: oc_db is not pdo also pgsql does not like double quotes --- lib/app.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/app.php b/lib/app.php index 6c882963a02..625ef889670 100755 --- a/lib/app.php +++ b/lib/app.php @@ -103,9 +103,9 @@ class OC_App{ */ public static function getEnabledApps(){ $apps=array(); - $query = OC_DB::prepare( 'SELECT appid FROM *PREFIX*appconfig WHERE configkey = "enabled" AND configvalue="yes"' ); - $query->execute(); - while($row=$query->fetchRow()){ + $query = OC_DB::prepare( 'SELECT appid FROM *PREFIX*appconfig WHERE configkey = \'enabled\' AND configvalue=\'yes\'' ); + $result=$query->execute(); + while($row=$result->fetchRow()){ $apps[]=$row['appid']; } return $apps; @@ -452,7 +452,7 @@ class OC_App{ */ public static function getAppVersions(){ $versions=array(); - $query = OC_DB::prepare( 'SELECT appid, configvalue FROM *PREFIX*appconfig WHERE configkey = "installed_version"' ); + $query = OC_DB::prepare( 'SELECT appid, configvalue FROM *PREFIX*appconfig WHERE configkey = \'installed_version\'' ); $result = $query->execute(); while($row = $result->fetchRow()){ $versions[$row['appid']]=$row['configvalue']; -- cgit v1.2.3 From cce59df2ae399bc43f1c11a3162b4855de70a1bc Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 1 Apr 2012 19:20:59 +0200 Subject: the core apps don't have types --- lib/app.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/app.php b/lib/app.php index 625ef889670..7d5e8fa91c6 100755 --- a/lib/app.php +++ b/lib/app.php @@ -55,7 +55,7 @@ class OC_App{ // Our very own core apps are hardcoded foreach( array('files', 'settings') as $app ){ - if(is_null($types) or self::isType($app,$types)){ + if(is_null($types)){ require( $app.'/appinfo/app.php' ); } } -- cgit v1.2.3 From ff4b0c4d7f6b422c9426205897c015b497429b03 Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Sun, 1 Apr 2012 13:30:41 -0400 Subject: Move writable check into local filestorage so shared files can be renamed --- lib/filestorage/local.php | 4 ++++ lib/filesystemview.php | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/filestorage/local.php b/lib/filestorage/local.php index 688501aee90..bd757f52ce7 100644 --- a/lib/filestorage/local.php +++ b/lib/filestorage/local.php @@ -86,6 +86,10 @@ class OC_Filestorage_Local extends OC_Filestorage{ return $this->delTree($path); } public function rename($path1,$path2){ + if (!$this->is_writable($path1)) { + OC_Log::write('core','unable to rename, file is not writable : '.$path1,OC_Log::ERROR); + return false; + } if(! $this->file_exists($path1)){ OC_Log::write('core','unable to rename, file does not exists : '.$path1,OC_Log::ERROR); return false; diff --git a/lib/filesystemview.php b/lib/filesystemview.php index a3736f1976c..9d530c7ad63 100644 --- a/lib/filesystemview.php +++ b/lib/filesystemview.php @@ -192,7 +192,7 @@ class OC_FilesystemView { return $this->basicOperation('unlink',$path,array('delete')); } public function rename($path1,$path2){ - if(OC_FileProxy::runPreProxies('rename',$path1,$path2) and $this->is_writable($path1) and OC_Filesystem::isValidPath($path2)){ + if(OC_FileProxy::runPreProxies('rename',$path1,$path2) and OC_Filesystem::isValidPath($path2)){ $run=true; OC_Hook::emit( OC_Filesystem::CLASSNAME, OC_Filesystem::signal_rename, array( OC_Filesystem::signal_param_oldpath => $path1 , OC_Filesystem::signal_param_newpath=>$path2, OC_Filesystem::signal_param_run => &$run)); if($run){ -- cgit v1.2.3 From acdce2b1e01f7c0a77b7e7949540e1b0ba94efd1 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Tue, 3 Apr 2012 22:31:34 +0000 Subject: Check blacklist before saving to filesystem --- lib/base.php | 3 +++ lib/filesystem.php | 13 +++++++++++++ 2 files changed, 16 insertions(+) (limited to 'lib') diff --git a/lib/base.php b/lib/base.php index a4a94e86968..83dd0c98f45 100644 --- a/lib/base.php +++ b/lib/base.php @@ -365,6 +365,9 @@ class OC{ OC_App::loadApps(); } } + + // Check for blacklisted files + OC_Hook::connect('OC_Filesystem','write','OC_Filesystem','isBlacklisted'); //make sure temporary files are cleaned up register_shutdown_function(array('OC_Helper','cleanTmp')); diff --git a/lib/filesystem.php b/lib/filesystem.php index 12905d189f9..b6909f5acdf 100644 --- a/lib/filesystem.php +++ b/lib/filesystem.php @@ -298,6 +298,19 @@ class OC_Filesystem{ } return true; } + + /** + * checks if a file is blacklsited for storage in the filesystem + * @param array $data from hook + */ + static public function isBlacklisted($data){ + $blacklist = array('.htaccess'); + $filename = strtolower(basename($data['path'])); + if(in_array($filename,$blacklist)){ + $data['run'] = false; + } + } + /** * following functions are equivilent to their php buildin equivilents for arguments/return values. */ -- cgit v1.2.3 From 96c99125da5fe74d4a8fa329869cca7f775805cf Mon Sep 17 00:00:00 2001 From: Bartek Przybylski Date: Thu, 5 Apr 2012 23:18:44 +0200 Subject: mimetype filter for filepicker --- core/js/oc-dialogs.js | 8 ++++---- files/ajax/rawlist.php | 3 ++- lib/filecache.php | 6 +++--- lib/files.php | 4 ++-- 4 files changed, 11 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/core/js/oc-dialogs.js b/core/js/oc-dialogs.js index e027ffb5103..a3aa1e8c149 100644 --- a/core/js/oc-dialogs.js +++ b/core/js/oc-dialogs.js @@ -107,8 +107,8 @@ OCdialogs = { $(c_id + ' #dirtree').focus(function() { var t = $(this); t.data('oldval', t.val())}) .change({dcid: c_id}, OC.dialogs.handleTreeListSelect); $(c_id).ready(function(){ - $.getJSON(OC.webroot+'/files/ajax/rawlist.php', function(r){OC.dialogs.fillFilePicker(r, c_id, callback)}); - }).data('multiselect', multiselect); + $.getJSON(OC.webroot+'/files/ajax/rawlist.php', {mimetype: mimetype_filter} ,function(r){OC.dialogs.fillFilePicker(r, c_id, callback)}); + }).data('multiselect', multiselect).data('mimetype',mimetype_filter); // build buttons var b = [ {text: t('dialogs', 'Choose'), click: function(){ @@ -222,7 +222,7 @@ OCdialogs = { $(this).children().each(function(i, element) { if (skip_first) {skip_first = false; return; }path += '/'+$(element).text(); }); $(event.data.dcid).data('path', path); $(event.data.dcid + ' .filepicker_loader').css('visibility', 'visible'); - $.getJSON(OC.webroot+'/files/ajax/rawlist.php', {dir: path}, function(r){OC.dialogs.fillFilePicker(r, event.data.dcid)}); + $.getJSON(OC.webroot+'/files/ajax/rawlist.php', {dir: path, mimetype: $(event.data.dcid).data('mimetype')}, function(r){OC.dialogs.fillFilePicker(r, event.data.dcid)}); }, // this function is in early development state, please dont use it unlsess you know what you are doing handlePickerClick:function(element, name, dcid) { @@ -240,6 +240,6 @@ OCdialogs = { var newval = parseInt($(dcid + ' #dirtree option:last').val())+1; $(dcid + ' #dirtree').append(''); $(dcid + ' .filepicker_loader').css('visibility', 'visible'); - $.getJSON(OC.webroot+'/files/ajax/rawlist.php', {dir: p}, function(r){OC.dialogs.fillFilePicker(r, dcid)}); + $.getJSON(OC.webroot+'/files/ajax/rawlist.php', {dir: p, mimetype: $(dcid).data('mimetype')}, function(r){OC.dialogs.fillFilePicker(r, dcid)}); } }; diff --git a/files/ajax/rawlist.php b/files/ajax/rawlist.php index 88ba48a6c48..e02c5b62733 100644 --- a/files/ajax/rawlist.php +++ b/files/ajax/rawlist.php @@ -11,10 +11,11 @@ OC_JSON::checkLoggedIn(); // Load the files $dir = isset( $_GET['dir'] ) ? $_GET['dir'] : ''; +$mimetype = isset($_GET['mimetype']) ? $_GET['mimetype'] : ''; // make filelist $files = array(); -foreach( OC_Files::getdirectorycontent( $dir ) as $i ){ +foreach( OC_Files::getdirectorycontent( $dir, $mimetype ) as $i ){ $i["date"] = OC_Util::formatDate($i["mtime"] ); $i['mimetype_icon'] = $i['type'] == 'dir' ? mimetype_icon('dir'): mimetype_icon($i['mimetype']); $files[] = $i; diff --git a/lib/filecache.php b/lib/filecache.php index 4a4183cbdb5..cdd91dcbfa4 100644 --- a/lib/filecache.php +++ b/lib/filecache.php @@ -240,7 +240,7 @@ class OC_FileCache{ * - encrypted * - versioned */ - public static function getFolderContent($path,$root=''){ + public static function getFolderContent($path,$root='',$mimetype_filter=''){ if(self::isUpdated($path,$root)){ self::updateFolder($path,$root); } @@ -252,8 +252,8 @@ class OC_FileCache{ } $path=$root.$path; $parent=self::getFileId($path); - $query=OC_DB::prepare('SELECT name,ctime,mtime,mimetype,size,encrypted,versioned,writable FROM *PREFIX*fscache WHERE parent=?'); - $result=$query->execute(array($parent))->fetchAll(); + $query=OC_DB::prepare('SELECT name,ctime,mtime,mimetype,size,encrypted,versioned,writable FROM *PREFIX*fscache WHERE parent=? AND (mimetype LIKE ? OR mimetype = ?)'); + $result=$query->execute(array($parent, $mimetype_filter.'%', 'httpd/unix-directory'))->fetchAll(); if(is_array($result)){ return $result; }else{ diff --git a/lib/files.php b/lib/files.php index e7bfbbc19bb..a68c29ad989 100644 --- a/lib/files.php +++ b/lib/files.php @@ -32,11 +32,11 @@ class OC_Files { * get the content of a directory * @param dir $directory */ - public static function getDirectoryContent($directory){ + public static function getDirectoryContent($directory, $mimetype_filter = ''){ if(strpos($directory,OC::$CONFIG_DATADIRECTORY)===0){ $directory=substr($directory,strlen(OC::$CONFIG_DATADIRECTORY)); } - $files=OC_FileCache::getFolderContent($directory); + $files=OC_FileCache::getFolderContent($directory, '', $mimetype_filter); foreach($files as &$file){ $file['directory']=$directory; $file['type']=($file['mimetype']=='httpd/unix-directory')?'dir':'file'; -- cgit v1.2.3 From d2886f202024243ae4d92e2eea9d3726037a9601 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 7 Apr 2012 17:27:09 +0000 Subject: Hide instance import as it eats data --- apps/admin_migrate/templates/settings.php | 9 ++++++++- lib/migrate.php | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/apps/admin_migrate/templates/settings.php b/apps/admin_migrate/templates/settings.php index 36eec84d489..91e305074e4 100644 --- a/apps/admin_migrate/templates/settings.php +++ b/apps/admin_migrate/templates/settings.php @@ -6,12 +6,16 @@

What would you like to export?

- ownCloud instance ( suitable for import )
+ ownCloud instance (suitable for import )
ownCloud system files
Just user files
+

t('Import an ownCloud instance. THIS WILL DELETE ALL CURRENT OWNCLOUD DATA');?> @@ -22,3 +26,6 @@
+ diff --git a/lib/migrate.php b/lib/migrate.php index 815333b4d89..22c05606912 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -258,6 +258,7 @@ class OC_Migrate{ break; case 'instance': /* + * EXPERIMENTAL // Check for new data dir and dbexport before doing anything // TODO -- cgit v1.2.3 From c0869887cf91bf17421a9163069584d29ec5ed49 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 7 Apr 2012 21:55:16 +0000 Subject: Return JSON for import and export methods of OC_Migrate --- apps/bookmarks/appinfo/migrate.php | 1 - apps/user_migrate/admin.php | 6 +++--- apps/user_migrate/ajax/export.php | 35 ++++++++++++++++--------------- lib/migrate.php | 42 +++++++++++++++++++------------------- 4 files changed, 42 insertions(+), 42 deletions(-) (limited to 'lib') diff --git a/apps/bookmarks/appinfo/migrate.php b/apps/bookmarks/appinfo/migrate.php index 603a8b72d3c..c1251e816ec 100644 --- a/apps/bookmarks/appinfo/migrate.php +++ b/apps/bookmarks/appinfo/migrate.php @@ -3,7 +3,6 @@ class OC_Migration_Provider_Bookmarks extends OC_Migration_Provider{ // Create the xml for the user supplied function export( ){ - OC_Log::write('migration','starting export for bookmarks',OC_Log::INFO); $options = array( 'table'=>'bookmarks', 'matchcol'=>'user_id', diff --git a/apps/user_migrate/admin.php b/apps/user_migrate/admin.php index 510c54abe3d..0160753af22 100644 --- a/apps/user_migrate/admin.php +++ b/apps/user_migrate/admin.php @@ -42,15 +42,15 @@ if (isset($_POST['user_import'])) { $tmpl->assign('error',$error); return $tmpl->fetchPage(); } - - if( !$appsstatus = OC_Migrate::import( $to, 'user' ) ){ + $response = json_decode( OC_Migrate::import( $to, 'user' ) ); + if( !$response->success ){ $error = array('error'=>'There was an error while importing the user!','hint'=>'Please check the logs for a more detailed explaination'); $tmpl = new OC_Template('user_migrate', 'admin'); $tmpl->assign('error',$error); return $tmpl->fetchPage(); } else { // Check import status - foreach( $appsstatus as $app => $status ){ + foreach( $response->data as $app => $status ){ if( $status != 'true' ){ // It failed for some reason if( $status == 'notsupported' ){ diff --git a/apps/user_migrate/ajax/export.php b/apps/user_migrate/ajax/export.php index fac96577fa2..86745d6b162 100644 --- a/apps/user_migrate/ajax/export.php +++ b/apps/user_migrate/ajax/export.php @@ -28,28 +28,29 @@ OC_JSON::checkLoggedIn(); OC_Util::checkAppEnabled('user_migrate'); // Which operation if( $_GET['operation']=='create' ){ -$uid = !empty( $_POST['uid'] ) ? $_POST['uid'] : OC_User::getUser(); -if( $uid != OC_User::getUser() ){ - // Needs to be admin to export someone elses account - OC_JSON::error(); - die(); -} -// Create the export zip -if( !$path = OC_Migrate::export( $uid ) ){ - // Error - OC_JSON::error(); + $uid = !empty( $_POST['uid'] ) ? $_POST['uid'] : OC_User::getUser(); + if( $uid != OC_User::getUser() ){ + // Needs to be admin to export someone elses account + OC_JSON::error(); + die(); + } + // Create the export zip + $response = json_decode( OC_Migrate::export( $uid ) ); + if( !$response->success ){ + // Error + OC_JSON::error(); + die(); + } else { + // Save path in session + $_SESSION['ocuserexportpath'] = $response->data; + } + OC_JSON::success(); die(); -} else { - // Save path in session - $_SESSION['ocuserexportpath'] = $path; -} -OC_JSON::success(); -die(); } else if( $_GET['operation']=='download' ){ // Download the export $path = isset( $_SESSION['ocuserexportpath'] ) ? $_SESSION['ocuserexportpath'] : false; if( !$path ){ - die(); + OC_JSON::error(); } header("Content-Type: application/zip"); header("Content-Disposition: attachment; filename=" . basename($path)); diff --git a/lib/migrate.php b/lib/migrate.php index 22c05606912..6734c805fcf 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -84,7 +84,7 @@ class OC_Migrate{ $types = array( 'user', 'instance', 'system', 'userfiles' ); if( !in_array( $type, $types ) ){ OC_Log::write( 'migration', 'Invalid export type', OC_Log::ERROR ); - return false; + return json_encode( array( array( 'success' => false ) ) ); } self::$exporttype = $type; // Userid? @@ -93,7 +93,7 @@ class OC_Migrate{ if( !is_null($uid) ){ if( !OC_User_Database::userExists( $uid ) ){ OC_Log::write('migration', 'User: '.$uid.' is not in the database and so cannot be exported.', OC_Log::ERROR); - return false; + return json_encode( array( 'success' => false ) ); } self::$uid = $uid; } else { @@ -114,7 +114,7 @@ class OC_Migrate{ // Validate custom path if( !file_exists( $path ) || !is_writeable( $path ) ){ OC_Log::write( 'migration', 'Path supplied is invalid.', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } self::$zippath = $path . $zipname; } else { @@ -124,7 +124,7 @@ class OC_Migrate{ } // Create the zip object if( !self::createZip() ){ - return false; + return json_encode( array( 'success' => false ) ); } // Do the export self::findProviders(); @@ -134,7 +134,7 @@ class OC_Migrate{ // Connect to the db self::$dbpath = $datadir . '/' . self::$uid . '/migration.db'; if( !self::connectDB() ){ - return false; + return json_encode( array( 'success' => false ) ); } self::$content = new OC_Migration_Content( self::$zip, self::$MDB2 ); // Export the app info @@ -178,14 +178,14 @@ class OC_Migrate{ break; } if( !$info = self::getExportInfo( $exportdata ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // Add the export info json to the export zip self::$content->addFromString( $info, 'export_info.json' ); if( !self::$content->finish() ){ - return false; + return json_encode( array( 'success' => false ) ); } - return self::$zippath; + return json_encode( array( 'success' => true, 'data' => self::$zippath ) ); } /** @@ -199,19 +199,19 @@ class OC_Migrate{ $datadir = OC_Config::getValue( 'datadirectory' ); // Extract the zip if( !$extractpath = self::extractZip( $path ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // Get export_info.json $scan = scandir( $extractpath ); // Check for export_info.json if( !in_array( 'export_info.json', $scan ) ){ OC_Log::write( 'migration', 'Invalid import file, export_info.json note found', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } $json = json_decode( file_get_contents( $extractpath . 'export_info.json' ) ); if( $json->exporttype != $type ){ OC_Log::write( 'migration', 'Invalid import file', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } self::$exporttype = $type; @@ -230,31 +230,31 @@ class OC_Migrate{ // Check user availability if( OC_User::userExists( self::$uid ) ){ OC_Log::write( 'migration', 'User already exists', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } // Create the user if( !self::createUser( self::$uid, $json->hash ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // Make the new users data dir $path = $datadir . '/' . self::$uid . '/files/'; if( !mkdir( $path, 0755, true ) ){ OC_Log::write( 'migration', 'Failed to create users data dir: '.$path, OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } // Copy data if( !self::copy_r( $extractpath . $json->exporteduser . '/files', $datadir . '/' . self::$uid . '/files' ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // Import user app data if( !$appsimported = self::importAppData( $extractpath . $json->exporteduser . '/migration.db', $json, self::$uid ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // All done! if( !self::unlink_r( $extractpath ) ){ OC_Log::write( 'migration', 'Failed to delete the extracted zip', OC_Log::ERROR ); } - return $appsimported; + return json_encode( array( 'success' => true, 'data' => $appsimported ) ); break; case 'instance': /* @@ -266,21 +266,21 @@ class OC_Migrate{ OC_Log::write( 'migration', "Deleting current data dir", OC_Log::INFO ); if( !self::unlink_r( $datadir, false ) ){ OC_Log::write( 'migration', 'Failed to delete the current data dir', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } // Copy over data if( !self::copy_r( $extractpath . 'userdata', $datadir ) ){ OC_Log::write( 'migration', 'Failed to copy over data directory', OC_Log::ERROR ); - return false; + return json_encode( array( 'success' => false ) ); } // Import the db if( !OC_DB::replaceDB( $extractpath . 'dbexport.xml' ) ){ - return false; + return json_encode( array( 'success' => false ) ); } // Done - return true; + return json_encode( 'success' => true ); */ break; } -- cgit v1.2.3 From 5e314e8effb42b60d720de40fbf74591ae421534 Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sat, 7 Apr 2012 22:00:32 +0000 Subject: Emit hooks for user creation --- lib/migrate.php | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index 6734c805fcf..2730d26a74a 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -232,10 +232,19 @@ class OC_Migrate{ OC_Log::write( 'migration', 'User already exists', OC_Log::ERROR ); return json_encode( array( 'success' => false ) ); } + $run = true; + OC_Hook::emit( "OC_User", "pre_createUser", array( "run" => &$run, "uid" => self::$uid, "password" => $json->hash )); + if( !$run ){ + // Something stopped the user creation + OC_Log::write( 'migration', 'User creation failed', OC_Log::ERROR ); + return json_encode( array( 'success' => false ) ); + } // Create the user if( !self::createUser( self::$uid, $json->hash ) ){ return json_encode( array( 'success' => false ) ); } + // Emit the post_createUser hook (password is already hashed, will cause problems + OC_Hook::emit( "OC_User", "post_createUser", array( "uid" => self::$uid, "password" => $json->hash )); // Make the new users data dir $path = $datadir . '/' . self::$uid . '/files/'; if( !mkdir( $path, 0755, true ) ){ -- cgit v1.2.3 From e5ebbacc9e951d1da82762daf67f993ee20e95cb Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sun, 8 Apr 2012 18:52:31 +0000 Subject: Fix sqlite version detection --- lib/migrate.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index 2730d26a74a..a8a5e581c3a 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -496,10 +496,14 @@ class OC_Migrate{ $datadir = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ); // DB type - if( !is_callable( 'sqlite_open' ) || !class_exists( 'SQLite3' ) ){ + if( class_exists( 'SQLite3' ) ){ + $dbtype = 'sqlite3'; + } else if( is_callable( 'sqlite_open' ) ){ + $dbtype = 'sqlite'; + } else { OC_Log::write( 'migration', 'SQLite not found', OC_Log::ERROR ); return false; - } + } // Prepare options array $options = array( @@ -510,7 +514,7 @@ class OC_Migrate{ 'quote_identifier' => true ); $dsn = array( - 'phptype' => 'sqlite3', + 'phptype' => $dbtype, 'database' => self::$dbpath, 'mode' => '0644' ); -- cgit v1.2.3 From 8e188cd96f4c1f19664aa5ee870420a26d230c5c Mon Sep 17 00:00:00 2001 From: Tom Needham Date: Sun, 8 Apr 2012 19:16:03 +0000 Subject: Copy over all file app data for imported user --- lib/migrate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/migrate.php b/lib/migrate.php index a8a5e581c3a..dff3abe9e93 100644 --- a/lib/migrate.php +++ b/lib/migrate.php @@ -246,13 +246,13 @@ class OC_Migrate{ // Emit the post_createUser hook (password is already hashed, will cause problems OC_Hook::emit( "OC_User", "post_createUser", array( "uid" => self::$uid, "password" => $json->hash )); // Make the new users data dir - $path = $datadir . '/' . self::$uid . '/files/'; + $path = $datadir . '/' . self::$uid; if( !mkdir( $path, 0755, true ) ){ OC_Log::write( 'migration', 'Failed to create users data dir: '.$path, OC_Log::ERROR ); return json_encode( array( 'success' => false ) ); } // Copy data - if( !self::copy_r( $extractpath . $json->exporteduser . '/files', $datadir . '/' . self::$uid . '/files' ) ){ + if( !self::copy_r( $extractpath . $json->exporteduser, $datadir . '/' . self::$uid ) ){ return json_encode( array( 'success' => false ) ); } // Import user app data -- cgit v1.2.3 From 17af1ba893d0b60f8be1d15391d4deb89f4666f4 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sat, 31 Mar 2012 00:03:21 +0200 Subject: Silence error for missing appinfo info file --- lib/app.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/app.php b/lib/app.php index 7d5e8fa91c6..1c81fbd4242 100755 --- a/lib/app.php +++ b/lib/app.php @@ -319,7 +319,7 @@ class OC_App{ $file=OC::$APPSROOT.'/apps/'.$appid.'/appinfo/info.xml'; } $data=array(); - $content=file_get_contents($file); + $content=@file_get_contents($file); if(!$content){ return; } -- cgit v1.2.3