Admin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * Sf_Admin
  4. *
  5. * @author André Genrich <andre.genrich@thurdata.ch>
  6. * @author Roland Käser <roland.keaser@thurdata.ch>
  7. * @version 0.9
  8. * @copyright Copyright (c) 2021, Thurdata
  9. * @example ../test.php
  10. */
  11. /**
  12. * Sf_Admin class documentation
  13. */
  14. /**
  15. * Sf_Admin is a class which allows to manage Seafile accounts via web-api/v2.1-admin
  16. *
  17. * You may create, modify, migrate, delete and get the attributes of a Seafile account using this class
  18. *
  19. * For the usage examples of all class methods check the source code of test.php
  20. */
  21. class Sf_Admin {
  22. private $loginSuccess;
  23. private $constructorSuccess;
  24. private $sfURL;
  25. private $sfConType;
  26. private $sfPort;
  27. private $sfSecure;
  28. private $sfAdminName;
  29. private $sfPassword;
  30. protected $sfToken;
  31. /**
  32. * Constructor
  33. * @param string $seafileURL Seafile URL (example: https://seafile.my.lan)
  34. * @param string $username admin/user account's e-mail
  35. * @param string $password admin/user account's password
  36. * @param string $secure optional false to force unsecure (default true)
  37. */
  38. function __construct($seafileURL, $username, $password, $secure=true) {
  39. if(!in_array('curl', get_loaded_extensions())) {
  40. $this->constructorSuccess = false;
  41. return array('error_msg' => 'Error: PHP curl extension not available');
  42. }
  43. if (empty($seafileURL) || empty($username) || empty($password)) {
  44. $this->constructorSuccess = false;
  45. return array('error_msg' => 'Error: Server login info missing, check server configuration');
  46. }
  47. if(preg_match('/^https/', $seafileURL)) {
  48. $this->sfConType = 'https://';
  49. if($secure) {
  50. $this->sfSecure = true;
  51. } else {
  52. $this->sfSecure = false;
  53. }
  54. }else {
  55. $this->sfConType = 'http://';
  56. $this->sfSecure = false;
  57. }
  58. $sfHostname = str_replace(array('http://', 'https://'), array('',''), $seafileURL);
  59. $sfHostname = explode(':', $sfHostname);
  60. if (gethostbyname($sfHostname[0]) == $sfHostname[0] && !filter_var($sfHostname[0], FILTER_VALIDATE_IP)) {
  61. $this->constructorSuccess = false;
  62. return array('error_msg' => 'Error: Cannot resolve ' . $sfHostname[0] . ', check server configuration');
  63. }
  64. $this->sfPort = ($sfHostname[1]) ? $sfHostname[1] : '8000';
  65. $this->sfURL = $this->sfConType . $sfHostname[0] . ':' . $this->sfPort;
  66. $this->sfAdminName = $username;
  67. $this->sfPassword = $password;
  68. $this->sfToken = null;
  69. $this->constructorSuccess = true;
  70. }
  71. public function constructorSuccess() {
  72. return $this->constructorSuccess;
  73. }
  74. /**
  75. * login
  76. *
  77. * fetch and set seafile lofin token
  78. * @return array error or true
  79. */
  80. public function login() {
  81. if (!$this->constructorSuccess()) {
  82. return array('error_msg' => 'Error: construct failed, something missing');
  83. }
  84. $sfClient = curl_init();
  85. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api2/auth-token/');
  86. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  87. curl_setopt($sfClient, CURLOPT_POST, true);
  88. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  89. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  90. curl_setopt($sfClient, CURLOPT_POSTFIELDS, array('username' => $this->sfAdminName, 'password' => $this->sfPassword));
  91. if(!$this->sfSecure) {
  92. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  93. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  94. }
  95. curl_setopt($sfClient, CURLOPT_HEADER, false);
  96. $authResponse = curl_exec($sfClient);
  97. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  98. curl_close($sfClient);
  99. unset($sfClient);
  100. return array('error_msg' => $authResponse);
  101. };
  102. curl_close($sfClient);
  103. unset($sfClient);
  104. $authResponseData = json_decode($authResponse, true);
  105. if (!$authResponseData['token']) {
  106. $this->loginSuccess = false;
  107. return array('error_msg' => $authResponse);
  108. } else {
  109. $this->sfToken = $authResponseData['token'];
  110. $this->loginSuccess = true;
  111. return true;
  112. }
  113. }
  114. /**
  115. * loginSuccess
  116. *
  117. * @return bool
  118. */
  119. public function loginSuccess() {
  120. return $this->loginSuccess;
  121. }
  122. /**
  123. * getAllAccounts
  124. *
  125. * @return array of seafile accounts array of informations or error message
  126. */
  127. public function getAllAccounts() {
  128. if (!$this->loginSuccess()) {
  129. return array('error_msg' => 'Error: not authenticated');
  130. }
  131. $sfClient = curl_init();
  132. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api/v2.1/admin/users/');
  133. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  134. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  135. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  136. if(!$this->sfSecure) {
  137. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  138. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  139. }
  140. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  141. $response = curl_exec($sfClient);
  142. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  143. curl_close($sfClient);
  144. return array('error_msg' => $response);
  145. };
  146. curl_close($sfClient);
  147. return json_decode($response, true);
  148. }
  149. /**
  150. * getAccount
  151. *
  152. * @param string $email login e-mail
  153. *
  154. * @return array of account informations or error message
  155. */
  156. public function getAccount($email) {
  157. if (!$this->loginSuccess()) {
  158. return array('error_msg' => 'Error: not authenticated');
  159. }
  160. $sfClient = curl_init();
  161. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api/v2.1/admin/users/' . $email . '/');
  162. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  163. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  164. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  165. if(!$this->sfSecure) {
  166. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  167. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  168. }
  169. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  170. $response = curl_exec($sfClient);
  171. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  172. curl_close($sfClient);
  173. return array('error_msg' => $response);
  174. };
  175. curl_close($sfClient);
  176. return json_decode($response, true);
  177. }
  178. /**
  179. * getAccount
  180. *
  181. * @param array $params avvount informations, email required.
  182. *
  183. * @see https://download.seafile.com/published/web-api/v2.1-admin/accounts.md#user-content-Add%20User
  184. *
  185. * @return array of account informations or error message
  186. */
  187. public function createAccount($params) {
  188. if (!$this->loginSuccess()) {
  189. return array('error_msg' => 'Error: not authenticated');
  190. }
  191. if(!isset($params['email'])) {
  192. return array('error_msg' => 'Error: missing parameter email');
  193. }
  194. $sfClient = curl_init();
  195. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api/v2.1/admin/users/');
  196. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  197. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  198. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  199. if(!$this->sfSecure) {
  200. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  201. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  202. }
  203. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  204. curl_setopt($sfClient, CURLOPT_POST, 1);
  205. curl_setopt($sfClient, CURLOPT_POSTFIELDS, $params);
  206. $response = curl_exec($sfClient);
  207. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  208. curl_close($sfClient);
  209. return array('error_msg' => $response);
  210. };
  211. curl_close($sfClient);
  212. return json_decode($response, true);
  213. }
  214. /**
  215. * modifyAccount
  216. *
  217. * @param array $params avvount informations, email required.
  218. *
  219. * @see https://download.seafile.com/published/web-api/v2.1-admin/accounts.md#user-content-Add%20User
  220. *
  221. * @return array of account informations or error message
  222. */
  223. public function modifyAccount($params) {
  224. if (!$this->loginSuccess()) {
  225. return array('error_msg' => 'Error: not authenticated');
  226. }
  227. if(!isset($params['email'])) {
  228. return array('error_msg' => 'Error: missing parameter email');
  229. }
  230. $account = $params['email'];
  231. unset($params['email']);
  232. $sfClient = curl_init();
  233. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api/v2.1/admin/users/' . $account . '/');
  234. curl_setopt($sfClient, CURLOPT_CUSTOMREQUEST, 'PUT');
  235. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  236. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  237. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  238. if(!$this->sfSecure) {
  239. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  240. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  241. }
  242. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  243. curl_setopt($sfClient, CURLOPT_POST, 1);
  244. curl_setopt($sfClient, CURLOPT_POSTFIELDS, $params);
  245. $response = curl_exec($sfClient);
  246. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  247. curl_close($sfClient);
  248. return array('error_msg' => $response);
  249. };
  250. curl_close($sfClient);
  251. return json_decode($response, true);
  252. }
  253. /**
  254. * deleteAccount
  255. *
  256. * @param string $email login e-mail
  257. *
  258. * @return array success => true or error message
  259. */
  260. public function deleteAccount($email)
  261. {
  262. if (!$this->loginSuccess()) {
  263. return array('error_msg' => 'Error: not authenticated');
  264. }
  265. if(!isset($email)) {
  266. return array('error_msg' => 'Error: missing parameter email');
  267. }
  268. $sfClient = curl_init();
  269. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api/v2.1/admin/users/' . $email . '/');
  270. curl_setopt($sfClient, CURLOPT_CUSTOMREQUEST, 'DELETE');
  271. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  272. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  273. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  274. if(!$this->sfSecure) {
  275. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  276. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  277. }
  278. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  279. $response = curl_exec($sfClient);
  280. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  281. curl_close($sfClient);
  282. return array('error_msg' => $response);
  283. };
  284. curl_close($sfClient);
  285. return json_decode($response, true);
  286. }
  287. /**
  288. * migrateAccount
  289. *
  290. * @param string $from source account login e-mail
  291. * @param string $to destination account login e-mail (must exist)
  292. *
  293. * @return array success => true or error message
  294. */
  295. public function migrateAccount($from, $to)
  296. {
  297. if (!$this->loginSuccess()) {
  298. return array('error_msg' => 'Error: not authenticated');
  299. }
  300. if(!isset($from)) {
  301. return array('error_msg' => 'Error: missing parameter from');
  302. }
  303. if(!isset($to)) {
  304. return array('error_msg' => 'Error: missing parameter to');
  305. }
  306. $postFields = array();
  307. $postFields['op'] = 'migrate';
  308. $postFields['to_user'] = $to;
  309. $sfClient = curl_init();
  310. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api2/accounts/' . $from . '/');
  311. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  312. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  313. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  314. if(!$this->sfSecure) {
  315. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  316. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  317. }
  318. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  319. curl_setopt($sfClient, CURLOPT_POST, 1);
  320. curl_setopt($sfClient, CURLOPT_POSTFIELDS, $postFields);
  321. $response = curl_exec($sfClient);
  322. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  323. curl_close($sfClient);
  324. return array('error_msg' => $response);
  325. };
  326. curl_close($sfClient);
  327. return json_decode($response, true);
  328. }
  329. }