How would I call this code (below) once in my php script for all services that need to be done on transactions Create account/enable/suspend/delete google accounts. Right now I have this code in every PHP function. I tried putting this in an include file but that didn't work. If I could set this once globally that would be nice. That way I would have to keep getting connected which slows down the transaction process.
(RESUED CODE)
// load classes
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Http_Client');
$user = "isuadmin@gdev.isu.edu";
$pass = "*********";
$domain = "gdev.isu.edu";
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, Zend_Gdata_Gapps::AUTH_SERVICE_NAME);
// $service = new Zend_Gdata_Gapps($client, $domain);
(ALL CODE)
#!/usr/bin/php -q
<?php
$loop_id = 1;
while ($loop_id > 0) {
$fileshutdown = '/home/acctmnt/gmail-SHUTDOWN';
if (file_exists($fileshutdown)) {
exit();
}
// include ("globals.inc");
$error_flag = "no";
$err_message = "";
$path = '/home/wheakory/ZendGdata-1.5.3/library/Zend';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
$files1 = glob("/home/acctmnt/acctFiles/*.m1ad");
$files2 = glob("/home/acctmnt/acctFiles/*.m1ds");
$files3 = glob("/home/acctmnt/acctFiles/*.m1en");
$files4 = glob("/home/acctmnt/acctFiles/*.m1pg");
$files5 = glob("/home/acctmnt/acctFiles/*.m1ps");
// Call functions to begin Transaction process
loadclass();
if ($files1 != null) {
gmailAdd($files1);
}
if ($files2 != null) {
gmailDisable($files2);
}
if ($files3 != null) {
gmailEnable($files3);
}
if ($files4 != null) {
gmailPurge($files4);
}
sleep(10);
} //loop
function gmailAdd($files1)
{
// load classes
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Http_Client');
$user = "isuadmin@gdev.isu.edu";
$pass = "******";
$domain = "gdev.isu.edu";
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, Zend_Gdata_Gapps::AUTH_S
ERVICE_NAME);
// $service = new Zend_Gdata_Gapps($client, $domain);
$error_flag = "no";
$err_message = "";
foreach ($files1 as $filename){
if (is_file($filename)) {
// echo "$filename\n";
$fp = fopen($filename, "r");
while ($line = fgets($fp))
{
$username = strtok($line, ":");
$familyname = strtok(":");
$givenname = strtok(":");
$passwordname = strtok(":");
echo "$filename\n";
echo "<b>User Name:</b> $username\n";
echo "\n";
// construct event object
// save to server
try {
echo "create user<br/>";
$quota = "50M";
$service->createUser($username, $familyname, $givenname, $passwordname, $passwordHash
Function=null, $quota=null);
}
catch (Zend_Gdata_Gapps_ServiceException $e) {
if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_EXISTS)) {
// If this is an ENTITY_DOES_NOT_EXIST error, return null
echo "user $username already EXISTS\n>";
$err_message = $err_message . "Already EXISTS \r\n";
$error_flag = "yes";
}
elseif ($e->hasError(Zend_Gdata_Gapps_Error::USER_DELETED_RECENTLY)) {
// If this is an ENTITY_DOES_NOT_EXIST error, return null
$err_message = $err_message . "Email Account Deleted Recently cannot create until 5 day
s \r\n";
$error_flag = "yes";
}
elseif ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_NAME_IS_RESERVED)) {
// If this is an ENTITY_RESERVED error, return null
echo "Email Account Name Reserved \r\n";
$err_message = $err_message . "Email Account Name Reserved \r\n";
$error_flag = "yes";
}
else {
// Outherwise, just print the errors that occured and exit
foreach ($e->getErrors() as $error) {
echo "Error encountered: {$error->getReason()} ({$error->getErrorCode()
})\n";
} // foreach
exit();
} // if ENTITY EXISTS
} // Catch
echo "user $error_flag \n>";
if ($error_flag == "yes") {
// define current time
$time = date('H:i:s');
// define the current date (it will be appended to the log file name)
$today = date('Y-m-d');
// if the file does not exist, attempt to create it
$file = fopen("/home/acctmnt/logs/acctlog.txt", 'a') or die("can't open file");
fwrite($file,"$filename $today:$time $username $err_message \r\n");
fclose($file);
$error_flag = "no";
$err_message = "";
// unlink($filename);
}else{
// define current time
$time = date('H:i:s');
// define the current date (it will be appended to the log file name)
$today = date('Y-m-d');
// open log file for writing only; place the file pointer at the end of the file
// if the file does not exist, attempt to create it
// $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");
$file = fopen("/home/acctmnt/logs/acctlog.txt", 'a') or die("can't open file");
fwrite($file,"$filename $today:$time $username Email Account Successful \r\n");
fclose($file);
$error_flag = "no";
$err_message = "";
} // error flag
} // while loop
exec("/bin/rm $filename");
}// IF FILE
} // foreach
} // End function
function gmailDisable($files2)
{
// load classes
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Http_Client');
$user = "isuadmin@gdev.isu.edu";
$pass = "*******";
$domain = "gdev.isu.edu";
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, Zend_Gdata_Gapps::AUTH_SE
RVICE_NAME);
$service = new Zend_Gdata_Gapps($client, $domain);
$error_flag = "no";
$err_message = "";
foreach ($files2 as $filename){
if (is_file($filename)) {
// echo "$filename\n";
$fp = fopen($filename, "r");
while ($line = fgets($fp))
{
$username = strtok($line, ":");
$username2 = $username;
$username3 = "";
$query = $service->newUserQuery($username);
try {
$username3 = $service->getUserEntry($query);
} catch (Zend_Gdata_Gapps_ServiceException $e) {
// Set the user to null if not found
if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
$username = NULL;
} else {
throw $e;
} // ENTITY_DOES_NOT_EXIST
}
// construct event object
// save to server
if ($username == null) {
echo "$username2 does exist no need to suspend\n";
$error_flag = "yes";
}else{
try {
echo "Suspend user<br/>";
echo "$username\n";
$service->suspendUser($username);
} // try
catch (Zend_Gdata_Gapps_ServiceException $e) {
if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
// If this is an ENTITY_DOES_NOT_EXIST error, return null
echo "Delete user $username failed not there\n>";
$error_flag = "yes";
}
else {
// Outherwise, just print the errors that occured and exit
foreach ($e->getErrors() as $error) {
echo "Error encountered: {$error->getReason()} ({$error->getErrorCode()})\n";
} // Foreach
exit();
} // if ENTITY_DOES_NOT_EXIST
} // CATCH
} // IF username == nulll
echo "3\n";
// may have to undo }
if ($error_flag == "yes") {
echo "user $username NOT EXISTS\n>";
// define current time
$time = date('H:i:s');
// define the current date (it will be appended to the log file name)
$today = date('Y-m-d');
// open log file for writing only; place the file pointer at the end of the file
// if the file does not exist, attempt to create it
$file = fopen("/home/acctmnt/logs/acctlog.txt", 'a') or die("can't open file");
fwrite($file,"$filename $today:$time $username2 Disabled attempt failed no such user
\r\n");
fclose($file);
$error_flag = "no";
// unlink($filename);
}else{
// define current time
$time = date('H:i:s');
// define the current date (it will be appended to the log file name)
$today = date('Y-m-d');
// open log file for writing only; place the file pointer at the end of the file
// if the file does not exist, attempt to create it
// $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");
$file = fopen("/home/acctmnt/logs/acctlog.txt", 'a') or die("can't open file");
fwrite($file,"$filename $today:$time $username2 Disable Successful \r\n");
fclose($file);
// unlink($filename);
} // error flag
} // While Loop
exec("/bin/rm $filename");
} // IF FILE
} // End Foreach
} // End Function Disable
function gmailEnable($files3)
{
// load classes
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Http_Client');
$user = "isuadmin@gdev.isu.edu";
$pass = "*******";
$domain = "gdev.isu.edu";
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, Zend_Gdata_Gapps::AUTH_SE
RVICE_NAME);
$service = new Zend_Gdata_Gapps($client, $domain);
$error_flag = "no";
$err_message = "";
foreach ($files3 as $filename){
if (is_file($filename)) {
$fp = fopen($filename, "r");
while ($line = fgets($fp))
{
echo "$filename\n";
$username = strtok($line, ":");
$username2 = $username;
echo "<b>User Name:</b> $username<br>";
$username3 = "";
$query = "";
$query = $service->newUserQuery($username);
try {
$username3 = $service->getUserEntry($query);
} catch (Zend_Gdata_Gapps_ServiceException $e) {
// Set the user to null if not found
if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
$username = NULL;
} else {
throw $e;
} // ENTITY_DOES_NOT_EXIST
echo "$username\n";
}
// construct event object
// save to server
if ($username == null) {
echo "$username2 does exist no need to Enable\n";
$error_flag = "yes";
}else{
// construct event object
// save to server
try {
echo "Restore user<br/>";
$service->restoreUser($username);
} // try
catch (Zend_Gdata_Gapps_ServiceException $e) {
if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
// If this is an ENTITY_DOES_NOT_EXIST error, return null
echo "Enable user $username failed not there\n>";
$error_flag = "yes";
}
else {
// Outherwise, just print the errors that occured and exit
foreach ($e->getErrors() as $error) {
echo "Error encountered: {$error->getReason()} ({$error->getErrorCode()}
)\n";
} // Foreach
exit();
} // if ENTITY_DOES_NOT_EXIST
} // CATCH
} // IF username == nulll
// may have to undo }
if ($error_flag == "yes") {
echo "user $username NOT EXISTS\n>";
// define current time
$time = date('H:i:s');
// define the current date (it will be appended to the log file name)
$today = date('Y-m-d');
// open log file for writing only; place the file pointer at the end of the file
// if the file does not exist, attempt to create it
// $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");
$file = fopen("/home/acctmnt/logs/acctlog.txt", 'a') or die("can't open file");
fwrite($file,"$filename $today:$time $username2 Enable attempt failed no such user\r
\n"
);
fclose($file);
$error_flag = "no";
// unlink($filename);
}else{
// define current time
$time = date('H:i:s');
// define the current date (it will be appended to the log file name)
$today = date('Y-m-d');
// open log file for writing only; place the file pointer at the end of the file
// if the file does not exist, attempt to create it
// $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");
$file = fopen("/home/acctmnt/logs/acctlog.txt", 'a') or die("can't open file");
fwrite($file,"$filename $today:$time $username2 Enable Successful \r\n");
fclose($file);
// unlink($filename);
} // error flag
} // While loop
exec("/bin/rm $filename");
} // IF FILE
}// foreach loop
} // End function
function gmailPurge($files4)
{
// load classes
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Gapps');
Zend_Loader::loadClass('Zend_Http_Client');
$user = "isuadmin@gdev.isu.edu";
$pass = "*******";
$domain = "gdev.isu.edu";
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, Zend_Gdata_Gapps::AUTH_SE
RVICE_NAME);
$service = new Zend_Gdata_Gapps($client, $domain);
$error_flag = "no";
$err_message = "";
foreach ($files4 as $filename){
if (is_file($filename)) {
$fp = fopen($filename, "r");
while ($line = fgets($fp))
{
echo "$filename\n";
$username = strtok($line, ":");
echo "<b>User Name:</b> $username<br>";
$username2 = $username;
$username3 = "";
$query = "";
$query = $service->newUserQuery($username);
try {
$username3 = $service->getUserEntry($query);
} catch (Zend_Gdata_Gapps_ServiceException $e) {
// Set the user to null if not found
if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
$username = NULL;
} else {
throw $e;
} // ENTITY_DOES_NOT_EXIST
echo "$username\n";
}
// construct event object
// save to server
if ($username == null) {
$error_flag = "yes";
}else{
// construct event object
// save to server
try{
echo "Delete user\n>";
$service->deleteUser($username);
}
catch (Zend_Gdata_Gapps_ServiceException $e) {
if ($e->hasError(Zend_Gdata_Gapps_Error::ENTITY_DOES_NOT_EXIST)) {
// If this is an ENTITY_DOES_NOT_EXIST error, return null
echo "Delete user $username failed not there\n>";
$error_flag = "yes";
}
else {
// Outherwise, just print the errors that occured and exit
foreach ($e->getErrors() as $error) {
echo "Error encountered: {$error->getReason()} ({$error->getErrorCode()})\n";
} // Foreach
exit();
} // if ENTITY_DOES_NOT_EXIST
} // CATCH
} // IF username == NULL
if ($error_flag == "yes") {
echo "user $username NOT EXISTS\n>";
// define current time
$time = date('H:i:s');
// define the current date (it will be appended to the log file name)
$today = date('Y-m-d');
// open log file for writing only; place the file pointer at the end of the file
// if the file does not exist, attempt to create it
// $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");
$file = fopen("/home/acctmnt/logs/acctlog.txt", 'a') or die("can't open file");
fwrite($file,"$filename $today:$time $username2 Delete attempt failed no such user\r
\n"
);
fclose($file);
$error_flag = "no";
// unlink($filename);
}else{
// define current time
$time = date('H:i:s');
// define the current date (it will be appended to the log file name)
$today = date('Y-m-d');
// open log file for writing only; place the file pointer at the end of the file
// if the file does not exist, attempt to create it
// $this->fp = fopen($lfile . '_' . $today, 'a') or exit("Can't open $lfile!");
$file = fopen("/home/acctmnt/logs/acctlog.txt", 'a') or die("can't open file");
fwrite($file,"$filename $today:$time $username2 Delete Successful \r\n");
fclose($file);
// unlink($filename);
} // error flag
} // While Loop
// unlink($filename);
exec("/bin/rm $filename");
} // IF FILE
}// foreach loop
} // End function