]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - SOAP.php
include [all] Include and file path should be devided with single space. File path...
[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     // check the "Authorization: Basic '.base64_encode("$this->username:$this->password").'\r\n'" header
42     if (isset($server->header['Authorization'])) {
43         $line = base64_decode(str_replace("Basic ","",trim($server->header['Authorization'])));
44         list($credentials['username'],$credentials['password']) = explode(':',$line);
45     } else {
46         if (!isset($_SERVER))
47             $_SERVER =& $GLOBALS['HTTP_SERVER_VARS'];
48         // TODO: where in the header is the client IP
49         if (!isset($credentials['username'])) {
50             if (isset($_SERVER['REMOTE_ADDR']))
51                 $credentials['username'] = $_SERVER['REMOTE_ADDR'];
52             elseif (isset($GLOBALS['REMOTE_ADDR']))
53                 $credentials['username'] = $GLOBALS['REMOTE_ADDR'];
54             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     global $server;
105     checkCredentials($server, $credentials,'edit',$pagename);
106     $dbi = WikiDB::open($GLOBALS['DBParams']);
107     $page = $dbi->getPage($pagename);
108     $current = $page->getCurrentRevision();
109     $meta = $current->_data;
110     $meta['summary'] = sprintf(_("SOAP Request %s", $credentials['username'])); // from user or IP ?
111     $version = $current->getVersion();
112     return $page->save($content, $version + 1, $meta);
113 }
114
115 // require 'view' access
116 function getPageContent($pagename,$credentials=false) {
117     global $server;
118     checkCredentials($server,$credentials,'view',$pagename);
119     $dbi = WikiDB::open($GLOBALS['DBParams']);
120     $page = $dbi->getPage($pagename);
121     $rev = $page->getCurrentRevision();
122     $text = $rev->getPackedContent();
123     return $text;
124 }
125 // require 'view' access
126 function getPageRevision($pagename,$revision,$credentials=false) {
127     global $server;
128     checkCredentials($server,$credentials,'view',$pagename);
129     $dbi = WikiDB::open($GLOBALS['DBParams']);
130     $page = $dbi->getPage($pagename);
131     $rev = $page->getCurrentRevision();
132     $text = $rev->getPackedContent();
133     return $text;
134 }
135 // require 'view' access
136 function getCurrentRevision($pagename,$credentials=false) {
137     global $server;
138     checkCredentials($server,$credentials,'view',$pagename);
139     if (!mayAccessPage ('view',$pagename))
140         $server->fault(401,'',"no permission");
141     $dbi = WikiDB::open($GLOBALS['DBParams']);
142     $page = $dbi->getPage($pagename);
143     $rev = $page->getCurrentRevision();
144     $version = $current->getVersion();
145     return (double)$version;
146 }
147 // require 'change' or 'view' access ?
148 function getPageMeta($pagename,$credentials=false) {
149     global $server;
150     checkCredentials($server,$credentials,'view',$pagename);
151     $dbi = WikiDB::open($GLOBALS['DBParams']);
152     $page = $dbi->getPage($pagename);
153     $rev = $page->getCurrentRevision();
154     $meta = $rev->_data;
155     //todo: reformat the meta hash
156     return $meta;
157 }
158 // require 'view' access to AllPages
159 function getAllPagenames($credentials=false) {
160     global $server;
161     checkCredentials($server,$credentials,'view',_("AllPages"));
162     $dbi = WikiDB::open($GLOBALS['DBParams']);
163     $page_iter = $dbi->getAllPages();
164     $pages = array();
165     while ($page = $page_iter->next()) {
166         $pages[] = array('pagename' => $page->_pagename);
167     }
168     return $pages;
169 }
170 // require 'view' access
171 function getBacklinks($pagename,$credentials=false) {
172     global $server;
173     checkCredentials($server,$credentials,'view',$pagename);
174     $dbi = WikiDB::open($GLOBALS['DBParams']);
175     $backend = &$dbi->_backend;
176     $result =  $backend->get_links($pagename);
177     $page_iter = new WikiDB_PageIterator($dbi, $result);
178     $pages = array();
179     while ($page = $page_iter->next()) {
180         $pages[] = array('pagename' => $page->getName());
181     }
182     return $pages;
183 }
184 // require 'view' access to TitleSearch
185 function doTitleSearch($s, $credentials=false) {
186     global $server;
187     checkCredentials($server,$credentials,'view',_("TitleSearch"));
188     $dbi = WikiDB::open($GLOBALS['DBParams']);
189     $query = new TextSearchQuery($s);
190     $page_iter = $dbi->titleSearch($query);
191     $pages = array();
192     while ($page = $page_iter->next()) {
193         $pages[] = array('pagename' => $page->getName());
194     }
195     return $pages;
196 }
197 // require 'view' access to FullTextSearch
198 function doFullTextSearch($s, $credentials=false) {
199     global $server;
200     checkCredentials($server,$credentials,'view',_("FullTextSearch"));
201     $dbi = WikiDB::open($GLOBALS['DBParams']);
202     $query = new TextSearchQuery($s);
203     $page_iter = $dbi->fullSearch($query);
204     $pages = array();
205     while ($page = $page_iter->next()) {
206         $pages[] = array('pagename' => $page->getName());
207     }
208     return $pages;
209 }
210
211 // require 'view' access to RecentChanges
212 function getRecentChanges($limit=false, $since=false, $include_minor=false, $credentials=false) {
213     global $server;
214     checkCredentials($server,$credentials,'view',_("RecentChanges"));
215     $dbi = WikiDB::open($GLOBALS['DBParams']);
216     $params = array('limit' => $limit, 'since' => $since,
217                     'include_minor_revisions' => $include_minor);
218     $page_iter = $dbi->mostRecent($params);
219     $pages = array();
220     while ($page = $page_iter->next()) {
221         $pages[] = array('pagename' => $page->getName(),
222                          'lastModified' => $page->get('mtime'),
223                          'author'  => $page->get('author'),
224                          'summary' => $page->get('summary'), // added with 1.3.13
225                          'version' => $page->getVersion()
226                          );
227     }
228     return $pages;
229 }
230 // require 'view' access
231 function listLinks($pagename, $credentials=false) {
232     global $server;
233     checkCredentials($server,$credentials,'view',$pagename);
234     $dbi = WikiDB::open($GLOBALS['DBParams']);
235     $page = $dbi->getPage($pagename);
236     $linkiterator = $page->getPageLinks();
237     $links = array();
238     while ($currentpage = $linkiterator->next()) {
239         if ($currentpage->exists())
240             $links[] = array('pagename' => $currentpage->getName());
241     }
242     return $links;
243 }
244 function listPlugins($credentials=false) {
245     global $server;
246     checkCredentials($server,$credentials,'change',_("HomePage"));
247     $plugin_dir = 'lib/plugin';
248     if (defined('PHPWIKI_DIR'))
249         $plugin_dir = PHPWIKI_DIR . "/$plugin_dir";
250     $pd = new fileSet($plugin_dir, '*.php');
251     $plugins = $pd->getFiles();
252     unset($pd);
253     sort($plugins);
254     $RetArray = array();
255     if (!empty($plugins)) {
256         require_once 'lib/WikiPlugin.php';
257         $w = new WikiPluginLoader;
258         foreach ($plugins as $plugin) {
259             $pluginName = str_replace(".php", "", $plugin);
260             $p = $w->getPlugin($pluginName, false); // second arg?
261             // trap php files which aren't WikiPlugin~s: wikiplugin + wikiplugin_cached only
262             if (strtolower(substr(get_parent_class($p), 0, 10)) == 'wikiplugin') {
263                 $RetArray[] = $pluginName;
264             }
265         }
266     }
267     return $RetArray;
268 }
269 function getPluginSynopsis($pluginname, $credentials=false) {
270     global $server;
271     checkCredentials($server,$credentials,'change',"Help/".$pluginname."Plugin");
272     require_once 'lib/WikiPlugin.php';
273     $w = new WikiPluginLoader;
274     $synopsis = '';
275     $p = $w->getPlugin($pluginName, false); // second arg?
276     // trap php files which aren't WikiPlugin~s: wikiplugin + wikiplugin_cached only
277     if (strtolower(substr(get_parent_class($p), 0, 10)) == 'wikiplugin') {
278         $plugin_args = '';
279         $desc = $p->getArgumentsDescription();
280         $src = array("\n",'"',"'",'|','[',']','\\');
281         $replace = array('%0A','%22','%27','%7C','%5B','%5D','%5C');
282         $desc = str_replace("<br />",' ',$desc->asXML());
283         if ($desc)
284             $plugin_args = '\n'.str_replace($src, $replace, $desc);
285         $synopsis = "<?plugin ".$pluginName.$plugin_args."?>"; // args?
286     }
287     return $synopsis;
288 }
289 // only plugins returning pagelists will return something useful. so omit the html output
290 function callPlugin($pluginname, $pluginargs, $credentials=false) {
291     global $server;
292     checkCredentials($server,$credentials,'change',"Help/".$pluginname."Plugin");
293
294     $basepage = '';;
295     require_once 'lib/WikiPlugin.php';
296     $w = new WikiPluginLoader;
297     $p = $w->getPlugin($pluginName, false); // second arg?
298     $pagelist = $p->run($dbi, $pluginargs, $request, $basepage);
299     $pages = array();
300     if (is_object($pagelist) and isa($pagelist, 'PageList')) {
301         foreach ($pagelist->pageNames() as $name)
302             $pages[] = array('pagename' => $name);
303     }
304     return $pages;
305 }
306 /**
307  * array listRelations([ Integer option = 1 ])
308  *
309  * Returns an array of all available relation names.
310  *   option: 1 relations only ( with 0 also )
311  *   option: 2 attributes only
312  *   option: 3 both, all names of relations and attributes
313  *   option: 4 unsorted, this might be added as bitvalue: 7 = 4+3. default: sorted
314  * For some semanticweb autofill methods.
315  *
316  * @author: Reini Urban
317  */
318 function listRelations($option = 1, $credentials=false) {
319     global $server;
320     checkCredentials($server,$credentials,'view',_("HomePage"));
321     $also_attributes = $option & 2;
322     $only_attributes = $option & 2 and !($option & 1);
323     $sorted = !($option & 4);
324     return $dbh->listRelations($also_attributes,
325                                $only_attributes,
326                                $sorted);
327 }
328 // some basic semantic search
329 function linkSearch($linktype, $search, $pages="*", $relation="*", $credentials=false) {
330     global $server;
331     checkCredentials($server,$credentials,'view',_("HomePage"));
332     $dbi = WikiDB::open($GLOBALS['DBParams']);
333     require_once 'lib/TextSearchQuery.php';
334     $pagequery = new TextSearchQuery($pages);
335     $linkquery = new TextSearchQuery($search);
336     if ($linktype == 'relation') {
337         $relquery = new TextSearchQuery($relation);
338         $links = $dbi->_backend->link_search($pagequery, $linkquery, $linktype, $relquery);
339     } elseif ($linktype == 'attribute') { // only numeric search withh attributes!
340         $relquery = new TextSearchQuery($relation);
341         require_once 'lib/SemanticWeb.php';
342         // search: "population > 1 million and area < 200 km^2" relation="*" pages="*"
343         $linkquery = new SemanticAttributeSearchQuery($search, $relation);
344         $links = $dbi->_backend->link_search($pagequery, $linkquery, $linktype, $relquery);
345     } else {
346         // we already do have forward and backlinks as SOAP
347         $links = $dbi->_backend->link_search($pagequery, $linkquery, $linktype);
348     }
349     return $links->asArray();
350 }
351
352 $server->service($GLOBALS['HTTP_RAW_POST_DATA']);
353
354 // Local Variables:
355 // mode: php
356 // tab-width: 8
357 // c-basic-offset: 4
358 // c-hanging-comment-ender-p: nil
359 // indent-tabs-mode: nil
360 // End: