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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
<?php defined("SYSPATH") or die("No direct script access.");
require_once("Exception.php");
require_once("XMPPLog.php");
require_once("Roster.php");
require_once("XMLObj.php");
require_once("XMLStream.php");
require_once("XMPP.php");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2013 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class notification {
static function get_subscription($item_id, $user=null) {
if (empty($user)) {
$user = identity::active_user();
}
return ORM::factory("subscription")
->where("item_id", "=", $item_id)
->where("user_id", "=", $user->id)
->find();
}
static function is_watching($item, $user=null) {
if (empty($user)) {
$user = identity::active_user();
}
return ORM::factory("subscription")
->where("item_id", "=", $item->id)
->where("user_id", "=", $user->id)
->find()
->loaded();
}
static function add_watch($item, $user=null) {
if ($item->is_album()) {
if (empty($user)) {
$user = identity::active_user();
}
$subscription = ORM::factory("subscription");
$subscription->item_id = $item->id;
$subscription->user_id = $user->id;
$subscription->save();
}
}
static function remove_watch($item, $user=null) {
if ($item->is_album()) {
if (empty($user)) {
$user = identity::active_user();
}
$subscription = ORM::factory("subscription")
->where("item_id", "=", $item->id)
->where("user_id", "=", $user->id)
->find()->delete();
}
}
static function get_subscriber_users($item) {
$subscriber_ids = array();
foreach (ORM::factory("subscription")
->select("user_id")
->join("items", "subscriptions.item_id", "items.id")
->where("items.left_ptr", "<=", $item->left_ptr)
->where("items.right_ptr", ">", $item->right_ptr)
->find_all()
->as_array() as $subscriber) {
$subscriber_ids[] = $subscriber->user_id;
}
if (empty($subscriber_ids)) {
return array();
}
return identity::get_user_list($subscriber_ids);
}
static function get_email_subscribers($item) {
$users = self::get_subscriber_users($item);
if (empty($users)) {
return array();
}
$subscribers = array();
foreach ($users as $user) {
if (access::user_can($user, "view", $item)
&& !empty($user->email)
&& empty($user->jabberid)) {
$subscribers[$user->email] = $user->locale;
}
}
return $subscribers;
}
static function get_jabber_subscribers($item) {
$users = self::get_subscriber_users($item);
if (empty($users)) {
return array();
}
$subscribers = array();
foreach ($users as $user) {
if (access::user_can($user, "view", $item)
&& !empty($user->jabberid)) {
$subscribers[$user->jabberid] = $user->locale;
}
}
return $subscribers;
}
private static function getItemUpdatedView($viewname, $item, $original, $locale) {
$v = new View($viewname);
$v->original = $original;
$v->item = $item;
$v->subject = $item->is_album() ?
t("Album \"%title\" updated", array("title" => $original->title, "locale" => $locale)) :
($item->is_photo() ?
t("Photo \"%title\" updated", array("title" => $original->title, "locale" => $locale))
: t("Movie \"%title\" updated", array("title" => $original->title, "locale" => $locale)));
return $v;
}
static function send_item_updated($original, $item) {
foreach (self::get_jabber_subscribers($item) as $jabberid => $locale) {
$v = self::getItemUpdatedView("item_updated.txt", $item, $original, $locale);
self::notifyViaXMPP($jabberid, $locale, $v->render());
}
foreach (self::get_email_subscribers($item) as $email => $locale) {
$v = self::getItemUpdatedView("item_updated.html", $item, $original, $locale);
self::_notify($email, $locale, $item, $v->render(), $v->subject);
}
}
private static function getItemAddSubject($item, $parent, $locale) {
return $item->is_album() ?
t("Album \"%title\" added to \"%parent_title\"",
array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale)) :
($item->is_photo() ?
t("Photo \"%title\" added to \"%parent_title\"",
array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale)) :
t("Movie \"%title\" added to \"%parent_title\"",
array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale)));
}
static function send_item_add($item) {
$parent = $item->parent();
Kohana_Log::add("error", "adding item notifiy");
foreach (self::get_jabber_subscribers($item) as $jabberid => $locale) {
Kohana_Log::add("error", "adding item notifiy jabber");
$jabberView = new View("item_added.txt");
$jabberView->item = $item;
$jabberView->subject = self::getItemAddSubject($item, $parent, $locale);
self::notifyViaXMPP($jabberid, $locale, $jabberView->render());
}
foreach (self::get_email_subscribers($item) as $email => $locale) {
$emailView = new View("item_added.html");
$emailView->item = $item;
$emailView->subject = self::getItemAddSubject($item, $parent, $locale);
self::_notify($email, $locale, $item, $emailView->render(), $emailView->subject);
}
}
private static function getItemDeletedView($viewname, $item, $parent, $locale) {
$v = new View($viewname);
$v->item = $item;
$v->subject = $item->is_album() ?
t("Album \"%title\" removed from \"%parent_title\"",
array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale)) :
($item->is_photo() ?
t("Photo \"%title\" removed from \"%parent_title\"",
array("title" => $item->title, "parent_title" => $parent->title, "locale" => $locale))
: t("Movie \"%title\" removed from \"%parent_title\"",
array("title" => $item->title, "parent_title" => $parent->title,
"locale" => $locale)));
return $v;
}
static function send_item_deleted($item) {
$parent = $item->parent();
foreach (self::get_jabber_subscribers($item) as $jabberid => $locale) {
$v = self::getItemDeletedView("item_deleted.txt", $item, $parent, $locale);
self::notifyViaXMPP($jabberid, $locale, $v->render());
}
foreach (self::get_email_subscribers($item) as $email => $locale) {
$v = self::getItemDeletedView("item_deleted.html", $item, $parent, $locale);
self::_notify($email, $locale, $item, $v->render(), $v->subject);
}
}
static function send_comment_published($comment) {
$item = $comment->item();
foreach (self::get_email_subscribers($item) as $email => $locale) {
$v = new View("comment_published.html");
$v->comment = $comment;
$v->subject = $item->is_album() ?
t("A new comment was published for album \"%title\"",
array("title" => $item->title, "locale" => $locale)) :
($item->is_photo() ?
t("A new comment was published for photo \"%title\"",
array("title" => $item->title, "locale" => $locale))
: t("A new comment was published for movie \"%title\"",
array("title" => $item->title, "locale" => $locale)));
self::_notify($email, $locale, $item, $v->render(), $v->subject);
}
}
static function send_pending_notifications() {
foreach (db::build()
->select(db::expr("DISTINCT `email`"))
->from("pending_notifications")
->execute() as $row) {
$email = $row->email;
$result = ORM::factory("pending_notification")
->where("email", "=", $email)
->find_all();
if ($result->count() == 1) {
$pending = $result->current();
Sendmail::factory()
->to($email)
->subject($pending->subject)
->header("Mime-Version", "1.0")
->header("Content-Type", "text/html; charset=UTF-8")
->message($pending->text)
->send();
$pending->delete();
} else {
$text = "";
$locale = null;
foreach ($result as $pending) {
$text .= $pending->text;
$locale = $pending->locale;
$pending->delete();
}
Sendmail::factory()
->to($email)
->subject(t("New activity for %site_name",
array("site_name" => item::root()->title, "locale" => $locale)))
->header("Mime-Version", "1.0")
->header("Content-Type", "text/html; charset=UTF-8")
->message($text)
->send();
}
}
}
private static function notifyViaXMPP($jabberid, $locale, $text) {
$XMPP = new XMPP('jabber.mail-zur.de', 5222, 'gallery', 'gallery', 'pics');
$XMPP->connect(5);
$XMPP->processUntil('session_start', 10);
//$XMPP->presence();
$XMPP->message($jabberid, $text, 'chat');
$XMPP->disconnect();
}
private static function _notify($email, $locale, $item, $text, $subject) {
if (!batch::in_progress()) {
Sendmail::factory()
->to($email)
->subject($subject)
->header("Mime-Version", "1.0")
->header("Content-Type", "text/html; charset=UTF-8")
->message($text)
->send();
} else {
$pending = ORM::factory("pending_notification");
$pending->subject = $subject;
$pending->text = $text;
$pending->email = $email;
$pending->locale = $locale;
$pending->save();
}
}
}
|