FileStorageAcl.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. Lib::loadClass('Core_AclBase');
  3. Lib::loadClass('FileStorage');
  4. class Schema_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. // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
  64. public function canCreateField($fieldName) {
  65. $fields = $this->getRealFieldListByIdZasob();
  66. if (!in_array($fieldName, $fields)) return false;
  67. return true;
  68. }
  69. public function canReadField($fieldName) {
  70. $fields = $this->getRealFieldListByIdZasob();
  71. if (!in_array($fieldName, $fields)) return false;
  72. return true;
  73. }
  74. public function canReadObjectField($fieldName, $record) {
  75. return $this->canReadField($fieldName);
  76. }
  77. public function canWriteField($fieldName) {
  78. $fields = $this->getRealFieldListByIdZasob();
  79. if (!in_array($fieldName, $fields)) return false;
  80. return true;
  81. }
  82. public function canWriteObjectField($fieldName, $record) {
  83. return $this->canWriteField($fieldName);
  84. }
  85. public function getItems($params = array()) {
  86. $sqlLimit = V::get('limit', 10000, $params);
  87. $sqlOffset = V::get('limitstart', 0, $params);
  88. // TODO: parse params:
  89. // [sortBy] => ID D,test_date A
  90. // [cols] => Array( [0] => ID
  91. // [1] => test_date
  92. // [2] => A_STATUS )
  93. // [ogc:Filter] => "<ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>id</ogc:PropertyName><ogc:Literal>35</ogc:Literal></ogc:Filter>"
  94. $sqlWhereAddOgcFilter = '';
  95. $ogcFilter = V::get('ogc:Filter', '', $params);
  96. if (!empty($ogcFilter)) {
  97. Lib::loadClass('ParseOgcFilter');
  98. $parser = new ParseOgcFilter();
  99. $parser->loadOgcFilter($ogcFilter);
  100. $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
  101. $usedFields = $queryWhereBuilder->getUsedFields();
  102. foreach ($usedFields as $fieldName) {
  103. if (!$this->getFieldIdByName($fieldName)) throw new Exception("Not allowed PropertyName '{$fieldName}'");
  104. }
  105. $sqlWhereAddOgcFilter = $queryWhereBuilder->getQueryWhere('t');
  106. if (!empty($sqlWhereAddOgcFilter)) $sqlWhereAddOgcFilter = " and {$sqlWhereAddOgcFilter}";
  107. DBG::_('DBG_DS', '>1', "ogc:Filter parser", $parser, __CLASS__, __FUNCTION__, __LINE__);
  108. DBG::_('DBG_DS', '>1', "ogc:Filter queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
  109. DBG::_('DBG_DS', '>1', "ogc:Filter usedFields", $usedFields, __CLASS__, __FUNCTION__, __LINE__);
  110. DBG::_('DBG_DS', '>1', "ogc:Filter sqlWhereAddOgcFilter", $sqlWhereAddOgcFilter, __CLASS__, __FUNCTION__, __LINE__);
  111. }
  112. $sqlTblName = FileStorage::getTableName();
  113. $sqlUserLogin = User::getLogin();
  114. $rows = array_map(function($row) {
  115. $wfsItem = array();
  116. $wfsItem['id'] = $row['ID'];
  117. $wfsItem['name'] = V::get('FILE_LABEL', $row['ID'], $row);
  118. $wfsItem['size'] = $row['FILE_SIZE'];
  119. $wfsItem['mimeType'] = $row['FILE_MIME_TYPE'];
  120. $wfsItem['version'] = $row['FILE_VERSION'];
  121. {// fetch file content
  122. $objectFile = FileStorage::getFileById($row['ID']);// TODO: avoid sql in FileStorage::convertFromDBRow($row)
  123. $wfsItem['content'] = ($objectFile['exists']) ? base64_encode(file_get_contents($objectFile['absolutePath'])) : null;
  124. }
  125. return $wfsItem;
  126. }, DB::getPDO()->fetchAll("
  127. select t.ID
  128. , t.FILE_HASH
  129. , t.FILE_LABEL
  130. , t.FILE_TYPE
  131. , t.FILE_MIME_TYPE
  132. , t.FILE_MTIME
  133. , t.FILE_SIZE
  134. , t.FILE_VERSION
  135. , t.A_STATUS
  136. , t.A_RECORD_CREATE_DATE
  137. , t.A_RECORD_CREATE_AUTHOR
  138. , t.A_RECORD_UPDATE_DATE
  139. , t.A_RECORD_UPDATE_AUTHOR
  140. , t.A_ADM_COMPANY
  141. , t.A_CLASSIFIED
  142. , INET_NTOA(t.A_USER_IP) as IP
  143. from `{$sqlTblName}` t
  144. where t.`A_RECORD_CREATE_AUTHOR` = '{$sqlUserLogin}'
  145. {$sqlWhereAddOgcFilter}
  146. order by ID DESC
  147. limit {$sqlLimit} offset {$sqlOffset}
  148. "));
  149. $items = array();
  150. foreach ($rows as $row) {
  151. $items[$row['id']] = (object)$row;
  152. }
  153. return $items;
  154. }
  155. public function addItem($itemTodo) {
  156. if (is_object($itemTodo)) {
  157. $itemTodo = (array)$itemTodo;
  158. }
  159. if (!is_array($itemTodo)) throw new HttpException('Item is not array', 400);
  160. if (empty($itemTodo)) {
  161. DBG::_('DBG_DS', '>2', "Item patch is empty", null, __CLASS__, __FUNCTION__, __LINE__);
  162. return 0;// nothing to insert
  163. }
  164. if (empty($itemTodo['content'])) throw new Exception("Empty file content");
  165. $fileName = V::get('name', '', $itemTodo);
  166. $binaryContent = base64_decode($itemTodo['content']);
  167. return FileStorage::addFile($binaryContent, $fileName);
  168. }
  169. public function getGeomFieldType($fieldName) { return null; }
  170. public function getPrimaryKeyField() { return 'id'; }
  171. public function getID() { return 0; }
  172. public function getAttributesFromZasoby() {
  173. $attributes = array();// fldName => [ 'id_zasob' => int, 'label' => str, 'description' => str ]
  174. // if ($acl->hasFieldPerm($idZasob, 'W')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
  175. // if ($acl->hasFieldPerm($idZasob, 'C')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_create", "true");
  176. // if (!$acl->hasFieldPerm($idZasob, 'R')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
  177. return $attributes;
  178. }
  179. public function getXsdFieldType($fieldName) {
  180. switch ($fieldName) {
  181. case 'id': return 'xsd:integer';
  182. case 'name': return 'xsd:string';
  183. case 'size': return 'xsd:integer';
  184. case 'mimeType': return 'xsd:string';
  185. case 'version': return 'xsd:integer';
  186. case 'content': return 'xsd:base64Binary';
  187. default: throw new HttpException("Error field not exists '{$fieldName}'", 404);
  188. }
  189. }
  190. }