zeg1jeux/lib/Jaklis.class.php

125 lines
2.6 KiB
PHP

<?php
require_once('Fred.class.php');
class Jaklis {
private $mode;
private $jaklisPath = __DIR__ . '/../vendors/jaklisse/jaklis';
private $nodes = [
'gchange' => 'data.gchange.fr'
];
private $msgLimit = 30;
private $userPubsecPath;
public function __construct ($userPubkey, $mode = 'local') {
$this->userPubsecPath = __DIR__ .'/../cache/pubsec/'. $userPubkey .'.dunikey';
if ($this->mode != 'local') {
$this->fred = new Fred();
}
}
public function getMessages () {
if ($this->mode = 'local') {
return [
$this->getInboundMessages(),
$this->getOutboundMessages()
];
} else {
$this->fred->donneMoiSesPutainDeMessagesGchange($_SESSION['salt'], $_SESSION['pepper'], 'coucou');
}
}
public function getInboundMessages () {
$cmd = $this->jaklisPath;
$cmd .= ' --key '. $this->userPubsecPath;
$cmd .= ' --node https://'. $this->nodes['gchange'];
$cmd .= ' read';
$cmd .= ' --number ' . $this->msgLimit;
$cmd .= ' --json';
$output=null;
$result_code=null;
exec($cmd, $output, $result_code);
$json = implode("\n", $output);
// echo '<p>' . $cmd . '</p>';
// echo '<pre>'; var_dump($json); echo '</pre>';
$result = json_decode($json);
// echo '<pre>'; var_dump($result); echo '</pre>';
return $result;
}
public function getOutboundMessages () {
$cmd = $this->jaklisPath;
$cmd .= ' --key '. $this->userPubsecPath;
$cmd .= ' --node http://'. $this->nodes['gchange'];
$cmd .= ' read';
$cmd .= ' --number ' . $this->msgLimit;
$cmd .= ' --json';
$cmd .= ' --outbox';
$output=null;
$result_code=null;
exec($cmd, $output, $result_code);
$json = implode("\n", $output);
// echo '<p>' . $cmd . '</p>';
// echo '<pre>'; var_dump($json); echo '</pre>';
$result = json_decode($json);
// echo '<pre>'; var_dump($result); echo '</pre>';
return $result;
}
private function jescape ($str) {
return str_replace('"', '\"', $str);
}
public function sendMessage ($msg, $toPubkey, $title, $fromPubkey) {
$cmd = $this->jaklisPath;
$cmd .= ' --key '. $this->userPubsecPath;
$cmd .= ' --node http://'. $this->nodes['gchange'];
$cmd .= ' send';
$cmd .= ' --destinataire "' . $this->jescape($toPubkey) . '"';
$cmd .= ' --titre "' . $this->jescape($title) . '"';
$cmd .= ' --message "' . $this->jescape($msg) . '"';
// echo '<pre>'. $cmd . '</pre>';
$output=null;
$result_code=null;
exec($cmd, $output, $result_code);
// echo '<pre>'; var_dump($result_code); echo '</pre>';
// echo '<pre>'; var_dump($output); echo '</pre>';
// list($jaklisResult, $id) = $output;
// echo '<p>'. $jaklisResult .'</p>';
}
}