test.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * test.php
  4. *
  5. * contains examples about how to use
  6. * class methods for Sf_Admin
  7. *
  8. * @author André Genrich <andre.genrich@thurdata.ch>
  9. * @version 1
  10. * @copyright Copyright (c) Thurdata GmbH 2020
  11. * @license GPL
  12. * @name test.php
  13. */
  14. /////////////
  15. // Require //
  16. /////////////
  17. require_once('config.php');
  18. require_once('cwp7/Admin.php');
  19. //////////
  20. // Args //
  21. //////////
  22. if(PHP_SAPI != 'cli')
  23. $args = $_GET;
  24. else
  25. $args = parse_args($argv);
  26. if(isset($args['action']))
  27. {
  28. $action = $args['action'];
  29. }
  30. else
  31. {
  32. echo 'No action, exiting' . PHP_EOL;
  33. exit (-1);
  34. }
  35. function parse_args($argv){
  36. array_shift($argv);
  37. $out = array();
  38. foreach ($argv as $arg){
  39. if (substr($arg,0,2) == '--'){
  40. $eqPos = strpos($arg,'=');
  41. if ($eqPos === false){
  42. $key = substr($arg,2);
  43. $out[$key] = isset($out[$key]) ? $out[$key] : true;
  44. } else {
  45. $key = substr($arg,2,$eqPos-2);
  46. $out[$key] = substr($arg,$eqPos+1);
  47. }
  48. } else if (substr($arg,0,1) == '-'){
  49. if (substr($arg,2,1) == '='){
  50. $key = substr($arg,1,1);
  51. $out[$key] = substr($arg,3);
  52. } else {
  53. $chars = str_split(substr($arg,1));
  54. foreach ($chars as $char){
  55. $key = $char;
  56. $out[$key] = isset($out[$key]) ? $out[$key] : true;
  57. }
  58. }
  59. } else {
  60. $out[] = $arg;
  61. }
  62. }
  63. return $out;
  64. }
  65. ///////////
  66. // Login //
  67. ///////////
  68. $cwp7 = new cwp7_Admin($cwp7URL, $cwp7AdminEmail, $cwp7AdminPassword);
  69. $r = $cwp7->login();
  70. if(isset($r['error_msg'])) {
  71. echo 'Error : cannot login to ' . $cwp7URL . ' :-(' . PHP_EOL;
  72. print_r($r);
  73. exit();
  74. }
  75. /////////////
  76. // Actions //
  77. /////////////
  78. // nop
  79. if($action == 'nop')
  80. {
  81. echo 'OK : login success, no further operation :-)' . PHP_EOL;
  82. exit;
  83. }
  84. // Get All Accounts
  85. if($action == 'gaa')
  86. {
  87. $r = $sf->getAllAccounts();
  88. if(isset($r['error_msg'])) {
  89. echo 'Error : could not fetch list of accounts for '. $seafileURL . ' :-(' . PHP_EOL;
  90. print_r($r);
  91. } else {
  92. echo 'OK : got a list of '. $r['total_count'] . ' accounts for ' . $seafileURL . ' :-)' . PHP_EOL;
  93. }
  94. }
  95. // Get Account Informations
  96. if($action == 'gai')
  97. {
  98. $r = $sf->getAccount($args['account_name']);
  99. if(isset($r['error_msg'])) {
  100. echo 'Error : could not fetch information of '. $args['account_name'] . ' :-(' . PHP_EOL;
  101. print_r($r);
  102. } else {
  103. echo 'OK : got the infos for account ' . $args['account_name'] . ' :-)' . PHP_EOL;
  104. }
  105. }
  106. // Create Account
  107. if($action == 'ca')
  108. {
  109. $attrs = array('email'=> $args['account_name'], 'password'=> $args['password']);
  110. $r = $sf->createAccount($attrs);
  111. if(isset($r['error_msg'])) {
  112. echo 'Error : cannot create account ' . $args['account_name'] . ' :-(' . PHP_EOL;
  113. print_r($r);
  114. } else {
  115. echo 'OK : account ' . $args['account_name'] . ' created :-)' . PHP_EOL;
  116. }
  117. }
  118. // Modify Account
  119. if($action == 'moa')
  120. {
  121. $attrs = array('email'=> $args['account_name'], 'quota_total' => $args['quota_total']);
  122. $r = $sf->modifyAccount($attrs);
  123. if(isset($r['error_msg'])) {
  124. echo 'Error : cannot modify account ' . $args['account_name'] . ' :-(' . PHP_EOL;
  125. print_r($r);
  126. } else {
  127. echo 'OK : modify account ' . $args['account_name'] . ' :-)' . PHP_EOL;
  128. print_r($r);
  129. }
  130. }
  131. // Migrate Account
  132. if($action == 'mia')
  133. {
  134. $r = $sf->migrateAccount($args['account_name'], $args['new_account_name']);
  135. if(isset($r['error_msg'])) {
  136. echo 'Error : account ' . $args['account_name'] . ' not migrated to ' . $args['new_account_name'] . ' :-(' . PHP_EOL;
  137. print_r($r);
  138. } else {
  139. echo 'OK : account ' . $args['account_name'] . ' migrated to ' . $args['new_account_name'] . ' :-)' . PHP_EOL;
  140. }
  141. }
  142. // Delete Account
  143. if($action == 'da')
  144. {
  145. $r = $sf->deleteAccount($args['account_name']);
  146. if(isset($r['error_msg'])) {
  147. echo 'Error : account ' . $args['account_name'] . ' not deleted :-(' . PHP_EOL;
  148. print_r($r);
  149. } else {
  150. echo 'OK : account ' . $args['account_name'] . ' deleted :-)' . PHP_EOL;
  151. }
  152. }