2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
38 require_once("include/Sugarpdf/sugarpdf_config.php");
41 * Contain all the errors generated during the process of FontManager
44 var $errors = array();
46 * store the log string when addFont is call
51 * Current font filename
56 * Current font file path
61 * Multidimentional array which contain all the detail of all the available fonts
64 var $fontList = array();
66 * Name of the font of the current font file
71 * Encoding of the current font
76 * Display name of the current font
79 var $font_displayname = "";
81 * Type of the current font
86 private function setFontPath(){
87 if(file_exists(K_PATH_CUSTOM_FONTS.$this->filename)){
88 $this->fontPath = K_PATH_CUSTOM_FONTS;
89 }else if(file_exists(K_PATH_FONTS.$this->filename)){
90 $this->fontPath = K_PATH_FONTS;
93 array_push($this->errors, "Unable to find the font!");
97 * This method return a boolean which describe if the font define
98 * in filename is embedded or not.
99 * @return boolean true if embedded.
101 private function getEmbedded(){
102 if(empty($this->fontList[$this->getFilenameShort()]['type'])){
103 if(!$this->loadFontFile()){
104 array_push($this->errors, translate('ERR_LOADFONTFILE', 'Configurator'));
108 if($this->font_type == "cidfont0" || $this->font_type == "core"){
114 * This method return the style of the given font set in filename
115 * Values can be regular, bold, italic.
116 * @return array of styles on success
117 * @return empty array on failure
119 private function getStyle(){
120 if(empty($this->filename)){
121 array_push($this->errors, translate("ERR_FONT_EMPTYFILE","Configurator"));
124 if(preg_match("/bi.php$/i",$this->filename)){
125 return array("bold","italic");
126 }else if(preg_match("/ib.php$/i",$this->filename)){
127 return array("bold","italic");
128 }else if(preg_match("/b.php$/i",$this->filename)){
129 return array("bold");
130 }else if(preg_match("/i.php$/i",$this->filename)){
131 return array("italic");
133 return array("regular");
137 * This method calculate the font size of $this->filename in KB
138 * .php file + .z file + .ctg.z file
139 * @return Integer font Size in KB
141 private function getFontSize(){
142 $fileSize=filesize($this->fontPath.$this->filename);
143 $name = substr($this->filename, 0, strrpos($this->filename, '.'));
144 if(file_exists($this->fontPath.$name.".z")){
145 $fileSize+=filesize($this->fontPath.$name.".z");
147 if(file_exists($this->fontPath.$name.".ctg.z")){
148 $fileSize+=filesize($this->fontPath.$name.".ctg.z");
150 return round($fileSize/1024);
153 * Fill the fontList attribute with the data contains in the font file.
155 public function getDetail(){
156 if($this->loadFontFile()){
157 $this->fontList[$this->getFilenameShort()]['filename'] = $this->filename;
158 $this->fontList[$this->getFilenameShort()]['fontpath'] = $this->fontPath;
159 if(!empty($this->font_name)){
160 $this->fontList[$this->getFilenameShort()]['name'] = $this->font_name;
162 $this->fontList[$this->getFilenameShort()]['name'] = $this->getFilenameShort();
164 if(!empty($this->font_displayname)){
165 $this->fontList[$this->getFilenameShort()]['displayname'] = $this->font_displayname;
167 if(!empty($this->font_enc)){
168 $this->fontList[$this->getFilenameShort()]['enc'] = $this->font_enc;
170 if(!empty($this->font_type)){
171 if ($this->font_type == 'cidfont0' || $this->font_type == 'core' || $this->font_type == 'TrueType' || $this->font_type == 'Type1' || $this->font_type == 'TrueTypeUnicode') {
172 $this->fontList[$this->getFilenameShort()]['type'] = $this->font_type;
174 array_push($this->errors, translate("ERR_FONT_UNKNOW_TYPE","Configurator") . " " . $this->font_type);
177 $this->fontList[$this->getFilenameShort()]['style'] = $this->getStyle();
178 $this->fontList[$this->getFilenameShort()]['filesize'] = $this->getFontSize();
179 $this->fontList[$this->getFilenameShort()]['embedded'] = $this->getEmbedded();
185 * This method load the font file and check if it is good formatted.
186 * @return boolean true on success
188 private function loadFontFile(){
189 if(empty($this->filename))
191 $this->setFontPath();
192 if(!file_exists($this->fontPath.$this->filename)){
195 @include($this->fontPath.$this->filename);
196 if ((!isset($type)) OR (!isset($cw))) {
197 //The font definition file has a bad format
201 $this->font_name = "";
202 $this->font_enc = "";
203 $this->font_displayname = "";
204 $this->font_type = "";
207 $this->font_name = $name;
210 $this->font_enc = $enc;
212 if(!empty($displayname)){
213 $this->font_displayname = $displayname;
216 $this->font_type = $type;
221 * This method parse the font path defined in the sugarpdf config file
222 * and fill the fontList
223 * @return boolean true if font files have been found
225 private function parseFolder(){
226 if(!file_exists(K_PATH_FONTS) || !is_dir(K_PATH_FONTS)){
227 array_push($this->errors, translate("ERR_NO_FONT_PATH","Configurator"));
230 $result[0] = scandir(K_PATH_FONTS);
231 if(file_exists(K_PATH_CUSTOM_FONTS) && is_dir(K_PATH_CUSTOM_FONTS)){
232 $result[1] = scandir(K_PATH_CUSTOM_FONTS);
234 foreach($result as $v){
236 if(preg_match("/.php$/i",$vv)){
237 $this->filename = $vv;
242 ksort($this->fontList);
243 if(count($this->fontList)>0){
250 * This method fill the fontList with all the fonts available
252 public function listFontFiles(){
253 $this->fontList=array();
254 if(file_exists($cachedfile = sugar_cached("Sugarpdf/cachedFontList.php"))) {
256 $this->fontList=$cachedFontList;
259 if($this->parseFolder()){
260 $cacheDir = create_cache_directory('Sugarpdf/');
261 write_array_to_file('cachedFontList', $this->fontList, $cacheDir . 'cachedFontList.php');
268 * This method generate an array of font which can be use with get_select_options_with_id
271 public function getSelectFontList(){
272 $returnArray = array();
273 if($this->listFontFiles()){
274 foreach($this->fontList as $k=>$v){
275 if(!empty($v['displayname'])){
276 $returnArray[$k]=$v['displayname'];
278 $returnArray[$k]=$v['name'];
285 * This method return the filename without the ".php"
286 * @return String The short filename
288 private function getFilenameShort(){
289 return preg_replace("/.php$/i", "",$this->filename);
292 * This method delete all the files related to the font define in the filename attribute.
293 * @return boolean true on success
295 public function deleteFont(){
296 global $current_user;
297 if(!is_admin($current_user)){
298 sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
300 $this->loadFontFile();
301 if($this->font_type == "core" || $this->fontPath == K_PATH_FONTS){
302 array_push($this->errors, translate("ERR_DELETE_CORE_FILE","Configurator"));
305 if(file_exists($this->fontPath.$this->filename)){
306 if(is_writable($this->fontPath.$this->filename)){
307 unlink($this->fontPath.$this->filename);
308 if(file_exists($this->fontPath.$this->getFilenameShort().".ctg.z") && is_writable($this->fontPath.$this->getFilenameShort().".ctg.z")){
309 unlink($this->fontPath.$this->getFilenameShort()."ctg.z");
311 if(file_exists($this->fontPath.$this->getFilenameShort().".z") && is_writable($this->fontPath.$this->getFilenameShort().".z")){
312 unlink($this->fontPath.$this->getFilenameShort().".z");
314 $this->clearCachedFile();
317 array_push($this->errors, $this->fontPath.$this->filename . " " . translate("ERR_FONT_NOT_WRITABLE","Configurator"));
320 array_push($this->errors, $this->fontPath . " " . translate("ERR_FONT_FILE_DO_NOT_EXIST","Configurator"));
325 * This method add a font to SugarCRM from a font file and a metric file using MakeFont()
326 * @param $font_file string
327 * @param $metric_file string
328 * @param $embedded boolean
329 * @param $encoding_table string
330 * @param $patch array
331 * @param $cid_info string
332 * @param $style string
333 * @return boolean true on success
334 * @see MakeFont() in K_PATH_FONTS/utils
336 public function addFont($font_file, $metric_file, $embedded=true, $encoding_table='cp1252', $patch=array(), $cid_info="", $style="regular"){
337 global $current_user;
338 if(!is_admin($current_user)){
339 sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
343 $oldStr=ob_get_contents();
345 require_once("include/tcpdf/fonts/utils/makefont.php");
346 $filename = MakeFont($font_file,$metric_file, $embedded, $encoding_table, $patch, $cid_info);
349 unlink($metric_file);
351 $this->log=ob_get_contents();
356 if(empty($filename)){
357 array_push($this->errors, translate("ERR_FONT_MAKEFONT","Configurator"));
360 require_once("include/utils/file_utils.php");
361 $this->filename = basename($filename.".php");
362 if(!$this->loadFontFile()){
363 if(!mkdir_recursive(K_PATH_CUSTOM_FONTS)){
364 array_push($this->errors, "Error : Impossible to create the custom font directory.");
370 $styleLetter="i";break;
372 $styleLetter="b";break;
374 $styleLetter="bi";break;
378 sugar_rename($filename.".php", K_PATH_CUSTOM_FONTS.basename($filename.$styleLetter.".php"));
379 $this->log .= "\n" . translate("LBL_FONT_MOVE_DEFFILE","Configurator") . K_PATH_CUSTOM_FONTS.basename($filename.$styleLetter.".php");
380 if(file_exists($filename.".z")){
381 sugar_rename($filename.".z", K_PATH_CUSTOM_FONTS.basename($filename.$styleLetter.".z"));
382 $this->log .= "\n" . translate("LBL_FONT_MOVE_FILE","Configurator") . K_PATH_CUSTOM_FONTS.basename($filename.$styleLetter.".z");
384 if(file_exists($filename.".ctg.z")){
385 sugar_rename($filename.".ctg.z", K_PATH_CUSTOM_FONTS.basename($filename.$styleLetter.".ctg.z"));
386 $this->log .= "\n" . translate("LBL_FONT_MOVE_FILE","Configurator") . K_PATH_CUSTOM_FONTS.basename($filename.$styleLetter.".ctg.z");
390 array_push($this->errors, "\n".translate("ERR_FONT_ALREADY_EXIST","Configurator"));
394 if(file_exists($filename.".php"))
395 unlink($filename.".php");
396 if(file_exists($filename.".ctg.z"))
397 unlink($filename.".ctg.z");
398 if(file_exists($filename.".z"))
399 unlink($filename.".z");
403 $this->clearCachedFile();
407 * This method delete the cached file cachedFontList.php
410 public function clearCachedFile(){
411 global $current_user;
412 if(!is_admin($current_user)){
413 sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
415 if(file_exists($cachedfile = sugar_cached("Sugarpdf/cachedFontList.php"))) {
416 return unlink($cachedfile);
421 * Check if the given font filename exist in the font directories
424 public function fontFileExist($filename){
425 $this->filename = $filename;
426 return $this->loadFontFile();