| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- Lib::loadClass('Core_AclBase');
- Lib::loadClass('FileStorage');
- class Schema_FileStorageAcl extends Core_AclBase {
- public function __construct() {}
- public function getSourceName() { return 'objects'; }
- public function init($force = false) {}
- public function isInitialized() { return true; }
- public function getName() { return 'File'; }
- public function getRootTableName() { return 'CRM_FILES'; }
- public function getFieldListByIdZasob() { return $this->getRealFieldListByIdZasob(); }
- public function getVirtualFieldListByIdZasob() { return array(); }
- public function getRealFieldListByIdZasob() {
- $cols = array();// FileStorage::getFileById()
- $cols[1] = 'id';
- $cols[2] = 'name';
- $cols[3] = 'size';
- $cols[4] = 'mimeType';
- $cols[5] = 'version';
- $cols[6] = 'content';
- // $cols[] = 'relativePath';
- // $cols[] = 'absolutePath';
- // $cols[] = 'exists';
- return $cols;
- }
- public function getFieldIdByName($fieldName) {
- $fields = $this->getRealFieldListByIdZasob();
- if (empty($fieldName)) return null;
- foreach ($fields as $idField => $vFieldName) {
- if ($vFieldName == $fieldName) return $idField;
- }
- return null;
- }
- public function isDecimalField($fieldName) { return false; }
- public function isGeomField($fieldName) { return false; }
- public function isDateField($fieldName) { return false; }
- public function isDateTimeField($fieldName) { return false; }
- public function isStringField($fieldName) {
- if ('name' == $fieldName) return true;
- if ('mimeType' == $fieldName) return true;
- return false;
- }
- public function isTextField($fieldName) { return false; }
- public function isBinaryField($fieldName) {
- if ('content' == $fieldName) return true;
- return false;
- }
- public function isEnumerationField($fieldName) { return false; }
- public function getFieldType($colName) {
- switch ($colName) {
- case 'id': return array(); break;
- }
- return null;
- }
- public function isAllowed($idZasob, $taskPerm, $record = null) {
- if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
- if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
- return false;
- }
- public function hasFieldPerm($idZasob, $taskPerm) {
- if ('C' == $taskPerm && $idZasob > 1 && $idZasob < 7) return true;
- if ('R' == $taskPerm && $idZasob > 0 && $idZasob < 7) return true;
- return false;
- }
- // TODO: replace legacy functions: isAllowed, hasFieldPerm, getFieldIdByName
- public function canCreateField($fieldName) {
- $fields = $this->getRealFieldListByIdZasob();
- if (!in_array($fieldName, $fields)) return false;
- return true;
- }
- public function canReadField($fieldName) {
- $fields = $this->getRealFieldListByIdZasob();
- if (!in_array($fieldName, $fields)) return false;
- return true;
- }
- public function canReadObjectField($fieldName, $record) {
- return $this->canReadField($fieldName);
- }
- public function canWriteField($fieldName) {
- $fields = $this->getRealFieldListByIdZasob();
- if (!in_array($fieldName, $fields)) return false;
- return true;
- }
- public function canWriteObjectField($fieldName, $record) {
- return $this->canWriteField($fieldName);
- }
- public function getItems($params = array()) {
- $sqlLimit = V::get('limit', 10000, $params);
- $sqlOffset = V::get('limitstart', 0, $params);
- // TODO: parse params:
- // [sortBy] => ID D,test_date A
- // [cols] => Array( [0] => ID
- // [1] => test_date
- // [2] => A_STATUS )
- // [ogc:Filter] => "<ogc:Filter><ogc:PropertyIsEqualTo><ogc:PropertyName>id</ogc:PropertyName><ogc:Literal>35</ogc:Literal></ogc:Filter>"
- $sqlWhereAddOgcFilter = '';
- $ogcFilter = V::get('ogc:Filter', '', $params);
- if (!empty($ogcFilter)) {
- Lib::loadClass('ParseOgcFilter');
- $parser = new ParseOgcFilter();
- $parser->loadOgcFilter($ogcFilter);
- $queryWhereBuilder = $parser->convertToSqlQueryWhereBuilder();
- $usedFields = $queryWhereBuilder->getUsedFields();
- foreach ($usedFields as $fieldName) {
- if (!$this->getFieldIdByName($fieldName)) throw new Exception("Not allowed PropertyName '{$fieldName}'");
- }
- $sqlWhereAddOgcFilter = $queryWhereBuilder->getQueryWhere('t');
- if (!empty($sqlWhereAddOgcFilter)) $sqlWhereAddOgcFilter = " and {$sqlWhereAddOgcFilter}";
- DBG::_('DBG_DS', '>1', "ogc:Filter parser", $parser, __CLASS__, __FUNCTION__, __LINE__);
- DBG::_('DBG_DS', '>1', "ogc:Filter queryWhereBuilder", $queryWhereBuilder, __CLASS__, __FUNCTION__, __LINE__);
- DBG::_('DBG_DS', '>1', "ogc:Filter usedFields", $usedFields, __CLASS__, __FUNCTION__, __LINE__);
- DBG::_('DBG_DS', '>1', "ogc:Filter sqlWhereAddOgcFilter", $sqlWhereAddOgcFilter, __CLASS__, __FUNCTION__, __LINE__);
- }
- $sqlTblName = FileStorage::getTableName();
- $sqlUserLogin = User::getLogin();
- $rows = array_map(function($row) {
- $wfsItem = array();
- $wfsItem['id'] = $row['ID'];
- $wfsItem['name'] = V::get('FILE_LABEL', $row['ID'], $row);
- $wfsItem['size'] = $row['FILE_SIZE'];
- $wfsItem['mimeType'] = $row['FILE_MIME_TYPE'];
- $wfsItem['version'] = $row['FILE_VERSION'];
- {// fetch file content
- $objectFile = FileStorage::getFileById($row['ID']);// TODO: avoid sql in FileStorage::convertFromDBRow($row)
- $wfsItem['content'] = ($objectFile['exists']) ? base64_encode(file_get_contents($objectFile['absolutePath'])) : null;
- }
- return $wfsItem;
- }, DB::getPDO()->fetchAll("
- select t.ID
- , t.FILE_HASH
- , t.FILE_LABEL
- , t.FILE_TYPE
- , t.FILE_MIME_TYPE
- , t.FILE_MTIME
- , t.FILE_SIZE
- , t.FILE_VERSION
- , t.A_STATUS
- , t.A_RECORD_CREATE_DATE
- , t.A_RECORD_CREATE_AUTHOR
- , t.A_RECORD_UPDATE_DATE
- , t.A_RECORD_UPDATE_AUTHOR
- , t.A_ADM_COMPANY
- , t.A_CLASSIFIED
- , INET_NTOA(t.A_USER_IP) as IP
- from `{$sqlTblName}` t
- where t.`A_RECORD_CREATE_AUTHOR` = '{$sqlUserLogin}'
- {$sqlWhereAddOgcFilter}
- order by ID DESC
- limit {$sqlLimit} offset {$sqlOffset}
- "));
- $items = array();
- foreach ($rows as $row) {
- $items[$row['id']] = (object)$row;
- }
- return $items;
- }
- public function addItem($itemTodo) {
- if (is_object($itemTodo)) {
- $itemTodo = (array)$itemTodo;
- }
- if (!is_array($itemTodo)) throw new HttpException('Item is not array', 400);
- if (empty($itemTodo)) {
- DBG::_('DBG_DS', '>2', "Item patch is empty", null, __CLASS__, __FUNCTION__, __LINE__);
- return 0;// nothing to insert
- }
- if (empty($itemTodo['content'])) throw new Exception("Empty file content");
- $fileName = V::get('name', '', $itemTodo);
- $binaryContent = base64_decode($itemTodo['content']);
- return FileStorage::addFile($binaryContent, $fileName);
- }
- public function getGeomFieldType($fieldName) { return null; }
- public function getPrimaryKeyField() { return 'id'; }
- public function getID() { return 0; }
- public function getAttributesFromZasoby() {
- $attributes = array();// fldName => [ 'id_zasob' => int, 'label' => str, 'description' => str ]
- // if ($acl->hasFieldPerm($idZasob, 'W')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_write", "true");
- // if ($acl->hasFieldPerm($idZasob, 'C')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_create", "true");
- // if (!$acl->hasFieldPerm($idZasob, 'R')) $elNode->setAttributeNS($rootWfsNsUri, "{$rootWfsNs}:allow_read", "false");
- return $attributes;
- }
- public function getXsdFieldType($fieldName) {
- switch ($fieldName) {
- case 'id': return 'xsd:integer';
- case 'name': return 'xsd:string';
- case 'size': return 'xsd:integer';
- case 'mimeType': return 'xsd:string';
- case 'version': return 'xsd:integer';
- case 'content': return 'xsd:base64Binary';
- default: throw new HttpException("Error field not exists '{$fieldName}'", 404);
- }
- }
- }
|