Admin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. $responseCode = curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE);
  98. if($responseCode != 200) {
  99. curl_close($sfClient);
  100. unset($sfClient);
  101. return array('error_msg' => $authResponse, 'response_code' => $responseCode);
  102. };
  103. curl_close($sfClient);
  104. unset($sfClient);
  105. $authResponseData = json_decode($authResponse, true);
  106. if (!$authResponseData['token']) {
  107. $this->loginSuccess = false;
  108. error_log(print_r($authResponseData,true));
  109. return array('error_msg' => $authResponse);
  110. } else {
  111. $this->sfToken = $authResponseData['token'];
  112. $this->loginSuccess = true;
  113. return true;
  114. }
  115. }
  116. /**
  117. * loginSuccess
  118. *
  119. * @return bool
  120. */
  121. public function loginSuccess() {
  122. return $this->loginSuccess;
  123. }
  124. /**
  125. * getAllAccounts
  126. *
  127. * @return array of seafile accounts array of informations or error message
  128. */
  129. public function getAllAccounts() {
  130. if (!$this->loginSuccess()) {
  131. return array('error_msg' => 'Error: not authenticated');
  132. }
  133. $sfClient = curl_init();
  134. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api/v2.1/admin/users/');
  135. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  136. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  137. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  138. if(!$this->sfSecure) {
  139. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  140. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  141. }
  142. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  143. $response = curl_exec($sfClient);
  144. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  145. curl_close($sfClient);
  146. return array('error_msg' => $response);
  147. };
  148. curl_close($sfClient);
  149. return json_decode($response, true);
  150. }
  151. /**
  152. * getAccount
  153. *
  154. * @param string $email login e-mail
  155. *
  156. * @return array of account informations or error message
  157. */
  158. public function getAccount($email) {
  159. if (!$this->loginSuccess()) {
  160. return array('error_msg' => 'Error: not authenticated');
  161. }
  162. $sfClient = curl_init();
  163. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api/v2.1/admin/users/' . $email . '/');
  164. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  165. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  166. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  167. if(!$this->sfSecure) {
  168. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  169. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  170. }
  171. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  172. $response = curl_exec($sfClient);
  173. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  174. curl_close($sfClient);
  175. return array('error_msg' => $response);
  176. };
  177. curl_close($sfClient);
  178. return json_decode($response, true);
  179. }
  180. /**
  181. * getAccount
  182. *
  183. * @param array $params avvount informations, email required.
  184. *
  185. * @see https://download.seafile.com/published/web-api/v2.1-admin/accounts.md#user-content-Add%20User
  186. *
  187. * @return array of account informations or error message
  188. */
  189. public function createAccount($params) {
  190. if (!$this->loginSuccess()) {
  191. return array('error_msg' => 'Error: not authenticated');
  192. }
  193. if(!isset($params['email'])) {
  194. return array('error_msg' => 'Error: missing parameter email');
  195. }
  196. $sfClient = curl_init();
  197. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api/v2.1/admin/users/');
  198. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  199. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  200. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  201. if(!$this->sfSecure) {
  202. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  203. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  204. }
  205. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  206. curl_setopt($sfClient, CURLOPT_POST, 1);
  207. curl_setopt($sfClient, CURLOPT_POSTFIELDS, $params);
  208. $response = curl_exec($sfClient);
  209. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  210. curl_close($sfClient);
  211. return array('error_msg' => $response);
  212. };
  213. curl_close($sfClient);
  214. return json_decode($response, true);
  215. }
  216. /**
  217. * modifyAccount
  218. *
  219. * @param array $params avvount informations, email required.
  220. *
  221. * @see https://download.seafile.com/published/web-api/v2.1-admin/accounts.md#user-content-Add%20User
  222. *
  223. * @return array of account informations or error message
  224. */
  225. public function modifyAccount($params) {
  226. if (!$this->loginSuccess()) {
  227. return array('error_msg' => 'Error: not authenticated');
  228. }
  229. if(!isset($params['email'])) {
  230. return array('error_msg' => 'Error: missing parameter email');
  231. }
  232. $account = $params['email'];
  233. unset($params['email']);
  234. $sfClient = curl_init();
  235. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api/v2.1/admin/users/' . $account . '/');
  236. curl_setopt($sfClient, CURLOPT_CUSTOMREQUEST, 'PUT');
  237. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  238. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  239. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  240. if(!$this->sfSecure) {
  241. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  242. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  243. }
  244. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  245. curl_setopt($sfClient, CURLOPT_POST, 1);
  246. curl_setopt($sfClient, CURLOPT_POSTFIELDS, $params);
  247. $response = curl_exec($sfClient);
  248. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  249. curl_close($sfClient);
  250. return array('error_msg' => $response);
  251. };
  252. curl_close($sfClient);
  253. return json_decode($response, true);
  254. }
  255. /**
  256. * deleteAccount
  257. *
  258. * @param string $email login e-mail
  259. *
  260. * @return array success => true or error message
  261. */
  262. public function deleteAccount($email)
  263. {
  264. if (!$this->loginSuccess()) {
  265. return array('error_msg' => 'Error: not authenticated');
  266. }
  267. if(!isset($email)) {
  268. return array('error_msg' => 'Error: missing parameter email');
  269. }
  270. $sfClient = curl_init();
  271. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api/v2.1/admin/users/' . $email . '/');
  272. curl_setopt($sfClient, CURLOPT_CUSTOMREQUEST, 'DELETE');
  273. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  274. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  275. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  276. if(!$this->sfSecure) {
  277. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  278. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  279. }
  280. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  281. $response = curl_exec($sfClient);
  282. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  283. curl_close($sfClient);
  284. return array('error_msg' => $response);
  285. };
  286. curl_close($sfClient);
  287. return json_decode($response, true);
  288. }
  289. /**
  290. * migrateAccount
  291. *
  292. * @param string $from source account login e-mail
  293. * @param string $to destination account login e-mail (must exist)
  294. *
  295. * @return array success => true or error message
  296. */
  297. public function migrateAccount($from, $to)
  298. {
  299. if (!$this->loginSuccess()) {
  300. return array('error_msg' => 'Error: not authenticated');
  301. }
  302. if(!isset($from)) {
  303. return array('error_msg' => 'Error: missing parameter from');
  304. }
  305. if(!isset($to)) {
  306. return array('error_msg' => 'Error: missing parameter to');
  307. }
  308. $postFields = array();
  309. $postFields['op'] = 'migrate';
  310. $postFields['to_user'] = $to;
  311. $sfClient = curl_init();
  312. curl_setopt($sfClient, CURLOPT_URL, $this->sfURL . '/api2/accounts/' . $from . '/');
  313. curl_setopt($sfClient, CURLOPT_CONNECTTIMEOUT, '30');
  314. curl_setopt($sfClient, CURLOPT_FOLLOWLOCATION, true);
  315. curl_setopt($sfClient, CURLOPT_RETURNTRANSFER, true);
  316. if(!$this->sfSecure) {
  317. curl_setopt($sfClient, CURLOPT_SSL_VERIFYPEER, false);
  318. curl_setopt($sfClient, CURLOPT_SSL_VERIFYHOST, false);
  319. }
  320. curl_setopt($sfClient, CURLOPT_HTTPHEADER, array( 'Authorization: Token ' . $this->sfToken, 'Accept: application/json; charset=utf-8; indent=4'));
  321. curl_setopt($sfClient, CURLOPT_POST, 1);
  322. curl_setopt($sfClient, CURLOPT_POSTFIELDS, $postFields);
  323. $response = curl_exec($sfClient);
  324. if(curl_getinfo($sfClient, CURLINFO_RESPONSE_CODE) != 200) {
  325. curl_close($sfClient);
  326. return array('error_msg' => $response);
  327. };
  328. curl_close($sfClient);
  329. return json_decode($response, true);
  330. }
  331. }