From: <xa...@us...> - 2011-01-17 18:09:25
|
Revision: 7470 http://supertuxkart.svn.sourceforge.net/supertuxkart/?rev=7470&view=rev Author: xapantu Date: 2011-01-17 18:09:19 +0000 (Mon, 17 Jan 2011) Log Message: ----------- [stkaddons] Added documentation and a a few recursive function detect the .xml and repack the .zip properly Modified Paths: -------------- stkaddons/trunk/include/coreAddon.php stkaddons/trunk/tests/addon_test.php Modified: stkaddons/trunk/include/coreAddon.php =================================================================== --- stkaddons/trunk/include/coreAddon.php 2011-01-17 06:07:49 UTC (rev 7469) +++ stkaddons/trunk/include/coreAddon.php 2011-01-17 18:09:19 UTC (rev 7470) @@ -240,7 +240,7 @@ <?php } - /* FIXME: this function needs a lot of cleanup. */ + /* FIXME: this function needs a lot of cleanup / a rewrite. */ function writeConfig() { global $dirDownload, $dirUpload; @@ -334,19 +334,46 @@ if(!sql_exist($this->addonType, "name", $name) && $USER_LOGGED) { + /* We add a new addon only if the user uploaded a file and if it is a .zip */ if(isset($_FILES['file_addon']) and $_FILES['file_addon']['type'] == "application/zip") { $zip_path = zip_path($name); + /* Add a _ until the file if not found. Because if the user upload + * a file with the same name but different content, we would + * have some problem. */ + while(true) + { + if(!file_exists($zip_path)) + { + break; + } + else + { + $zip_path .= "_"; + } + } + /* Little hack for the unit test, forget that */ if(defined("UNIT_TEST")) { $zip_path = "./test.zip"; } - if(!defined("UNIT_TEST")) + else { move_uploaded_file($_FILES['file_addon']['tmp_name'], $zip_path."-uploaded.zip"); } + + /* Read the information from the xml file (track.xml/kart.xml) + * the name, the version of stk, etc... + * This function modify alsor some fields, as the addon group. + **/ $info = read_info_from_zip($zip_path."-uploaded.zip"); + + /* Then, we repack it, the file repacked will be our nice addon + * package. */ repack_zip($zip_path."-uploaded.zip-extract", $zip_path); + + /* And add a entry in the DB, to generate the xml files and the + * addons-view.php page. */ sql_insert($this->addonType, array('user', 'name', 'Description', @@ -365,6 +392,8 @@ $info["version"], $info["designer"], 0)); + + /* Then, we re-load it, to diaply it information in the upload page. */ $this->reqSql = sql_get_all_where($this->addonType, "name", $info["name"]); $this->addonCurrent = sql_next($this->reqSql); } @@ -389,7 +418,7 @@ } } -/* Utilities to generate paths */ +/** Utilities to generate paths */ function image_path($name) { return UP_LOCATION."image/".$name.".png"; @@ -402,10 +431,12 @@ function read_info_from_zip($path_zip) { - /*$zip = zip_open($path_zip);*/ $zip = new ZipArchive; $addon_information = array(); $addon_information["description"] = ""; + + /* FIXME: this list is hardcoded :( It shouldn't */ + /* All attributes we can find if the xml files */ $attribute = array("name", "version", "groups", @@ -428,20 +459,28 @@ "designer", "music", "screenshot"); + + /* We open it, there souldn't be any error here, the file is really a .zip + * and exist. */ if ($zip->open($path_zip) === TRUE) { + /* Make the directory only if it doesn't exist yet, ither wise, it causes + * an error. */ if(!file_exists($path_zip."-extract")) mkdir($path_zip."-extract"); $zip->extractTo($path_zip."-extract"); $zip->close(); - if(file_exists($path_zip."-extract"."/kart.xml")) + + $path_xml = find_xml($path_zip."-extract"); + /* If there is no track/kart .xml, error */ + if($path_xml != false) { $reader = new XMLReader(); + $writer = new XMLWriter(); - $reader->open($path_zip."-extract"."/kart.xml"); - $writer = new XMLWriter(); - $writer->openURI('file://'.realpath($path_zip."-extract"."/kart.xml")); + $reader->open($path_xml); + $writer->openURI('file://'.realpath($path_xml)); $writer->startDocument("1.0"); $writer->setIndent(true); while ($reader->read()) @@ -483,6 +522,7 @@ $writer->flush(); return $addon_information; } + /* Wrong archive, no .xml in it */ else { return null; @@ -507,12 +547,28 @@ echo("Cannot open <$filename>\n"); return false; } + repack_internal($zip, $path_zip); + $succes = $zip->close(); + if(!$succes) + { + echo "Can't close the zip\n"; + return false; + } + return true; +} + +function repack_internal($zip, $path_zip) +{ foreach(scandir($path_zip) as $file) { if($file != ".." and $file != ".") { - if(!$zip->addFile($path_zip."/".$file, $file)) + if(is_dir($path_zip."/".$file)) { + repack_internal($zip, $path_zip."/".$file); + } + else if(!$zip->addFile($path_zip."/".$file, $file)) + { echo "Can't add this file: ".$file."\n"; return false; } @@ -523,12 +579,32 @@ } } } - $succes = $zip->close(); - if(!$succes) +} + +function find_xml($dir) +{ + if(is_dir($dir)) { - echo "Can't close the zip\n"; - return false; - } - return true; + foreach(scandir($dir) as $file) + { + if(is_dir($dir."/".$file) && $file != "." && $file != "..") + { + $name = find_xml($dir."/".$file); + if($name != false) + { + return $name; + } + } + else if(file_exists($dir."/kart.xml")) + { + return $dir."/kart.xml"; + } + else if(file_exists($dir."/track.xml")) + { + return $dir."/track.xml"; + } + } + } + return false; } ?> Modified: stkaddons/trunk/tests/addon_test.php =================================================================== --- stkaddons/trunk/tests/addon_test.php 2011-01-17 06:07:49 UTC (rev 7469) +++ stkaddons/trunk/tests/addon_test.php 2011-01-17 18:09:19 UTC (rev 7470) @@ -62,9 +62,11 @@ $this->assertEquals(true, $stack->setInformation("Description", "New description")); $this->assertEquals(false, $stack->setInformation("property_not_exist", "New description")); - $this->assertEquals(UP_LOCATION."file/test.png", zip_path("test")); + $this->assertEquals(UP_LOCATION."file/test.zip", zip_path("test")); $this->assertEquals(UP_LOCATION."image/test.png", image_path("test")); + $this->assertEquals("test2.zip-extract//bigbuckbunny/kart.xml", find_xml("test2.zip-extract/")); + $USER_LOGGED = false; $this->assertEquals(false, $stack->addAddon("Test_addon", "this is a test description")); $USER_LOGGED = true; @@ -73,13 +75,13 @@ $this->assertEquals(true, $stack->addAddon("Test_addon", "this is a test description")); $_SESSION['range']["manageaddons"] = true; $this->assertEquals(true, $stack->remove()); - $path_zip = "./test.zip"; + $path_zip = "./test2.zip"; $info = read_info_from_zip($path_zip); $this->assertNotNull($info); - $this->assertEquals("chinchila", $info["name"]); + $this->assertEquals("bigbuckbunny", $info["name"]); $this->assertEquals("2", $info["version"]); - $this->assertEquals(true, repack_zip("./test.zip-extract")); + $this->assertEquals(true, repack_zip("./test2.zip-extract", "./test.zip-repack")); } } ?> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |