Kronolith_FreeBusy::get PHP Метод

get() публичный статический Метод

Retrieves the free/busy information for a given email address, if any information is available.
public static get ( string $email, boolean $json = false ) : Horde_Icalendar_Vfreebusy | object
$email string The email address to look for.
$json boolean Whether to return the free/busy data as a simple object suitable to be transferred as json.
Результат Horde_Icalendar_Vfreebusy | object Free/busy component.
    public static function get($email, $json = false)
    {
        $default_domain = empty($GLOBALS['conf']['storage']['default_domain']) ? null : $GLOBALS['conf']['storage']['default_domain'];
        $rfc822 = new Horde_Mail_Rfc822();
        try {
            $res = $rfc822->parseAddressList($email, array('default_domain' => $default_domain));
        } catch (Horde_Mail_Exception $e) {
            throw new Kronolith_Exception($e);
        }
        if (!($tmp = $res[0])) {
            throw new Kronolith_Exception(_("No valid email address found"));
        }
        $email = $tmp->bare_address;
        /* Check if we can retrieve a VFB from the Free/Busy URL, if one is
         * set. */
        $url = self::getUrl($email);
        if ($url) {
            $url = trim($url);
            $http = $GLOBALS['injector']->getInstance('Horde_Core_Factory_HttpClient')->create(array('request.verifyPeer' => false));
            try {
                $response = $http->get($url);
            } catch (Horde_Http_Exception $e) {
                throw new Kronolith_Exception(sprintf(_("The free/busy url for %s cannot be retrieved."), $email));
            }
            if ($response->code == 200 && ($data = $response->getBody())) {
                // Detect the charset of the iCalendar data.
                $contentType = $response->getHeader('Content-Type');
                if ($contentType && strpos($contentType, ';') !== false) {
                    list(, $charset, ) = explode(';', $contentType);
                    $data = Horde_String::convertCharset($data, trim(str_replace('charset=', '', $charset)), 'UTF-8');
                }
                $vCal = new Horde_Icalendar();
                $vCal->parsevCalendar($data, 'VCALENDAR');
                $components = $vCal->getComponents();
                $vCal = new Horde_Icalendar();
                $vFb = Horde_Icalendar::newComponent('vfreebusy', $vCal);
                $vFb->setAttribute('ORGANIZER', $email);
                $found = false;
                foreach ($components as $component) {
                    if ($component instanceof Horde_Icalendar_Vfreebusy) {
                        $found = true;
                        $vFb->merge($component);
                    }
                }
                if ($found) {
                    // @todo: actually store the results in the storage, so
                    // that they can be retrieved later. We should store the
                    // plain iCalendar data though, to avoid versioning
                    // problems with serialize iCalendar objects.
                    return $json ? self::toJson($vFb) : $vFb;
                }
            }
        }
        /* Check storage driver. */
        $storage = $GLOBALS['injector']->getInstance('Kronolith_Factory_Storage')->create();
        try {
            $fb = $storage->search($email);
            return $json ? self::toJson($fb) : $fb;
        } catch (Horde_Exception_NotFound $e) {
            if ($url) {
                throw new Kronolith_Exception(sprintf(_("No free/busy information found at the free/busy url of %s."), $email));
            }
            throw new Kronolith_Exception(sprintf(_("No free/busy url found for %s."), $email));
        }
    }

Usage Example

Пример #1
0
 /**
  * Return fb information for the requested attendee or resource.
  *
  * Uses the following request parameters:
  *<pre>
  *  -email:    The attendee's email address.
  *  -resource: The resource id.
  *</pre>
  */
 public function getFreeBusy()
 {
     $result = new stdClass();
     if ($this->vars->email) {
         try {
             $result->fb = Kronolith_FreeBusy::get($this->vars->email, true);
         } catch (Exception $e) {
             $GLOBALS['notification']->push($e->getMessage(), 'horde.warning');
         }
     } elseif ($this->vars->resource) {
         try {
             $resource = Kronolith::getDriver('Resource')->getResource($this->vars->resource);
             try {
                 $result->fb = $resource->getFreeBusy(null, null, true, true);
             } catch (Horde_Exception $e) {
                 // Resource groups can't provide FB information.
                 $result->fb = null;
             }
         } catch (Exception $e) {
             $GLOBALS['notification']->push($e->getMessage(), 'horde.warning');
         }
     }
     return $result;
 }
All Usage Examples Of Kronolith_FreeBusy::get