#!/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; } ?>