示例#1
0
 public function actionParserest()
 {
     header('Content-type: application/json');
     $result = array();
     $result['start'] = date('m-d-Y\\TH:i:s');
     $scannedFiles = [];
     try {
         $data = file_get_contents("php://input");
         $data = json_decode($data);
         if ($data) {
             $this->ftpHost = isset($data->ftpHost) && $data->ftpHost ? $data->ftpHost : $this->ftpHost;
             $this->ftpLogin = isset($data->ftpLogin) && $data->ftpLogin ? $data->ftpLogin : $this->ftpLogin;
             $this->ftpPassword = isset($data->ftpPassword) && $data->ftpPassword ? $data->ftpPassword : $this->ftpPassword;
             $this->lsrUrl = isset($data->lsrURL) && $data->lsrURL ? $data->lsrURL : $this->lsrUrl;
             $this->eventType = isset($data->eventType) && $data->eventType ? $data->eventType : $this->eventType;
             $this->force = isset($data->force) && $data->force ? $data->force : $this->force;
             $this->_debug = isset($data->_debug) && $data->_debug ? $data->_debug : $this->_debug;
             if (!is_array($this->eventType)) {
                 throw new Exception('EventType should be an array');
             } else {
                 $this->eventType = array_map('strtolower', $this->eventType);
             }
             $ftpHelper = new FtpHelper();
             $ftpHelper->ftpHost = $this->ftpHost;
             $ftpHelper->ftpLogin = $this->ftpLogin;
             $ftpHelper->ftpPassword = $this->ftpPassword;
             $ftpHelper->fileNameFormat = $this->fileNameFormat;
             $ftpHelper->filterEventType = $this->eventType;
             $ftpHelper->forceScan = $this->force;
             $filesList = $ftpHelper->connectToFtp();
             if (!$this->lsrUrl) {
                 $scannedFiles[] = $ftpHelper->performScan($filesList);
             } else {
                 $this->_writeLogLine('LSR File to scan - ' . $this->lsrUrl);
                 if (in_array($this->lsrUrl, $filesList)) {
                     $scannedFiles[] = $ftpHelper->performScan([$this->lsrUrl]);
                 } else {
                     //                        $this->_errorMessage = "Can't find such file on FTP";
                     //                        $this->_writeLogLine($this->_errorMessage.'('.$this->lsrUrl.')');
                     throw new Exception("Can't find such file on FTP");
                 }
             }
             $result['end'] = date('m-d-Y\\TH:i:s');
             $this->_writeLogLine('Process started - ' . $result['start']);
             $this->_writeLogLine('Process ended - ' . $result['end']);
             $result['insertedAlerts'] = $ftpHelper->insertedAlerts;
             $result['ignoredAlerts'] = $ftpHelper->ignoredAlerts;
             $this->_writeLogLine('Inserted alerts - ' . $result['insertedAlerts']);
             $this->_writeLogLine('Ignored Alerts - ' . $result['ignoredAlerts']);
             $result['files'] = $scannedFiles[0];
             echo json_encode($result);
         } else {
             throw new Exception('Missing lsrURL parameter');
         }
     } catch (\Exception $e) {
         echo json_encode(['error' => $e->getMessage() . ' Line:' . $e->getLine()]);
         $this->_writeLogLine('ERROR - ' . $e->getMessage());
     }
     Yii::$app->end();
 }
示例#2
0
 public function parseLsrFeed()
 {
     $result = array();
     $result['start'] = date('m-d-Y\\TH:i:s');
     $scannedFiles = [];
     try {
         $data = Yii::$app->params['LsrParser'];
         if ($data) {
             if (!is_array($data['filter'])) {
                 throw new Exception('EventType should be an array');
             } else {
                 $filter = array_map('strtolower', $data['filter']);
             }
             $ftpHelper = new helpers\FtpHelper();
             $ftpHelper->ftpHost = $data['ftpHost'];
             $ftpHelper->ftpLogin = $data['ftpLogin'];
             $ftpHelper->ftpPassword = $data['ftpPassword'];
             $ftpHelper->fileNameFormat = $data['fileNameFormat'];
             $ftpHelper->filterEventType = $filter;
             $ftpHelper->forceScan = $data['force'];
             $filesList = $ftpHelper->connectToFtp();
             if (!$data['files']) {
                 $scannedFiles[] = $ftpHelper->performScan($filesList);
             } else {
                 $this->consoleLog('LSR File to scan - ' . $this->lsrUrl);
                 if (in_array($data['files'], $filesList)) {
                     $scannedFiles[] = $ftpHelper->performScan([$data['files']]);
                 } else {
                     //                        $this->_errorMessage = "Can't find such file on FTP";
                     //                        $this->consoleLog($this->_errorMessage.'('.$this->lsrUrl.')');
                     throw new Exception("Can't find such file on FTP");
                 }
             }
             $result['end'] = date('m-d-Y\\TH:i:s');
             $this->consoleLog('Process started - ' . $result['start']);
             $this->consoleLog('Process ended - ' . $result['end']);
             $result['insertedAlerts'] = $ftpHelper->insertedAlerts;
             $result['ignoredAlerts'] = $ftpHelper->ignoredAlerts;
             $this->consoleLog('Inserted alerts - ' . $result['insertedAlerts']);
             $this->consoleLog('Ignored Alerts - ' . $result['ignoredAlerts']);
             $result['files'] = $scannedFiles[0];
             //                echo json_encode($result);
         } else {
             throw new Exception('Missing lsrURL parameter');
         }
     } catch (\Exception $e) {
         //            echo json_encode(['error'=>$e->getMessage().' Line:'.$e->getLine()]);
         $this->consoleLog('ERROR - ' . $e->getMessage());
         $this->consoleLog('LINE - ' . $e->getLine());
         $this->consoleLog('FILE - ' . $e->getFile());
     }
     Yii::$app->end();
 }