Skip to content

Commit

Permalink
Fixes in data.php and calc_data_usage.php to write to log file
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Apr 28, 2022
1 parent a9c3b7b commit 3cbeeef
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 9 deletions.
8 changes: 4 additions & 4 deletions bin/calc_data_usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<?php
chdir(dirname(__FILE__));

$include_paths = array('../libs');
$include_paths = array(__DIR__ '/../libs');
set_include_path(get_include_path() . ":" . implode(':',$include_paths));

function my_autoloader($class_name) {
Expand All @@ -12,9 +12,9 @@ function my_autoloader($class_name) {
}
spl_autoload_register('my_autoloader');

require_once '../conf/app.inc.php';
require_once '../conf/config.inc.php';
require_once '../vendor/autoload.php';
require_once __DIR__ . '/../conf/app.inc.php';
require_once __DIR__ . '/../conf/config.inc.php';
require_once __DIR__ . '/../vendor/autoload.php';

date_default_timezone_set(settings::get_timezone());

Expand Down
11 changes: 6 additions & 5 deletions bin/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
set_include_path(get_include_path() . ":" . implode(':',$include_paths));

function my_autoloader($class_name) {
if(file_exists("../libs/" . $class_name . ".class.inc.php")) {
if(file_exists(__DIR__ . "/../libs/" . $class_name . ".class.inc.php")) {
require_once $class_name . '.class.inc.php';
}
}
spl_autoload_register('my_autoloader');

require_once '../conf/app.inc.php';
require_once '../conf/config.inc.php';
require_once '../vendor/autoload.php';
require_once __DIR__ . '/../conf/app.inc.php';
require_once __DIR__ . '/../conf/config.inc.php';
require_once __DIR__ . '/../vendor/autoload.php';

date_default_timezone_set(settings::get_timezone());

Expand Down Expand Up @@ -53,7 +53,8 @@ function my_autoloader($class_name) {
}
$log_file = new \IGBIllinois\log(settings::get_log_enabled(),settings::get_log_file());
$start_time = microtime(true);
$log_file->send_log("Data Usage: Start");
$log_file->send_log("Data Usage: Start");
var_dump($log_file);
$directories = data_functions::get_all_directories($db);
foreach ($directories as $directory) {
$data_dir = new data_dir($db,$directory['data_dir_id']);
Expand Down
89 changes: 89 additions & 0 deletions bin/disable_users.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env php
<?php
chdir(dirname(__FILE__));

$include_paths = array('../libs');
set_include_path(get_include_path() . ":" . implode(':',$include_paths));

function my_autoloader($class_name) {
if(file_exists("../libs/" . $class_name . ".class.inc.php")) {
require_once $class_name . '.class.inc.php';
}
}
spl_autoload_register('my_autoloader');

require_once '../conf/app.inc.php';
require_once '../conf/config.inc.php';
require_once '../vendor/autoload.php';

date_default_timezone_set(settings::get_timezone());

//Command parameters
$output_command = "data.php Inserts data usage into database\n";
$output_command .= "Usage: php data.php \n";
$output_command .= " --dry-run Do dry run, do not add to database\n";
$output_command .= " -h, --help Display help menu\n";

//Parameters
$shortopts = "h";

$longopts = array(
"dry-run",
"help"
);

//Following code is to test if the script is being run from the command line or the apache server.
$sapi_type = php_sapi_name();
if ($sapi_type != 'cli') {
echo "Error: This script can only be run from the command line.";
}
else {

$options = getopt($shortopts,$longopts);

if (isset($options['h']) || isset($options['help'])) {
echo $output_command;
exit;
}

try {
$db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASSWORD);
} catch (PDOException $e) {
die("Error initializing PDO: " . $e->getMessage());
}
//Sets up ldap connection
$ldap = new \IGBIllinois\ldap(settings::get_ldap_host(),
settings::get_ldap_base_dn(),
settings::get_ldap_port(),
settings::get_ldap_ssl(),
settings::get_ldap_tls());
$log_file = new \IGBIllinois\log(settings::get_log_enabled(),settings::get_log_file());
$users = User::getUsers($db,User::DISABLED);

$devices = Device::getAllDevices($db);
foreach ($users as $user) {
$user_obj = new User($db);
$user_obj->load($user['id']);
$user_obj->setGroupIds(array());
$device_access = array();
foreach ($devices as $device) {
if ($user_obj->hasAccessTo($device['id'])) {
$user_obj->removeAccessTo($device['id']);
}
}

}

$groups = Group::getAllGroups($db);
foreach ($groups as $group) {
$group_obj = new Group($db);
$group_obj->load($group['id']);
$members = $group_obj->getMembers();
if (!count($members)) {
$group_obj->delete();
}

}
}
?>

95 changes: 95 additions & 0 deletions bin/group_folders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env php
<?php
chdir(dirname(__FILE__));

$include_paths = array('../libs');
set_include_path(get_include_path() . ":" . implode(':',$include_paths));

function my_autoloader($class_name) {
if(file_exists("../libs/" . $class_name . ".class.inc.php")) {
require_once $class_name . '.class.inc.php';
}
}
spl_autoload_register('my_autoloader');

require_once '../conf/app.inc.php';
require_once '../conf/config.inc.php';
require_once '../vendor/autoload.php';

date_default_timezone_set(settings::get_timezone());

//Command parameters
$output_command = "data.php Inserts data usage into database\n";
$output_command .= "Usage: php data.php \n";
$output_command .= " --dry-run Do dry run, do not add to database\n";
$output_command .= " -h, --help Display help menu\n";

//Parameters
$shortopts = "h";

$longopts = array(
"dry-run",
"help"
);

//Following code is to test if the script is being run from the command line or the apache server.
$sapi_type = php_sapi_name();
if ($sapi_type != 'cli') {
echo "Error: This script can only be run from the command line.";
}
else {

$options = getopt($shortopts,$longopts);

if (isset($options['h']) || isset($options['help'])) {
echo $output_command;
exit;
}

try {
$db = new PDO("mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASSWORD);
} catch (PDOException $e) {
die("Error initializing PDO: " . $e->getMessage());
}
//Sets up ldap connection
$ldap = new \IGBIllinois\ldap(settings::get_ldap_host(),
settings::get_ldap_base_dn(),
settings::get_ldap_port(),
settings::get_ldap_ssl(),
settings::get_ldap_tls());
$log_file = new \IGBIllinois\log(settings::get_log_enabled(),settings::get_log_file());

$handle = fopen("group_folders.csv","r");
$all_users = User::getUsers($db);
if ($handle) {
while (($line = fgets($handle)) !== false) {
$user_name = trim(rtrim($line));
$path = "/core-server/groups/" . $user_name;
echo $path . "\n";
$user_obj = new user($db);
$sql = "SELECT id FROM users where user_name=:user_name LIMIT 1";
$query = $db->prepare($sql);
$query->execute(array(':user_name'=>$user_name));
$result = $query->fetch(PDO::FETCH_ASSOC);
$user_id = $result['id'];
echo "User ID: " . $user_id . "\n";
$group_sql = "SELECT id FROM groups WHERE netid=:user_name LIMIT 1";
$group_query = $db->prepare($group_sql);
$group_query->execute(array(':user_name'=>$user_name));
$group_result = $group_query->fetch(PDO::FETCH_ASSOC);
$group_id = $group_result['id'];
echo "Group ID: " . $group_id . "\n";
if (!data_dir::get_id_by_directory($db,$path)) {
$insert_sql = "INSERT INTO data_dir(data_dir_group_id,data_dir_user_id,data_dir_path) VALUES(:group_id,:user_id,:path)";

$insert_query = $db->prepare($insert_sql);
$insert_query->execute(array(':group_id'=>$group_id,':user_id'=>$user_id,':path'=>$path));
}


}

}
}
?>

0 comments on commit 3cbeeef

Please sign in to comment.