server_category.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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'); // Do not check anti CSRF attack test
  24. }
  25. if (!defined('NOTOKENRENEWAL')) {
  26. define('NOTOKENRENEWAL', '1'); // Do not check anti POST attack test
  27. }
  28. if (!defined('NOREQUIREMENU')) {
  29. define('NOREQUIREMENU', '1'); // If there is no need to load and show top and left menu
  30. }
  31. if (!defined('NOREQUIREHTML')) {
  32. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  33. }
  34. if (!defined('NOREQUIREAJAX')) {
  35. define('NOREQUIREAJAX', '1'); // Do not load ajax.lib.php library
  36. }
  37. if (!defined("NOLOGIN")) {
  38. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  39. }
  40. if (!defined("NOSESSION")) {
  41. define("NOSESSION", '1');
  42. }
  43. require '../main.inc.php';
  44. require_once NUSOAP_PATH.'/nusoap.php'; // Include SOAP
  45. require_once DOL_DOCUMENT_ROOT.'/core/lib/ws.lib.php';
  46. require_once DOL_DOCUMENT_ROOT."/categories/class/categorie.class.php";
  47. dol_syslog("Call Dolibarr webservices interfaces");
  48. // Enable and test if module web services is enabled
  49. if (empty($conf->global->MAIN_MODULE_WEBSERVICES)) {
  50. $langs->load("admin");
  51. dol_syslog("Call Dolibarr webservices interfaces with module webservices disabled");
  52. print $langs->trans("WarningModuleNotActive", 'WebServices').'.<br><br>';
  53. print $langs->trans("ToActivateModule");
  54. exit;
  55. }
  56. // Create the soap Object
  57. $server = new nusoap_server();
  58. $server->soap_defencoding = 'UTF-8';
  59. $server->decode_utf8 = false;
  60. $ns = 'http://www.dolibarr.org/ns/';
  61. $server->configureWSDL('WebServicesDolibarrCategorie', $ns);
  62. $server->wsdl->schemaTargetNamespace = $ns;
  63. // Define WSDL content
  64. $server->wsdl->addComplexType(
  65. 'authentication',
  66. 'complexType',
  67. 'struct',
  68. 'all',
  69. '',
  70. array(
  71. 'dolibarrkey' => array('name'=>'dolibarrkey', 'type'=>'xsd:string'),
  72. 'sourceapplication' => array('name'=>'sourceapplication', 'type'=>'xsd:string'),
  73. 'login' => array('name'=>'login', 'type'=>'xsd:string'),
  74. 'password' => array('name'=>'password', 'type'=>'xsd:string'),
  75. 'entity' => array('name'=>'entity', 'type'=>'xsd:string'),
  76. )
  77. );
  78. /*
  79. * Une catégorie
  80. */
  81. $server->wsdl->addComplexType(
  82. 'categorie',
  83. 'complexType',
  84. 'struct',
  85. 'all',
  86. '',
  87. array(
  88. 'id' => array('name'=>'id', 'type'=>'xsd:string'),
  89. 'id_mere' => array('name'=>'id_mere', 'type'=>'xsd:string'),
  90. 'label' => array('name'=>'label', 'type'=>'xsd:string'),
  91. 'description' => array('name'=>'description', 'type'=>'xsd:string'),
  92. 'socid' => array('name'=>'socid', 'type'=>'xsd:string'),
  93. 'type' => array('name'=>'type', 'type'=>'xsd:string'),
  94. 'visible' => array('name'=>'visible', 'type'=>'xsd:string'),
  95. 'dir'=> array('name'=>'dir', 'type'=>'xsd:string'),
  96. 'photos' => array('name'=>'photos', 'type'=>'tns:PhotosArray'),
  97. 'filles' => array('name'=>'filles', 'type'=>'tns:FillesArray')
  98. )
  99. );
  100. /*
  101. * Les catégories filles, sous tableau dez la catégorie
  102. */
  103. $server->wsdl->addComplexType(
  104. 'FillesArray',
  105. 'complexType',
  106. 'array',
  107. '',
  108. 'SOAP-ENC:Array',
  109. array(),
  110. array(
  111. array('ref'=>'SOAP-ENC:arrayType', 'wsdl:arrayType'=>'tns:categorie[]')
  112. ),
  113. 'tns:categorie'
  114. );
  115. /*
  116. * Image of product
  117. */
  118. $server->wsdl->addComplexType(
  119. 'PhotosArray',
  120. 'complexType',
  121. 'array',
  122. 'sequence',
  123. '',
  124. array(
  125. 'image' => array(
  126. 'name' => 'image',
  127. 'type' => 'tns:image',
  128. 'minOccurs' => '0',
  129. 'maxOccurs' => 'unbounded'
  130. )
  131. )
  132. );
  133. /*
  134. * An image
  135. */
  136. $server->wsdl->addComplexType(
  137. 'image',
  138. 'complexType',
  139. 'struct',
  140. 'all',
  141. '',
  142. array(
  143. 'photo' => array('name'=>'photo', 'type'=>'xsd:string'),
  144. 'photo_vignette' => array('name'=>'photo_vignette', 'type'=>'xsd:string'),
  145. 'imgWidth' => array('name'=>'imgWidth', 'type'=>'xsd:string'),
  146. 'imgHeight' => array('name'=>'imgHeight', 'type'=>'xsd:string')
  147. )
  148. );
  149. /*
  150. * Retour
  151. */
  152. $server->wsdl->addComplexType(
  153. 'result',
  154. 'complexType',
  155. 'struct',
  156. 'all',
  157. '',
  158. array(
  159. 'result_code' => array('name'=>'result_code', 'type'=>'xsd:string'),
  160. 'result_label' => array('name'=>'result_label', 'type'=>'xsd:string'),
  161. )
  162. );
  163. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  164. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  165. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  166. $styledoc = 'rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  167. $styleuse = 'encoded'; // encoded/literal/literal wrapped
  168. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  169. // Register WSDL
  170. $server->register(
  171. 'getCategory',
  172. // Entry values
  173. array('authentication'=>'tns:authentication', 'id'=>'xsd:string'),
  174. // Exit values
  175. array('result'=>'tns:result', 'categorie'=>'tns:categorie'),
  176. $ns,
  177. $ns.'#getCategory',
  178. $styledoc,
  179. $styleuse,
  180. 'WS to get category'
  181. );
  182. /**
  183. * Get category infos and children
  184. *
  185. * @param array $authentication Array of authentication information
  186. * @param int $id Id of object
  187. * @return mixed
  188. */
  189. function getCategory($authentication, $id)
  190. {
  191. global $db, $conf, $langs;
  192. $nbmax = 10;
  193. dol_syslog("Function: getCategory login=".$authentication['login']." id=".$id);
  194. if ($authentication['entity']) {
  195. $conf->entity = $authentication['entity'];
  196. }
  197. $objectresp = array();
  198. $errorcode = ''; $errorlabel = '';
  199. $error = 0;
  200. $fuser = check_authentication($authentication, $error, $errorcode, $errorlabel);
  201. if (!$error && !$id) {
  202. $error++;
  203. $errorcode = 'BAD_PARAMETERS'; $errorlabel = "Parameter id must be provided.";
  204. }
  205. if (!$error) {
  206. $fuser->getrights();
  207. $nbmax = 10;
  208. if ($fuser->hasRight('categorie', 'lire')) {
  209. $categorie = new Categorie($db);
  210. $result = $categorie->fetch($id);
  211. if ($result > 0) {
  212. $dir = (!empty($conf->categorie->dir_output) ? $conf->categorie->dir_output : $conf->service->dir_output);
  213. $pdir = get_exdir($categorie->id, 2, 0, 0, $categorie, 'category').$categorie->id."/photos/";
  214. $dir = $dir.'/'.$pdir;
  215. $cat = array(
  216. 'id' => $categorie->id,
  217. 'id_mere' => $categorie->id_mere,
  218. 'label' => $categorie->label,
  219. 'description' => $categorie->description,
  220. 'socid' => $categorie->socid,
  221. //'visible'=>$categorie->visible,
  222. 'type' => $categorie->type,
  223. 'dir' => $pdir,
  224. 'photos' => $categorie->liste_photos($dir, $nbmax)
  225. );
  226. $cats = $categorie->get_filles();
  227. if (count($cats) > 0) {
  228. foreach ($cats as $fille) {
  229. $dir = (!empty($conf->categorie->dir_output) ? $conf->categorie->dir_output : $conf->service->dir_output);
  230. $pdir = get_exdir($fille->id, 2, 0, 0, $categorie, 'category').$fille->id."/photos/";
  231. $dir = $dir.'/'.$pdir;
  232. $cat['filles'][] = array(
  233. 'id'=>$fille->id,
  234. 'id_mere' => $categorie->id_mere,
  235. 'label'=>$fille->label,
  236. 'description'=>$fille->description,
  237. 'socid'=>$fille->socid,
  238. //'visible'=>$fille->visible,
  239. 'type'=>$fille->type,
  240. 'dir' => $pdir,
  241. 'photos' => $fille->liste_photos($dir, $nbmax)
  242. );
  243. }
  244. }
  245. // Create
  246. $objectresp = array(
  247. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  248. 'categorie'=> $cat
  249. );
  250. } else {
  251. $error++;
  252. $errorcode = 'NOT_FOUND'; $errorlabel = 'Object not found for id='.$id;
  253. }
  254. } else {
  255. $error++;
  256. $errorcode = 'PERMISSION_DENIED'; $errorlabel = 'User does not have permission for this request';
  257. }
  258. }
  259. if ($error) {
  260. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  261. }
  262. return $objectresp;
  263. }
  264. // Return the results.
  265. $server->service(file_get_contents("php://input"));