From fb4a6a5788223cf337d5dd0dbf1c5974d5be8228 Mon Sep 17 00:00:00 2001
From: Rasmus Fuhse <fuhse@data-quest.de>
Date: Thu, 11 Sep 2014 17:25:27 +0200
Subject: [PATCH] more works

---
 PluginMarket.class.php          |  1 +
 classes/MarketRelease.class.php | 60 +++++++++++++++++++++++++++++++++
 controllers/myplugins.php       | 13 +++++++
 controllers/update.php          | 18 ++++++++++
 install.sql                     |  3 +-
 views/myplugins/edit.php        | 14 +++++---
 views/myplugins/overview.php    | 20 +++++++++--
 views/presenting/details.php    | 24 +++++++++++++
 8 files changed, 145 insertions(+), 8 deletions(-)
 create mode 100644 controllers/update.php

diff --git a/PluginMarket.class.php b/PluginMarket.class.php
index 1b09a87..b39d88f 100644
--- a/PluginMarket.class.php
+++ b/PluginMarket.class.php
@@ -1,6 +1,7 @@
 <?php
 
 require_once __DIR__."/classes/MarketPlugin.class.php";
+require_once __DIR__."/classes/MarketRelease.class.php";
 
 class PluginMarket extends StudIPPlugin implements SystemPlugin {
 
diff --git a/classes/MarketRelease.class.php b/classes/MarketRelease.class.php
index fa4c936..f4c8952 100644
--- a/classes/MarketRelease.class.php
+++ b/classes/MarketRelease.class.php
@@ -1,7 +1,13 @@
 <?php
 
+require_once 'lib/datei.inc.php';
+
 class MarketRelease extends SimpleORMap {
 
+    static public function getReleaseDataPath() {
+        return $GLOBALS['STUDIP_BASE_PATH'] . "/data/pluginmarket_releases";
+    }
+
     protected static function configure($config = array())
     {
         $config['db_table'] = 'pluginmarket_releases';
@@ -11,4 +17,58 @@ class MarketRelease extends SimpleORMap {
         );
         parent::configure($config);
     }
+
+    public function installFile() {
+        $hash = md5(uniqid());
+        $tmp_folder = $GLOBALS['TMP_PATH']."/temp_plugin_".$hash;
+        mkdir($tmp_folder);
+        $file = $GLOBALS['TMP_PATH']."/temp_plugin_".$hash.".zip";
+        if ($this['repository_download_url']) {
+            file_put_contents($file, file_get_contents($this['repository_download_url']));
+        } elseif ($_FILES['release_file']['tmp_name']) {
+            move_uploaded_file($_FILES['release_file']['tmp_name'], $file);
+        } else {
+            return false;
+        }
+        unzip_file($file, $tmp_folder);
+        $objects = scandir($tmp_folder);
+        if (count($objects) === 3) {
+            foreach ($objects as $object) {
+                if ($object != "." && $object != "..") {
+                    $plugin_dir = $tmp_folder."/".$object;
+                }
+            }
+        } else {
+            $plugin_dir = $tmp_folder;
+        }
+        $this->installFromDirectory($plugin_dir);
+
+        rmdirr($tmp_folder);
+        unlink($file);
+    }
+
+    protected function installFromDirectory($dir) {
+        $manifest = PluginManager::getInstance()->getPluginManifest($dir);
+        $this['studip_min_version'] = $manifest['studipMinVersion'];
+        $this['studip_max_version'] = $manifest['studipMaxVersion'];
+        if (!$this['version']) {
+            $this['version'] = $manifest['version'];
+        }
+        $hash = md5(uniqid());
+        $plugin = $GLOBALS['TMP_PATH']."/plugin_$hash.zip";
+        create_zip_from_directory($dir, $plugin);
+
+        $RELEASE_DATA_PATH = self::getReleaseDataPath();
+        if (!file_exists($RELEASE_DATA_PATH)) {
+            mkdir($RELEASE_DATA_PATH);
+        }
+        if (!$this->getId()) {
+            $this->setId($this->getNewId());
+        }
+
+        copy($plugin, $RELEASE_DATA_PATH."/".$this->getId());
+        unlink($plugin);
+        return true;
+    }
+
 }
\ No newline at end of file
diff --git a/controllers/myplugins.php b/controllers/myplugins.php
index 72e7fac..62f81b5 100644
--- a/controllers/myplugins.php
+++ b/controllers/myplugins.php
@@ -26,6 +26,10 @@ class MypluginsController extends PluginController {
 
     public function edit_action($plugin_id) {
         $this->marketplugin = new MarketPlugin($plugin_id);
+        if (Request::isXhr()) {
+            $this->response->add_header('X-Title', _("Plugin bearbeiten"));
+            $this->set_layout(null);
+        }
     }
 
     public function save_action() {
@@ -38,6 +42,15 @@ class MypluginsController extends PluginController {
             $this->marketplugin['user_id'] = $GLOBALS['user']->id;
         }
         $this->marketplugin->store();
+        $release_data = Request::getArray("release");
+        if ($release_data['type']) {
+            $release = new MarketRelease();
+            $release->setData($release_data);
+            $release['plugin_id'] = $this->marketplugin->getId();
+            $release['user_id'] = $GLOBALS['user']->id;
+            $release->installFile();
+            $release->store();
+        }
         PageLayout::postMessage(MessageBox::success(_("Plugin wurde gespeichert.")));
         $this->redirect("pluginmarket/presenting/details/".$this->marketplugin->getId());
     }
diff --git a/controllers/update.php b/controllers/update.php
new file mode 100644
index 0000000..baa8709
--- /dev/null
+++ b/controllers/update.php
@@ -0,0 +1,18 @@
+<?php
+require_once 'app/controllers/plugin_controller.php';
+
+class UpdateController extends PluginController {
+
+    public function release_action($release_id) {
+        $release = new MarketRelease($release_id);
+        if ($release->isNew()) {
+            throw new Exception("Unknown release.");
+        }
+        if (!$release['repository_download_url']) {
+            //might happen more often than we think, so we better be polite and die.
+            echo "OK";
+            die();
+        }
+    }
+
+}
\ No newline at end of file
diff --git a/install.sql b/install.sql
index fd4a4dc..2e4038c 100644
--- a/install.sql
+++ b/install.sql
@@ -27,7 +27,8 @@ CREATE TABLE IF NOT EXISTS `pluginmarket_releases` (
     `file_id` varchar(32) default NULL,
     `downloads` int(20) NOT NULL default '0',
     `release_type` varchar(255) default NULL,
-    `origin` varchar(255) NOT NULL,
+    `origin` varchar(255) NULL,
+    `repository_download_url` VARCHAR( 128 ) NULL,
     `chdate` int(20) NOT NULL,
     `mkdate` int(20) NOT NULL,
     PRIMARY KEY (`release_id`),
diff --git a/views/myplugins/edit.php b/views/myplugins/edit.php
index 3dfaf1a..d37a4c0 100644
--- a/views/myplugins/edit.php
+++ b/views/myplugins/edit.php
@@ -1,4 +1,4 @@
-<form action="<?= PluginEngine::getLink($plugin, array(), "myplugins/save") ?>" method="post" class="studip_form">
+<form action="<?= PluginEngine::getLink($plugin, array(), "myplugins/save") ?>" method="post" class="studip_form" enctype="multipart/form-data">
     <input type="hidden" name="id" value="<?= $marketplugin->getId() ?>">
     <fieldset>
         <legend>
@@ -44,7 +44,7 @@
 
         <label>
             <?= _("Releasebezeichnung") ?>
-            <input type="text" name="release[name]" placeholder="<?= _("z.B. Rocky Raccoon 3.0.1") ?>">
+            <input type="text" name="release[version]" placeholder="<?= _("z.B. Rocky Raccoon 3.0.1") ?>">
         </label>
 
         <div>
@@ -56,6 +56,10 @@
                 <input type="radio" name="release[type]" value="git">
                 <?= _("Als Git-Branch") ?>
             </label>
+            <label>
+                <input type="radio" name="release[type]" value="">
+                <?= _("Kein Release hinzuf�gen") ?>
+            </label>
         </div>
 
         <fieldset>
@@ -65,7 +69,7 @@
             <label>
                 <a style="cursor: pointer">
                     <?= Assets::img("icons/20/blue/upload") ?>
-                    <input type="file" name="release[file]">
+                    <input type="file" name="release_file">
                 </a>
             </label>
         </fieldset>
@@ -77,10 +81,10 @@
 
             <label>
                 <?= _("Download-URL des Branches oder des Tags") ?>
-                <input type="release[git_download]">
+                <input type="text" name="release[repository_download_url]">
             </label>
             <p class="info">
-                <?= _("Github und gitlab bieten zu jedem Branch und Tag den Download als ZIP-Datei an. Klicken Sie dort mit rechter Maustaste auf den Downloadbutton und kopieren Sie die URL, um sie hier einzuf�gen. Nach dem Speichern hier k�nnen Sie auf github bzw. gitlab Webhooks einrichten, damit der Marktplatz sich automatisch die neuste Version des Plugins vom Repository holt.") ?>
+                <?= _("Github.com und gitlab bieten zu jedem Branch und Tag den Download als ZIP-Datei an. Klicken Sie dort mit rechter Maustaste auf den Downloadbutton und kopieren Sie die URL, um sie hier einzuf�gen. Nach dem Speichern hier k�nnen Sie auf github bzw. gitlab Webhooks einrichten, damit der Marktplatz sich automatisch die neuste Version des Plugins vom Repository holt. Damit ist das Plugin auf dem Pluginmarktplatz immer brandaktuell.") ?>
             </p>
 
         </fieldset>
diff --git a/views/myplugins/overview.php b/views/myplugins/overview.php
index 9e41645..1ce0e4a 100644
--- a/views/myplugins/overview.php
+++ b/views/myplugins/overview.php
@@ -1,6 +1,22 @@
 
-<? foreach ($plugins as $plugin) : ?>
-<? endforeach ?>
+<table class="default">
+    <thead>
+        <tr>
+            <th><?= _("Name") ?></th>
+            <th></th>
+        </tr>
+    </thead>
+    <tbody>
+        <? foreach ($plugins as $marketplugin) : ?>
+        <tr>
+            <td><?= htmlReady($marketplugin['name']) ?></td>
+            <td>
+                <a href="<?= PluginEngine::getLink($plugin, array(), "myplugins/edit/".$marketplugin->getId()) ?>" data-dialog><?= Assets::img("icons/20/blue/edit") ?></a>
+            </td>
+        </tr>
+        <? endforeach ?>
+    </tbody>
+</table>
 
 <?
 $sidebar = Sidebar::Get();
diff --git a/views/presenting/details.php b/views/presenting/details.php
index 5cc5b89..db93063 100644
--- a/views/presenting/details.php
+++ b/views/presenting/details.php
@@ -1,4 +1,28 @@
 <h1><?= htmlReady($marketplugin['name']) ?></h1>
 <div>
     <?= formatReady($marketplugin['description']) ?>
+</div>
+
+<h2><?= _("Releases") ?></h2>
+<table class="default">
+    <thead>
+        <tr>
+            <th><?= _("Version") ?></th>
+            <th><?= _("Miniale Stud.IP-Versionsnummer") ?></th>
+            <th><?= _("Maximale Stud.IP-Versionsnummer") ?></th>
+        </tr>
+    </thead>
+    <tbody>
+    <? foreach ($marketplugin->releases as $release) : ?>
+        <tr>
+            <td><?= htmlReady($release['version']) ?></td>
+            <td><?= $release['studip_min_version'] ? htmlReady($release['studip_min_version']) : " - " ?></td>
+            <td><?= $release['studip_max_version'] ? htmlReady($release['studip_max_version']) : " - " ?></td>
+        </tr>
+    <? endforeach ?>
+    </tbody>
+</table>
+
+<div style="text-align: center">
+    <?= \Studip\LinkButton::create(_("bearbeiten"), PluginEngine::getURL($plugin, array(), "myplugins/edit/".$marketplugin->getId())) ?>
 </div>
\ No newline at end of file
-- 
GitLab