/** * Create / Add users */ function ns_wp_add_user($target_id, $useremail, $username, $userpass = '', $userrole = 'administrator', $logfile = false) { global $ns_cloner; ns_log_write("ENTER ns_wp_add_user - target_id:{$target_id}, useremail:{$useremail}, username:{$username}, userrole:{$userrole}", $logfile); $useremail = stripslashes($useremail); $username = stripslashes($username); $userpass = stripslashes($userpass); $user_by_email = get_user_by('email', $useremail); $user_by_username = get_user_by('username', $username); // check for existing user by email if (!empty($user_by_email)) { $user_id = $user_by_email->ID; ns_log_write("Found user with email '{$useremail}' (id={$user_id})", $logfile); } elseif (!empty($user_by_username)) { $user_id = $user_by_username->ID; ns_log_write("Found user with username '{$username}' (id={$user_id})", $logfile); } else { if (empty($userpass) || $userpass == strtolower('null')) { $userpass = wp_generate_password(); } $user_id = wpmu_create_user($username, $userpass, $useremail); if ($user_id != false) { ns_log_write("Created new user '{$username}' with email '{$useremail}'", $logfile); // send notification to new users if the option is set if (isset($ns_cloner->request['do_user_notify'])) { wpmu_welcome_notification($target_id, $user_id, $userpass, 'New Site with ID: ' . $target_id); ns_log_write("Sent welcome email to new user '{$username}' with email '{$useremail}'", $logfile); } } else { ns_log_write("Failed creating user '{$username}' with email '{$useremail}' - that username or email is probably already taken for a different user.", $logfile); } } // we now have a user id (or should) - give them privileges on this blog if (!empty($target_id) && !empty($user_id) && !empty($userrole)) { $result = add_user_to_blog($target_id, $user_id, $userrole); if ($result === true) { ns_log_write("Successfully added user with id {$user_id} to blog {$target_id}", $logfile); } else { $error_message = $result->get_error_message(); ns_log_write("Failed adding user to blog. WP error: {$error_message}", $logfile); } return $result; } else { $error_message = "Target id, user id, or user role were empty"; ns_log_write("Failed adding user to blog. {$error_message}", $logfile); return new WP_Error(false, $error_message); } }
/** * Organize a sequence of search/replace values (adding new corrective search/replace pairs) to avoid compounding replacement issues * @param array - **by reference** - $search strings of search text to sort/process * @param array - **by reference** - $replace strings of replacement text to sort/process (keeping same order as $search of course so no mix ups) * @param array - **by reference** - $regex_search strings of regular expressions (this func may add to the array but won't manipulate anything already in it) * @param array - **by reference** - $regex_replace strings of replacement text for $regex_search (also may be added to but will not be otherwise modified) * @return void */ function ns_set_search_replace_sequence(&$search, &$replace, &$regex_search, &$regex_replace, $logfile = false) { $fix_insertion_index = 1; // Sort string replacements by order longest to shortest to prevent a situation like // Source Site w/ url="neversettle.it",uploaddir="/neversettle.it/wp-content/uploads" and Target Site w/url="blog.neversettle.it",uploaddir="/neversettle.it/wp-content/uploads/sites/2" // resulting in target uploaddir being "/blog.neversettle.it/wp-content/uploads" when url replacement is applied before uploaddir replacement $search_replace = array_unique(array_combine($search, $replace)); uksort($search_replace, create_function('$a,$b', 'return strlen($b)-strlen($a);')); $search = $new_search = array_keys($search_replace); $replace = $new_replace = array_values($search_replace); // If any search terms are found in replace terms which have already been inserted (ie came earlier in the find/replace sequence), remove this search term plus // its accompanying replacement from the string-based search/replace and a correction to change that replacement back again so replacements won't be compounded. // This prevents a situation like Source Site w/ title="Brown",url="brown.com" and Target Site w/ title="Brown Subsite",url="subsite.brown.com" // resulting in target urls like "subsite.Brown Subsite.com" when title replacement is applied after url replacement. $regex_search = $regex_replace = $repeated_exact_conflicts = array(); foreach ($search as $index => $search_text) { // figure out what the desired replace text is from other array in case we need it $replace_text = $replace[$index]; // get replacements earlier in array (which could've already been inserted into text so we need to watch out for them) $past_replacements = array_slice($replace, 0, $index); // identify any of those replacements which the search text and save as array of conflicts $conflicting_replacements = array_filter($past_replacements, create_function('$past_replace_text', 'return stripos($past_replace_text,"' . $search_text . '")!==false;')); if (!empty($conflicting_replacements)) { ns_log_write("Conflicting replacement found: search text '{$search_text}' appears in one or more previous replacement(s): '" . join("','", $conflicting_replacements) . "'", $logfile); foreach ($conflicting_replacements as $conflicting_replacement) { // if it's an exact match, assume it's supposed to happen and skip fixing it - not 100% sure what the right logic is here, but seems more likely to be right this way if ($conflicting_replacement == $search_text) { ns_log_write("Conflicting exact match replacement found: '{$search_text}'. Skipping since this is most likely supposed to happen", $logfile); continue; } // insert into the search/replace arrays right after the current item which will produce the bad replacement array_splice($new_search, $fix_insertion_index, 0, str_ireplace($search_text, $replace_text, $conflicting_replacement)); array_splice($new_replace, $fix_insertion_index, 0, $conflicting_replacement); $fix_insertion_index++; } } $fix_insertion_index++; } $search = $new_search; $replace = $new_replace; }
public function dlog($message, $debug_only = false) { $is_ajax = $this->current_action == "ajax_validate" || defined('DOING_AJAX') && DOING_AJAX == true; $is_extra_debug_on = isset($this->request["debug"]) && $this->request["debug"] == true; if (($debug_only == true || $is_ajax) && !$is_extra_debug_on) { return; } ns_log_write($message, NS_CLONER_LOG_FILE_DETAILED); }
function ns_get_upload_dir($id, $logfile = false) { // --------------------------------------------------------------------------------------------------------------------- // T E S T I N G N E W C O D E // --------------------------------------------------------------------------------------------------------------------- // initialize switch_to_blog($id); $wp_upload_dir_a = wp_upload_dir(); restore_current_blog(); $wp_upload_dir = ns_norm_winpath($wp_upload_dir_a['basedir']); $upload_dir = ''; // eventual return value // handle difference between ID = 1 and everything else // in ID = 1 test_1 and test_2 are identical if ($id == 1) { $test_1_base = ns_norm_winpath(WP_CONTENT_DIR . '/uploads'); $test_1_upload_dir = ns_norm_winpath($test_1_base); $test_2_base = ns_norm_winpath(WP_CONTENT_DIR . '/uploads'); $test_2_upload_dir = ns_norm_winpath($test_2_base); } else { $test_1_base = ns_norm_winpath(WP_CONTENT_DIR . '/blogs.dir'); $test_1_upload_dir = ns_norm_winpath($test_1_base . '/' . $id . '/files'); $test_2_base = ns_norm_winpath(WP_CONTENT_DIR . '/uploads/sites'); $test_2_upload_dir = ns_norm_winpath($test_2_base . '/' . $id); } // compare and set conditions for determining confidence level $test_1_base_exists = file_exists($test_1_base) ? true : false; $test_1_upload_dir_exists = file_exists($test_1_upload_dir) ? true : false; $test_1_matches_wp = $test_1_upload_dir == $wp_upload_dir ? true : false; $test_2_base_exists = file_exists($test_2_base) ? true : false; $test_2_upload_dir_exists = file_exists($test_2_upload_dir) ? true : false; $test_2_matches_wp = $test_2_upload_dir == $wp_upload_dir ? true : false; $wp_upload_dir_exists = file_exists($wp_upload_dir) ? true : false; // cascade in order of confidence // HIGH CONFIDENCE ---------------------------------------------------------------------------------- // WP Origin < 3.5 if ($test_1_base_exists && $test_1_upload_dir_exists && $test_1_matches_wp) { $upload_dir = $test_1_upload_dir; $confidence = 'HIGH CONFIDENCE'; } // WP Origin > 3.5 (fall through if $upload_dir already set) if ($upload_dir == '' && $test_2_base_exists && $test_2_upload_dir_exists && $test_2_matches_wp) { $upload_dir = $test_2_upload_dir; $confidence = 'HIGH CONFIDENCE'; } // MEDIUM CONFIDENCE -------------------------------------------------------------------------------- // WP Origin < 3.5 (fall through if $upload_dir already set) if ($upload_dir == '' && $test_1_base_exists && $test_1_matches_wp) { $upload_dir = $test_1_upload_dir; $confidence = 'MEDIUM CONFIDENCE'; } // WP Origin > 3.5 (fall through if $upload_dir already set) if ($upload_dir == '' && $test_2_base_exists && $test_2_matches_wp) { $upload_dir = $test_2_upload_dir; $confidence = 'MEDIUM CONFIDENCE'; } // LOW CONFIDENCE ----------------------------------------------------------------------------------- // WP Origin < 3.5 (fall through if $upload_dir already set) if ($upload_dir == '' && $test_1_base_exists) { $upload_dir = $test_1_upload_dir; $confidence = 'LOW CONFIDENCE'; } // WP Origin > 3.5 (fall through if $upload_dir already set) if ($upload_dir == '' && $test_2_base_exists) { $upload_dir = $test_2_upload_dir; $confidence = 'LOW CONFIDENCE'; } // FAIL SAFE ---------------------------------------------------------------------------------------- if ($upload_dir == '') { $upload_dir = $wp_upload_dir; $confidence = 'FAIL SAFE'; } // log results for debugging ns_log_section_break($logfile); ns_log_write('TESTING UPLOAD LOCATION for ID = ' . $id, $logfile); ns_log_section_break($logfile); ns_log_write('wp_upload_dir = ' . $wp_upload_dir . ns_t2e($wp_upload_dir_exists), $logfile); ns_log_write('test_1_base = ' . $test_1_base . ns_t2e($test_1_base_exists), $logfile); ns_log_write('test_1_upload_dir = ' . $test_1_upload_dir . ns_t2e($test_1_upload_dir_exists), $logfile); ns_log_write('test_1_matches_wp = ' . ns_t2e($test_1_matches_wp), $logfile); ns_log_write('test_2_base = ' . $test_2_base . ns_t2e($test_2_base_exists), $logfile); ns_log_write('test_2_upload_dir = ' . $test_2_upload_dir . ns_t2e($test_2_upload_dir_exists), $logfile); ns_log_write('test_2_matches_wp = ' . ns_t2e($test_2_matches_wp), $logfile); ns_log_write('<b>upload_dir = ' . $upload_dir . '</b> with ' . $confidence, $logfile); ns_log_section_break($logfile); return $upload_dir; }
/** * Diagnostics */ function ns_diag($logfile) { global $wp_version, $wp_db_version, $required_php_version, $required_mysql_version; ns_log_write("ENVIRONMENT DIAGNOSTICS:", $logfile); ns_log_write("Web Server Info:", $logfile); ns_log_write("PHP Version Required: <strong>" . $required_php_version . " </strong>", $logfile); ns_log_write("PHP Version Current: <strong>" . phpversion() . " </strong>", $logfile); ns_log_write("MySQL Version Required: <strong>" . $required_mysql_version . " </strong>", $logfile); ns_log_write("MySQL Version Current: <strong>" . ns_get_mysql_variable('version') . " </strong>", $logfile); ns_log_write("WP Version: <strong>{$wp_version}</strong>", $logfile); ns_log_write("WP Memory Limit: <strong>" . WP_MEMORY_LIMIT . " </strong>", $logfile); ns_log_write("WP Debug Mode: <strong>" . WP_DEBUG . " </strong>", $logfile); ns_log_write("WP Multisite: <strong>" . MULTISITE . " </strong>", $logfile); ns_log_write("WP Subdomain Install: <strong>" . SUBDOMAIN_INSTALL . " </strong>", $logfile); ns_log_write("PHP Post Max Size: <strong>" . ini_get('post_max_size') . " </strong>", $logfile); ns_log_write("PHP Upload Max Size: <strong>" . ini_get('upload_max_size') . " </strong>", $logfile); ns_log_write("PHP Memory Limit: <strong>" . ini_get('memory_limit') . " </strong>", $logfile); ns_log_write("PHP Max Input Vars: <strong>" . ini_get('max_input_vars') . " </strong>", $logfile); ns_log_write("PHP Max Execution Time: <strong>" . ini_get('max_execution_time') . " </strong>", $logfile); ns_log_section_break($logfile); ns_log_write("PLUGIN DIAGNOSTICS:", $logfile); foreach (get_plugins() as $plugin_file => $data) { ns_log_write("{$data['Name']} {$data['Version']} by {$data['Author']}" . ($data["Network"] == true ? " <strong>Network Enabled</strong>" : ""), $logfile); } }