Exemplo n.º 1
0
function gwfontform_submit(Pieform $form, $values)
{
    global $USER, $SESSION;
    require_once 'file.php';
    require_once 'uploadmanager.php';
    $fontpath = get_config('dataroot') . 'skins/fonts/';
    check_dir_exists($fontpath, true, true);
    $currentfont = null;
    $licence = null;
    $previewfont = null;
    $variants = array();
    $variants['regular'] = array("variant" => "regular", "font-weight" => "normal", "font-style" => "normal");
    $zip = new ZipArchive();
    if ($zip->open($values['gwfzipfile']['tmp_name'])) {
        $currentfont = dirname($zip->getNameIndex(0));
        for ($i = 0; $i < $zip->numFiles; $i++) {
            $extractfiles = array();
            $fontname = dirname($zip->getNameIndex($i));
            $filename = basename($zip->getNameIndex($i));
            $makefolder = false;
            if (empty($fontname) || $fontname == '.') {
                $fontname = substr($values['gwfzipfile']['name'], 0, -1 * strlen('.zip'));
                $makefolder = true;
            }
            // Find correct licence file...
            if (substr($zip->getNameIndex($i), -3) == 'txt') {
                $licence = $filename;
                $extractfiles[] = $zip->getNameIndex($i);
            }
            // Find correct TTF font file for generating skin previews...
            $possiblenames = array(str_replace("_", "", $fontname) . ".ttf", str_replace("_", "", $fontname) . "-Regular.ttf", str_replace("_", "", $fontname) . "-Normal.ttf");
            if (in_array($filename, $possiblenames)) {
                $previewfont = $filename;
                $extractfiles[] = $zip->getNameIndex($i);
            }
            // Reset settings for each new font...
            if (!is_null($licence) && !is_null($previewfont)) {
                $foldername = preg_replace(Skin::FONTNAME_FILTER_CHARACTERS, '', $fontname);
                // Assign a new name, if the font with the same name already exists...
                $foldername = Skin::new_font_name($foldername);
                if ($makefolder == true) {
                    $fontpath .= $foldername . '/';
                    check_dir_exists($fontpath, true, true);
                }
                $installfont = array("name" => $foldername, "title" => str_replace("_", " ", $fontname), "licence" => $licence, "notice" => "", "previewfont" => $previewfont, "variants" => serialize($variants), "fonttype" => "google", "onlyheading" => 0, "fontstack" => "'" . escape_css_string(str_replace("_", " ", $fontname)) . "'", "genericfont" => "sans-serif");
                // Install fonts (write data into database). Check if the record doesn't exist!!!
                ensure_record_exists('skin_fonts', (object) array('name' => $installfont['name']), (object) $installfont);
                // Extract installed fonts
                foreach ($extractfiles as $extractfile) {
                    $fullfontpath = $fontpath . $foldername . '/';
                    check_dir_exists($fullfontpath, true, true);
                    copy("zip://" . $values['gwfzipfile']['tmp_name'] . "#" . $extractfile, $fullfontpath . $previewfont);
                }
                $currentfont = $fontname;
                $licence = null;
                $previewfont = null;
            }
        }
        $SESSION->add_ok_msg(get_string('gwfontadded', 'skin'));
    } else {
        $SESSION->add_error_msg(get_string('archivereadingerror', 'skin'));
    }
    redirect('/admin/site/fonts.php');
}
Exemplo n.º 2
0
function importskinform_submit(Pieform $form, $values)
{
    global $USER, $SESSION;
    require_once get_config('docroot') . 'artefact/file/lib.php';
    // Open XML file and import Skin(s)...
    $filename = $values['file']['tmp_name'];
    $contents = file_get_contents($filename);
    $xmldoc = new DOMDocument('1.0', 'UTF-8');
    //$xmldoc->load($filename);
    $xmldoc->loadXML($contents);
    $skinsdata = $xmldoc->getElementsByTagName('skin');
    $siteskin = $values['skintype'] == 'site';
    // A non-admin can't create a site skin.
    if ($siteskin && !$USER->get('admin')) {
        $values['skintype'] = 'private';
        $siteskin = false;
    }
    foreach ($skinsdata as $skindata) {
        db_begin();
        // Join all view skin css/formating data to array...
        $skin = array();
        // Body element...
        $items = $skindata->getElementsByTagName('body');
        foreach ($items as $item) {
            $skin = array_merge($skin, array('body_background_color' => $item->getAttribute('background-color')));
            $skin = array_merge($skin, array('body_background_image' => 0));
            $skin = array_merge($skin, array('body_background_repeat' => Skin::background_repeat_value_to_number($item->getAttribute('background-repeat'))));
            $skin = array_merge($skin, array('body_background_attachment' => $item->getAttribute('background-attachment')));
            $skin = array_merge($skin, array('body_background_position' => Skin::background_position_value_to_number($item->getAttribute('background-position'))));
        }
        // Header element...
        $items = $skindata->getElementsByTagName('header');
        foreach ($items as $item) {
            $skin = array_merge($skin, array('header_background_color' => $item->getAttribute('background-color')));
            $skin = array_merge($skin, array('header_text_font_color' => $item->getAttribute('font-color')));
            $skin = array_merge($skin, array('header_link_normal_color' => $item->getAttribute('normal-color')));
            if ($item->getAttribute('normal-decoration') == 'none') {
                $skin = array_merge($skin, array('header_link_normal_underline' => 0));
            } else {
                $skin = array_merge($skin, array('header_link_normal_underline' => 1));
            }
            $skin = array_merge($skin, array('header_link_hover_color' => $item->getAttribute('hover-color')));
            if ($item->getAttribute('hover-decoration') == 'none') {
                $skin = array_merge($skin, array('header_link_hover_underline' => 0));
            } else {
                $skin = array_merge($skin, array('header_link_hover_underline' => 1));
            }
            $skin = array_merge($skin, array('header_logo_image' => $item->getAttribute('logo-image')));
        }
        // View element...
        $items = $skindata->getElementsByTagName('view');
        foreach ($items as $item) {
            $skin = array_merge($skin, array('view_background_color' => $item->getAttribute('background-color')));
            $skin = array_merge($skin, array('view_background_image' => 0));
            $skin = array_merge($skin, array('view_background_repeat' => Skin::background_repeat_value_to_number($item->getAttribute('background-repeat'))));
            $skin = array_merge($skin, array('view_background_attachment' => $item->getAttribute('background-attachment')));
            $skin = array_merge($skin, array('view_background_position' => Skin::background_position_value_to_number($item->getAttribute('background-position'))));
            $skin = array_merge($skin, array('view_background_width' => str_replace("%", "", $item->getAttribute('width'))));
            // odstrani znak %!
            $skin = array_merge($skin, array('view_background_margin' => $item->getAttribute('margin-top')));
        }
        // Text element...
        $items = $skindata->getElementsByTagName('text');
        foreach ($items as $item) {
            $skin = array_merge($skin, array('view_text_font_family' => $item->getAttribute('text-font')));
            $skin = array_merge($skin, array('view_heading_font_family' => $item->getAttribute('heading-font')));
            $skin = array_merge($skin, array('view_text_font_size' => $item->getAttribute('font-size')));
            $skin = array_merge($skin, array('view_text_font_color' => $item->getAttribute('font-color')));
            $skin = array_merge($skin, array('view_text_heading_color' => $item->getAttribute('heading-color')));
            $skin = array_merge($skin, array('view_text_emphasized_color' => $item->getAttribute('emphasized-color')));
        }
        // Link element...
        $items = $skindata->getElementsByTagName('link');
        foreach ($items as $item) {
            $skin = array_merge($skin, array('view_link_normal_color' => $item->getAttribute('normal-color')));
            if ($item->getAttribute('normal-decoration') == 'none') {
                $skin = array_merge($skin, array('view_link_normal_underline' => 0));
            } else {
                $skin = array_merge($skin, array('view_link_normal_underline' => 1));
            }
            $skin = array_merge($skin, array('view_link_hover_color' => $item->getAttribute('hover-color')));
            if ($item->getAttribute('hover-decoration') == 'none') {
                $skin = array_merge($skin, array('view_link_hover_underline' => 0));
            } else {
                $skin = array_merge($skin, array('view_link_hover_underline' => 1));
            }
        }
        // Table element...
        $items = $skindata->getElementsByTagName('table');
        foreach ($items as $item) {
            $skin = array_merge($skin, array('view_table_border_color' => $item->getAttribute('border-color')));
            $skin = array_merge($skin, array('view_table_odd_row_color' => $item->getAttribute('odd-row-color')));
            $skin = array_merge($skin, array('view_table_even_row_color' => $item->getAttribute('even-row-color')));
        }
        // Custom CSS element...
        $items = $skindata->getElementsByTagName('customcss');
        foreach ($items as $item) {
            $skin['view_custom_css'] = clean_css(unserialize($item->getAttribute('contents')), $preserve_css = true);
        }
        // Image element...
        // TODO: Background image file support for site skins
        if ($siteskin) {
            $skin['body_background_image'] = 0;
            $skin['view_background_image'] = 0;
        } else {
            $items = $skindata->getElementsByTagName('image');
            foreach ($items as $item) {
                // Write necessary data in 'artefact' table...
                // TODO: When we rework the file upload code to make it more general,
                // rewrite this to reuse content from filebrowser.php
                $now = date("Y-m-d H:i:s");
                $artefact = (object) array_merge((array) unserialize($item->getAttribute('artefact')), (array) unserialize($item->getAttribute('artefact_file_files')), (array) unserialize($item->getAttribute('artefact_file_image')));
                unset($artefact->id);
                unset($artefact->fileid);
                $artefact->owner = $USER->get('id');
                $artefact->author = $USER->get('id');
                $artefact->atime = $now;
                $artefact->ctime = $now;
                $artefact->mtime = $now;
                $artobj = new ArtefactTypeImage(0, $artefact);
                $artobj->commit();
                $id = $artobj->get('id');
                // Create folder and file inside it. then write contents into it...
                $imagedir = get_config('dataroot') . ArtefactTypeFile::get_file_directory($id);
                if (!check_dir_exists($imagedir, true, true)) {
                    throw new SystemException("Unable to create folder {$imagedir}");
                } else {
                    // Write contents to a file...
                    $imagepath = $imagedir . '/' . $id;
                    $contents = base64_decode($item->getAttribute('contents'));
                    $fp = fopen($imagepath, 'w');
                    fwrite($fp, $contents);
                    fclose($fp);
                    // We can keep going, but the skin will be missing one of its files
                    if ($clamerror = mahara_clam_scan_file($imagepath)) {
                        $SESSION->add_error_msg($clamerror);
                    }
                    chmod($imagepath, get_config('filepermissions'));
                }
                $type = $item->getAttribute('type');
                if ($type == 'body-background-image') {
                    $skin['body_background_image'] = $id;
                }
                if ($type == 'view-background-image') {
                    $skin['view_background_image'] = $id;
                }
            }
        }
        $viewskin = array();
        if ($skindata->getAttribute('title') != '') {
            $viewskin['title'] = $skindata->getAttribute('title');
        }
        $viewskin['description'] = $skindata->getAttribute('description');
        $viewskin['owner'] = $USER->get('id');
        $viewskin['type'] = $values['skintype'];
        $viewskin['viewskin'] = $skin;
        // Fonts element...
        // Only admins can install site fonts
        if ($USER->get('admin')) {
            $fonts = $skindata->getElementsByTagName('font');
            foreach ($fonts as $font) {
                $fontname = preg_replace("#[^A-Za-z0-9]#", "", $font->getAttribute('name'));
                $fontname = Skin::new_font_name($fontname);
                // Only upload font if it doesn't already exist on the site
                if (!Skin::font_exists($font->getAttribute('title'))) {
                    $fontdata = array('name' => $fontname, 'title' => $font->getAttribute('title'), 'licence' => $font->getAttribute('font-licence'), 'previewfont' => $font->getAttribute('font-preview'), 'variants' => base64_decode($font->getAttribute('font-variants')), 'fonttype' => $font->getAttribute('font-type'), 'onlyheading' => $font->getAttribute('heading-font-only'), 'fontstack' => $font->getAttribute('font-stack'), 'genericfont' => $font->getAttribute('generic-font'));
                    insert_record('skin_fonts', $fontdata);
                    $fontpath = get_config('dataroot') . 'skins/fonts/' . $fontdata['name'] . '/';
                    if (!check_dir_exists($fontpath, true, true)) {
                        throw new SystemException("Unable to create folder {$fontpath}");
                    } else {
                        $files = $font->getElementsByTagName('file');
                        foreach ($files as $file) {
                            // Read the filename and the contents of each file from XML...
                            $filename = $file->getAttribute('name');
                            $contents = base64_decode($file->getAttribute('contents'));
                            // Import and copy each file to the appropriate folder...
                            $fp = fopen($fontpath . $filename, 'wb');
                            fwrite($fp, $contents);
                            fclose($fp);
                            // We can keep going, but the skin will be missing one of its files
                            if ($clamerror = mahara_clam_scan_file($fontpath . $filename)) {
                                $SESSION->add_error_msg($clamerror);
                            }
                            chmod($fontpath . $filename, get_config('filepermissions'));
                        }
                    }
                }
            }
        }
        Skin::create($viewskin);
        db_commit();
    }
    $SESSION->add_ok_msg(get_string('skinimported', 'skin'));
    if ($values['skintype'] == 'site') {
        redirect('/admin/site/skins.php');
    } else {
        redirect('/skin/index.php');
    }
}
Exemplo n.º 3
0
function addfontform_submit(Pieform $form, $values)
{
    global $USER, $SESSION;
    $foldername = preg_replace(Skin::FONTNAME_FILTER_CHARACTERS, '', $values['fonttitle']);
    // Assign a new name, if the font with the same name already exists...
    $foldername = Skin::new_font_name($foldername);
    $fontpath = get_config('dataroot') . 'skins/fonts/' . $foldername . '/';
    check_dir_exists($fontpath, true, true);
    // If we are uploading a zip file
    if (!empty($values['fontfileZip'])) {
        safe_require('artefact', 'file');
        $zip = new ZipArchive();
        if ($zip->open($values['fontfileZip']['tmp_name'])) {
            for ($i = 0; $i < $zip->numFiles; $i++) {
                $fontname = dirname($zip->getNameIndex($i));
                $filename = basename($zip->getNameIndex($i));
                if (empty($fontname) || $fontname == '.') {
                    $fontname = substr($values['fontfileZip']['name'], 0, -1 * strlen('.zip'));
                }
                // Check that all the needed files exist in the zip file
                $check = uploadfiles_info();
                foreach ($check as $key => $item) {
                    if (end(explode('.', $zip->getNameIndex($i))) == $item['suffix']) {
                        // Extract font file
                        $zip->extractTo($fontpath, $zip->getNameIndex($i));
                        $values['fontfile' . strtoupper($item['suffix'])]['name'] = $zip->getNameIndex($i);
                    }
                }
            }
        }
    }
    // Get SVG id from SVG font file...
    $tempname = !empty($values['fontfileZip']) ? $fontpath . $values['fontfileSVG']['name'] : $values['fontfileSVG']['tmp_name'];
    $filename = $values['fontfileSVG']['name'];
    $xmlDoc = simplexml_load_string(file_get_contents($tempname));
    $svg_id = (string) $xmlDoc->defs->font->attributes()->id;
    // Insert new record with font data into 'skin_fonts' table in database...
    // $foldername equals (only alphanumerical) font name, e.g. 'Nimbus Roman No.9' -> 'NimbusRomanNo9'
    // $foldername is also used as primary key in 'skin_fonts' table.
    switch ($values['fontstyle']) {
        case 'regular':
            $font_variant = 'regular';
            $font_weight = 'normal';
            $font_style = 'normal';
            break;
        case 'bold':
            $font_variant = 'bold';
            $font_weight = 'bold';
            $font_style = 'normal';
            break;
        case 'italic':
            $font_variant = 'italic';
            $font_weight = 'normal';
            $font_style = 'italic';
            break;
        case 'bolditalic':
            $font_variant = 'bolditalic';
            $font_weight = 'bold';
            $font_style = 'italic';
            break;
    }
    $variantdata = array('variant' => $font_variant, 'EOT' => $values['fontfileEOT']['name'], 'SVG' => $values['fontfileSVG']['name'], 'SVGid' => $svg_id, 'TTF' => $values['fontfileTTF']['name'], 'WOFF' => $values['fontfileWOFF']['name'], 'font-weight' => $font_weight, 'font-style' => $font_style);
    // We'll create the database record before we copy the files over, so that
    // Mahara will know about this font in order to be able to delete its contents
    // from the filesystem if something goes wrong.
    ensure_record_exists('skin_fonts', (object) array('name' => $foldername), (object) array('name' => $foldername, 'title' => $values['fonttitle'], 'licence' => $values['fontfilelicence']['name'], 'notice' => $values['fontnotice'], 'previewfont' => $values['fontfileTTF']['name'], 'variants' => serialize(array($font_variant => $variantdata)), 'fonttype' => 'site', 'onlyheading' => $values['fonttype'] == 'heading' ? 1 : 0, 'fontstack' => '\'' . escape_css_string($values['fonttitle']) . '\'', 'genericfont' => $values['genericfont']));
    if (empty($values['fontfileZip'])) {
        // Copy SVG font file to folder...
        $tempname = $values['fontfileSVG']['tmp_name'];
        $filename = $values['fontfileSVG']['name'];
        move_uploaded_file($tempname, $fontpath . $filename);
        // Copy EOT font file.
        $tempname = $values['fontfileEOT']['tmp_name'];
        $filename = $values['fontfileEOT']['name'];
        move_uploaded_file($tempname, $fontpath . $filename);
        // Copy TTF font file to folder...
        $tempname = $values['fontfileTTF']['tmp_name'];
        $filename = $values['fontfileTTF']['name'];
        move_uploaded_file($tempname, $fontpath . $filename);
        // Copy WOFF font file to folder...
        $tempname = $values['fontfileWOFF']['tmp_name'];
        $filename = $values['fontfileWOFF']['name'];
        move_uploaded_file($tempname, $fontpath . $filename);
        // Copy optional font licence file to folder, if it exists...
        if (!empty($values['fontfilelicence'])) {
            $tempname = $values['fontfilelicence']['tmp_name'];
            $filename = $values['fontfilelicence']['name'];
            move_uploaded_file($tempname, $fontpath . $filename);
        }
    }
    $SESSION->add_ok_msg(get_string('fontinstalled', 'skin'));
    redirect('/admin/site/fonts.php');
}