CRM-19453
[civicrm-core.git] / CRM / Utils / System / Joomla.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2017
32 */
33
34 /**
35 * Joomla specific stuff goes here.
36 */
37 class CRM_Utils_System_Joomla extends CRM_Utils_System_Base {
38 /**
39 * Class constructor.
40 */
41 public function __construct() {
42 /**
43 * deprecated property to check if this is a drupal install. The correct method is to have functions on the UF classes for all UF specific
44 * functions and leave the codebase oblivious to the type of CMS
45 * @deprecated
46 * @var bool
47 */
48 $this->is_drupal = FALSE;
49 }
50
51 /**
52 * @inheritDoc
53 */
54 public function createUser(&$params, $mail) {
55 $baseDir = JPATH_SITE;
56 require_once $baseDir . '/components/com_users/models/registration.php';
57
58 $userParams = JComponentHelper::getParams('com_users');
59 $model = new UsersModelRegistration();
60 $ufID = NULL;
61
62 // get the default usertype
63 $userType = $userParams->get('new_usertype');
64 if (!$userType) {
65 $userType = 2;
66 }
67
68 if (isset($params['name'])) {
69 $fullname = trim($params['name']);
70 }
71 elseif (isset($params['contactID'])) {
72 $fullname = trim(CRM_Contact_BAO_Contact::displayName($params['contactID']));
73 }
74 else {
75 $fullname = trim($params['cms_name']);
76 }
77
78 // Prepare the values for a new Joomla user.
79 $values = array();
80 $values['name'] = $fullname;
81 $values['username'] = trim($params['cms_name']);
82 $values['password1'] = $values['password2'] = $params['cms_pass'];
83 $values['email1'] = $values['email2'] = trim($params[$mail]);
84
85 $lang = JFactory::getLanguage();
86 $lang->load('com_users', $baseDir);
87
88 $register = $model->register($values);
89
90 $ufID = JUserHelper::getUserId($values['username']);
91 return $ufID;
92 }
93
94 /**
95 * @inheritDoc
96 */
97 public function updateCMSName($ufID, $ufName) {
98 $ufID = CRM_Utils_Type::escape($ufID, 'Integer');
99 $ufName = CRM_Utils_Type::escape($ufName, 'String');
100
101 $values = array();
102 $user = JUser::getInstance($ufID);
103
104 $values['email'] = $ufName;
105 $user->bind($values);
106
107 $user->save();
108 }
109
110 /**
111 * Check if username and email exists in the Joomla db.
112 *
113 * @param array $params
114 * Array of name and mail values.
115 * @param array $errors
116 * Array of errors.
117 * @param string $emailName
118 * Field label for the 'email'.
119 */
120 public function checkUserNameEmailExists(&$params, &$errors, $emailName = 'email') {
121 $config = CRM_Core_Config::singleton();
122
123 $dao = new CRM_Core_DAO();
124 $name = $dao->escape(CRM_Utils_Array::value('name', $params));
125 $email = $dao->escape(CRM_Utils_Array::value('mail', $params));
126 //don't allow the special characters and min. username length is two
127 //regex \\ to match a single backslash would become '/\\\\/'
128 $isNotValid = (bool) preg_match('/[\<|\>|\"|\'|\%|\;|\(|\)|\&|\\\\|\/]/im', $name);
129 if ($isNotValid || strlen($name) < 2) {
130 $errors['cms_name'] = ts('Your username contains invalid characters or is too short');
131 }
132
133 $JUserTable = &JTable::getInstance('User', 'JTable');
134
135 $db = $JUserTable->getDbo();
136 $query = $db->getQuery(TRUE);
137 $query->select('username, email');
138 $query->from($JUserTable->getTableName());
139
140 // LOWER in query below roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
141 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) OR (LOWER(email) = LOWER(\'' . $email . '\'))');
142 $db->setQuery($query, 0, 10);
143 $users = $db->loadAssocList();
144
145 $row = array();
146 if (count($users)) {
147 $row = $users[0];
148 }
149
150 if (!empty($row)) {
151 $dbName = CRM_Utils_Array::value('username', $row);
152 $dbEmail = CRM_Utils_Array::value('email', $row);
153 if (strtolower($dbName) == strtolower($name)) {
154 $errors['cms_name'] = ts('The username %1 is already taken. Please select another username.',
155 array(1 => $name)
156 );
157 }
158 if (strtolower($dbEmail) == strtolower($email)) {
159 $resetUrl = str_replace('administrator/', '', $config->userFrameworkBaseURL) . 'index.php?option=com_users&view=reset';
160 $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
161 array(1 => $email, 2 => $resetUrl)
162 );
163 }
164 }
165 }
166
167 /**
168 * @inheritDoc
169 */
170 public function setTitle($title, $pageTitle = NULL) {
171 if (!$pageTitle) {
172 $pageTitle = $title;
173 }
174
175 $template = CRM_Core_Smarty::singleton();
176 $template->assign('pageTitle', $pageTitle);
177
178 $document = JFactory::getDocument();
179 $document->setTitle($title);
180 }
181
182 /**
183 * @inheritDoc
184 */
185 public function appendBreadCrumb($breadCrumbs) {
186 $template = CRM_Core_Smarty::singleton();
187 $bc = $template->get_template_vars('breadcrumb');
188
189 if (is_array($breadCrumbs)) {
190 foreach ($breadCrumbs as $crumbs) {
191 if (stripos($crumbs['url'], 'id%%')) {
192 $args = array('cid', 'mid');
193 foreach ($args as $a) {
194 $val = CRM_Utils_Request::retrieve($a, 'Positive', CRM_Core_DAO::$_nullObject,
195 FALSE, NULL, $_GET
196 );
197 if ($val) {
198 $crumbs['url'] = str_ireplace("%%{$a}%%", $val, $crumbs['url']);
199 }
200 }
201 }
202 $bc[] = $crumbs;
203 }
204 }
205 $template->assign_by_ref('breadcrumb', $bc);
206 }
207
208 /**
209 * @inheritDoc
210 */
211 public function resetBreadCrumb() {
212 }
213
214 /**
215 * @inheritDoc
216 */
217 public function addHTMLHead($string = NULL) {
218 if ($string) {
219 $document = JFactory::getDocument();
220 $document->addCustomTag($string);
221 }
222 }
223
224 /**
225 * @inheritDoc
226 */
227 public function addStyleUrl($url, $region) {
228 if ($region == 'html-header') {
229 $document = JFactory::getDocument();
230 $document->addStyleSheet($url);
231 return TRUE;
232 }
233 return FALSE;
234 }
235
236 /**
237 * @inheritDoc
238 */
239 public function addStyle($code, $region) {
240 if ($region == 'html-header') {
241 $document = JFactory::getDocument();
242 $document->addStyleDeclaration($code);
243 return TRUE;
244 }
245 return FALSE;
246 }
247
248 /**
249 * @inheritDoc
250 */
251 public function url(
252 $path = NULL,
253 $query = NULL,
254 $absolute = FALSE,
255 $fragment = NULL,
256 $frontend = FALSE,
257 $forceBackend = FALSE
258 ) {
259 $config = CRM_Core_Config::singleton();
260 $separator = '&';
261 $Itemid = '';
262 $script = '';
263 $path = CRM_Utils_String::stripPathChars($path);
264
265 if ($config->userFrameworkFrontend) {
266 $script = 'index.php';
267 if (JRequest::getVar("Itemid")) {
268 $Itemid = "{$separator}Itemid=" . JRequest::getVar("Itemid");
269 }
270 }
271
272 if (isset($fragment)) {
273 $fragment = '#' . $fragment;
274 }
275
276 $base = $absolute ? $config->userFrameworkBaseURL : $config->useFrameworkRelativeBase;
277
278 if (!empty($query)) {
279 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$separator}{$query}{$fragment}";
280 }
281 else {
282 $url = "{$base}{$script}?option=com_civicrm{$separator}task={$path}{$Itemid}{$fragment}";
283 }
284
285 // gross hack for joomla, we are in the backend and want to send a frontend url
286 if ($frontend && $config->userFramework == 'Joomla') {
287 // handle both joomla v1.5 and v1.6, CRM-7939
288 $url = str_replace('/administrator/index2.php', '/index.php', $url);
289 $url = str_replace('/administrator/index.php', '/index.php', $url);
290
291 // CRM-8215
292 $url = str_replace('/administrator/', '/index.php', $url);
293 }
294 elseif ($forceBackend) {
295 if (defined('JVERSION')) {
296 $joomlaVersion = JVERSION;
297 }
298 else {
299 $jversion = new JVersion();
300 $joomlaVersion = $jversion->getShortVersion();
301 }
302
303 if (version_compare($joomlaVersion, '1.6') >= 0) {
304 $url = str_replace('/index.php', '/administrator/index.php', $url);
305 }
306 }
307 return $url;
308 }
309
310 /**
311 * Set the email address of the user.
312 *
313 * @param object $user
314 * Handle to the user object.
315 */
316 public function setEmail(&$user) {
317 global $database;
318 $query = $db->getQuery(TRUE);
319 $query->select($db->quoteName('email'))
320 ->from($db->quoteName('#__users'))
321 ->where($db->quoteName('id') . ' = ' . $user->id);
322 $database->setQuery($query);
323 $user->email = $database->loadResult();
324 }
325
326 /**
327 * @inheritDoc
328 */
329 public function authenticate($name, $password, $loadCMSBootstrap = FALSE, $realPath = NULL) {
330 require_once 'DB.php';
331
332 $config = CRM_Core_Config::singleton();
333 $user = NULL;
334
335 if ($loadCMSBootstrap) {
336 $bootStrapParams = array();
337 if ($name && $password) {
338 $bootStrapParams = array(
339 'name' => $name,
340 'pass' => $password,
341 );
342 }
343 CRM_Utils_System::loadBootStrap($bootStrapParams, TRUE, TRUE, FALSE);
344 }
345
346 jimport('joomla.application.component.helper');
347 jimport('joomla.database.table');
348 jimport('joomla.user.helper');
349
350 $JUserTable = JTable::getInstance('User', 'JTable');
351
352 $db = $JUserTable->getDbo();
353 $query = $db->getQuery(TRUE);
354 $query->select('id, name, username, email, password');
355 $query->from($JUserTable->getTableName());
356 $query->where('(LOWER(username) = LOWER(\'' . $name . '\')) AND (block = 0)');
357 $db->setQuery($query, 0, 0);
358 $users = $db->loadObjectList();
359
360 $row = array();
361 if (count($users)) {
362 $row = $users[0];
363 }
364
365 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
366 if (!defined('JVERSION')) {
367 require $joomlaBase . '/libraries/cms/version/version.php';
368 $jversion = new JVersion();
369 define('JVERSION', $jversion->getShortVersion());
370 }
371
372 if (!empty($row)) {
373 $dbPassword = $row->password;
374 $dbId = $row->id;
375 $dbEmail = $row->email;
376
377 if (version_compare(JVERSION, '2.5.18', 'lt') ||
378 (version_compare(JVERSION, '3.0', 'ge') && version_compare(JVERSION, '3.2.1', 'lt'))
379 ) {
380 // now check password
381 list($hash, $salt) = explode(':', $dbPassword);
382 $cryptpass = md5($password . $salt);
383 if ($hash != $cryptpass) {
384 return FALSE;
385 }
386 }
387 else {
388 if (!JUserHelper::verifyPassword($password, $dbPassword, $dbId)) {
389 return FALSE;
390 }
391
392 //include additional files required by Joomla 3.2.1+
393 if (version_compare(JVERSION, '3.2.1', 'ge')) {
394 require_once $joomlaBase . '/libraries/cms/application/helper.php';
395 require_once $joomlaBase . '/libraries/cms/application/cms.php';
396 require_once $joomlaBase . '/libraries/cms/application/administrator.php';
397 }
398 }
399
400 CRM_Core_BAO_UFMatch::synchronizeUFMatch($row, $dbId, $dbEmail, 'Joomla');
401 $contactID = CRM_Core_BAO_UFMatch::getContactId($dbId);
402 if (!$contactID) {
403 return FALSE;
404 }
405 return array($contactID, $dbId, mt_rand());
406 }
407
408 return FALSE;
409 }
410
411 /**
412 * Set a init session with user object.
413 *
414 * @param array $data
415 * Array with user specific data.
416 */
417 public function setUserSession($data) {
418 list($userID, $ufID) = $data;
419 $user = new JUser($ufID);
420 $session = JFactory::getSession();
421 $session->set('user', $user);
422
423 parent::setUserSession($data);
424 }
425
426 /**
427 * FIXME: Do something
428 *
429 * @param string $message
430 */
431 public function setMessage($message) {
432 }
433
434 /**
435 * @param \string $username
436 * @param \string $password
437 *
438 * @return bool
439 */
440 public function loadUser($username, $password = NULL) {
441 $uid = JUserHelper::getUserId($username);
442 if (empty($uid)) {
443 return FALSE;
444 }
445 $contactID = CRM_Core_BAO_UFMatch::getContactId($uid);
446 if (!empty($password)) {
447 $instance = JFactory::getApplication('site');
448 $params = array(
449 'username' => $username,
450 'password' => $password,
451 );
452 //perform the login action
453 $instance->login($params);
454 }
455
456 $session = CRM_Core_Session::singleton();
457 $session->set('ufID', $uid);
458 $session->set('userID', $contactID);
459 return TRUE;
460 }
461
462 /**
463 * FIXME: Use CMS-native approach
464 */
465 public function permissionDenied() {
466 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
467 }
468
469 /**
470 * @inheritDoc
471 */
472 public function logout() {
473 session_destroy();
474 CRM_Utils_System::setHttpHeader("Location", "index.php");
475 }
476
477 /**
478 * @inheritDoc
479 */
480 public function getUFLocale() {
481 if (defined('_JEXEC')) {
482 $conf = JFactory::getConfig();
483 $locale = $conf->get('language');
484 return str_replace('-', '_', $locale);
485 }
486 return NULL;
487 }
488
489 /**
490 * @inheritDoc
491 */
492 public function setUFLocale($civicrm_language) {
493 // TODO
494 return TRUE;
495 }
496
497 /**
498 * @inheritDoc
499 */
500 public function getVersion() {
501 if (class_exists('JVersion')) {
502 $version = new JVersion();
503 return $version->getShortVersion();
504 }
505 else {
506 return 'Unknown';
507 }
508 }
509
510 /**
511 * Load joomla bootstrap.
512 *
513 * @param array $params
514 * with uid or name and password.
515 * @param bool $loadUser
516 * load cms user?.
517 * @param bool|\throw $throwError throw error on failure?
518 * @param null $realPath
519 * @param bool $loadDefines
520 *
521 * @return bool
522 */
523 public function loadBootStrap($params = array(), $loadUser = TRUE, $throwError = TRUE, $realPath = NULL, $loadDefines = TRUE) {
524 // Setup the base path related constant.
525 $joomlaBase = dirname(dirname(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__))))))));
526
527 // load BootStrap here if needed
528 // We are a valid Joomla entry point.
529 if (!defined('_JEXEC') && $loadDefines) {
530 define('_JEXEC', 1);
531 define('DS', DIRECTORY_SEPARATOR);
532 define('JPATH_BASE', $joomlaBase . '/administrator');
533 require $joomlaBase . '/administrator/includes/defines.php';
534 }
535
536 // Get the framework.
537 if (file_exists($joomlaBase . '/libraries/import.legacy.php')) {
538 require $joomlaBase . '/libraries/import.legacy.php';
539 }
540 require $joomlaBase . '/libraries/import.php';
541 require $joomlaBase . '/libraries/joomla/event/dispatcher.php';
542 require $joomlaBase . '/configuration.php';
543
544 // Files may be in different places depending on Joomla version
545 if (!defined('JVERSION')) {
546 require $joomlaBase . '/libraries/cms/version/version.php';
547 $jversion = new JVersion();
548 define('JVERSION', $jversion->getShortVersion());
549 }
550
551 if (version_compare(JVERSION, '3.0', 'lt')) {
552 require $joomlaBase . '/libraries/joomla/environment/uri.php';
553 require $joomlaBase . '/libraries/joomla/application/component/helper.php';
554 }
555 else {
556 require $joomlaBase . '/libraries/cms.php';
557 require $joomlaBase . '/libraries/joomla/uri/uri.php';
558 }
559
560 jimport('joomla.application.cli');
561
562 // CRM-14281 Joomla wasn't available during bootstrap, so hook_civicrm_config never executes.
563 $config = CRM_Core_Config::singleton();
564 CRM_Utils_Hook::config($config);
565
566 return TRUE;
567 }
568
569 /**
570 * @inheritDoc
571 */
572 public function isUserLoggedIn() {
573 $user = JFactory::getUser();
574 return ($user->guest) ? FALSE : TRUE;
575 }
576
577 /**
578 * @inheritDoc
579 */
580 public function getLoggedInUfID() {
581 $user = JFactory::getUser();
582 return ($user->guest) ? NULL : $user->id;
583 }
584
585 /**
586 * @inheritDoc
587 */
588 public function getLoggedInUniqueIdentifier() {
589 $user = JFactory::getUser();
590 return $this->getUniqueIdentifierFromUserObject($user);
591 }
592
593 /**
594 * @inheritDoc
595 */
596 public function getUserIDFromUserObject($user) {
597 return !empty($user->id) ? $user->id : NULL;
598 }
599
600 /**
601 * @inheritDoc
602 */
603 public function getUniqueIdentifierFromUserObject($user) {
604 return ($user->guest) ? NULL : $user->email;
605 }
606
607 /**
608 * @inheritDoc
609 */
610 public function getTimeZoneString() {
611 $timezone = JFactory::getConfig()->get('offset');
612 return !$timezone ? date_default_timezone_get() : $timezone;
613 }
614
615 /**
616 * Get a list of all installed modules, including enabled and disabled ones
617 *
618 * @return array
619 * CRM_Core_Module
620 */
621 public function getModules() {
622 $result = array();
623
624 $db = JFactory::getDbo();
625 $query = $db->getQuery(TRUE);
626 $query->select('type, folder, element, enabled')
627 ->from('#__extensions')
628 ->where('type =' . $db->Quote('plugin'));
629 $plugins = $db->setQuery($query)->loadAssocList();
630 foreach ($plugins as $plugin) {
631 // question: is the folder really a critical part of the plugin's name?
632 $name = implode('.', array('joomla', $plugin['type'], $plugin['folder'], $plugin['element']));
633 $result[] = new CRM_Core_Module($name, $plugin['enabled'] ? TRUE : FALSE);
634 }
635
636 return $result;
637 }
638
639 /**
640 * @inheritDoc
641 */
642 public function getLoginURL($destination = '') {
643 $config = CRM_Core_Config::singleton();
644 $loginURL = $config->userFrameworkBaseURL;
645 $loginURL = str_replace('administrator/', '', $loginURL);
646 $loginURL .= 'index.php?option=com_users&view=login';
647
648 //CRM-14872 append destination
649 if (!empty($destination)) {
650 $loginURL .= '&return=' . urlencode(base64_encode($destination));
651 }
652 return $loginURL;
653 }
654
655 /**
656 * @inheritDoc
657 */
658 public function getLoginDestination(&$form) {
659 $args = NULL;
660
661 $id = $form->get('id');
662 if ($id) {
663 $args .= "&id=$id";
664 }
665 else {
666 $gid = $form->get('gid');
667 if ($gid) {
668 $args .= "&gid=$gid";
669 }
670 else {
671 // Setup Personal Campaign Page link uses pageId
672 $pageId = $form->get('pageId');
673 if ($pageId) {
674 $component = $form->get('component');
675 $args .= "&pageId=$pageId&component=$component&action=add";
676 }
677 }
678 }
679
680 $destination = NULL;
681 if ($args) {
682 // append destination so user is returned to form they came from after login
683 $args = 'reset=1' . $args;
684 $destination = CRM_Utils_System::url(CRM_Utils_System::currentPath(), $args, TRUE, NULL, FALSE, TRUE);
685 }
686
687 return $destination;
688 }
689
690 /**
691 * Determine the location of the CMS root.
692 *
693 * @return string|NULL
694 * local file system path to CMS root, or NULL if it cannot be determined
695 */
696 public function cmsRootPath() {
697 list($url, $siteName, $siteRoot) = $this->getDefaultSiteSettings();
698 $includePath = "$siteRoot/libraries/cms/version";
699 if (file_exists("$includePath/version.php")) {
700 return $siteRoot;
701 }
702 return NULL;
703 }
704
705 /**
706 * @inheritDoc
707 */
708 public function getDefaultSiteSettings($dir = NULL) {
709 $config = CRM_Core_Config::singleton();
710 $url = preg_replace(
711 '|/administrator|',
712 '',
713 $config->userFrameworkBaseURL
714 );
715 // CRM-19453 revisited. Under Windows, the pattern wasn't recognised.
716 // This is the original pattern, but it doesn't work under Windows.
717 // By setting the pattern to the one used before the change first and only
718 // changing it means that the change code only affects Windows users.
719 $pattern = '|/media/civicrm/.*$|';
720 if (DIRECTORY_SEPARATOR == '\\') {
721 // This regular expression will handle Windows as well as Linux
722 // and any combination of forward and back slashes in directory
723 // separators. We only apply it if the directory separator is the one
724 // used by Windows.
725 $pattern = '|[\\\\/]media[\\\\/]civicrm[\\\\/].*$|';
726 }
727 $siteRoot = preg_replace(
728 $pattern,
729 '',
730 $config->imageUploadDir
731 );
732 return array($url, NULL, $siteRoot);
733 }
734
735 /**
736 * @inheritDoc
737 */
738 public function getUserRecordUrl($contactID) {
739 $uid = CRM_Core_BAO_UFMatch::getUFId($contactID);
740 $userRecordUrl = NULL;
741 // if logged in user has user edit access, then allow link to other users joomla profile
742 if (JFactory::getUser()->authorise('core.edit', 'com_users')) {
743 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_users&view=user&task=user.edit&id=" . $uid;
744 }
745 elseif (CRM_Core_Session::singleton()->get('userID') == $contactID) {
746 return CRM_Core_Config::singleton()->userFrameworkBaseURL . "index.php?option=com_admin&view=profile&layout=edit&id=" . $uid;
747 }
748 }
749
750 /**
751 * @inheritDoc
752 */
753 public function checkPermissionAddUser() {
754 if (JFactory::getUser()->authorise('core.create', 'com_users')) {
755 return TRUE;
756 }
757 }
758
759 /**
760 * Output code from error function.
761 * @param string $content
762 */
763 public function outputError($content) {
764 if (class_exists('JErrorPage')) {
765 $error = new Exception($content);
766 JErrorPage::render($error);
767 }
768 elseif (class_exists('JError')) {
769 JError::raiseError('CiviCRM-001', $content);
770 }
771 else {
772 parent::outputError($content);
773 }
774 }
775
776 /**
777 * Append Joomla js to coreResourcesList.
778 *
779 * @param array $list
780 */
781 public function appendCoreResources(&$list) {
782 $list[] = 'js/crm.joomla.js';
783 }
784
785 /**
786 * @inheritDoc
787 */
788 public function synchronizeUsers() {
789 $config = CRM_Core_Config::singleton();
790 if (PHP_SAPI != 'cli') {
791 set_time_limit(300);
792 }
793 $id = 'id';
794 $mail = 'email';
795 $name = 'name';
796
797 $JUserTable = &JTable::getInstance('User', 'JTable');
798
799 $db = $JUserTable->getDbo();
800 $query = $db->getQuery(TRUE);
801 $query->select($id . ', ' . $mail . ', ' . $name);
802 $query->from($JUserTable->getTableName());
803 $query->where($mail != '');
804
805 $db->setQuery($query);
806 $users = $db->loadObjectList();
807
808 $user = new StdClass();
809 $uf = $config->userFramework;
810 $contactCount = 0;
811 $contactCreated = 0;
812 $contactMatching = 0;
813 for ($i = 0; $i < count($users); $i++) {
814 $user->$id = $users[$i]->$id;
815 $user->$mail = $users[$i]->$mail;
816 $user->$name = $users[$i]->$name;
817 $contactCount++;
818 if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($user,
819 $users[$i]->$id,
820 $users[$i]->$mail,
821 $uf,
822 1,
823 'Individual',
824 TRUE
825 )
826 ) {
827 $contactCreated++;
828 }
829 else {
830 $contactMatching++;
831 }
832 if (is_object($match)) {
833 $match->free();
834 }
835 }
836
837 return array(
838 'contactCount' => $contactCount,
839 'contactMatching' => $contactMatching,
840 'contactCreated' => $contactCreated,
841 );
842 }
843
844 }