-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtcpdf_static.php
2650 lines (2571 loc) · 107 KB
/
tcpdf_static.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
//============================================================+
// File name : tcpdf_static.php
// Version : 1.1.4
// Begin : 2002-08-03
// Last Update : 2019-11-01
// Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - [email protected]
// License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
// -------------------------------------------------------------------
// Copyright (C) 2002-2015 Nicola Asuni - Tecnick.com LTD
//
// This file is part of TCPDF software library.
//
// TCPDF is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// TCPDF is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the License
// along with TCPDF. If not, see
// <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
//
// See LICENSE.TXT file for more information.
// -------------------------------------------------------------------
//
// Description :
// Static methods used by the TCPDF class.
//
//============================================================+
/**
* @file
* This is a PHP class that contains static methods for the TCPDF class.<br>
* @package com.tecnick.tcpdf
* @author Nicola Asuni
* @version 1.1.2
*/
/**
* @class TCPDF_STATIC
* Static methods used by the TCPDF class.
* @package com.tecnick.tcpdf
* @brief PHP class for generating PDF documents without requiring external extensions.
* @version 1.1.1
* @author Nicola Asuni - [email protected]
*/
class TCPDF_STATIC {
/**
* Current TCPDF version.
* @private static
*/
private static $tcpdf_version = '6.3.5';
/**
* String alias for total number of pages.
* @public static
*/
public static $alias_tot_pages = '{:ptp:}';
/**
* String alias for page number.
* @public static
*/
public static $alias_num_page = '{:pnp:}';
/**
* String alias for total number of pages in a single group.
* @public static
*/
public static $alias_group_tot_pages = '{:ptg:}';
/**
* String alias for group page number.
* @public static
*/
public static $alias_group_num_page = '{:png:}';
/**
* String alias for right shift compensation used to correctly align page numbers on the right.
* @public static
*/
public static $alias_right_shift = '{rsc:';
/**
* Encryption padding string.
* @public static
*/
public static $enc_padding = "\x28\xBF\x4E\x5E\x4E\x75\x8A\x41\x64\x00\x4E\x56\xFF\xFA\x01\x08\x2E\x2E\x00\xB6\xD0\x68\x3E\x80\x2F\x0C\xA9\xFE\x64\x53\x69\x7A";
/**
* ByteRange placemark used during digital signature process.
* @since 4.6.028 (2009-08-25)
* @public static
*/
public static $byterange_string = '/ByteRange[0 ********** ********** **********]';
/**
* Array page boxes names
* @public static
*/
public static $pageboxes = array('MediaBox', 'CropBox', 'BleedBox', 'TrimBox', 'ArtBox');
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* Return the current TCPDF version.
* @return TCPDF version string
* @since 5.9.012 (2010-11-10)
* @public static
*/
public static function getTCPDFVersion() {
return self::$tcpdf_version;
}
/**
* Return the current TCPDF producer.
* @return TCPDF producer string
* @since 6.0.000 (2013-03-16)
* @public static
*/
public static function getTCPDFProducer() {
return "\x54\x43\x50\x44\x46\x20".self::getTCPDFVersion()."\x20\x28\x68\x74\x74\x70\x3a\x2f\x2f\x77\x77\x77\x2e\x74\x63\x70\x64\x66\x2e\x6f\x72\x67\x29";
}
/**
* Sets the current active configuration setting of magic_quotes_runtime (if the set_magic_quotes_runtime function exist)
* @param $mqr (boolean) FALSE for off, TRUE for on.
* @since 4.6.025 (2009-08-17)
* @public static
*/
public static function set_mqr($mqr) {
if (!defined('PHP_VERSION_ID')) {
$version = PHP_VERSION;
define('PHP_VERSION_ID', (($version[0] * 10000) + ($version[2] * 100) + $version[4]));
}
if (PHP_VERSION_ID < 50300) {
@set_magic_quotes_runtime($mqr);
}
}
/**
* Gets the current active configuration setting of magic_quotes_runtime (if the get_magic_quotes_runtime function exist)
* @return Returns 0 if magic quotes runtime is off or get_magic_quotes_runtime doesn't exist, 1 otherwise.
* @since 4.6.025 (2009-08-17)
* @public static
*/
public static function get_mqr() {
if (!defined('PHP_VERSION_ID')) {
$version = PHP_VERSION;
define('PHP_VERSION_ID', (($version[0] * 10000) + ($version[2] * 100) + $version[4]));
}
if (PHP_VERSION_ID < 50300) {
return @get_magic_quotes_runtime();
}
return 0;
}
/**
* Check if the URL exist.
* @param $url (string) URL to check.
* @return Boolean true if the URl exist, false otherwise.
* @since 5.9.204 (2013-01-28)
* @public static
*/
public static function isValidURL($url) {
$headers = @get_headers($url);
return (strpos($headers[0], '200') !== false);
}
/**
* Removes SHY characters from text.
* Unicode Data:<ul>
* <li>Name : SOFT HYPHEN, commonly abbreviated as SHY</li>
* <li>HTML Entity (decimal): "&#173;"</li>
* <li>HTML Entity (hex): "&#xad;"</li>
* <li>HTML Entity (named): "&shy;"</li>
* <li>How to type in Microsoft Windows: [Alt +00AD] or [Alt 0173]</li>
* <li>UTF-8 (hex): 0xC2 0xAD (c2ad)</li>
* <li>UTF-8 character: chr(194).chr(173)</li>
* </ul>
* @param $txt (string) input string
* @param $unicode (boolean) True if we are in unicode mode, false otherwise.
* @return string without SHY characters.
* @since (4.5.019) 2009-02-28
* @public static
*/
public static function removeSHY($txt='', $unicode=true) {
$txt = preg_replace('/([\\xc2]{1}[\\xad]{1})/', '', $txt);
if (!$unicode) {
$txt = preg_replace('/([\\xad]{1})/', '', $txt);
}
return $txt;
}
/**
* Get the border mode accounting for multicell position (opens bottom side of multicell crossing pages)
* @param $brd (mixed) Indicates if borders must be drawn around the cell block. The value can be a number:<ul><li>0: no border (default)</li><li>1: frame</li></ul>or a string containing some or all of the following characters (in any order):<ul><li>L: left</li><li>T: top</li><li>R: right</li><li>B: bottom</li></ul> or an array of line styles for each border group: array('LTRB' => array('width' => 2, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0)))
* @param $position (string) multicell position: 'start', 'middle', 'end'
* @param $opencell (boolean) True when the cell is left open at the page bottom, false otherwise.
* @return border mode array
* @since 4.4.002 (2008-12-09)
* @public static
*/
public static function getBorderMode($brd, $position='start', $opencell=true) {
if ((!$opencell) OR empty($brd)) {
return $brd;
}
if ($brd == 1) {
$brd = 'LTRB';
}
if (is_string($brd)) {
// convert string to array
$slen = strlen($brd);
$newbrd = array();
for ($i = 0; $i < $slen; ++$i) {
$newbrd[$brd[$i]] = array('cap' => 'square', 'join' => 'miter');
}
$brd = $newbrd;
}
foreach ($brd as $border => $style) {
switch ($position) {
case 'start': {
if (strpos($border, 'B') !== false) {
// remove bottom line
$newkey = str_replace('B', '', $border);
if (strlen($newkey) > 0) {
$brd[$newkey] = $style;
}
unset($brd[$border]);
}
break;
}
case 'middle': {
if (strpos($border, 'B') !== false) {
// remove bottom line
$newkey = str_replace('B', '', $border);
if (strlen($newkey) > 0) {
$brd[$newkey] = $style;
}
unset($brd[$border]);
$border = $newkey;
}
if (strpos($border, 'T') !== false) {
// remove bottom line
$newkey = str_replace('T', '', $border);
if (strlen($newkey) > 0) {
$brd[$newkey] = $style;
}
unset($brd[$border]);
}
break;
}
case 'end': {
if (strpos($border, 'T') !== false) {
// remove bottom line
$newkey = str_replace('T', '', $border);
if (strlen($newkey) > 0) {
$brd[$newkey] = $style;
}
unset($brd[$border]);
}
break;
}
}
}
return $brd;
}
/**
* Determine whether a string is empty.
* @param $str (string) string to be checked
* @return bool true if string is empty
* @since 4.5.044 (2009-04-16)
* @public static
*/
public static function empty_string($str) {
return (is_null($str) OR (is_string($str) AND (strlen($str) == 0)));
}
/**
* Returns a temporary filename for caching object on filesystem.
* @param $type (string) Type of file (name of the subdir on the tcpdf cache folder).
* @param $file_id (string) TCPDF file_id.
* @return string filename.
* @since 4.5.000 (2008-12-31)
* @public static
*/
public static function getObjFilename($type='tmp', $file_id='') {
return tempnam(K_PATH_CACHE, '__tcpdf_'.$file_id.'_'.$type.'_'.md5(TCPDF_STATIC::getRandomSeed()).'_');
}
/**
* Add "\" before "\", "(" and ")"
* @param $s (string) string to escape.
* @return string escaped string.
* @public static
*/
public static function _escape($s) {
// the chr(13) substitution fixes the Bugs item #1421290.
return strtr($s, array(')' => '\\)', '(' => '\\(', '\\' => '\\\\', chr(13) => '\r'));
}
/**
* Escape some special characters (< > &) for XML output.
* @param $str (string) Input string to convert.
* @return converted string
* @since 5.9.121 (2011-09-28)
* @public static
*/
public static function _escapeXML($str) {
$replaceTable = array("\0" => '', '&' => '&', '<' => '<', '>' => '>');
$str = strtr($str, $replaceTable);
return $str;
}
/**
* Creates a copy of a class object
* @param $object (object) class object to be cloned
* @return cloned object
* @since 4.5.029 (2009-03-19)
* @public static
*/
public static function objclone($object) {
if (($object instanceof Imagick) AND (version_compare(phpversion('imagick'), '3.0.1') !== 1)) {
// on the versions after 3.0.1 the clone() method was deprecated in favour of clone keyword
return @$object->clone();
}
return @clone($object);
}
/**
* Output input data and compress it if possible.
* @param $data (string) Data to output.
* @param $length (int) Data length in bytes.
* @since 5.9.086
* @public static
*/
public static function sendOutputData($data, $length) {
if (!isset($_SERVER['HTTP_ACCEPT_ENCODING']) OR empty($_SERVER['HTTP_ACCEPT_ENCODING'])) {
// the content length may vary if the server is using compression
header('Content-Length: '.$length);
}
echo $data;
}
/**
* Replace page number aliases with number.
* @param $page (string) Page content.
* @param $replace (array) Array of replacements (array keys are replacement strings, values are alias arrays).
* @param $diff (int) If passed, this will be set to the total char number difference between alias and replacements.
* @return replaced page content and updated $diff parameter as array.
* @public static
*/
public static function replacePageNumAliases($page, $replace, $diff=0) {
foreach ($replace as $rep) {
foreach ($rep[3] as $a) {
if (strpos($page, $a) !== false) {
$page = str_replace($a, $rep[0], $page);
$diff += ($rep[2] - $rep[1]);
}
}
}
return array($page, $diff);
}
/**
* Returns timestamp in seconds from formatted date-time.
* @param $date (string) Formatted date-time.
* @return int seconds.
* @since 5.9.152 (2012-03-23)
* @public static
*/
public static function getTimestamp($date) {
if (($date[0] == 'D') AND ($date[1] == ':')) {
// remove date prefix if present
$date = substr($date, 2);
}
return strtotime($date);
}
/**
* Returns a formatted date-time.
* @param $time (int) Time in seconds.
* @return string escaped date string.
* @since 5.9.152 (2012-03-23)
* @public static
*/
public static function getFormattedDate($time) {
return substr_replace(date('YmdHisO', intval($time)), '\'', (0 - 2), 0).'\'';
}
/**
* Returns a string containing random data to be used as a seed for encryption methods.
* @param $seed (string) starting seed value
* @return string containing random data
* @author Nicola Asuni
* @since 5.9.006 (2010-10-19)
* @public static
*/
public static function getRandomSeed($seed='') {
$rnd = uniqid(rand().microtime(true), true);
if (function_exists('posix_getpid')) {
$rnd .= posix_getpid();
}
if (function_exists('openssl_random_pseudo_bytes') AND (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN')) {
// this is not used on windows systems because it is very slow for a know bug
$rnd .= openssl_random_pseudo_bytes(512);
} else {
for ($i = 0; $i < 23; ++$i) {
$rnd .= uniqid('', true);
}
}
return $rnd.$seed.__FILE__.serialize($_SERVER).microtime(true);
}
/**
* Encrypts a string using MD5 and returns it's value as a binary string.
* @param $str (string) input string
* @return String MD5 encrypted binary string
* @since 2.0.000 (2008-01-02)
* @public static
*/
public static function _md5_16($str) {
return pack('H*', md5($str));
}
/**
* Returns the input text exrypted using AES algorithm and the specified key.
* This method requires openssl or mcrypt. Text is padded to 16bytes blocks
* @param $key (string) encryption key
* @param $text (String) input text to be encrypted
* @return String encrypted text
* @author Nicola Asuni
* @since 5.0.005 (2010-05-11)
* @public static
*/
public static function _AES($key, $text) {
// padding (RFC 2898, PKCS #5: Password-Based Cryptography Specification Version 2.0)
$padding = 16 - (strlen($text) % 16);
$text .= str_repeat(chr($padding), $padding);
if (extension_loaded('openssl')) {
$iv = openssl_random_pseudo_bytes (openssl_cipher_iv_length('aes-256-cbc'));
$text = openssl_encrypt($text, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
return $iv.substr($text, 0, -16);
}
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC), MCRYPT_RAND);
$text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
$text = $iv.$text;
return $text;
}
/**
* Returns the input text exrypted using AES algorithm and the specified key.
* This method requires openssl or mcrypt. Text is not padded
* @param $key (string) encryption key
* @param $text (String) input text to be encrypted
* @return String encrypted text
* @author Nicola Asuni
* @since TODO
* @public static
*/
public static function _AESnopad($key, $text) {
if (extension_loaded('openssl')) {
$iv = str_repeat("\x00", openssl_cipher_iv_length('aes-256-cbc'));
$text = openssl_encrypt($text, 'aes-256-cbc', $key, OPENSSL_RAW_DATA, $iv);
return substr($text, 0, -16);
}
$iv = str_repeat("\x00", mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC));
$text = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $text, MCRYPT_MODE_CBC, $iv);
return $text;
}
/**
* Returns the input text encrypted using RC4 algorithm and the specified key.
* RC4 is the standard encryption algorithm used in PDF format
* @param $key (string) Encryption key.
* @param $text (String) Input text to be encrypted.
* @param $last_enc_key (String) Reference to last RC4 key encrypted.
* @param $last_enc_key_c (String) Reference to last RC4 computed key.
* @return String encrypted text
* @since 2.0.000 (2008-01-02)
* @author Klemen Vodopivec, Nicola Asuni
* @public static
*/
public static function _RC4($key, $text, &$last_enc_key, &$last_enc_key_c) {
if (function_exists('mcrypt_encrypt') AND ($out = @mcrypt_encrypt(MCRYPT_ARCFOUR, $key, $text, MCRYPT_MODE_STREAM, ''))) {
// try to use mcrypt function if exist
return $out;
}
if ($last_enc_key != $key) {
$k = str_repeat($key, ((256 / strlen($key)) + 1));
$rc4 = range(0, 255);
$j = 0;
for ($i = 0; $i < 256; ++$i) {
$t = $rc4[$i];
$j = ($j + $t + ord($k[$i])) % 256;
$rc4[$i] = $rc4[$j];
$rc4[$j] = $t;
}
$last_enc_key = $key;
$last_enc_key_c = $rc4;
} else {
$rc4 = $last_enc_key_c;
}
$len = strlen($text);
$a = 0;
$b = 0;
$out = '';
for ($i = 0; $i < $len; ++$i) {
$a = ($a + 1) % 256;
$t = $rc4[$a];
$b = ($b + $t) % 256;
$rc4[$a] = $rc4[$b];
$rc4[$b] = $t;
$k = $rc4[($rc4[$a] + $rc4[$b]) % 256];
$out .= chr(ord($text[$i]) ^ $k);
}
return $out;
}
/**
* Return the permission code used on encryption (P value).
* @param $permissions (Array) the set of permissions (specify the ones you want to block).
* @param $mode (int) encryption strength: 0 = RC4 40 bit; 1 = RC4 128 bit; 2 = AES 128 bit; 3 = AES 256 bit.
* @since 5.0.005 (2010-05-12)
* @author Nicola Asuni
* @public static
*/
public static function getUserPermissionCode($permissions, $mode=0) {
$options = array(
'owner' => 2, // bit 2 -- inverted logic: cleared by default
'print' => 4, // bit 3
'modify' => 8, // bit 4
'copy' => 16, // bit 5
'annot-forms' => 32, // bit 6
'fill-forms' => 256, // bit 9
'extract' => 512, // bit 10
'assemble' => 1024,// bit 11
'print-high' => 2048 // bit 12
);
$protection = 2147422012; // 32 bit: (01111111 11111111 00001111 00111100)
foreach ($permissions as $permission) {
if (isset($options[$permission])) {
if (($mode > 0) OR ($options[$permission] <= 32)) {
// set only valid permissions
if ($options[$permission] == 2) {
// the logic for bit 2 is inverted (cleared by default)
$protection += $options[$permission];
} else {
$protection -= $options[$permission];
}
}
}
}
return $protection;
}
/**
* Convert hexadecimal string to string
* @param $bs (string) byte-string to convert
* @return String
* @since 5.0.005 (2010-05-12)
* @author Nicola Asuni
* @public static
*/
public static function convertHexStringToString($bs) {
$string = ''; // string to be returned
$bslength = strlen($bs);
if (($bslength % 2) != 0) {
// padding
$bs .= '0';
++$bslength;
}
for ($i = 0; $i < $bslength; $i += 2) {
$string .= chr(hexdec($bs[$i].$bs[($i + 1)]));
}
return $string;
}
/**
* Convert string to hexadecimal string (byte string)
* @param $s (string) string to convert
* @return byte string
* @since 5.0.010 (2010-05-17)
* @author Nicola Asuni
* @public static
*/
public static function convertStringToHexString($s) {
$bs = '';
$chars = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as $c) {
$bs .= sprintf('%02s', dechex(ord($c)));
}
return $bs;
}
/**
* Convert encryption P value to a string of bytes, low-order byte first.
* @param $protection (string) 32bit encryption permission value (P value)
* @return String
* @since 5.0.005 (2010-05-12)
* @author Nicola Asuni
* @public static
*/
public static function getEncPermissionsString($protection) {
$binprot = sprintf('%032b', $protection);
$str = chr(bindec(substr($binprot, 24, 8)));
$str .= chr(bindec(substr($binprot, 16, 8)));
$str .= chr(bindec(substr($binprot, 8, 8)));
$str .= chr(bindec(substr($binprot, 0, 8)));
return $str;
}
/**
* Encode a name object.
* @param $name (string) Name object to encode.
* @return (string) Encoded name object.
* @author Nicola Asuni
* @since 5.9.097 (2011-06-23)
* @public static
*/
public static function encodeNameObject($name) {
$escname = '';
$length = strlen($name);
for ($i = 0; $i < $length; ++$i) {
$chr = $name[$i];
if (preg_match('/[0-9a-zA-Z#_=-]/', $chr) == 1) {
$escname .= $chr;
} else {
$escname .= sprintf('#%02X', ord($chr));
}
}
return $escname;
}
/**
* Convert JavaScript form fields properties array to Annotation Properties array.
* @param $prop (array) javascript field properties. Possible values are described on official Javascript for Acrobat API reference.
* @param $spot_colors (array) Reference to spot colors array.
* @param $rtl (boolean) True if in Right-To-Left text direction mode, false otherwise.
* @return array of annotation properties
* @author Nicola Asuni
* @since 4.8.000 (2009-09-06)
* @public static
*/
public static function getAnnotOptFromJSProp($prop, &$spot_colors, $rtl=false) {
if (isset($prop['aopt']) AND is_array($prop['aopt'])) {
// the annotation options area lready defined
return $prop['aopt'];
}
$opt = array(); // value to be returned
// alignment: Controls how the text is laid out within the text field.
if (isset($prop['alignment'])) {
switch ($prop['alignment']) {
case 'left': {
$opt['q'] = 0;
break;
}
case 'center': {
$opt['q'] = 1;
break;
}
case 'right': {
$opt['q'] = 2;
break;
}
default: {
$opt['q'] = ($rtl)?2:0;
break;
}
}
}
// lineWidth: Specifies the thickness of the border when stroking the perimeter of a field's rectangle.
if (isset($prop['lineWidth'])) {
$linewidth = intval($prop['lineWidth']);
} else {
$linewidth = 1;
}
// borderStyle: The border style for a field.
if (isset($prop['borderStyle'])) {
switch ($prop['borderStyle']) {
case 'border.d':
case 'dashed': {
$opt['border'] = array(0, 0, $linewidth, array(3, 2));
$opt['bs'] = array('w'=>$linewidth, 's'=>'D', 'd'=>array(3, 2));
break;
}
case 'border.b':
case 'beveled': {
$opt['border'] = array(0, 0, $linewidth);
$opt['bs'] = array('w'=>$linewidth, 's'=>'B');
break;
}
case 'border.i':
case 'inset': {
$opt['border'] = array(0, 0, $linewidth);
$opt['bs'] = array('w'=>$linewidth, 's'=>'I');
break;
}
case 'border.u':
case 'underline': {
$opt['border'] = array(0, 0, $linewidth);
$opt['bs'] = array('w'=>$linewidth, 's'=>'U');
break;
}
case 'border.s':
case 'solid': {
$opt['border'] = array(0, 0, $linewidth);
$opt['bs'] = array('w'=>$linewidth, 's'=>'S');
break;
}
default: {
break;
}
}
}
if (isset($prop['border']) AND is_array($prop['border'])) {
$opt['border'] = $prop['border'];
}
if (!isset($opt['mk'])) {
$opt['mk'] = array();
}
if (!isset($opt['mk']['if'])) {
$opt['mk']['if'] = array();
}
$opt['mk']['if']['a'] = array(0.5, 0.5);
// buttonAlignX: Controls how space is distributed from the left of the button face with respect to the icon.
if (isset($prop['buttonAlignX'])) {
$opt['mk']['if']['a'][0] = $prop['buttonAlignX'];
}
// buttonAlignY: Controls how unused space is distributed from the bottom of the button face with respect to the icon.
if (isset($prop['buttonAlignY'])) {
$opt['mk']['if']['a'][1] = $prop['buttonAlignY'];
}
// buttonFitBounds: If true, the extent to which the icon may be scaled is set to the bounds of the button field.
if (isset($prop['buttonFitBounds']) AND ($prop['buttonFitBounds'] == 'true')) {
$opt['mk']['if']['fb'] = true;
}
// buttonScaleHow: Controls how the icon is scaled (if necessary) to fit inside the button face.
if (isset($prop['buttonScaleHow'])) {
switch ($prop['buttonScaleHow']) {
case 'scaleHow.proportional': {
$opt['mk']['if']['s'] = 'P';
break;
}
case 'scaleHow.anamorphic': {
$opt['mk']['if']['s'] = 'A';
break;
}
}
}
// buttonScaleWhen: Controls when an icon is scaled to fit inside the button face.
if (isset($prop['buttonScaleWhen'])) {
switch ($prop['buttonScaleWhen']) {
case 'scaleWhen.always': {
$opt['mk']['if']['sw'] = 'A';
break;
}
case 'scaleWhen.never': {
$opt['mk']['if']['sw'] = 'N';
break;
}
case 'scaleWhen.tooBig': {
$opt['mk']['if']['sw'] = 'B';
break;
}
case 'scaleWhen.tooSmall': {
$opt['mk']['if']['sw'] = 'S';
break;
}
}
}
// buttonPosition: Controls how the text and the icon of the button are positioned with respect to each other within the button face.
if (isset($prop['buttonPosition'])) {
switch ($prop['buttonPosition']) {
case 0:
case 'position.textOnly': {
$opt['mk']['tp'] = 0;
break;
}
case 1:
case 'position.iconOnly': {
$opt['mk']['tp'] = 1;
break;
}
case 2:
case 'position.iconTextV': {
$opt['mk']['tp'] = 2;
break;
}
case 3:
case 'position.textIconV': {
$opt['mk']['tp'] = 3;
break;
}
case 4:
case 'position.iconTextH': {
$opt['mk']['tp'] = 4;
break;
}
case 5:
case 'position.textIconH': {
$opt['mk']['tp'] = 5;
break;
}
case 6:
case 'position.overlay': {
$opt['mk']['tp'] = 6;
break;
}
}
}
// fillColor: Specifies the background color for a field.
if (isset($prop['fillColor'])) {
if (is_array($prop['fillColor'])) {
$opt['mk']['bg'] = $prop['fillColor'];
} else {
$opt['mk']['bg'] = TCPDF_COLORS::convertHTMLColorToDec($prop['fillColor'], $spot_colors);
}
}
// strokeColor: Specifies the stroke color for a field that is used to stroke the rectangle of the field with a line as large as the line width.
if (isset($prop['strokeColor'])) {
if (is_array($prop['strokeColor'])) {
$opt['mk']['bc'] = $prop['strokeColor'];
} else {
$opt['mk']['bc'] = TCPDF_COLORS::convertHTMLColorToDec($prop['strokeColor'], $spot_colors);
}
}
// rotation: The rotation of a widget in counterclockwise increments.
if (isset($prop['rotation'])) {
$opt['mk']['r'] = $prop['rotation'];
}
// charLimit: Limits the number of characters that a user can type into a text field.
if (isset($prop['charLimit'])) {
$opt['maxlen'] = intval($prop['charLimit']);
}
if (!isset($ff)) {
$ff = 0; // default value
}
// readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
$ff += 1 << 0;
}
// required: Specifies whether a field requires a value.
if (isset($prop['required']) AND ($prop['required'] == 'true')) {
$ff += 1 << 1;
}
// multiline: Controls how text is wrapped within the field.
if (isset($prop['multiline']) AND ($prop['multiline'] == 'true')) {
$ff += 1 << 12;
}
// password: Specifies whether the field should display asterisks when data is entered in the field.
if (isset($prop['password']) AND ($prop['password'] == 'true')) {
$ff += 1 << 13;
}
// NoToggleToOff: If set, exactly one radio button shall be selected at all times; selecting the currently selected button has no effect.
if (isset($prop['NoToggleToOff']) AND ($prop['NoToggleToOff'] == 'true')) {
$ff += 1 << 14;
}
// Radio: If set, the field is a set of radio buttons.
if (isset($prop['Radio']) AND ($prop['Radio'] == 'true')) {
$ff += 1 << 15;
}
// Pushbutton: If set, the field is a pushbutton that does not retain a permanent value.
if (isset($prop['Pushbutton']) AND ($prop['Pushbutton'] == 'true')) {
$ff += 1 << 16;
}
// Combo: If set, the field is a combo box; if clear, the field is a list box.
if (isset($prop['Combo']) AND ($prop['Combo'] == 'true')) {
$ff += 1 << 17;
}
// editable: Controls whether a combo box is editable.
if (isset($prop['editable']) AND ($prop['editable'] == 'true')) {
$ff += 1 << 18;
}
// Sort: If set, the field's option items shall be sorted alphabetically.
if (isset($prop['Sort']) AND ($prop['Sort'] == 'true')) {
$ff += 1 << 19;
}
// fileSelect: If true, sets the file-select flag in the Options tab of the text field (Field is Used for File Selection).
if (isset($prop['fileSelect']) AND ($prop['fileSelect'] == 'true')) {
$ff += 1 << 20;
}
// multipleSelection: If true, indicates that a list box allows a multiple selection of items.
if (isset($prop['multipleSelection']) AND ($prop['multipleSelection'] == 'true')) {
$ff += 1 << 21;
}
// doNotSpellCheck: If true, spell checking is not performed on this editable text field.
if (isset($prop['doNotSpellCheck']) AND ($prop['doNotSpellCheck'] == 'true')) {
$ff += 1 << 22;
}
// doNotScroll: If true, the text field does not scroll and the user, therefore, is limited by the rectangular region designed for the field.
if (isset($prop['doNotScroll']) AND ($prop['doNotScroll'] == 'true')) {
$ff += 1 << 23;
}
// comb: If set to true, the field background is drawn as series of boxes (one for each character in the value of the field) and each character of the content is drawn within those boxes. The number of boxes drawn is determined from the charLimit property. It applies only to text fields. The setter will also raise if any of the following field properties are also set multiline, password, and fileSelect. A side-effect of setting this property is that the doNotScroll property is also set.
if (isset($prop['comb']) AND ($prop['comb'] == 'true')) {
$ff += 1 << 24;
}
// radiosInUnison: If false, even if a group of radio buttons have the same name and export value, they behave in a mutually exclusive fashion, like HTML radio buttons.
if (isset($prop['radiosInUnison']) AND ($prop['radiosInUnison'] == 'true')) {
$ff += 1 << 25;
}
// richText: If true, the field allows rich text formatting.
if (isset($prop['richText']) AND ($prop['richText'] == 'true')) {
$ff += 1 << 25;
}
// commitOnSelChange: Controls whether a field value is committed after a selection change.
if (isset($prop['commitOnSelChange']) AND ($prop['commitOnSelChange'] == 'true')) {
$ff += 1 << 26;
}
$opt['ff'] = $ff;
// defaultValue: The default value of a field - that is, the value that the field is set to when the form is reset.
if (isset($prop['defaultValue'])) {
$opt['dv'] = $prop['defaultValue'];
}
$f = 4; // default value for annotation flags
// readonly: The read-only characteristic of a field. If a field is read-only, the user can see the field but cannot change it.
if (isset($prop['readonly']) AND ($prop['readonly'] == 'true')) {
$f += 1 << 6;
}
// display: Controls whether the field is hidden or visible on screen and in print.
if (isset($prop['display'])) {
if ($prop['display'] == 'display.visible') {
//
} elseif ($prop['display'] == 'display.hidden') {
$f += 1 << 1;
} elseif ($prop['display'] == 'display.noPrint') {
$f -= 1 << 2;
} elseif ($prop['display'] == 'display.noView') {
$f += 1 << 5;
}
}
$opt['f'] = $f;
// currentValueIndices: Reads and writes single or multiple values of a list box or combo box.
if (isset($prop['currentValueIndices']) AND is_array($prop['currentValueIndices'])) {
$opt['i'] = $prop['currentValueIndices'];
}
// value: The value of the field data that the user has entered.
if (isset($prop['value'])) {
if (is_array($prop['value'])) {
$opt['opt'] = array();
foreach ($prop['value'] AS $key => $optval) {
// exportValues: An array of strings representing the export values for the field.
if (isset($prop['exportValues'][$key])) {
$opt['opt'][$key] = array($prop['exportValues'][$key], $prop['value'][$key]);
} else {
$opt['opt'][$key] = $prop['value'][$key];
}
}
} else {
$opt['v'] = $prop['value'];
}
}
// richValue: This property specifies the text contents and formatting of a rich text field.
if (isset($prop['richValue'])) {
$opt['rv'] = $prop['richValue'];
}
// submitName: If nonempty, used during form submission instead of name. Only applicable if submitting in HTML format (that is, URL-encoded).
if (isset($prop['submitName'])) {
$opt['tm'] = $prop['submitName'];
}
// name: Fully qualified field name.
if (isset($prop['name'])) {
$opt['t'] = $prop['name'];
}
// userName: The user name (short description string) of the field.
if (isset($prop['userName'])) {
$opt['tu'] = $prop['userName'];
}
// highlight: Defines how a button reacts when a user clicks it.
if (isset($prop['highlight'])) {
switch ($prop['highlight']) {
case 'none':
case 'highlight.n': {
$opt['h'] = 'N';
break;
}
case 'invert':
case 'highlight.i': {
$opt['h'] = 'i';
break;
}
case 'push':
case 'highlight.p': {
$opt['h'] = 'P';
break;
}
case 'outline':
case 'highlight.o': {
$opt['h'] = 'O';
break;
}