function_stuff.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?
  2. require("config.php");
  3. /* This function connects to datebase, where users bills are.
  4. username and pass used here are my own. And I'm not a mad one!
  5. It's just necessary.
  6. Datebase has one table with 2 fields: uid && bill (copecks) */
  7. function connect_to_DB(){
  8. mysql_connect($GLOBALS['DB_PARAMS']['host'], $GLOBALS['DB_PARAMS']['user'], $GLOBALS['DB_PARAMS']['password']) or die("Can't connect to the database");
  9. mysql_select_db($GLOBALS['DB_PARAMS']['name']) or die(mysql_error());
  10. }
  11. /* finishs connection to datebase*/
  12. function close_connection(){
  13. mysql_close();
  14. }
  15. /* it checks if certain user has enough money to pay for a print job.
  16. the only parameter is uid */
  17. function check_bill($userid){
  18. connect_to_DB();
  19. $query = "SELECT bill FROM users WHERE userid='".$userid."'";
  20. $res = mysql_query($query);
  21. while($row = mysql_fetch_array($res)) $bill = $row['bill'];
  22. close_connection();
  23. return $bill;
  24. }
  25. /* pays money for a print job. It substracts necessary sum of money from the user's bill
  26. the first parameter is uid
  27. the second shows the sum to pay
  28. the third - how much money is there on user's account (before payment has been done)*/
  29. function pay($userid,$sum_to_pay,$how_much_it_was){
  30. $query = "UPDATE users SET bill=".(int)($how_much_it_was-$sum_to_pay)." WHERE userid='".$userid."'";
  31. return mysql_query($query);// or die(mysql_error());
  32. }
  33. /* 1. checks if user with such username and pass exists in LDAP
  34. 2. if exists - gets his uid
  35. 3. checks if user with such uid exists in MySQL datebase
  36. 4. if not - creates corresponding record in table users*/
  37. function check_user($username,$password){
  38. exec("echo ".trim($password)." | " . UMS_UTILS_PATH . $GLOBALS['UMS_UTILS']['check_passwd'] . " " . trim($username), $output, $err);
  39. if($err==0){
  40. exec('echo -e login=' . trim($username) . ' \nvalues=uid | ' . UMS_UTILS_PATH . $GLOBALS['UMS_UTILS']['user_info'], $output,$err);
  41. foreach($output as $string){$userid = substr(trim($string),4);}
  42. connect_to_DB();
  43. $query = "SELECT userid FROM users WHERE userid='".$userid."'";
  44. $res = mysql_query($query);
  45. if(mysql_num_rows($res)!=1) {
  46. mysql_query("INSERT INTO users(userid,bill) VALUES('".$userid."','0')");
  47. }
  48. close_connection();
  49. return $userid;
  50. }
  51. else return -1;
  52. }
  53. function get_uid_by_username($username){
  54. exec('echo -e "login='.trim($username).'\nvalues=uid" | ' . UMS_UTILS_PATH . $GLOBALS['UMS_UTILS']['user_info'],$output,$err);
  55. foreach($output as $string){$userid = substr(trim($string),4);}
  56. return $userid;
  57. }
  58. /* to represent the price of the job properly, it should looks like: hryvnas.copecks */
  59. function echo_price($to_echo){
  60. if ((int)(($to_echo % 100)/10) == 0) return (int)($to_echo/100).".0".(int)($to_echo % 10);
  61. else return (int)($to_echo/100).".".(int)($to_echo % 100);
  62. }
  63. /* opens connection to the print server, where some scripts should execute*/
  64. function connect_to_printserver(){
  65. $con = ssh2_connect($GLOBALS['PRINT_SERVER_PARAMS']['hostname'], 22) or die("unable to establish connection to " . $GLOBALS['PRINT_SERVER_PARAMS']['hostname']);
  66. ssh2_auth_password($con, $GLOBALS['PRINT_SERVER_PARAMS']['username'], $GLOBALS['PRINT_SERVER_PARAMS']['password']) or die("fail: unable to authenticate");
  67. return $con;
  68. }
  69. /* counts how many pages are there in the document.
  70. the first parameter is var with opened connection
  71. the second isid of the job, whoes number of pages we should count */
  72. function count_pages($con, $jobid){
  73. /* jobid should be have five signs.
  74. if it has less than 5, then some zeros should be added on the front */
  75. if(!(int)($jobid / 10)) $jobid = "0000".$jobid;
  76. else if(!(int)($jobid / 100)) $jobid = "000".$jobid;
  77. else if(!(int)($jobid / 1000)) $jobid = "00".$jobid;
  78. else if(!(int)($jobid / 10000)) $jobid = "0".$jobid;
  79. /* special script, which is located on the print server
  80. and can count number of pages in any document from the print queue */
  81. if ($stream = ssh2_exec($con, CUPS_UTILS_PATH . $GLOBALS['CUPS_UTILS']['pages'] . $jobid)) {
  82. // collects returning data from command
  83. stream_set_blocking($stream, true);
  84. $data = "";
  85. while ($buf = fread($stream,4096)) {
  86. $data .= $buf;
  87. }
  88. }
  89. fclose($stream);
  90. return $data;
  91. }
  92. /* send document with given id from print queue directly to print
  93. also checks one more time if user has enough money to pay for the service */
  94. function print_job($ssh_con, $jobid, $price = 0, $userid = 0){
  95. /* for operators */
  96. if ($userid == 0 && $price == 0){
  97. connect_to_DB();
  98. $stream = ssh2_exec($ssh_con, CUPS_UTILS_PATH . $GLOBALS['CUPS_UTILS']['print'] . " allow ".trim($jobid));
  99. fclose($stream);
  100. close_connection();
  101. } else {
  102. /* for the rest */
  103. $how_much_money_we_have = check_bill($userid);
  104. connect_to_DB();
  105. if(($how_much_money_we_have>=$price) && pay($userid,$price, $how_much_money_we_have)){
  106. $stream = ssh2_exec($ssh_con, CUPS_UTILS_PATH . $GLOBALS['CUPS_UTILS']['print'] . " allow ".trim($jobid));
  107. fclose($stream);
  108. }
  109. close_connection();
  110. }
  111. }
  112. /* removes job with given id from the print queue */
  113. function cancel_job($ssh_con, $jobid){
  114. $stream = ssh2_exec($ssh_con, CUPS_UTILS_PATH . $GLOBALS['CUPS_UTILS']['print'] . " deny ".trim($jobid));
  115. fclose($stream);
  116. }
  117. /*
  118. function get_username_by_uid($uid){
  119. exec("getent passwd ".trim($uid), $output1, $err1);
  120. foreach($output1 as $string){
  121. for($i=0; $i<strlen($string) && $string[$i]!=':'; $i++) $username .= $string[$i];
  122. }
  123. return $username;
  124. }*/
  125. /* returns an array, where the first element is a code of error (it's 0 if no errors)
  126. and the second is list of print jobs of user with given uid */
  127. function show_queue($ssh_con, $userid = 0){
  128. /* for operators */
  129. if ($userid == 0){
  130. exec("/home/gloria/public_html/files/usicprint show", $output[1], $output[0]);
  131. } else {
  132. /* for the rest */
  133. exec("getent passwd ".trim($userid), $output1, $err1);
  134. foreach($output1 as $string){
  135. for($i=0; $i<strlen($string) && $string[$i]!=':'; $i++) $username .= $string[$i];
  136. }
  137. // $username = trim(get_username_by_uid($userid));
  138. exec("/home/gloria/public_html/files/usicprint show ".$username, $output[1], $output[0]);
  139. }
  140. return $output;
  141. }
  142. ?>