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