]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - SOAP.php
Allow bold, italics or underlined for numbers
[SourceForge/phpwiki.git] / SOAP.php
1 <?php
2 /**
3  * SOAP server
4  * Taken from http://www.wlug.org.nz/archive/
5  * Please see http://phpwiki.sourceforge.net/phpwiki/PhpWiki.wdsl
6  * for the wdsl discussion.
7  *
8  * Todo:
9  * checkCredentials: set the $GLOBALS['request']->_user object for
10  *                   mayAccessPage
11  * enable native pecl extension (xml-rpc or soap)
12  * serverurl:
13  *   Installer helper which changes server url of the default PhpWiki.wdsl
14  *   Or do it dynamically in the soap class? No, the client must connect to us.
15  *
16  * @author: Reini Urban
17  */
18 define ("WIKI_SOAP", true);
19
20 include_once './index.php';
21 include_once 'lib/main.php';
22 if (!loadExtension('soap'))
23     require_once 'lib/nusoap/nusoap.php';
24
25 /*
26 // bypass auth and request loop for now.
27 // require_once('lib/prepend.php');
28 include_once 'index.php';
29
30 //require_once('lib/stdlib.php');
31 require_once 'lib/WikiDB.php';
32 require_once 'lib/config.php';
33 class WikiRequest extends Request {}
34 $request = new WikiRequest();
35 require_once 'lib/PagePerm.php';
36 require_once 'lib/WikiUserNew.php';
37 require_once 'lib/WikiGroup.php';
38 */
39
40 function checkCredentials(&$server, &$credentials, $access, $pagename)
41 {
42     // check the "Authorization: Basic '.base64_encode("$this->username:$this->password").'\r\n'" header
43     if (isset($server->header['Authorization'])) {
44         $line = base64_decode(str_replace("Basic ", "", trim($server->header['Authorization'])));
45         list($credentials['username'], $credentials['password']) = explode(':', $line);
46     } else {
47         if (!isset($_SERVER))
48             $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
49         // TODO: where in the header is the client IP
50         if (!isset($credentials['username'])) {
51             if (isset($_SERVER['REMOTE_ADDR']))
52                 $credentials['username'] = $_SERVER['REMOTE_ADDR'];
53             elseif (isset($GLOBALS['REMOTE_ADDR']))
54                 $credentials['username'] = $GLOBALS['REMOTE_ADDR']; else
55                 $credentials['username'] = $server->host;
56         }
57     }
58     if (!isset($credentials['password'])) $credentials['password'] = '';
59
60     global $request;
61     if (ENABLE_USER_NEW) {
62         $request->_user = WikiUser($credentials['username']);
63     } else {
64         $request->_user = new WikiUser($request, $credentials['username']);
65     }
66     $request->_user->AuthCheck(array('userid' => $credentials['username'],
67         'passwd' => $credentials['password']));
68     if (!mayAccessPage($access, $pagename))
69         $server->fault(401, '', "no permission");
70 }
71
72 $GLOBALS['SERVER_NAME'] = SERVER_URL;
73 $GLOBALS['SCRIPT_NAME'] = DATA_PATH . "/SOAP.php";
74 $url = SERVER_URL . DATA_PATH . "/SOAP.php";
75
76 // Local or external wdsl support is experimental.
77 // It works without also. Just the client has to
78 // know the wdsl definitions.
79 $server = new soap_server( /* 'PhpWiki.wdsl' */);
80 // Now change the server url to ours, because in the wdsl is the original PhpWiki address
81 //   <soap:address location="http://phpwiki.sourceforge.net/phpwiki/SOAP.php" />
82 //   <soap:operation soapAction="http://phpwiki.sourceforge.net/phpwiki/SOAP.php" />
83
84 $server->ports[$server->currentPort]['location'] = $url;
85 $server->bindings[$server->ports[$server->currentPort]['binding']]['endpoint'] = $url;
86 $server->soapaction = $url; // soap_transport_http
87
88 $actions = array('getPageContent', 'getPageRevision', 'getCurrentRevision',
89     'getPageMeta', 'doSavePage', 'getAllPagenames',
90     'getBackLinks', 'doTitleSearch', 'doFullTextSearch',
91     'getRecentChanges', 'listLinks', 'listPlugins',
92     'getPluginSynopsis', 'callPlugin', 'listRelations',
93     'linkSearch'
94 );
95 foreach ($actions as $action) {
96     $server->register($actions);
97     $server->operations[$actions]['soapaction'] = $url;
98 }
99
100 //todo: check and set credentials
101 // requiredAuthorityForPage($action);
102 // require 'edit' access
103 function doSavePage($pagename, $content, $credentials = false)
104 {
105     global $server;
106     checkCredentials($server, $credentials, 'edit', $pagename);
107     $dbi = WikiDB::open($GLOBALS['DBParams']);
108     $page = $dbi->getPage($pagename);
109     $current = $page->getCurrentRevision();
110     $meta = $current->_data;
111     $meta['summary'] = sprintf(_("SOAP Request %s", $credentials['username'])); // from user or IP ?
112     $version = $current->getVersion();
113     return $page->save($content, $version + 1, $meta);
114 }
115
116 // require 'view' access
117 function getPageContent($pagename, $credentials = false)
118 {
119     global $server;
120     checkCredentials($server, $credentials, 'view', $pagename);
121     $dbi = WikiDB::open($GLOBALS['DBParams']);
122     $page = $dbi->getPage($pagename);
123     $rev = $page->getCurrentRevision();
124     $text = $rev->getPackedContent();
125     return $text;
126 }
127
128 // require 'view' access
129 function getPageRevision($pagename, $revision, $credentials = false)
130 {
131     global $server;
132     checkCredentials($server, $credentials, 'view', $pagename);
133     $dbi = WikiDB::open($GLOBALS['DBParams']);
134     $page = $dbi->getPage($pagename);
135     $rev = $page->getCurrentRevision();
136     $text = $rev->getPackedContent();
137     return $text;
138 }
139
140 // require 'view' access
141 function getCurrentRevision($pagename, $credentials = false)
142 {
143     global $server;
144     checkCredentials($server, $credentials, 'view', $pagename);
145     if (!mayAccessPage('view', $pagename))
146         $server->fault(401, '', "no permission");
147     $dbi = WikiDB::open($GLOBALS['DBParams']);
148     $page = $dbi->getPage($pagename);
149     $rev = $page->getCurrentRevision();
150     $version = $current->getVersion();
151     return (double)$version;
152 }
153
154 // require 'change' or 'view' access ?
155 function getPageMeta($pagename, $credentials = false)
156 {
157     global $server;
158     checkCredentials($server, $credentials, 'view', $pagename);
159     $dbi = WikiDB::open($GLOBALS['DBParams']);
160     $page = $dbi->getPage($pagename);
161     $rev = $page->getCurrentRevision();
162     $meta = $rev->_data;
163     //todo: reformat the meta hash
164     return $meta;
165 }
166
167 // require 'view' access to AllPages
168 function getAllPagenames($credentials = false)
169 {
170     global $server;
171     checkCredentials($server, $credentials, 'view', _("AllPages"));
172     $dbi = WikiDB::open($GLOBALS['DBParams']);
173     $page_iter = $dbi->getAllPages();
174     $pages = array();
175     while ($page = $page_iter->next()) {
176         $pages[] = array('pagename' => $page->_pagename);
177     }
178     return $pages;
179 }
180
181 // require 'view' access
182 function getBacklinks($pagename, $credentials = false)
183 {
184     global $server;
185     checkCredentials($server, $credentials, 'view', $pagename);
186     $dbi = WikiDB::open($GLOBALS['DBParams']);
187     $backend = &$dbi->_backend;
188     $result = $backend->get_links($pagename);
189     $page_iter = new WikiDB_PageIterator($dbi, $result);
190     $pages = array();
191     while ($page = $page_iter->next()) {
192         $pages[] = array('pagename' => $page->getName());
193     }
194     return $pages;
195 }
196
197 // require 'view' access to TitleSearch
198 function doTitleSearch($s, $credentials = false)
199 {
200     global $server;
201     checkCredentials($server, $credentials, 'view', _("TitleSearch"));
202     $dbi = WikiDB::open($GLOBALS['DBParams']);
203     $query = new TextSearchQuery($s);
204     $page_iter = $dbi->titleSearch($query);
205     $pages = array();
206     while ($page = $page_iter->next()) {
207         $pages[] = array('pagename' => $page->getName());
208     }
209     return $pages;
210 }
211
212 // require 'view' access to FullTextSearch
213 function doFullTextSearch($s, $credentials = false)
214 {
215     global $server;
216     checkCredentials($server, $credentials, 'view', _("FullTextSearch"));
217     $dbi = WikiDB::open($GLOBALS['DBParams']);
218     $query = new TextSearchQuery($s);
219     $page_iter = $dbi->fullSearch($query);
220     $pages = array();
221     while ($page = $page_iter->next()) {
222         $pages[] = array('pagename' => $page->getName());
223     }
224     return $pages;
225 }
226
227 // require 'view' access to RecentChanges
228 function getRecentChanges($limit = false, $since = false, $include_minor = false, $credentials = false)
229 {
230     global $server;
231     checkCredentials($server, $credentials, 'view', _("RecentChanges"));
232     $dbi = WikiDB::open($GLOBALS['DBParams']);
233     $params = array('limit' => $limit, 'since' => $since,
234         'include_minor_revisions' => $include_minor);
235     $page_iter = $dbi->mostRecent($params);
236     $pages = array();
237     while ($page = $page_iter->next()) {
238         $pages[] = array('pagename' => $page->getName(),
239             'lastModified' => $page->get('mtime'),
240             'author' => $page->get('author'),
241             'summary' => $page->get('summary'), // added with 1.3.13
242             'version' => $page->getVersion()
243         );
244     }
245     return $pages;
246 }
247
248 // require 'view' access
249 function listLinks($pagename, $credentials = false)
250 {
251     global $server;
252     checkCredentials($server, $credentials, 'view', $pagename);
253     $dbi = WikiDB::open($GLOBALS['DBParams']);
254     $page = $dbi->getPage($pagename);
255     $linkiterator = $page->getPageLinks();
256     $links = array();
257     while ($currentpage = $linkiterator->next()) {
258         if ($currentpage->exists())
259             $links[] = array('pagename' => $currentpage->getName());
260     }
261     return $links;
262 }
263
264 function listPlugins($credentials = false)
265 {
266     global $server;
267     checkCredentials($server, $credentials, 'change', _("HomePage"));
268     $plugin_dir = 'lib/plugin';
269     if (defined('PHPWIKI_DIR'))
270         $plugin_dir = PHPWIKI_DIR . "/$plugin_dir";
271     $pd = new fileSet($plugin_dir, '*.php');
272     $plugins = $pd->getFiles();
273     unset($pd);
274     sort($plugins);
275     $RetArray = array();
276     if (!empty($plugins)) {
277         require_once 'lib/WikiPlugin.php';
278         $w = new WikiPluginLoader();
279         foreach ($plugins as $plugin) {
280             $pluginName = str_replace(".php", "", $plugin);
281             $p = $w->getPlugin($pluginName, false); // second arg?
282             // trap php files which aren't WikiPlugin~s: wikiplugin + wikiplugin_cached only
283             if (strtolower(substr(get_parent_class($p), 0, 10)) == 'wikiplugin') {
284                 $RetArray[] = $pluginName;
285             }
286         }
287     }
288     return $RetArray;
289 }
290
291 function getPluginSynopsis($pluginname, $credentials = false)
292 {
293     global $server;
294     checkCredentials($server, $credentials, 'change', "Help/" . $pluginname . "Plugin");
295     require_once 'lib/WikiPlugin.php';
296     $w = new WikiPluginLoader();
297     $synopsis = '';
298     $p = $w->getPlugin($pluginName, false); // second arg?
299     // trap php files which aren't WikiPlugin~s: wikiplugin + wikiplugin_cached only
300     if (strtolower(substr(get_parent_class($p), 0, 10)) == 'wikiplugin') {
301         $plugin_args = '';
302         $desc = $p->getArgumentsDescription();
303         $src = array("\n", '"', "'", '|', '[', ']', '\\');
304         $replace = array('%0A', '%22', '%27', '%7C', '%5B', '%5D', '%5C');
305         $desc = str_replace("<br />", ' ', $desc->asXML());
306         if ($desc)
307             $plugin_args = '\n' . str_replace($src, $replace, $desc);
308         $synopsis = "<?plugin " . $pluginName . $plugin_args . "?>"; // args?
309     }
310     return $synopsis;
311 }
312
313 // only plugins returning pagelists will return something useful. so omit the html output
314 function callPlugin($pluginname, $pluginargs, $credentials = false)
315 {
316     global $server;
317     checkCredentials($server, $credentials, 'change', "Help/" . $pluginname . "Plugin");
318
319     $basepage = '';
320     ;
321     require_once 'lib/WikiPlugin.php';
322     $w = new WikiPluginLoader();
323     $p = $w->getPlugin($pluginName, false); // second arg?
324     $pagelist = $p->run($dbi, $pluginargs, $request, $basepage);
325     $pages = array();
326     if (is_object($pagelist) and isa($pagelist, 'PageList')) {
327         foreach ($pagelist->pageNames() as $name)
328             $pages[] = array('pagename' => $name);
329     }
330     return $pages;
331 }
332
333 /**
334  * array listRelations([ Integer option = 1 ])
335  *
336  * Returns an array of all available relation names.
337  *   option: 1 relations only ( with 0 also )
338  *   option: 2 attributes only
339  *   option: 3 both, all names of relations and attributes
340  *   option: 4 unsorted, this might be added as bitvalue: 7 = 4+3. default: sorted
341  * For some semanticweb autofill methods.
342  *
343  * @author: Reini Urban
344  */
345 function listRelations($option = 1, $credentials = false)
346 {
347     global $server;
348     checkCredentials($server, $credentials, 'view', _("HomePage"));
349     $also_attributes = $option & 2;
350     $only_attributes = $option & 2 and !($option & 1);
351     $sorted = !($option & 4);
352     return $dbh->listRelations($also_attributes,
353         $only_attributes,
354         $sorted);
355 }
356
357 // some basic semantic search
358 function linkSearch($linktype, $search, $pages = "*", $relation = "*", $credentials = false)
359 {
360     global $server;
361     checkCredentials($server, $credentials, 'view', _("HomePage"));
362     $dbi = WikiDB::open($GLOBALS['DBParams']);
363     require_once 'lib/TextSearchQuery.php';
364     $pagequery = new TextSearchQuery($pages);
365     $linkquery = new TextSearchQuery($search);
366     if ($linktype == 'relation') {
367         $relquery = new TextSearchQuery($relation);
368         $links = $dbi->_backend->link_search($pagequery, $linkquery, $linktype, $relquery);
369     } elseif ($linktype == 'attribute') { // only numeric search withh attributes!
370         $relquery = new TextSearchQuery($relation);
371         require_once 'lib/SemanticWeb.php';
372         // search: "population > 1 million and area < 200 km^2" relation="*" pages="*"
373         $linkquery = new SemanticAttributeSearchQuery($search, $relation);
374         $links = $dbi->_backend->link_search($pagequery, $linkquery, $linktype, $relquery);
375     } else {
376         // we already do have forward and backlinks as SOAP
377         $links = $dbi->_backend->link_search($pagequery, $linkquery, $linktype);
378     }
379     return $links->asArray();
380 }
381
382 $server->service($GLOBALS['HTTP_RAW_POST_DATA']);
383
384 // Local Variables:
385 // mode: php
386 // tab-width: 8
387 // c-basic-offset: 4
388 // c-hanging-comment-ender-p: nil
389 // indent-tabs-mode: nil
390 // End: