1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: class Agora {
14:
15: 16: 17:
18: const VFS_PATH = '.horde/agora/attachments/';
19: const AVATAR_PATH = '.horde/agora/avatars/';
20:
21: 22: 23: 24: 25: 26: 27:
28: function getAgoraId()
29: {
30: if (($id = Horde_Util::getFormData('agora')) !== null) {
31: if (strstr($id, '.')) {
32: list($forum_id, $message_id) = explode('.', $id, 2);
33: } else {
34: $forum_id = $id;
35: $message_id = 0;
36: }
37: } else {
38: $forum_id = Horde_Util::getFormData('forum_id');
39: $message_id = Horde_Util::getFormData('message_id');
40: }
41: $scope = basename(Horde_Util::getFormData('scope', 'agora'));
42:
43: return array($forum_id, $message_id, $scope);
44: }
45:
46: 47: 48: 49: 50: 51: 52:
53: function setAgoraId($forum_id, $message_id, $url = '', $scope = null, $encode = false)
54: {
55: $agora_id = $forum_id . '.' . $message_id;
56:
57: if (!empty($url)) {
58: if ($scope) {
59: $url = Horde_Util::addParameter($url, 'scope', $scope, $encode);
60: } else {
61: $url = Horde_Util::addParameter($url, 'scope', Horde_Util::getGet('scope', 'agora'), $encode);
62: }
63: return Horde_Util::addParameter($url, 'agora', $agora_id, $encode);
64: }
65:
66: return $agora_id;
67: }
68:
69: 70: 71: 72: 73: 74: 75: 76:
77: function getCAPTCHA($new = false)
78: {
79: global $session;
80:
81: if ($new || !$session->get('agora', 'captcha')) {
82: $captcha = '';
83: for ($i = 0; $i < 5; ++$i) {
84: $captcha .= chr(rand(65, 90));
85: }
86: $session->set('agora', 'captcha', $captcha);
87: }
88:
89: return $session->get('agora', 'captcha');
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99:
100: function formatCategoryTree($forums)
101: {
102: 103:
104: $forums_list = array();
105: foreach (array_values($forums) as $forum) {
106: $levels = explode(':', $forum['forum_name']);
107: $forums_list[$forum['forum_id']] = str_repeat('.. ', count($levels) - 1) . array_pop($levels);
108: }
109: return $forums_list;
110: }
111:
112: 113: 114: 115: 116: 117: 118: 119: 120:
121: function getSortBy($view)
122: {
123: global $prefs;
124:
125: if (($sortby = Horde_Util::getFormData($view . '_sortby')) !== null) {
126: $prefs->setValue($view . '_sortby', $sortby);
127: }
128: $sort_by = $prefs->getValue($view . '_sortby');
129:
130:
131: if ($sort_by == 'message_date' || substr($sort_by, 0, 1) == 'l') {
132: $sort_by = $prefs->getDefault($view . '_sortby');
133: $prefs->setValue($view . '_sortby', $sortby);
134: }
135:
136: return $sort_by;
137: }
138:
139: 140: 141: 142: 143: 144: 145: 146: 147:
148: function getSortDir($view)
149: {
150: global $prefs;
151: if (($sortdir = Horde_Util::getFormData($view . '_sortdir')) !== null) {
152: $prefs->setValue($view . '_sortdir', $sortdir);
153: }
154: return $prefs->getValue($view . '_sortdir');
155: }
156:
157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167:
168: function formatColumnHeaders($columns, $sort_by, $sort_dir, $view)
169: {
170:
171: $url = Horde::selfUrl(true);
172: $url = Horde_Util::removeParameter($url, array($view . '_sortby', $view . '_sortdir'));
173:
174:
175: $headers = array();
176: foreach ($columns as $col_name => $col_title) {
177: $extra = array();
178:
179: if (is_array($col_title)) {
180: $keys = array_keys($col_title);
181: $extra_name = $keys[0];
182: if ($sort_by == $keys[1]) {
183: $extra = array($keys[0] => $col_title[$keys[0]]);
184: $col_name = $keys[1];
185: $col_title = $col_title[$keys[1]];
186: } else {
187: $extra = array($keys[1] => $col_title[$keys[1]]);
188: $col_name = $keys[0];
189: $col_title = $col_title[$keys[0]];
190: }
191: }
192: if ($sort_by == $col_name) {
193: 194:
195: $sort_img = ($sort_dir ? 'za.png' : 'az.png');
196: $sort_title = ($sort_dir ? _("Sort Ascending") : _("Sort Descending"));
197: $col_arrow = Horde::link(Horde_Util::addParameter($url, array($view . '_sortby' => $col_name, $view . '_sortdir' => $sort_dir ? 0 : 1)), $sort_title) .
198: Horde::img($sort_img, $sort_title) . '</a> ';
199: $col_class = 'selected';
200: } else {
201: 202:
203: $col_arrow = '';
204: $col_title = Horde::link(Horde_Util::addParameter($url, $view . '_sortby', $col_name), sprintf(_("Sort by %s"), $col_title)) . $col_title . '</a>';
205: $col_class = 'item';
206: }
207: $col_class .= ' leftAlign';
208: if (count($extra)) {
209: list($name, $title) = each($extra);
210: $col_title .= ' <small>[' .
211: Horde::link(Horde_Util::addParameter($url, $view . '_sortby', $name), sprintf(_("Sort by %s"), $title)) . $title . '</a>' .
212: ']</small>';
213: $col_name = $extra_name;
214: }
215: $headers[$col_name] = $col_arrow . $col_title;
216: $headers[$col_name . '_class_plain'] = $col_class;
217: $headers[$col_name . '_class'] = empty($col_class) ? '' : ' class="' . $col_class . '"';
218: }
219:
220: return $headers;
221: }
222:
223: 224: 225: 226: 227:
228: function getVFS()
229: {
230: global $conf;
231:
232: if (!isset($conf['vfs']['type'])) {
233: return PEAR::raiseError(_("The VFS backend needs to be configured to enable attachment uploads."));
234: }
235:
236: try {
237: return $GLOBALS['injector']->getInstance('Horde_Core_Factory_Vfs')->create();
238: } catch (Horde_Vfs_Exception $e) {
239: return PEAR::raiseError($e);
240: }
241: }
242:
243: function validateAvatar($avatar_path)
244: {
245: if (!$GLOBALS['conf']['avatar']['allow_avatars'] || !$avatar_path) {
246: return false;
247: }
248:
249: preg_match('/^(http|vfs):\/\/(.*)\/(gallery|uploaded|.*)\/(.*\..*)/i',
250: $avatar_path, $matches);
251:
252: switch ($matches[1]) {
253: case 'http':
254: if (!$GLOBALS['conf']['avatar']['enable_external']) {
255: 256:
257: return false;
258: }
259: $dimensions = @getimagesize($avatar_path);
260: if (($dimensions === false) ||
261: ($dimensions[0] > $GLOBALS['conf']['avatar']['max_width']) ||
262: ($dimensions[1] > $GLOBALS['conf']['avatar']['max_height'])) {
263: 264:
265: return false;
266: } else {
267: $avatar = null;
268:
269: $flock = fopen($avatar_path, 'r');
270: while (!feof($flock)) {
271: $avatar .= fread($flock, 2048);
272: }
273: fclose($flock);
274:
275: if (strlen($avatar) > ($GLOBALS['conf']['avatar']['max_size'] * 1024)) {
276: 277:
278: return false;
279: }
280: }
281: return true;
282:
283: case 'vfs':
284: switch ($matches[3]) {
285: case 'gallery':
286:
287: return $GLOBALS['conf']['avatar']['enable_gallery'];
288:
289: case 'uploaded':
290:
291: return $GLOBALS['conf']['avatar']['enable_uploads'];
292:
293: default:
294:
295: return false;
296: }
297: break;
298:
299: default:
300:
301: return false;
302: }
303:
304: return false;
305: }
306:
307: function getAvatarUrl($avatar_path, $scopeend_sid = true)
308: {
309: if (!$avatar_path) {
310: return PEAR::raiseError(_("Malformed avatar."));
311: }
312:
313: preg_match('/^(http|vfs):\/\/(.*)\/(gallery|uploaded|.*)\/(.*\..*)/i',
314: $avatar_path, $matches);
315:
316: switch ($matches[1]) {
317: case 'http':
318:
319: break;
320:
321: case 'vfs':
322:
323: switch ($matches[3]) {
324: case 'gallery':
325: $avatar_collection_id = '1';
326: break;
327:
328: case 'uploaded':
329: $avatar_collection_id = '2';
330: break;
331:
332: default:
333: return PEAR::raiseError(_("Malformed database entry."));
334: }
335:
336: $avatar_path = Horde::url('avatars/?id=' . urlencode($matches[4]) . ':' . $avatar_collection_id, true, $scopeend_sid);
337: break;
338: }
339:
340: return $avatar_path;
341: }
342:
343: 344: 345: 346: 347: 348: 349:
350: function distribute($message_id)
351: {
352: global $conf;
353:
354: $storage = $GLOBALS['injector']->getInstance('Agora_Factory_Driver')->create();
355: $message = $storage->getMessage($message_id);
356: $forum = $storage->getForum($message['forum_id']);
357:
358: if (empty($forum['forum_distribution_address'])) {
359: return;
360: }
361:
362: $mail = new Horde_Mime_Mail();
363: $mail->addHeader('X-Horde-Agora-Post', $message_id);
364: $mail->addHeader('From', strpos($message['message_author'], '@') ? $message['message_author'] : $forum['forum_distribution_address']);
365: $mail->addHeader('Subject', '[' . $forum['forum_name'] . '] ' . $message['message_subject']);
366: $mail->addHeader('To', $forum['forum_distribution_address']);
367: $mail->setBody($message['body']);
368:
369: $mail->send($GLOBALS['injector']->getInstance('Horde_Mail'));
370: }
371: }
372: