verbose; if($verbose && self::$instance->verbose || !$verbose && !self::$instance->verbose){ echo($data); } flush(); $theDirectory = dirname(__FILE__) . '/logs/'; if(is_dir($theDirectory)){ $fileName = date('Y-m-d') . '.log'; file_put_contents($theDirectory . $fileName, $data, FILE_APPEND ); } } public function __construct(){ if(is_null(self::$instance) ){ /* * Singleton pattern: The hooks will be invoked once! */ add_action('admin_init', array($this , 'init')); add_action('admin_menu', array($this, 'createPages')); self::$instance = $this; } return self::$instance; } public function init(){ load_plugin_textdomain(self::LANG_DOMAIN, false, basename(dirname( __FILE__ )).'/languages/' ); if(isset($_GET['aeiou']) && $_GET['aeiou'] == 'export'){ $this->exportData(); } } private function toXMLItem($user){ echo("\tID}\">\n"); echo("\t\tuser_login}]]>\n"); /* * I should export Password too (as requested by user) */ if(isset($_GET['pwd'])) echo("user_pass}]]>\n"); echo("\t\tuser_nicename}]]>\n"); echo("\t\tuser_email}]]>\n"); echo("\t\tuser_url}]]>\n"); echo("\t\tuser_registered}]]>\n"); echo("\t\tuser_activation_key}]]>\n"); echo("\t\tuser_status}]]>\n"); echo("\t\tdisplay_name}]]>\n"); if($this->exportMetadata || $this->exportOptions){ /* * I should export metadata (as requested by user) */ echo("\t\t"); global $wpdb; $umeta = get_user_meta($user->ID); foreach($umeta as $key => $value){ $isOption = preg_match("#^" . preg_quote($wpdb->prefix) ."#", $key); if( ($isOption && $this->exportOptions) || (!$isOption && $this->exportMetadata) ){ echo("\t\t\tprefix) ."#", $key)){ /* * If the value is an option i should remove the db prefix from the option * name and add the attribute option to allow the importer to understand * when a node is an option or a setting. */ echo('option="true" '); echo('global="false" '); $key =substr($key, strlen($wpdb->prefix)); } echo("key=\"$key\">\n"); } } echo("\t\t"); } #if( class_exists('BuddyPress') ){ if(class_exists('BP_XProfile_Component') && $this->exportXProfile ){ /* * I should export xprofile data too. */ $groups = BP_XProfile_Group::get( array( 'fetch_fields' => true ) ); /* * Fetching all groups */ echo("\t\t"); foreach($groups as $group){ $groupName = $group->name; if(!empty($group->fields)){ /* * Has fields in group */ foreach($group->fields as $field){ $value = xprofile_get_field_data($field->id, $user->ID, 'array'); echo("name}\">"); $value = serialize($value); # Ver 0.7 $value = base64_encode($value); echo $value; echo(""); } } } echo("\t\t"); } #} do_action('aeiou_export_extra_data', $user); echo("\t\n\n"); } public function exportData(){ if(isset($_GET['aeiou']) && $_GET['aeiou'] == 'export'){ $u = new WP_User_Query(array('fields' => 'all')); $users = $u->get_results(); /* * Make the XML but ensure it will be downloaded */ $url = site_url(); $url = preg_replace('#[^a-z0-9_\-.]+#i','_', $url); $date = date('Y-m-d-h-i-s'); header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-type: application/xml; encoding=utf-8'); header("Content-Disposition: attachment; filename=aeiou_{$url}_{$date}.xml"); header("Content-Transfer-Encoding: binary"); echo("\n"); echo("\n\n"); echo("\n"); $this->exportMetadata = isset($_GET['metadata']); $this->exportOptions = isset($_GET['options']); $this->exportXProfile = isset($_GET['xprofile']); foreach($users as $user){ echo $this->toXMLItem($user); } echo(""); exit(); } } public function createPages(){ add_users_page("AEIOU - Panel", 'AEIOU', 'add_users', 'aeiou', array($this, 'thePage')); } public function thePage(){ global $wpdb; $screen = WP_Screen::get(); add_meta_box('div-export-options-' .$screen->id, __('Export Users', self::LANG_DOMAIN), array($this,'addExportMetabox'), $screen, 'normal', 'high'); add_meta_box('div-import-options' . $screen->id, __('Import Users', self::LANG_DOMAIN), array($this,'addImportMetabox'), $screen, 'normal', 'high'); wp_enqueue_script('post'); add_screen_option('layout_columns', array('max' => 1, 'default' => 1) ); global $wp_meta_boxes; ?>

consider a donation. ', self::LANG_DOMAIN); ?> a lunch.', self::LANG_DOMAIN), 'https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=info@tosend.it&item_name=Donation%20for%20AEIOU&item_number=1&amount=30%2e00¤cy_code=EUR' ); ?>

'all')); $userCount = $u->get_total() * 10; $amt = intval($userCount / 100); $dec = "00".($userCount % 100); $dec = substr($dec, -2); ?> 0,10 € (euro) per user could be the right ammount.', self::LANG_DOMAIN), "https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=info@tosend.it&item_name=Donation%20for%20AEIOU&item_number=1&amount=$amt%2e$dec¤cy_code=EUR") ; ?>

contact us for a consulting.', self::LANG_DOMAIN)?>

WordPress support forum for this plugin.', self::LANG_DOMAIN), 'http://wordpress.org/support/plugin/aeiou'); ?>

AUTH_KEY, SECURE_AUTH_KEY LOGGED_IN_KEY.

" />

found with the same username):', self::LANG_DOMAIN)?>




" />

verbose 	= isset($_POST['verbose']);
						
						/*
						 * The behavior when a wordpress user is found could be:
						 * - skip
						 * - replace
						 * - update
						 */
						$this->exists			= isset($_POST['exists'])?$_POST['exists']:'skip';
						
						
						$this->importMetadata 	= isset($_POST['metadata']);
						$this->importOptions 	= isset($_POST['options']);
						$this->importXProfile	= isset($_POST['xprofile']);
						
						
						if($theFile['error'] != 0){
							/*
							 * Some errors occours
							 */
							echo $this->decodeFileError($theFile['error']);
						}else{
							/*
							 * Starting import
							 */
							$this->outputLog( __('Import started.', self::LANG_DOMAIN) . "\n\n" );
							_e('Please do not refresh the page and not go anywhere.'); echo "\n";
							_e('Interruption of page execution could cause data loss and user table to break!'); echo "\n"; 
							$parser = new AEIOU_Parser_SimpleXML();
							$users = $parser->parse($theFile['tmp_name']);
							
							foreach($users as $user){
								/*
								 * Increase the time limit up to 10 seconds per user
								 */
								set_time_limit(10);
								
								$login = $user['user_login'];
								$this->outputLog("\n[$login] ");
								$usr = get_user_by('login', $login);
								
								$userID = 0;
								
								$userMeta 		= (isset($user['metadata'])) 	? $user['metadata'] : array();
								/*
								 * v0.2: Added user options and xprofile import
								 */
								$userOptions 	= (isset($user['options'])) 	? $user['options'] : array();
								$xprofile 		= (isset($user['xprofile'])) 	? $user['xprofile'] : array();
								
								/*
								 * Cleanup extra keys
								 */
								if(isset($user['metadata'])) 	unset($user['metadata']);
								if(isset($user['options'])) 	unset($user['options']);
								if(isset($user['xprofile'])) 	unset($user['xprofile']);
								
								do_action('aeiou_before_import_user', $user);
								
								if($usr && $this->exists != 'skip'){
									$usr = (array) $usr->data;
									
									if($this->exists == 'update'){
										
										/*
										 * Keeping metadata and removing from array
										 */
										
										foreach($user as $key => $value){
											/*
											 * Removing all non empty key from import
											 */
											if(isset($usr[$key]) && !empty($usr[$key])){
												unset($user[$key]);
											}
										}
									}
									
									if(count($user)>0){
										$changes = array_keys($user);
										$changes = "`".implode("`, `", $changes)."`";
										
										$this->outputLog( sprintf(__("has new %s value(s).", self::LANG_DOMAIN),$changes) . "\n");
										$user['ID'] = $usr['ID'];
										/*
										 * If password is defined in the XML file the script uses it (as is)!
										 */
										wp_update_user($user);
										global $wpdb;
											
										if(isset($usr['user_pass']))
											$wpdb->query(
												$wpdb->prepare("update {$wpdb->prefix}_users set user_pass='%s' where ID=%d", $user['user_pass'], $user['ID'])
											);
										
									}else{
										_e("unchanged.", self::LANG_DOMAIN) . "\n";
									}
									$userID = $user['ID'];
									
								}else{
								
									if(!$usr){
										/*
										 * New User must be created
										 */
										if(!isset($user['user_pass'])) $user['user_pass'] = ''; 
										$userID = wp_insert_user($user);
										
										/*
										 * If i was unable to import the user I will give the error message to the admin.
										 */
										if(is_wp_error($userID)){
											
											$this->outputLog( $userID->get_error_message() . "\n" );
											
										}else{
											$user['ID'] = $userID;
											/*
											 * If password is defined in the XML file we should to import it
											 */
											if(isset($usr['user_pass']))
												$wpdb->query(
														$wpdb->prepare("update {$wpdb->prefix}_users set user_pass='%s' where ID=%d", $user['user_pass'], $user['ID'])
														);

											$this->outputLog( sprintf(__("inserted with ID %d", self::LANG_DOMAIN),$userID) . "\n" );
										}
									}else{
										$this->outputLog( __("skipped due it's already in the database", self::LANG_DOMAIN) . "\n" );
										$userID = $usr->ID;
									}
								}
								if(!is_wp_error($userID)){
									
									if($this->importMetadata && $userID != 0){
										do_action('aeiou_before_import_metadata', $user);
										$this->importUserMetaData($userMeta, $userID, $login);
									}
									
									if($this->importOptions && $userID != 0){
										do_action('aeiou_before_import_options', $user);
										$this->outputLog( __("Importing user options\n", self::LANG_DOMAIN) );
										$this->importUserOptions($userOptions, $userID, $login);
									}
									
									if($this->importXProfile && $userID != 0 && class_exists('BP_XProfile_Component')){
										do_action('aeiou_before_import_xprofile', $user);
										$this->outputLog( __("Importing XProfile data\n", self::LANG_DOMAIN) );
										$this->importXProfileData($xprofile, $userID);
									}
									do_action('aeiou_after_import_user', $user);
								}
								
							}
							
							
						}
					}
				}
				?>
			
$value){ $metaKey = get_user_meta($userID, $key); if(empty($metaKey[0])) $metaKey = false; if(!$metaKey || $this->exists=='replace'){ $value = maybe_unserialize($value); delete_user_meta($userID, $key); if( !empty( $value ) ){ $count+=1; $this->outputLog( sprintf(__("Metadata `%s` updated for user [%s]", self::LANG_DOMAIN), $key, $login) . "\n", true); update_user_meta($userID, $key, $value ); }else{ if($metaKey) $this->outputLog( sprintf(__("Metadata `%s` removed for user [%s]", self::LANG_DOMAIN), $key, $login) . "\n", true); } } } if($count>0) $this->outputLog( sprintf(__("Updated %d metadatas for user [%s]", self::LANG_DOMAIN), $count, $login) . "\n"); } private function importUserOptions($userOptions, $userID, $login){ /* * Admin asked for options import */ $count = 0; foreach($userOptions as $key => $data){ list($value, $global) = $data; $option = get_user_option( $key, $userID); $optExists = !empty($option); $value = maybe_unserialize($value); if(!$optExists || $this->exists=='replace'){ delete_user_option($userID, $key); if( !empty( $value ) ){ $count+=1; $this->outputLog( sprintf(__("Option `%s` updated for user [%s]", self::LANG_DOMAIN), $key, $login) . "\n" , true); update_user_option($userID, $key, $value, $global ); }else{ if($optExists) $this->outputLog( sprintf(__("Option `%s` removed for user [%s]", self::LANG_DOMAIN), $key, $login) . "\n" ); } } } if($count>0) $this->outputLog( sprintf(__("Updated %d metadatas for user [%s]", self::LANG_DOMAIN), $count, $login) . "\n"); } private function importXProfileData($xprofile, $userID){ /* * Admin asked for xprofile import */ $count = 0; foreach($xprofile as $name => $data){ $value = $data[0]; $group = $data[1]; $this->outputLog( sprintf(__("Importing field `%s` of group `%s`", self::LANG_DOMAIN), $name, $group) . "\n", true); /* * Get the XProfile Fields Group ID * If it's not available the will be created as new. */ $groupId = $this->getXProfileGroupIDByName($group); /* * Get the XProfile Field ID * If it's not available the will be created as new. */ $fieldId = $this->getXProfileFieldIDByName($name, $groupId); $fieldData = maybe_unserialize($value); $oldValue = BP_XProfile_ProfileData::get_value_byid($fieldId, $userID); if(is_null($oldValue) || ($oldValue == '' && $this->exists == 'update') || ($this->exists == 'replace') ){ /* * Saving data if not exists or need to be updated */ $data = new BP_XProfile_ProfileData(); $data->field_id = $fieldId; $data->user_id = $userID; $data->value = maybe_unserialize($value); $data->save(); } } } private function getXProfileFieldIDByName($name, $groupId = 0, $create = true){ $out = BP_XProfile_Field::get_id_from_name($name); if(is_null($out) && $create){ $field = new BP_XProfile_Field(); $field->name = $name; $field->group_id = $groupId; /* * TODO: Store in the XML the type. * Aactually i will create a generic text type if the field is not available. */ $field->type = "text"; $out = $field->save(); } return $out; } private function getXProfileGroupIDByName($name, $create = true){ global $wpdb, $bp; $sql = "select id from {$bp->profile->table_name_groups} where name = %s"; $sql = $wpdb->prepare($sql, $name); $out = $wpdb->get_var($sql); if( is_null( $out ) && $create ){ $newGroup = new BP_XProfile_Group(); $newGroup->name = $name; $out = $newGroup->save(); } return $out; } } $aeiou = new AEIOU(); } ?>