diff --git a/www/api/class/centreon_wiki.class.php b/www/api/class/centreon_wiki.class.php index 8680e16ac3485a386c0a155b964cdcc56daf2baa..598d41626c33038067876295a234f5837b26eba1 100644 --- a/www/api/class/centreon_wiki.class.php +++ b/www/api/class/centreon_wiki.class.php @@ -74,8 +74,7 @@ class CentreonWiki extends CentreonWebService public function postDeletePage() { $wikiApi = new WikiApi(); - $result = $wikiApi->deletePage($this->arguments['title']); - + $result = $wikiApi->deletePage($this->arguments['title']); return array( 'result' => $result ); diff --git a/www/class/centreon-knowledge/wikiApi.class.php b/www/class/centreon-knowledge/wikiApi.class.php index 6754cc5f3757d9e3fede9a30dd6bb142e2755788..bb922ebd4397440687066939a8dc42bc789fd103 100644 --- a/www/class/centreon-knowledge/wikiApi.class.php +++ b/www/class/centreon-knowledge/wikiApi.class.php @@ -72,10 +72,12 @@ class WikiApi private function getCurl() { $curl = curl_init(); - + $cookiefile = tempnam("/tmp", "CURLCOOKIE"); curl_setopt($curl, CURLOPT_URL, $this->url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile); + curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile); if($this->noSslCertificate == 1){ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); @@ -114,11 +116,20 @@ class WikiApi curl_setopt($this->curl, CURLOPT_HEADER, true); // Get Connection Cookie/Token - $postfields = array( - 'action' => 'login', - 'format' => 'json', - 'lgname' => $this->username - ); + if ($this->version >= 1.27) { + $postfields = array( + 'action' => 'query', + 'meta' => 'tokens', + 'format' => 'json', + 'type' => 'login' + ); + } else { + $postfields = array( + 'action' => 'login', + 'format' => 'json', + 'lgname' => $this->username + ); + } curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postfields); $result = curl_exec($this->curl); @@ -128,6 +139,7 @@ class WikiApi // 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); @@ -135,13 +147,20 @@ class WikiApi $result = json_decode($body, true); $token = ''; - if (isset($result['login']['lgtoken'])) { - $token = $result['login']['lgtoken']; + if (isset($result['query']['tokens']['logintoken'])) { + $token = $result['query']['tokens']['logintoken']; } elseif (isset($result['login']['token'])) { $token = $result['login']['token']; } // Launch Connection + + $postfields = [ + 'action' => 'login', + 'lgname' => $this->username, + 'format' => 'json' + ]; + $postfields['lgpassword'] = $this->password; $postfields['lgtoken'] = $token; @@ -183,10 +202,6 @@ class WikiApi public function getMethodToken($method = 'delete', $title = '') { - if (isset($this->tokens[$method])) { - return $this->tokens[$method]; - } - if ($this->version >= 1.24) { $postfields = array( 'action' => 'query', @@ -211,10 +226,14 @@ class WikiApi } curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postfields); $result = curl_exec($this->curl); + $result = json_decode($result, true); if ($this->version >= 1.24) { $this->tokens[$method] = $result['query']['tokens']['csrftoken']; + if ($this->tokens[$method] == '+/'){ + $this->tokens[$method] = $this->getMethodToken('delete',$title); + } } elseif ($this->version >= 1.20) { $this->tokens[$method] = $result['tokens'][$method . 'token']; } else { @@ -227,7 +246,7 @@ class WikiApi public function movePage($oldTitle = '', $newTitle = '') { - $this->login(); + $login = $this->login(); $token = $this->getMethodToken('move', $oldTitle); @@ -244,22 +263,26 @@ class WikiApi return true; } + /** + * API Endpoint for deleting Knowledgebase Page + * @param string $title + * @return bool + */ public function deletePage($title = '') { - $this->login(); - - $token = $this->getMethodToken('delete', $title); - - if ($token) { - $postfields = array( - 'action' => 'delete', - 'title' => $title, - 'token' => $token - ); + $tries = 0; + $deleteResult = $this->deleteMWPage($title); + while ($tries < 5 && isset($deleteResult->error)){ + $deleteResult = $this->deleteMWPage($title); + $tries++; + } - curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postfields); - curl_exec($this->curl); + //remove cookies related to this action + unlink('/tmp/CURLCOOKIE*'); + if (isset($deleteResult->error)){ + return false; + } elseif (isset($deleteResult->delete)) { return true; } else { return false; @@ -473,4 +496,26 @@ class WikiApi "WHERE service_service_id = '" . $tuple['service_id'] . "' "; $this->db->query($queryUpdate); } + + /** + * make a call to mediawiki api to delete a page + * @param string $title + * @return array + */ + private function deleteMWPage($title='') + { + $this->login(); + + $token = $this->getMethodToken('delete', $title); + + $postfields = array( + 'action' => 'delete', + 'title' => $title, + 'token' => $token, + 'format' => 'json' + ); + curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postfields); + $result = curl_exec($this->curl); + return json_decode($result); + } }