1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12: class Ingo
13: {
14: 15: 16: 17:
18: const BLACKLIST_MARKER = '++DELETE++';
19:
20: 21: 22:
23: const = '++USER_HEADER++';
24:
25: 26: 27: 28: 29:
30: static private $_shareCache = null;
31:
32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48:
49: static public function createSession()
50: {
51: global $prefs, $session;
52:
53: if ($session->exists('ingo', 'script_generate')) {
54: return;
55: }
56:
57: 58:
59: foreach (self::getBackend() as $key => $val) {
60: if ($val) {
61: $session->set('ingo', 'backend/' . $key, $val);
62: }
63: }
64:
65: $ingo_script = self::loadIngoScript();
66: $session->set('ingo', 'script_generate', $ingo_script->generateAvailable());
67:
68:
69: $categories = array_flip(
70: array_merge($ingo_script->availableActions(),
71: $ingo_script->availableCategories()));
72: if ($prefs->isLocked('blacklist')) {
73: unset($categories[Ingo_Storage::ACTION_BLACKLIST]);
74: }
75: if ($prefs->isLocked('whitelist')) {
76: unset($categories[Ingo_Storage::ACTION_WHITELIST]);
77: }
78: if ($prefs->isLocked('vacation')) {
79: unset($categories[Ingo_Storage::ACTION_VACATION]);
80: }
81: if ($prefs->isLocked('forward')) {
82: unset($categories[Ingo_Storage::ACTION_FORWARD]);
83: }
84: if ($prefs->isLocked('spam')) {
85: unset($categories[Ingo_Storage::ACTION_SPAM]);
86: }
87: $categories = array_flip($categories);
88:
89:
90: $session->set('ingo', 'script_categories', $categories);
91: }
92:
93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104:
105: static public function flistSelect($value = null, $form = null,
106: $tagname = 'actionvalue')
107: {
108: global $conf, $registry;
109:
110: if ($registry->hasMethod('mail/folderlist')) {
111: $createfolder = $registry->hasMethod('mail/createFolder');
112: try {
113: $mailboxes = $registry->call('mail/folderlist');
114:
115: $text = '<select class="flistSelect" id="' . $tagname . '" name="' . $tagname . '">' .
116: '<option value="">' . _("Select target folder:") . '</option>' .
117: '<option disabled="disabled">- - - - - - - - - -</option>';
118:
119: if ($createfolder) {
120: $text .= '<option class="flistCreate" value="">' . _("Create new folder") . '</option>' .
121: '<option disabled="disabled">- - - - - - - - - -</option>';
122: }
123:
124: foreach ($mailboxes as $key => $val) {
125: $text .= sprintf(
126: "<option value=\"%s\"%s>%s</option>\n",
127: htmlspecialchars($key),
128: ($key === $value) ? ' selected="selected"' : '',
129: str_repeat(' ', $val['level'] * 2) . htmlspecialchars($val['label'])
130: );
131: }
132:
133: Horde::addScriptFile('new_folder.js', 'ingo');
134: Horde::addInlineJsVars(array(
135: 'IngoNewFolder.folderprompt' => _("Please enter the name of the new folder:")
136: ));
137:
138: return $text . '</select>';
139: } catch (Horde_Exception $e) {}
140: }
141:
142: return '<input id="' . $tagname . '" name="' . $tagname . '" size="40" value="' . $value . '" />';
143: }
144:
145: 146: 147: 148: 149: 150: 151: 152: 153:
154: static public function validateFolder(Horde_Variables $vars, $name)
155: {
156: $new_id = $name . '_new';
157: if (isset($vars->$new_id)) {
158: if ($GLOBALS['registry']->hasMethod('mail/createFolder') &&
159: $GLOBALS['registry']->call('mail/createFolder', array(Horde_String::convertCharset($vars->$new_id, 'UTF-8', 'UTF7-IMAP')))) {
160: return $vars->$new_id;
161: }
162: } elseif (isset($vars->$name) && strlen($vars->$name)) {
163: return $vars->$name;
164: }
165:
166: throw new Ingo_Exception(_("Could not validate IMAP mailbox."));
167: }
168:
169: 170: 171: 172: 173: 174: 175:
176: static public function getUser($full = true)
177: {
178: if (empty($GLOBALS['ingo_shares'])) {
179: $baseuser = ($full ||
180: ($GLOBALS['session']->get('ingo', 'backend/hordeauth') === 'full'));
181: $user = $GLOBALS['registry']->getAuth($baseuser ? null : 'bare');
182: } else {
183: list(, $user) = explode(':', $GLOBALS['session']->get('ingo', 'current_share'), 2);
184: }
185:
186: return $user;
187: }
188:
189: 190: 191: 192: 193: 194:
195: static public function getDomain()
196: {
197: $user = self::getUser(true);
198: $pos = strpos($user, '@');
199:
200: return ($pos !== false)
201: ? substr($user, $pos + 1)
202: : false;
203: }
204:
205: 206: 207: 208: 209: 210: 211: 212: 213: 214:
215: static public function activateScript($script, $deactivate = false,
216: $additional = array())
217: {
218: $transport = self::getTransport();
219:
220: try {
221: $transport->setScriptActive($script, $additional);
222: } catch (Ingo_Exception $e) {
223: $msg = ($deactivate)
224: ? _("There was an error deactivating the script.")
225: : _("There was an error activating the script.");
226: $GLOBALS['notification']->push($msg . ' ' . _("The driver said: ") . $e->getMessage(), 'horde.error');
227: return false;
228: }
229:
230: $msg = ($deactivate)
231: ? _("Script successfully deactivated.")
232: : _("Script successfully activated.");
233: $GLOBALS['notification']->push($msg, 'horde.success');
234:
235: return true;
236: }
237:
238: 239: 240: 241: 242:
243: static public function getScript()
244: {
245: return self::getTransport()->getScript();
246: }
247:
248: 249: 250:
251: static public function updateScript()
252: {
253: if ($GLOBALS['session']->get('ingo', 'script_generate')) {
254: try {
255: $ingo_script = self::loadIngoScript();
256:
257:
258: self::activateScript($ingo_script->generate(),
259: false,
260: $ingo_script->additionalScripts());
261: } catch (Ingo_Exception $e) {
262: $GLOBALS['notification']->push(_("Script not updated."), 'horde.error');
263: }
264: }
265: }
266:
267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277:
278: static public function getBackend()
279: {
280: $backends = Horde::loadConfiguration('backends.php', 'backends', 'ingo');
281: if (!isset($backends) || !is_array($backends)) {
282: throw new Ingo_Exception(_("No backends configured in backends.php"));
283: }
284:
285: $backend = null;
286: foreach ($backends as $name => $temp) {
287: if (!empty($temp['disabled'])) {
288: continue;
289: }
290: if (!isset($backend)) {
291: $backend = $name;
292: } elseif (!empty($temp['preferred'])) {
293: if (is_array($temp['preferred'])) {
294: foreach ($temp['preferred'] as $val) {
295: if (($val == $_SERVER['SERVER_NAME']) ||
296: ($val == $_SERVER['HTTP_HOST'])) {
297: $backend = $name;
298: }
299: }
300: } elseif (($temp['preferred'] == $_SERVER['SERVER_NAME']) ||
301: ($temp['preferred'] == $_SERVER['HTTP_HOST'])) {
302: $backend = $name;
303: }
304: }
305: }
306:
307:
308: if (is_null($backend)) {
309: throw new Ingo_Exception(_("No backend configured for this host"));
310: }
311:
312: $backends[$backend]['id'] = $name;
313: $backend = $backends[$backend];
314:
315: foreach (array('script', 'transport') as $val) {
316: if (empty($backend[$val])) {
317: throw new Ingo_Exception(sprintf(_("No \"%s\" element found in backend configuration."), $val));
318: }
319: }
320:
321:
322: if (!isset($backend['params'])) {
323: $backend['params'] = array();
324: }
325:
326: return $backend;
327: }
328:
329: 330: 331: 332: 333: 334:
335: static public function loadIngoScript()
336: {
337: return Ingo_Script::factory(
338: $GLOBALS['session']->get('ingo', 'backend/script'),
339: $GLOBALS['session']->get('ingo', 'backend/scriptparams', Horde_Session::TYPE_ARRAY)
340: );
341: }
342:
343: 344: 345: 346: 347: 348:
349: static public function getTransport()
350: {
351: global $registry, $session;
352:
353: $params = $session->get('ingo', 'backend/params');
354:
355:
356: if (($hordeauth = $session->get('ingo', 'backend/hordeauth')) ||
357: !isset($params['username']) ||
358: !isset($params['password'])) {
359: $params['username'] = $registry->getAuth(($hordeauth === 'full') ? null : 'bare');
360: $params['password'] = $registry->getAuthCredential('password');
361: }
362:
363: return Ingo_Transport::factory($GLOBALS['session']->get('ingo', 'backend/transport'), $params);
364: }
365:
366: 367: 368: 369: 370: 371: 372: 373: 374: 375:
376: static public function listRulesets($owneronly = false,
377: $permission = Horde_Perms::SHOW)
378: {
379: try {
380: $rulesets = $GLOBALS['ingo_shares']->listShares(
381: $GLOBALS['registry']->getAuth(),
382: array('perm' => $permission,
383: 'attributes' => $owneronly ? $GLOBALS['registry']->getAuth() : null));
384: } catch (Horde_Share_Exception $e) {
385: Horde::logMessage($e, 'ERR');
386: return array();
387: }
388:
389: return $rulesets;
390: }
391:
392: 393: 394:
395: static public function hasSharePermission($mask = null)
396: {
397: if (!isset($GLOBALS['ingo_shares'])) {
398: return true;
399: }
400:
401: if (is_null(self::$_shareCache)) {
402: self::$_shareCache = $GLOBALS['ingo_shares']->getPermissions($GLOBALS['session']->get('ingo', 'current_share'), $GLOBALS['registry']->getAuth());
403: }
404:
405: return self::$_shareCache & $mask;
406: }
407:
408: 409: 410: 411: 412: 413: 414: 415:
416: static public function filterEmptyAddress($address)
417: {
418: $address = trim($address);
419: return !empty($address) && ($address != '@');
420: }
421:
422: 423: 424: 425: 426:
427: static public function ()
428: {
429: $t = $GLOBALS['injector']->createInstance('Horde_Template');
430: $t->set('form_url', Horde::url('filters.php'));
431: $t->set('forminput', Horde_Util::formInput());
432:
433: if (!empty($GLOBALS['ingo_shares']) &&
434: (count($GLOBALS['all_rulesets']) > 1)) {
435: $options = array();
436: foreach (array_keys($GLOBALS['all_rulesets']) as $id) {
437: $options[] = array(
438: 'name' => htmlspecialchars($GLOBALS['all_rulesets'][$id]->get('name')),
439: 'selected' => ($GLOBALS['session']->get('ingo', 'current_share') == $id),
440: 'val' => htmlspecialchars($id)
441: );
442: }
443: $t->set('options', $options);
444: }
445:
446: $t->set('menu_string', Horde::menu(array('menu_ob' => true))->render());
447:
448: $menu = $t->fetch(INGO_TEMPLATES . '/menu/menu.html');
449:
450: 451:
452: Horde::startBuffer();
453: require HORDE_BASE . '/services/sidebar.php';
454: return $menu . Horde::endBuffer();
455: }
456:
457: 458: 459:
460: static public function status()
461: {
462: $GLOBALS['notification']->notify(array('listeners' => array('status', 'audio')));
463: }
464:
465: }
466: