skeleton_webservice_server.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /* Copyright (C) 2006-2011 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_skeleton.php
  19. * \brief File that is entry point to call Dolibarr WebServices
  20. * \version $Id: server_skeleton.php,v 1.7 2010/12/19 11:49:37 eldy Exp $
  21. */
  22. // This is to make Dolibarr working with Plesk
  23. set_include_path($_SERVER['DOCUMENT_ROOT'].'/htdocs');
  24. require_once("../master.inc.php");
  25. require_once(NUSOAP_PATH.'/nusoap.php'); // Include SOAP
  26. require_once(DOL_DOCUMENT_ROOT."/core/lib/ws.lib.php");
  27. require_once(DOL_DOCUMENT_ROOT."/skeleton/class/skeleton.class.php");
  28. dol_syslog("Call Skeleton webservices interfaces");
  29. // Enable and test if module web services is enabled
  30. if (empty($conf->global->MAIN_MODULE_WEBSERVICES))
  31. {
  32. $langs->load("admin");
  33. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  34. print $langs->trans("WarningModuleNotActive",'WebServices').'.<br><br>';
  35. print $langs->trans("ToActivateModule");
  36. exit;
  37. }
  38. // Create the soap Object
  39. $server = new nusoap_server();
  40. $server->soap_defencoding='UTF-8';
  41. $server->decode_utf8=false;
  42. $ns='http://www.dolibarr.org/ns/';
  43. $server->configureWSDL('WebServicesDolibarrSkeleton',$ns);
  44. $server->wsdl->schemaTargetNamespace=$ns;
  45. // Define WSDL Authentication object
  46. $server->wsdl->addComplexType(
  47. 'authentication',
  48. 'complexType',
  49. 'struct',
  50. 'all',
  51. '',
  52. array(
  53. 'dolibarrkey' => array('name'=>'dolibarrkey','type'=>'xsd:string'),
  54. 'sourceapplication' => array('name'=>'sourceapplication','type'=>'xsd:string'),
  55. 'login' => array('name'=>'login','type'=>'xsd:string'),
  56. 'password' => array('name'=>'password','type'=>'xsd:string'),
  57. 'entity' => array('name'=>'entity','type'=>'xsd:string'),
  58. )
  59. );
  60. // Define WSDL Return object
  61. $server->wsdl->addComplexType(
  62. 'result',
  63. 'complexType',
  64. 'struct',
  65. 'all',
  66. '',
  67. array(
  68. 'result_code' => array('name'=>'result_code','type'=>'xsd:string'),
  69. 'result_label' => array('name'=>'result_label','type'=>'xsd:string'),
  70. )
  71. );
  72. // Define other specific objects
  73. $server->wsdl->addComplexType(
  74. 'skeleton',
  75. 'complexType',
  76. 'struct',
  77. 'all',
  78. '',
  79. array(
  80. 'prop1'=>'xxx',
  81. 'prop2'=>'xxx',
  82. //...
  83. )
  84. );
  85. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  86. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  87. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  88. $styledoc='rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  89. $styleuse='encoded'; // encoded/literal/literal wrapped
  90. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  91. // Register WSDL
  92. $server->register(
  93. 'getSkeleton',
  94. // Entry values
  95. array('authentication'=>'tns:authentication','id'=>'xsd:string','ref'=>'xsd:string','ref_ext'=>'xsd:string'),
  96. // Exit values
  97. array('result'=>'tns:result','skeleton'=>'tns:skeleton'),
  98. $ns,
  99. $ns.'#getSkeleton',
  100. $styledoc,
  101. $styleuse,
  102. 'WS to get skeleton'
  103. );
  104. // Register WSDL
  105. $server->register(
  106. 'createSkeleton',
  107. // Entry values
  108. array('authentication'=>'tns:authentication','skeleton'=>'tns:skeleton'),
  109. // Exit values
  110. array('result'=>'tns:result','id'=>'xsd:string'),
  111. $ns,
  112. $ns.'#createSkeleton',
  113. $styledoc,
  114. $styleuse,
  115. 'WS to create a skeleton'
  116. );
  117. /**
  118. * Get Skeleton
  119. *
  120. * @param array $authentication Array of authentication information
  121. * @param int $id Id of object
  122. * @param string $ref Ref of object
  123. * @param ref_ext $ref_ext Ref external of object
  124. * @return mixed
  125. */
  126. function getSkeleton($authentication,$id,$ref='',$ref_ext='')
  127. {
  128. global $db,$conf,$langs;
  129. dol_syslog("Function: getSkeleton login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
  130. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  131. // Init and check authentication
  132. $objectresp=array();
  133. $errorcode='';$errorlabel='';
  134. $error=0;
  135. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  136. // Check parameters
  137. if (! $error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext)))
  138. {
  139. $error++;
  140. $errorcode='BAD_PARAMETERS'; $errorlabel="Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
  141. }
  142. if (! $error)
  143. {
  144. $fuser->getrights();
  145. if ($fuser->rights->skeleton->read)
  146. {
  147. $skeleton=new Skeleton($db);
  148. $result=$skeleton->fetch($id,$ref,$ref_ext);
  149. if ($result > 0)
  150. {
  151. // Create
  152. $objectresp = array(
  153. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  154. 'skeleton'=>array(
  155. 'prop1'=>$skeleton->prop1,
  156. 'prop2'=>$skeleton->prop2,
  157. //...
  158. )
  159. );
  160. }
  161. else
  162. {
  163. $error++;
  164. $errorcode='NOT_FOUND'; $errorlabel='Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
  165. }
  166. }
  167. else
  168. {
  169. $error++;
  170. $errorcode='PERMISSION_DENIED'; $errorlabel='User does not have permission for this request';
  171. }
  172. }
  173. if ($error)
  174. {
  175. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  176. }
  177. return $objectresp;
  178. }
  179. /**
  180. * Create Skeleton
  181. *
  182. * @param array $authentication Array of authentication information
  183. * @param Skeleton $skeleton $skeleton
  184. * @return array Array result
  185. */
  186. function createSkeleton($authentication,$skeleton)
  187. {
  188. global $db,$conf,$langs;
  189. $now=dol_now();
  190. dol_syslog("Function: createSkeleton login=".$authentication['login']);
  191. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  192. // Init and check authentication
  193. $objectresp=array();
  194. $errorcode='';$errorlabel='';
  195. $error=0;
  196. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  197. // Check parameters
  198. if (! $error)
  199. {
  200. $newobject=new Skeleton($db);
  201. $newobject->prop1=$skeleton->prop1;
  202. $newobject->prop2=$skeleton->prop2;
  203. //...
  204. $db->begin();
  205. $result=$newobject->create($fuser);
  206. if ($result <= 0)
  207. {
  208. $error++;
  209. }
  210. if (! $error)
  211. {
  212. $db->commit();
  213. $objectresp=array('result'=>array('result_code'=>'OK', 'result_label'=>''),'id'=>$newobject->id,'ref'=>$newobject->ref);
  214. }
  215. else
  216. {
  217. $db->rollback();
  218. $error++;
  219. $errorcode='KO';
  220. $errorlabel=$newobject->error;
  221. }
  222. }
  223. if ($error)
  224. {
  225. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  226. }
  227. return $objectresp;
  228. }
  229. // Return the results.
  230. $server->service($HTTP_RAW_POST_DATA);