Rev 6 | Rev 8 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4 | daniel-mar | 1 | #!/usr/bin/php |
2 | <?php |
||
3 | |||
4 | require_once __DIR__ . '/includes/db.inc.php'; |
||
5 | require_once __DIR__ . '/includes/src.inc.php'; |
||
6 | |||
7 | if ($argc <> 2) { |
||
8 | echo "Syntax: $argv[0] <line>\n"; |
||
9 | exit(2); |
||
10 | } |
||
11 | |||
7 | daniel-mar | 12 | if (isset($stam_cfg['weblog2_system']) && !empty($stam_cfg['weblog2_system'])) { |
13 | |||
14 | |||
15 | $ent = rtrim($argv[1]); |
||
16 | |||
17 | # --- |
||
18 | |||
19 | |||
20 | $oidplus_rest_auth = $stam_cfg['weblog2_auth']; |
||
21 | |||
22 | /* |
||
23 | $data = array(); |
||
24 | $ch = curl_init($stam_cfg['weblog2_system']."rest/v1/objects/oid:".$stam_cfg['weblog2_oid']); |
||
25 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
26 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); |
||
27 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); |
||
28 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer $oidplus_rest_auth")); |
||
29 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
||
30 | $i=0; for($i=0;$i<10;$i++) { $response = curl_exec($ch); if ($response) { break; } else { sleep(1); } } |
||
31 | if ((!$response) || (json_decode($response,true)['status'] < 0)) |
||
32 | { |
||
33 | fwrite(STDERR, "Weblog GET failed: $response\n"); |
||
34 | exit(2); |
||
35 | } |
||
36 | #echo "$response\n\n"; |
||
37 | $children = json_decode($response,true)['children']; |
||
38 | if (count($children) == 0) { |
||
39 | $lfd = 1; |
||
40 | } else { |
||
41 | natsort($children); |
||
42 | $last = end($children); |
||
43 | $arcs = explode('.', $last); |
||
44 | $lfd = end($arcs)+1; |
||
45 | } |
||
46 | */ |
||
47 | |||
48 | $lfd = floor(microtime(true)*10000); |
||
49 | |||
50 | $data = array("title" => "STAM Weblog Entry", "description" => $ent, "ra_email" => "info@daniel-marschall.de"); |
||
51 | $ch = curl_init($stam_cfg['weblog2_system']."rest/v1/objects/oid:".$stam_cfg['weblog2_oid'].".".$lfd); |
||
52 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
53 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
||
54 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); |
||
55 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer $oidplus_rest_auth")); |
||
56 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); |
||
57 | $i=0; for($i=0;$i<10;$i++) { $response = curl_exec($ch); if ($response) { break; } else { sleep(1); } } |
||
58 | if ((!$response) || (json_decode($response,true)['status'] < 0)) |
||
59 | { |
||
60 | fwrite(STDERR, "Weblog POST failed: $response\n"); |
||
61 | exit(2); |
||
62 | } |
||
63 | #echo "$response\n\n"; |
||
64 | |||
65 | echo green("Weblog written (".$stam_cfg['weblog2_oid'].".$lfd)!\n"); |
||
66 | |||
67 | # --- |
||
68 | |||
69 | |||
70 | |||
71 | |||
72 | |||
73 | } |
||
74 | |||
75 | |||
76 | |||
77 | } |
||
78 | |||
4 | daniel-mar | 79 | if (isset($stam_cfg['weblog_host']) && !empty($stam_cfg['weblog_host'])) { |
80 | db_close(); |
||
81 | |||
82 | $weblog_host = $stam_cfg['weblog_host']; |
||
83 | $weblog_user = $stam_cfg['weblog_user']; |
||
84 | $weblog_pass = $stam_cfg['weblog_pass']; |
||
85 | $weblog_base = $stam_cfg['weblog_base']; |
||
86 | |||
87 | if (!db_connect($weblog_host, $weblog_user, $weblog_pass)) { |
||
88 | fwrite(STDERR, "Weblog MySQL connect error\n"); |
||
89 | exit(2); |
||
90 | } |
||
91 | |||
92 | if (!db_select_db($weblog_base)) { |
||
93 | fwrite(STDERR, "Weblog MySQL DB select error\n"); |
||
6 | daniel-mar | 94 | db_close(); |
4 | daniel-mar | 95 | exit(2); |
96 | } |
||
97 | |||
98 | $ent = rtrim($argv[1]); |
||
99 | |||
100 | if (isset($stam_cfg['weblog_password']) && !empty($stam_cfg['weblog_password'])) { |
||
101 | $iv = 'AAAAAAAAAAAAAAAA'; |
||
102 | $ent = base64_encode(openssl_encrypt($ent, 'AES-128-CBC', $stam_cfg['weblog_password'], OPENSSL_RAW_DATA, $iv)); |
||
103 | } |
||
104 | |||
105 | $x = db_query("INSERT INTO stam_weblog (ts, inp) VALUES ( |
||
106 | NOW(), |
||
107 | '".db_real_escape_string($ent)."' |
||
108 | )"); |
||
109 | if (!$x) { |
||
110 | fwrite(STDERR, db_error()."\n"); |
||
111 | db_close(); |
||
112 | exit(1); |
||
113 | } |
||
114 | |||
115 | $id = db_insert_id(); |
||
116 | if (!$id) { |
||
117 | fwrite(STDERR, "Error: Could not insert\n"); |
||
118 | db_close(); |
||
119 | exit(1); |
||
120 | } |
||
121 | |||
5 | daniel-mar | 122 | echo green("Weblog written\n"); |
4 | daniel-mar | 123 | } |
124 | |||
125 | # --- |
||
126 | |||
127 | function green($txt) { |
||
128 | return "\033[1;32;40m".$txt."\033[0m"; |
||
129 | } |