Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

Commit

Permalink
fix(knowledgebase): fix compatibility with MW1.30 (#6361)
Browse files Browse the repository at this point in the history
* fix(knowledgebase): fix compatibility with MW1.30

Apply new API rules and add compatibility downwards to 1.27

Resolves: CP7M-63

* refactor(knowledgebase): remove cookies after curl call
  • Loading branch information
victorvassilev authored and kduret committed Jun 20, 2018
1 parent abd70b4 commit ae3fd9f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 27 deletions.
3 changes: 1 addition & 2 deletions www/api/class/centreon_wiki.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ public function postCheckConnection()
public function postDeletePage()
{
$wikiApi = new WikiApi();
$result = $wikiApi->deletePage($this->arguments['title']);

$result = $wikiApi->deletePage($this->arguments['title']);
return array(
'result' => $result
);
Expand Down
95 changes: 70 additions & 25 deletions www/class/centreon-knowledge/wikiApi.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,12 @@ public function __construct()
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);
Expand Down Expand Up @@ -114,11 +116,20 @@ public function login()
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);
Expand All @@ -128,20 +139,28 @@ public function login()

// 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'];
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;

Expand Down Expand Up @@ -183,10 +202,6 @@ public function logout()

public function getMethodToken($method = 'delete', $title = '')
{
if (isset($this->tokens[$method])) {
return $this->tokens[$method];
}

if ($this->version >= 1.24) {
$postfields = array(
'action' => 'query',
Expand All @@ -211,10 +226,14 @@ public function getMethodToken($method = 'delete', $title = '')
}
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 {
Expand All @@ -227,7 +246,7 @@ public function getMethodToken($method = 'delete', $title = '')

public function movePage($oldTitle = '', $newTitle = '')
{
$this->login();
$login = $this->login();

$token = $this->getMethodToken('move', $oldTitle);

Expand All @@ -244,22 +263,26 @@ public function movePage($oldTitle = '', $newTitle = '')
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;
Expand Down Expand Up @@ -473,4 +496,26 @@ public function updateLinkForServiceTemplate($serviceName)
"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);
}
}

0 comments on commit ae3fd9f

Please sign in to comment.