Settings.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. const DEFAULT_PAPER = 'A4';
  69. /**
  70. * Compatibility option for XMLWriter
  71. *
  72. * @var bool
  73. */
  74. private static $xmlWriterCompatibility = true;
  75. /**
  76. * Name of the class used for Zip file management
  77. *
  78. * @var string
  79. */
  80. private static $zipClass = self::ZIPARCHIVE;
  81. /**
  82. * Name of the external Library used for rendering PDF files
  83. *
  84. * @var string
  85. */
  86. private static $pdfRendererName = null;
  87. /**
  88. * Directory Path to the external Library used for rendering PDF files
  89. *
  90. * @var string
  91. */
  92. private static $pdfRendererPath = null;
  93. /**
  94. * Measurement unit
  95. *
  96. * @var int|float
  97. */
  98. private static $measurementUnit = self::UNIT_TWIP;
  99. /**
  100. * Default font name
  101. *
  102. * @var string
  103. */
  104. private static $defaultFontName = self::DEFAULT_FONT_NAME;
  105. /**
  106. * Default font size
  107. * @var int
  108. */
  109. private static $defaultFontSize = self::DEFAULT_FONT_SIZE;
  110. /**
  111. * Default paper
  112. * @var string
  113. */
  114. private static $defaultPaper = self::DEFAULT_PAPER;
  115. /**
  116. * The user defined temporary directory.
  117. *
  118. * @var string
  119. */
  120. private static $tempDir = '';
  121. /**
  122. * Enables built-in output escaping mechanism.
  123. * Default value is `false` for backward compatibility with versions below 0.13.0.
  124. *
  125. * @var bool
  126. */
  127. private static $outputEscapingEnabled = false;
  128. /**
  129. * Return the compatibility option used by the XMLWriter
  130. *
  131. * @return bool Compatibility
  132. */
  133. public static function hasCompatibility()
  134. {
  135. return self::$xmlWriterCompatibility;
  136. }
  137. /**
  138. * Set the compatibility option used by the XMLWriter
  139. *
  140. * This sets the setIndent and setIndentString for better compatibility
  141. *
  142. * @param bool $compatibility
  143. * @return bool
  144. */
  145. public static function setCompatibility($compatibility)
  146. {
  147. $compatibility = (bool) $compatibility;
  148. self::$xmlWriterCompatibility = $compatibility;
  149. return true;
  150. }
  151. /**
  152. * Get zip handler class
  153. *
  154. * @return string
  155. */
  156. public static function getZipClass()
  157. {
  158. return self::$zipClass;
  159. }
  160. /**
  161. * Set zip handler class
  162. *
  163. * @param string $zipClass
  164. * @return bool
  165. */
  166. public static function setZipClass($zipClass)
  167. {
  168. if (in_array($zipClass, array(self::PCLZIP, self::ZIPARCHIVE, self::OLD_LIB))) {
  169. self::$zipClass = $zipClass;
  170. return true;
  171. }
  172. return false;
  173. }
  174. /**
  175. * Set details of the external library for rendering PDF files
  176. *
  177. * @param string $libraryName
  178. * @param string $libraryBaseDir
  179. * @return bool Success or failure
  180. */
  181. public static function setPdfRenderer($libraryName, $libraryBaseDir)
  182. {
  183. if (!self::setPdfRendererName($libraryName)) {
  184. return false;
  185. }
  186. return self::setPdfRendererPath($libraryBaseDir);
  187. }
  188. /**
  189. * Return the PDF Rendering Library.
  190. *
  191. * @return string
  192. */
  193. public static function getPdfRendererName()
  194. {
  195. return self::$pdfRendererName;
  196. }
  197. /**
  198. * Identify the external library to use for rendering PDF files
  199. *
  200. * @param string $libraryName
  201. * @return bool
  202. */
  203. public static function setPdfRendererName($libraryName)
  204. {
  205. $pdfRenderers = array(self::PDF_RENDERER_DOMPDF, self::PDF_RENDERER_TCPDF, self::PDF_RENDERER_MPDF);
  206. if (!in_array($libraryName, $pdfRenderers)) {
  207. return false;
  208. }
  209. self::$pdfRendererName = $libraryName;
  210. return true;
  211. }
  212. /**
  213. * Return the directory path to the PDF Rendering Library.
  214. *
  215. * @return string
  216. */
  217. public static function getPdfRendererPath()
  218. {
  219. return self::$pdfRendererPath;
  220. }
  221. /**
  222. * Location of external library to use for rendering PDF files
  223. *
  224. * @param string $libraryBaseDir Directory path to the library's base folder
  225. * @return bool Success or failure
  226. */
  227. public static function setPdfRendererPath($libraryBaseDir)
  228. {
  229. if (false === file_exists($libraryBaseDir) || false === is_readable($libraryBaseDir)) {
  230. return false;
  231. }
  232. self::$pdfRendererPath = $libraryBaseDir;
  233. return true;
  234. }
  235. /**
  236. * Get measurement unit
  237. *
  238. * @return string
  239. */
  240. public static function getMeasurementUnit()
  241. {
  242. return self::$measurementUnit;
  243. }
  244. /**
  245. * Set measurement unit
  246. *
  247. * @param string $value
  248. * @return bool
  249. */
  250. public static function setMeasurementUnit($value)
  251. {
  252. $units = array(self::UNIT_TWIP, self::UNIT_CM, self::UNIT_MM, self::UNIT_INCH,
  253. self::UNIT_POINT, self::UNIT_PICA, );
  254. if (!in_array($value, $units)) {
  255. return false;
  256. }
  257. self::$measurementUnit = $value;
  258. return true;
  259. }
  260. /**
  261. * Sets the user defined path to temporary directory.
  262. *
  263. * @since 0.12.0
  264. *
  265. * @param string $tempDir The user defined path to temporary directory
  266. */
  267. public static function setTempDir($tempDir)
  268. {
  269. self::$tempDir = $tempDir;
  270. }
  271. /**
  272. * Returns path to temporary directory.
  273. *
  274. * @since 0.12.0
  275. *
  276. * @return string
  277. */
  278. public static function getTempDir()
  279. {
  280. if (!empty(self::$tempDir)) {
  281. $tempDir = self::$tempDir;
  282. } else {
  283. $tempDir = sys_get_temp_dir();
  284. }
  285. return $tempDir;
  286. }
  287. /**
  288. * @since 0.13.0
  289. *
  290. * @return bool
  291. */
  292. public static function isOutputEscapingEnabled()
  293. {
  294. return self::$outputEscapingEnabled;
  295. }
  296. /**
  297. * @since 0.13.0
  298. *
  299. * @param bool $outputEscapingEnabled
  300. */
  301. public static function setOutputEscapingEnabled($outputEscapingEnabled)
  302. {
  303. self::$outputEscapingEnabled = $outputEscapingEnabled;
  304. }
  305. /**
  306. * Get default font name
  307. *
  308. * @return string
  309. */
  310. public static function getDefaultFontName()
  311. {
  312. return self::$defaultFontName;
  313. }
  314. /**
  315. * Set default font name
  316. *
  317. * @param string $value
  318. * @return bool
  319. */
  320. public static function setDefaultFontName($value)
  321. {
  322. if (is_string($value) && trim($value) !== '') {
  323. self::$defaultFontName = $value;
  324. return true;
  325. }
  326. return false;
  327. }
  328. /**
  329. * Get default font size
  330. *
  331. * @return int
  332. */
  333. public static function getDefaultFontSize()
  334. {
  335. return self::$defaultFontSize;
  336. }
  337. /**
  338. * Set default font size
  339. *
  340. * @param int $value
  341. * @return bool
  342. */
  343. public static function setDefaultFontSize($value)
  344. {
  345. $value = (int) $value;
  346. if ($value > 0) {
  347. self::$defaultFontSize = $value;
  348. return true;
  349. }
  350. return false;
  351. }
  352. /**
  353. * Load setting from phpword.yml or phpword.yml.dist
  354. *
  355. * @param string $filename
  356. * @return array
  357. */
  358. public static function loadConfig($filename = null)
  359. {
  360. // Get config file
  361. $configFile = null;
  362. $configPath = __DIR__ . '/../../';
  363. if ($filename !== null) {
  364. $files = array($filename);
  365. } else {
  366. $files = array("{$configPath}phpword.ini", "{$configPath}phpword.ini.dist");
  367. }
  368. foreach ($files as $file) {
  369. if (file_exists($file)) {
  370. $configFile = realpath($file);
  371. break;
  372. }
  373. }
  374. // Parse config file
  375. $config = array();
  376. if ($configFile !== null) {
  377. $config = @parse_ini_file($configFile);
  378. if ($config === false) {
  379. return $config;
  380. }
  381. }
  382. // Set config value
  383. foreach ($config as $key => $value) {
  384. $method = "set{$key}";
  385. if (method_exists(__CLASS__, $method)) {
  386. self::$method($value);
  387. }
  388. }
  389. return $config;
  390. }
  391. /**
  392. * Get default paper
  393. *
  394. * @return string
  395. */
  396. public static function getDefaultPaper()
  397. {
  398. return self::$defaultPaper;
  399. }
  400. /**
  401. * Set default paper
  402. *
  403. * @param string $value
  404. * @return bool
  405. */
  406. public static function setDefaultPaper($value)
  407. {
  408. if (is_string($value) && trim($value) !== '') {
  409. self::$defaultPaper = $value;
  410. return true;
  411. }
  412. return false;
  413. }
  414. /**
  415. * Return the compatibility option used by the XMLWriter
  416. *
  417. * @deprecated 0.10.0
  418. *
  419. * @codeCoverageIgnore
  420. */
  421. public static function getCompatibility()
  422. {
  423. return self::hasCompatibility();
  424. }
  425. }