FileStorageAcl.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 isIntegerField($fieldName) {
  23. if ('id' == $fieldName) return true;
  24. if ('size' == $fieldName) return true;
  25. if ('version' == $fieldName) return true;
  26. return false;
  27. }
  28. public function isDecimalField($fieldName) { return false; }
  29. public function isGeomField($fldName) { return false; }
  30. public function isDateField($fldName) { return false; }
  31. public function isDateTimeField($fldName) { return false; }
  32. public function isStringField($fieldName) {
  33. if ('name' == $fieldName) return true;
  34. if ('mimeType' == $fieldName) return true;
  35. return false;
  36. }
  37. public function isTextField($fldName) { return false; }
  38. public function isBinaryField($fieldName) {
  39. if ('content' == $fieldName) return true;
  40. return false;
  41. }
  42. public function isEnumerationField($fldName) { return false; }
  43. public function getFieldType($colName) {
  44. switch ($colName) {
  45. case 'id': return array(); break;
  46. }
  47. return null;
  48. }
  49. public function isAllowed($idZasob, $taskPerm, $record = null) {
  50. if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
  51. if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
  52. return false;
  53. }
  54. public function hasFieldPerm($idZasob, $taskPerm) {
  55. if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
  56. if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
  57. return false;
  58. }
  59. public function getItems($params = array()) {
  60. $sqlLimit = V::get('limit', 10000, $params);
  61. $sqlOffset = V::get('limitstart', 0, $params);
  62. // TODO: parse params:
  63. // [sortBy] => ID D,test_date A
  64. // [cols] => Array( [0] => ID
  65. // [1] => test_date
  66. // [2] => A_STATUS )
  67. $sqlTblName = FileStorage::getTableName();
  68. $sqlUserLogin = User::getLogin();
  69. $rows = array_map(function($row) {
  70. $wfsItem = array();
  71. $wfsItem['id'] = $row['ID'];
  72. $wfsItem['name'] = V::get('FILE_LABEL', $row['ID'], $row);
  73. $wfsItem['size'] = $row['FILE_SIZE'];
  74. $wfsItem['mimeType'] = $row['FILE_MIME_TYPE'];
  75. $wfsItem['version'] = $row['FILE_VERSION'];
  76. {// fetch file content
  77. $objectFile = FileStorage::getFileById($row['ID']);// TODO: avoid sql in FileStorage::convertFromDBRow($row)
  78. $wfsItem['content'] = ($objectFile['exists']) ? base64_encode(file_get_contents($objectFile['absolutePath'])) : null;
  79. }
  80. return $wfsItem;
  81. }, DB::getPDO()->fetchAll("
  82. select t.ID
  83. , t.FILE_HASH
  84. , t.FILE_LABEL
  85. , t.FILE_TYPE
  86. , t.FILE_MIME_TYPE
  87. , t.FILE_MTIME
  88. , t.FILE_SIZE
  89. , t.FILE_VERSION
  90. , t.A_STATUS
  91. , t.A_RECORD_CREATE_DATE
  92. , t.A_RECORD_CREATE_AUTHOR
  93. , t.A_RECORD_UPDATE_DATE
  94. , t.A_RECORD_UPDATE_AUTHOR
  95. , t.A_ADM_COMPANY
  96. , t.A_CLASSIFIED
  97. , INET_NTOA(t.A_USER_IP) as IP
  98. from `{$sqlTblName}` t
  99. where t.`A_RECORD_CREATE_AUTHOR` = '{$sqlUserLogin}'
  100. order by ID DESC
  101. limit {$sqlLimit} offset {$sqlOffset}
  102. "));
  103. $items = array();
  104. foreach ($rows as $row) {
  105. $items[$row['id']] = (object)$row;
  106. }
  107. return $items;
  108. }
  109. public function addItem($itemTodo) {
  110. if (is_object($itemTodo)) {
  111. $itemTodo = (array)$itemTodo;
  112. }
  113. if (!is_array($itemTodo)) throw new HttpException('Item is not array', 400);
  114. if (empty($itemTodo)) {
  115. DBG::_('DBG_DS', '>2', "Item patch is empty", null, __CLASS__, __FUNCTION__, __LINE__);
  116. return 0;// nothing to insert
  117. }
  118. if (empty($itemTodo['content'])) throw new Exception("Empty file content");
  119. $fileName = V::get('name', '', $itemTodo);
  120. $binaryContent = base64_decode($itemTodo['content']);
  121. return FileStorage::addFile($binaryContent, $fileName);
  122. }
  123. public function getGeomFieldType() { return null; }
  124. public function getPrimaryKeyField() { return 'id'; }
  125. public function getID() { return 0; }
  126. public function getAttributesFromZasoby() {
  127. $attributes = array();// fldName => [ 'id_zasob' => int, 'label' => str, 'description' => str ]
  128. // if ($acl->hasFieldPerm($idZasob, 'W')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
  129. // if ($acl->hasFieldPerm($idZasob, 'C')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_create", "true");
  130. // if (!$acl->hasFieldPerm($idZasob, 'R')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
  131. return $attributes;
  132. }
  133. }