]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/Sugarpdf/FontManager.php
Release 6.4.0
[Github/sugarcrm.git] / include / Sugarpdf / FontManager.php
1 <?php
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-2011 SugarCRM Inc.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38 require_once("include/Sugarpdf/sugarpdf_config.php");
39 class FontManager{
40     /**
41      * Contain all the errors generated during the process of FontManager
42      * @var String[]
43      */
44     var $errors = array();
45     /**
46      * store the log string when addFont is call
47      * @var String
48      */
49     var $log = "";
50     /**
51      * Current font filename
52      * @var String
53      */
54     var $filename = "";
55      /**
56      * Current font file path
57      * @var String
58      */
59     var $fontPath = "";
60     /**
61      * Multidimentional array which contain all the detail of all the available fonts
62      * @var Array
63      */
64     var $fontList = array();
65     /**
66      * Name of the font of the current font file
67      * @var String
68      */
69     var $font_name = "";
70     /**
71      * Encoding of the current font
72      * @var String
73      */
74     var $font_enc = "";
75     /**
76      * Display name of the current font
77      * @var String
78      */
79     var $font_displayname = "";
80     /**
81      * Type of the current font
82      * @var String
83      */
84     var $font_type = "";
85
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;
91         }else{
92             $this->fontPath = "";
93             array_push($this->errors,  "Unable to find the font!");
94         }
95     }
96     /**
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.
100      */
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'));
105                 return false;
106             }
107         }
108         if($this->font_type == "cidfont0" || $this->font_type == "core"){
109             return false;
110         }
111         return true;
112     }
113     /**
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
118      */
119     private function getStyle(){
120         if(empty($this->filename)){
121             array_push($this->errors,  translate("ERR_FONT_EMPTYFILE","Configurator"));
122             return array();
123         }
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");
132         }else{
133             return array("regular");
134         }
135     }
136     /**
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
140      */
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");
146         }
147         if(file_exists($this->fontPath.$name.".ctg.z")){
148             $fileSize+=filesize($this->fontPath.$name.".ctg.z");
149         }
150         return round($fileSize/1024);
151     }
152     /**
153      * Fill the fontList attribute with the data contains in the font file.
154      */
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;
161             }else{
162                 $this->fontList[$this->getFilenameShort()]['name'] = $this->getFilenameShort();
163             }
164             if(!empty($this->font_displayname)){
165                 $this->fontList[$this->getFilenameShort()]['displayname'] = $this->font_displayname;
166             }
167             if(!empty($this->font_enc)){
168                 $this->fontList[$this->getFilenameShort()]['enc'] = $this->font_enc;
169             }
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;
173                 }else{
174                     array_push($this->errors,  translate("ERR_FONT_UNKNOW_TYPE","Configurator") . " " . $this->font_type);
175                 }
176             }
177             $this->fontList[$this->getFilenameShort()]['style'] = $this->getStyle();
178             $this->fontList[$this->getFilenameShort()]['filesize'] = $this->getFontSize();
179             $this->fontList[$this->getFilenameShort()]['embedded'] = $this->getEmbedded();
180             return true;
181         }
182         return false;
183     }
184     /**
185      * This method load the font file and check if it is good formatted.
186      * @return boolean true on success
187      */
188     private function loadFontFile(){
189         if(empty($this->filename))
190             return false;
191         $this->setFontPath();
192         if(!file_exists($this->fontPath.$this->filename)){
193             return false;
194         }
195         @include($this->fontPath.$this->filename);
196         if ((!isset($type)) OR (!isset($cw))) {
197             //The font definition file has a bad format
198             return false;
199         }
200
201         $this->font_name = "";
202         $this->font_enc = "";
203         $this->font_displayname = "";
204         $this->font_type = "";
205
206         if(!empty($name)){
207             $this->font_name = $name;
208         }
209         if(!empty($enc)){
210             $this->font_enc = $enc;
211         }
212         if(!empty($displayname)){
213             $this->font_displayname = $displayname;
214         }
215         if(!empty($type)){
216             $this->font_type = $type;
217         }
218         return true;
219     }
220     /**
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
224      */
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"));
228             return false;
229         }
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);
233         }
234         foreach($result as $v){
235             foreach($v as $vv){
236                 if(preg_match("/.php$/i",$vv)){
237                     $this->filename = $vv;
238                     $this->getDetail();
239                 }
240             }
241         }
242         ksort($this->fontList);
243         if(count($this->fontList)>0){
244             return true;
245         }else{
246             return false;
247         }
248     }
249     /**
250      * This method fill the fontList with all the fonts available
251      */
252     public function listFontFiles(){
253         $this->fontList=array();
254         if(file_exists($cachedfile = sugar_cached("Sugarpdf/cachedFontList.php"))) {
255             require $cachedfile;
256             $this->fontList=$cachedFontList;
257             return true;
258         }else{
259             if($this->parseFolder()){
260                 $cacheDir = create_cache_directory('Sugarpdf/');
261                 write_array_to_file('cachedFontList', $this->fontList, $cacheDir . 'cachedFontList.php');
262                 return true;
263             }
264         }
265         return false;
266     }
267     /**
268      * This method generate an array of font which can be use with get_select_options_with_id
269      * @return Array
270      */
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'];
277                 }else{
278                     $returnArray[$k]=$v['name'];
279                 }
280             }
281         }
282         return $returnArray;
283     }
284     /**
285      * This method return the filename without the ".php"
286      * @return String The short filename
287      */
288     private function getFilenameShort(){
289         return preg_replace("/.php$/i", "",$this->filename);
290     }
291     /**
292      * This method delete all the files related to the font define in the filename attribute.
293      * @return boolean true on success
294      */
295     public function deleteFont(){
296         global $current_user;
297         if(!is_admin($current_user)){
298             sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
299         }
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"));
303              return false;
304         }
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");
310                 }
311                 if(file_exists($this->fontPath.$this->getFilenameShort().".z") && is_writable($this->fontPath.$this->getFilenameShort().".z")){
312                     unlink($this->fontPath.$this->getFilenameShort().".z");
313                 }
314                 $this->clearCachedFile();
315                 return true;
316             }else{
317                 array_push($this->errors, $this->fontPath.$this->filename . " " . translate("ERR_FONT_NOT_WRITABLE","Configurator"));
318             }
319         }else{
320             array_push($this->errors, $this->fontPath . " " . translate("ERR_FONT_FILE_DO_NOT_EXIST","Configurator"));
321         }
322         return false;
323     }
324     /**
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
335      */
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']);
340         }
341         $error=false;
342
343         $oldStr=ob_get_contents();
344         ob_clean();
345         require_once("include/tcpdf/fonts/utils/makefont.php");
346         $filename = MakeFont($font_file,$metric_file, $embedded, $encoding_table, $patch, $cid_info);
347
348         unlink($font_file);
349         unlink($metric_file);
350
351         $this->log=ob_get_contents();
352         ob_clean();
353
354         echo $oldStr;
355
356         if(empty($filename)){
357             array_push($this->errors, translate("ERR_FONT_MAKEFONT","Configurator"));
358             $error=true;
359         }else{
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.");
365                     $error=true;
366                 }else{
367                     $styleLetter="";
368                     switch($style){
369                         case "italic":
370                             $styleLetter="i";break;
371                         case "bold":
372                             $styleLetter="b";break;
373                         case "boldItalic":
374                             $styleLetter="bi";break;
375                         default:
376                             $styleLetter="";
377                     }
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");
383                     }
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");
387                     }
388                 }
389             }else{
390                 array_push($this->errors, "\n".translate("ERR_FONT_ALREADY_EXIST","Configurator"));
391                 $error=true;
392             }
393             if($error){
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");
400             }
401
402         }
403         $this->clearCachedFile();
404         return $error;
405     }
406     /**
407      * This method  delete the cached file cachedFontList.php
408      * @return boolean
409      */
410     public function clearCachedFile(){
411         global $current_user;
412         if(!is_admin($current_user)){
413             sugar_die($GLOBALS['app_strings']['ERR_NOT_ADMIN']);
414         }
415          if(file_exists($cachedfile = sugar_cached("Sugarpdf/cachedFontList.php"))) {
416             return unlink($cachedfile);
417         }
418         return true;
419     }
420     /**
421      * Check if the given font filename exist in the font directories
422      * @return boolean
423      */
424     public function fontFileExist($filename){
425         $this->filename = $filename;
426         return $this->loadFontFile();
427     }
428 }
429
430 ?>