/**
 * Actually move a file to the uploads directory
 *
 * @param array $file The PHP $_FILE array for the file
 * @param string $path The path to save the file in
 * @param string $filename The filename for the file (if blank, current is used)
 * @return array The uploaded file
 */
function upload_file($file, $path, $filename = "")
{
    global $plugins, $mybb;
    $upload = array();
    if (empty($file['name']) || $file['name'] == "none" || $file['size'] < 1) {
        $upload['error'] = 1;
        return $upload;
    }
    if (!$filename) {
        $filename = $file['name'];
    }
    $upload['original_filename'] = preg_replace("#/\$#", "", $file['name']);
    // Make the filename safe
    $filename = preg_replace("#/\$#", "", $filename);
    // Make the filename safe
    $moved = @move_uploaded_file($file['tmp_name'], $path . "/" . $filename);
    $cdn_path = '';
    $moved_cdn = copy_file_to_cdn($path . "/" . $filename, $cdn_path);
    if (!$moved) {
        $upload['error'] = 2;
        return $upload;
    }
    @my_chmod($path . "/" . $filename, '0644');
    $upload['filename'] = $filename;
    $upload['path'] = $path;
    $upload['type'] = $file['type'];
    $upload['size'] = $file['size'];
    $upload = $plugins->run_hooks("upload_file_end", $upload);
    if ($moved_cdn) {
        $upload['cdn_path'] = $cdn_path;
    }
    return $upload;
}
/**
 * Caches a stylesheet to the file system.
 *
 * @param string $tid The theme ID this stylesheet belongs to.
 * @param string $filename The name of the stylesheet.
 * @param string $stylesheet The contents of the stylesheet.
 *
 * @return string The cache file path.
 */
function cache_stylesheet($tid, $filename, $stylesheet)
{
    global $mybb;
    $filename = str_replace('/', '', $filename);
    $tid = (int) $tid;
    $theme_directory = "cache/themes/theme{$tid}";
    // If we're in safe mode save to the main theme folder by default
    if ($mybb->safemode) {
        $theme_directory = "cache/themes";
        $filename = $tid . "_" . $filename;
    } elseif (!is_dir(MYBB_ROOT . $theme_directory)) {
        if (!@mkdir(MYBB_ROOT . $theme_directory)) {
            $theme_directory = "cache/themes";
            $filename = $tid . "_" . $filename;
        } else {
            // Add in empty index.html!
            $fp = @fopen(MYBB_ROOT . $theme_directory . "/index.html", "w");
            @fwrite($fp, "");
            @fclose($fp);
        }
    }
    $theme_vars = array("theme" => $theme_directory);
    $stylesheet = parse_theme_variables($stylesheet, $theme_vars);
    $stylesheet = preg_replace_callback("#url\\((\"|'|)(.*)\\1\\)#", create_function('$matches', 'return fix_css_urls($matches[2]);'), $stylesheet);
    $fp = @fopen(MYBB_ROOT . "{$theme_directory}/{$filename}", "wb");
    if (!$fp) {
        return false;
    }
    @fwrite($fp, $stylesheet);
    @fclose($fp);
    $stylesheet_min = minify_stylesheet($stylesheet);
    $filename_min = str_replace('.css', '.min.css', $filename);
    $fp_min = @fopen(MYBB_ROOT . "{$theme_directory}/{$filename_min}", "wb");
    if (!$fp_min) {
        return false;
    }
    @fwrite($fp_min, $stylesheet_min);
    @fclose($fp_min);
    copy_file_to_cdn(MYBB_ROOT . "{$theme_directory}/{$filename}");
    copy_file_to_cdn(MYBB_ROOT . "{$theme_directory}/{$filename_min}");
    return "{$theme_directory}/{$filename}";
}