server_category.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /* Copyright (C) 2006-2016 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2012 JF FERRY <jfefe@aternatik.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/webservices/server_category.php
  20. * \brief File that is entry point to call Dolibarr WebServices
  21. */
  22. if (!defined("NOCSRFCHECK")) {
  23. define("NOCSRFCHECK", '1');
  24. }
  25. require "../master.inc.php";
  26. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
  28. require_once DOL_DOCUMENT_ROOT."/categories/class/categorie.class.php";
  29. dol_syslog("Call Dolibarr webservices interfaces");
  30. // Enable and test if module web services is enabled
  31. if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
  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('WebServicesDolibarrCategorie', $ns);
  44. $server->wsdl->schemaTargetNamespace = $ns;
  45. // Define WSDL content
  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. /*
  61. * Une catégorie
  62. */
  63. $server->wsdl->addComplexType(
  64. 'categorie',
  65. 'complexType',
  66. 'struct',
  67. 'all',
  68. '',
  69. array(
  70. 'id' => array('name'=>'id', 'type'=>'xsd:string'),
  71. 'id_mere' => array('name'=>'id_mere', 'type'=>'xsd:string'),
  72. 'label' => array('name'=>'label', 'type'=>'xsd:string'),
  73. 'description' => array('name'=>'description', 'type'=>'xsd:string'),
  74. 'socid' => array('name'=>'socid', 'type'=>'xsd:string'),
  75. 'type' => array('name'=>'type', 'type'=>'xsd:string'),
  76. 'visible' => array('name'=>'visible', 'type'=>'xsd:string'),
  77. 'dir'=> array('name'=>'dir', 'type'=>'xsd:string'),
  78. 'photos' => array('name'=>'photos', 'type'=>'tns:PhotosArray'),
  79. 'filles' => array('name'=>'filles', 'type'=>'tns:FillesArray')
  80. )
  81. );
  82. /*
  83. * Les catégories filles, sous tableau dez la catégorie
  84. */
  85. $server->wsdl->addComplexType(
  86. 'FillesArray',
  87. 'complexType',
  88. 'array',
  89. '',
  90. 'SOAP-ENC:Array',
  91. array(),
  92. array(
  93. array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:categorie[]')
  94. ),
  95. 'tns:categorie'
  96. );
  97. /*
  98. * Image of product
  99. */
  100. $server->wsdl->addComplexType(
  101. 'PhotosArray',
  102. 'complexType',
  103. 'array',
  104. 'sequence',
  105. '',
  106. array(
  107. 'image' => array(
  108. 'name' => 'image',
  109. 'type' => 'tns:image',
  110. 'minOccurs' => '0',
  111. 'maxOccurs' => 'unbounded'
  112. )
  113. )
  114. );
  115. /*
  116. * An image
  117. */
  118. $server->wsdl->addComplexType(
  119. 'image',
  120. 'complexType',
  121. 'struct',
  122. 'all',
  123. '',
  124. array(
  125. 'photo' => array('name'=>'photo', 'type'=>'xsd:string'),
  126. 'photo_vignette' => array('name'=>'photo_vignette', 'type'=>'xsd:string'),
  127. 'imgWidth' => array('name'=>'imgWidth', 'type'=>'xsd:string'),
  128. 'imgHeight' => array('name'=>'imgHeight', 'type'=>'xsd:string')
  129. )
  130. );
  131. /*
  132. * Retour
  133. */
  134. $server->wsdl->addComplexType(
  135. 'result',
  136. 'complexType',
  137. 'struct',
  138. 'all',
  139. '',
  140. array(
  141. 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
  142. 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
  143. )
  144. );
  145. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  146. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  147. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  148. $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  149. $styleuse = 'encoded'; // encoded/literal/literal wrapped
  150. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  151. // Register WSDL
  152. $server->register(
  153. 'getCategory',
  154. // Entry values
  155. array('authentication'=>'tns:authentication', 'id'=>'xsd:string'),
  156. // Exit values
  157. array('result'=>'tns:result', 'categorie'=>'tns:categorie'),
  158. $ns,
  159. $ns.'#getCategory',
  160. $styledoc,
  161. $styleuse,
  162. 'WS to get category'
  163. );
  164. /**
  165. * Get category infos and children
  166. *
  167. * @param array $authentication Array of authentication information
  168. * @param int $id Id of object
  169. * @return mixed
  170. */
  171. function getCategory($authentication, $id)
  172. {
  173. global $db, $conf, $langs;
  174. $nbmax = 10;
  175. dol_syslog("Function: getCategory login=".$authentication['login']." id=".$id);
  176. if ($authentication['entity']) {
  177. $conf->entity = $authentication['entity'];
  178. }
  179. $objectresp = array();
  180. $errorcode = ''; $errorlabel = '';
  181. $error = 0;
  182. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  183. if (!$error && !$id) {
  184. $error++;
  185. $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id must be provided.";
  186. }
  187. if (!$error) {
  188. $fuser->getrights();
  189. $nbmax = 10;
  190. if ($fuser->rights->categorie->lire) {
  191. $categorie = new Categorie($db);
  192. $result = $categorie->fetch($id);
  193. if ($result > 0) {
  194. $dir = (!empty($conf->categorie->dir_output) ? $conf->categorie->dir_output : $conf->service->dir_output);
  195. $pdir = get_exdir($categorie->id, 2, 0, 0, $categorie, 'category').$categorie->id."/photos/";
  196. $dir = $dir.'/'.$pdir;
  197. $cat = array(
  198. 'id' => $categorie->id,
  199. 'id_mere' => $categorie->id_mere,
  200. 'label' => $categorie->label,
  201. 'description' => $categorie->description,
  202. 'socid' => $categorie->socid,
  203. //'visible'=>$categorie->visible,
  204. 'type' => $categorie->type,
  205. 'dir' => $pdir,
  206. 'photos' => $categorie->liste_photos($dir, $nbmax)
  207. );
  208. $cats = $categorie->get_filles();
  209. if (count($cats) > 0) {
  210. foreach ($cats as $fille) {
  211. $dir = (!empty($conf->categorie->dir_output) ? $conf->categorie->dir_output : $conf->service->dir_output);
  212. $pdir = get_exdir($fille->id, 2, 0, 0, $categorie, 'category').$fille->id."/photos/";
  213. $dir = $dir.'/'.$pdir;
  214. $cat['filles'][] = array(
  215. 'id'=>$fille->id,
  216. 'id_mere' => $categorie->id_mere,
  217. 'label'=>$fille->label,
  218. 'description'=>$fille->description,
  219. 'socid'=>$fille->socid,
  220. //'visible'=>$fille->visible,
  221. 'type'=>$fille->type,
  222. 'dir' => $pdir,
  223. 'photos' => $fille->liste_photos($dir, $nbmax)
  224. );
  225. }
  226. }
  227. // Create
  228. $objectresp = array(
  229. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  230. 'categorie'=> $cat
  231. );
  232. } else {
  233. $error++;
  234. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id;
  235. }
  236. } else {
  237. $error++;
  238. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  239. }
  240. }
  241. if ($error) {
  242. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  243. }
  244. return $objectresp;
  245. }
  246. // Return the results.
  247. $server->service(file_get_contents("php://input"));