Beispiel #1
0
/**
 * Populate network settings.
 *
 * @since 0.0.1
 *
 * @global hqdb       $hqdb
 * @global object     $current_site
 * @global int        $hq_db_version
 * @global HQ_Rewrite $hq_rewrite
 *
 * @param int $network_id ID of network to populate.
 * @return bool|HQ_Error True on success, or HQ_Error on warning (with the install otherwise successful,
 *                       so the error code must be checked) or failure.
 */
function populate_network($network_id = 1, $domain = '', $email = '', $site_name = '', $path = '/', $subdomain_install = false)
{
    global $hqdb, $current_site, $hq_db_version, $hq_rewrite;
    $errors = new HQ_Error();
    if ('' == $domain) {
        $errors->add('empty_domain', __('You must provide a domain name.'));
    }
    if ('' == $site_name) {
        $errors->add('empty_sitename', __('You must provide a name for your network of sites.'));
    }
    // Check for network collision.
    if ($network_id == $hqdb->get_var($hqdb->prepare("SELECT id FROM {$hqdb->site} WHERE id = %d", $network_id))) {
        $errors->add('siteid_exists', __('The network already exists.'));
    }
    $site_user = get_user_by('email', $email);
    if (!is_email($email)) {
        $errors->add('invalid_email', __('You must provide a valid e-mail address.'));
    }
    if ($errors->get_error_code()) {
        return $errors;
    }
    // Set up site tables.
    $template = get_option('template');
    $stylesheet = get_option('stylesheet');
    $allowed_themes = array($stylesheet => true);
    if ($template != $stylesheet) {
        $allowed_themes[$template] = true;
    }
    if (HQ_DEFAULT_THEME != $stylesheet && HQ_DEFAULT_THEME != $template) {
        $allowed_themes[HQ_DEFAULT_THEME] = true;
    }
    if (1 == $network_id) {
        $hqdb->insert($hqdb->site, array('domain' => $domain, 'path' => $path));
        $network_id = $hqdb->insert_id;
    } else {
        $hqdb->insert($hqdb->site, array('domain' => $domain, 'path' => $path, 'id' => $network_id));
    }
    hq_cache_delete('networks_have_paths', 'site-options');
    //TODO: no multisite
    //if ( !is_multisite() ) {
    $site_admins = array($site_user->user_login);
    $users = get_users(array('fields' => array('ID', 'user_login')));
    if ($users) {
        foreach ($users as $user) {
            if (is_super_admin($user->ID) && !in_array($user->user_login, $site_admins)) {
                $site_admins[] = $user->user_login;
            }
        }
    }
    //} else {
    //	$site_admins = get_site_option( 'site_admins' );
    //}
    /* translators: Do not translate USERNAME, SITE_NAME, BLOG_URL, PASSWORD: those are placeholders. */
    $welcome_email = __('Howdy USERNAME,

Your new SITE_NAME site has been successfully set up at:
BLOG_URL

You can log in to the administrator account with the following information:

Username: USERNAME
Password: PASSWORD
Log in here: BLOG_URLhq-login.php

We hope you enjoy your new site. Thanks!

--The Team @ SITE_NAME');
    $misc_exts = array('jpg', 'jpeg', 'png', 'gif', 'mov', 'avi', 'mpg', '3gp', '3g2', 'midi', 'mid', 'pdf', 'doc', 'ppt', 'odt', 'pptx', 'docx', 'pps', 'ppsx', 'xls', 'xlsx', 'key');
    $audio_exts = hq_get_audio_extensions();
    $video_exts = hq_get_video_extensions();
    $upload_filetypes = array_unique(array_merge($misc_exts, $audio_exts, $video_exts));
    $sitemeta = array('site_name' => $site_name, 'admin_email' => $site_user->user_email, 'admin_user_id' => $site_user->ID, 'registration' => 'none', 'upload_filetypes' => implode(' ', $upload_filetypes), 'blog_upload_space' => 100, 'fileupload_maxk' => 1500, 'site_admins' => $site_admins, 'allowedthemes' => $allowed_themes, 'illegal_names' => array('www', 'web', 'root', 'admin', 'main', 'invite', 'administrator', 'files'), 'hqmu_upgrade_site' => $hq_db_version, 'welcome_email' => $welcome_email, 'first_post' => __('Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!'), 'siteurl' => get_option('siteurl') . '/', 'add_new_users' => '0', 'upload_space_check_disabled' => '1', 'subdomain_install' => intval($subdomain_install), 'global_terms_enabled' => global_terms_enabled() ? '1' : '0', 'ms_files_rewriting' => '0', 'initial_db_version' => get_option('initial_db_version'), 'active_sitewide_plugins' => array(), 'HQLANG' => get_locale());
    if (!$subdomain_install) {
        $sitemeta['illegal_names'][] = 'blog';
    }
    /**
     * Filter meta for a network on creation.
     *
     * @since 0.0.1
     *
     * @param array $sitemeta   Associative array of network meta keys and values to be inserted.
     * @param int   $network_id ID of network to populate.
     */
    $sitemeta = apply_filters('populate_network_meta', $sitemeta, $network_id);
    $insert = '';
    foreach ($sitemeta as $meta_key => $meta_value) {
        if (is_array($meta_value)) {
            $meta_value = serialize($meta_value);
        }
        if (!empty($insert)) {
            $insert .= ', ';
        }
        $insert .= $hqdb->prepare("( %d, %s, %s)", $network_id, $meta_key, $meta_value);
    }
    $hqdb->query("INSERT INTO {$hqdb->sitemeta} ( site_id, meta_key, meta_value ) VALUES " . $insert);
    /*
     * When upgrading from single to multisite, assume the current site will
     * become the main site of the network. When using populate_network()
     * to create another network in an existing multisite environment, skip
     * these steps since the main site of the new network has not yet been
     * created.
     */
    //TODO: no multisite
    //if ( ! is_multisite() ) {
    $current_site = new stdClass();
    $current_site->domain = $domain;
    $current_site->path = $path;
    $current_site->site_name = ucfirst($domain);
    $hqdb->insert($hqdb->blogs, array('site_id' => $network_id, 'blog_id' => 1, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')));
    $current_site->blog_id = $blog_id = $hqdb->insert_id;
    update_user_meta($site_user->ID, 'source_domain', $domain);
    update_user_meta($site_user->ID, 'primary_blog', $blog_id);
    if ($subdomain_install) {
        $hq_rewrite->set_permalink_structure('/%year%/%monthnum%/%day%/%postname%/');
    } else {
        $hq_rewrite->set_permalink_structure('/blog/%year%/%monthnum%/%day%/%postname%/');
    }
    flush_rewrite_rules();
    if (!$subdomain_install) {
        return true;
    }
    $vhost_ok = false;
    $errstr = '';
    $hostname = substr(md5(time()), 0, 6) . '.' . $domain;
    // Very random hostname!
    $page = hq_remote_get('http://' . $hostname, array('timeout' => 5, 'httpversion' => '1.1'));
    if (is_hq_error($page)) {
        $errstr = $page->get_error_message();
    } elseif (200 == hq_remote_retrieve_response_code($page)) {
        $vhost_ok = true;
    }
    if (!$vhost_ok) {
        $msg = '<p><strong>' . __('Warning! Wildcard DNS may not be configured correctly!') . '</strong></p>';
        $msg .= '<p>' . sprintf(__('The installer attempted to contact a random hostname (<code>%1$s</code>) on your domain.'), $hostname);
        if (!empty($errstr)) {
            $msg .= ' ' . sprintf(__('This resulted in an error message: %s'), '<code>' . $errstr . '</code>');
        }
        $msg .= '</p>';
        $msg .= '<p>' . __('To use a subdomain configuration, you must have a wildcard entry in your DNS. This usually means adding a <code>*</code> hostname record pointing at your web server in your DNS configuration tool.') . '</p>';
        $msg .= '<p>' . __('You can still use your site but any subdomain you create may not be accessible. If you know your DNS is correct, ignore this message.') . '</p>';
        return new HQ_Error('no_wildcard_dns', $msg);
    }
    //}
    return true;
}
Beispiel #2
0
/**
 * Perform a HTTP HEAD or GET request.
 *
 * If $file_path is a writable filename, this will do a GET request and write
 * the file to that path.
 *
 * @since 0.0.1
 *
 * @param string      $url       URL to fetch.
 * @param string|bool $file_path Optional. File path to write request to. Default false.
 * @param int         $red       Optional. The number of Redirects followed, Upon 5 being hit,
 *                               returns false. Default 1.
 * @return bool|string False on failure and string of headers if HEAD request.
 */
function hq_get_http($url, $file_path = false, $red = 1)
{
    @set_time_limit(60);
    if ($red > 5) {
        return false;
    }
    $options = array();
    $options['redirection'] = 5;
    if (false == $file_path) {
        $options['method'] = 'HEAD';
    } else {
        $options['method'] = 'GET';
    }
    $response = hq_safe_remote_request($url, $options);
    if (is_hq_error($response)) {
        return false;
    }
    $headers = hq_remote_retrieve_headers($response);
    $headers['response'] = hq_remote_retrieve_response_code($response);
    // HQ_HTTP no longer follows redirects for HEAD requests.
    if ('HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset($headers['location'])) {
        return hq_get_http($headers['location'], $file_path, ++$red);
    }
    if (false == $file_path) {
        return $headers;
    }
    // GET request - write it to the supplied filename
    $out_fp = fopen($file_path, 'w');
    if (!$out_fp) {
        return $headers;
    }
    fwrite($out_fp, hq_remote_retrieve_body($response));
    fclose($out_fp);
    clearstatcache();
    return $headers;
}
Beispiel #3
0
/**
 * Downloads a url to a local temporary file using the HiveQueen HTTP Class.
 * Please note, That the calling function must unlink() the file.
 *
 * @since 0.0.1
 *
 * @param string $url the URL of the file to download
 * @param int $timeout The timeout for the request to download the file default 300 seconds
 * @return mixed HQ_Error on failure, string Filename on success.
 */
function download_url($url, $timeout = 300)
{
    //WARNING: The file is not automatically deleted, The script must unlink() the file.
    if (!$url) {
        return new HQ_Error('http_no_url', __('Invalid URL Provided.'));
    }
    $tmpfname = hq_tempnam($url);
    if (!$tmpfname) {
        return new HQ_Error('http_no_file', __('Could not create Temporary file.'));
    }
    $response = hq_safe_remote_get($url, array('timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname));
    if (is_hq_error($response)) {
        unlink($tmpfname);
        return $response;
    }
    if (200 != hq_remote_retrieve_response_code($response)) {
        unlink($tmpfname);
        return new HQ_Error('http_404', trim(hq_remote_retrieve_response_message($response)));
    }
    $content_md5 = hq_remote_retrieve_header($response, 'content-md5');
    if ($content_md5) {
        $md5_check = verify_file_md5($tmpfname, $content_md5);
        if (is_hq_error($md5_check)) {
            unlink($tmpfname);
            return $md5_check;
        }
    }
    return $tmpfname;
}