<?php

// Service name for custom data processing:
// Custom\multidomain\CoreServiceJobReceiver
// Install location:
// "c:\Program Files (x86)\MyQ\PhpApps\PrintServer\Custom\multidomain\CoreServiceJobReceiver.php"
namespace Custom\multidomain;
 
use Wsf\Platform\Plugin\Services\AbstractService;
 
class CoreServiceJobReceiver extends AbstractService
{
    // User identifier types
    const UIT_USERNAME = 1;
    const UIT_EMAIL = 4;
  
    static function getServiceInfo()
    {
        return ['interfaces' => ['JobReceiver'], 'reference' => __CLASS__];
    }
  
    public function onBeforeProcessUserHost(array $params)
    {
        $header = base64_decode($params['jobHeader']);
        $userIdentifier = $params['userIdentifier'];
        $userIdentifierType = $params['userIdentifierType'];
        $hostname = $params['hostname'];
    
        // Start of the actual logic
		//dla HP
        if (preg_match('/@PJL\s{1,}SET\s{1,}JOBATTR=\"JobAcct3=([^"]+)\"/i', $header, $matches)) {
            $userIdentifierType = self::UIT_USERNAME;
            $userdomain = $matches[1];

            // Add @
            if( $userdomain == "PGN" ){
                $userdomain = "pgn.corp";
                $userIdentifier .= "@pgn.corp";
            }
            
        }
		
		//dla Kyocera
		if (preg_match('/@PJL\s{1,}COMMENT=\"USERDOMAIN:([^"]+);\"/i', $header, $matches)) {
            $userIdentifierType = self::UIT_USERNAME;
            $userdomain = $matches[1];

            // Add @
            if( $userdomain == "PGN.CORP" ){
                $userdomain = "pgn.corp";
                $userIdentifier .= "@pgn.corp";
            }
            
        }
		
		
		
        // End of the actual logic
        return [
            'userIdentifier' => $userIdentifier,
            'userIdentifierType' => $userIdentifierType,
            'hostname' => $hostname,
	    'domain' => $userdomain,
	    'header' => $header,
        ];
    }
}

