server_user.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 2 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_user.php
  19. * \brief File that is entry point to call Dolibarr WebServices
  20. * \version $Id: server_user.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.'/user/class/user.class.php';
  28. dol_syslog("Call User 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('WebServicesDolibarrUser',$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 other specific objects
  74. $server->wsdl->addComplexType(
  75. 'user',
  76. 'complexType',
  77. 'struct',
  78. 'all',
  79. '',
  80. array(
  81. 'element' => array('name'=>'element','type'=>'xsd:string'),
  82. 'id' => array('name'=>'id','type'=>'xsd:string'),
  83. 'lastname' => array('name'=>'lastname','type'=>'xsd:string'),
  84. 'firstname' => array('name'=>'firstname','type'=>'xsd:string'),
  85. 'note' => array('name'=>'note','type'=>'xsd:string'),
  86. 'email' => array('name'=>'email','type'=>'xsd:string'),
  87. 'signature' => array('name'=>'signature','type'=>'xsd:string'),
  88. 'office_phone' => array('name'=>'office_phone','type'=>'xsd:string'),
  89. 'office_fax' => array('name'=>'office_fax','type'=>'xsd:string'),
  90. 'user_mobile' => array('name'=>'user_mobile','type'=>'xsd:string'),
  91. 'admin' => array('name'=>'admin','type'=>'xsd:string'),
  92. 'login' => array('name'=>'login','type'=>'xsd:string'),
  93. 'entity' => array('name'=>'entity','type'=>'xsd:string'),
  94. 'pass_indatabase' => array('name'=>'pass_indatabase','type'=>'xsd:string'),
  95. 'pass_indatabase_crypted' => array('name'=>'pass_indatabase_crypted','type'=>'xsd:string'),
  96. 'datec' => array('name'=>'datec','type'=>'xsd:dateTime'),
  97. 'datem' => array('name'=>'datem','type'=>'xsd:dateTime'),
  98. 'societe_id' => array('name'=>'societe_id','type'=>'xsd:string'),
  99. 'fk_member' => array('name'=>'fk_member','type'=>'xsd:string'),
  100. 'datelastlogin' => array('name'=>'datelastlogin','type'=>'xsd:dateTime'),
  101. 'datepreviouslogin' => array('name'=>'datepreviouslogin','type'=>'xsd:dateTime'),
  102. 'statut' => array('name'=>'statut','type'=>'xsd:string'),
  103. 'photo' => array('name'=>'photo','type'=>'xsd:string'),
  104. 'lang' => array('name'=>'lang','type'=>'xsd:string'),
  105. 'entrepots' => array('name'=>'entrepots','type'=>'xsd:string'),
  106. //'rights' => array('name'=>'rights','type'=>'xsd:string'),
  107. 'canvas' => array('name'=>'canvas','type'=>'xsd:string')
  108. )
  109. );
  110. // Define other specific objects
  111. $server->wsdl->addComplexType(
  112. 'group',
  113. 'complexType',
  114. 'struct',
  115. 'all',
  116. '',
  117. array(
  118. 'nom' => array('name'=>'nom','type'=>'xsd:string'),
  119. 'id' => array('name'=>'id','type'=>'xsd:string'),
  120. 'datec' => array('name'=>'datec','type'=>'xsd:string'),
  121. 'nb' => array('name'=>'nb','type'=>'xsd:string')
  122. )
  123. );
  124. $server->wsdl->addComplexType(
  125. 'GroupsArray',
  126. 'complexType',
  127. 'array',
  128. '',
  129. 'SOAP-ENC:Array',
  130. array(),
  131. array(
  132. array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:group[]')
  133. ),
  134. 'tns:group'
  135. );
  136. // 5 styles: RPC/encoded, RPC/literal, Document/encoded (not WS-I compliant), Document/literal, Document/literal wrapped
  137. // Style merely dictates how to translate a WSDL binding to a SOAP message. Nothing more. You can use either style with any programming model.
  138. // http://www.ibm.com/developerworks/webservices/library/ws-whichwsdl/
  139. $styledoc='rpc'; // rpc/document (document is an extend into SOAP 1.0 to support unstructured messages)
  140. $styleuse='encoded'; // encoded/literal/literal wrapped
  141. // Better choice is document/literal wrapped but literal wrapped not supported by nusoap.
  142. // Register WSDL
  143. $server->register(
  144. 'getUser',
  145. // Entry values
  146. array('authentication'=>'tns:authentication','id'=>'xsd:string','ref'=>'xsd:string','ref_ext'=>'xsd:string'),
  147. // Exit values
  148. array('result'=>'tns:result','user'=>'tns:user'),
  149. $ns,
  150. $ns.'#getUser',
  151. $styledoc,
  152. $styleuse,
  153. 'WS to get user'
  154. );
  155. $server->register(
  156. 'getListOfGroups',
  157. // Entry values
  158. array('authentication'=>'tns:authentication'),
  159. // Exit values
  160. array('result'=>'tns:result','groups'=>'tns:GroupsArray'),
  161. $ns,
  162. $ns.'#getListOfGroups',
  163. $styledoc,
  164. $styleuse,
  165. 'WS to get list of groups'
  166. );
  167. /**
  168. * Get produt or service
  169. *
  170. * @param array $authentication Array of authentication information
  171. * @param int $id Id of object
  172. * @param string $ref Ref of object
  173. * @param ref_ext $ref_ext Ref external of object
  174. * @return mixed
  175. */
  176. function getUser($authentication,$id,$ref='',$ref_ext='')
  177. {
  178. global $db,$conf,$langs;
  179. dol_syslog("Function: getUser login=".$authentication['login']." id=".$id." ref=".$ref." ref_ext=".$ref_ext);
  180. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  181. // Init and check authentication
  182. $objectresp=array();
  183. $errorcode='';$errorlabel='';
  184. $error=0;
  185. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  186. // Check parameters
  187. if (! $error && (($id && $ref) || ($id && $ref_ext) || ($ref && $ref_ext)))
  188. {
  189. $error++;
  190. $errorcode='BAD_PARAMETERS'; $errorlabel="Parameter id, ref and ref_ext can't be both provided. You must choose one or other but not both.";
  191. }
  192. if (! $error)
  193. {
  194. $fuser->getrights();
  195. if ($fuser->rights->user->user->lire
  196. || ($fuser->rights->user->self->creer && $id && $id==$fuser->id)
  197. || ($fuser->rights->user->self->creer && $ref && $ref==$fuser->login)
  198. || ($fuser->rights->user->self->creer && $ref_ext && $ref_ext==$fuser->ref_ext))
  199. {
  200. $user=new User($db);
  201. $result=$user->fetch($id,$ref,$ref_ext);
  202. if ($result > 0)
  203. {
  204. // Create
  205. $objectresp = array(
  206. 'result'=>array('result_code'=>'OK', 'result_label'=>''),
  207. 'user'=>array(
  208. 'id' => $user->id,
  209. 'lastname' => $user->lastname,
  210. 'firstname' => $user->firstname,
  211. 'note' => $user->note,
  212. 'email' => $user->email,
  213. 'signature' => $user->signature,
  214. 'office_phone' => $user->office_phone,
  215. 'office_fax' => $user->office_fax,
  216. 'user_mobile' => $user->user_mobile,
  217. 'admin' => $user->admin,
  218. 'login' => $user->login,
  219. 'entity' => $user->entity,
  220. 'pass_indatabase' => $user->pass_indatabase,
  221. 'pass_indatabase_crypted' => $user->pass_indatabase_crypted,
  222. 'datec' => dol_print_date($user->datec,'dayhourrfc'),
  223. 'datem' => dol_print_date($user->datem,'dayhourrfc'),
  224. 'societe_id' => $user->societe_id,
  225. 'fk_member' => $user->fk_member,
  226. 'webcal_login' => $user->webcal_login,
  227. 'phenix_login' => $user->phenix_login,
  228. 'phenix_pass' => $user->phenix_pass,
  229. 'phenix_pass_crypted' => $user->phenix_pass_crypted,
  230. 'datelastlogin' => dol_print_date($user->datelastlogin,'dayhourrfc'),
  231. 'datepreviouslogin' => dol_print_date($user->datepreviouslogin,'dayhourrfc'),
  232. 'statut' => $user->statut,
  233. 'photo' => $user->photo,
  234. 'lang' => $user->lang,
  235. //'rights' => $user->rights,
  236. 'canvas' => $user->canvas
  237. )
  238. );
  239. }
  240. else
  241. {
  242. $error++;
  243. $errorcode='NOT_FOUND'; $errorlabel='Object not found for id='.$id.' nor ref='.$ref.' nor ref_ext='.$ref_ext;
  244. }
  245. }
  246. else
  247. {
  248. $error++;
  249. $errorcode='PERMISSION_DENIED'; $errorlabel='User does not have permission for this request';
  250. }
  251. }
  252. if ($error)
  253. {
  254. $objectresp = array('result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel));
  255. }
  256. return $objectresp;
  257. }
  258. /**
  259. * getListOfGroups
  260. *
  261. * @param array $authentication Array of authentication information
  262. * @return array Array result
  263. */
  264. function getListOfGroups($authentication)
  265. {
  266. global $db,$conf,$langs;
  267. $now=dol_now();
  268. dol_syslog("Function: getListOfGroups login=".$authentication['login']);
  269. if ($authentication['entity']) $conf->entity=$authentication['entity'];
  270. // Init and check authentication
  271. $objectresp=array();
  272. $arraygroups=array();
  273. $errorcode='';$errorlabel='';
  274. $error=0;
  275. $fuser=check_authentication($authentication,$error,$errorcode,$errorlabel);
  276. // Check parameters
  277. if (! $error)
  278. {
  279. $sql = "SELECT g.rowid, g.nom, g.entity, g.datec, COUNT(DISTINCT ugu.fk_user) as nb";
  280. $sql.= " FROM ".MAIN_DB_PREFIX."usergroup as g";
  281. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."usergroup_user as ugu ON ugu.fk_usergroup = g.rowid";
  282. if (! empty($conf->multicompany->enabled) && $conf->entity == 1 && ($conf->multicompany->transverse_mode || ($user->admin && ! $user->entity)))
  283. {
  284. $sql.= " WHERE g.entity IS NOT NULL";
  285. }
  286. else
  287. {
  288. $sql.= " WHERE g.entity IN (0,".$conf->entity.")";
  289. }
  290. $sql.= " GROUP BY g.rowid, g.nom, g.entity, g.datec";
  291. $resql=$db->query($sql);
  292. if ($resql)
  293. {
  294. $num=$db->num_rows($resql);
  295. $i=0;
  296. while ($i < $num)
  297. {
  298. $obj=$db->fetch_object($resql);
  299. $arraygroups[]=array('id'=>$obj->rowid,'nom'=>$obj->nom,'datec'=>$obj->datec,'nb'=>$obj->nb);
  300. $i++;
  301. }
  302. }
  303. else
  304. {
  305. $error++;
  306. $errorcode=$db->lasterrno();
  307. $errorlabel=$db->lasterror();
  308. }
  309. }
  310. if ($error)
  311. {
  312. $objectresp = array(
  313. 'result'=>array('result_code' => $errorcode, 'result_label' => $errorlabel),
  314. 'groups'=>$arraygroups
  315. );
  316. }
  317. else
  318. {
  319. $objectresp = array(
  320. 'result'=>array('result_code' => 'OK', 'result_label' => ''),
  321. 'groups'=>$arraygroups
  322. );
  323. }
  324. return $objectresp;
  325. }
  326. // Return the results.
  327. $server->service($HTTP_RAW_POST_DATA);
  328. ?>