6.0.0-git
2018-12-10
Last Modified 2013-09-04 by Jan Schneider

Using Horde from a custom website or application

The following code snippets show how easy it is to use Horde applications from any PHP code, for example websites, portals, or custom web applications.

Each Horde application can provide a set of methods through its external API. These APIs are used for inter-application communication and data exchange between the different Horde modules. The API methods are always called through the Horde Registry. The Registry and the API methods are also used to provide the methods for Horde's RPC servers, name the XMLRPC, SOAP, and REST servers.

Let's look at some code:

<?php

// Define path to Horde.
define('HORDE_BASE', '/path/to/horde');

// Load the Horde Framework core, and set up inclusion paths.
require_once HORDE_BASE . '/lib/core.php';

// Create the Registry object.
$registry = &Registry::singleton();

// Call a Registry method.
$calendars = $registry->call('calendar/listCalendars');

That's it. Assuming that you configured Horde's permissions to allow guest access to Kronolith, the calendar application, you will get an array with calendar names back. If there are no calendars that allow guest access, the array will be empty.

Next we want to see the list of calendars that a certain Horde user has access to. You can for example create a calendar that should be displayed on your website, but only on your website and not through the RPC servers.

<?php

// Define path to Horde.
define('HORDE_BASE', '/path/to/horde');

// Tell the registry that we are doing authentication ourselves.
define('AUTH_HANDLER', true);

// Load the Horde Framework core, and set up inclusion paths.
require_once HORDE_BASE . '/lib/core.php';

// Create the Registry object.
$registry = &Registry::singleton();

// Authenticate a user.
$auth = &Auth::singleton('none');
$auth->setAuth('username', array('password' => 'foo'));

// Call a Registry method.
$calendars = $registry->call('calendar/listCalendars');

We only added three lines, one to tell the Registry object to not care about authentication, one to create an Auth object, and one to set the current active user. Note that these calls don't really authenticate the user, they only make Horde think that the specified user is authenticated. The 'password' argument can have any value, as long as it isn't needed later, for example to really authenticate to a IMAP server.

If you want to check authentication, or need to access a backend that requires a valid login, use the following calls to the Auth library instead:

<?php

$auth = &Auth::singleton($conf['auth']['driver']);
$authenticated = $auth->authenticate('username', array('password' => 'realpassword'));

The Registry already takes care of loading the Horde configuration, so $conf['auth']['driver'] contains the correct driver name. The Auth library retrieves the authentication driver parameters from the configuration automatically.
$authenticated will contain a boolean indicating whether the authentication succeeded.

Using Horde_Blocks to embed HTML

A quick way to embed HTML snippets from a Horde application into a website or application is to use a Horde_Block. The Horde_Block library powers the portal page of the Horde Application Framework. Each application and Horde itself are providing blocks of HTML that can be arranged by the users on their personal portal page. All blocks can be retrieved through a single API method.

To embed a simple month calender for example, all you have to do is:

<?php

echo $registry->call('horde/blockContent',
                     array('kronolith', 'month', array('calendar' => 'jan')));

This code is calling the "month" block from the "kronolith" application, and passes it a "calendar" parameter. The method returns a string with HTML code that can be embedded into any HTML page.

As Horde is using CSS for all styling and the CSS rules are not included in the HTML, the local CSS need to tweaked to give the block a nice look.
One could also use Horde's CSS files, but these would be applied to the complete page:

<?php

echo Horde::stylesheetLink('kronolith', 'mozilla');

Such a call would have to happen after the Registry has been initialized, and so that the output is echoed in the page's HTML HEAD.

Using the calendar widgets to embed calendars

See the help text for more information. You can select either a single calendar or '__all' as the calendar to show all calendars. You can also specify the time period as well, just pass the same parameter name the block uses in the query parameters.

Example upcoming events (monthlist) - show 2 months worth of events, but with a max. of 10 events:

<div id="kronolist"></div><script 
src="http://host.example.com/horde/services/imple.php?imple=Embed/impleApp=kronolith/view=monthlist/calendar=calendar_name/container=kronolist/months=2/ maxevents=10" 
type="text/javascript"></script>

Summary view (show 7 days worth of events only with alarms but only show up to 20 events max):

<div id="kronosummary"> </div><script type="text/javascript" 
src="http://hostname.example.com/horde/services/imple.php?imple=Embed/impleApp=kronolith/container=kronosummary/view=summary/calendar=calendar_name/days=7/maxvents=20/alarms=1"></script>

Summary view (show 7 days worth of events only with alarms but only show up to 20 events max) but with all shared calendars instead of one specific calendar:

<div id="kronosummary"> </div><script type="text/javascript" 
src="http://hostname.example.com/horde/services/imple.php?imple=Embed/impleApp=kronolith/container=kronosummary/view=summary/__all/days=7/maxvents=20/alarms=1"></script>

The summary view makes a listing of upcoming events that can be used as an agenda and which prints nicely on 8.5X11 paper.