/**
  * @param string $table
  * @param array $recordData
  * @return bool
  */
 public function copyFiles($tableName, &$recordData)
 {
     $schemaArray = TableSchema::getSchemaArray($tableName);
     foreach ($schemaArray as $column) {
         $colName = $column['id'];
         // Ignore absent values & non-arrays
         if (!isset($recordData[$colName]) || !is_array($recordData[$colName])) {
             continue;
         }
         $foreignRow = $recordData[$colName];
         $colUiType = $column['ui'];
         // $isManyToOne = (array_key_exists('relationship', $column) &&
         //     $column['relationship']['type'] == 'MANYTOONE'
         // );
         // $isManyToMany = (array_key_exists('relationship', $column) &&
         //     $column['relationship']['type'] == 'MANYTOMANY'
         // );
         $foreignTableName = $column['relationship']['table_related'];
         // @todo: rewrite this
         if ($foreignTableName === 'directus_files') {
             // Update/Add foreign record
             $Files = new \Directus\Files\Files();
             if (count(array_filter($foreignRow, 'is_array')) == count($foreignRow)) {
                 $index = 0;
                 foreach ($foreignRow as $row) {
                     if (!isset($row['data'][$this->primaryKeyFieldName]) && isset($row['data']['data'])) {
                         if (array_key_exists('type', $row['data']) && strpos($row['data']['type'], 'embed/') === 0) {
                             $recordData[$colName][$index]['data'] = $Files->saveEmbedData($row['data']);
                         } else {
                             $recordData[$colName][$index]['data'] = $Files->saveData($row['data']['data'], $row['data']['name']);
                         }
                     }
                     unset($recordData[$colName][$index]['data']['data']);
                     $index++;
                 }
             } else {
                 if (!isset($foreignRow[$this->primaryKeyFieldName]) && isset($foreignRow['data'])) {
                     if (array_key_exists('type', $foreignRow) && strpos($foreignRow['type'], 'embed/') === 0) {
                         $recordData[$colName] = $Files->saveEmbedData($foreignRow);
                     } else {
                         $recordData[$colName] = $Files->saveData($foreignRow['data'], $foreignRow['name']);
                     }
                 }
                 unset($recordData[$colName]['data']);
             }
         }
     }
     return true;
 }
Esempio n. 2
0
})->via('GET', 'PUT')->name('table_meta');
/**
 * UPLOAD COLLECTION
 */
$app->post("/{$v}/upload/?", function () use($params, $requestPayload, $app, $acl, $ZendDb) {
    // $Transfer = new Files\Transfer();
    // $Storage = new Files\Storage\Storage();
    $Files = new Directus\Files\Files();
    $result = [];
    foreach ($_FILES as $file) {
        $result[] = $Files->upload($file);
    }
    JsonView::render($result);
});
$app->post("/{$v}/upload/link/?", function () use($params, $requestPayload, $app, $acl, $ZendDb) {
    $Files = new \Directus\Files\Files();
    $result = array();
    if (isset($_POST['link'])) {
        $fileData = array('caption' => '', 'tags' => '', 'location' => '');
        $fileData = array_merge($fileData, $Files->getLink($_POST['link']));
        $result[] = array('type' => $fileData['type'], 'name' => $fileData['name'], 'title' => $fileData['title'], 'tags' => $fileData['tags'], 'caption' => $fileData['caption'], 'location' => $fileData['location'], 'charset' => $fileData['charset'], 'size' => $fileData['size'], 'width' => $fileData['width'], 'height' => $fileData['height'], 'url' => isset($fileData['url']) ? $fileData['url'] : '', 'data' => isset($fileData['data']) ? $fileData['data'] : null);
    }
    JsonView::render($result);
});
$app->get("/{$v}/messages/rows/?", function () use($params, $requestPayload, $app, $acl, $ZendDb) {
    $currentUser = Auth::getUserInfo();
    if (isset($_GET['max_id'])) {
        $messagesRecipientsTableGateway = new DirectusMessagesRecipientsTableGateway($acl, $ZendDb);
        $ids = $messagesRecipientsTableGateway->getMessagesNewerThan($_GET['max_id'], $currentUser['id']);
        if (sizeof($ids) > 0) {
            $messagesTableGateway = new DirectusMessagesTableGateway($acl, $ZendDb);
Esempio n. 3
0
 /**
  * Get Hook Emitter
  *
  * @return Emitter
  */
 private static function hookEmitter()
 {
     $emitter = new Emitter();
     $emitter->addAction('application.error', function ($e) {
         $log = Bootstrap::get('log');
         $log->error($e);
     });
     $emitter->addAction('table.insert.directus_groups', function ($data) {
         $acl = Bootstrap::get('acl');
         $zendDb = Bootstrap::get('zendDb');
         $privilegesTable = new DirectusPrivilegesTableGateway($acl, $zendDb);
         $privilegesTable->insertPrivilege(['group_id' => $data['id'], 'allow_view' => 1, 'allow_add' => 0, 'allow_edit' => 1, 'allow_delete' => 0, 'allow_alter' => 0, 'table_name' => 'directus_users', 'read_field_blacklist' => 'token', 'write_field_blacklist' => 'group,token']);
     });
     $emitter->addFilter('table.insert:before', function ($tableName, $data) {
         if ($tableName == 'directus_files') {
             unset($data['data']);
             $data['user'] = AuthProvider::getUserInfo('id');
         }
         return $data;
     });
     // Add file url and thumb url
     $emitter->addFilter('table.select', function ($result, $selectState) {
         if ($selectState['table'] == 'directus_files') {
             $fileRows = $result->toArray();
             $files = new \Directus\Files\Files();
             foreach ($fileRows as &$row) {
                 $config = Bootstrap::get('config');
                 $fileURL = $config['filesystem']['root_url'];
                 $thumbnailURL = $config['filesystem']['root_thumb_url'];
                 $thumbnailFilenameParts = explode('.', $row['name']);
                 $thumbnailExtension = array_pop($thumbnailFilenameParts);
                 $row['url'] = $fileURL . '/' . $row['name'];
                 if (in_array($thumbnailExtension, ['tif', 'tiff', 'psd', 'pdf'])) {
                     $thumbnailExtension = 'jpg';
                 }
                 $thumbnailFilename = $row['id'] . '.' . $thumbnailExtension;
                 $row['thumbnail_url'] = $thumbnailURL . '/' . $thumbnailFilename;
                 // filename-ext-100-100-true.jpg
                 // @TODO: This should be another hook listener
                 $row['thumbnail_url'] = null;
                 $filename = implode('.', $thumbnailFilenameParts);
                 if ($row['type'] == 'embed/vimeo') {
                     $oldThumbnailFilename = $row['name'] . '-vimeo-220-124-true.jpg';
                 } else {
                     $oldThumbnailFilename = $filename . '-' . $thumbnailExtension . '-160-160-true.jpg';
                 }
                 // 314551321-vimeo-220-124-true.jpg
                 // hotfix: there's not thumbnail for this file
                 if ($files->exists('thumbs/' . $oldThumbnailFilename)) {
                     $row['thumbnail_url'] = $thumbnailURL . '/' . $oldThumbnailFilename;
                 }
                 if ($files->exists('thumbs/' . $thumbnailFilename)) {
                     $row['thumbnail_url'] = $thumbnailURL . '/' . $thumbnailFilename;
                 }
                 $embedManager = Bootstrap::get('embedManager');
                 $provider = $embedManager->getByType($row['type']);
                 $row['html'] = null;
                 if ($provider) {
                     $row['html'] = $provider->getCode($row);
                 }
             }
             $filesArrayObject = new \ArrayObject($fileRows);
             $result->initialize($filesArrayObject->getIterator());
         }
         return $result;
     });
     return $emitter;
 }
Esempio n. 4
0
})->via('GET', 'PUT', 'DELETE')->name('table_meta');
/**
 * UPLOAD COLLECTION
 */
$app->post("/{$v}/upload/?", function () use($params, $requestPayload, $app, $acl, $ZendDb) {
    // $Transfer = new Files\Transfer();
    // $Storage = new Files\Storage\Storage();
    $Files = new Directus\Files\Files();
    $result = [];
    foreach ($_FILES as $file) {
        $result[] = $Files->upload($file);
    }
    JsonView::render($result);
});
$app->post("/{$v}/upload/link/?", function () use($params, $requestPayload, $app, $acl, $ZendDb) {
    $Files = new \Directus\Files\Files();
    $result = ['message' => __t('invalid_unsupported_url'), 'success' => false];
    $app->response->setStatus(400);
    if (isset($_POST['link']) && filter_var($_POST['link'], FILTER_VALIDATE_URL)) {
        $fileData = ['caption' => '', 'tags' => '', 'location' => ''];
        $linkInfo = $Files->getLink($_POST['link']);
        if ($linkInfo) {
            $currentUser = Auth::getUserInfo();
            $app->response->setStatus(200);
            $fileData = array_merge($fileData, $linkInfo);
            $result = [];
            $result[] = ['type' => $fileData['type'], 'name' => $fileData['name'], 'title' => $fileData['title'], 'tags' => $fileData['tags'], 'caption' => $fileData['caption'], 'location' => $fileData['location'], 'charset' => $fileData['charset'], 'size' => $fileData['size'], 'width' => $fileData['width'], 'height' => $fileData['height'], 'html' => isset($fileData['html']) ? $fileData['html'] : null, 'embed_id' => isset($fileData['embed_id']) ? $fileData['embed_id'] : '', 'data' => isset($fileData['data']) ? $fileData['data'] : null, 'user' => $currentUser['id']];
        }
    }
    JsonView::render($result);
});
Esempio n. 5
0
 public function addOrUpdateRecordByArray(array $recordData, $tableName = null)
 {
     foreach ($recordData as $columnName => $columnValue) {
         if (is_array($columnValue)) {
             $table = is_null($tableName) ? $this->table : $tableName;
             throw new SuppliedArrayAsColumnValue("Attempting to write an array as the value for column `{$table}`.`{$columnName}`.");
         }
     }
     $tableName = is_null($tableName) ? $this->table : $tableName;
     $TableGateway = new self($this->acl, $tableName, $this->adapter);
     $rowExists = isset($recordData[$TableGateway->primaryKeyFieldName]);
     if ($rowExists) {
         $Update = new Update($tableName);
         $Update->set($recordData);
         $Update->where(array($TableGateway->primaryKeyFieldName => $recordData[$TableGateway->primaryKeyFieldName]));
         $TableGateway->updateWith($Update);
         Hooks::runHook('postUpdate', array($TableGateway, $recordData, $this->adapter, $this->acl));
     } else {
         $d = $recordData;
         unset($d['data']);
         $TableGateway->insert($d);
         $recordData[$TableGateway->primaryKeyFieldName] = $TableGateway->getLastInsertValue();
         if ($tableName == "directus_files") {
             $Files = new \Directus\Files\Files();
             $ext = pathinfo($recordData['name'], PATHINFO_EXTENSION);
             $thumbnailPath = 'thumbs/THUMB_' . $recordData['name'];
             if ($Files->exists($thumbnailPath)) {
                 $Files->rename($thumbnailPath, 'thumbs/' . $recordData[$this->primaryKeyFieldName] . '.' . $ext);
             }
             $updateArray = array();
             if ($Files->getSettings('file_naming') == 'file_id') {
                 $Files->rename($recordData['name'], str_pad($recordData[$this->primaryKeyFieldName], 11, "0", STR_PAD_LEFT) . '.' . $ext);
                 $updateArray['name'] = str_pad($recordData[$this->primaryKeyFieldName], 11, "0", STR_PAD_LEFT) . '.' . $ext;
                 $recordData['name'] = $updateArray['name'];
             }
             if (!empty($updateArray)) {
                 $Update = new Update($tableName);
                 $Update->set($updateArray);
                 $Update->where(array($TableGateway->primaryKeyFieldName => $recordData[$TableGateway->primaryKeyFieldName]));
                 $TableGateway->updateWith($Update);
             }
         }
         Hooks::runHook('postInsert', array($TableGateway, $recordData, $this->adapter, $this->acl));
     }
     $columns = TableSchema::getAllNonAliasTableColumnNames($tableName);
     $recordData = $TableGateway->fetchAll(function ($select) use($recordData, $columns, $TableGateway) {
         $select->columns($columns)->limit(1);
         $select->where->equalTo($TableGateway->primaryKeyFieldName, $recordData[$TableGateway->primaryKeyFieldName]);
     })->current();
     return $recordData;
 }
Esempio n. 6
0
 public function addOrUpdateRecordByArray(array $recordData, $tableName = null)
 {
     $tableName = is_null($tableName) ? $this->table : $tableName;
     foreach ($recordData as $columnName => $columnValue) {
         if (is_array($columnValue)) {
             // $table = is_null($tableName) ? $this->table : $tableName;
             throw new SuppliedArrayAsColumnValue('Attempting to write an array as the value for column `' . $tableName . '`.`' . $columnName . '.');
         }
     }
     $columns = TableSchema::getAllNonAliasTableColumns($tableName);
     $recordData = SchemaManager::parseRecordValuesByType($recordData, $columns);
     $TableGateway = $this->makeTable($tableName);
     $rowExists = isset($recordData[$TableGateway->primaryKeyFieldName]);
     if ($rowExists) {
         $Update = new Update($tableName);
         $Update->set($recordData);
         $Update->where([$TableGateway->primaryKeyFieldName => $recordData[$TableGateway->primaryKeyFieldName]]);
         $TableGateway->updateWith($Update);
         $this->runHook('postUpdate', [$TableGateway, $recordData, $this->adapter, null]);
     } else {
         $d = $this->applyHook('table.insert:before', [$tableName, $recordData]);
         $TableGateway->insert($d);
         $recordData[$TableGateway->primaryKeyFieldName] = $TableGateway->getLastInsertValue();
         if ($tableName == 'directus_files') {
             $Files = new \Directus\Files\Files();
             $ext = pathinfo($recordData['name'], PATHINFO_EXTENSION);
             $thumbnailPath = 'thumbs/THUMB_' . $recordData['name'];
             if ($Files->exists($thumbnailPath)) {
                 $Files->rename($thumbnailPath, 'thumbs/' . $recordData[$this->primaryKeyFieldName] . '.' . $ext);
             }
             $updateArray = [];
             if ($Files->getSettings('file_naming') == 'file_id') {
                 $Files->rename($recordData['name'], str_pad($recordData[$this->primaryKeyFieldName], 11, '0', STR_PAD_LEFT) . '.' . $ext);
                 $updateArray['name'] = str_pad($recordData[$this->primaryKeyFieldName], 11, '0', STR_PAD_LEFT) . '.' . $ext;
                 $recordData['name'] = $updateArray['name'];
             }
             if (!empty($updateArray)) {
                 $Update = new Update($tableName);
                 $Update->set($updateArray);
                 $Update->where([$TableGateway->primaryKeyFieldName => $recordData[$TableGateway->primaryKeyFieldName]]);
                 $TableGateway->updateWith($Update);
             }
         }
         $this->runHook('postInsert', [$TableGateway, $recordData, $this->adapter, null]);
     }
     $columns = TableSchema::getAllNonAliasTableColumnNames($tableName);
     $recordData = $TableGateway->fetchAll(function ($select) use($recordData, $columns, $TableGateway) {
         $select->columns($columns)->limit(1);
         $select->where->equalTo($TableGateway->primaryKeyFieldName, $recordData[$TableGateway->primaryKeyFieldName]);
     })->current();
     return $recordData;
 }