PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/gapi/startwork.php

http://stuffpack.googlecode.com/
PHP | 306 lines | 273 code | 12 blank | 21 comment | 14 complexity | 49931d108c361d382c52db09b4ff9814 MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. This script is designed for corporate or educational users
  5. of google apps. It lets you add, remove, suspend, or view
  6. info about users from a command line script. It is best
  7. used in conjuction with existing create-a-user scripts for
  8. other applications.
  9. */
  10. error_reporting(E_ALL ^ (E_NOTICE|E_WARNING));
  11. define('COLOR_BOLD',1);
  12. define('COLOR_RED', 31);
  13. define('COLOR_GREEN', 32);
  14. define('COLOR_BROWN', 33);
  15. // All of these are set in config.php (and thats it)
  16. // $config['user password domain']
  17. require_once 'config.php';
  18. // set the email right here.
  19. $config['email']= $config['user'].'@'.$config['domain'];
  20. require_once 'Zend/Loader.php';
  21. Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
  22. Zend_Loader::loadClass('Zend_Gdata_Gapps');
  23. $actions = Array(
  24. "add" => Array(
  25. "required_opts" => Array ('u','f','l','p'),
  26. "help" => "-u username -f first name -l last name -p password"
  27. ),
  28. "del" => Array(
  29. "required_opts" => Array ('u'),
  30. "help" => "-u username"
  31. ),
  32. "suspend" => Array (
  33. "required_opts" => Array('u'),
  34. "help" => "-u username"
  35. ),
  36. "restore" => Array (
  37. "required_opts" => Array('u'),
  38. "help" => "-u username"
  39. ),
  40. "display" => Array (
  41. "required_opts" => Array('u'),
  42. "help" => "-u username"
  43. ),
  44. "change-name" => Array(
  45. "required_opts" => Array('u','f','l'),
  46. "help" => "-u username -f first name -l last name"
  47. ),
  48. "change-password" => Array(
  49. "required_opts" => Array('u','p'),
  50. "help" => "Changes a user's password"
  51. ),
  52. "force-pw-change" => Array(
  53. "required_opts" => Array('u'),
  54. "help" => "-u username",
  55. "descr" => "Require a user to change the password at next login"
  56. ),
  57. "update" => Array(
  58. "enabled" => false,
  59. "required_opts" => Array('u'),
  60. "help" => "-u username ... "
  61. ),
  62. "list-add" => Array(
  63. "enabled" => false,
  64. "required_opts" => Array('u'),
  65. "help" => "-u username -e listname ",
  66. "descr" => "Adds username to email list"
  67. ),
  68. "list-rm" => Array(
  69. "enabled" => false,
  70. "required_opts" => Array('u'),
  71. "help" => "-u username ... -e listname",
  72. "descr" => "Removed username from email list"
  73. ),
  74. "list-all-admins" => Array(
  75. "enabled" => true,
  76. "required_opts" => Array(),
  77. "help" => "",
  78. "descr" => "list all current administrators"
  79. )
  80. );
  81. // short opts == for the api
  82. // long opts == for the script
  83. $shortopts = "";
  84. $shortopts .= "a:"; //action
  85. $shortopts .= "u:"; //username
  86. $shortopts .= "f:"; //First Name
  87. $shortopts .= "l:"; //Last name
  88. $shortopts .= "p:"; //password
  89. $shortopts .= "h"; //print usage info
  90. $longopts = Array ("action:", 'debug', 'verbose','check-config');
  91. $cli = getopt($shortopts, $longopts);
  92. if (!is_null($cli['h']))
  93. {
  94. usage();
  95. }
  96. //convert from getopt moe to useable mode...
  97. $doWhat = (empty($cli['action'])) ? '' : $cli['action'];
  98. if (empty($doWhat))
  99. {
  100. $doWhat = (empty($cli['a'])) ? '' : $cli['a'];
  101. }
  102. $doWhatPrint = colorText(COLOR_GREEN,$doWhat);
  103. echo "Trying to $doWhatPrint...\n";
  104. $username = (empty($cli['u'])) ? '' : $cli['u'];
  105. $name[0] = (empty($cli['f'])) ? '' : $cli['f'];
  106. $name[1] = (empty($cli['l'])) ? '' : $cli['l'];
  107. $givepass = (empty($cli['p'])) ? '' : $cli['p'];
  108. $nickname = (empty($cli['n'])) ? '' : $cli['n'];
  109. // long opts
  110. $debug = (array_key_exists('debug',$cli)) ? true : false;
  111. $verbose = (array_key_exists('verbose',$cli)) ? true : false;
  112. $check_config = (array_key_exists('check-config',$cli)) ? true : false;
  113. // act on the long opts
  114. if ($check_config)
  115. {
  116. $email = colorText(1,$config['email']);
  117. $password = colorText(1,$config['password']);
  118. echo "Please verify that $email is your email address and that $password is your password\n";
  119. // we don't go farther than this with check-config
  120. return;
  121. }
  122. if ($debug)
  123. {
  124. echo "We are in debug mode. Operations may change output mode\n";
  125. }
  126. // if we don't list a valid action
  127. if (!array_key_exists($doWhat,$actions))
  128. {
  129. //show usage...
  130. usage("dowhat not in array");
  131. }
  132. // triple === for null checking
  133. if ($actions[$doWhat]["enabled"] === false)
  134. {
  135. usage("This action is not enabled");
  136. }
  137. // check to make sure that all relevent fields are listed...
  138. foreach ($actions[$doWhat]["required_opts"] as $opt)
  139. {
  140. if (empty($cli[$opt]))
  141. {
  142. usage("Missing $opt option\n");
  143. }
  144. }
  145. $client = Zend_Gdata_ClientLogin::getHttpClient($config['email'], $config['password'], Zend_Gdata_Gapps::AUTH_SERVICE_NAME);
  146. $service = new Zend_Gdata_Gapps($client, $config['domain']);
  147. echo "Editing user:".colorText(COLOR_GREEN,$username)."\n";
  148. // I think we should move to getting the user object before the case and submitting it after the case
  149. // This should make it easier to do certain things (like updating an account).
  150. // Only problem is what if user does not exist?
  151. try
  152. {
  153. switch ($doWhat)
  154. {
  155. case 'add':
  156. $service->createUser($username, $name[0], $name[1], $givepass, null, null);
  157. break;
  158. case 'del':
  159. $service->deleteUser($username);
  160. break;
  161. case 'suspend':
  162. $service->suspendUser($username);
  163. break;
  164. case 'restore':
  165. $service->restoreUser($username);
  166. break;
  167. case 'display':
  168. displayUser($username);
  169. break;
  170. case 'force-pw-change':
  171. $user = $service->retrieveUser($username);
  172. $user->login->changePasswordAtNextLogin = true;
  173. $user = $user->save();
  174. break;
  175. case 'change-name':
  176. if ($verbose)
  177. {
  178. echo colorText(1,"Before"),":\n";
  179. displayUser($username);
  180. }
  181. $user = $service->retrieveUser($username);
  182. $user->name->givenName = $name[0];
  183. $user->name->familyName = $name[1];
  184. $user = $user->save();
  185. if ($verbose)
  186. {
  187. echo colorText(1,"After"),":\n";
  188. displayUser($username);
  189. }
  190. break;
  191. case 'change-password':
  192. $user = $service->retrieveUser($username);
  193. $user->login->password = $givepass;
  194. $user = $user->save();
  195. break;
  196. }
  197. echo "This appears to have worked...\n";
  198. }
  199. catch (Zend_Gdata_Gapps_ServiceException $e)
  200. {
  201. if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST))
  202. {
  203. echo "Our Error encountered: ".colorText(1,"User does not exist")."\n";
  204. }
  205. else
  206. {
  207. // Outherwise, just print the errors that occured and exit
  208. foreach ($e->getErrors() as $error)
  209. {
  210. echo "Our Error encountered: ".colorText(1,$error->getReason())." (". colorText(1,$error->getErrorCode()).")\n";
  211. }
  212. }
  213. exit();
  214. }
  215. function usage($message="")
  216. {
  217. global $actions, $doWhat;
  218. $me = colorText(COLOR_BOLD,basename ( __FILE__ ));
  219. echo "This file is called $me\n\n";
  220. foreach ($actions as $key => $params)
  221. {
  222. $helpline = $params['help'];
  223. $sur_text="";
  224. // triple equals to avoid null
  225. if ($params['enabled'] === false)
  226. {
  227. $sur_text=colorText(COLOR_RED,'Disabled');
  228. }
  229. if (!empty($params['descr']))
  230. {
  231. echo $params['descr'] . "\n";
  232. }
  233. $key_print = colorText(1,$key);
  234. // Indicate which action we want to use";
  235. if ($doWhat == $key)
  236. {
  237. $key_print = colorText(COLOR_GREEN,$key);
  238. $helpline = colorText(COLOR_GREEN,$helpline);
  239. }
  240. echo "\t$sur_text [-a|--action] $key_print $helpline\n";
  241. }
  242. echo colorText(COLOR_BOLD," \t--check-config")."\t Verifies that your configuration options are correct\n";
  243. echo colorText(COLOR_BOLD," \t--debug")."\t Prints debugging information\n";
  244. echo colorText(COLOR_BOLD," \t--verbose")."\t Increases amount of output generated by the script\n";
  245. if (!empty($message)) {echo "\n". colorText(COLOR_BROWN,$message)."\n";}
  246. echo "Please send all bug reports to Eitan Adler\n";
  247. die();
  248. }
  249. function displayUser($username)
  250. {
  251. global $service, $debug;
  252. $user = $service->retrieveUser($username);
  253. if ($debug)
  254. {
  255. var_dump($user);
  256. return;
  257. }
  258. $info['nicks'] = $service->retrieveNicknames($username);
  259. $info['lists'] = $service->retrieveEmailLists($username);
  260. echo 'Username: ' . $user->login->userName . "\n";
  261. echo 'Given Name: ' . $user->name->givenName . "\n";
  262. echo 'Family Name: ' . $user->name->familyName . "\n";
  263. echo 'Suspended: ' . ($user->login->suspended ? 'Yes' : 'No') . "\n";
  264. echo 'Admin: ' . ($user->login->admin ? 'Yes' : 'No') . "\n";
  265. echo 'Must Change Password: ' .
  266. ($user->login->changePasswordAtNextLogin ? 'Yes' : 'No') . "\n";
  267. echo 'Has Agreed To Terms: ' .
  268. ($user->login->agreedToTerms ? 'Yes' : 'No') . "\n";
  269. echo "IP whitelisted: ";
  270. echo ($user->login->extensionAttributes->ipWhitelisted) ? "Yes" : "No" . "\n";
  271. echo 'List of Nicknames: ';
  272. foreach ($info['nicks'] as $nickname)
  273. {
  274. echo ' * ' . $nickname->nickname->name . "\n";
  275. }
  276. echo "\n";
  277. echo 'List of Email Lists: ';
  278. foreach ($info['lists'] as $list)
  279. {
  280. echo ' * ' . $list->emailList->name . "\n";
  281. }
  282. echo "\n";
  283. }
  284. function colorText($color,$text)
  285. {
  286. return "\033[".$color."m$text\033[0m";
  287. }
  288. ?>