-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackend_utils.lua
164 lines (149 loc) · 5.17 KB
/
backend_utils.lua
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
local db = require 'lapis.db'
function altImageFor (aProject, wantsRaw)
local dir = 'projects/' .. math.floor(aProject.id / 1000) .. '/' .. aProject.id -- we store max 1000 projects per dir
local file = io.open(dir .. '/image.png', 'r')
if (file) then
local image = file:read("*all")
file:close()
return {
layout = false,
status = 200,
readyState = 4,
image
}
else
return {
layout = false,
status = 200,
readyState = 4,
'/static/no-image.png'
}
end
end
function getStats()
function count (tableName, interval, dateField)
local query = 'select count(*) from ' .. tableName
if interval ~= nil then
query = query .. ' where ' .. dateField .. ' > current_date - interval \'' .. interval .. '\''
end
return (db.query(query))[1]['count']
end
function sum (tableName, field)
return (db.query('select sum(' .. field .. ') from ' .. tableName))[1]['sum']
end
return {
projects = {
total = count('projects'),
public = count('projects where ispublic'),
views = sum('projects', 'views'),
likes = count('likes'),
haveNotes = count('projects where notes is not null'),
updatedDuring = {
thisYear = count('projects', '1 year', 'updated'),
thisMonth = count('projects', '1 month', 'updated'),
thisWeek = count('projects', '1 week', 'updated'),
today = count('projects', '1 day', 'updated')
},
sharedDuring = {
thisYear = count('projects', '1 year', 'shared'),
thisMonth = count('projects', '1 month', 'shared'),
thisWeek = count('projects', '1 week', 'shared'),
today = count('projects', '1 day', 'shared')
}
},
users = {
total = count('users'),
admins = count('users where isadmin'),
haveDescription = count('users where about is not null'),
haveLocation = count('users where location is not null'),
joinedDuring = {
thisYear = count('users', '1 year', 'joined'),
thisMonth = count('users', '1 month', 'joined'),
thisWeek = count('users', '1 week', 'joined'),
today = count('users', '1 day', 'joined')
}
}
}
end
function dateString(sqlDate)
if (sqlDate == nil) then return 'never' end
actualDate = require('date')(sqlDate)
return string.format('%02d', actualDate:getday()) ..
'.' .. string.format('%02d', actualDate:getmonth()) ..
'.' .. actualDate:getyear()
end
send_mail = function (rcpt, subject, body)
local socket = require 'socket'
local base = _G
-----------------------------------------------------------------------------
-- Mega hack. Don't try to do this at home.
-----------------------------------------------------------------------------
-- we can't yield across calls to protect on Lua 5.1, so we rewrite it with
-- coroutines
-- make sure you don't require any module that uses socket.protect before
-- loading our hack
if string.sub(base._VERSION, -3) == "5.1" then
local function _protect(co, status, ...)
if not status then
local msg = ...
if base.type(msg) == 'table' then
return nil, msg[1]
else
base.error(msg, 0)
end
end
if coroutine.status(co) == "suspended" then
return _protect(co, coroutine.resume(co, coroutine.yield(...)))
else
return ...
end
end
function socket.protect(f)
return function(...)
local co = coroutine.create(f)
return _protect(co, coroutine.resume(co, ...))
end
end
end
local smtp = require 'socket.smtp'
local ssl = require 'ssl'
local https = require 'ssl.https'
local ltn12 = require 'ltn12'
local config = require "lapis.config".get()
function sslCreate()
local sock = socket.tcp()
return setmetatable({
connect = function(_, host, port)
local r, e = sock:connect(host, port)
if not r then return r, e end
sock = ssl.wrap(sock, {mode='client', protocol='tlsv1'})
return sock:dohandshake()
end
}, {
__index = function(t,n)
return function(_, ...)
return sock[n](sock, ...)
end
end
})
end
local msg = {
headers = {
from = config.mail_from_name .. " <" .. config.mail_from .. ">",
to = rcpt,
subject = subject
},
body = body
}
local ok, err = smtp.send {
from = config.mail_from,
rcpt = rcpt,
source = smtp.message(msg),
user = config.mail_user,
password = config.mail_password,
server = config.mail_server,
port = 465,
create = sslCreate
}
return ok, err
end