case 'like':
$this->requireValidHost();
- $cmd = new Command_Like($this->cfg->host);
+ $cmd = new Command_Like($this->cfg);
$cmd->run($res->command->args['url']);
break;
case 'reply':
$this->requireValidHost();
- $cmd = new Command_Reply($this->cfg->host);
+ $cmd = new Command_Reply($this->cfg);
$cmd->run(
$res->command->args['url'],
implode(' ', $res->command->args['text'])
if ($opts['user'] !== null) {
$this->cfg->host->user = $opts['user'];
}
+ $this->cfg->setDebug($opts['debug']);
return $res;
} catch (\Exception $exc) {
'default' => null,
)
);
+ $optParser->addOption(
+ 'debug',
+ array(
+ 'short_name' => '-d',
+ 'long_name' => '--debug',
+ 'description' => 'Verbose output',
+ 'action' => 'StoreTrue',
+ 'default' => false,
+ )
+ );
$cmd = $optParser->addCommand('connect');
$cmd->addOption(
class Command_Like
{
/**
- * @var Config_Host
+ * @var Config
*/
- protected $host;
+ protected $cfg;
- public function __construct($host)
+ public function __construct($cfg)
{
- $this->host = $host;
+ $this->cfg = $cfg;
}
public function run($url)
]
);
- $req = new Request($this->host);
+ $req = new Request($this->cfg->host, $this->cfg);
$res = $req->send($body);
$postUrl = $res->getHeader('Location');
- echo "Like created at server\n";
- echo $postUrl . "\n";
+ if ($postUrl === null) {
+ Log::err('Error: Server sent no "Location" header and said:');
+ Log::err($res->getBody());
+ exit(20);
+ } else {
+ echo "Like created at server\n";
+ echo $postUrl . "\n";
+ }
}
}
?>
class Command_Reply
{
/**
- * @var Config_Host
+ * @var Config
*/
- protected $host;
+ protected $cfg;
- public function __construct($host)
+ public function __construct($cfg)
{
- $this->host = $host;
+ $this->cfg = $cfg;
}
public function run($url, $text)
]
);
- $req = new Request($this->host);
+ $req = new Request($this->cfg->host, $this->cfg);
$res = $req->send($body);
$postUrl = $res->getHeader('Location');
echo "Reply created at server\n";
*/
public $host;
+ public $debug = false;
+
protected function getConfigFilePath()
{
if (!isset($_SERVER['HOME'])) {
}
return '"' . $val . '"';
}
+
+ public function setDebug($debug)
+ {
+ $this->debug = $debug;
+ }
}
?>
class Request
{
public $req;
+ public $cfg;
- public function __construct($host)
+ public function __construct($host, $cfg)
{
+ $this->cfg = $cfg;
$this->req = new \HTTP_Request2($host->endpoints->micropub, 'POST');
+ $this->req->setHeader('User-Agent: shpub');
if (version_compare(PHP_VERSION, '5.6.0', '<')) {
//correct ssl validation on php 5.5 is a pain, so disable
$this->req->setConfig('ssl_verify_host', false);
$this->req->setConfig('ssl_verify_peer', false);
}
$this->req->setHeader('Content-type', 'application/x-www-form-urlencoded');
- $this->req->setHeader('Authorization', 'Bearer ' . $host->token);
+ $this->req->setHeader('authorization', 'Bearer ' . $host->token);
}
public function send($body)
{
$this->req->setBody($body);
+ if ($this->cfg->debug) {
+ $this->printCurl();
+ }
$res = $this->req->send();
+
if (intval($res->getStatus() / 100) != 2) {
Log::err(
'Server returned an error status code ' . $res->getStatus()
}
return $res;
}
+
+ protected function printCurl()
+ {
+ $command = 'curl';
+ if ($this->req->getMethod() != 'GET') {
+ $command .= ' -X ' . $this->req->getMethod();
+ }
+ foreach ($this->req->getHeaders() as $key => $val) {
+ $command .= ' -H ' . escapeshellarg($key . ': ' . $val);
+ }
+ $command .= ' --data ' . escapeshellarg($this->req->getBody());
+ $command .= ' ' . escapeshellarg((string) $this->req->getUrl());
+
+ echo $command . "\n";
+ }
}
\ No newline at end of file