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
102   if($retv == '1') {
103       echo "XCOM_LAG\n";
104       exit;
105   }
106   echo $retv;
107
108   echo "\n";
109
110}
111else {
112
113    echo  $helper->getLang('noconnection') . "\n";
114}
115
116function xcom_connect($url,$user,$pwd, $debug=false) {
117    $url = rtrim($url,'/') . '/lib/exe/xmlrpc.php';
118    $client = new IXR_Client($url);
119    $client->debug = $debug; // enable for debugging
120    $client->query('dokuwiki.login',$user,$pwd);
121    $ok = $client->getResponse();
122    if($ok) return $client;
123    return false;
124
125}
126
127function xcom_lock($page, $lock, $client) {
128 global $secs;
129
130 $locks = array('lock'=>array(), 'unlock'=>array()) ;
131 if($lock) {
132   $locks['lock'][] = $page;
133   echo "locking $page\n";
134 }
135 else {
136 echo "unlocking\n";
137       $locks['unlock'][] = $page;
138 }
139   $time_start = time();
140   while(!$client->query('dokuwiki.setLocks',$locks)) {
141      if((time() - $time_start ) > $secs ) {
142        break;
143       }
144      usleep(50);
145   }
146
147  $data = $client->getResponse();
148
149  if(in_array($page,$data['locked'])) {
150     return true;
151   }
152  if(in_array($page,$data['unlocked'])) {
153     return true;
154   }
155
156   return false;
157
158 }