FileStorageAcl.php 7.8 KB

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