]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - Classes/class-notes.php
Hopefully fix #51
[irc/unrealircd/unrealircd-webpanel.git] / Classes / class-notes.php
1 <?php
2
3 /* Make notes against nicks, IPs and account names. */
4
5 class Notes { // OKAY CLASS GET YOUR NOTEPAD OUT
6 /**
7 * Find function for Notes
8 * @param array $query The query to search for -- ["ip" => "127.0.0.1", "nick" => "bob", "account" => "bob", "id" => "lol]
9 * @return array Returns an array of objects (notes)
10 */
11 public static function find(array $query) : array
12 {
13 global $config;
14 read_config_db();
15 if (!isset($config['notes']))
16 return [];
17
18 $notes = [];
19 foreach ($query as $key => $value)
20 {
21 foreach ($config['notes'] as $nkey => $nvalue) // $nkey = "ip" "nick" "account", $nvalue = array
22 {
23 foreach ($nvalue as $k => $n) // $k = "127.0.0.1", "bob", "bobsaccount", $n = array of notes [id => note]
24 {
25 if ($value != $k)
26 continue;
27 $note = [];
28 $note["type"] = $nkey;
29 $note["data"] = $k;
30 $note["notes"] = $n;
31 $notes[$key] = $note;
32 }
33 }
34 }
35 return !empty($notes) ? $notes : [];
36 }
37
38 /**
39 * Add a note to one or more peices of data
40 * @param array ["ip" => "127.0.0.1"]
41 * @param string $note "This is a note"
42 * @return void
43 */
44 public static function add(array $params, string $note)
45 {
46 global $config;
47 read_config_db();
48 foreach ($params as $key => $value)
49 {
50 $id = md5(random_bytes(20)); // note ID (for linking)
51 $config['notes'][$key][$value][$id] = $note;
52 }
53 write_config(); // write db
54 }
55
56 public static function delete_by_id(string $id)
57 {
58 global $config;
59 read_config_db();
60 if (!isset($config['notes']))
61 return NULL;
62
63 foreach ($config['notes'] as $nkey => $nvalue)
64 foreach ($nvalue as $key => $value)
65 if ($value == $id)
66 {
67 unset($config['notes'][$nkey][$key]);
68 break;
69 }
70
71 write_config('notes');
72 }
73 }