123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?
- require("config.php");
- /* This function connects to datebase, where users bills are.
- username and pass used here are my own. And I'm not a mad one!
- It's just necessary.
- Datebase has one table with 2 fields: uid && bill (copecks) */
- function connect_to_DB(){
- mysql_connect($GLOBALS['DB_PARAMS']['host'], $GLOBALS['DB_PARAMS']['user'], $GLOBALS['DB_PARAMS']['password']) or die("Can't connect to the database");
- mysql_select_db($GLOBALS['DB_PARAMS']['name']) or die(mysql_error());
- }
- /* finishs connection to datebase*/
- function close_connection(){
- mysql_close();
- }
- /* it checks if certain user has enough money to pay for a print job.
- the only parameter is uid */
- function check_bill($userid){
- connect_to_DB();
- $query = "SELECT bill FROM users WHERE userid='".$userid."'";
- $res = mysql_query($query);
- while($row = mysql_fetch_array($res)) $bill = $row['bill'];
- close_connection();
- return $bill;
- }
- /* pays money for a print job. It substracts necessary sum of money from the user's bill
- the first parameter is uid
- the second shows the sum to pay
- the third - how much money is there on user's account (before payment has been done)*/
- function pay($userid,$sum_to_pay,$how_much_it_was){
- $query = "UPDATE users SET bill=".(int)($how_much_it_was-$sum_to_pay)." WHERE userid='".$userid."'";
- return mysql_query($query);// or die(mysql_error());
- }
- /* 1. checks if user with such username and pass exists in LDAP
- 2. if exists - gets his uid
- 3. checks if user with such uid exists in MySQL datebase
- 4. if not - creates corresponding record in table users*/
- function check_user($username,$password){
- exec("echo ".trim($password)." | " . UMS_UTILS_PATH . $GLOBALS['UMS_UTILS']['check_passwd'] . " " . trim($username), $output, $err);
- if($err==0){
- exec('echo -e login=' . trim($username) . ' \nvalues=uid | ' . UMS_UTILS_PATH . $GLOBALS['UMS_UTILS']['user_info'], $output,$err);
- foreach($output as $string){$userid = substr(trim($string),4);}
- connect_to_DB();
- $query = "SELECT userid FROM users WHERE userid='".$userid."'";
- $res = mysql_query($query);
- if(mysql_num_rows($res)!=1) {
- mysql_query("INSERT INTO users(userid,bill) VALUES('".$userid."','0')");
- }
- close_connection();
- return $userid;
- }
- else return -1;
- }
- function get_uid_by_username($username){
- exec('echo -e "login='.trim($username).'\nvalues=uid" | ' . UMS_UTILS_PATH . $GLOBALS['UMS_UTILS']['user_info'],$output,$err);
- foreach($output as $string){$userid = substr(trim($string),4);}
- return $userid;
- }
- /* to represent the price of the job properly, it should looks like: hryvnas.copecks */
- function echo_price($to_echo){
- if ((int)(($to_echo % 100)/10) == 0) return (int)($to_echo/100).".0".(int)($to_echo % 10);
- else return (int)($to_echo/100).".".(int)($to_echo % 100);
- }
- /* opens connection to the print server, where some scripts should execute*/
- function connect_to_printserver(){
- $con = ssh2_connect($GLOBALS['PRINT_SERVER_PARAMS']['hostname'], 22) or die("unable to establish connection to " . $GLOBALS['PRINT_SERVER_PARAMS']['hostname']);
- ssh2_auth_password($con, $GLOBALS['PRINT_SERVER_PARAMS']['username'], $GLOBALS['PRINT_SERVER_PARAMS']['password']) or die("fail: unable to authenticate");
- return $con;
- }
- /* counts how many pages are there in the document.
- the first parameter is var with opened connection
- the second isid of the job, whoes number of pages we should count */
- function count_pages($con, $jobid){
- /* jobid should be have five signs.
- if it has less than 5, then some zeros should be added on the front */
- if(!(int)($jobid / 10)) $jobid = "0000".$jobid;
- else if(!(int)($jobid / 100)) $jobid = "000".$jobid;
- else if(!(int)($jobid / 1000)) $jobid = "00".$jobid;
- else if(!(int)($jobid / 10000)) $jobid = "0".$jobid;
- /* special script, which is located on the print server
- and can count number of pages in any document from the print queue */
- if ($stream = ssh2_exec($con, CUPS_UTILS_PATH . $GLOBALS['CUPS_UTILS']['pages'] . $jobid)) {
- // collects returning data from command
- stream_set_blocking($stream, true);
- $data = "";
- while ($buf = fread($stream,4096)) {
- $data .= $buf;
- }
- }
- fclose($stream);
- return $data;
- }
- /* send document with given id from print queue directly to print
- also checks one more time if user has enough money to pay for the service */
- function print_job($ssh_con, $jobid, $price = 0, $userid = 0){
- /* for operators */
- if ($userid == 0 && $price == 0){
- connect_to_DB();
- $stream = ssh2_exec($ssh_con, CUPS_UTILS_PATH . $GLOBALS['CUPS_UTILS']['print'] . " allow ".trim($jobid));
- fclose($stream);
- close_connection();
- } else {
- /* for the rest */
- $how_much_money_we_have = check_bill($userid);
- connect_to_DB();
- if(($how_much_money_we_have>=$price) && pay($userid,$price, $how_much_money_we_have)){
- $stream = ssh2_exec($ssh_con, CUPS_UTILS_PATH . $GLOBALS['CUPS_UTILS']['print'] . " allow ".trim($jobid));
- fclose($stream);
- }
- close_connection();
- }
- }
- /* removes job with given id from the print queue */
- function cancel_job($ssh_con, $jobid){
- $stream = ssh2_exec($ssh_con, CUPS_UTILS_PATH . $GLOBALS['CUPS_UTILS']['print'] . " deny ".trim($jobid));
- fclose($stream);
- }
- /*
- function get_username_by_uid($uid){
- exec("getent passwd ".trim($uid), $output1, $err1);
- foreach($output1 as $string){
- for($i=0; $i<strlen($string) && $string[$i]!=':'; $i++) $username .= $string[$i];
- }
- return $username;
- }*/
- /* returns an array, where the first element is a code of error (it's 0 if no errors)
- and the second is list of print jobs of user with given uid */
- function show_queue($ssh_con, $userid = 0){
- /* for operators */
- if ($userid == 0){
- exec("/home/gloria/public_html/files/usicprint show", $output[1], $output[0]);
- } else {
- /* for the rest */
- exec("getent passwd ".trim($userid), $output1, $err1);
- foreach($output1 as $string){
- for($i=0; $i<strlen($string) && $string[$i]!=':'; $i++) $username .= $string[$i];
- }
- // $username = trim(get_username_by_uid($userid));
- exec("/home/gloria/public_html/files/usicprint show ".$username, $output[1], $output[0]);
- }
- return $output;
- }
- ?>
|