FileStorageAcl.php 6.6 KB

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