Subversion Repositories stackman

Rev

Rev 7 | 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
 
4 daniel-mar 76
if (isset($stam_cfg['weblog_host']) && !empty($stam_cfg['weblog_host'])) {
77
	db_close();
78
 
79
	$weblog_host = $stam_cfg['weblog_host'];
80
	$weblog_user = $stam_cfg['weblog_user'];
81
	$weblog_pass = $stam_cfg['weblog_pass'];
82
	$weblog_base = $stam_cfg['weblog_base'];
83
 
84
	if (!db_connect($weblog_host, $weblog_user, $weblog_pass)) {
85
		fwrite(STDERR, "Weblog MySQL connect error\n");
86
		exit(2);
87
	}
88
 
89
	if (!db_select_db($weblog_base)) {
90
		fwrite(STDERR, "Weblog MySQL DB select error\n");
6 daniel-mar 91
		db_close();
4 daniel-mar 92
		exit(2);
93
	}
94
 
95
	$ent = rtrim($argv[1]);
96
 
97
	if (isset($stam_cfg['weblog_password']) && !empty($stam_cfg['weblog_password'])) {
98
		$iv = 'AAAAAAAAAAAAAAAA';
99
		$ent = base64_encode(openssl_encrypt($ent, 'AES-128-CBC', $stam_cfg['weblog_password'], OPENSSL_RAW_DATA, $iv));
100
	}
101
 
102
	$x = db_query("INSERT INTO stam_weblog (ts, inp) VALUES (
103
		NOW(),
104
		'".db_real_escape_string($ent)."'
105
		)");
106
	if (!$x) {
107
		fwrite(STDERR, db_error()."\n");
108
		db_close();
109
		exit(1);
110
	}
111
 
112
	$id = db_insert_id();
113
	if (!$id) {
114
		fwrite(STDERR, "Error: Could not insert\n");
115
		db_close();
116
		exit(1);
117
	}
118
 
5 daniel-mar 119
	echo green("Weblog written\n");
4 daniel-mar 120
}
121
 
122
# ---
123
 
124
function green($txt) {
125
	return "\033[1;32;40m".$txt."\033[0m";
126
}