function editfontform_submit(Pieform $form, $values) { global $USER, $SESSION; update_record('skin_fonts', array('title' => $values['fonttitle'], 'notice' => $values['fontnotice'], 'onlyheading' => $values['fonttype'] == 'heading' ? 1 : 0, 'fontstack' => '\'' . escape_css_string($values['fonttitle']) . '\'', 'genericfont' => $values['genericfont']), array('name' => $values['fontname'])); $SESSION->add_ok_msg(get_string('fontedited', 'skin')); redirect('/admin/site/fonts.php'); }
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'); }
/** * Gets the CSS for a font-face, based on the font face's name in the DB * @param string $font * @return string */ public static function get_css_font_face_from_font_name($font) { $fontdata = get_record('skin_fonts', 'name', $font); // If the font is missing, just quietly omit it. The page will still display, just with the wrong font if (!$fontdata) { return ''; } $fontface = ''; if ($fontdata->fonttype == 'site') { $fontfamily = urlencode($fontdata->title); // We need to create @font-face css rule for each variant of the font $variants = unserialize($fontdata->variants); foreach ($variants as $variant) { $baseurl = get_config('wwwroot') . 'skin/font.php?family=' . $fontfamily . '&variant=' . $variant['variant']; $fontface .= '@font-face {'; $fontface .= 'font-family: \'' . escape_css_string($fontdata->title) . '\'; '; $fontface .= 'src: url(\'' . $baseurl . '&format=EOT\'); '; $fontface .= 'src: local(\'☺\'), url(\'' . $baseurl . '&format=WOFF\') format(\'woff\'), '; $fontface .= 'url(\'' . $baseurl . '&format=TTF\') format(\'truetype\'), '; $fontface .= 'url(\'' . $baseurl . '&format=SVG#' . $variant['SVGid'] . '\') format(\'svg\'); '; $fontface .= 'font-weight: ' . $variant['font-weight'] . '; '; $fontface .= 'font-style: ' . $variant['font-style'] . '; '; $fontface .= '}'; } } return $fontface; }
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'); }