tunethemusic /public/zf-demos/Zend/Gdata/Gapps.php

Language PHP Lines 1993
MD5 Hash 29cd901faa83329e30bd85e22be50995 Estimated Cost $25,261 (why?)
Repository https://bitbucket.org/hieronim1981/tunethemusic.git View Raw File View Project SPDX
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
<?php
/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Gdata
 * @subpackage Demos
 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * PHP sample code for the Google Calendar data API.  Utilizes the
 * Zend Framework Gdata components to communicate with the Google API.
 *
 * Requires the Zend Framework Gdata components and PHP >= 5.1.4
 *
 * You can run this sample both from the command line (CLI) and also
 * from a web browser.  Run this script without any command line options to
 * see usage, eg:
 *     /usr/bin/env php Gapps.php
 *
 * More information on the Command Line Interface is available at:
 *     http://www.php.net/features.commandline
 *
 * When running this code from a web browser, be sure to fill in your
 * Google Apps credentials below and choose a password for authentication
 * via the web browser.
 *
 * Since this is a demo, only minimal error handling and input validation
 * are performed. THIS CODE IS FOR DEMONSTRATION PURPOSES ONLY. NOT TO BE
 * USED IN A PRODUCTION ENVIRONMENT.
 *
 * NOTE: You must ensure that Zend Framework is in your PHP include
 * path.  You can do this via php.ini settings, or by modifying the
 * argument to set_include_path in the code below.
 */

// ************************ BEGIN WWW CONFIGURATION ************************

/**
 * Google Apps username. This is the username (without domain) used
 * to administer your Google Apps account. This value is only
 * used when accessing this demo on a web server.
 *
 * For example, if you login to Google Apps as 'foo@bar.com.inavlid',
 * your username is 'foo'.
 */
define('GAPPS_USERNAME', 'username');

/**
 * Google Apps domain. This is the domain associated with your
 * Google Apps account. This value is only used when accessing this demo
 * on a web server.
 *
 * For example, if you login to Google Apps as foo@bar.com.inavlid,
 * your domain is 'bar.com.invalid'.
 */
define('GAPPS_DOMAIN', 'example.com.invalid');

/**
 * Google Apps password. This is the password associated with the above
 * username. This value is only used when accessing this demo on a
 * web server.
 */
define('GAPPS_PASSWORD', 'your password here');

/**
 * Login password. This password is used to protect your account from
 * unauthorized access when running this demo on a web server.
 *
 * If this field is blank, all access will be denied. A blank password
 * field is not the same as no password (which is disallowed for
 * security reasons).
 *
 * NOTE: While we could technically just ask the user for their Google Apps
 *       credentials, the ClientLogin API is not intended for direct use by
 *       web applications. If you are the only user of the application, this
 *       is fine--- but you should not ask other users to enter their
 *       credentials via your web application.
 */
define('LOGIN_PASSWORD', '');

// ************************* END WWW CONFIGURATION *************************

/**
 * @see Zend_Loader
 */
require_once 'Zend/Loader.php';

/**
 * @see Zend_Gdata
 */
Zend_Loader::loadClass('Zend_Gdata');

/**
 * @see Zend_Gdata_ClientLogin
 */
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

/**
 * @see Zend_Gdata_Gapps
 */
Zend_Loader::loadClass('Zend_Gdata_Gapps');

/**
 * Returns a HTTP client object with the appropriate headers for communicating
 * with Google using the ClientLogin credentials supplied.
 *
 * @param  string $user The username, in e-mail address format, to authenticate
 * @param  string $pass The password for the user specified
 * @return Zend_Http_Client
 */
function getClientLoginHttpClient($user, $pass)
{
  $service = Zend_Gdata_Gapps::AUTH_SERVICE_NAME;
  $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  return $client;
}

/**
 * Creates a new user for the current domain. The user will be created
 * without admin privileges.
 *
 * @param  Zend_Gdata_Gapps $gapps      The service object to use for communicating with the Google
 *                                      Apps server.
 * @param  boolean          $html       True if output should be formatted for display in a web browser.
 * @param  string           $username   The desired username for the user.
 * @param  string           $givenName  The given name for the user.
 * @param  string           $familyName The family name for the user.
 * @param  string           $password   The plaintext password for the user.
 * @return void
 */
function createUser($gapps, $html, $username, $givenName, $familyName,
        $password)
{
    if ($html) {echo "<h2>Create User</h2>\n";}
    $gapps->createUser($username, $givenName, $familyName,
        $password);
    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Retrieves a user for the current domain by username. Information about
 * that user is then output.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $username The desired username for the user.
 * @return void
 */
function retrieveUser($gapps, $html, $username)
{
    if ($html) {echo "<h2>User Information</h2>\n";}

    $user = $gapps->retrieveUser($username);

    if ($html) {echo '<p>';}

    if ($user !== null) {
        echo '             Username: ' . $user->login->username;
        if ($html) {echo '<br />';}
        echo "\n";

        echo '           Given Name: ';
        if ($html) {
            echo htmlspecialchars($user->name->givenName);
        } else {
            echo $user->name->givenName;
        }
        if ($html) {echo '<br />';}
        echo "\n";

        echo '          Family Name: ';
        if ($html) {
            echo htmlspecialchars($user->name->familyName);
        } else {
            echo $user->name->familyName;
        }
        if ($html) {echo '<br />';}
        echo "\n";

        echo '            Suspended: ' . ($user->login->suspended ? 'Yes' : 'No');
        if ($html) {echo '<br />';}
        echo "\n";

        echo '                Admin: ' . ($user->login->admin ? 'Yes' : 'No');
        if ($html) {echo '<br />';}
        echo "\n";

        echo ' Must Change Password: ' .
            ($user->login->changePasswordAtNextLogin ? 'Yes' : 'No');
        if ($html) {echo '<br />';}
        echo "\n";

        echo '  Has Agreed To Terms: ' .
            ($user->login->agreedToTerms ? 'Yes' : 'No');

    } else {
        echo 'Error: Specified user not found.';
    }
    if ($html) {echo '</p>';}
    echo "\n";
}

/**
 * Retrieves the list of users for the current domain and outputs
 * that list.
 *
 * @param  Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google Apps server.
 * @param  boolean          $html  True if output should be formatted for display in a web browser.
 * @return void
 */
function retrieveAllUsers($gapps, $html)
{
    if ($html) {echo "<h2>Registered Users</h2>\n";}

    $feed = $gapps->retrieveAllUsers();

    if ($html) {echo "<ul>\n";}

    foreach ($feed as $user) {
        if ($html) {
            echo "  <li>";
        } else {
            echo "  * ";
        }
        echo $user->login->username . ' (';
        if ($html) {
            echo htmlspecialchars($user->name->givenName . ' ' .
                $user->name->familyName);
        } else {
            echo $user->name->givenName . ' ' . $user->name->familyName;
        }
        echo ')';
        if ($html) {echo '</li>';}
        echo "\n";
    }
    if ($html) {echo "</ul>\n";}
}

/**
 * Change the name for an existing user.
 *
 * @param  Zend_Gdata_Gapps $gapps         The service object to use for communicating with the Google
 *                                         Apps server.
 * @param  boolean          $html          True if output should be formatted for display in a web browser.
 * @param  string           $username      The username which should be updated
 * @param  string           $newGivenName  The new given name for the user.
 * @param  string           $newFamilyName The new family name for the user.
 * @return void
 */
function updateUserName($gapps, $html, $username, $newGivenName, $newFamilyName)
{
    if ($html) {echo "<h2>Update User Name</h2>\n";}

    $user = $gapps->retrieveUser($username);

    if ($user !== null) {
        $user->name->givenName = $newGivenName;
        $user->name->familyName = $newFamilyName;
        $user->save();
    } else {
        if ($html) {echo '<p>';}
        echo 'Error: Specified user not found.';
        if ($html) {echo '</p>';}
        echo "\n";
    }

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Change the password for an existing user.
 *
 * @param  Zend_Gdata_Gapps $gapps       The service object to use for communicating with the Google
 *                                       Apps server.
 * @param  boolean          $html        True if output should be formatted for display in a web browser.
 * @param  string           $username    The username which should be updated
 * @param  string           $newPassword The new password for the user.
 * @return void
 */
function updateUserPassword($gapps, $html, $username, $newPassword)
{
    if ($html) {echo "<h2>Update User Password</h2>\n";}

    $user = $gapps->retrieveUser($username);

    if ($user !== null) {
        $user->login->password = $newPassword;
        $user->save();
    } else {
        if ($html) {echo '<p>';}
        echo 'Error: Specified user not found.';
        if ($html) {echo '</p>';}
        echo "\n";
    }

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Suspend a given user. The user will not be able to login until restored.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $username The username which should be updated.
 * @return void
 */
function suspendUser($gapps, $html, $username)
{
    if ($html) {echo "<h2>Suspend User</h2>\n";}

    $user = $gapps->retrieveUser($username);

    if ($user !== null) {
        $user->login->suspended = true;
        $user->save();
    } else {
        if ($html) {echo '<p>';}
        echo 'Error: Specified user not found.';
        if ($html) {echo '</p>';}
        echo "\n";
    }

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Restore a given user after being suspended.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $username The username which should be updated.
 * @return void
 */
function restoreUser($gapps, $html, $username)
{
    if ($html) {echo "<h2>Restore User</h2>\n";}

    $user = $gapps->retrieveUser($username);

    if ($user !== null) {
        $user->login->suspended = false;
        $user->save();
    } else {
        if ($html) {echo '<p>';}
        echo 'Error: Specified user not found.';
        if ($html) {echo '</p>';}
        echo "\n";
    }

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Give a user admin rights.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $username The username which should be updated.
 * @return void
 */
function giveUserAdminRights($gapps, $html, $username)
{
    if ($html) {echo "<h2>Grant Administrative Rights</h2>\n";}

    $user = $gapps->retrieveUser($username);

    if ($user !== null) {
        $user->login->admin = true;
        $user->save();
    } else {
        if ($html) {echo '<p>';}
        echo 'Error: Specified user not found.';
        if ($html) {echo '</p>';}
        echo "\n";
    }

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Revoke a user's admin rights.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $username The username which should be updated.
 * @return void
 */
function revokeUserAdminRights($gapps, $html, $username)
{
    if ($html) {echo "<h2>Revoke Administrative Rights</h2>\n";}

    $user = $gapps->retrieveUser($username);

    if ($user !== null) {
        $user->login->admin = false;
        $user->save();
    } else {
        if ($html) {echo '<p>';}
        echo 'Error: Specified user not found.';
        if ($html) {echo '</p>';}
        echo "\n";
    }

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Force a user to change their password at next login.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $username The username which should be updated.
 * @return void
 */
function setUserMustChangePassword($gapps, $html, $username)
{
    if ($html) {echo "<h2>Force User To Change Password</h2>\n";}

    $user = $gapps->retrieveUser($username);

    if ($user !== null) {
        $user->login->changePasswordAtNextLogin = true;
        $user->save();
    } else {
        if ($html) {echo '<p>';}
        echo 'Error: Specified user not found.';
        if ($html) {echo '</p>';}
        echo "\n";
    }

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Undo forcing a user to change their password at next login.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $username The username which should be updated.
 * @return void
 */
function clearUserMustChangePassword($gapps, $html, $username)
{
    if ($html) {echo "<h2>Undo Force User To Change Password</h2>\n";}

    $user = $gapps->retrieveUser($username);

    if ($user !== null) {
        $user->login->changePasswordAtNextLogin = false;
        $user->save();
    } else {
        if ($html) {echo '<p>';}
        echo 'Error: Specified user not found.';
        if ($html) {echo '</p>';}
        echo "\n";
    }

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Delete the user who owns a given username.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $username The username which should be deleted.
 * @return void
 */
function deleteUser($gapps, $html, $username)
{
    if ($html) {echo "<h2>Delete User</h2>\n";}

    $gapps->deleteUser($username);

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Create a new nickname.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $username The username to which the nickname should be assigned.
 * @param  string           $nickname The name of the nickname to be created.
 * @return void
 */
function createNickname($gapps, $html, $username, $nickname)
{
    if ($html) {echo "<h2>Create Nickname</h2>\n";}

    $gapps->createNickname($username, $nickname);

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Retrieve a specified nickname and output its ownership information.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $nickname The name of the nickname to be retrieved.
 * @return void
 */
function retrieveNickname($gapps, $html, $nickname)
{
    if ($html) {echo "<h2>Nickname Information</h2>\n";}

    $nickname = $gapps->retrieveNickname($nickname);

    if ($html) {echo '<p>';}

    if ($nickname !== null) {
        echo ' Nickname: ' . $nickname->nickname->name;
        if ($html) {echo '<br />';}
        echo "\n";

        echo '    Owner: ' . $nickname->login->username;
    } else {
        echo 'Error: Specified nickname not found.';
    }
    if ($html) {echo '</p>';}
    echo "\n";
}

/**
 * Outputs all nicknames owned by a specific username.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $username The username whose nicknames should be displayed.
 * @return void
 */
function retrieveNicknames($gapps, $html, $username)
{
    if ($html) {echo "<h2>Registered Nicknames For {$username}</h2>\n";}

    $feed = $gapps->retrieveNicknames($username);

    if ($html) {echo "<ul>\n";}

    foreach ($feed as $nickname) {
        if ($html) {
            echo "  <li>";
        } else {
            echo "  * ";
        }
        echo $nickname->nickname->name;
        if ($html) {echo '</li>';}
        echo "\n";
    }
    if ($html) {echo "</ul>\n";}
}


/**
 * Retrieves the list of nicknames for the current domain and outputs
 * that list.
 *
 * @param  Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
 *                                 Apps server.
 * @param  boolean          $html  True if output should be formatted for display in a web browser.
 * @return void
 */
function retrieveAllNicknames($gapps, $html)
{
    if ($html) {echo "<h2>Registered Nicknames</h2>\n";}

    $feed = $gapps->retrieveAllNicknames();

    if ($html) {echo "<ul>\n";}

    foreach ($feed as $nickname) {
        if ($html) {
            echo "  <li>";
        } else {
            echo "  * ";
        }
        echo $nickname->nickname->name . ' => ' . $nickname->login->username;
        if ($html) {echo '</li>';}
        echo "\n";
    }
    if ($html) {echo "</ul>\n";}
}

/**
 * Delete's a specific nickname from the current domain.
 *
 * @param  Zend_Gdata_Gapps $gapps    The service object to use for communicating with the Google
 *                                    Apps server.
 * @param  boolean          $html     True if output should be formatted for display in a web browser.
 * @param  string           $nickname The nickname that should be deleted.
 * @return void
 */
function deleteNickname($gapps, $html, $nickname)
{
    if ($html) {echo "<h2>Delete Nickname</h2>\n";}

    $gapps->deleteNickname($nickname);

    if ($html) {echo "<p>Done.</p>\n";}

}

/**
 * Create a new email list.
 *
 * @param  Zend_Gdata_Gapps $gapps     The service object to use for communicating with the Google
 *                                     Apps server.
 * @param  boolean          $html      True if output should be formatted for display in a web browser.
 * @param  string           $emailList The name of the email list to be created.
 * @return void
 */
function createEmailList($gapps, $html, $emailList)
{
    if ($html) {echo "<h2>Create Email List</h2>\n";}

    $gapps->createEmailList($emailList);

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Outputs the list of email lists to which the specified address is
 * subscribed.
 *
 * @param  Zend_Gdata_Gapps $gapps     The service object to use for communicating with the Google
 *                                     Apps server.
 * @param  boolean          $html      True if output should be formatted for display in a web browser.
 * @param  string           $recipient The email address of the recipient whose subscriptions should
 *                                     be retrieved. Only a username is required if the recipient is a
 *                                     member of the current domain.
 * @return void
 */
function retrieveEmailLists($gapps, $html, $recipient)
{
    if ($html) {echo "<h2>Email List Subscriptions For {$recipient}</h2>\n";}

    $feed = $gapps->retrieveEmailLists($recipient);

    if ($html) {echo "<ul>\n";}

    foreach ($feed as $list) {
        if ($html) {
            echo "  <li>";
        } else {
            echo "  * ";
        }
        echo $list->emailList->name;
        if ($html) {echo '</li>';}
        echo "\n";
    }
    if ($html) {echo "</ul>\n";}
}

/**
 * Outputs the list of all email lists on the current domain.
 *
 * @param  Zend_Gdata_Gapps $gapps The service object to use for communicating with the Google
 *                                 Apps server.
 * @param  boolean          $html  True if output should be formatted for display in a web browser.
 * @return void
 */
function retrieveAllEmailLists($gapps, $html)
{
    if ($html) {echo "<h2>Registered Email Lists</h2>\n";}

    $feed = $gapps->retrieveAllEmailLists();

    if ($html) {echo "<ul>\n";}

    foreach ($feed as $list) {
        if ($html) {
            echo "  <li>";
        } else {
            echo "  * ";
        }
        echo $list->emailList->name;
        if ($html) {echo '</li>';}
        echo "\n";
    }
    if ($html) {echo "</ul>\n";}
}

/**
 * Delete's a specific email list from the current domain.
 *
 * @param  Zend_Gdata_Gapps $gapps     The service object to use for communicating with the Google
 *                                     Apps server.
 * @param  boolean          $html      True if output should be formatted for display in a web browser.
 * @param  string           $emailList The email list that should be deleted.
 * @return void
 */
function deleteEmailList($gapps, $html, $emailList)
{
    if ($html) {echo "<h2>Delete Email List</h2>\n";}

    $gapps->deleteEmailList($emailList);

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Add a recipient to an existing email list.
 *
 * @param  Zend_Gdata_Gapps $gapps            The service object to use for communicating with the
 *                                            Google Apps server.
 * @param  boolean          $html             True if output should be formatted for display in a
 *                                            web browser.
 * @param  string           $recipientAddress The address of the recipient who should be added.
 * @param  string           $emailList        The name of the email address the recipient be added to.
 * @return void
 */
function addRecipientToEmailList($gapps, $html, $recipientAddress,
        $emailList)
{
    if ($html) {echo "<h2>Subscribe Recipient</h2>\n";}

    $gapps->addRecipientToEmailList($recipientAddress, $emailList);

    if ($html) {echo "<p>Done.</p>\n";}
}

/**
 * Outputs the list of all recipients for a given email list.
 *
 * @param  Zend_Gdata_Gapps $gapps     The service object to use for communicating with the Google
 *                                     Apps server.
 * @param  boolean          $html      True if output should be formatted for display in a web browser.
 * @param  string           $emailList The email list whose recipients should be output.
 * @return void
 */
function retrieveAllRecipients($gapps, $html, $emailList)
{
    if ($html) {echo "<h2>Email List Recipients For {$emailList}</h2>\n";}

    $feed = $gapps->retrieveAllRecipients($emailList);

    if ($html) {echo "<ul>\n";}

    foreach ($feed as $recipient) {
        if ($html) {
            echo "  <li>";
        } else {
            echo "  * ";
        }
        echo $recipient->who->email;
        if ($html) {echo '</li>';}
        echo "\n";
    }
    if ($html) {echo "</ul>\n";}
}

/**
 * Remove an existing recipient from an email list.
 *
 * @param  Zend_Gdata_Gapps $gapps            The service object to use for communicating with the
 *                                            Google Apps server.
 * @param  boolean          $html             True if output should be formatted for display in a
 *                                            web browser.
 * @param  string           $recipientAddress The address of the recipient who should be removed.
 * @param  string           $emailList        The email list from which the recipient should be removed.
 * @return void
 */
function removeRecipientFromEmailList($gapps, $html, $recipientAddress,
        $emailList)
{
    if ($html) {echo "<h2>Unsubscribe Recipient</h2>\n";}

    $gapps->removeRecipientFromEmailList($recipientAddress, $emailList);

    if ($html) {echo "<p>Done.</p>\n";}

}

// ************************ BEGIN CLI SPECIFIC CODE ************************

/**
 * Display list of valid commands.
 *
 * @param  string $executable The name of the current script. This is usually available as $argv[0].
 * @return void
 */
function displayHelp($executable)
{
    echo "Usage: php {$executable} <action> [<username>] [<password>] " .
        "[<arg1> <arg2> ...]\n\n";
    echo "Possible action values include:\n" .
        "createUser\n" .
        "retrieveUser\n" .
        "retrieveAllUsers\n" .
        "updateUserName\n" .
        "updateUserPassword\n" .
        "suspendUser\n" .
        "restoreUser\n" .
        "giveUserAdminRights\n" .
        "revokeUserAdminRights\n" .
        "setUserMustChangePassword\n" .
        "clearUserMustChangePassword\n" .
        "deleteUser\n" .
        "createNickname\n" .
        "retrieveNickname\n" .
        "retrieveNicknames\n" .
        "retrieveAllNicknames\n" .
        "deleteNickname\n" .
        "createEmailList\n" .
        "retrieveEmailLists\n" .
        "retrieveAllEmailLists\n" .
        "deleteEmailList\n" .
        "addRecipientToEmailList\n" .
        "retrieveAllRecipients\n" .
        "removeRecipientFromEmailList\n";
}

/**
 * Parse command line arguments and execute appropriate function when
 * running from the command line.
 *
 * If no arguments are provided, usage information will be provided.
 *
 * @param  array   $argv    The array of command line arguments provided by PHP.
 *                 $argv[0] should be the current executable name or '-' if not available.
 * @param  integer $argc    The size of $argv.
 * @return void
 */
function runCLIVersion($argv, $argc)
{
    if (isset($argc) && $argc >= 2) {
        # Prepare a server connection
        if ($argc >= 5) {
            try {
                $client = getClientLoginHttpClient($argv[2] . '@' . $argv[3], $argv[4]);
                $gapps = new Zend_Gdata_Gapps($client, $argv[3]);
            } catch (Zend_Gdata_App_AuthException $e) {
                echo "Error: Unable to authenticate. Please check your credentials.\n";
                exit(1);
            }
        }

        # Dispatch arguments to the desired method
        switch ($argv[1]) {
            case 'createUser':
                if ($argc == 9) {
                    createUser($gapps, false, $argv[5], $argv[6], $argv[7], $argv[8]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username> <given name> <family name> <user's password>\n\n";
                    echo "This creates a new user with the given username.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe John Doe p4ssw0rd\n";
                }
                break;
            case 'retrieveUser':
                if ($argc == 6) {
                    retrieveUser($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username>\n\n";
                    echo "This retrieves the user with the specified " .
                        "username and displays information about that user.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe\n";
                }
                break;
            case 'retrieveAllUsers':
                if ($argc == 5) {
                    retrieveAllUsers($gapps, false);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "\n\n";
                    echo "This lists all users on the current domain.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password>\n";
                }
                break;
            case 'updateUserName':
                if ($argc == 8) {
                    updateUserName($gapps, false, $argv[5], $argv[6], $argv[7]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username> <new given name> <new family name>\n\n";
                    echo "Renames an existing user.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe Jane Doe\n";
                }
                break;
            case 'updateUserPassword':
                if ($argc == 7) {
                    updateUserPassword($gapps, false, $argv[5], $argv[6]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username> <new user password>\n\n";
                    echo "Changes the password for an existing user.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe password1\n";
                }
                break;
            case 'suspendUser':
                if ($argc == 6) {
                    suspendUser($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username>\n\n";
                    echo "This suspends the given user.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe\n";
                }
                break;
            case 'restoreUser':
                if ($argc == 6) {
                    restoreUser($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username>\n\n";
                    echo "This restores the given user after being suspended.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe\n";
                }
                break;
            case 'giveUserAdminRights':
                if ($argc == 6) {
                    giveUserAdminRights($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username>\n\n";
                    echo "Give a user admin rights for this domain.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe\n";
                }
                break;
            case 'revokeUserAdminRights':
                if ($argc == 6) {
                    revokeUserAdminRights($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username>\n\n";
                    echo "Remove a user's admin rights for this domain.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe\n";
                }
                break;
            case 'setUserMustChangePassword':
                if ($argc == 6) {
                    setUserMustChangePassword($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username>\n\n";
                    echo "Force a user to change their password at next login.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe\n";
                }
                break;
            case 'clearUserMustChangePassword':
                if ($argc == 6) {
                    clearUserMustChangePassword($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username>\n\n";
                    echo "Clear the flag indicating that a user must change " .
                        "their password at next login.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe\n";
                }
                break;
            case 'deleteUser':
                if ($argc == 6) {
                    deleteUser($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username>\n\n";
                    echo "Delete the user who owns a given username.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe\n";
                }
                break;
            case 'createNickname':
                if ($argc == 7) {
                    createNickname($gapps, false, $argv[5], $argv[6]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username> <nickname>\n\n";
                    echo "Create a new nickname for the specified user.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe johnny\n";
                }
                break;
            case 'retrieveNickname':
                if ($argc == 6) {
                    retrieveNickname($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<nickname>\n\n";
                    echo "Retrieve a nickname and display its ownership " .
                        "information.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "johnny\n";
                }
                break;
            case 'retrieveNicknames':
                if ($argc == 6) {
                    retrieveNicknames($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<user's username>\n\n";
                    echo "Output all nicknames owned by a specific username.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "jdoe\n";
                }
                break;
            case 'retrieveAllNicknames':
                if ($argc == 5) {
                    retrieveAllNicknames($gapps, false);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "\n\n";
                    echo "Output all registered nicknames on the system.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "\n";
                }
                break;
            case 'deleteNickname':
                if ($argc == 6) {
                    deleteNickname($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<nickname>\n\n";
                    echo "Delete a specific nickname.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "johnny\n";
                }
                break;
            case 'createEmailList':
                if ($argc == 6) {
                    createEmailList($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<email list>\n\n";
                    echo "Create a new email list with the specified name.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "friends\n";
                }
                break;
            case 'retrieveEmailLists':
                if ($argc == 6) {
                    retrieveEmailLists($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<recipient>\n\n";
                    echo "Retrieve all email lists to which the specified " .
                        "address is subscribed.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "johnny@somewhere.com.invalid\n";
                }
                break;
            case 'retrieveAllEmailLists':
                if ($argc == 5) {
                    retrieveAllEmailLists($gapps, false);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "\n\n";
                    echo "Retrieve a list of all email lists on the current " .
                        "domain.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "\n";
                }
                break;
            case 'deleteEmailList':
                if ($argc == 6) {
                    deleteEmailList($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<email list>\n\n";
                    echo "Delete a specified email list.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "friends\n";
                }
                break;
            case 'addRecipientToEmailList':
                if ($argc == 7) {
                    addRecipientToEmailList($gapps, false, $argv[5], $argv[6]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<recipient> <email list>\n\n";
                    echo "Add a recipient to an existing email list.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "johnny@somewhere.com.invalid friends\n";
                }
                break;
            case 'retrieveAllRecipients':
                if ($argc == 6) {
                    retrieveAllRecipients($gapps, false, $argv[5]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<email list>\n\n";
                    echo "Retrieve all recipients for an existing email list.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "friends\n";
                }
                break;
            case 'removeRecipientFromEmailList':
                if ($argc == 7) {
                    removeRecipientFromEmailList($gapps, false, $argv[5], $argv[6]);
                } else {
                    echo "Usage: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "<recipient> <email list>\n\n";
                    echo "Remove an existing recipient from an email list.\n";
                    echo "EXAMPLE: php {$argv[0]} {$argv[1]} <username> <domain> <password> " .
                        "johnny@somewhere.com.invalid friends\n";
                }
                break;
            default:
                // Invalid action entered
                displayHelp($argv[0]);
        // End switch block
        }
    } else {
        // action left unspecified
        displayHelp($argv[0]);
    }
}

// ************************ BEGIN WWW SPECIFIC CODE ************************

/**
 * Writes the HTML prologue for this app.
 *
 * NOTE: We would normally keep the HTML/CSS markup separate from the business
 *       logic above, but have decided to include it here for simplicity of
 *       having a single-file sample.
 *
 *
 * @param  boolean $displayMenu (optional) If set to true, a navigation
 *                              menu is displayed at the top of the page. Default is true.
 * @return void
 */
function startHTML($displayMenu = true)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Google Apps Provisioning API Demo</title>

    <style type="text/css" media="screen">
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: small;
        }

        #header {
            background-color: #9cF;
            -moz-border-radius: 5px;
            -webkit-border-radius: 5px;
            padding-left: 5px;
            height: 2.4em;
        }

        #header h1 {
            width: 49%;
            display: inline;
            float: left;
            margin: 0;
            padding: 0;
            font-size: 2em;
        }

        #header p {
            width: 49%;
            margin: 0;
            padding-right: 15px;
            float: right;
            line-height: 2.4em;
            text-align: right;
        }

        .clear {
            clear:both;
        }

        h2 {
            background-color: #ccc;
            -moz-border-radius: 5px;
            -webkit-border-radius: 5px;
            margin-top: 1em;
            padding-left: 5px;
        }

        .error {
            color: red;
        }

        form {
            width: 500px;
            background: #ddf8cc;
            border: 1px solid #80c605;
            padding: 0 1em;
            margin: 1em auto;
        }

        .warning {
            width: 500px;
            background: #F4B5B4;
            border: 1px solid #900;
            padding: 0 1em;
            margin: 1em auto;
        }

        label {
            display: block;
            width: 130px;
            float: left;
            text-align: right;
            padding-top: 0.3em;
            padding-right: 3px;
        }

        .radio {
            margin: 0;
            padding-left: 130px;
        }

        #menuSelect {
            padding: 0;
        }

        #menuSelect li {
            display: block;
            width: 500px;
            background: #ddf8cc;
            border: 1px solid #80c605;
            margin: 1em auto;
            padding: 0;
            font-size: 1.3em;
            text-align: center;
            list-style-type: none;
        }

        #menuSelect li:hover {
            background: #c4faa2;
        }

        #menuSelect a {
            display: block;
            height: 2em;
            margin: 0px;
            padding-top: 0.75em;
            padding-bottom: -0.25em;
            text-decoration: none;
        }

        #content {
            width: 600px;
            margin: 0 auto;
            padding: 0;
            text-align: left;
        }
    </style>

</head>

<body>

<div id="header">
    <h1>Google Apps API Demo</h1>
    <?php if ($displayMenu === true) { ?>
        <p><?php echo GAPPS_DOMAIN ?> | <a href="?">Main</a> | <a href="?menu=logout">Logout</a></p>
    <?php } ?>
    <div class="clear"></div>
</div>

<div id="content">
<?php
}

/**
 * Writes the HTML epilogue for this app and exit.
 *
 * @param  boolean $displayBackButton (optional) If true, displays a
 *                                    link to go back at the bottom of the page. Defaults to false.
 * @return void
 */
function endHTML($displayBackButton = false)
{
    if ($displayBackButton === true) {
        echo '<a href="javascript:history.go(-1)">&larr; Back</a>';
    }
?>
</div>
</body>
</html>
<?php
exit();
}

/**
 * Displays a notice indicating that a login password needs to be
 * set before continuing.
 *
 * @return void
 */
function displayPasswordNotSetNotice()
{
?>
    <div class="warning">
        <h3>Almost there...</h3>
        <p>Before using this demo, you must set an application password
            to protect your account. You will also need to set your
            Google Apps credentials in order to communicate with the Google
            Apps servers.</p>
        <p>To continue, open this file in a text editor and fill
            out the information in the configuration section.</p>
    </div>
<?php
}

/**
 * Displays a notice indicating that authentication to Google Apps failed.
 *
 * @return void
 */
function displayAuthenticationFailedNotice()
{
?>
    <div class="warning">
        <h3>Google Apps Authentication Failed</h3>
        <p>Authentication with the Google Apps servers failed.</p>
        <p>Please open this file in a text editor and make
            sure your credentials are correct.</p>
    </div>
<?php
}

/**
 * Outputs a request to the user to enter their login password.
 *
 * @param  string $errorText (optional) Error text to be displayed next to the login form.
 * @return void
 */
function requestUserLogin($errorText = null)
{
?>
    <form method="post" accept-charset="utf-8">
        <h3>Authentication Required</h3>
        <?php
            if ($errorText !== null) {
                echo '<span class="error">' . $errorText . "</span>\n";
            }
        ?>
        <p>Please enter your login password to continue.</p>
        <p><label for="password">Password: </label>
             <input type="password" name="password" value="" /></p>
        <p><strong>Notice:</strong> This application is for demonstration
            purposes only. Not for use in a production environment.</p>
        <p><input type="submit" value="Continue &rarr;"></p>
    </form>
<?php
}

/**
 * Display the main menu for running in a web browser.
 *
 * @return void
 */
function displayMenu()
{
?>
<h2>Main Menu</h2>

<p>Welcome to the Google Apps Provisioning API demo page. Please select
    from one of the following three options to see a list of commands.</p>

    <ul id="menuSelect">
        <li><a class="menuSelect" href="?menu=user">User Maintenance Menu</a></li>
        <li><a class="menuSelect" href="?menu=nickname">Nickname Maintenance Menu</a></li>
        <li><a class="menuSelect" href="?menu=emailList">Email List Maintenance Menu</a></li>
    </ul>

<p>Tip: You can also run this demo from the command line if your system
    has PHP CLI support enabled.</p>
<?php
}

/**
 * Display the user maintenance menu for running in a web browser.
 *
 * @return void
 */
function displayUserMenu()
{
?>
<h2>User Maintenance Menu</h2>

<form method="post" accept-charset="utf-8">
    <h3>Create User</h3>
    <p>Create a new user with the given properties.</p>
    <p>
        <input type="hidden" name="command" value="createUser" />
        <label for="user">Username: </label>
        <input type="text" name="user" value="" /><br />
        <label for="givenName">Given Name: </label>
        <input type="text" name="givenName" value="" /><br />
        <label for="familyName">Family Name: </label>
        <input type="text" name="familyName" value="" /><br />
        <label for="pass">Password: </label>
        <input type="password" name="pass" value="" />
    </p>

    <p><input type="submit" value="Create User &rarr;"></p>
</form>
<form method="get" accept-charset="utf-8">
    <h3>Retrieve User</h3>
    <p>Retrieve the information for an existing user.</p>
    <p>
        <input type="hidden" name="command" value="retrieveUser" />
        <label for="user">Username: </label>
        <input type="text" name="user" value="" /><br />
    </p>

    <p><input type="submit" value="Retrieve User &rarr;"></p>
</form>
<form method="get" accept-charset="utf-8">
    <h3>Retrieve All Users</h3>
    <p>Retrieve the list of all users on the current domain.</p>
    <p>
        <input type="hidden" name="command" value="retrieveAllUsers" />
    </p>

    <p><input type="submit" value="Retrieve Users &rarr;"></p>
</form>
<form method="post" accept-charset="utf-8">
    <h3>Update Name</h3>
    <p>Update the name for an existing user.</p>
    <p>
        <input type="hidden" name="command" value="updateUserName" />
        <label for="user">Username: </label>
        <input type="text" name="user" value="" /><br />
        <label for="givenName">New Given Name: </label>
        <input type="text" name="givenName" value="" /><br />
        <label for="familyName">New Family Name: </label>
        <input type="text" name="familyName" value="" /><br />
    </p>

    <p><input type="submit" value="Update User &rarr;"></p>
</form>
<form method="post" accept-charset="utf-8">
    <h3>Update Password</h3>
    <p>Update the password for an existing user.</p>
    <p>
        <input type="hidden" name="command" value="updateUserPassword" />
        <label for="user">Username: </label>
        <input type="text" name="user" value="" /><br />
        <label for="pass">New Password: </label>
        <input type="password" name="pass" value="" /></p>
    </p>

    <p><input type="submit" value="Update User &rarr;"></p>
</form>
<form method="post" accept-charset="utf-8">
    <h3>Suspend/Restore User</h3>
    <p>Mark an existing user as suspended or restore a suspended user.
        While suspended, the user will be prohibited from logging into
        this domain.</p>
    <p>
        <input type="hidden" name="command" value="setUserSuspended" />
        <label for="user">Username: </label>
        <input type="text" name="user" value="" />
    </p>
    <div class="radio">
        <input type="radio" name="mode" value="restore">User may log into
            this domain.</input><br />
        <input type="radio" name="mode" value="suspend" checked="true">User
            may <strong>not</strong> log into this domain.</input>
    </div>

    <p><input type="submit" value="Update User &rarr;"></p>
</form>
<form method="post" accept-charset="utf-8">
    <h3>Issue/Revoke Admin Rights</h3>
    <p>Set whether an existing user has administrative rights for the current
         domain.</p>
    <p>
        <input type="hidden" name="command" value="setUserAdmin" />
        <label for="user">Username: </label>
        <input type="text" name="user" value="" />
    </p>
    <div class="radio">
        <input type="radio" name="mode" value="issue">User
            may administer this domain.</input><br />
        <input type="radio" name="mode" value="revoke" checked="true">User
            may <strong>not</strong> administer this domain.</input>
    </div>

    <p><input type="submit" value="Update User &rarr;"></p>
</form>
<form method="post" accept-charset="utf-8">
    <h3>Force User To Change Password</h3>
    <p>Set whether an existing user must change their password at
        their next login.</p>
    <p>
        <input type="hidden" name="command" value="setForceChangePassword" />
        <label for="user">Username: </label>
        <input type="text" name="user" value="" />
    </p>
    <div class="radio">
        <input type="radio" name="mode" value="set">User is required to
            change their password at next login.</input><br />
        <input type="radio" name="mode" value="clear" checked="true">User is
            <strong>not</strong> required to change their password at next
                login.</input>
    </div>

    <p><input type="submit" value="Update User &rarr;"></p>
</form>
<form method="post" accept-charset="utf-8">
    <h3>Delete User</h3>
    <p>Delete an existing user on the current domain.</p>
    <p>
        <input type="hidden" name="command" value="deleteUser" />
        <label for="user">Username: </label>
        <input type="text" name="user" value="" /><br />
    </p>

    <p><input type="submit" value="Delete User &rarr;"></p>
</form>
<?php
}

/**
 * Display the nickname maintenance menu for running in a web browser.
 *
 * @return void
 */
function displayNicknameMenu()
{
?>
<h2>Nickname Maintenance Menu</h2>

<form method="post" accept-charset="utf-8">
    <h3>Create Nickname</h3>
    <p>Create a nickname for an existing user.</p>
    <p>
        <input type="hidden" name="command" value="createNickname" />
        <label for="user">Username: </label>
        <input type="text" name="user" value="" /><br />
        <label for="nickname">Nickname: </label>
        <input type="text" name="nickname" value="" /><br />
    </p>

    <p><input type="submit" value="Create Nickname &rarr;"></p>
</form>
<form method="get" accept-charset="utf-8">
    <h3>Retrieve Nickname</h3>
    <p>Retrieve the information for an existing nickname.</p>
    <p>
        <input type="hidden" name="command" value="retrieveNickname" />
        <label for="nickname">Nickname: </label>
        <input type="text" name="nickname" value="" /><br />
    </p>

    <p><input type="submit" value="Retrieve Nickname &rarr;"></p>
</form>
<form method="get" accept-charset="utf-8">
    <h3>Retrieve Nicknames</h3>
    <p>Retrieve the nicknames associated with an existing username.</p>
    <p>
        <input type="hidden" name="command" value="retrieveNicknames" />
        <label for="user">Username: </label>
        <input type="text" name="user" value="" /><br />
    </p>

    <p><input type="submit" value="Retrieve Nicknames &rarr;"></p>
</form>
<form method="get" accept-charset="utf-8">
    <h3>Retrieve All Nicknames</h3>
    <p>Retrieve the nicknames on the current domain.</p>
    <p>
        <input type="hidden" name="command" value="retrieveAllNicknames" />
    </p>

    <p><input type="submit" value="Retrieve Nicknames &rarr;"></p>
</form>
<form method="post" accept-charset="utf-8">
    <h3>Delete Nickname</h3>
    <p>Delete an existing nickname from the current domain.</p>
    <p>
        <input type="hidden" name="command" value="deleteNickname" />
        <label for="nickname">Nickname: </label>
        <input type="text" name="nickname" value="" /><br />
    </p>

    <p><input type="submit" value="Delete Nickname &rarr;"></p>
</form>
<?php
}

/**
 * Display the email list maintenance menu for running in a web browser.
 *
 * @return void
 */
function displayEmailListMenu()
{
?>
<h2>Email List Maintenance Menu</h2>

<form method="post" accept-charset="utf-8">
    <h3>Create Email List</h3>
    <p>Create a new email list for the current domain.</p>
    <p>
        <input type="hidden" name="command" value="createEmailList" />
        <label for="emailList">List Name: </label>
        <input type="text" name="emailList" value="" /><br />
    </p>

    <p><input type="submit" value="Create List &rarr;"></p>
</form>
<form method="get" accept-charset="utf-8">
    <h3>Retrieve Email Lists</h3>
    <p>Retrieve all email lists to which a given email address is
        subscribed.</p>
    <p>
        <input type="hidden" name="command" value="retrieveEmailLists" />
        <label for="recipient">Recipient Address: </label>
        <input type="text" name="recipient" value="" /><br />
    </p>

    <p><input type="submit" value="Retrieve Lists &rarr;"></p>
</form>
<form method="get" accept-charset="utf-8">
    <h3>Retrieve All Email Lists</h3>
    <p>Retrieve all email lists on the current domain.</p>
    <p>
        <input type="hidden" name="command" value="retrieveAllEmailLists" />
    </p>

    <p><input type="submit" value="Retrieve Lists &rarr;"></p>
</form>
<form method="post" accept-charset="utf-8">
    <h3>Delete Email List</h3>
    <p>Delete an existing email list from the current domain.</p>
    <p>
        <input type="hidden" name="command" value="deleteEmailList" />
        <label for="emailList">List Name: </label>
        <input type="text" name="emailList" value="" /><br />
    </p>

    <p><input type="submit" value="Delete List &rarr;"></p>
</form>
<form method="post" accept-charset="utf-8">
    <h3>Add Recipient To Email List</h3>
    <p>Add or remove a recipient from an existing email list. A complete
        email address is required for recipients outside the current
        domain.</p>
    <p>
        <input type="hidden" name="command" value="modifySubscription" />
        <label for="emailList">List Name: </label>
        <input type="text" name="emailList" value="" /><br />
        <label for="recipient">Recipient Address: </label>
        <input type="text" name="recipient" value="" /><br />
        <div class="radio">
            <input type="radio" name="mode" value="subscribe">Subscribe
                recipient.</input><br />
            <input type="radio" name="mode" value="unsubscribe"
                checked="true">Unsubscribe recipient.</input>
        </div>
    </p>

    <p><input type="submit" value="Update Subscription &rarr;"></p>
</form>
<form method="get" accept-charset="utf-8">
    <h3>Retrieve All Recipients</h3>
    <p>Retrieve all recipients subscribed to an existing email list.</p>
    <p>
        <input type="hidden" name="command" value="retrieveAllRecipients" />
        <label for="emailList">List Name: </label>
        <input type="text" name="emailList" value="" /><br />
    </p>

    <p><input type="submit" value="Retrieve Recipients &rarr;"></p>
</form>
<?php
}

/**
 * Log the current user out of the application.
 *
 * @return void
 */
function logout()
{
session_destroy();
?>
<h2>Logout</h2>

<p>Logout successful.</p>

<ul id="menuSelect">
    <li><a class="menuSelect" href="?">Login</a></li>
</ul>
<?php
}


/**
 * Processes loading of this sample code through a web browser.
 *
 * @return void
 */
function runWWWVersion()
{
    session_start();

    // Note that all calls to endHTML() below end script execution!

    // Check to make sure that the user has set a password.
    $p = LOGIN_PASSWORD;
    if (empty($p)) {
        startHTML(false);
        displayPasswordNotSetNotice();
        endHTML();
    }

    // Grab any login credentials that might be waiting in the request
    if (!empty($_POST['password'])) {
        if ($_POST['password'] == LOGIN_PASSWORD) {
            $_SESSION['authenticated'] = 'true';
        } else {
            // Invalid password. Stop and display a login screen.
            startHTML(false);
            requestUserLogin("Incorrect password.");
            endHTML();
        }
    }

    // If the user isn't authenticated, display a login screen
    if (!isset($_SESSION['authenticated'])) {
        startHTML(false);
        requestUserLogin();
        endHTML();
    }

    // Try to login. If login fails, log the user out and display an
    // error message.
    try {
        $client = getClientLoginHttpClient(GAPPS_USERNAME . '@' .
            GAPPS_DOMAIN, GAPPS_PASSWORD);
        $gapps = new Zend_Gdata_Gapps($client, GAPPS_DOMAIN);
    } catch (Zend_Gdata_App_AuthException $e) {
        session_destroy();
        startHTML(false);
        displayAuthenticationFailedNotice();
        endHTML();
    }

    // Success! We're logged in.
    // First we check for commands that can be submitted either though
    // POST or GET (they don't make any changes).
    if (!empty($_REQUEST['command'])) {
        switch ($_REQUEST['command']) {
            case 'retrieveUser':
                startHTML();
                retrieveUser($gapps, true, $_REQUEST['user']);
                endHTML(true);
            case 'retrieveAllUsers':
                startHTML();
                retrieveAllUsers($gapps, true);
                endHTML(true);
            case 'retrieveNickname':
                startHTML();
                retrieveNickname($gapps, true, $_REQUEST['nickname']);
                endHTML(true);
            case 'retrieveNicknames':
                startHTML();
                retrieveNicknames($gapps, true, $_REQUEST['user']);
                endHTML(true);
            case 'retrieveAllNicknames':
                startHTML();
                retrieveAllNicknames($gapps, true);
                endHTML(true);
            case 'retrieveEmailLists':
                startHTML();
                retrieveEmailLists($gapps, true, $_REQUEST['recipient']);
                endHTML(true);
            case 'retrieveAllEmailLists':
                startHTML();
                retrieveAllEmailLists($gapps, true);
                endHTML(true);
            case 'retrieveAllRecipients':
                startHTML();
                retrieveAllRecipients($gapps, true, $_REQUEST['emailList']);
                endHTML(true);
        }
    }

    // Now we handle the potentially destructive commands, which have to
    // be submitted by POST only.
    if (!empty($_POST['command'])) {
        switch ($_POST['command']) {
            case 'createUser':
                startHTML();
                createUser($gapps, true, $_POST['user'],
                    $_POST['givenName'], $_POST['familyName'],
                    $_POST['pass']);
                endHTML(true);
            case 'updateUserName':
                startHTML();
                updateUserName($gapps, true, $_POST['user'],
                    $_POST['givenName'], $_POST['familyName']);
                endHTML(true);
            case 'updateUserPassword':
                startHTML();
                updateUserPassword($gapps, true, $_POST['user'],
                    $_POST['pass']);
                endHTML(true);
            case 'setUserSuspended':
                if ($_POST['mode'] == 'suspend') {
                    startHTML();
                    suspendUser($gapps, true, $_POST['user']);
                    endHTML(true);
                } elseif ($_POST['mode'] == 'restore') {
                    startHTML();
                    restoreUser($gapps, true, $_POST['user']);
                    endHTML(true);
                } else {
                    header('HTTP/1.1 400 Bad Request');
                    startHTML();
                    echo "<h2>Invalid mode.</h2>\n";
                    echo "<p>Please check your request and try again.</p>";
                    endHTML(true);
                }
            case 'setUserAdmin':
                if ($_POST['mode'] == 'issue') {
                    startHTML();
                    giveUserAdminRights($gapps, true, $_POST['user']);
                    endHTML(true);
                } elseif ($_POST['mode'] == 'revoke') {
                    startHTML();
                    revokeUserAdminRights($gapps, true, $_POST['user']);
                    endHTML(true);
                } else {
                    header('HTTP/1.1 400 Bad Request');
                    startHTML();
                    echo "<h2>Invalid mode.</h2>\n";
                    echo "<p>Please check your request and try again.</p>";
                    endHTML(true);
                }
            case 'setForceChangePassword':
                if ($_POST['mode'] == 'set') {
                    startHTML();
                    setUserMustChangePassword($gapps, true, $_POST['user']);
                    endHTML(true);
                } elseif ($_POST['mode'] == 'clear') {
                    startHTML();
                    clearUserMustChangePassword($gapps, true, $_POST['user']);
                    endHTML(true);
                } else {
                    header('HTTP/1.1 400 Bad Request');
                    startHTML();
                    echo "<h2>Invalid mode.</h2>\n";
                    echo "<p>Please check your request and try again.</p>";
                    endHTML(true);
                }
            case 'deleteUser':
                startHTML();
                deleteUser($gapps, true, $_POST['user']);
                endHTML(true);
            case 'createNickname':
                startHTML();
                createNickname($gapps, true, $_POST['user'],
                    $_POST['nickname']);
                endHTML(true);
            case 'deleteNickname':
                startHTML();
                deleteNickname($gapps, true, $_POST['nickname']);
                endHTML(true);
            case 'createEmailList':
                startHTML();
                createEmailList($gapps, true, $_POST['emailList']);
                endHTML(true);
            case 'deleteEmailList':
                startHTML();
                deleteEmailList($gapps, true, $_POST['emailList']);
                endHTML(true);
            case 'modifySubscription':
                if ($_POST['mode'] == 'subscribe') {
                    startHTML();
                    addRecipientToEmailList($gapps, true, $_POST['recipient'],
                        $_POST['emailList']);
                    endHTML(true);
                } elseif ($_POST['mode'] == 'unsubscribe') {
                    startHTML();
                    removeRecipientFromEmailList($gapps, true,
                        $_POST['recipient'], $_POST['emailList']);
                    endHTML(true);
                } else {
                    header('HTTP/1.1 400 Bad Request');
                    startHTML();
                    echo "<h2>Invalid mode.</h2>\n";
                    echo "<p>Please check your request and try again.</p>";
                    endHTML(true);
                }
        }
    }

    // Check for an invalid command. If so, display an error and exit.
    if (!empty($_REQUEST['command'])) {
        header('HTTP/1.1 400 Bad Request');
        startHTML();
        echo "<h2>Invalid command.</h2>\n";
        echo "<p>Please check your request and try again.</p>";
        endHTML(true);
    }

    // If a menu parameter is available, display a submenu.
    if (!empty($_REQUEST['menu'])) {
        switch ($_REQUEST['menu']) {
            case 'user':
                startHTML();
                displayUserMenu();
                endHTML();
            case 'nickname':
                startHTML();
                displayNicknameMenu();
                endHTML();
            case 'emailList':
                startHTML();
                displayEmailListMenu();
                endHTML();
            case 'logout':
                startHTML(false);
                logout();
                endHTML();
            default:
                header('HTTP/1.1 400 Bad Request');
                startHTML();
                echo "<h2>Invalid menu selection.</h2>\n";
                echo "<p>Please check your request and try again.</p>";
                endHTML(true);
        }
    }

    // If we get this far, that means there's nothing to do. Display
    // the main menu.
    // If no command was issued and no menu was selected, display the
    // main menu.
    startHTML();
    displayMenu();
    endHTML();
}

// ************************** PROGRAM ENTRY POINT **************************

if (!isset($_SERVER["HTTP_HOST"]))  {
    // running through command line
    runCLIVersion($argv, $argc);
} else {
    // running through web server
    try {
        runWWWVersion();
    } catch (Zend_Gdata_Gapps_ServiceException $e) {
        // Try to recover gracefully from a service exception.
        // The HTML prologue will have already been sent.
        echo "<p><strong>Service Error Encountered</strong></p>\n";
        echo "<pre>" . htmlspecialchars($e->__toString()) . "</pre>";
        endHTML(true);
    }
}
Back to Top