Subversion Repositories stackman

Rev

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