From: Neil McKillop Date: Tue, 27 Jun 2023 13:28:11 +0000 (+0100) Subject: Initial commit X-Git-Url: https://git.mckillop.org/gitweb/?a=commitdiff_plain;h=e8f8c5217fdfa9a01ff173b9be973806d59c41e4;p=lemmy-api-php Initial commit --- e8f8c5217fdfa9a01ff173b9be973806d59c41e4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4f4773f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +config.php diff --git a/config.example.php b/config.example.php new file mode 100644 index 0000000..c5c8b53 --- /dev/null +++ b/config.example.php @@ -0,0 +1,6 @@ +/api/v3/"; +$username = ""; +$password = ""; +$token_file = ".lemmy-api-token"; +?> diff --git a/lemmy-api.php b/lemmy-api.php new file mode 100755 index 0000000..65ad258 --- /dev/null +++ b/lemmy-api.php @@ -0,0 +1,88 @@ +#!/usr/bin/php + "PERSONID [reason]", + "deletePost" => "POSTID" +); + +if(empty($argv[1]) or empty($argv[2])) { + echo "Usage: ./".basename(__FILE__)." COMMAND [parameters]\n"; + echo "You can get help on a specific command with ./".basename(__FILE__)." help COMMAND\n"; + echo "Available commands: ".implode(", ", array_keys($implemented_commands))."\n"; + exit(0); +} else { + $selected_command = $argv[1]; +} + +// generate token, or load one from the cache file +if(!file_exists($token_file)) { + echo "Logging in for the first time and storing token..."; + $data = json_decode(http_post($lemmy_api_url."user/login", array('username_or_email'=>$username, 'password'=>$password)), true); + $token = $data['jwt']; + if(!empty($token)) { + echo "OK"; + file_put_contents($token_file, $token); + } else { + echo "Error logging in."; + } + echo "\n"; +} else { + $token = file_get_contents($token_file); +} + + +switch($selected_command) { + case "help": + if(in_array($argv[2], array_keys($implemented_commands))) { + echo "Usage: ./".basename(__FILE__)." {$argv[2]} ".$implemented_commands[$argv[2]]."\n"; + } else { + echo "Unrecognised command, available commands: ".implode(", ", array_keys($implemented_commands))."\n"; + } + break; + case "purgePerson": + if(sizeof($argv) > 4) + exit("Too many parameters, if supplying a reason, please use enclose with quotation marks\n"); + $person_id = $argv[2]; + $reason = ""; + if(!empty($argv[3])) + $reason = $argv[3]; + echo "Attempting to purge person $person_id..."; + $data = json_decode(http_post($lemmy_api_url."admin/purge/person", array('auth' => $token, 'person_id'=>intval($person_id), 'reason'=>$reason)), true); + $status = "ERROR"; + if(!empty($data['success']) and $data['success'] == 1) + $status = "OK"; + echo $status."\n"; + break; + case "deletePost": + $post_id = $argv[2]; + echo "Attempting to delete post ID $post_id..."; + $data = json_decode(http_post($lemmy_api_url."post/delete", array('auth' => $token, 'post_id'=>intval($post_id), 'deleted'=>true)), true); + $status = "ERROR"; + if(!empty($data['post_view']) and $data['post_view']['post']['deleted'] == 1) + $status = "OK"; + echo $status."\n"; + break; + default: + echo "Unrecognised command"; + break; +} + +function http_post($url, $data) { + $ch = curl_init(); + $payload = json_encode($data); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, + array('Content-Type:application/json') + ); + $result = curl_exec($ch); + if(curl_errno($ch)) + print_r(curl_error($ch)); + return $result; +} + +?>