protected function cmdDiff($args, $extra)
 {
     // TODO: Accepts a language code to compare it with another. (Default: English)
     $languageManager = Bootstrap::get('languagesManager');
     $languages = $languageManager->getLanguageList();
     if (count($languages) <= 1) {
         return [];
     }
     $languagesPhrases = [];
     foreach ($languages as $code => $name) {
         $languagesPhrases[] = ['code' => $code, 'name' => $name, 'phrases_keys' => get_locale_keys($code), 'diff' => []];
     }
     // =============================================================================
     // A language to compare to the rest of languages keys
     // =============================================================================
     $mainLanguage = $languagesPhrases[0];
     unset($languagesPhrases[0]);
     foreach ($languagesPhrases as $language) {
         $diff = ArrayUtils::missing($mainLanguage['phrases_keys'], $language['phrases_keys']);
         echo "--------------------" . PHP_EOL;
         echo $language['name'] . " has " . count($diff) . " missing/removed keys compared against " . $mainLanguage['name'] . PHP_EOL;
         foreach ($diff as $key) {
             echo "\t - " . $key . PHP_EOL;
         }
         echo "--------------------" . PHP_EOL . PHP_EOL;
     }
 }
 public function testMissing()
 {
     $array1 = ['one', 'two', 'three', 'five'];
     $array2 = ['one', 'four', 'five'];
     $result = ArrayUtils::missing($array1, $array2);
     $this->assertTrue(in_array('four', $result));
 }
Exemple #3
0
 public static function send($viewPath, $data, $callback)
 {
     $config = Bootstrap::get('config');
     $mailer = Bootstrap::get('mailer');
     if (!$mailer) {
         throw new InvalidArgumentException(__t('mail_configuration_no_defined'));
     }
     $DirectusSettingsTableGateway = new \Zend\Db\TableGateway\TableGateway('directus_settings', Bootstrap::get('zendDb'));
     $rowSet = $DirectusSettingsTableGateway->select();
     $settings = [];
     foreach ($rowSet as $setting) {
         $settings[$setting['collection']][$setting['name']] = $setting['value'];
     }
     $instance = new static($mailer, $settings);
     $message = Swift_Message::newInstance();
     // default mail from address
     $mailConfig = $config['mail'];
     $message->setFrom($mailConfig['from']);
     $bcc = ArrayUtils::get($mailConfig, 'bcc', null);
     if ($bcc !== null) {
         $message->setBcc($bcc);
     }
     $cc = ArrayUtils::get($mailConfig, 'cc', null);
     if ($cc !== null) {
         $message->setCc($cc);
     }
     call_user_func($callback, $message);
     if ($message->getBody() == null) {
         // Slim Extras View twig act weird on this version
         $viewContent = $instance->getViewContent($viewPath, $data);
         $message->setBody($viewContent, 'text/html');
     }
     $instance->sendMessage($message);
 }
 public function setValues($collection, $data)
 {
     $whiteList = array('files' => array('file_naming', 'thumbnail_quality', 'thumbnail_crop_enabled', 'youtube_api_key'), 'global' => array('project_name', 'project_url', 'cms_color', 'cms_user_auto_sign_out', 'rows_per_page', 'cms_thumbnail_url'));
     if ($collection !== 'files' && $collection !== 'global') {
         throw new \Exception("The settings collection {$collection} is not supported");
     }
     $data = ArrayUtils::pick($data, $whiteList[$collection]);
     foreach ($data as $key => $value) {
         $parameters[] = '(' . $this->adapter->platform->quoteValue($collection) . ',' . $this->adapter->platform->quoteValue($key) . ',' . $this->adapter->platform->quoteValue($value) . ')';
     }
     $sql = 'INSERT INTO directus_settings (`collection`, `name`, `value`) VALUES ' . implode(',', $parameters) . ' ' . 'ON DUPLICATE KEY UPDATE `collection` = VALUES(collection), `name` = VALUES(name), `value` = VALUES(value)';
     $query = $this->adapter->query($sql, Adapter::QUERY_MODE_EXECUTE);
 }
 public function call()
 {
     $corsOptions = ArrayUtils::get($this->app->container->get('config'), 'cors', []);
     if (ArrayUtils::get($corsOptions, 'enabled', false)) {
         $response = $this->app->response();
         $response->header('Access-Control-Allow-Origin', ArrayUtils::get($corsOptions, 'origin', '*'));
         foreach (ArrayUtils::get($corsOptions, 'headers', []) as list($headerType, $headerValue)) {
             $response->header($headerType, $headerValue);
         }
     }
     if (!$this->app->request()->isOptions()) {
         $this->next->call();
     }
 }
 protected function formatColumnFromInfo($columnInfo, $directusColumnInfo)
 {
     $matches = [];
     preg_match('#^([a-zA-Z]+)(\\(.*\\)){0,1}$#', $columnInfo->getDataType(), $matches);
     $dataType = strtoupper($matches[1]);
     return ['id' => $columnInfo->getName(), 'column_name' => $columnInfo->getName(), 'type' => $dataType, 'char_length' => $columnInfo->getCharacterMaximumLength(), 'is_nullable' => $columnInfo->getIsNullable() ? 'YES' : 'NO', 'default_value' => $columnInfo->getColumnDefault() == 'NULL' ? NULL : $columnInfo->getColumnDefault(), 'comment' => '', 'sort' => $columnInfo->getOrdinalPosition(), 'column_type' => $columnInfo->getDataType(), 'ui' => ArrayUtils::get($directusColumnInfo, 'ui', null), 'hidden_list' => ArrayUtils::get($directusColumnInfo, 'hidden_list', 0), 'hidden_input' => ArrayUtils::get($directusColumnInfo, 'hidden_input', 0), 'relationship_type' => ArrayUtils::get($directusColumnInfo, 'relationship_type', null), 'related_table' => ArrayUtils::get($directusColumnInfo, 'related_table', null), 'junction_table' => ArrayUtils::get($directusColumnInfo, 'junction_table', null), 'junction_key_left' => ArrayUtils::get($directusColumnInfo, 'junction_key_left', null), 'junction_key_right' => ArrayUtils::get($directusColumnInfo, 'junction_key_right', null), 'required' => ArrayUtils::get($directusColumnInfo, 'required', 0)];
 }
Exemple #7
0
 public function testPickItems()
 {
     $items = ['name' => 'Jim', 'age' => 79, 'sex' => 'M', 'country' => 'N/A'];
     $this->assertEquals(count(ArrayUtils::pick($items, ['name', 'age'])), 2);
     $this->assertEquals(count(ArrayUtils::pick($items, ['name', 'age', 'city'])), 2);
 }
 /**
  * @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']['related_table'];
         // @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']);
                             // @NOTE: this is duplicate code from the upload file endpoint
                             //        to maintain the file title.
                             $recordData[$colName][$index]['data'] = array_merge($recordData[$colName][$index]['data'], ArrayUtils::omit($row['data'], ['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']);
                         // @NOTE: this is duplicate code from the upload file endpoint
                         //        to maintain the file title.
                         $recordData[$colName] = array_merge($recordData[$colName], ArrayUtils::omit($foreignRow, ['data', 'name']));
                     }
                 }
                 unset($recordData[$colName]['data']);
             }
         }
     }
     return true;
 }
 /**
  * Add Directus default user
  *
  * @param array $data
  * @return array
  */
 public static function addDefaultUser($data)
 {
     $db = Bootstrap::get('ZendDb');
     $tableGateway = new TableGateway('directus_users', $db);
     $hash = password_hash($data['directus_password'], PASSWORD_DEFAULT, ['cost' => 12]);
     $data['user_salt'] = StringUtils::randomString();
     $data['user_token'] = StringUtils::randomString(32);
     $data['avatar'] = get_gravatar($data['directus_email']);
     $tableGateway->insert(['active' => 1, 'first_name' => 'Admin', 'last_name' => 'User', 'email' => $data['directus_email'], 'password' => $hash, 'salt' => $data['user_salt'], 'avatar' => $data['avatar'], 'group' => 1, 'token' => $data['user_token'], 'language' => ArrayUtils::get($data, 'default_language', 'en')]);
     return $data;
 }
Exemple #10
0
 private static function formatColumnRow($row)
 {
     $columnName = $row['column_name'];
     foreach ($row as $key => $value) {
         if (is_null($value)) {
             unset($row[$key]);
         }
     }
     unset($row['table_name']);
     $row['id'] = $columnName;
     $row['options'] = array();
     // Many-to-Many type it actually can be null,
     // it's based on a junction table, not a real column.
     // Issue #612 https://github.com/RNGR/directus6/issues/612
     if (array_key_exists('type', $row) && $row['type'] == 'MANYTOMANY') {
         $row['is_nullable'] = "YES";
     }
     if ($row['is_nullable'] == "NO") {
         $row["required"] = true;
     }
     // Basic type casting. Should eventually be done with the schema
     $row["required"] = (bool) $row['required'];
     $row["system"] = (bool) $row["system"];
     $row["master"] = (bool) $row["master"];
     $row["hidden_list"] = (bool) $row["hidden_list"];
     $row["hidden_input"] = (bool) $row["hidden_input"];
     //$row["is_writable"] = !in_array($row['id'], $writeFieldBlacklist);
     if (array_key_exists('sort', $row)) {
         $row["sort"] = (int) $row['sort'];
     }
     $hasMaster = $row["master"];
     // Default UI types.
     if (!isset($row["ui"])) {
         $row['ui'] = self::columnTypeToUIType($row['type']);
     }
     // Defualts as system columns
     if ($row["id"] == 'id' || $row["id"] == STATUS_COLUMN_NAME || $row["id"] == 'sort') {
         $row["system"] = true;
         $row["hidden"] = true;
     }
     if (array_key_exists('table_related', $row)) {
         $row['relationship'] = array();
         $row['relationship']['type'] = ArrayUtils::get($row, 'relationship_type');
         $row['relationship']['table_related'] = $row['table_related'];
         unset($row['relationship_type']);
         unset($row['table_related']);
         if (array_key_exists('junction_key_left', $row)) {
             $row['relationship']['junction_key_left'] = $row['junction_key_left'];
             unset($row['junction_key_left']);
         }
         if (array_key_exists('junction_key_right', $row)) {
             $row['relationship']['junction_key_right'] = $row['junction_key_right'];
             unset($row['junction_key_right']);
         }
         if (array_key_exists('junction_table', $row)) {
             $row['relationship']['junction_table'] = $row['junction_table'];
             unset($row['junction_table']);
         }
     }
     return $row;
 }
Exemple #11
0
 /**
  * Get all Directus core tables name
  *
  * @param array $filterNames
  * @return array
  */
 public static function getDirectusTables(array $filterNames = [])
 {
     $tables = static::$directusTables;
     if ($filterNames) {
         $tables = ArrayUtils::pick($tables, $filterNames);
     }
     return static::addCoreTablePrefix($tables);
 }
Exemple #12
0
 private static function formatColumnRow($row)
 {
     $columnName = $row['column_name'];
     foreach ($row as $key => $value) {
         if (is_null($value)) {
             unset($row[$key]);
         }
     }
     unset($row['table_name']);
     $row['id'] = $columnName;
     $row['options'] = [];
     // Many-to-Many type it actually can be null,
     // it's based on a junction table, not a real column.
     // Issue #612 https://github.com/RNGR/directus6/issues/612
     if (array_key_exists('type', $row) && $row['type'] == 'ALIAS') {
         $row['is_nullable'] = 'YES';
     }
     $hasDefaultValue = isset($row['default_value']);
     $anAlias = static::isColumnTypeAnAlias($row['type']);
     if ($row['is_nullable'] === 'NO' && !$hasDefaultValue && !$anAlias) {
         $row['required'] = true;
     }
     // Basic type casting. Should eventually be done with the schema
     if ($hasDefaultValue) {
         $row['default_value'] = SchemaManager::parseType($row['default_value'], $row['type']);
     }
     $row['required'] = (bool) $row['required'];
     $row['system'] = (bool) static::isSystemColumn($row['id']);
     $row['hidden_list'] = (bool) $row['hidden_list'];
     $row['hidden_input'] = (bool) $row['hidden_input'];
     //$row['is_writable'] = !in_array($row['id'], $writeFieldBlacklist);
     if (array_key_exists('sort', $row)) {
         $row['sort'] = (int) $row['sort'];
     }
     // Default UI types.
     if (!isset($row['ui'])) {
         $row['ui'] = self::columnTypeToUIType($row['type']);
     }
     // Defualts as system columns
     if (static::isSystemColumn($row['id'])) {
         $row['system'] = true;
         $row['hidden'] = true;
     }
     if (array_key_exists('related_table', $row)) {
         $row['relationship'] = [];
         $row['relationship']['type'] = ArrayUtils::get($row, 'relationship_type');
         $row['relationship']['related_table'] = $row['related_table'];
         unset($row['relationship_type']);
         unset($row['related_table']);
         if (array_key_exists('junction_key_left', $row)) {
             $row['relationship']['junction_key_left'] = $row['junction_key_left'];
             unset($row['junction_key_left']);
         }
         if (array_key_exists('junction_key_right', $row)) {
             $row['relationship']['junction_key_right'] = $row['junction_key_right'];
             unset($row['junction_key_right']);
         }
         if (array_key_exists('junction_table', $row)) {
             $row['relationship']['junction_table'] = $row['junction_table'];
             unset($row['junction_table']);
         }
     }
     return $row;
 }
Exemple #13
0
 $TableGateway = new TableGateway($acl, $table, $ZendDb);
 $activityLoggingEnabled = !(isset($_GET['skip_activity_log']) && 1 == $_GET['skip_activity_log']);
 $activityMode = $activityLoggingEnabled ? TableGateway::ACTIVITY_ENTRY_MODE_PARENT : TableGateway::ACTIVITY_ENTRY_MODE_DISABLED;
 switch ($app->request()->getMethod()) {
     case 'POST':
         $requestPayload['user'] = $currentUser['id'];
         $requestPayload['date_uploaded'] = DateUtils::now();
         // When the file is uploaded there's not a data key
         if (array_key_exists('data', $requestPayload)) {
             $Files = new \Directus\Files\Files();
             if (!array_key_exists('type', $requestPayload) || strpos($requestPayload['type'], 'embed/') === 0) {
                 $recordData = $Files->saveEmbedData($requestPayload);
             } else {
                 $recordData = $Files->saveData($requestPayload['data'], $requestPayload['name']);
             }
             $requestPayload = array_merge($recordData, ArrayUtils::omit($requestPayload, ['data', 'name']));
         }
         $newRecord = $TableGateway->manageRecordUpdate($table, $requestPayload, $activityMode);
         $params['id'] = $newRecord['id'];
         break;
     case 'PATCH':
         $requestPayload['id'] = $id;
     case 'PUT':
         if (!is_null($id)) {
             $TableGateway->manageRecordUpdate($table, $requestPayload, $activityMode);
             break;
         }
 }
 $Files = new TableGateway($acl, $table, $ZendDb);
 $response = $Files->getEntries($params);
 if (!$response) {
Exemple #14
0
 /**
  * Check if a user is logged in.
  * @return boolean
  */
 public static function loggedIn()
 {
     if (self::$authenticated != null) {
         return self::$authenticated;
     }
     self::prependSessionKey();
     if (php_sapi_name() != 'cli' && '' === session_id()) {
         session_start();
     }
     self::$authenticated = $isLoggedIn = false;
     $ZendDb = Bootstrap::get('ZendDb');
     $session = [];
     if (isset($_SESSION[self::$SESSION_KEY]) && !empty($_SESSION[self::$SESSION_KEY])) {
         $session = $_SESSION[self::$SESSION_KEY];
     }
     if (is_array($session) && ArrayUtils::contains($session, ['id', 'access_token'])) {
         $DirectusUsersTableGateway = new \Zend\Db\TableGateway\TableGateway('directus_users', $ZendDb);
         $user = $DirectusUsersTableGateway->select(['id' => $session['id'], 'access_token' => $session['access_token']]);
         if ($user->count()) {
             self::$authenticated = $isLoggedIn = true;
         }
     }
     return $isLoggedIn;
 }
 public function addColumn($tableName, $tableData)
 {
     // @TODO: enforce permission
     $directus_types = ['MANYTOMANY', 'ONETOMANY', 'ALIAS'];
     $relationshipType = ArrayUtils::get($tableData, 'relationship_type', null);
     // TODO: list all types which need manytoone ui
     // Hard-coded
     $manytoones = ['single_file', 'many_to_one', 'many_to_one_typeahead', 'MANYTOONE'];
     if (in_array($relationshipType, $directus_types)) {
         //This is a 'virtual column'. Write to directus schema instead of MYSQL
         $this->addVirtualColumn($tableName, $tableData);
     } else {
         $this->addTableColumn($tableName, $tableData);
         // Temporary solutions to #481, #645
         if (array_key_exists('ui', $tableData) && in_array($tableData['ui'], $manytoones)) {
             $tableData['relationship_type'] = 'MANYTOONE';
             $tableData['junction_key_right'] = $tableData['column_name'];
         }
         $this->addVirtualColumn($tableName, $tableData);
     }
     return $tableData['column_name'];
 }