From 67345895c888b781fb3ccca7438a5c4a50896543 Mon Sep 17 00:00:00 2001
From: Kevin Duret <kduret@centreon.com>
Date: Tue, 16 May 2017 11:25:16 +0200
Subject: [PATCH] fix wiki page deletion on new mediawiki version

---
 .../centreon-knowledge/wikiApi.class.php      | 36 ++++++++++++++-----
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/www/class/centreon-knowledge/wikiApi.class.php b/www/class/centreon-knowledge/wikiApi.class.php
index ef707b3c92..10ad514b60 100644
--- a/www/class/centreon-knowledge/wikiApi.class.php
+++ b/www/class/centreon-knowledge/wikiApi.class.php
@@ -49,6 +49,7 @@ class WikiApi
     private $curl;
     private $loggedIn;
     private $tokens;
+    private $cookies;
 
     /**
      * WikiApi constructor.
@@ -63,6 +64,7 @@ class WikiApi
         $this->password = $config['kb_wiki_password'];
         $this->curl = $this->getCurl();
         $this->version = $this->getWikiVersion();
+        $this->cookies = array();
     }
 
     private function getCurl()
@@ -103,26 +105,42 @@ class WikiApi
             return $this->loggedIn;
         }
 
+        curl_setopt($this->curl, CURLOPT_HEADER, true);
+
         // Get Connection Cookie/Token
         $postfields = array(
             'action' => 'login',
             'format' => 'json',
-            'lgname' => $this->username,
-            'lgpassword' => $this->password
+            'lgname' => $this->username
         );
 
         curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postfields);
         $result = curl_exec($this->curl);
-        $result = json_decode($result, true);
-        $token = $result['login']['lgtoken'];
+        $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
+        $header = substr($result, 0, $header_size);
+        $body = substr($result, $header_size);
+
+        // Get cookies
+        preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $header, $matches);
+        $this->cookies = array_merge($this->cookies, $matches[1]);
+        $cookies = implode('; ', $this->cookies);
+        curl_setopt($this->curl, CURLOPT_COOKIE, $cookies);
+
+        $result = json_decode($body, true);
+
+        $token = '';
+        if (isset($result['login']['lgtoken'])) {
+            $token = $result['login']['lgtoken'];
+        } elseif (isset($result['login']['token'])) {
+            $token = $result['login']['token'];
+        }
 
         // Launch Connection
+        $postfields['lgpassword'] = $this->password;
         $postfields['lgtoken'] = $token;
 
         curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postfields);
-        curl_setopt($this->curl, CURLOPT_HEADER, true);
         $result = curl_exec($this->curl);
-        curl_setopt($this->curl, CURLOPT_HEADER, false);
 
         $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE);
         $header = substr($result, 0, $header_size);
@@ -130,7 +148,8 @@ class WikiApi
 
         // Get cookies
         preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $header, $matches);
-        $cookies = implode('; ', $matches[1]);
+        $this->cookies = array_merge($this->cookies, $matches[1]);
+        $cookies = implode('; ', $this->cookies);
         curl_setopt($this->curl, CURLOPT_COOKIE, $cookies);
 
         $result = json_decode($body, true);
@@ -141,6 +160,8 @@ class WikiApi
             $this->loggedIn = true;
         }
 
+        curl_setopt($this->curl, CURLOPT_HEADER, false);
+
         return $this->loggedIn;
     }
 
@@ -182,7 +203,6 @@ class WikiApi
                 'titles' => $title
             );
         }
-
         curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postfields);
         $result = curl_exec($this->curl);
         $result = json_decode($result, true);
-- 
GitLab