server_other.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/webservices/server_other.php
  19. * \brief File that is entry point to call Dolibarr WebServices
  20. */
  21. if (! defined("NOCSRFCHECK")) define("NOCSRFCHECK",'1');
  22. require '../master.inc.php';
  23. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php';
  28. dol_syslog("Call Dolibarr webservices interfaces");
  29. $langs->load("main");
  30. // Enable and test if module web services is enabled
  31. if (empty($conf->global->MAIN_MODULE_WEBSERVICES))
  32. {
  33. $langs->load("admin");
  34. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  35. print $langs->trans("WarningModuleNotActive",'WebServices').'.<br><br>';
  36. print $langs->trans("ToActivateModule");
  37. exit;
  38. }
  39. // Create the soap Object
  40. $server = new nusoap_server();
  41. $server->soap_defencoding='UTF-8';
  42. $server->decode_utf8=false;
  43. $ns='http://www.dolibarr.org/ns/';
  44. $server->configureWSDL('WebServicesDolibarrOther',$ns);
  45. $server->wsdl->schemaTargetNamespace=$ns;
  46. // Define WSDL Authentication object
  47. $server->wsdl->addComplexType(
  48. 'authentication',
  49. 'complexType',
  50. 'struct',
  51. 'all',
  52. '',
  53. array(
  54. 'dolibarrkey' => array('name'=>'dolibarrkey','type'=>'xsd:string'),
  55. 'sourceapplication' => array('name'=>'sourceapplication','type'=>'xsd:string'),
  56. 'login' => array('name'=>'login','type'=>'xsd:string'),
  57. 'password' => array('name'=>'password','type'=>'xsd:string'),
  58. 'entity' => array('name'=>'entity','type'=>'xsd:string'),
  59. )
  60. );
  61. // Define WSDL Return object
  62. $server->wsdl->addComplexType(
  63. 'result',
  64. 'complexType',
  65. 'struct',
  66. 'all',
  67. '',
  68. array(
  69. 'result_code' => array('name'=>'result_code','type'=>'xsd:string'),
  70. 'result_label' => array('name'=>'result_label','type'=>'xsd:string'),
  71. )
  72. );
  73. // Define WSDL Return object for document
  74. $server->wsdl->addComplexType(
  75. 'document',
  76. 'complexType',
  77. 'struct',
  78. 'all',
  79. '',
  80. array(
  81. 'filename' => array('name'=>'filename','type'=>'xsd:string'),
  82. 'mimetype' => array('name'=>'mimetype','type'=>'xsd:string'),
  83. 'content' => array('name'=>'content','type'=>'xsd:string'),
  84. 'length' => array('name'=>'length','type'=>'xsd:string')
  85. )
  86. );
  87. // Define other specific objects
  88. // None
  89. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  90. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  91. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  92. $styledoc='rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  93. $styleuse='encoded'; // encoded/literal/literal wrapped
  94. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  95. // Register WSDL
  96. $server->register(
  97. 'getVersions',
  98. // Entry values
  99. array('authentication'=>'tns:authentication'),
  100. // Exit values
  101. array('result'=>'tns:result','dolibarr'=>'xsd:string','os'=>'xsd:string','php'=>'xsd:string','webserver'=>'xsd:string'),
  102. $ns,
  103. $ns.'#getVersions',
  104. $styledoc,
  105. $styleuse,
  106. 'WS to get Versions'
  107. );
  108. // Register WSDL
  109. $server->register(
  110. 'getDocument',
  111. // Entry values
  112. array('authentication'=>'tns:authentication', 'modulepart'=>'xsd:string', 'file'=>'xsd:string' ),
  113. // Exit values
  114. array('result'=>'tns:result','document'=>'tns:document'),
  115. $ns,
  116. $ns.'#getDocument',
  117. $styledoc,
  118. $styleuse,
  119. 'WS to get document'
  120. );
  121. // Full methods code
  122. function getVersions($authentication)
  123. {
  124. global $db,$conf,$langs;
  125. dol_syslog("Function: getVersions login=".$authentication['login']);
  126. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  127. // Init and check authentication
  128. $objectresp=array();
  129. $errorcode='';$errorlabel='';
  130. $error=0;
  131. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  132. // Check parameters
  133. if (! $error)
  134. {
  135. $objectresp['result']=array('result_code'=>'OK', 'result_label'=>'');
  136. $objectresp['dolibarr']=version_dolibarr();
  137. $objectresp['os']=version_os();
  138. $objectresp['php']=version_php();
  139. $objectresp['webserver']=version_webserver();
  140. }
  141. if ($error)
  142. {
  143. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  144. }
  145. return $objectresp;
  146. }
  147. /**
  148. * Method to get a document by webservice
  149. *
  150. * @param array $authentication Array with permissions
  151. * @param string $modulepart Properties of document
  152. * @param string $file Relative path
  153. * @param string $refname Ref of object to check permission for external users (autodetect if not provided)
  154. * @return void
  155. */
  156. function getDocument($authentication, $modulepart, $file, $refname='')
  157. {
  158. global $db,$conf,$langs,$mysoc;
  159. dol_syslog("Function: getDocument login=".$authentication['login'].' - modulepart='.$modulepart.' - file='.$file);
  160. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  161. $objectresp=array();
  162. $errorcode='';$errorlabel='';
  163. $error=0;
  164. // Properties of doc
  165. $original_file = $file;
  166. $type=dol_mimetype($original_file);
  167. //$relativefilepath = $ref . "/";
  168. //$relativepath = $relativefilepath . $ref.'.pdf';
  169. $accessallowed=0;
  170. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  171. if ($fuser->societe_id) $socid=$fuser->societe_id;
  172. // Check parameters
  173. if (! $error && ( ! $file || ! $modulepart ) )
  174. {
  175. $error++;
  176. $errorcode='BAD_PARAMETERS'; $errorlabel="Parameter file and modulepart must be both provided.";
  177. }
  178. if (! $error)
  179. {
  180. $fuser->getrights();
  181. // Suppression de la chaine de caractere ../ dans $original_file
  182. $original_file = str_replace("../","/", $original_file);
  183. // find the subdirectory name as the reference
  184. if (empty($refname)) $refname=basename(dirname($original_file)."/");
  185. // Security check
  186. $check_access = dol_check_secure_access_document($modulepart,$original_file,$conf->entity,$fuser,$refname);
  187. $accessallowed = $check_access['accessallowed'];
  188. $sqlprotectagainstexternals = $check_access['sqlprotectagainstexternals'];
  189. $original_file = $check_access['original_file'];
  190. // Basic protection (against external users only)
  191. if ($fuser->societe_id > 0)
  192. {
  193. if ($sqlprotectagainstexternals)
  194. {
  195. $resql = $db->query($sqlprotectagainstexternals);
  196. if ($resql)
  197. {
  198. $num=$db->num_rows($resql);
  199. $i=0;
  200. while ($i < $num)
  201. {
  202. $obj = $db->fetch_object($resql);
  203. if ($fuser->societe_id != $obj->fk_soc)
  204. {
  205. $accessallowed=0;
  206. break;
  207. }
  208. $i++;
  209. }
  210. }
  211. }
  212. }
  213. // Security:
  214. // Limite acces si droits non corrects
  215. if (! $accessallowed)
  216. {
  217. $errorcode='NOT_PERMITTED';
  218. $errorlabel='Access not allowed';
  219. $error++;
  220. }
  221. // Security:
  222. // On interdit les remontees de repertoire ainsi que les pipe dans
  223. // les noms de fichiers.
  224. if (preg_match('/\.\./',$original_file) || preg_match('/[<>|]/',$original_file))
  225. {
  226. dol_syslog("Refused to deliver file ".$original_file);
  227. $errorcode='REFUSED';
  228. $errorlabel='';
  229. $error++;
  230. }
  231. clearstatcache();
  232. if(!$error)
  233. {
  234. if(file_exists($original_file))
  235. {
  236. dol_syslog("Function: getDocument $original_file $filename content-type=$type");
  237. $file=$fileparams['fullname'];
  238. $filename = basename($file);
  239. $f = fopen($original_file,'r');
  240. $content_file = fread($f,filesize($original_file));
  241. $objectret = array(
  242. 'filename' => basename($original_file),
  243. 'mimetype' => dol_mimetype($original_file),
  244. 'content' => base64_encode($content_file),
  245. 'length' => filesize($original_file)
  246. );
  247. // Create return object
  248. $objectresp = array(
  249. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  250. 'document'=>$objectret
  251. );
  252. }
  253. else
  254. {
  255. dol_syslog("File doesn't exist ".$original_file);
  256. $errorcode='NOT_FOUND';
  257. $errorlabel='';
  258. $error++;
  259. }
  260. }
  261. }
  262. if ($error)
  263. {
  264. $objectresp = array(
  265. 'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel)
  266. );
  267. }
  268. return $objectresp;
  269. }
  270. // Return the results.
  271. $server->service(file_get_contents("php://input"));