FileStorageAcl.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. Lib::loadClass('FileStorage');
  3. class FileStorageAcl {
  4. public function __construct() {
  5. }
  6. public function init($force = false) {}
  7. public function isInitialized() { return true; }
  8. public function getName() { return 'File'; }
  9. public function getRealFieldListByIdZasob($force = false) {
  10. $cols = array();// FileStorage::getFileById()
  11. $cols[1] = 'id';
  12. $cols[2] = 'name';
  13. $cols[3] = 'size';
  14. $cols[4] = 'mimeType';
  15. $cols[5] = 'version';
  16. $cols[6] = 'content';
  17. // $cols[] = 'relativePath';
  18. // $cols[] = 'absolutePath';
  19. // $cols[] = 'exists';
  20. return $cols;
  21. }
  22. public function getFieldIdByName($fieldName) {
  23. $fields = $this->getRealFieldListByIdZasob();
  24. if (empty($fieldName)) return null;
  25. foreach ($fields as $idField => $vFieldName) {
  26. if ($vFieldName == $fieldName) return $idField;
  27. }
  28. return null;
  29. }
  30. public function isIntegerField($fieldName) {
  31. if ('id' == $fieldName) return true;
  32. if ('size' == $fieldName) return true;
  33. if ('version' == $fieldName) return true;
  34. return false;
  35. }
  36. public function isDecimalField($fieldName) { return false; }
  37. public function isGeomField($fldName) { return false; }
  38. public function isDateField($fldName) { return false; }
  39. public function isDateTimeField($fldName) { return false; }
  40. public function isStringField($fieldName) {
  41. if ('name' == $fieldName) return true;
  42. if ('mimeType' == $fieldName) return true;
  43. return false;
  44. }
  45. public function isTextField($fldName) { return false; }
  46. public function isBinaryField($fieldName) {
  47. if ('content' == $fieldName) return true;
  48. return false;
  49. }
  50. public function isEnumerationField($fldName) { return false; }
  51. public function getFieldType($colName) {
  52. switch ($colName) {
  53. case 'id': return array(); break;
  54. }
  55. return null;
  56. }
  57. public function isAllowed($idZasob, $taskPerm, $record = null) {
  58. if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
  59. if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
  60. return false;
  61. }
  62. public function hasFieldPerm($idZasob, $taskPerm) {
  63. if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
  64. if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
  65. return false;
  66. }
  67. public function getItems($params = array()) {
  68. $sqlLimit = V::get('limit', 10000, $params);
  69. $sqlOffset = V::get('limitstart', 0, $params);
  70. // TODO: parse params:
  71. // [sortBy] => ID D,test_date A
  72. // [cols] => Array( [0] => ID
  73. // [1] => test_date
  74. // [2] => A_STATUS )
  75. // [ogc:Filter] => "<ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>id</ogc:PropertyName><ogc:Literal>35</ogc:Literal></ogc:Filter>"
  76. $sqlWhereAddOgcFilter = '';
  77. $ogcFilter = V::get('ogc:Filter', '', $params);
  78. if (!empty($ogcFilter)) {
  79. Lib::loadClass('ParseOgcFilter');
  80. $parser = new ParseOgcFilter();
  81. $parser->loadOgcFilter($ogcFilter);
  82. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  83. $usedFields = $queryWhereBuilder->getUsedFields();
  84. foreach ($usedFields as $fldName) {
  85. if (!$this->getFieldIdByName($fldName)) throw new Exception("Not allowed PropertyName '{$fldName}'");
  86. }
  87. $sqlWhereAddOgcFilter = $queryWhereBuilder->getQueryWhere('t');
  88. if (!empty($sqlWhereAddOgcFilter)) $sqlWhereAddOgcFilter = " and {$sqlWhereAddOgcFilter}";
  89. DBG::_('DBG_DS', '>1', "ogc:Filter parser", $parser, __CLASS__, __FUNCTION__, __LINE__);
  90. DBG::_('DBG_DS', '>1', "ogc:Filter queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
  91. DBG::_('DBG_DS', '>1', "ogc:Filter usedFields", $usedFields, __CLASS__, __FUNCTION__, __LINE__);
  92. DBG::_('DBG_DS', '>1', "ogc:Filter sqlWhereAddOgcFilter", $sqlWhereAddOgcFilter, __CLASS__, __FUNCTION__, __LINE__);
  93. }
  94. $sqlTblName = FileStorage::getTableName();
  95. $sqlUserLogin = User::getLogin();
  96. $rows = array_map(function($row) {
  97. $wfsItem = array();
  98. $wfsItem['id'] = $row['ID'];
  99. $wfsItem['name'] = V::get('FILE_LABEL', $row['ID'], $row);
  100. $wfsItem['size'] = $row['FILE_SIZE'];
  101. $wfsItem['mimeType'] = $row['FILE_MIME_TYPE'];
  102. $wfsItem['version'] = $row['FILE_VERSION'];
  103. {// fetch file content
  104. $objectFile = FileStorage::getFileById($row['ID']);// TODO: avoid sql in FileStorage::convertFromDBRow($row)
  105. $wfsItem['content'] = ($objectFile['exists']) ? base64_encode(file_get_contents($objectFile['absolutePath'])) : null;
  106. }
  107. return $wfsItem;
  108. }, DB::getPDO()->fetchAll("
  109. select t.ID
  110. , t.FILE_HASH
  111. , t.FILE_LABEL
  112. , t.FILE_TYPE
  113. , t.FILE_MIME_TYPE
  114. , t.FILE_MTIME
  115. , t.FILE_SIZE
  116. , t.FILE_VERSION
  117. , t.A_STATUS
  118. , t.A_RECORD_CREATE_DATE
  119. , t.A_RECORD_CREATE_AUTHOR
  120. , t.A_RECORD_UPDATE_DATE
  121. , t.A_RECORD_UPDATE_AUTHOR
  122. , t.A_ADM_COMPANY
  123. , t.A_CLASSIFIED
  124. , INET_NTOA(t.A_USER_IP) as IP
  125. from `{$sqlTblName}` t
  126. where t.`A_RECORD_CREATE_AUTHOR` = '{$sqlUserLogin}'
  127. {$sqlWhereAddOgcFilter}
  128. order by ID DESC
  129. limit {$sqlLimit} offset {$sqlOffset}
  130. "));
  131. $items = array();
  132. foreach ($rows as $row) {
  133. $items[$row['id']] = (object)$row;
  134. }
  135. return $items;
  136. }
  137. public function addItem($itemTodo) {
  138. if (is_object($itemTodo)) {
  139. $itemTodo = (array)$itemTodo;
  140. }
  141. if (!is_array($itemTodo)) throw new HttpException('Item is not array', 400);
  142. if (empty($itemTodo)) {
  143. DBG::_('DBG_DS', '>2', "Item patch is empty", null, __CLASS__, __FUNCTION__, __LINE__);
  144. return 0;// nothing to insert
  145. }
  146. if (empty($itemTodo['content'])) throw new Exception("Empty file content");
  147. $fileName = V::get('name', '', $itemTodo);
  148. $binaryContent = base64_decode($itemTodo['content']);
  149. return FileStorage::addFile($binaryContent, $fileName);
  150. }
  151. public function getGeomFieldType() { return null; }
  152. public function getPrimaryKeyField() { return 'id'; }
  153. public function getID() { return 0; }
  154. public function getAttributesFromZasoby() {
  155. $attributes = array();// fldName => [ 'id_zasob' => int, 'label' => str, 'description' => str ]
  156. // if ($acl->hasFieldPerm($idZasob, 'W')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
  157. // if ($acl->hasFieldPerm($idZasob, 'C')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_create", "true");
  158. // if (!$acl->hasFieldPerm($idZasob, 'R')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
  159. return $attributes;
  160. }
  161. }