Plugin Directory

Changeset 670742


Ignore:
Timestamp:
02/20/2013 12:48:10 PM (13 years ago)
Author:
glatze
Message:

class BulkSyncBackADIntegrationPlugin moved from syncback.php to a new file.
[Issue 00075] http://bt.steindorff.de/view.php?id=00075

Location:
active-directory-integration/trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • active-directory-integration/trunk/syncback.php

    r489381 r670742  
    2525// let's load WordPress
    2626require_once( WP_LOAD_PATH . 'wp-load.php');
     27
     28// load class BulkSyncBackADIntegrationPlugin
     29require_once( dirname( __FILE__ ) .'/BulkSyncBackADIntegrationPlugin.class.php');
    2730
    2831// turn off possible output buffering
     
    5356
    5457
    55 // Extend the ADIntegrationPlugin class
    56 class BulkSyncBackADIntegrationPlugin extends ADIntegrationPlugin {
    5758
    58     /**
    59      * Output debug informations
    60      *
    61      * @param integer level
    62      * @param string $notice
    63      */
    64     protected function _log($level = 0, $info = '') {
    65         if ($level <= $this->_loglevel) {
    66             switch ($level) {
    67                 case ADI_LOG_DEBUG:
    68                     $class = 'debug';
    69                     $type  = '[DEBUG]  ';
    70                     break;
    71                 case ADI_LOG_INFO:
    72                     $class = 'info';
    73                     $type  = '[INFO]   ';
    74                     break;
    75                 case ADI_LOG_NOTICE:
    76                     $class = 'notice';
    77                     $type = '[NOTICE] ';
    78                     break;
    79                 case ADI_LOG_WARN:
    80                     $class = 'warn';
    81                     $type = '[WARN]   ';
    82                     break;
    83                 case ADI_LOG_ERROR:
    84                     $class = 'error';
    85                     $type = '[ERROR]  ';
    86                     break;
    87                 case ADI_LOG_FATAL:
    88                     $class = 'fatal';
    89                     $type = '[FATAL]  ';
    90                         break;
    91                 default:
    92                     $class = '';
    93                     $type = '';
    94                    
    95             }
    96             $output = '<span class="'.$class.'">'.$type;
    97             $output .= str_replace("\n","<br />         ",$info).'</span><br />';
    98             echo $output;
    99            
    100             if (WP_DEBUG) {
    101                 if ($fh = @fopen($this->_logfile,'a+')) {
    102                     fwrite($fh,$type . str_replace("\n","\n         ",$info) . "\n");
    103                     fclose($fh);
    104                 }
    105             }       
    106         }
    107     }
    108    
    109        
    110    
    111    
    112     /**
    113      * Do Bulk SyncBack
    114      *
    115      * @param string $authcode
    116      * @return bool true on success, false on error
    117      */
    118     public function bulksyncback($userid = NULL)
    119     {
    120         global $wp_version;
    121         global $wpdb;
    122        
    123         $this->setLogFile(dirname(__FILE__).'/syncback.log');
    124        
    125         $this->_log(ADI_LOG_INFO,"-------------------------------------\n".
    126                                  "START OF BULK SYNCBACK\n".
    127                                  date('Y-m-d / H:i:s')."\n".
    128                                  "-------------------------------------\n");
    129        
    130         $time = time();
    131         $updated_users = 0;
    132         $all_users = array();
    133        
    134         // Is bulk syncback enabled?
    135         if (!$this->_syncback) {
    136             $this->_log(ADI_LOG_INFO,'SyncBack is disabled.');
    137             return false;
    138         }
    139        
    140         $ad_password = $this->_decrypt($this->_syncback_global_pwd);
    141        
    142         // Log informations
    143         $this->_log(ADI_LOG_INFO,"Options for adLDAP connection:\n".
    144                       "- base_dn: $this->_base_dn\n".
    145                       "- domain_controllers: $this->_domain_controllers\n".
    146                       "- ad_username: $this->_syncback_global_user\n".
    147                       "- ad_password: **not shown**\n".
    148                       "- ad_port: $this->_port\n".
    149                       "- use_tls: ".(int) $this->_use_tls."\n".
    150                       "- network timeout: ". $this->_network_timeout);
    151            
    152         // Connect to Active Directory
    153         try {
    154             $this->_adldap = @new adLDAP(array(
    155                         "base_dn" => $this->_base_dn,
    156                         "domain_controllers" => explode(';', $this->_domain_controllers),
    157                         "ad_username" => $this->_syncback_global_user,      // Bulk Import User
    158                         "ad_password" => $ad_password,                  // password
    159                         "ad_port" => $this->_port,                      // AD port
    160                         "use_tls" => $this->_use_tls,                   // secure?
    161                         "network_timeout" => $this->_network_timeout    // network timeout
    162                         ));
    163         } catch (Exception $e) {
    164             $this->_log(ADI_LOG_ERROR,'adLDAP exception: ' . $e->getMessage());
    165             return false;
    166         }
    167         $this->_log(ADI_LOG_NOTICE,'adLDAP object created.');
    168         $this->_log(ADI_LOG_INFO,'Domain Controller: ' . $this->_adldap->get_last_used_dc());
    169        
    170         // Let's give us some more time (60 minutes)
    171         $max_execution_time = ini_get('max_execution_time');
    172         if ($max_execution_time < 3600) {
    173             ini_set('max_execution_time', 3600);
    174         }
    175         if (ini_get('max_execution_time') < 3600) {
    176             $this->_log(ADI_LOG_ERROR,'Can not increase PHP configuration option "max_execution_time".');
    177             return false;
    178         }
    179 
    180         $attributes = $this->_get_attributes_array();
    181         $this->_log(ADI_LOG_DEBUG, 'attributes: ' . print_r($attributes, true));
    182 
    183         // Do we have possible attributes for SyncBack?
    184         if (count($attributes) > 0) {
    185        
    186        
    187             // Get IDs of users to SyncBack
    188             // They must have a wp_usermeta.metakey = 'adi_samaccount' with a not empty meta_value and User 1 (admin) is excluded.
    189             if ($userid == NULL) {
    190                 $users = $wpdb->get_results("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'adi_samaccountname' AND meta_value <> '' AND user_id <> 1 ORDER BY user_id");
    191             } else {
    192                 $users = $wpdb->get_results("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'adi_samaccountname' AND meta_value <> '' AND user_id <> 1 AND user_id = $userid");
    193             }
    194 
    195             // Do we have possible users for SyncBack?
    196             if ($users) {
    197                 foreach ( $users as $user ) {
    198                    
    199                     $userinfo = get_userdata($user->user_id);
    200                     if ($userinfo) {
    201                         $this->_log(ADI_LOG_INFO, 'User-Login: '.$userinfo->user_login);
    202                         $this->_log(ADI_LOG_INFO, 'User-ID: '.$user->user_id);
    203                    
    204                        
    205                         $no_attribute = false;
    206                         $attributes_to_sync = array();
    207                         foreach ($attributes AS $key => $attribute) {
    208                            
    209                             if ($no_attribute === false) {
    210                                
    211                                 if (isset($attribute['sync']) && ($attribute['sync'] == true)) {
    212                                     // $value = get_user_meta($user->user_id, $attribute['metakey'], true); // BAD
    213                                      $value = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->usermeta WHERE meta_key = '".$attribute['metakey']."' AND user_id = ".$user->user_id ) );
    214                                      
    215                                     if ($value !== FALSE) {
    216                                         if ($attribute['type'] == 'list') {
    217                                             // List
    218                                             $list = explode("\n",str_replace("\r",'',$value));
    219                                             $i=0;
    220                                             foreach ($list AS $line) {
    221                                                 if (trim($line) != '') {
    222                                                     $attributes_to_sync[$key][$i] = $line;
    223                                                     $i++;
    224                                                 }
    225                                             }
    226                                             if ($i == 0) {
    227                                                 $attributes_to_sync[$key][0] = ' '; // Use a SPACE !!!
    228                                             }
    229                                         } else {
    230                                             // single value
    231                                             if ($value == '') {
    232                                                 $attributes_to_sync[$key][0] = ' '; // Use a SPACE !!!!
    233                                             } else {
    234                                                 $attributes_to_sync[$key][0] = $value;
    235                                             }
    236                                         }
    237                                     }
    238                                    
    239                                 }
    240                             }
    241                         }
    242                
    243                         //  Now we can modify the user
    244                         $this->_log(ADI_LOG_INFO,'attributes to sync: '.print_r($attributes_to_sync, true));
    245                         $this->_log(ADI_LOG_DEBUG,'modifying user: '.$userinfo->user_login);
    246                         $modified = $this->_adldap->user_modify_without_schema($userinfo->user_login, $attributes_to_sync);
    247                         if (!$modified) {
    248                             $this->_log(ADI_LOG_WARN,'SyncBack: modifying user failed');
    249                             $this->_log(ADI_LOG_DEBUG,$this->_adldap->get_last_errno().': '.$this->_adldap->get_last_error());
    250                         } else {
    251                             $this->_log(ADI_LOG_NOTICE,'SyncBack: User successfully modified.');
    252                             $updated_users++;
    253                         }
    254                     } else {
    255                         $this->_log(ADI_LOG_NOTICE,'User with ID ' . $user->user_id .' not found.');
    256                     }
    257                 }
    258             } else {
    259                 $this->_log(ADI_LOG_INFO, 'No possible users for SyncBack found.');
    260             }
    261         } else {
    262             $this->_log(ADI_LOG_INFO, 'No possible attributes for SyncBack found.');
    263         }
    264 
    265         // Logging 
    266         $elapsed_time = time() - $time;
    267         $this->_log(ADI_LOG_INFO,$updated_users . ' Users updated.');
    268         $this->_log(ADI_LOG_INFO,'In '. $elapsed_time . ' seconds.');
    269        
    270         $this->_log(ADI_LOG_INFO,"-------------------------------------\n".
    271                                  "END OF BULK SYNCBACK\n".
    272                                  date('Y-m-d / H:i:s')."\n".
    273                                  "-------------------------------------\n");       
    274 
    275         return true;
    276     }
    277        
    278 }
    27959
    28060define('ADINTEGRATION_DEBUG', true);
Note: See TracChangeset for help on using the changeset viewer.