From 22704e94a0fadf2ce447c5cca63faeb2b2de753a Mon Sep 17 00:00:00 2001
From: hahn <hahn@AAE49.campus.unibe.ch>
Date: Fri, 19 Sep 2014 08:25:18 +0200
Subject: [PATCH] - fix vcs.git.class: url is not required field in project
 settings - fix project.class: fetch json with curl http get request (new
 dependency: php5-curl)

---
 .../deployment/classes/project.class.php      | 47 +++++++++++++------
 .../deployment/classes/vcs.git.class.php      | 12 ++++-
 2 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/public_html/deployment/classes/project.class.php b/public_html/deployment/classes/project.class.php
index 6dbaa410..1014a40a 100644
--- a/public_html/deployment/classes/project.class.php
+++ b/public_html/deployment/classes/project.class.php
@@ -396,6 +396,23 @@ class project {
         return $this->_aConfig["archiveDir"] . '/' . $this->_aConfig["id"];
     }
 
+    /**
+     * make an http get request and return the response body
+     * @param string $url
+     * @return string
+     */
+    private function _httpGet($url, $iTimeout=5) {
+        if (!function_exists("curl_init")){
+            die("ERROR: PHP CURL module is not installed.");
+        }
+        $ch = curl_init($url);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_TIMEOUT, $iTimeout);
+        $res = curl_exec($ch);
+        curl_close($ch);
+        return $res;
+    }
+
     /**
      * get all existing versions in archive and its usage
      * versions are array keys; where they are used is written in values
@@ -663,7 +680,7 @@ class project {
             $sJsonfile = $this->_getInfofile($sPhase, $sKey);
             $aTmp[$sKey] = array();
             if ($this->isActivePhase($sPhase)) {
-                $sJsonData = @file_get_contents($sJsonfile);
+                $sJsonData = $this->_httpGet($sJsonfile);
                 if ($sJsonData) {
                     $aJson = json_decode($sJsonData, true);
                     if (array_key_exists("version", $aJson)) {
@@ -943,6 +960,7 @@ class project {
         }
         return $this->_oVcs;
     }
+
     /**
      * get a flat array of all existing ssh keys
      * @return array
@@ -961,20 +979,19 @@ class project {
      * @param string $sPhase  phase
      * @return array
      */
-    private function _getDeploytimes($sPhase){
-        if (!$this->isActivePhase($sPhase)){
+    private function _getDeploytimes($sPhase) {
+        if (!$this->isActivePhase($sPhase)) {
             $sError = sprintf(t("class-project-warning-phase-not-active"), $sPhase);
             $this->_logaction($sError, __FUNCTION__, "error");
             return false;
         }
-        $aDeploytimes=array();
+        $aDeploytimes = array();
         if (array_key_exists("deploytimes", $this->_aConfig["phases"][$sPhase])) {
-            $aDeploytimes=$this->_aConfig["phases"][$sPhase]["deploytimes"];
+            $aDeploytimes = $this->_aConfig["phases"][$sPhase]["deploytimes"];
         }
-        if (array_key_exists("deploytimes", $this->_aPrjConfig["phases"][$sPhase])
-           && $this->_aPrjConfig["phases"][$sPhase]["deploytimes"]
+        if (array_key_exists("deploytimes", $this->_aPrjConfig["phases"][$sPhase]) && $this->_aPrjConfig["phases"][$sPhase]["deploytimes"]
         ) {
-            $aDeploytimes=array($this->_aPrjConfig["phases"][$sPhase]["deploytimes"]);
+            $aDeploytimes = array($this->_aPrjConfig["phases"][$sPhase]["deploytimes"]);
         }
         return $aDeploytimes;
     }
@@ -1541,8 +1558,8 @@ class project {
         // checks
         // --------------------------------------------------
         $sReturn.="<h3>" . t("class-project-deploy-label-checks") . "</h3>";
-        
-        $aDeploytimes=$this->_getDeploytimes($sPhase);
+
+        $aDeploytimes = $this->_getDeploytimes($sPhase);
         if (count($aDeploytimes)) {
             // check if the a deploy time is reached
             $sNow = date("D H:i:s");
@@ -2186,7 +2203,7 @@ class project {
                     $sReturn.="<pre>" . strip_tags($aRepodata["message"], '<br>') . "</pre>";
                 } else {
                     $sReturn .= $this->getBox("error", sprintf(t('class-project-error-no-repoaccess'), $aRepodata["error"]))
-                            . $this->renderLink("setup").'<br>';
+                            . $this->renderLink("setup") . '<br>';
                 }
 
                 break;
@@ -2463,9 +2480,9 @@ class project {
         }
 
         // generate datalist with exisating ssh keys for auth field
-        $sAuthListitems='';
-        foreach ($this->_getSshKeys() as $sKey){
-            $sAuthListitems.='<option value="'.$sKey.'">';
+        $sAuthListitems = '';
+        foreach ($this->_getSshKeys() as $sKey) {
+            $sAuthListitems.='<option value="' . $sKey . '">';
         }
         $aForms = array(
             'setup' => array(
@@ -2567,7 +2584,7 @@ class project {
                     ),
                     'input' . $i++ => array(
                         'type' => 'markup',
-                        'value' => '<datalist id="listauth">'.$sAuthListitems.'</datalist>',
+                        'value' => '<datalist id="listauth">' . $sAuthListitems . '</datalist>',
                     ),
                     'input' . $i++ => array(
                         'type' => 'markup',
diff --git a/public_html/deployment/classes/vcs.git.class.php b/public_html/deployment/classes/vcs.git.class.php
index a94b1242..c1cb451a 100644
--- a/public_html/deployment/classes/vcs.git.class.php
+++ b/public_html/deployment/classes/vcs.git.class.php
@@ -65,7 +65,8 @@ class vcs implements iVcs {
     public function setConfig($aRepoConfig = array()) {
 
         // checks
-        foreach (array("type", "url") as $key) {
+        // foreach (array("type", "url") as $key) {
+        foreach (array("type") as $key) {
             if (!array_key_exists($key, $aRepoConfig)) {
                 die("ERROR: key $key does not exist in config <pre>" . print_r($aRepoConfig, true) . "</pre>");
             }
@@ -145,6 +146,9 @@ class vcs implements iVcs {
     private function _fetchRemoteBranches() {
         $aReturn = array();
         $sGitCmd = 'export GIT_SSH="' . $this->_sWrapper . '" ; export PKEY="' . $this->_sKeyfile . '" ; ';
+        if (!$this->getUrl()){
+            return false;
+        }
         if (!file_exists($this->_sTempDir . ".git")) {
             $sGitCmd.='mkdir "' . $this->_sTempDir . '" && cd "' . $this->_sTempDir . '" && ';
             $sGitCmd.='git init >/dev/null && ';
@@ -204,6 +208,9 @@ class vcs implements iVcs {
     public function getRevision($sWorkDir = false) {
         $aReturn = array();
         $sGitCmd = 'export GIT_SSH="' . $this->_sWrapper . '" ; export PKEY="' . $this->_sKeyfile . '" ; ';
+        if (!$this->getUrl()){
+            return false;
+        }
 
         if ($sWorkDir) {
             $sGitCmd.='cd "' . $sWorkDir . '" && ';
@@ -251,6 +258,9 @@ class vcs implements iVcs {
         if (!$sWorkDir || !is_dir($sWorkDir)) {
             return false;
         }
+        if (!$this->getUrl()){
+            return false;
+        }
         $sBranchname=$this->_sCurrentBranch;
         $sBranchname=str_replace("origin/", "", $sBranchname);
         
-- 
GitLab