1<?php
2
3/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
4
5/**
6 * Service context (factory) class
7 *
8 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
9 * @author Stefan Hecken <stefan.hecken@concepts-and-training.de>
10 * @version $Id$
11 *
12 * @ingroup ServicesContext
13 */
14class ilContext
15{
16    protected static $class_name; // [string]
17    protected static $type; // [string]
18
19    const CONTEXT_WEB = "ilContextWeb";
20    const CONTEXT_CRON = "ilContextCron";
21    const CONTEXT_RSS = "ilContextRss";
22    const CONTEXT_ICAL = "ilContextIcal";
23    const CONTEXT_SOAP = "ilContextSoap";
24    const CONTEXT_SOAP_NO_AUTH = 'ilContextSoapNoAuth';
25    const CONTEXT_WEBDAV = "ilContextWebdav";
26    const CONTEXT_RSS_AUTH = "ilContextRssAuth";
27    const CONTEXT_SESSION_REMINDER = "ilContextSessionReminder";
28    const CONTEXT_SOAP_WITHOUT_CLIENT = "ilContextSoapWithoutClient";
29    const CONTEXT_UNITTEST = "ilContextUnitTest";
30    const CONTEXT_REST = "ilContextRest";
31    const CONTEXT_SCORM = "ilContextScorm";
32    const CONTEXT_WAC = "ilContextWAC";
33    const CONTEXT_APACHE_SSO = 'ilContextApacheSSO';
34    const CONTEXT_SHIBBOLETH = 'ilContextShibboleth';
35    const CONTEXT_LTI_PROVIDER = 'ilContextLTIProvider';
36    const CONTEXT_SAML = 'ilContextSaml';
37
38
39    /**
40     * Init context by type
41     *
42     * @param string $a_type
43     * @return bool
44     */
45    public static function init($a_type)
46    {
47        include_once "Services/Context/classes/class." . $a_type . ".php";
48        self::$class_name = $a_type;
49        self::$type = $a_type;
50
51        return true;
52    }
53
54    /**
55     * Call context method directly without internal handling
56     *
57     * @param int $a_type
58     * @return mixed
59     */
60    public static function directCall($a_type, $a_method)
61    {
62        $class_name = $a_type;
63        if ($class_name) {
64            include_once "Services/Context/classes/class." . $class_name . ".php";
65            if (method_exists($class_name, $a_method)) {
66                return call_user_func(array($class_name, $a_method));
67            }
68        }
69    }
70
71    /**
72     * Call current content
73     *
74     * @param string $a_method
75     * @return bool
76     */
77    protected static function callContext($a_method)
78    {
79        if (!self::$class_name) {
80            self::init(self::CONTEXT_WEB);
81        }
82        return call_user_func(array(self::$class_name, $a_method));
83    }
84
85    /**
86     * Are redirects supported?
87     *
88     * @return bool
89     */
90    public static function supportsRedirects()
91    {
92        global $DIC;
93
94        $ilCtrl = null;
95        if (isset($DIC["ilCtrl"])) {
96            $ilCtrl = $DIC->ctrl();
97        }
98
99        // asynchronous calls must never be redirected
100        if ($ilCtrl && $ilCtrl->isAsynch()) {
101            return false;
102        }
103
104        return (bool) self::callContext("supportsRedirects");
105    }
106
107    /**
108     * Based on user authentication?
109     *
110     * @return bool
111     */
112    public static function hasUser()
113    {
114        return (bool) self::callContext("hasUser");
115    }
116
117    /**
118     * Uses HTTP aka browser
119     *
120     * @return bool
121     */
122    public static function usesHTTP()
123    {
124        return (bool) self::callContext("usesHTTP");
125    }
126
127    /**
128     * Has HTML output
129     *
130     * @return bool
131     */
132    public static function hasHTML()
133    {
134        return (bool) self::callContext("hasHTML");
135    }
136
137    /**
138     * Uses template engine
139     *
140     * @return bool
141     */
142    public static function usesTemplate()
143    {
144        return (bool) self::callContext("usesTemplate");
145    }
146
147    /**
148     * Init client
149     *
150     * @return bool
151     */
152    public static function initClient()
153    {
154        return (bool) self::callContext("initClient");
155    }
156
157    /**
158     * Try authentication
159     *
160     * @return bool
161     */
162    public static function doAuthentication()
163    {
164        return (bool) self::callContext("doAuthentication");
165    }
166
167    /**
168     * Supports push messages
169     *
170     * @return bool
171     */
172    public static function supportsPushMessages()
173    {
174        return (bool) self::callContext("supportsPushMessages");
175    }
176
177    /**
178     * Get context type
179     *
180     * @return string
181     */
182    public static function getType()
183    {
184        return self::$type;
185    }
186
187    /**
188     * Check if context supports persistent
189     * session handling.
190     * false for cli context
191     *
192     * @return bool
193     */
194    public static function supportsPersistentSessions()
195    {
196        return (bool) self::callContext('supportsPersistentSessions');
197    }
198
199    /**
200     * Context that are not only temporary in a session (e.g. WAC is, Cron is not)
201     *
202     * @return bool
203     */
204    public static function isSessionMainContext()
205    {
206        return (bool) self::callContext('isSessionMainContext');
207    }
208}
209