-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharmo.php
More file actions
109 lines (95 loc) · 3.13 KB
/
Copy patharmo.php
File metadata and controls
109 lines (95 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace armo;
class armo {
// use also sql
const databaseName = 'mcc_irc';
const users = 'users';
const messages = 'quotes';
const extendToSql = true;
private $data;
private $isStatic;
public function __construct($user) {
$fileName = __DIR__ . '/quotes/' . $user . '.json';
if (!file_exists($fileName) && !self::extendToSql) {
throw new \Exception("Citations for '$user' do not exist.", 310);
} elseif (!file_exists($fileName)) {
// try from database
$db = self::getDB();
$data = $db->getById(self::users, $user, 'username', 'id');
if (!isset($data['id'])) {
throw new \Exception("Citations for '$user' do not exist.", 310);
}
// now fetch quotes
$query = $db->prepare('select message, UNIX_TIMESTAMP(timestamp) as timestamp from ' . self::messages . ' where userId=:userId order by timestamp desc');
$query->bindValue(':userId', $data['id'], \PDO::PARAM_INT);
$this->data = $db->getData($query);
$this->isStatic = false;
if (count($this->data) == 0) {
throw new \Exception("Citations for '$user' do not exist.", 310);
}
} else {
$this->data = json_decode(file_get_contents($fileName), true);
$this->isStatic = true;
}
}
// static methods doesn't include now sql
static public function available($getAmounts = false) {
$who = glob(__DIR__ . '/quotes/*.json');
$av = array();
foreach ($who as $val) {
$name = str_replace(array(__DIR__ . '/quotes/', '.json'), '', $val);
if ($getAmounts) {
$temp = json_decode(file_get_contents($val), true);
$name .= ' (' . count($temp) . ')';
}
array_push($av, $name);
}
return $av;
}
static function save($username, $quote) {
if (self::extendToSql) {
// This is about saving to database
$db = self::getDB();
$data = $db->getById(self::users, $username, 'username', 'id');
if (!isset($data['id'])) {
$db->insert(self::users, array('username' => $username));
$userId = $db->getLastInsertId();
} else {
$userId = $data['id'];
}
$db->insert(self::messages, array('userId' => $userId,
'message' => $quote));
} else {
$fileName = __DIR__ . '/quotes/' . $username . '.json';
if (!file_exists($fileName)) {
$data = array($quote);
} else {
$data = json_decode(file_get_contents($fileName), true);
array_push($data, $quote);
}
file_put_contents($fileName, json_encode($data, JSON_PRETTY_PRINT));
}
}
// public methods
public function get($ind = null) {
if (is_null($ind) || $ind > $this->getMaxInd()) {
$ind = rand(0, $this->getMaxInd());
}
if ($this->isStatic){
return $this->data[$ind];
}
else {
return '"' . $this->data[$ind]['message'] . '" ' . date('D d.m.Y H:i:s',$this->data[$ind]['timestamp']) . ' UTC';
}
}
public function getMaxInd() {
return ($this->getSum() - 1);
}
public function getSum() {
return count($this->data);
}
static private function getDB() {
return new \database\sql(self::databaseName);
}
}
?>