Theme: Integrate ability to use Google Fonts.

This commit is contained in:
reliberate
2016-05-05 12:57:08 -06:00
parent f9e5ac74a6
commit df22a29ed4
12 changed files with 605 additions and 5 deletions
+90
View File
@@ -0,0 +1,90 @@
<?php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
Portions created by the Initial Developer are Copyright (C) 2008-2012
the Initial Developer. All Rights Reserved.
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
*/
// make sure the PATH_SEPARATOR is defined
umask(2);
if (!defined("PATH_SEPARATOR")) {
if (strpos($_ENV["OS"], "Win") !== false) {
define("PATH_SEPARATOR", ";");
} else {
define("PATH_SEPARATOR", ":");
}
}
if (!isset($output_format)) $output_format = (PHP_SAPI == 'cli') ? 'text' : 'html';
// make sure the document_root is set
$_SERVER["SCRIPT_FILENAME"] = str_replace("\\", '/', $_SERVER["SCRIPT_FILENAME"]);
if(PHP_SAPI == 'cli'){
chdir(pathinfo(realpath($_SERVER["PHP_SELF"]), PATHINFO_DIRNAME));
$script_full_path = str_replace("\\", '/', getcwd() . '/' . $_SERVER["SCRIPT_FILENAME"]);
$dirs = explode('/', pathinfo($script_full_path, PATHINFO_DIRNAME));
if (file_exists('/project_root.php')) {
$path = '/';
} else {
$i = 1;
$path = '';
while ($i < count($dirs)) {
$path .= '/' . $dirs[$i];
if (file_exists($path. '/project_root.php')) {
break;
}
$i++;
}
}
$_SERVER["DOCUMENT_ROOT"] = $path;
}else{
$_SERVER["DOCUMENT_ROOT"] = str_replace($_SERVER["PHP_SELF"], "", $_SERVER["SCRIPT_FILENAME"]);
}
$_SERVER["DOCUMENT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"]);
// try to detect if a project path is being used
if (!defined('PROJECT_PATH')) {
if (is_dir($_SERVER["DOCUMENT_ROOT"]. '/fusionpbx')) {
define('PROJECT_PATH', '/fusionpbx');
} elseif (file_exists($_SERVER["DOCUMENT_ROOT"]. '/project_root.php')) {
define('PROJECT_PATH', '');
} else {
$dirs = explode('/', str_replace('\\', '/', pathinfo($_SERVER["PHP_SELF"], PATHINFO_DIRNAME)));
$i = 1;
$path = $_SERVER["DOCUMENT_ROOT"];
while ($i < count($dirs)) {
$path .= '/' . $dirs[$i];
if (file_exists($path. '/project_root.php')) {
break;
}
$i++;
}
if(!file_exists($path. '/project_root.php')){
die("Failed to locate the Project Root by searching for project_root.php please contact support for assistance");
}
$project_path = str_replace($_SERVER["DOCUMENT_ROOT"], "", $path);
define('PROJECT_PATH', $project_path);
}
$_SERVER["PROJECT_ROOT"] = realpath($_SERVER["DOCUMENT_ROOT"] . PROJECT_PATH);
set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER["PROJECT_ROOT"]);
}
?>
+106
View File
@@ -0,0 +1,106 @@
<?php
include "root.php";
require_once "resources/require.php";
$font_loader_version = ($_GET['v'] != '') ? $_GET['v'] : 1;
header("Content-type: text/javascript; charset: UTF-8");
//web font loader
if ($_SESSION['theme']['font_loader']['text'] == 'true') {
//parse font names
if (is_array($_SESSION['theme']) && sizeof($_SESSION['theme']) > 0) {
foreach ($_SESSION['theme'] as $subcategory => $type) {
if (substr_count($subcategory, '_font') > 0) {
$font_string = $type['text'];
if ($font_string != '') {
if (substr_count($font_string, ',') > 0) {
$tmp_array = explode(',', $font_string);
}
else {
$tmp_array[] = $font_string;
}
foreach ($tmp_array as $font_name) {
$font_name = trim($font_name, "'");
$font_name = trim($font_name, '"');
$font_name = trim($font_name);
$fonts[] = $font_name;
}
}
}
unset($fonts, $tmp_array);
}
}
//optimize fonts array
$fonts = array_unique($fonts);
$common_fonts = 'arial,arial black,arial narrow,calibri,'.
'candara,apple gothic,geneva,tahoma,microsoft sans serif,'.
'lucidia,lucidia console,monaco,lucidia sans unicode,'.
'lucidiagrande,consolas,menlo,trebuchet,trebuchet ms,'.
'helvetica,times,times new roman,courier,courier new,'.
'impact,comic sans,comic sans ms,georgia,palatino,'.
'palatino linotype,verdana,franklin gothic,'.
'franklin gothic medium,gill sans,gill sans mt,'.
'brush script,corbel';
$common_fonts = array(explode(',', $common_fonts));
foreach ($fonts as $index => $font) {
if (in_array(strtolower($font), $common_fonts)) {
unset($fonts[$index]);
}
}
//load fonts
if (is_array($fonts) && sizeof($fonts) > 0) {
if ($_SESSION['theme']['font_retrieval']['text'] == 'asynchronous') {
?>
WebFontConfig = {
google: {
families: ['<?php echo implode("','", $fonts); ?>']
}
};
(function(d) {
var wf = d.createElement('script'), s = d.scripts[0];
wf.src = '//ajax.googleapis.com/ajax/libs/webfont/<?php echo $font_loader_version; ?>/webfont.js';
s.parentNode.insertBefore(wf, s);
})(document);
<?php
}
else { //synchronous
?>
WebFont.load({
google: {
families: ['<?php echo implode("','", $fonts); ?>']
}
});
<?php
}
}
}
?>
+132
View File
@@ -1776,4 +1776,136 @@ function number_pad($number,$n) {
//$string = "2089068227)$"; echo $string." ".string_to_regex($string)."\n";
}
//dynamically load available web fonts
if (!function_exists('get_available_fonts')) {
function get_available_fonts($sort = 'alpha') {
if ($_SESSION['theme']['font_source_key']['text'] != '') {
if (!is_array($_SESSION['fonts_available']) || sizeof($_SESSION['fonts_available']) == 0) {
/*
sort options:
alpha - alphabetically
date - by date added (most recent font added or updated first)
popularity - by popularity (most popular family first)
style - by number of styles available (family with most styles first)
trending - by families seeing growth in usage (family seeing the most growth first)
*/
$google_api_url = 'https://www.googleapis.com/webfonts/v1/webfonts?key='.$_SESSION['theme']['font_source_key']['text'].'&sort='.$sort;
$response = file_get_contents($google_api_url);
if ($response != '') {
$data = json_decode($response, true);
$items = $data['items'];
foreach ($items as $item) {
$fonts[] = $item['family'];
}
//echo "<pre>".print_r($font_list, true)."</pre>";
}
$_SESSION['fonts_available'] = $fonts;
unset($fonts);
}
return (is_array($_SESSION['fonts_available']) && sizeof($_SESSION['fonts_available']) > 0) ? $_SESSION['fonts_available'] : array();
}
else {
return false;
}
}
}
//dynamically import web fonts (by reading static css file)
if (!function_exists('import_fonts')) {
function import_fonts($file_to_parse, $line_styles_begin = null) {
/*
This function reads the contents of $file_to_parse, beginning at $line_styles_begin (if set),
and attempts to parse the specified google fonts used. The assumption is that each curly brace
will be on its own line, each CSS style (attribute: value;) will be on its own line, a single
Google Fonts name will be used per selector, and that it will be surrounded by SINGLE quotes,
as shown in the example below:
.class_name {
font-family: 'Google Font';
font-weight: 300;
font-style: italic;
}
If the CSS styles are formatted as described, the necessary @import string should be generated
correctly.
*/
$file = file_get_contents($_SERVER["DOCUMENT_ROOT"].$file_to_parse);
$lines = explode("\n", $file);
$style_counter = 0;
foreach ($lines as $line_number => $line) {
if ($line_styles_begin != '' && $line_number < $line_styles_begin - 1) { continue; }
if (substr_count($line, "{") > 0) {
$style_lines[$style_counter]['begins'] = $line_number;
}
if (substr_count($line, "}") > 0) {
$style_lines[$style_counter]['ends'] = $line_number;
$style_counter++;
}
}
//echo "\n\n".print_r($style_lines, true)."\n\n";
if (is_array($style_lines) && sizeof($style_lines) > 0) {
foreach ($style_lines as $index => $style_line) {
for ($l = $style_line['begins']+1; $l < $style_line['ends']; $l++) {
$tmp[] = $lines[$l];
}
$style_groups[] = $tmp;
unset($tmp);
}
//echo "\n\n".print_r($style_groups, true)."\n\n";
if (is_array($style_groups) && sizeof($style_groups) > 0) {
foreach ($style_groups as $style_group_index => $style_group) {
foreach ($style_group as $style_index => $style) {
$tmp = explode(':', $style);
$attribute = trim($tmp[0]);
$value = trim(trim($tmp[1]),';');
$style_array[$attribute] = $value;
}
$style_groups[$style_group_index] = $style_array;
unset($style_array);
}
//echo "\n\n".print_r($style_groups, true)."\n\n";
foreach ($style_groups as $style_group_index => $style_group) {
$style_value = $style_group['font-family'];
if (substr_count($style_value, "'") > 0) {
//determine font
$font_begin = strpos($style_value, "'")+1;
$font_end = strpos($style_value, "'", $font_begin);
$font_name = substr($style_value, $font_begin, $font_end - $font_begin);
//determine modifiers
$weight = (is_numeric($style_group['font-weight']) || strtolower($style_group['font-weight']) == 'bold') ? strtolower($style_group['font-weight']) : null;
$italic = (strtolower($style_group['font-style']) == 'italic') ? 'italic' : null;
//add font to array
$fonts[$font_name][] = $weight.$italic;
}
}
//echo "\n\n/*".print_r($fonts, true)."*/\n\n";
if (is_array($fonts)) {
foreach ($fonts as $font_name => $modifiers) {
$modifiers = array_unique($modifiers);
$import_font_string = str_replace(' ', '+', $font_name);
if (is_array($modifiers) && sizeof($modifiers) > 0) {
$import_font_string .= ':'.implode(',', $modifiers);
}
$import_fonts[] = $import_font_string;
}
//echo "\n\n/*".print_r($import_fonts, true)."*/\n\n";
$import_string = "@import url(//fonts.googleapis.com/css?family=".implode('|', $import_fonts).");";
echo $import_string."\n";
}
}
}
}
}
?>