Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-28-11-04-58
[civicrm-core.git] / CRM / Utils / System / Base.php
1 <?php
2
3 /**
4 * Base class for UF system integrations
5 */
6 abstract class CRM_Utils_System_Base {
7 var $is_drupal = FALSE;
8 var $is_joomla = FALSE;
9 var $is_wordpress = FALSE;
10
11 /*
12 * Does the CMS allow CMS forms to be extended by hooks
13 */
14 var $supports_form_extensions = FALSE;
15
16 /**
17 * if we are using a theming system, invoke theme, else just print the
18 * content
19 *
20 * @param string $content the content that will be themed
21 * @param boolean $print are we displaying to the screen or bypassing theming?
22 * @param boolean $maintenance for maintenance mode
23 *
24 * @return void prints content on stdout
25 * @access public
26 */
27 function theme(&$content, $print = FALSE, $maintenance = FALSE) {
28 $ret = FALSE;
29
30 // TODO: Split up; this was copied verbatim from CiviCRM 4.0's multi-UF theming function
31 // but the parts should be copied into cleaner subclass implementations
32 $config = CRM_Core_Config::singleton();
33 if (
34 $config->userSystem->is_drupal &&
35 function_exists('theme') &&
36 !$print
37 ) {
38 if ($maintenance) {
39 drupal_set_breadcrumb('');
40 drupal_maintenance_theme();
41 if ($region = CRM_Core_Region::instance('html-header', FALSE)) {
42 CRM_Utils_System::addHTMLHead($region->render(''));
43 }
44 print theme('maintenance_page', array('content' => $content));
45 exit();
46 }
47 $ret = TRUE; // TODO: Figure out why D7 returns but everyone else prints
48 }
49 $out = $content;
50
51 $config = &CRM_Core_Config::singleton();
52 if (
53 !$print &&
54 $config->userFramework == 'WordPress'
55 ) {
56 if (is_admin()) {
57 require_once (ABSPATH . 'wp-admin/admin-header.php');
58 }
59 else {
60 // FIX ME: we need to figure out to replace civicrm content on the frontend pages
61 }
62 }
63
64 if ($ret) {
65 return $out;
66 }
67 else {
68 print $out;
69 }
70 }
71
72 function getDefaultBlockLocation() {
73 return 'left';
74 }
75
76 function getVersion() {
77 return 'Unknown';
78 }
79
80 /**
81 * Format the url as per language Negotiation.
82 *
83 * @param string $url
84 *
85 * @return string $url, formatted url.
86 * @static
87 */
88 function languageNegotiationURL(
89 $url,
90 $addLanguagePart = TRUE,
91 $removeLanguagePart = FALSE
92 ) {
93 return $url;
94 }
95
96 /**
97 * Determine the location of the CMS root.
98 *
99 * @return string|NULL local file system path to CMS root, or NULL if it cannot be determined
100 */
101 function cmsRootPath() {
102 return NULL;
103 }
104
105 /**
106 * Get user login URL for hosting CMS (method declared in each CMS system class)
107 *
108 * @param string $destination - if present, add destination to querystring (works for Drupal only)
109 *
110 * @return string - loginURL for the current CMS
111 * @static
112 */
113 public abstract function getLoginURL($destination = '');
114
115 /**
116 * Determine the native ID of the CMS user
117 *
118 * @param $username
119 * @return int|NULL
120 */
121 function getUfId($username) {
122 $className = get_class($this);
123 throw new CRM_Core_Exception("Not implemented: {$className}->getUfId");
124 }
125
126 /**
127 * Set a init session with user object
128 *
129 * @param array $data array with user specific data
130 *
131 * @access public
132 */
133 function setUserSession($data) {
134 list($userID, $ufID) = $data;
135 $session = CRM_Core_Session::singleton();
136 $session->set('ufID', $ufID);
137 $session->set('userID', $userID);
138 }
139
140 /**
141 * Reset any system caches that may be required for proper CiviCRM
142 * integration.
143 */
144 function flush() {
145 // nullop by default
146 }
147
148 /**
149 * Return default Site Settings
150 * @return array array
151 * - $url, (Joomla - non admin url)
152 * - $siteName,
153 * - $siteRoot
154 */
155 function getDefaultSiteSettings($dir) {
156 $config = CRM_Core_Config::singleton();
157 $url = $config->userFrameworkBaseURL;
158 return array($url, NULL, NULL);
159 }
160
161 /**
162 * Perform any post login activities required by the CMS -
163 * e.g. for drupal: records a watchdog message about the new session, saves the login timestamp,
164 * calls hook_user op 'login' and generates a new session.
165 *
166 * @param array params
167 *
168 * FIXME: Document values accepted/required by $params
169 */
170 function userLoginFinalize($params = array()){
171 }
172
173 /**
174 * Set timezone in mysql so that timestamp fields show the correct time
175 */
176 function setMySQLTimeZone(){
177 $timeZoneOffset = $this->getTimeZoneOffset();
178 if($timeZoneOffset){
179 $sql = "SET time_zone = '$timeZoneOffset'";
180 CRM_Core_DAO::executequery($sql);
181 }
182 }
183
184
185 /**
186 * Get timezone from CMS
187 * @return boolean|string
188 */
189 /**
190 * Get timezone from Drupal
191 * @return boolean|string
192 */
193 function getTimeZoneOffset(){
194 $timezone = $this->getTimeZoneString();
195 if($timezone){
196 $tzObj = new DateTimeZone($timezone);
197 $dateTime = new DateTime("now", $tzObj);
198 $tz = $tzObj->getOffset($dateTime);
199
200 if(empty($tz)){
201 return false;
202 }
203
204 $timeZoneOffset = sprintf("%02d:%02d", $tz / 3600, abs(($tz/60)%60));
205
206 if($timeZoneOffset > 0){
207 $timeZoneOffset = '+' . $timeZoneOffset;
208 }
209 return $timeZoneOffset;
210 }
211 }
212
213 /**
214 * Over-ridable function to get timezone as a string eg.
215 * @return string Timezone e.g. 'America/Los_Angeles'
216 */
217 function getTimeZoneString() {
218 return date_default_timezone_get();
219 }
220
221 /**
222 * Get Unique Identifier from UserFramework system (CMS)
223 * @param object $user object as described by the User Framework
224 * @return mixed $uniqueIdentifer Unique identifier from the user Framework system
225 *
226 */
227 function getUniqueIdentifierFromUserObject($user) {}
228
229 /**
230 * Get User ID from UserFramework system (CMS)
231 * @param object $user object as described by the User Framework
232 * @return mixed <NULL, number>
233 *
234 */
235 function getUserIDFromUserObject($user) {}
236
237 /**
238 * Get currently logged in user uf id.
239 *
240 * @return int $userID logged in user uf id.
241 */
242 function getLoggedInUfID() {}
243
244 /**
245 * Get currently logged in user unique identifier - this tends to be the email address or user name.
246 *
247 * @return string $userID logged in user unique identifier
248 */
249 function getLoggedInUniqueIdentifier() {}
250
251 /**
252 * return a UFID (user account ID from the UserFramework / CMS system being based on the user object
253 * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs
254 * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the user id before calling
255 * the function
256 *
257 * Note there is already a function getUFId which takes $username as a param - we could add $user
258 * as a second param to it but it seems messy - just overloading it because the name is taken
259 * @param object $user
260 * @return int $ufid - user ID of UF System
261 */
262 function getBestUFID($user = NULL) {
263 if($user) {
264 return $this->getUserIDFromUserObject($user);
265 }
266 return $this->getLoggedInUfID();
267 }
268
269 /**
270 * return a unique identifier (usually an email address or username) from the UserFramework / CMS system being based on the user object
271 * passed, defaulting to the logged in user if not passed. Note that ambiguous situation occurs
272 * in CRM_Core_BAO_UFMatch::synchronize - a cleaner approach would seem to be resolving the unique identifier before calling
273 * the function
274 *
275 * @param object $user
276 * @return string $uniqueIdentifier - unique identifier from the UF System
277 */
278 function getBestUFUniqueIdentifier($user = NULL) {
279 if($user) {
280 return $this->getUniqueIdentifierFromUserObject($user);
281 }
282 return $this->getLoggedInUniqueIdentifier();
283 }
284 }
285