Example #1
0
function ttw_uploadit()
{
    // upload theme from users computer
    // they've supplied and uploaded a file
    $ok = true;
    // no errors so far
    if (isset($_FILES['uploaded']['name'])) {
        $filename = $_FILES['uploaded']['name'];
    } else {
        $filename = "";
    }
    if (isset($_FILES['uploaded']['tmp_name'])) {
        $openname = $_FILES['uploaded']['tmp_name'];
    } else {
        $openname = "";
    }
    //Check the file extension
    $check_file = strtolower($filename);
    $ext_check = end(explode('.', $check_file));
    if ($filename == "") {
        $errors[] = "You didn't select a file to upload<br />";
        $ok = false;
    }
    if ($ok && $ext_check != 'wvr') {
        $errors[] = "Theme files must have <em>.wvr</em> extension.<br />";
        $ok = false;
    }
    if ($ok) {
        $handle = fopen($openname, 'r');
        // now try to open the uploaded file
        if (!$handle) {
            $errors[] = '<strong><em style="color:red;">' . __('Sorry, there was a problem uploading your file. You may need to check your folder permissions or other server settings.', TTW_TRANS) . '</em></strong>' . "<br />(Trying to use file '{$openname}')";
            $ok = false;
        }
    }
    if (!$ok) {
        echo '<div id="message" class="updated fade"><p><strong><em style="color:red;">ERROR</em></strong></p><p>';
        foreach ($errors as $error) {
            echo $error . '<br />';
        }
        echo '</p></div>';
    } else {
        // OK - read file and save to My Saved Theme
        // $handle has file handle to temp file.
        $contents = null;
        while (!feof($handle)) {
            $contents .= fread($handle, 1024);
        }
        fclose($handle);
        if (!ttw_save_serialized_theme($contents)) {
            echo '<div id="message" class="updated fade"><p><strong><em style="color:red;">' . __('Sorry, there was a problem uploading your file. The file you picked was not a valid Weaver theme file.', TTW_TRANS) . '</em></strong></p></div>';
        } else {
            $t = ttw_getopt('ttw_subtheme');
            if ($t == '') {
                $t = TTW_DEFAULT_THEME;
            }
            /* did we save a theme? */
            echo '<div id="message" class="updated fade"><p><strong>' . __("Twenty Ten Weaver theme options reset to uploaded theme, saved as: ", TTW_TRANS) . $t . '.</strong></p></div>';
        }
    }
}
function ttw_upload_theme($filename)
{
    $handle = fopen($filename, 'r');
    if (!$handle) {
        return ttw_file_fail("Can't open {$filename}");
    }
    /* can't open */
    $contents = null;
    while (!feof($handle)) {
        $contents .= fread($handle, 1024);
    }
    fclose($handle);
    return ttw_save_serialized_theme($contents);
}