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