]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - SOAP.php
various
[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  * serverurl: 
12  *   Installer helper which changes server url of the default PhpWiki.wdsl
13  *   Or do it dynamically in the soap class? No, the client must connect to us.
14  *
15  * @author: Reini Urban
16  */
17 define ("WIKI_SOAP", "true");
18
19 include_once("my-index.php");
20 include_once("lib/main.php");
21 require_once('lib/nusoap/nusoap.php');
22
23 /*
24 // bypass auth and request loop for now.
25 // require_once('lib/prepend.php');
26 include_once("index.php");
27
28 //require_once('lib/stdlib.php');
29 require_once('lib/WikiDB.php');
30 require_once('lib/config.php');
31 class WikiRequest extends Request {}
32 $request = new WikiRequest();
33 require_once('lib/PagePerm.php');
34 require_once('lib/WikiUserNew.php');
35 require_once('lib/WikiGroup.php');
36 */
37
38 function checkCredentials(&$server, &$credentials, $access, $pagename) {
39     // check the "Authorization: Basic '.base64_encode("$this->username:$this->password").'\r\n'" header
40     if (isset($server->header['Authorization'])) {
41         $line = base64_decode(str_replace("Basic ","",trim($server->header['Authorization'])));
42         list($credentials['username'],$credentials['password']) = explode(':',$line);
43     } else {
44         // TODO: where in the header is the client IP
45         if (!isset($credentials['username'])) {
46             if (isset($_SERVER['REMOTE_ADDR']))
47                 $credentials['username'] = $_SERVER['REMOTE_ADDR'];
48             elseif (isset($GLOBALS['REMOTE_ADDR']))
49                 $credentials['username'] = $GLOBALS['REMOTE_ADDR'];
50             else 
51                 $credentials['username'] = $server->host;
52         }
53     }
54     if (!isset($credentials['password'])) $credentials['password'] = '';
55
56     //Fixme: set the GLOBALS['request']->_user object
57     if (!mayAccessPage ($access,$pagename))
58         $server->fault(401,'',"no permission");
59 }
60
61 $GLOBALS['SERVER_NAME'] = SERVER_URL;
62 $GLOBALS['SCRIPT_NAME'] = DATA_PATH . "/SOAP.php";
63 $url = SERVER_URL . DATA_PATH . "/SOAP.php";
64
65 // Local or external wdsl support is experimental. 
66 // It works without also. Just the client has to 
67 // know the wdsl definitions.
68 $server = new soap_server(/* 'PhpWiki.wdsl' */);
69 // Now change the server url to ours, because in the wdsl is the original PhpWiki address
70 //   <soap:address location="http://phpwiki.sourceforge.net/phpwiki/SOAP.php"/>
71 //   <soap:operation soapAction="http://phpwiki.sourceforge.net/phpwiki/SOAP.php" />
72 /*
73 $server->ports[$server->currentPort]['location'] = $url;
74 $server->bindings[ $server->ports[$server->currentPort]['binding'] ]['endpoint'] = $url;
75 $server->soapaction = $url; // soap_transport_http
76 */
77
78 $actions = array('getPageContent','getPageRevision','getCurrentRevision',
79                  'getPageMeta','doSavePage','getAllPagenames',
80                  'getBackLinks','doTitleSearch','doFullTextSearch');
81 foreach ($actions as $action) {
82     $server->register($actions);
83     $server->operations[$actions]['soapaction'] = $url;
84 }
85
86 //todo: check and set credentials
87 // requiredAuthorityForPage($action);
88 // require 'edit' access
89 function doSavePage($pagename,$content,$credentials=false) {
90     checkCredentials(&$server,&$credentials,'edit',$pagename);
91     $dbi = WikiDB::open($GLOBALS['DBParams']);
92     $page = $dbi->getPage($pagename);
93     $current = $page->getCurrentRevision();
94     $meta = $current->_data;
95     $meta['summary'] = sprintf(_("SOAP Request %s", $credentials['username'])); // from user or IP ?
96     $version = $current->getVersion();
97     return $page->save($content, $version + 1, $meta);
98 }
99
100 // require 'view' access
101 function getPageContent($pagename,$credentials=false) {
102     checkCredentials(&$server,&$credentials,'view',$pagename);
103     $dbi = WikiDB::open($GLOBALS['DBParams']);
104     $page = $dbi->getPage($pagename);
105     $rev = $page->getCurrentRevision();
106     $text = $rev->getPackedContent();
107     return $text;
108 }
109 // require 'view' access
110 function getPageRevision($pagename,$revision,$credentials=false) {
111     checkCredentials(&$server,&$credentials,'view',$pagename);
112     $dbi = WikiDB::open($GLOBALS['DBParams']);
113     $page = $dbi->getPage($pagename);
114     $rev = $page->getCurrentRevision();
115     $text = $rev->getPackedContent();
116     return $text;
117 }
118 // require 'view' access
119 function getCurrentRevision($pagename,$credentials=false) {
120     checkCredentials(&$server,&$credentials,'view',$pagename);
121     if (!mayAccessPage ('view',$pagename))
122         $server->fault(401,'',"no permission");
123     $dbi = WikiDB::open($GLOBALS['DBParams']);
124     $page = $dbi->getPage($pagename);
125     $rev = $page->getCurrentRevision();
126     $version = $current->getVersion();
127     return (double)$version;
128 }
129 // require 'change' or 'view' access ?
130 function getPageMeta($pagename,$credentials=false) {
131     checkCredentials(&$server,&$credentials,'view',$pagename);
132     $dbi = WikiDB::open($GLOBALS['DBParams']);
133     $page = $dbi->getPage($pagename);
134     $rev = $page->getCurrentRevision();
135     $meta = $rev->_data;
136     //todo: reformat the meta hash
137     return $meta;
138 }
139 // require 'view' access to AllPages
140 function getAllPagenames($credentials=false) {
141     checkCredentials(&$server,&$credentials,'view',_("AllPages"));
142     $dbi = WikiDB::open($GLOBALS['DBParams']);
143     $page_iter = $dbi->getAllPages();
144     $pages = array();
145     while ($page = $page_iter->next()) {
146         $pages[] = array('pagename' => $page->_pagename);
147     }
148     return $pages;
149 }
150 // require 'view' access
151 function getBacklinks($pagename,$credentials=false) {
152     checkCredentials(&$server,&$credentials,'view',$pagename);
153     $dbi = WikiDB::open($GLOBALS['DBParams']);
154     $backend = &$dbi->_backend;
155     $result =  $backend->get_links($pagename);
156     $page_iter = new WikiDB_PageIterator($dbi, $result);
157     $pages = array();
158     while ($page = $page_iter->next()) {
159         $pages[] = array('pagename' => $page->_pagename);
160     }
161     return $pages;
162 }
163 // require 'view' access to TitleSearch
164 function doTitleSearch($query,$credentials=false) {
165     checkCredentials(&$server,&$credentials,'view',_("TitleSearch"));
166     $dbi = WikiDB::open($GLOBALS['DBParams']);
167     $page_iter = $dbi->titleSearch($query);
168     $pages = array();
169     while ($page = $page_iter->next()) {
170         $pages[] = array('pagename' => $page->_pagename);
171     }
172     return $pages;
173 }
174 // require 'view' access to FullTextSearch
175 function doFullTextSearch($query,$credentials=false) {
176     checkCredentials(&$server,&$credentials,'view',_("FullTextSearch"));
177     $dbi = WikiDB::open($GLOBALS['DBParams']);
178     $page_iter = $dbi->fullSearch($query);
179     $pages = array();
180     while ($page = $page_iter->next()) {
181         $pages[] = array('pagename' => $page->_pagename);
182     }
183     return $pages;
184 }
185
186 $server->service($GLOBALS['HTTP_RAW_POST_DATA']);
187
188 // (c-file-style: "gnu")
189 // Local Variables:
190 // mode: php
191 // tab-width: 8
192 // c-basic-offset: 4
193 // c-hanging-comment-ender-p: nil
194 // indent-tabs-mode: nil
195 // End:   
196 ?>