Merge pull request #14 from jpirek/master

NETBEANS-3239:  Reference to binary of a deleted plugin remains in PPUC catalog
diff --git a/pp3/README.txt b/pp3/README.txt
index 62eff5a..fa224f2 100644
--- a/pp3/README.txt
+++ b/pp3/README.txt
@@ -8,5 +8,6 @@
 	module/Application/config/
 4) setup local database with config/pp3.sql script
 5) to become admin user add your google email into module/Application/config/module.config.php: pp3->admin property 
-6) open pp3/public/ folder in teh browser to see the application
-7) set write access to data/ and public/data folders so app can store files
+6) set write access to data/, vendor/ and public/data folders so app can store files (user uploads, app cache etc...
+7) open pp3/public/ folder in teh browser to see the application
+
diff --git a/pp3/composer.json b/pp3/composer.json
index fc68677..6eb73f0 100755
--- a/pp3/composer.json
+++ b/pp3/composer.json
@@ -17,6 +17,7 @@
         "doctrine/doctrine-orm-module": "^0.9.1",
         "ezyang/htmlpurifier": "^4.10",
         "zendframework/zendoauth": "2.0.*",
-        "knplabs/knp-paginator-bundle": "^3.0"
+        "knplabs/knp-paginator-bundle": "^3.0",
+        "google/apiclient": "^2.4"
     }
 }
diff --git a/pp3/module/Application/config/module.config.php.dist b/pp3/module/Application/config/module.config.php.dist
index 7687101..13fd914 100755
--- a/pp3/module/Application/config/module.config.php.dist
+++ b/pp3/module/Application/config/module.config.php.dist
@@ -19,7 +19,8 @@
         'admin' => array(
             'jan.pirek@gmail.com',
             'jiri.kovalsky@gmail.com'
-        )
+        ),
+        'googleClientId' => '432862904114-ierlf9j6qmmuhtd44ecmlfqivlirq7uc.apps.googleusercontent.com',
     ), 
     'router' => array(
         'routes' => array(  
diff --git a/pp3/module/Application/src/Application/Controller/AdminController.php b/pp3/module/Application/src/Application/Controller/AdminController.php
index 03defb9..215fbb5 100644
--- a/pp3/module/Application/src/Application/Controller/AdminController.php
+++ b/pp3/module/Application/src/Application/Controller/AdminController.php
@@ -119,6 +119,7 @@
                         break;
                     case 'delete':
                         $this->_pluginRepository->remove($plugin);
+                        $this->rebuildAllCatalogs();
                         $this->flashMessenger()->setNamespace('success')->addMessage('Plugin '.$plugin->getName().' deleted.');
                         return $this->redirect()->toRoute('admin', array(
                             'action' => 'index'
@@ -278,6 +279,27 @@
         return $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["HTTP_HOST"].$this->url()->fromRoute('catalogue', array('action' => 'download')).'?id=';
     }
 
+    private function rebuildAllCatalogs() {
+        $versions = $this->_nbVersionRepository->getEntityRepository()->findAll();
+        foreach ($versions as $v) {
+            $version = $v->getVersion();
+            $itemsVerified = $this->_pluginVersionRepository->getVerifiedVersionsByNbVersion($version);
+            $itemsExperimental = $this->_pluginVersionRepository->getNonVerifiedVersionsByNbVersion($version);
+            $catalog = new Catalog($version, $itemsVerified, false, $this->_config['pp3']['dtdPath'], $this->_getCatalogLink());
+            try {
+                $xml = $catalog->asXml(true);
+                $catalog->storeXml($this->_config['pp3']['catalogSavepath'], $xml);
+            } catch (\Exception $e) { }                 
+            
+            $catalog = new Catalog($version, $itemsExperimental, true, $this->_config['pp3']['dtdPath'], $this->_getCatalogLink());
+            try {
+                $xml = $catalog->asXml(true);
+                $catalog->storeXml($this->_config['pp3']['catalogSavepath'], $xml);
+            } catch (\Exception $e) { }                 
+            
+        }
+    }
+
     private function _checkAdminUser() {
         if (!$this->_isAdmin) {
             return $this->redirect()->toRoute('plugin', array(
diff --git a/pp3/module/Application/src/Application/Controller/LoginController.php b/pp3/module/Application/src/Application/Controller/LoginController.php
index dc0cc38..52ce609 100644
--- a/pp3/module/Application/src/Application/Controller/LoginController.php
+++ b/pp3/module/Application/src/Application/Controller/LoginController.php
@@ -6,6 +6,7 @@
 use Zend\Mvc\Controller\AbstractActionController;
 use Zend\View\Model\ViewModel;
 use Zend\Session\Container;
+// use \Google\Apiclient\Client;
 
 class LoginController extends AbstractActionController {
 
@@ -27,13 +28,20 @@
             $response->setContent('');
         } else {
             if ($req->isPost()) {
-                $name = $this->params()->fromPost('name');
-                $email= $this->params()->fromPost('email');
-                $_SESSION['sessionUserId'] = $email;
-                $_SESSION['sessionUserEmail'] = $email;        
-                $this->checkVerifier($email);
-                $this->checkAdmin($email);
-                $response->setContent('reload');
+                $client = new \Google_Client(['client_id' => $this->_config['pp3']['googleClientId']]);  // Specify the CLIENT_ID of the app that accesses the backend
+                $idToken = $this->params()->fromPost('idtoken');
+                $payload = $client->verifyIdToken($idToken);
+                if ($payload) {
+                    $email = $payload['email'];
+                    $_SESSION['sessionUserId'] = $email;
+                    $_SESSION['sessionUserEmail'] = $email;        
+                    $this->checkVerifier($email);
+                    $this->checkAdmin($email);
+                    $response->setContent('reload');
+                } else {
+                    // Invalid ID token
+                    $response->setContent('Failure');
+                }
             }
         }
        return $response;
diff --git a/pp3/module/Application/src/Application/Controller/PluginController.php b/pp3/module/Application/src/Application/Controller/PluginController.php
index 80d3d1e..8e03082 100644
--- a/pp3/module/Application/src/Application/Controller/PluginController.php
+++ b/pp3/module/Application/src/Application/Controller/PluginController.php
@@ -8,6 +8,7 @@
 use Application\Pp\MavenDataLoader;
 use Application\Entity\Plugin;
 use Application\Entity\PluginVersion;
+use Application\Pp\Catalog;
 use Zend\Mail;
 use HTMLPurifier;
 use HTMLPurifier_Config;
@@ -20,13 +21,15 @@
     private $_pluginVersionRepository;
     private $_categoryRepository;
     private $_verifierRepository;
+    private $_nbVersionRepository;
 
-    public function __construct($pluginRepo, $pvRepo, $categRepository, $config, $verifierRepository) {
+    public function __construct($pluginRepo, $pvRepo, $categRepository, $config, $verifierRepository, $nbVersionRepository) {
         parent::__construct($config);
         $this->_pluginRepository = $pluginRepo;
         $this->_pluginVersionRepository = $pvRepo;
         $this->_categoryRepository = $categRepository;       
         $this->_verifierRepository = $verifierRepository;
+        $this->_nbVersionRepository = $nbVersionRepository;
     }
 
     public function syncAction() {
@@ -277,11 +280,37 @@
         };        
         $this->flashMessenger()->setNamespace('success')->addMessage('Plugin '.$plugin->getName().' deleted.');
         $this->_pluginRepository->remove($plugin);
+        $this->rebuildAllCatalogs();
         return $this->redirect()->toRoute('plugin', array(
             'action' => 'list'
         ));
     }
 
+    private function rebuildAllCatalogs() {
+        $versions = $this->_nbVersionRepository->getEntityRepository()->findAll();
+        foreach ($versions as $v) {
+            $version = $v->getVersion();
+            $itemsVerified = $this->_pluginVersionRepository->getVerifiedVersionsByNbVersion($version);
+            $itemsExperimental = $this->_pluginVersionRepository->getNonVerifiedVersionsByNbVersion($version);
+            $catalog = new Catalog($version, $itemsVerified, false, $this->_config['pp3']['dtdPath'], $this->_getCatalogLink());
+            try {
+                $xml = $catalog->asXml(true);
+                $catalog->storeXml($this->_config['pp3']['catalogSavepath'], $xml);
+            } catch (\Exception $e) { }                 
+            
+            $catalog = new Catalog($version, $itemsExperimental, true, $this->_config['pp3']['dtdPath'], $this->_getCatalogLink());
+            try {
+                $xml = $catalog->asXml(true);
+                $catalog->storeXml($this->_config['pp3']['catalogSavepath'], $xml);
+            } catch (\Exception $e) { }                 
+            
+        }
+    }
+
+    private function _getCatalogLink() {
+        return $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["HTTP_HOST"].$this->url()->fromRoute('catalogue', array('action' => 'download')).'?id=';
+    }
+
     private function _validateAndCleanPluginData($author, $name, $license, $description, $shortDescription, $category, $homepage) {
         if (empty($author) || empty($name) || empty($license) || empty($category) || empty($shortDescription)) {
             return false;
diff --git a/pp3/module/Application/src/Application/Controller/PluginVersionController.php b/pp3/module/Application/src/Application/Controller/PluginVersionController.php
index 64410b8..236b6d7 100644
--- a/pp3/module/Application/src/Application/Controller/PluginVersionController.php
+++ b/pp3/module/Application/src/Application/Controller/PluginVersionController.php
@@ -9,6 +9,7 @@
 use Application\Entity\Plugin;
 use Application\Entity\PluginVersion;
 use Application\Entity\NbVersionPluginVersion;
+use Application\Pp\Catalog;
 use HTMLPurifier;
 use HTMLPurifier_Config;
 
@@ -74,6 +75,8 @@
                     $showFlash = true;
                 }
             }
+
+            $this->rebuildAllCatalogs();
             
             if ($showFlash) {
                 $this->flashMessenger()->setNamespace('success')->addMessage('Plugin version updated');
@@ -119,8 +122,34 @@
         };        
         $this->flashMessenger()->setNamespace('success')->addMessage('Plugin version '.$pluginVersion->getVersion().' deleted');
         $this->_pluginVersionRepository->remove($pluginVersion);
+        $this->rebuildAllCatalogs();
         return $this->redirect()->toRoute('plugin', array(
             'action' => 'list'
         ));
     }
+
+    private function rebuildAllCatalogs() {
+        $versions = $this->_nbVersionRepository->getEntityRepository()->findAll();
+        foreach ($versions as $v) {
+            $version = $v->getVersion();
+            $itemsVerified = $this->_pluginVersionRepository->getVerifiedVersionsByNbVersion($version);
+            $itemsExperimental = $this->_pluginVersionRepository->getNonVerifiedVersionsByNbVersion($version);
+            $catalog = new Catalog($version, $itemsVerified, false, $this->_config['pp3']['dtdPath'], $this->_getCatalogLink());
+            try {
+                $xml = $catalog->asXml(true);
+                $catalog->storeXml($this->_config['pp3']['catalogSavepath'], $xml);
+            } catch (\Exception $e) { }                 
+            
+            $catalog = new Catalog($version, $itemsExperimental, true, $this->_config['pp3']['dtdPath'], $this->_getCatalogLink());
+            try {
+                $xml = $catalog->asXml(true);
+                $catalog->storeXml($this->_config['pp3']['catalogSavepath'], $xml);
+            } catch (\Exception $e) { }                 
+            
+        }
+    }
+
+    private function _getCatalogLink() {
+        return $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["HTTP_HOST"].$this->url()->fromRoute('catalogue', array('action' => 'download')).'?id=';
+    }
 }
diff --git a/pp3/module/Application/src/Application/Factory/PluginControllerFactory.php b/pp3/module/Application/src/Application/Factory/PluginControllerFactory.php
index 9f6cce7..bd35002 100755
--- a/pp3/module/Application/src/Application/Factory/PluginControllerFactory.php
+++ b/pp3/module/Application/src/Application/Factory/PluginControllerFactory.php
@@ -9,6 +9,7 @@
 use Application\Repository\CategoryRepository;
 use Application\Controller\PluginController;
 use Application\Repository\VerifierRepository;
+use Application\Repository\NbVersionRepository;
 
 class PluginControllerFactory implements FactoryInterface
 {
@@ -29,6 +30,9 @@
         $verifierRepository = new VerifierRepository();
         $verifierRepository->setEntityManager($em);
 
-        return new PluginController($repository, $pvRepository, $categRepository, $config, $verifierRepository);
+        $nbVersionRepository = new NbVersionRepository();
+        $nbVersionRepository->setEntityManager($em);
+
+        return new PluginController($repository, $pvRepository, $categRepository, $config, $verifierRepository, $nbVersionRepository);
     }
 }
diff --git a/pp3/public/js/script.js b/pp3/public/js/script.js
index f19281d..97e5797 100755
--- a/pp3/public/js/script.js
+++ b/pp3/public/js/script.js
@@ -28,6 +28,7 @@
 function onSignIn(googleUser) {
     var profile = googleUser.getBasicProfile();
     var auth2 = gapi.auth2.getAuthInstance();
+    id_token = googleUser.getAuthResponse().id_token;
     googleUser.disconnect()
     auth2.disconnect();
     $.ajax({
@@ -36,6 +37,7 @@
         data: {
             name: profile.getName(),
             email: profile.getEmail(),
+            idtoken: id_token,
         },
         success: (response) => {
             if(response === 'reload') {