From 7dd57d4ef7f84194299e061ee2249263e709222e Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Thu, 9 Dec 2004 19:10:57 +0000 Subject: [PATCH] moved lamdaemon function to lamdaemon.inc --- lam/lib/lamdaemon.inc | 91 +++++++++++++++++++++++++++++++++++++++ lam/lib/modules.inc | 55 ++--------------------- lam/lib/modules/quota.inc | 6 +-- lam/templates/delete.php | 3 +- 4 files changed, 99 insertions(+), 56 deletions(-) create mode 100644 lam/lib/lamdaemon.inc diff --git a/lam/lib/lamdaemon.inc b/lam/lib/lamdaemon.inc new file mode 100644 index 00000000..88497abb --- /dev/null +++ b/lam/lib/lamdaemon.inc @@ -0,0 +1,91 @@ +decrypt_login(); + /* $towrite has the following syntax: + * admin-username, admin-password, owner of homedir, 'home', operation='add' + * use escapeshellarg to make exec() shell-safe + */ + $towrite = escapeshellarg($_SESSION['config']->scriptServer)." ".escapeshellarg($_SESSION['config']->scriptPath)." ". + escapeshellarg($ldap_q[0]).' '.escapeshellarg($ldap_q[1]); + + $userstring = implode ("\n", $commands); + if (function_exists(proc_open)) { + // New Code, requires PHP 4.3 + $descriptorspec = array( + 0 => array("pipe", "r"), // stdin + 1 => array("pipe", "w"), // stout + 2 => array("file", "/dev/null", "a") // sterr + ); + $process = proc_open(escapeshellarg($_SESSION['lampath']."lib/lamdaemon.pl")." ".$towrite, + $descriptorspec, + $pipes); + if (is_resource($process)) { + /* perl-script is running + * $pipes[0] is writeable handle to child stdin + * $pipes[1] is readable handle to child stdout + * any error is send to /dev/null + */ + // Write to stdin + fwrite($pipes[0], $userstring); + } + fclose($pipes[0]); + while (!feof($pipes[1])) { + $output = fgets($pipes[1], 1024); + if ($output!='') $output_array[] = $output; + } + fclose($pipes[1]); + proc_close($process); + } + else { // PHP 4.3> + $command = escapeshellarg($_SESSION['lampath']."lib/lamdaemon.pl")." ".$towrite; + $pipe = popen("echo \"$userstring\"|$command" , 'r'); + while(!feof($pipe)) { + //$output .= fread($pipe, 1024); + $output = fgets($pipe, 1024); + if ($output!='') $output_array[] = $output; + } + pclose($pipe); + } + return $output_array; +} + +?> diff --git a/lam/lib/modules.inc b/lam/lib/modules.inc index 276d56a8..166c8a3a 100644 --- a/lam/lib/modules.inc +++ b/lam/lib/modules.inc @@ -39,6 +39,8 @@ include_once("account.inc"); include_once("baseModule.inc"); /** access to LDAP server */ include_once("ldap.inc"); +/** lamdaemon functions */ +include_once("lamdaemon.inc"); /** * This includes all module files. @@ -1424,7 +1426,7 @@ class accountContainer { if (!$stopprocessing) { foreach ($attributes as $DN) { - if (is_array($DN['lamdaemon']['command'])) $result = $this->lamdaemon($DN['lamdaemon']['command']); + if (is_array($DN['lamdaemon']['command'])) $result = lamdaemon($DN['lamdaemon']['command']); // Error somewhere in lamdaemon if (is_array($result)) foreach ($result as $singleresult) { @@ -1442,57 +1444,6 @@ class accountContainer { return 0; } - function lamdaemon($commands) { - // get username and password of the current lam-admin - $ldap_q = $_SESSION['ldap']->decrypt_login(); - /* $towrite has the following syntax: - * admin-username, admin-password, owner of homedir, 'home', operation='add' - * use escapeshellarg to make exec() shell-safe - */ - $towrite = escapeshellarg($_SESSION['config']->scriptServer)." ".escapeshellarg($_SESSION['config']->scriptPath)." ". - escapeshellarg($ldap_q[0]).' '.escapeshellarg($ldap_q[1]); - - $userstring = implode ("\n", $commands); - if (function_exists(proc_open)) { - // New Code, requires PHP 4.3 - $descriptorspec = array( - 0 => array("pipe", "r"), // stdin - 1 => array("pipe", "w"), // stout - 2 => array("file", "/dev/null", "a") // sterr - ); - $process = proc_open(escapeshellarg($_SESSION['lampath']."lib/lamdaemon.pl")." ".$towrite, - $descriptorspec, - $pipes); - if (is_resource($process)) { - /* perl-script is running - * $pipes[0] is writeable handle to child stdin - * $pipes[1] is readable handle to child stdout - * any error is send to /dev/null - */ - // Write to stdin - fwrite($pipes[0], $userstring); - } - fclose($pipes[0]); - while (!feof($pipes[1])) { - $output = fgets($pipes[1], 1024); - if ($output!='') $output_array[] = $output; - } - fclose($pipes[1]); - proc_close($process); - } - else { // PHP 4.3> - $command = escapeshellarg($_SESSION['lampath']."lib/lamdaemon.pl")." ".$towrite; - $pipe = popen("echo \"$userstring\"|$command" , 'r'); - while(!feof($pipe)) { - //$output .= fread($pipe, 1024); - $output = fgets($pipe, 1024); - if ($output!='') $output_array[] = $output; - } - pclose($pipe); - } - return $output_array; - } - /** * * diff --git a/lam/lib/modules/quota.inc b/lam/lib/modules/quota.inc index a66adc87..05182616 100644 --- a/lam/lib/modules/quota.inc +++ b/lam/lib/modules/quota.inc @@ -105,7 +105,7 @@ class quota extends baseModule { // call parent init parent::init($base); // Get basic quotas for new account - $output_array = $_SESSION[$this->base]->lamdaemon(array("+ quota get " . $_SESSION[$this->base]->type)); + $output_array = lamdaemon(array("+ quota get " . $_SESSION[$this->base]->type)); // process quotas if (is_array($output_array)) { $all_quota = explode(':', $output_array[0]); @@ -174,7 +174,7 @@ class quota extends baseModule { if ($_SESSION[$this->base]->type=='user') $id = $attr['uid'][0]; if ($_SESSION[$this->base]->type=='group') $id = $attr['cn'][0]; // Get quotas - $output_array = $_SESSION[$this->base]->lamdaemon(array("$id quota get " . $_SESSION[$this->base]->type)); + $output_array = lamdaemon(array("$id quota get " . $_SESSION[$this->base]->type)); // process quotas if (is_array($output_array)) { $all_quota = explode(':', $output_array[0]); @@ -336,7 +336,7 @@ class quota extends baseModule { */ function get_profileOptions() { // Get quotas - $quotas = $_SESSION[$this->base]->lamdaemon(array("+ quota get " . $this->get_scope())); + $quotas = lamdaemon(array("+ quota get " . $this->get_scope())); $dirs = split(":", $quotas[0]); array_pop($dirs); // remove empty element at the end for ($i = 0; $i < sizeof($dirs); $i++) { diff --git a/lam/templates/delete.php b/lam/templates/delete.php index 88bc0dd2..13f27e7a 100644 --- a/lam/templates/delete.php +++ b/lam/templates/delete.php @@ -31,6 +31,7 @@ include_once('../lib/profiles.inc'); // functions to load and save profiles include_once('../lib/status.inc'); // Return error-message include_once('../lib/pdf.inc'); // Return a pdf-file include_once('../lib/ldap.inc'); // LDAP-functions +include_once('../lib/lamdaemon.inc'); // lamdaemon /* We have to include all modules * before start session @@ -201,7 +202,7 @@ if ($_POST['delete']) { } if (!$stopprocessing) { foreach ($attributes as $DN) { - if (is_array($DN['lamdaemon']['command'])) $result = $_SESSION['account']->lamdaemon($DN['lamdaemon']['command']); + if (is_array($DN['lamdaemon']['command'])) $result = lamdaemon($DN['lamdaemon']['command']); // Error somewhere in lamdaemon foreach ($result as $singleresult) { if (is_array($singleresult)) {