]> WPIA git - gigi.git/blob - doc/jenkinsJob/dyn-txt.php
Merge "upd: remove 'browser install'"
[gigi.git] / doc / jenkinsJob / dyn-txt.php
1 <?php
2 header("Content-type: text/plain");
3
4 define("ZONENAME", "your-zonename");
5 define("KEYNAME", "your/dns/update.key");
6 $appIdentifier = "someca";
7
8 if(!isset($_GET['token']) || !isset($_GET['t1']) || !isset($_GET['t2']) || !isset($_GET['action'])){
9   die("Error");
10 }
11 $link = mysqli_connect("localhost", "db-user", "db-pw", "db");
12 if($_GET['token'] != "your-token-here"){
13   die ();
14 }
15 $t1 = $_GET['t1'];
16 $t2 = $_GET['t2'];
17 if(!preg_match("/^[a-zA-Z0-9]+$/", $t1) || !preg_match("/^[a-zA-Z0-9]+$/", $t2)){
18   die("Error");
19 }
20
21 $dnscalls = "";
22 if($t1!="purge"){
23   $stmt = $link->prepare("INSERT INTO tokens SET type=?, name=?");
24   $stmt->bind_param("ss", $type, $name);
25   $type=$_GET['action'];
26   $name = $t1;
27   if($_GET['action'] == "http"){
28     $stmt->execute();
29
30     file_put_contents(".well-known/$appIdentifier-challenge/$t1.txt", $t2);
31   } else if($_GET['action'] == "dns") {
32     $stmt->execute();
33
34     $dnscalls .= "update delete {$t1}._$appIdentifier._auth." . ZONENAME . " TXT\n"
35       ."update add {$t1}._$appIdentifier._auth." . ZONENAME . " 60 TXT {$t2}\n";
36   }
37 }
38 $stmt = $link->prepare("SELECT type, name FROM tokens WHERE created + 60000 < CURRENT_TIMESTAMP;");
39 $stmt->execute();
40
41 /* bind result variables */
42 $stmt->bind_result($type, $name);
43 $todelete = array();
44
45 /* fetch value */
46 while($stmt->fetch()){
47   if($type == "http"){
48     unlink(".well-known/$appIdentifier-challenge/{$name}.txt");
49   } else if($type == "dns") {
50     $dnscalls .= "update delete {$name}._$appIdentifier._auth." . ZONENAME . " TXT\n";
51   }
52   $todelete[] = array("type"=>$type, "name"=>$name);
53 }
54
55 $stmtd = $link->prepare("DELETE FROM tokens WHERE type=? AND name=?");
56 $stmtd->bind_param("ss", $type, $name);
57
58 foreach($todelete as $val){
59   $type = $val["type"];
60   $name = $val["name"];
61   $stmtd->execute();
62 }
63
64 if($dnscalls != ""){
65   dnsAction($dnscalls);
66 }
67
68 function dnsAction($command) {
69   $call = "server localhost\n$command\nsend\nquit\n";
70
71   $nsupdate = popen("/usr/bin/nsupdate -k " . KEYNAME, 'w');
72   fwrite($nsupdate, $call);
73   $retval = pclose($nsupdate); // nsupdate doesn't return anything useful when called this way
74 }