FileStorageAcl.php 7.0 KB

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