1<?php
2define('DOKU_INC', realpath(dirname(__FILE__)) . '/../../../../');
3require_once(DOKU_INC.'inc/init.php');
4session_write_close();
5$helper =  plugin_load('helper', 'xcom');
6$credentials = json_decode($_REQUEST['credentials']);
7$url = rtrim ($credentials->url,'/') . '/';
8
9
10
11$params = json_decode($_REQUEST['params']);
12if($_REQUEST['debug'] == 'false'){
13    $debug = 0;
14}
15elseif ($_REQUEST['debug'] == 'true') {
16    $debug = 1;
17}
18
19$client = xcom_connect($url,$credentials->user,$credentials->pwd ,$debug);
20$secs = 15;
21$fn = $params[0] ;
22
23if($client)
24{
25
26   if($fn =='wiki.putPage' || $fn=='dokuwiki.appendPage') {
27        if(!xcom_lock($params[1], true, $client)) {
28           echo "Lock failed\n";
29           exit;
30        }
31    }
32
33   $array_types = array('dokuwiki.getPagelist','plugin.xcom.pageVersions','plugin.xcom.getPageInfo','wiki.getAllPages','wiki.getAttachmentInfo','wiki.getAttachments', 'wiki.getRecentChanges','wiki.getBackLinks', 'wiki.listLinks','dokuwiki.search','plugin.xcom.getMedia', 'plugin.xcom.listNamespaces');
34   $time_start = time();
35   $resp = "";
36
37   if($fn == 'plugin.xcom.listNamespaces') {
38       for($p=0; $p<count($params);$p++) {
39            if(is_array($params[$p])) {
40               $params[$p] = json_encode($params[$p]);
41            }
42       }
43   }
44
45    if($fn == 'dokuwiki.getPagelist') {
46       for($p=0; $p<count($params);$p++) {
47            if(is_array($params[$p])) {
48                    $elems = $params[$p];
49                    foreach($elems as $el) {
50                        if(strpos($el,':') !== false) {
51                            list($key,$val) = explode (':' , $el);
52                            $key = trim($key); $val = trim($val);
53                            $ar[$key] = ($val+=1);
54                            $params[$p] = $ar;
55                            break;
56                        }
57                    }
58                }
59            }
60       }
61
62
63    while(!($resp = call_user_func_array(array($client,"query"),$params))){
64        if((time() - $time_start ) > $secs ) {
65        break;
66        }
67       usleep(50);
68   }
69
70
71   $retv = $client->getResponse();
72       if($fn =='wiki.putPage' || $fn=='dokuwiki.appendPage') {
73         $resp = print_r($resp,1);
74         $_retv =print_r($retv,1);
75         $retv = "retv: $_retv resp: $resp";
76       }
77   if($fn =='wiki.putPage' || $fn=='dokuwiki.appendPage') {
78        xcom_lock($params[1], false, $client);
79   }
80
81   if(!$retv) {
82     $retv = $helper->getLang('timedout');
83   }
84   elseif(is_array($retv)) {
85      if(in_array($fn,$array_types) && !$retv['faultCode'] && !$retv['faultString']) {
86	   if($fn == 'wiki.getAttachmentInfo' && isset($params[1])) {
87	      $retv = array_merge(array('id' => $params[1]), $retv);
88	  }
89       $retv = json_encode($retv);
90       echo $retv;
91       exit;
92    }
93    else  {
94      $temp = print_r($retv,true);
95      $retv = rawurlencode($temp);
96      }
97   }
98   else {
99
100   }
101   echo $retv;
102
103   echo "\n";
104
105}
106else {
107
108    echo  $helper->getLang('noconnection') . "\n";
109}
110
111function xcom_connect($url,$user,$pwd, $debug=false) {
112    $url = rtrim($url,'/') . '/lib/exe/xmlrpc.php';
113    $client = new IXR_Client($url);
114    $client->debug = $debug; // enable for debugging
115    $client->query('dokuwiki.login',$user,$pwd);
116    $ok = $client->getResponse();
117    if($ok) return $client;
118    return false;
119
120}
121
122function xcom_lock($page, $lock, $client) {
123 global $secs;
124
125 $locks = array('lock'=>array(), 'unlock'=>array()) ;
126 if($lock) {
127   $locks['lock'][] = $page;
128   echo "locking $page\n";
129 }
130 else {
131 echo "unlocking\n";
132       $locks['unlock'][] = $page;
133 }
134   $time_start = time();
135   while(!$client->query('dokuwiki.setLocks',$locks)) {
136      if((time() - $time_start ) > $secs ) {
137        break;
138       }
139      usleep(50);
140   }
141
142  $data = $client->getResponse();
143
144  if(in_array($page,$data['locked'])) {
145     return true;
146   }
147  if(in_array($page,$data['unlocked'])) {
148     return true;
149   }
150
151   return false;
152
153 }