Example #1
0
function importData($type, $format, $filename)
{
    switch ($type) {
        case 'ioc':
            $iocList = [];
            switch ($format) {
                case 'json':
                    $iocList = json_decode(file_get_contents($filename), true);
                    // 					foreach ($iocList as &$ioc) $ioc['value'] = json_encode($ioc['value']);
                    break;
                case 'csv':
                    $iocList = parseCsv(file($filename));
                    foreach ($iocList as &$ioc) {
                        unpackValues($ioc['value']);
                    }
                    break;
                default:
                    throw new Exception('Unsupported format');
            }
            if (!is_array($iocList) || isIocData($iocList)) {
                $iocList = [$iocList];
            }
            $iocApi = new Ioc([]);
            foreach ($iocList as $ioc) {
                if (isIocData($ioc)) {
                    $ioc['value'] = json_encode($ioc['value']);
                    $iocApi->setParams($ioc)->addAction();
                } else {
                    throw new Exception('Bad data');
                }
            }
            headerRedirect(1, 'IOC import successful');
        case 'set':
            $setList = [];
            switch ($format) {
                case 'json':
                    $setList = json_decode(file_get_contents($filename), true);
                    break;
                default:
                    throw new Exception('Unsupported format');
            }
            if (!is_array($setList) || isSetData($setList)) {
                $setList = [$setList];
            }
            $iocApi = new Ioc([]);
            $setApi = new Set([]);
            foreach ($setList as $set) {
                if (isSetData($set)) {
                    $goodName = $set['name'];
                    $iter = 1;
                    $namePassed = false;
                    while (!$namePassed) {
                        $namePassed = true;
                        try {
                            $setApi->setParams(['name' => $goodName, 'type' => 'root', 'parent' => -1])->addAction();
                        } catch (Exception $e) {
                            $namePassed = false;
                            $iter++;
                            $goodName = $set['name'] . ' ' . $iter;
                        }
                    }
                    foreach ($set['data'] as $root) {
                        importTree($goodName, $root, 0);
                    }
                } else {
                    throw new Exception('Bad data');
                }
            }
            headerRedirect(1, 'Set import successful');
        case 'rep':
            $repList = [];
            switch ($format) {
                case 'json':
                    $repList = json_decode(file_get_contents($filename), true);
                    break;
                default:
                    throw new Exception('Unsupported format');
            }
            if (!is_array($repList) || isRepData($repList)) {
                $repList = [$repList];
            }
            $clientApi = new Client([]);
            foreach ($repList as $report) {
                if (isRepData($report)) {
                    $clientApi->setParams(['report' => json_encode($report)])->uploadAction();
                } else {
                    throw new Exception('Bad data');
                }
            }
            headerRedirect(1, 'Report import successful');
        default:
            throw new Exception('Invalid type');
    }
}