-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxSYS.pm
More file actions
186 lines (150 loc) · 3.71 KB
/
xSYS.pm
File metadata and controls
186 lines (150 loc) · 3.71 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/perl
use Data::UUID;
use xComment;
use TIPUS_ELEMENT;
package xSYS;
my $ug = new Data::UUID;
my %requestKeys = ();
my $publicIP = 0;
my $nginxSitePath = '';
my $APIVersion = 0;
my $globalVersion = 0;
my %applicationKeys = (
'c740f63ca8184204a983dfee6dbd807f35118975d9e346aaa6032306cec0e254' => 001, #0.01
);
sub initialize
{
# Obtenir variables
$publicIP = `ip addr | awk '/inet / {sub(/\\/.*/, "", \$2); print \$2}' | tail -1`;
$nginxSitePath = `cat /etc/nginx/sites-available/emuseum | awk '/root / { print \$2 }' | head -1`;
# Borrat espais, salts de linia, el que sigui
$publicIP =~ s/\s+$//;
$nginxSitePath =~ s/;\s+$//;
my $version = xDB::queryReturnAll('STMT_VERSION');
if (scalar @{ $version } == 0) # No rows
{
return 0;
}
foreach my $row (@{ $version })
{
if ($row->[0] eq 'API')
{
$APIVersion = $row->[1];
}
elsif ($row->[0] eq 'Global')
{
$globalVersion = $row->[1];
}
}
return $publicIP != 0 && $nginxSitePath ne '' && $APIVersion != 0 && $globalVersion != 0;
}
sub getWebsite
{
return 'http://' . $publicIP . '/';
}
sub genKey
{
return substr($ug->create_hex(), 2) . substr($ug->create_hex(), 2);
}
sub newRequestKey
{
my $client = shift;
my $key = xSYS::genKey();
$requestKeys{$key} = $client;
return $key;
}
sub useRequestKey
{
my $key = shift;
if (exists $requestKeys{$key})
{
my $client = $requestKeys{$key};
delete $requestKeys{$key};
return $client;
}
return undef;
}
sub existsAppKey
{
my $key = shift;
if (!exists $applicationKeys{$key})
{
return 0;
}
return $applicationKeys{$key};
}
sub checkGlobalVersion
{
my $version = shift;
if ($version != $globalVersion)
{
return 0;
}
return 1;
}
sub getDBURL()
{
return 'http://' . $publicIP . '/data/emuseum-' . $globalVersion . '.db'
}
sub getComments
{
my($id, $type, $from) = @_;
my $comments = xDB::queryReturnAll('STMT_GET_COMMENTS', $id, $type, $from);
if (scalar @{ $comments } == 0) # No rows
{
return '{}';
}
my $json = '';
foreach my $comment (@{ $comments })
{
if (length($json) > 0)
{
$json .= ',';
}
my $comment = new xComment($comment->[0], $comment->[1], $comment->[2], $comment->[3]);
$json .= $comment->get();
}
return '[' . $json . ']';
}
sub getRating
{
my($id, $type) = @_;
if ($type == TIPUS_ELEMENT::TIPUS_OBRA)
{
my @r = xDB::queryWithReturn('STMT_GET_RATING_OBRA', $id);
return $r[0];
}
elsif ($type == TIPUS_ELEMENT::TIPUS_AUTOR)
{
my @r = xDB::queryWithReturn('STMT_GET_RATING_AUTOR', $id);
return $r[0];
}
elsif ($type == TIPUS_ELEMENT::TIPUS_MUSEU)
{
my @r = xDB::queryWithReturn('STMT_GET_RATING_MUSEU', $id);
return $r[0];
}
else
{
$::errno = 2;
$::errmsg = xLang::get($self->{lang}, 'USER_COMMENT_TYPE_ERROR');
return 0;
}
return 0;
}
sub generateNewVersion
{
# Nova versió
my $newVersion = $globalVersion + 1;
my $dbName = 'emuseum-' . $newVersion . '.db';
# Guardar en DB el número de la nova versió
xDB::doQuery('STMT_RISE_VERSION', $newVersion);
# Generar la nova db en sqlite
`./mysql2sqlite.sh -u emuseum_u -p123qwe emuseum e_version e_museus e_autors e_obres | sed 's/ collate utf8_unicode_ci/ /gI' | sqlite3 $dbName`;
# Moure a la carpeta corresponent
`mv $dbName $nginxSitePath/data/`;
# Agugmentar la versió
$globalVersion++;
}
1
__END__