0
0

Settings.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /**
  3. * This file is part of PHPWord - A pure PHP library for reading and writing
  4. * word processing documents.
  5. *
  6. * PHPWord is free software distributed under the terms of the GNU Lesser
  7. * General Public License version 3 as published by the Free Software Foundation.
  8. *
  9. * For the full copyright and license information, please read the LICENSE
  10. * file that was distributed with this source code. For the full list of
  11. * contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
  12. *
  13. * @see https://github.com/PHPOffice/PHPWord
  14. * @copyright 2010-2018 PHPWord contributors
  15. * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
  16. */
  17. namespace PhpOffice\PhpWord;
  18. /**
  19. * PHPWord settings class
  20. *
  21. * @since 0.8.0
  22. */
  23. class Settings
  24. {
  25. /**
  26. * Zip libraries
  27. *
  28. * @const string
  29. */
  30. const ZIPARCHIVE = 'ZipArchive';
  31. const PCLZIP = 'PclZip';
  32. const OLD_LIB = 'PhpOffice\\PhpWord\\Shared\\ZipArchive'; // @deprecated 0.11
  33. /**
  34. * PDF rendering libraries
  35. *
  36. * @const string
  37. */
  38. const PDF_RENDERER_DOMPDF = 'DomPDF';
  39. const PDF_RENDERER_TCPDF = 'TCPDF';
  40. const PDF_RENDERER_MPDF = 'MPDF';
  41. /**
  42. * Measurement units multiplication factor
  43. *
  44. * Applied to:
  45. * - Section: margins, header/footer height, gutter, column spacing
  46. * - Tab: position
  47. * - Indentation: left, right, firstLine, hanging
  48. * - Spacing: before, after
  49. *
  50. * @const string
  51. */
  52. const UNIT_TWIP = 'twip'; // = 1/20 point
  53. const UNIT_CM = 'cm';
  54. const UNIT_MM = 'mm';
  55. const UNIT_INCH = 'inch';
  56. const UNIT_POINT = 'point'; // = 1/72 inch
  57. const UNIT_PICA = 'pica'; // = 1/6 inch = 12 points
  58. /**
  59. * Default font settings
  60. *
  61. * OOXML defined font size values in halfpoints, i.e. twice of what PhpWord
  62. * use, and the conversion will be conducted during XML writing.
  63. */
  64. const DEFAULT_FONT_NAME = 'Arial';
  65. const DEFAULT_FONT_SIZE = 10;
  66. const DEFAULT_FONT_COLOR = '000000';
  67. const DEFAULT_FONT_CONTENT_TYPE = 'default'; // default|eastAsia|cs
  68. /**
  69. * Compatibility option for XMLWriter
  70. *
  71. * @var bool
  72. */
  73. private static $xmlWriterCompatibility = true;
  74. /**
  75. * Name of the class used for Zip file management
  76. *
  77. * @var string
  78. */
  79. private static $zipClass = self::ZIPARCHIVE;
  80. /**
  81. * Name of the external Library used for rendering PDF files
  82. *
  83. * @var string
  84. */
  85. private static $pdfRendererName = null;
  86. /**
  87. * Directory Path to the external Library used for rendering PDF files
  88. *
  89. * @var string
  90. */
  91. private static $pdfRendererPath = null;
  92. /**
  93. * Measurement unit
  94. *
  95. * @var int|float
  96. */
  97. private static $measurementUnit = self::UNIT_TWIP;
  98. /**
  99. * Default font name
  100. *
  101. * @var string
  102. */
  103. private static $defaultFontName = self::DEFAULT_FONT_NAME;
  104. /**
  105. * Default font size
  106. * @var int
  107. */
  108. private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
  109. /**
  110. * The user defined temporary directory.
  111. *
  112. * @var string
  113. */
  114. private static $tempDir = '';
  115. /**
  116. * Enables built-in output escaping mechanism.
  117. * Default value is `false` for backward compatibility with versions below 0.13.0.
  118. *
  119. * @var bool
  120. */
  121. private static $outputEscapingEnabled = false;
  122. /**
  123. * Return the compatibility option used by the XMLWriter
  124. *
  125. * @return bool Compatibility
  126. */
  127. public static function hasCompatibility()
  128. {
  129. return self::$xmlWriterCompatibility;
  130. }
  131. /**
  132. * Set the compatibility option used by the XMLWriter
  133. *
  134. * This sets the setIndent and setIndentString for better compatibility
  135. *
  136. * @param bool $compatibility
  137. * @return bool
  138. */
  139. public static function setCompatibility($compatibility)
  140. {
  141. $compatibility = (bool) $compatibility;
  142. self::$xmlWriterCompatibility = $compatibility;
  143. return true;
  144. }
  145. /**
  146. * Get zip handler class
  147. *
  148. * @return string
  149. */
  150. public static function getZipClass()
  151. {
  152. return self::$zipClass;
  153. }
  154. /**
  155. * Set zip handler class
  156. *
  157. * @param string $zipClass
  158. * @return bool
  159. */
  160. public static function setZipClass($zipClass)
  161. {
  162. if (in_array($zipClass, array(self::PCLZIP, self::ZIPARCHIVE, self::OLD_LIB))) {
  163. self::$zipClass = $zipClass;
  164. return true;
  165. }
  166. return false;
  167. }
  168. /**
  169. * Set details of the external library for rendering PDF files
  170. *
  171. * @param string $libraryName
  172. * @param string $libraryBaseDir
  173. * @return bool Success or failure
  174. */
  175. public static function setPdfRenderer($libraryName, $libraryBaseDir)
  176. {
  177. if (!self::setPdfRendererName($libraryName)) {
  178. return false;
  179. }
  180. return self::setPdfRendererPath($libraryBaseDir);
  181. }
  182. /**
  183. * Return the PDF Rendering Library.
  184. *
  185. * @return string
  186. */
  187. public static function getPdfRendererName()
  188. {
  189. return self::$pdfRendererName;
  190. }
  191. /**
  192. * Identify the external library to use for rendering PDF files
  193. *
  194. * @param string $libraryName
  195. * @return bool
  196. */
  197. public static function setPdfRendererName($libraryName)
  198. {
  199. $pdfRenderers = array(self::PDF_RENDERER_DOMPDF, self::PDF_RENDERER_TCPDF, self::PDF_RENDERER_MPDF);
  200. if (!in_array($libraryName, $pdfRenderers)) {
  201. return false;
  202. }
  203. self::$pdfRendererName = $libraryName;
  204. return true;
  205. }
  206. /**
  207. * Return the directory path to the PDF Rendering Library.
  208. *
  209. * @return string
  210. */
  211. public static function getPdfRendererPath()
  212. {
  213. return self::$pdfRendererPath;
  214. }
  215. /**
  216. * Location of external library to use for rendering PDF files
  217. *
  218. * @param string $libraryBaseDir Directory path to the library's base folder
  219. * @return bool Success or failure
  220. */
  221. public static function setPdfRendererPath($libraryBaseDir)
  222. {
  223. if (false === file_exists($libraryBaseDir) || false === is_readable($libraryBaseDir)) {
  224. return false;
  225. }
  226. self::$pdfRendererPath = $libraryBaseDir;
  227. return true;
  228. }
  229. /**
  230. * Get measurement unit
  231. *
  232. * @return string
  233. */
  234. public static function getMeasurementUnit()
  235. {
  236. return self::$measurementUnit;
  237. }
  238. /**
  239. * Set measurement unit
  240. *
  241. * @param string $value
  242. * @return bool
  243. */
  244. public static function setMeasurementUnit($value)
  245. {
  246. $units = array(self::UNIT_TWIP, self::UNIT_CM, self::UNIT_MM, self::UNIT_INCH,
  247. self::UNIT_POINT, self::UNIT_PICA, );
  248. if (!in_array($value, $units)) {
  249. return false;
  250. }
  251. self::$measurementUnit = $value;
  252. return true;
  253. }
  254. /**
  255. * Sets the user defined path to temporary directory.
  256. *
  257. * @since 0.12.0
  258. *
  259. * @param string $tempDir The user defined path to temporary directory
  260. */
  261. public static function setTempDir($tempDir)
  262. {
  263. self::$tempDir = $tempDir;
  264. }
  265. /**
  266. * Returns path to temporary directory.
  267. *
  268. * @since 0.12.0
  269. *
  270. * @return string
  271. */
  272. public static function getTempDir()
  273. {
  274. if (!empty(self::$tempDir)) {
  275. $tempDir = self::$tempDir;
  276. } else {
  277. $tempDir = sys_get_temp_dir();
  278. }
  279. return $tempDir;
  280. }
  281. /**
  282. * @since 0.13.0
  283. *
  284. * @return bool
  285. */
  286. public static function isOutputEscapingEnabled()
  287. {
  288. return self::$outputEscapingEnabled;
  289. }
  290. /**
  291. * @since 0.13.0
  292. *
  293. * @param bool $outputEscapingEnabled
  294. */
  295. public static function setOutputEscapingEnabled($outputEscapingEnabled)
  296. {
  297. self::$outputEscapingEnabled = $outputEscapingEnabled;
  298. }
  299. /**
  300. * Get default font name
  301. *
  302. * @return string
  303. */
  304. public static function getDefaultFontName()
  305. {
  306. return self::$defaultFontName;
  307. }
  308. /**
  309. * Set default font name
  310. *
  311. * @param string $value
  312. * @return bool
  313. */
  314. public static function setDefaultFontName($value)
  315. {
  316. if (is_string($value) && trim($value) !== '') {
  317. self::$defaultFontName = $value;
  318. return true;
  319. }
  320. return false;
  321. }
  322. /**
  323. * Get default font size
  324. *
  325. * @return int
  326. */
  327. public static function getDefaultFontSize()
  328. {
  329. return self::$defaultFontSize;
  330. }
  331. /**
  332. * Set default font size
  333. *
  334. * @param int $value
  335. * @return bool
  336. */
  337. public static function setDefaultFontSize($value)
  338. {
  339. $value = (int) $value;
  340. if ($value > 0) {
  341. self::$defaultFontSize = $value;
  342. return true;
  343. }
  344. return false;
  345. }
  346. /**
  347. * Load setting from phpword.yml or phpword.yml.dist
  348. *
  349. * @param string $filename
  350. * @return array
  351. */
  352. public static function loadConfig($filename = null)
  353. {
  354. // Get config file
  355. $configFile = null;
  356. $configPath = __DIR__ . '/../../';
  357. if ($filename !== null) {
  358. $files = array($filename);
  359. } else {
  360. $files = array("{$configPath}phpword.ini", "{$configPath}phpword.ini.dist");
  361. }
  362. foreach ($files as $file) {
  363. if (file_exists($file)) {
  364. $configFile = realpath($file);
  365. break;
  366. }
  367. }
  368. // Parse config file
  369. $config = array();
  370. if ($configFile !== null) {
  371. $config = @parse_ini_file($configFile);
  372. if ($config === false) {
  373. return $config;
  374. }
  375. }
  376. // Set config value
  377. foreach ($config as $key => $value) {
  378. $method = "set{$key}";
  379. if (method_exists(__CLASS__, $method)) {
  380. self::$method($value);
  381. }
  382. }
  383. return $config;
  384. }
  385. /**
  386. * Return the compatibility option used by the XMLWriter
  387. *
  388. * @deprecated 0.10.0
  389. *
  390. * @codeCoverageIgnore
  391. */
  392. public static function getCompatibility()
  393. {
  394. return self::hasCompatibility();
  395. }
  396. }