PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Search/Lucene/Document/Xlsx.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 266 lines | 128 code | 37 blank | 101 comment | 34 complexity | cae7aa77e2e49c47ecf8a89f169c629f MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Search_Lucene
  17. * @subpackage Document
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Search_Lucene_Document_OpenXml */
  23. #require_once 'Zend/Search/Lucene/Document/OpenXml.php';
  24. /** Zend_Xml_Security */
  25. #require_once 'Zend/Xml/Security.php';
  26. /**
  27. * Xlsx document.
  28. *
  29. * @category Zend
  30. * @package Zend_Search_Lucene
  31. * @subpackage Document
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Search_Lucene_Document_Xlsx extends Zend_Search_Lucene_Document_OpenXml
  36. {
  37. /**
  38. * Xml Schema - SpreadsheetML
  39. *
  40. * @var string
  41. */
  42. const SCHEMA_SPREADSHEETML = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
  43. /**
  44. * Xml Schema - DrawingML
  45. *
  46. * @var string
  47. */
  48. const SCHEMA_DRAWINGML = 'http://schemas.openxmlformats.org/drawingml/2006/main';
  49. /**
  50. * Xml Schema - Shared Strings
  51. *
  52. * @var string
  53. */
  54. const SCHEMA_SHAREDSTRINGS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings';
  55. /**
  56. * Xml Schema - Worksheet relation
  57. *
  58. * @var string
  59. */
  60. const SCHEMA_WORKSHEETRELATION = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet';
  61. /**
  62. * Xml Schema - Slide notes relation
  63. *
  64. * @var string
  65. */
  66. const SCHEMA_SLIDENOTESRELATION = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide';
  67. /**
  68. * Object constructor
  69. *
  70. * @param string $fileName
  71. * @param boolean $storeContent
  72. * @throws Zend_Search_Lucene_Exception
  73. */
  74. private function __construct($fileName, $storeContent)
  75. {
  76. if (!class_exists('ZipArchive', false)) {
  77. #require_once 'Zend/Search/Lucene/Exception.php';
  78. throw new Zend_Search_Lucene_Exception('MS Office documents processing functionality requires Zip extension to be loaded');
  79. }
  80. // Document data holders
  81. $sharedStrings = array();
  82. $worksheets = array();
  83. $documentBody = array();
  84. $coreProperties = array();
  85. // Open OpenXML package
  86. $package = new ZipArchive();
  87. $package->open($fileName);
  88. // Read relations and search for officeDocument
  89. $relationsXml = $package->getFromName('_rels/.rels');
  90. if ($relationsXml === false) {
  91. #require_once 'Zend/Search/Lucene/Exception.php';
  92. throw new Zend_Search_Lucene_Exception('Invalid archive or corrupted .xlsx file.');
  93. }
  94. $relations = Zend_Xml_Security::scan($relationsXml);
  95. foreach ($relations->Relationship as $rel) {
  96. if ($rel["Type"] == Zend_Search_Lucene_Document_OpenXml::SCHEMA_OFFICEDOCUMENT) {
  97. // Found office document! Read relations for workbook...
  98. $workbookRelations = Zend_Xml_Security::scan($package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/_rels/" . basename($rel["Target"]) . ".rels")) );
  99. $workbookRelations->registerXPathNamespace("rel", Zend_Search_Lucene_Document_OpenXml::SCHEMA_RELATIONSHIP);
  100. // Read shared strings
  101. $sharedStringsPath = $workbookRelations->xpath("rel:Relationship[@Type='" . Zend_Search_Lucene_Document_Xlsx::SCHEMA_SHAREDSTRINGS . "']");
  102. $sharedStringsPath = (string)$sharedStringsPath[0]['Target'];
  103. $xmlStrings = Zend_Xml_Security::scan($package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . $sharedStringsPath)) );
  104. if (isset($xmlStrings) && isset($xmlStrings->si)) {
  105. foreach ($xmlStrings->si as $val) {
  106. if (isset($val->t)) {
  107. $sharedStrings[] = (string)$val->t;
  108. } elseif (isset($val->r)) {
  109. $sharedStrings[] = $this->_parseRichText($val);
  110. }
  111. }
  112. }
  113. // Loop relations for workbook and extract worksheets...
  114. foreach ($workbookRelations->Relationship as $workbookRelation) {
  115. if ($workbookRelation["Type"] == Zend_Search_Lucene_Document_Xlsx::SCHEMA_WORKSHEETRELATION) {
  116. $worksheets[ str_replace( 'rId', '', (string)$workbookRelation["Id"]) ] = Zend_Xml_Security::scan(
  117. $package->getFromName( $this->absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($workbookRelation["Target"]) . "/" . basename($workbookRelation["Target"])) )
  118. );
  119. }
  120. }
  121. break;
  122. }
  123. }
  124. // Sort worksheets
  125. ksort($worksheets);
  126. // Extract contents from worksheets
  127. foreach ($worksheets as $sheetKey => $worksheet) {
  128. foreach ($worksheet->sheetData->row as $row) {
  129. foreach ($row->c as $c) {
  130. // Determine data type
  131. $dataType = (string)$c["t"];
  132. switch ($dataType) {
  133. case "s":
  134. // Value is a shared string
  135. if ((string)$c->v != '') {
  136. $value = $sharedStrings[intval($c->v)];
  137. } else {
  138. $value = '';
  139. }
  140. break;
  141. case "b":
  142. // Value is boolean
  143. $value = (string)$c->v;
  144. if ($value == '0') {
  145. $value = false;
  146. } else if ($value == '1') {
  147. $value = true;
  148. } else {
  149. $value = (bool)$c->v;
  150. }
  151. break;
  152. case "inlineStr":
  153. // Value is rich text inline
  154. $value = $this->_parseRichText($c->is);
  155. break;
  156. case "e":
  157. // Value is an error message
  158. if ((string)$c->v != '') {
  159. $value = (string)$c->v;
  160. } else {
  161. $value = '';
  162. }
  163. break;
  164. default:
  165. // Value is a string
  166. $value = (string)$c->v;
  167. // Check for numeric values
  168. if (is_numeric($value) && $dataType != 's') {
  169. if ($value == (int)$value) $value = (int)$value;
  170. elseif ($value == (float)$value) $value = (float)$value;
  171. elseif ($value == (double)$value) $value = (double)$value;
  172. }
  173. }
  174. $documentBody[] = $value;
  175. }
  176. }
  177. }
  178. // Read core properties
  179. $coreProperties = $this->extractMetaData($package);
  180. // Close file
  181. $package->close();
  182. // Store filename
  183. $this->addField(Zend_Search_Lucene_Field::Text('filename', $fileName, 'UTF-8'));
  184. // Store contents
  185. if ($storeContent) {
  186. $this->addField(Zend_Search_Lucene_Field::Text('body', implode(' ', $documentBody), 'UTF-8'));
  187. } else {
  188. $this->addField(Zend_Search_Lucene_Field::UnStored('body', implode(' ', $documentBody), 'UTF-8'));
  189. }
  190. // Store meta data properties
  191. foreach ($coreProperties as $key => $value)
  192. {
  193. $this->addField(Zend_Search_Lucene_Field::Text($key, $value, 'UTF-8'));
  194. }
  195. // Store title (if not present in meta data)
  196. if (!isset($coreProperties['title']))
  197. {
  198. $this->addField(Zend_Search_Lucene_Field::Text('title', $fileName, 'UTF-8'));
  199. }
  200. }
  201. /**
  202. * Parse rich text XML
  203. *
  204. * @param SimpleXMLElement $is
  205. * @return string
  206. */
  207. private function _parseRichText($is = null) {
  208. $value = array();
  209. if (isset($is->t)) {
  210. $value[] = (string)$is->t;
  211. } else {
  212. foreach ($is->r as $run) {
  213. $value[] = (string)$run->t;
  214. }
  215. }
  216. return implode('', $value);
  217. }
  218. /**
  219. * Load Xlsx document from a file
  220. *
  221. * @param string $fileName
  222. * @param boolean $storeContent
  223. * @return Zend_Search_Lucene_Document_Xlsx
  224. */
  225. public static function loadXlsxFile($fileName, $storeContent = false)
  226. {
  227. return new Zend_Search_Lucene_Document_Xlsx($fileName, $storeContent);
  228. }
  229. }