adding updated code

This commit is contained in:
Janis Steiner
2024-06-18 09:36:11 +01:00
parent 0aaa1964fb
commit 0d7038c4b6
29 changed files with 4350 additions and 0 deletions

66
sys0-code/api/queue.php Normal file
View File

@@ -0,0 +1,66 @@
<?php
function test_queue($link)//function to check if any printer is free and if there are jobs in queue. If yes push jo to printer
{
$sql="select id, from_userid,filepath,print_on from queue order by id";
$qid=0;
$quserid=0;
$qfilepath="";
$print_on=0;
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $qid,$quserid,$qfilepath,$print_on);
mysqli_stmt_fetch($stmt);
$num_of_printers=0;
if($print_on==-1)
$sql="select count(*) from printer";
else
$sql="select count(*) from printer where id=$print_on";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $num_of_printers);
mysqli_stmt_fetch($stmt);
$last_id=0;
$printers_av=0;
if($num_of_printers!=0)
{
$id=0;
$papikey="";
$userid=$_SESSION["id"];
$username=$_SESSION["username"];
$purl="";
if($print_on==-1)
$sql="Select id,apikey,printer_url from printer where id>$last_id and free=1 order by id";
else
$sql="Select id,apikey,printer_url from printer where id=$print_on and free=1";
//echo $sql;
$stmt = mysqli_prepare($link, $sql)mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $id,$papikey,$purl);
mysqli_stmt_fetch($stmt);
if($id!=0)
{
exec('curl -k -H "X-Api-Key: '.$papikey.'" -F "select=true" -F "print=true" -F "file=@'.$qfilepath.'" "'.$purl.'/api/files/local" > /var/www/html/user_files/'.$username.'/json.json');
$fg=file_get_contents("/var/www/html/user_files/$username/json.json");
$json=json_decode($fg,true);
if($json['effectivePrint']==true and $json["effectiveSelect"]==true)
{
$sql="update printer set free=0, printing=1,mail_sent=0, used_by_userid=$quserid where id=$id";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_execute($stmt);
$sql="delete from queue where id=$qid";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_execute($stmt);
}
else
{
}
}
}
}
?>

102
sys0-code/api/sendmail.php Normal file
View File

@@ -0,0 +1,102 @@
<?php
$username="sys0_autonomous";
//no auth, we only check if any printe rhas finished and if yes, if we should send a mail, if yes send the mail.
include "../config/config.php";
include "../api/queue.php";
test_queue($link);
//iterate over all printers and receive theyr status
$cnt=0;
$url="";
$apikey="";
$sql="select count(*) from printer where printing=1";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $cnt);
mysqli_stmt_fetch($stmt);
//echo($cnt);
$is_free=0;
$last_id=0;
$mail_sent=1;
$used_by_userid=0;
while($cnt!=0)
{
$sql="select free,id,printer_url,apikey,cancel,used_by_userid,mail_sent from printer where id>$last_id and printing=1 ORDER BY id";
$cancel=0;
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $is_free,$printer_id,$url,$apikey,$cancel,$used_by_userid,$mail_sent);
mysqli_stmt_fetch($stmt);
$last_id=$printer_id;
//printer is printing
exec("curl --max-time 10 $url/api/job?apikey=$apikey > /var/www/html/user_files/$username/json.json");
$fg=file_get_contents("/var/www/html/user_files/$username/json.json");
$json=json_decode($fg,true);
$used_by_user="";
$telegram_id="";
$notification_telegram=0;
$notification_mail=0;
$sql="select username,telegram_id,notification_telegram,notification_mail from users where id=$used_by_userid";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
mysqli_stmt_bind_result($stmt, $used_by_user,$telegram_id,$notification_telegram,$notification_mail);
mysqli_stmt_fetch($stmt);
$username3=explode("@",$used_by_user);
$username2=$username3[0];
$progress=(int) $json['progress']['completion'];
if($progress<0)
$progress=-$progress;
$file=$json['job']['file']['name'];
if($progress==100){
//print finished
//check if mail has not been sent:
if($mail_sent==0 && $notification_telegram==1){
//send telegram message
echo("sending telegram for printer $printer_id<br>");
$text = urlencode("Hi $username2\nDein Druck auf Drucker $printer_id ist fertig\nDatei, welche du gedruckt hast: $file\n");
exec("curl \"https://api.telegram.org/$api/sendMessage?chat_id=$telegram_id&text=$text\"");
$sql="update printer set mail_sent=1 where id=$printer_id";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_execute($stmt);
}
if($mail_sent==0 && $notification_mail==1)
{
echo("sending mail for printer $printer_id<br>");
$mail=<<<EOF
curl --request POST \
--url https://api.sendgrid.com/v3/mail/send \
--header "Authorization: Bearer $SENDGRID_API_KEY" \
--header 'Content-Type: application/json' \
--data '{"personalizations": [{"to": [{"email": "$used_by_user"}]}],"from": {"email": "$sendgrid_email"},"subject": "3D-Druck $file abholbereit","content": [{"type": "text/html", "value": "Hallo $username2<br>Dein 3D-Druck auf Drucker $printer_id ist fertig.<br>Bitte hole diesen ab und vergiss nicht den Drucker danach freizugeben!<br>Deine Aufträge: <a href='https://app.ksw3d.ch/system0/html/php/login/v3/php/overview.php?private'>https://app.ksw3d.ch/system0/html/php/login/v3/php/overview.php?private</a><br>Datei, welche du gedruckt hast: $file<br><br>Vielen dank für dein Vertrauen in uns!<br>Code Camp 2024<br>"}]}'
EOF;
$out="";
exec($mail,$out);
$sql="update printer set mail_sent=1 where id=$printer_id";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_execute($stmt);
}
}
else if($cancel==1){
//print cancelled
}
//else: print still running
$cnt--;
}
?>

View File

@@ -0,0 +1,41 @@
<?php
include "../config/config.php";
session_start();
if(isset($_GET["telegram_id"])){
$telegram_id=htmlspecialchars($_GET["telegram_id"]);
$id=$_SESSION["id"];
$sql="update users set telegram_id=? where id=?";
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, "si", $telegram_id,$id);
mysqli_stmt_execute($stmt);
$_SESSION["telegram_id"]=$telegram_id;
}
if(isset($_GET["notification_telegram"])){
if($_GET["notification_telegram"]=="true"){
$sql="update users set notification_telegram=1 where id=?";
$_SESSION["notification_telegram"]=1;
}
else{
$sql="update users set notification_telegram=0 where id=?";
$_SESSION["notification_telegram"]=0;
}
$id=$_SESSION["id"];
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, "i" ,$id);
mysqli_stmt_execute($stmt);
}
if(isset($_GET["notification_mail"])){
if($_GET["notification_mail"]=="true"){
$sql="update users set notification_mail=1 where id=?";
$_SESSION["notification_mail"]=1;
}
else{
$sql="update users set notification_mail=0 where id=?";
$_SESSION["notification_mail"]=0;
}
$id=$_SESSION["id"];
$stmt = mysqli_prepare($link, $sql);
mysqli_stmt_bind_param($stmt, "i" ,$id);
mysqli_stmt_execute($stmt);
}
?>