minor fix
[civicrm-core.git] / CRM / Utils / System / WordPress.php
index dea048da3bd14799fdbb00835d587dfd111e0997..355b5bc535843ba3418110557475fd52742a883e 100644 (file)
@@ -76,29 +76,48 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
    * Moved from CRM_Utils_System_Base
    */
   public function getDefaultFileStorage() {
+    $config = CRM_Core_Config::singleton();
+    $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
+    $cmsPath = $this->cmsRootPath();
+    $filesPath = CRM_Utils_File::baseFilePath();
+    $filesRelPath = CRM_Utils_File::relativize($filesPath, $cmsPath);
+    $filesURL = rtrim($cmsUrl, '/') . '/' . ltrim($filesRelPath, ' /');
+    return array(
+      'url' => CRM_Utils_File::addTrailingSlash($filesURL, '/'),
+      'path' => CRM_Utils_File::addTrailingSlash($filesPath),
+    );
+  }
+
+  /**
+   * Determine the location of the CiviCRM source tree.
+   *
+   * @return array
+   *   - url: string. ex: "http://example.com/sites/all/modules/civicrm"
+   *   - path: string. ex: "/var/www/sites/all/modules/civicrm"
+   */
+  public function getCiviSourceStorage() {
     global $civicrm_root;
-    $config  = CRM_Core_Config::singleton();
-    $baseURL = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
 
-    $filesURL        = NULL;
-    $filesPath       = NULL;
-    $upload_dir      = wp_upload_dir();
-    $settingsDir     = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR;
-    $settingsURL     = $upload_dir['baseurl'] . DIRECTORY_SEPARATOR . 'civicrm' . DIRECTORY_SEPARATOR;
-    if (is_dir(WP_PLUGIN_DIR . '/files/civicrm/')) {
-      //for legacy path
-      $filesURL = WP_PLUGIN_URL . "/files/civicrm/";
-    }
-    elseif (is_dir($settingsDir)) {
-      $filesURL = $settingsURL;
+    // Don't use $config->userFrameworkBaseURL; it has garbage on it.
+    // More generally, we shouldn't be using $config here.
+    if (!defined('CIVICRM_UF_BASEURL')) {
+      throw new RuntimeException('Undefined constant: CIVICRM_UF_BASEURL');
     }
-    else {
-      throw new CRM_Core_Exception("Failed to locate default file storage ($config->userFramework)");
+
+    $cmsPath = $this->cmsRootPath();
+
+    // $config  = CRM_Core_Config::singleton();
+    // overkill? // $cmsUrl = CRM_Utils_System::languageNegotiationURL($config->userFrameworkBaseURL, FALSE, TRUE);
+    $cmsUrl = CIVICRM_UF_BASEURL;
+    if (CRM_Utils_System::isSSL()) {
+      $cmsUrl = str_replace('http://', 'https://', $cmsUrl);
     }
 
+    $civiRelPath = CRM_Utils_File::relativize($civicrm_root, $cmsPath);
+    $civiUrl = rtrim($cmsUrl, '/') . '/' . ltrim($civiRelPath, ' /');
     return array(
-      'url'  => $filesURL,
-      'path' => CRM_Utils_File::baseFilePath(),
+      'url' => CRM_Utils_File::addTrailingSlash($civiUrl, '/'),
+      'path' => CRM_Utils_File::addTrailingSlash($civicrm_root),
     );
   }
 
@@ -180,13 +199,12 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
     $query = NULL,
     $absolute = FALSE,
     $fragment = NULL,
-    $htmlize = TRUE,
     $frontend = FALSE,
     $forceBackend = FALSE
   ) {
     $config = CRM_Core_Config::singleton();
     $script = '';
-    $separator = $htmlize ? '&' : '&';
+    $separator = '&';
     $wpPageParam = '';
     $fragment = isset($fragment) ? ('#' . $fragment) : '';
 
@@ -308,13 +326,24 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
   }
 
   /**
-   * FIXME: Do something
-   *
-   * @param \obj $user
+   * @param \string $user
    *
    * @return bool
    */
   public function loadUser($user) {
+    $userdata = get_user_by('login', $user);
+    if (!$userdata->data->ID) {
+      return FALSE;
+    }
+
+    $uid = $userdata->data->ID;
+    wp_set_current_user($uid);
+    $contactID = CRM_Core_BAO_UFMatch::getContactId($uid);
+
+    // lets store contact id and user id in session
+    $session = CRM_Core_Session::singleton();
+    $session->set('ufID', $uid);
+    $session->set('userID', $contactID);
     return TRUE;
   }
 
@@ -375,13 +404,17 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
    * @return bool
    */
   public function loadBootStrap($name = NULL, $pass = NULL) {
-    global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb;
+    global $wp, $wp_rewrite, $wp_the_query, $wp_query, $wpdb, $current_site, $current_blog, $current_user;
+
+    if (!defined('WP_USE_THEMES')) {
+      define('WP_USE_THEMES', FALSE);
+    }
 
     $cmsRootPath = $this->cmsRootPath();
     if (!$cmsRootPath) {
       CRM_Core_Error::fatal("Could not find the install directory for WordPress");
     }
-    $path = CRM_Core_BAO_Setting::getItem('CiviCRM Preferences', 'wpLoadPhp');
+    $path = Civi::settings()->get('wpLoadPhp');
     if (!empty($path)) {
       require_once $path;
     }
@@ -402,7 +435,7 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
       $name = $name ? $name : trim(CRM_Utils_Array::value('name', $_REQUEST));
       $pass = $pass ? $pass : trim(CRM_Utils_Array::value('pass', $_REQUEST));
       if ($name) {
-        $uid = wp_authenticate($name, $pass);
+        $uid = wp_authenticate($name, $pass); // this returns a WP_User object if successful
         if (!$uid) {
           if ($throwError) {
             echo '<br />Sorry, unrecognized username or password.';
@@ -413,7 +446,12 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
       }
     }
     if ($uid) {
-      $account = wp_set_current_user($uid);
+      if ($uid instanceof WP_User) {
+        $account = wp_set_current_user($uid->ID);
+      }
+      else {
+        $account = wp_set_current_user($uid);
+      }
       if ($account && $account->data->ID) {
         global $user;
         $user = $account;
@@ -551,9 +589,8 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
         $errors[$emailName] = "Your email is invaid";
       }
       elseif (email_exists($params['mail'])) {
-        $resetUrl = $config->userFrameworkBaseURL . 'wp-login.php?action=lostpassword';
         $errors[$emailName] = ts('The email address %1 already has an account associated with it. <a href="%2">Have you forgotten your password?</a>',
-          array(1 => $params['mail'], 2 => $resetUrl)
+          array(1 => $params['mail'], 2 => wp_lostpassword_url())
         );
       }
     }
@@ -623,8 +660,7 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
    */
   public function getLoginURL($destination = '') {
     $config = CRM_Core_Config::singleton();
-    $loginURL = $config->userFrameworkBaseURL;
-    $loginURL .= 'wp-login.php';
+    $loginURL = wp_login_url();
     return $loginURL;
   }
 
@@ -679,4 +715,52 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base {
     $list[] = 'js/crm.wordpress.js';
   }
 
+  /**
+   * @inheritDoc
+   */
+  public function synchronizeUsers() {
+    $config = CRM_Core_Config::singleton();
+    if (PHP_SAPI != 'cli') {
+      set_time_limit(300);
+    }
+    $id = 'ID';
+    $mail = 'user_email';
+
+    $uf = $config->userFramework;
+    $contactCount = 0;
+    $contactCreated = 0;
+    $contactMatching = 0;
+
+    global $wpdb;
+    $wpUserIds = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users");
+
+    foreach ($wpUserIds as $wpUserId) {
+      $wpUserData = get_userdata($wpUserId);
+      $contactCount++;
+      if ($match = CRM_Core_BAO_UFMatch::synchronizeUFMatch($wpUserData,
+        $wpUserData->$id,
+        $wpUserData->$mail,
+        $uf,
+        1,
+        'Individual',
+        TRUE
+      )
+      ) {
+        $contactCreated++;
+      }
+      else {
+        $contactMatching++;
+      }
+      if (is_object($match)) {
+        $match->free();
+      }
+    }
+
+    return array(
+      'contactCount' => $contactCount,
+      'contactMatching' => $contactMatching,
+      'contactCreated' => $contactCreated,
+    );
+  }
+
 }