Parse attachments
@return array    Returns array with failed or success data
(See parser-common/src/Parser.php) for more info.
160517
public function parse()
    {
        /**
         *  Try to find ARF report.
         *  Some notification emails do not contain an ARF report. Instead they
         *  contain a 'table row'-ish with abuse info. In that case we jump down
         *  and parse the email body.
         */
        $foundArf = false;
        foreach ($this->parsedMail->getAttachments() as $attachment) {
            // Only use the Webiron formatted reports, skip all others
            if (preg_match(config("{$this->configBase}.parser.report_file"), $attachment->filename)) {
                $raw_report = $attachment->getContent();

                // We found an ARF report, yay!
                if (!empty($raw_report)) {
                    $foundArf = true;

                    if (!preg_match_all('/([\w\-]+): (.*)[ ]*\r?\n/', $raw_report, $matches)) {
                        $this->warningCount++;
                        continue;
                    }

                    $report = array_combine($matches[1], array_map('trim', $matches[2]));

                    if (!empty($report['Report-Type'])) {
                        $this->feedName = $report['Report-Type'];

                        // If feed is known and enabled, validate data and save report
                        if ($this->isKnownFeed() && $this->isEnabledFeed()) {
                            // Sanity check
                            if ($this->hasRequiredFields($report) === true) {
                                // incident has all requirements met, filter and add!
                                $report = $this->applyFilters($report);

                                $incident = new Incident();

                                $incident->source      = config("{$this->configBase}.parser.name");
                                $incident->source_id   = false;
                                $incident->ip          = $report['Source'];
                                $incident->domain      = false;
                                $incident->class       = config("{$this->configBase}.feeds.{$this->feedName}.class");
                                $incident->type        = config("{$this->configBase}.feeds.{$this->feedName}.type");
                                $incident->timestamp   = strtotime(str_replace('\'', '', $report['Date']));
                                $incident->information = json_encode($report);

                                $this->incidents[] = $incident;
                            }
                        }
                    } else {
                        $this->warningCount++;
                    }
                }
            }
        }

        if ($foundArf === false) {
            // Didn't find an ARF report, go scrape the email body!
            $body = $this->parsedMail->getMessageBody();
            $this->feedName = 'botnet-infection';

            // If feed is known and enabled, validate data and save report
            if ($this->isKnownFeed() && $this->isEnabledFeed()) {
                if (preg_match_all('/  - ([^:]+): ([^\n]+)\n/', $body, $matches)) {
                    $report = array_combine($matches[1], array_map('trim', $matches[2]));

                    // Get IP address is not in the report
                    if (empty($report['ip'])) {
                        if (preg_match(
                            '/(?:Offending|Source) IP:[ ]+([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})\n/',
                            $body,
                            $matches
                        )) {
                            $report['ip'] = $matches[1];
                        } elseif (preg_match(
                            '/(?:Offending|Source) IP:[ ]+(?>(?>([a-f0-9]{1,4})(?>:(?1)){7}|'.
                            '(?!(?:.*[a-f0-9](?>:|$)){8,})((?1)(?>:(?1)){0,6})?::(?2)?)|(?>('.
                            '?>(?1)(?>:(?1)){5}:|(?!(?:.*[a-f0-9]:){6,})(?3)?::(?>((?1)(?>:('.
                            '?1)){0,4}):)?)?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?4)){3}))/',
                            $body,
                            $matches
                        )) {
                            $matches = explode('IP:', $matches[0]);
                            $report['ip'] = trim($matches[1]);
                        } else {
                            $this->warningCount++;
                        }
                    }

                    // Sanity check
                    if ($this->hasRequiredFields($report) === true) {
                        // incident has all requirements met, filter and add!
                        $report = $this->applyFilters($report);

                        $incident = new Incident();

                        $incident->source      = config("{$this->configBase}.parser.name");
                        $incident->source_id   = false;
                        $incident->ip          = $report['ip'];
                        $incident->domain      = false;
                        $incident->class       = config("{$this->configBase}.feeds.{$this->feedName}.class");
                        $incident->type        = config("{$this->configBase}.feeds.{$this->feedName}.type");
                        $incident->timestamp   = strtotime($report['Time']);
                        $incident->information = json_encode($report);

                        $this->incidents[] = $incident;

                    }
                } else {
                    $this->warningCount++;
                }
            }
        }

        return $this->success();
    }
776749
public function getData(): array
    {
        $uploads = [];
        //if the user is trying to upload a image
        if ($this->request->hasFiles()) {
            foreach ($this->request->getUploadedFiles() as $file) {
                $uploads[] = [
                    'name' => $file->getKey(),
                    'filename' => $file->getName(),
                    'contents' => file_get_contents($file->getTempName()),
                ];
            }

            $parseUpload = function ($request) use (&$uploads) {
                foreach ($request as $key => $value) {
                    if (is_array($value)) {
                        foreach ($value as $f => $v) {
                            $uploads[] = ['name' => $key . '[' . $f . ']', 'contents' => $v];
                        }
                    } else {
                        $uploads[] = ['name' => $key, 'contents' => $value];
                    }
                }
            };
        }

        switch ($this->request->getMethod()) {
            case 'GET':
                $queryParams = $this->request->getQuery();
                unset($queryParams['_url']);
                return ['query' => $queryParams];
                break;

            case 'POST':
                if (!$uploads) {
                    //if we dont get post send the raw
                    if (count($this->request->getPost()) > 0) {
                        return ['form_params' => $this->request->getPost()];
                    } elseif ($this->request->getJsonRawBody()) {
                        return ['json' => json_decode($this->request->getRawBody(), true)];
                    }
                    //why did we get here?
                    return ['form_params' => $this->request->getPost()];
                } else {
                    $parseUpload($this->request->getPost());
                    return ['multipart' => $uploads];
                }
                break;

            case 'PUT':
                if (!$uploads) {
                    //if we get put send it with the normal put, else send json
                    if (count($this->request->getPut()) > 0) {
                        return ['form_params' => $this->request->getPut()];
                    } elseif ($this->request->getJsonRawBody()) {
                        return ['json' => json_decode($this->request->getRawBody(), true)];
                    }
                    //why did we get here?
                    return ['form_params' => $this->request->getPut()];
                } else {
                    $parseUpload($this->request->getPut());
                    return ['multipart' => $uploads];
                }

                break;
            default:
                return [];
                break;
        }
    }
816075
public function getValuesAsArray()
    {
        //converts the files array with multiple values into PHP's ugly format
        //http://php.net/manual/en/reserved.variables.files.php#89674
        if ($this->files) {
            $parsedFileArray = [];
            foreach ($this->files as $name => $file) {
                if (!isset($file['name'])) {
                    if (!isset($parsedFileArray[$name])) {
                        $parsedFileArray[$name]['name'] = [];
                        $parsedFileArray[$name]['type'] = [];
                        $parsedFileArray[$name]['tmp_name'] = [];
                        $parsedFileArray[$name]['error'] = [];
                        $parsedFileArray[$name]['size'] = [];
                    }
                    foreach ($file as $subfile) {
                        $parsedFileArray[$name]['name'][] = $subfile['name'];
                        $parsedFileArray[$name]['type'][] = $subfile['type'];
                        $parsedFileArray[$name]['tmp_name'][] = $subfile['tmp_name'];
                        $parsedFileArray[$name]['error'][] = $subfile['error'];
                        $parsedFileArray[$name]['size'][] = $subfile['size'];
                    }
                } else {
                    $parsedFileArray[$name] = $file;
                }
            }
        }

        return [
            'post'  => $this->post,
            'files' => isset($parsedFileArray) ? $parsedFileArray : $this->files
        ];
    }
434886
protected function getCurrentAttachmentData()
    {
        $attributes = [
            'file_name'    => $this->originalFilename(),
            'file_size'    => $this->size(),
            'content_type' => $this->contentType(),
            'updated_at'   => $this->updatedAt(),
            'created_at'   => $this->createdAt(),
            'variants'     => $this->variantsAttribute(),
        ];

        $variants = [];

        foreach ($this->variants() as $variant) {

            $variants[ $variant ] = [
                'file_name'    => $this->variantFilename($variant),
                'content_type' => $this->variantContentType($variant),
                'extension'    => $this->variantExtension($variant),
            ];
        }

        return new AttachmentData(
            $this->name,
            $this->getConfig(),
            $attributes,
            $variants,
            $this->getInstanceKey(),
            $this->getInstanceClass()
        );
    }
1144657
public function attachmentsAsRows()
    {
        $rows = [];

        if ($this->hasAttachments()) {
            $rows = array_chunk($this->attachments()->values(), $this->numColumns);

            /** Map row content with useful front-end properties. */
            array_walk($rows, function(&$value, $key) {
                $value = [
                    'columns' => $value,
                    'isFirst' => $key === 0
                ];
            });
        }

        return $rows;
    }
1090920
public function parse() : array
    {
        $buffer = $this->routing_rule;
        
        $section_list_set = [];
        $current_list = [];
        do
        {
            $pos = strpos(substr($buffer,1),'/');
            $section = $pos !== false ? substr($buffer, 1, $pos) : substr($buffer, 1);
            $buffer = $pos !== false ? substr($buffer, $pos + 1) : '';
            
            $length = strlen($section);
            if ($length === 0){
                $current_list[] = new class implements EmptyRoutingPathInterface {};
                if (!empty($buffer)){
                    throw new RoutingRuleParseException($this->routing_rule, "Empty routing section is allowed only for last element");
                }
                break;
            }
            $first = $section[0];
            $last = $section[$length - 1];
            if ($first === '[' && $last === ']'){
                // push current list
                $section_list_set[] = $current_list + ['()'];
                // add optional part to current list
                $inside = substr($section, 1, $length - 2);
                if ($inside[0] === ':') {
                    $current_list[] = self::parseVariablePattern($inside);
                }
                else {
                    $current_list[] = $inside;
                }
            }
            else if ($first === ':') {
                $current_list[] = self::parseVariablePattern($section);
            }
            else {
                $current_list[] = $section;
            }
        }
        while(!empty($buffer));
        
        $section_list_set[] = $current_list;
        return $section_list_set;
    }
301998
protected function getAttachments()
    {
        $attachments = array();
        foreach ($this->getSections(self::SECTION_ATTACHMENTS) as $section) {
            $obj = $this->getSection($section);
            $attachment = new IncomingMessageAttachment($obj);
            $objNew = new \stdClass();
            $objNew->name = $attachment->name;
            $objNew->body = $attachment->body;
            $objNew->info = $obj;
            $attachments[] = $objNew;
        }
        $this->attachments = $attachments;
    }
1250303
public function getParsed()
    {
        $parsed = [];

        $parsers = $this->getParsers();
        if (0 === count($parsers)) {
            $this->addDefaultParsers();
        }

        foreach ($this->getParsers() as $name => $parser) {
            $parser->getLink()->setUrl($this->getUrl());

            if ($parser->isValidParser()) {
                $parsed[$name] = $parser->parseLink();

                if (!$this->getPropagation()) {
                    break;
                }
            }
        }

        return $parsed;
    }
1189764
protected function parse()
	{
		$inside = false;
		$doc_open	= false;
		$func_data	= array();

		$f = new FS_File_Reader( $this->fileName );
		$lines = $f->readArray();
		array_pop( $lines );
		array_shift( $lines ); 
		foreach( $lines as $line )
		{
			$line = trim( $line );
			$line = str_replace( array( "*", "\t\t", "\t\t" ), array( "#", "\t", "\t" ), $line );
			if( $inside )
			{
				if( !$doc_open )
				{
					if( ereg( $this->patterns['doc_start'], $line ) )
						$doc_open = true;
					else if( ereg( $this->patterns["method"], $line ) )
					{
						$parts = explode( "function", $line );
						$method = substr( $parts[1], 0, strpos($parts[1], "(" ) );
						$method = str_replace( "&", "", $method );
						$method = trim( $method );
						$this->methods[$method] = $func_data;
						$func_data = array();
					}
				}
				if( $doc_open )
				{
					$found_pattern = false;
					if( ereg( $this->patterns["doc_data"], $line ) )
					{
						foreach( $this->methodProperties as $prop => $pattern )
						{
							if( ereg( $this->patterns[$pattern], $line ) )
							{
								$found_pattern = true;
								$func_data[$prop] = $this->getValueOfDocLine( $line, $pattern );
							}
						}
						if( ereg( $this->patterns["d_param"], $line ) )
						{
							$parts = explode( $this->patterns["d_param"], $line );
							$parts = explode( "\t", trim($parts[1] ) );
							$func_data["param"][$parts[1]]['type'] = $parts[0];
							if( isset( $parts[2] ) )
								$func_data["param"][$parts[1]]['desc'] = $parts[2];
						}
						else if( !$found_pattern && !ereg( $this->patterns['doc_end'], $line ) )
						{
							$desc = ereg_replace( $this->patterns["doc_data"], "", $line );
							$desc = trim( $desc );
							if( $desc )
								$func_data["desc"][] = $desc;
						}
					}
					else	if( ereg( $this->patterns["d_var"], $line ) )
					{
						$parts = explode( $this->patterns["d_var"], $line );
						$parts = preg_split( "@\t+@", trim($parts[1] ) );
						$this->vars[$parts[1]]['type'] = $parts[0];
						$access = ( substr($parts[1], 0, 1) == "_" ) ? "private" : "public";
							$this->vars[$parts[1]]['access'] = $access;
						$this->vars[$parts[1]]['desc'][] = str_replace( array( "#", "/" ), "", $parts[2] );
					}
					if( ereg( $this->patterns['doc_end'], $line ) )
						$doc_open = false;
				}
			}
			else
			{
				if( eregi( $this->patterns["import"], $line ) )
				{
					$import = str_replace( array( "import", " ", "(", ")", ";", "'", '"' ), "", $line );
					$parts = explode( ".", $import );
					$this->imports[$parts[count( $parts )-1]] = implode( "/", $parts );
				}
				else if( eregi( $this->patterns["class"], $line ) )
				{
					$line	= preg_replace( "@".$this->patterns["class"]."@i", "", $line );
					$parts = explode( " extends ", $line );
					$class = explode( " implements ", $parts[0] );
					$class = $class[0];
					
//					$class = str_replace( array( "class", " " ), "", $class );
					$this->classData["class"] = $class;
					if( isset( $parts[1] ) )
						$this->classData["extends"] = $parts[1];
					$inside = true;
				}
				else if( !$doc_open )
				{
					if( ereg( $this->patterns['doc_start'], $line ) )
						$doc_open = true;
				}
				else if( $doc_open )
				{
					if( ereg( $this->patterns['doc_end'], $line ) )
						$doc_open = false;
					else
					{
						if( ereg( $this->patterns["doc_data"], $line ) )
						{
							$found_pattern = false;
							foreach( $this->classProperties as $prop => $pattern )
							{
								if( ereg( $this->patterns[$pattern], $line ) )
								{
									$found_pattern = true;
									$this->classData[$prop][] = $this->getValueOfDocLine( $line, $pattern );
								}
							}
							if( !$found_pattern && !ereg( $this->patterns['doc_end'], $line ) )
							{
								if( ereg( $this->patterns['d_uses'], $line ) )
								{
									$parts = explode( $this->patterns["d_uses"], $line );
									$uses = trim( $parts[1] );
									if( !in_array ( $uses, $this->classData["uses"] ) )
										$this->classData["uses"][] = $uses; 
								}
								else if( ereg( $this->patterns['d_todo'], $line ) )
								{
									$parts = explode( $this->patterns["d_todo"], $line );
									$todo = trim( $parts[1] );
									if( !in_array( $todo, $this->classData["todo"] ) )
										$this->classData["todo"][] = $todo; 
								}
								else
								{
									$desc = ereg_replace( $this->patterns["doc_data"], "", $line );
									$desc = trim( $desc );
									if( $desc && !in_array( $desc, $this->classData["desc"] ) )
										$this->classData["desc"][] = $desc;
								}
							}
						}
					}	
				}
			}
		}
		foreach( $this->classProperties as $prop => $pattern )
			if( isset( $this->classData[$prop] ) && is_array( $this->classData[$prop] ) )
				$this->classData[$prop]	= array_unique( $this->classData[$prop] );
	}
294909
private function prepareAttachments($attachments = null)
    {
        $file_params = [];

        if ($attachments) {
            $counter = 1;

            foreach ($attachments as $attachment) {
                $path = is_array($attachment) ? $attachment[0] : $attachment;

                if (is_readable($path)) {
                    $file_params['attachment_'.$counter++] = $attachment;
                } else {
                    throw new FileNotReadable($attachment);
                }
            }
        }

        return $file_params;
    }
264955
protected function parse()
    {
        $formidable = $this;
        $generate = function() use ($formidable) {
            // Parses the contents
            $parser = $formidable->getFactory()->getParser($formidable->getContent());
            $formidable->isCached = false;

            return $parser;
        };

        if ($this->cache) {
            $formInfos = array(
                'path' => $this->path,
                'content' => $this->content
            );
            $cacheFile = sha1(serialize($formInfos));

            $conditions = array();
            if ($this->path !== null) {
                $conditions['younger-than'] = $this->path;
            }

            $cacheData = $this->cache->getOrCreate($cacheFile, $conditions, function($cacheFile) use ($generate) {
                $parserData = $generate();
                file_put_contents($cacheFile, serialize($parserData));
            });

            $this->originalParserData = unserialize($cacheData);
        } else {
            $this->originalParserData = $generate();
        }

        $this->reset();
    }
340643
public function parse()
    {
        $templates = array();
        $finder = new Finder(); 
        
        $directories = array(
            $this->templatesDir, 
        );
        
        $globalResourcesFolder = $this->kernelDir . '/Resources/views/' . $this->themeName;
        if (is_dir($globalResourcesFolder)) {
            $directories[] = $globalResourcesFolder;
        }
        
        $templateFiles = $finder->files('*.twig')->in($directories);
        foreach ($templateFiles as $template) {
            $template = (string)$template;
            $templateName = basename($template);
            $templateContents = file_get_contents($template);
            
            $slots = $this->fetchSlots($templateContents);
            if (empty($slots)) {
                continue;
            }
            
            $templates[$templateName]['assets']['external_stylesheets'] = $this->fetchExternalStylesheets($templateContents);
            $templates[$templateName]['assets']['external_javascripts'] = $this->fetchExternalJavascripts($templateContents);
            $templates[$templateName]['assets']['external_stylesheets_cms'] = $this->fetchExternalStylesheetsCms($templateContents);
            $templates[$templateName]['assets']['external_javascripts_cms'] = $this->fetchExternalJavascriptsCms($templateContents);
            $templates[$templateName]['slots'] = $slots;
            if (strpos($template, $this->kernelDir) === false) {
                $templates[$templateName]['generate_template'] = dirname($template) == $this->templatesDir;
            }
        }
        
        return $templates;
    }
952970
protected function processAttachments($data)
    {
        $idList = [];
        foreach ($data as $attachmentData) {
            $idList[] = $attachmentData['id'];

            // Find a matching attachment or create it
            $attachment = Attachment::get()->filter(['HailID' => $attachmentData['id']])->first();

            if (!$attachment) {
                $attachment = new Attachment();
            }
            $attachment->OrganisationID = $this->OrganisationID;
            $attachment->HailOrgID = $this->HailOrgID;
            $attachment->HailOrgName = $this->HailOrgName;

            // Update the Hail Attachments
            $attachment->importHailData($attachmentData);

            if (!$this->Attachments()->byID($attachment->ID)) {
                $this->Attachments()->add($attachment);
            }
        }

        // Remove old attachments that are currently assigned to this article,
        // but have not been returned this time around
        if ($idList) {
            $this->Attachments()->exclude('HailID', $idList)->removeAll();
        } else {
            // If there's no attachements, just remove everything.
            $this->Attachments()->removeAll();
        }
    }
11274
protected function parseTitle() {
    if (is_object($this->file)) {
      if (isset($this->file->title)) {
        return $this->file->title;
      }
    }

    if (is_array($this->file)) {
      if (isset($this->file['title'])) {
        return $this->file['title'];
      }
    }

    return '';
  }
60885
protected static function getAttachmentArray($attachment)
    {
        $default = array(
            'content_type'  => 'text/plain',    // 'application/octet-stream',
            'disposition'   => 'attachment',    // inline
            'body'          => '',
            'file'          => null,
            'filename'      => null,
            'content_id'    => null,
        );
        $eol = "\r\n";
        $attachment = array_merge($default, $attachment);
        if (isset($attachment['file'])) {
            $attachment['body'] = file_get_contents($attachment['file']);
            if (empty($attachment['filename'])) {
                $attachment['filename'] = basename($attachment['file']);
            }
            $attachment['content_transfer_encoding'] = 'base64';
            $attachment['body'] = chunk_split(base64_encode($attachment['body']), 68, $eol);
            $attachment['body'] = substr($a['body'], 0, -strlen($eol));  // remove final $eol
            $attachment['file'] = null;
        }
        return $attachment;
    }
1124532
public function attachmentTypes()
    {
        $attachableObjects = $this->attachableObjects();

        $out = [];

        if (!$attachableObjects) {
            return $out;
        }

        $i = 0;
        foreach ($attachableObjects as $attType => $attMeta) {
            $i++;
            $label = $attMeta['label'];

            $out[] = [
                'id'             => (isset($attMeta['att_id']) ? $attMeta['att_id'] : null),
                'ident'          => $this->createIdent($attType),
                'skipForm'       => $attMeta['skipForm'],
                'formIdent'      => $attMeta['formIdent'],
                'quickFormIdent' => $attMeta['quickFormIdent'],
                'hasFaIcon'      => !!$attMeta['faIcon'],
                'faIcon'         => $attMeta['faIcon'],
                'label'          => $label,
                'val'            => $attType,
                'locked'         => $attMeta['locked'],
                'active'         => ($i == 1)
            ];
        }

        return $out;
    }
980039
public function parse()
    {
        $result = [];

        $result['status'] = $this->service->getStatusCode();
        $this->setLogs($result['status'], 'status');

        // Format the head response.
        $result['headers'] = $this->service->getHeaders();
        if ($result['headers']) {
            $result['headers'] = $this->getResponseArray($result['headers'], 'headers');
        }
        $result['headersRaw'] = implode('; ', $result['headers']);
        $this->setLogs($result['headers'], 'headers');

        $result['bodySize'] = $this->service->getBody()->getSize();
        $this->setLogs($result['bodySize'], 'size');

        $result['bodyRaw'] = $this->service->getBody()->getContents();
        $this->setLogs($result['bodyRaw'], 'bodyRaw');

        // Format the body response.
        $responseExpectedFormat = trim(
            strtolower(isset($this->responseExpected->format) ? $this->responseExpected->format : 'auto')
        );
        // Move the expected format to the top of the detection array.
        if ('auto' !== $responseExpectedFormat) {
            $key = array_search($responseExpectedFormat, $this->contentTypes);
            if (false !== $key) {
                $this->contentTypes = array_flip(
                    array_merge([$this->contentTypes[$key] => '-'], array_flip($this->contentTypes))
                );
            }
        }
        $this->setLogs($responseExpectedFormat, 'format');
        $result['format'] = $responseExpectedFormat;

        // If auto mode, discern content type in a very forgiving manner from the header.
        if ('auto' === $result['format']) {
            foreach ($result['headers'] as $keaderType => $header) {
                if ('contenttype' === str_replace(['-', '_', ' '], '', strtolower($keaderType))) {
                    foreach ($this->contentTypes as $key => $contentType) {
                        if (strpos(strtolower($header), $contentType)) {
                            $result['format'] = $contentType;
                            // Move this type to the top of the detection array.
                            $this->contentTypes = array_flip(
                                array_merge([$this->contentTypes[$key] => '-'], array_flip($this->contentTypes))
                            );
                            $this->setLogs($contentType, 'headerFormat');
                            break;
                        }
                    }
                    break;
                }
            }
        }

        $result['body'] = [];
        // Attempt to parse with a proffered list of types.
        foreach ($this->contentTypes as $contentType) {
            $attemptBody = $this->getResponseArray($result['bodyRaw'], $contentType);
            if ($attemptBody) {
                $result['body']   = $attemptBody;
                $result['format'] = $contentType;
                $this->setLogs($contentType, 'bodyFormat');
                break;
            }
            $this->setLogs($contentType, 'bodyFormatsAttempted');
            // If in test-mode do not cycle through all types unless we are truly in auto mode.
            if (!$this->test && 'auto' === $responseExpectedFormat) {
                break;
            }
        }
        $this->setLogs($result['body'], 'body');

        $this->valid          = (bool) $result['status'];
        $this->responseActual = $result;

        return $this;
    }