Subversion Repositories stackman

Rev

Rev 6 | Rev 8 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#!/usr/bin/php
<?php

require_once __DIR__ . '/includes/db.inc.php';
require_once __DIR__ . '/includes/src.inc.php';

if ($argc <> 2) {
        echo "Syntax: $argv[0] <line>\n";
        exit(2);
}

if (isset($stam_cfg['weblog2_system']) && !empty($stam_cfg['weblog2_system'])) {


        $ent = rtrim($argv[1]);

# ---


        $oidplus_rest_auth = $stam_cfg['weblog2_auth'];

        /*
        $data = array();
        $ch = curl_init($stam_cfg['weblog2_system']."rest/v1/objects/oid:".$stam_cfg['weblog2_oid']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer $oidplus_rest_auth"));
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $i=0; for($i=0;$i<10;$i++) { $response = curl_exec($ch); if ($response) { break; } else { sleep(1); } }
        if ((!$response) || (json_decode($response,true)['status'] < 0))
        {
                fwrite(STDERR, "Weblog GET failed: $response\n");
                exit(2);
        }
        #echo "$response\n\n";
        $children = json_decode($response,true)['children'];
        if (count($children) == 0) {
                $lfd = 1;
        } else {
                natsort($children);
                $last = end($children);
                $arcs = explode('.', $last);
                $lfd = end($arcs)+1;
        }
        */

        $lfd = floor(microtime(true)*10000);

        $data = array("title" => "STAM Weblog Entry", "description" => $ent, "ra_email" => "info@daniel-marschall.de");
        $ch = curl_init($stam_cfg['weblog2_system']."rest/v1/objects/oid:".$stam_cfg['weblog2_oid'].".".$lfd);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer $oidplus_rest_auth"));
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $i=0; for($i=0;$i<10;$i++) { $response = curl_exec($ch); if ($response) { break; } else { sleep(1); } }
        if ((!$response) || (json_decode($response,true)['status'] < 0))
        {
                fwrite(STDERR, "Weblog POST failed: $response\n");
                exit(2);
        }
        #echo "$response\n\n";

        echo green("Weblog written (".$stam_cfg['weblog2_oid'].".$lfd)!\n");

# ---





        }



}

if (isset($stam_cfg['weblog_host']) && !empty($stam_cfg['weblog_host'])) {
        db_close();

        $weblog_host = $stam_cfg['weblog_host'];
        $weblog_user = $stam_cfg['weblog_user'];
        $weblog_pass = $stam_cfg['weblog_pass'];
        $weblog_base = $stam_cfg['weblog_base'];

        if (!db_connect($weblog_host, $weblog_user, $weblog_pass)) {
                fwrite(STDERR, "Weblog MySQL connect error\n");
                exit(2);
        }

        if (!db_select_db($weblog_base)) {
                fwrite(STDERR, "Weblog MySQL DB select error\n");
                db_close();
                exit(2);
        }

        $ent = rtrim($argv[1]);

        if (isset($stam_cfg['weblog_password']) && !empty($stam_cfg['weblog_password'])) {
                $iv = 'AAAAAAAAAAAAAAAA';
                $ent = base64_encode(openssl_encrypt($ent, 'AES-128-CBC', $stam_cfg['weblog_password'], OPENSSL_RAW_DATA, $iv));
        }

        $x = db_query("INSERT INTO stam_weblog (ts, inp) VALUES (
                NOW(),
                '".db_real_escape_string($ent)."'
                )");
        if (!$x) {
                fwrite(STDERR, db_error()."\n");
                db_close();
                exit(1);
        }

        $id = db_insert_id();
        if (!$id) {
                fwrite(STDERR, "Error: Could not insert\n");
                db_close();
                exit(1);
        }

        echo green("Weblog written\n");
}

# ---

function green($txt) {
        return "\033[1;32;40m".$txt."\033[0m";
}