From 484b1d09adf46f94fb409bf125ceb156be267e31 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Fri, 6 Jan 2012 01:07:16 +0100 Subject: package viewer and upload implemented. --- r7r_repo/main.php | 127 ++++++++++++++++++++++++++++++++++++ r7r_repo/models.php | 2 +- r7r_repo/pluginpackage.php | 40 ++++++------ r7r_repo/templates/src/package.html | 37 +++++++++++ r7r_repo/templates/src/upload.html | 12 ++++ 5 files changed, 197 insertions(+), 21 deletions(-) create mode 100644 r7r_repo/templates/src/package.html create mode 100644 r7r_repo/templates/src/upload.html (limited to 'r7r_repo') diff --git a/r7r_repo/main.php b/r7r_repo/main.php index 6c097a8..8554d3c 100644 --- a/r7r_repo/main.php +++ b/r7r_repo/main.php @@ -364,6 +364,133 @@ $url_handlers = array( echo $ste->exectemplate("account.html"); }, + "p" => function(&$data, $url_now, &$url_next) + { + global $ste, $user; + + list($pkgname) = $url_next; + $url_next = array(); + + if(empty($pkgname)) + throw new NotFoundError(); + + try + { + $pkg = Package::by_name($pkgname); + } + catch(DoesNotExistError $e) + { + throw new NotFoundError(); + } + + $ste->vars["title"] = $pkg->get_name(); + $admin_mode = (($user !== NULL) and (($user->isadmin) or ($user->get_id() == $pkg->get_user()->get_id()))); + + if($admin_mode) + { + if(isset($_POST["delete_package"]) and ($_POST["really_delete"] == "yes")) + { + $pkg->delete(); + $ste->vars["success"] = "Package deleted."; + $url_next = array("index"); + return; + } + + if(isset($_POST["new_version"])) + { + if(is_uploaded_file($_FILES["pkgfile"]["tmp_name"])) + { + $raw_pkg = @file_get_contents($_FILES["pkgfile"]["tmp_name"]); + @unlink($_FILES["pkgfile"]["tmp_name"]); + if($raw_pkg === False) + $ste->vars["error"] = "Upload failed."; + else + { + try + { + $newpkg = PluginPackage::load($raw_pkg); + $pkg->newversion($newpkg); + $ste->vars["success"] = "Successfully uploaded new version."; + } + catch(InvalidPackage $e) + { + $ste->vars["error"] = "Invalid package. Reason: " . $e->getMessage(); + } + catch(NotAllowedError $e) + { + $ste->vars["error"] = "This is not allowed. Reason: " . $e->getMessage(); + } + } + } + else + $ste->vars["error"] = "Upload failed."; + } + } + + $ste->vars["package"] = array( + "name" => $pkg->get_name(), + "description" => $pkg->description, + "author" => $pkg->author, + "admin_mode" => $admin_mode, + "version" => $pkg->txtversion + ); + + echo $ste->exectemplate("package.html"); + }, + "upload" => function(&$data, $url_now, &$url_next) + { + global $ste, $settings, $user; + + if(($user === NULL) or ((!$user->isadmin) and ($settings["repo_mode"] == "private"))) + throw new NotFoundError(); + + $url_now = array(); + $ste->vars["menu"] = "upload"; + $ste->vars["title"] = "Upload new package"; + + if(isset($_POST["upload_package"])) + { + if(is_uploaded_file($_FILES["pkgfile"]["tmp_name"])) + { + $raw_pkg = @file_get_contents($_FILES["pkgfile"]["tmp_name"]); + @unlink($_FILES["pkgfile"]["tmp_name"]); + if($raw_pkg === False) + $ste->vars["error"] = "Upload failed."; + else + { + try + { + $newpkg = PluginPackage::load($raw_pkg); + $pkg = Package::create($newpkg->name, $user); + $pkg->newversion($newpkg); + $ste->vars["success"] = "Successfully uploaded new package."; + $url_next = array("p", $newpkg->name); + return; + } + catch(InvalidPackage $e) + { + $ste->vars["error"] = "Invalid package. Reason: " . $e->getMessage(); + } + catch(NotAllowedError $e) + { + $ste->vars["error"] = "This is not allowed. Reason: " . $e->getMessage(); + } + catch(InvalidArgumentException $e) + { + $ste->vars["error"] = $e->getMessage(); + } + catch(AlreadyExistsError $e) + { + $ste->vars["error"] = "A package with this name already exists."; + } + } + } + else + $ste->vars["error"] = "Upload failed."; + } + + echo $ste->exectemplate("upload.html"); + }, "setup" => function(&$data, $url_now, &$url_next) { global $settings, $ste; diff --git a/r7r_repo/models.php b/r7r_repo/models.php index b35a604..cd1ae2b 100644 --- a/r7r_repo/models.php +++ b/r7r_repo/models.php @@ -367,7 +367,7 @@ class Package extends BySQLRowEnabled $update_info = array( "current-version" => $this->lastversion, - "dl-path" => $settings["root_url"] . "/packages/" . urlencode($this->name) . "/versions/" . $this->lastversion + "dl-path" => $settings["repo_baseurl"] . "/packages/" . urlencode($this->name) . "/versions/" . $this->lastversion ); file_put_contents(dirname(__FILE__) . "/../packages/" . urlencode($this->name) . "/update", serialize($update_info)); diff --git a/r7r_repo/pluginpackage.php b/r7r_repo/pluginpackage.php index d8ce170..cf667cc 100644 --- a/r7r_repo/pluginpackage.php +++ b/r7r_repo/pluginpackage.php @@ -61,6 +61,23 @@ function array2dir($a, $dir) } } +function validate_url ($u) { return preg_match("/^http[s]{0,1}:\\/\\/.*$/", $u) != 0; } +function validate_arraydir($a) +{ + if(!is_array($a)) + return False; + foreach($a as $k=>$v) + { + if(!is_string($k)) + return False; + if(is_array($v) and (!validate_arraydir($v))) + return False; + elseif(!is_string($v)) + return False; + } + return True; +} + /* * Class: InvalidPackage * An Exception that 's function can throw, if the package is invalid. @@ -122,23 +139,6 @@ class PluginPackage */ public function validate() { - function validate_url ($u) { return preg_match("/^http[s]{0,1}:\\/\\/.*$/", $u) != 0; } - function validate_arraydir($a) - { - if(!is_array($a)) - return False; - foreach($a as $k=>$v) - { - if(!is_string($k)) - return False; - if(is_array($v) and (!validate_arraydir($v))) - return False; - elseif(!is_string($v)) - return False; - } - return True; - } - if(!is_string($this->code)) throw new InvalidPackage("Invalid code value."); if(!is_string($this->classname)) @@ -156,9 +156,9 @@ class PluginPackage if(!is_string($this->short_description)) throw new InvalidPackage("Invalid short_description value."); - if(($this->updatepath !== NULL) and (!validate_url($this->updatepath))) - throw new InvalidPackage("Invalid updatepath value. Must be an URL."); - if(($this->web !== NULL) and (!validate_url($this->web))) + if((!empty($this->updatepath)) and (!validate_url($this->updatepath))) + throw new InvalidPackage("Invalid updatepath value. Must be an URL. " .$this->updatepath); + if((!empty($this->web)) and (!validate_url($this->web))) throw new InvalidPackage("Invalid web value. Must be an URL."); if(($this->license !== NULL) and (!is_string($this->license))) throw new InvalidPackage("Invalid license value."); diff --git a/r7r_repo/templates/src/package.html b/r7r_repo/templates/src/package.html new file mode 100644 index 0000000..b40f583 --- /dev/null +++ b/r7r_repo/templates/src/package.html @@ -0,0 +1,37 @@ + + + + + +

$package[name]

+ $package[description] + +

Download package.

+ +

Current version: $package[version]

+

Author: $package[author]

+
+ + $package[admin_mode] + + +
+
+

Upload a new version

+
+ +
+
+
+
+

Delete package

+ + +
+
+
+
+
diff --git a/r7r_repo/templates/src/upload.html b/r7r_repo/templates/src/upload.html new file mode 100644 index 0000000..5b46da1 --- /dev/null +++ b/r7r_repo/templates/src/upload.html @@ -0,0 +1,12 @@ + + + + + +
+

Upload a new package

+ + Package file:
+ +
+
-- cgit v1.2.3-70-g09d2