| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 | <?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Api\ApiController;
use App\Http\Services\Api\SeminarSignUpService;
use App\Http\Requests\Api\SeminarSignUp\StoreRequest;
use Log;
class SeminarSignUpController extends ApiController
{
    
    private $seminarSignUpSv;
    
    public function __construct()
    {
        $this->seminarSignUpSv = new SeminarSignUpService();
    }
    // save data to db
    public function insertData(StoreRequest $request)
    {
        \Log::info(print_r($request->all(),true));
        $trackNo = $request->input('trackNo', '');
        $firstName = $this->safeEncrypt($request->input('firstName', ''), 'arm');
        $lastName = $this->safeEncrypt($request->input('lastName', ''), 'arm');
        $companyName = $this->safeEncrypt($request->input('companyName', ''), 'arm');
        $companyEmail = $this->safeEncrypt($request->input('companyEmail', ''), 'arm');
        
        $backupEmail = $request->input('backupEmail', '');
        if (!is_null($backupEmail)) {
            $backupEmail = $this->safeEncrypt($backupEmail, 'arm');
        }
        $phoneNumber = $this->safeEncrypt($request->input('phoneNumber', ''), 'arm');
        $country = $request->input('country', '');
        $registeredSession = $request->input('registeredSession', '');
        $lunchOptions = $request->input('lunchOptions', '');
        $typeOfIndustry = $request->input('typeOfIndustry', '');
        $typeOfJob = $request->input('typeOfJob', '');
        $jobTitle = $request->input('jobTitle', '');
        $trackOfInterest = $request->input('trackOfInterest', '');
        $areaOfInterest = $request->input('areaOfInterest', '');
        $howToKnowAboutTheEvent = $request->input('howToKnowAboutTheEvent', '');
        $consentAcceptEmail = $request->input('consentAcceptEmail', '');
        $consentPrivacyPolicy = $request->input('consentPrivacyPolicy', '');
        
        $overOrNot = $this->seminarSignUpSv->overLimitOrNot($trackNo);
        \Log::info($overOrNot);
        if ($overOrNot) {
            $this->seminarSignUpSv->insertData(
                $firstName, 
                $lastName, 
                $companyName, 
                $companyEmail, 
                $backupEmail, 
                $phoneNumber, 
                $country, 
                $trackNo, 
                $registeredSession,
                $lunchOptions,
                $typeOfIndustry,
                $typeOfJob,
                $jobTitle,
                $trackOfInterest,
                $areaOfInterest,
                $howToKnowAboutTheEvent,
                $consentAcceptEmail,
                $consentPrivacyPolicy,
            );
            $res = '報名成功';
        } else {
            $res = '已達報名上限';
        }
        $data = [
            'res' => $res,
        ];
        \Log::info(print_r($res,true));
        
        return $this->apiResponse($data);
    }
    public function getData()
    {
        
        $list = $this->seminarSignUpSv->getData();
        
        $data = [
            'list' => $list
        ];
        
        return $this->apiResponse($data);
    }
    /**
     * 參數加解密模組: 加密部分,建議使用環境變數中的 secret key 作加解密種子
     */
    public function safeEncrypt(string $message, string $skey): string
    {
        // if (mb_strlen($key, '8bit') !== SODIUM_CRYPTO_SECRETBOX_KEYBYTES) {
        //     throw new RangeException('Key is not the correct size (must be 32 bytes).');
        // }
        // $nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
        
        // $cipher = base64_encode(
        //     $nonce .
        //     sodium_crypto_secretbox(
        //         $message,
        //         $nonce,
        //         $key
        //     )
        // );
        // sodium_memzero($message);
        // sodium_memzero($key);
        // return $cipher;
        $strArr = str_split(base64_encode($message));
        $strCount = count($strArr);
        foreach (str_split($skey) as $key => $value)
        $key < $strCount && $strArr[$key].=$value;
        return str_replace(array('=', ' ', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
    }
    
    /**
     * 參數加解密模組: 解密部分,建議使用環境變數中的 secret key 作加解密種子
     */
    public function safeDecrypt(string $encrypted, string $skey): string
    {
        // $decoded = base64_decode($encrypted);
        // $nonce = mb_substr($decoded, 0, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, '8bit');
        // $ciphertext = mb_substr($decoded, SODIUM_CRYPTO_SECRETBOX_NONCEBYTES, null, '8bit');
        
        // $plain = sodium_crypto_secretbox_open(
        //     $ciphertext,
        //     $nonce,
        //     $key
        // );
        // if (!is_string($plain)) {
        //     throw new Exception('Invalid MAC');
        // }
        // sodium_memzero($ciphertext);
        // sodium_memzero($key);
        
        // return $plain;
        $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', ' ', '/'), $encrypted), 2);
        $strCount = count($strArr);
        foreach (str_split($skey) as $key => $value)
        $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
        return base64_decode(join('', $strArr));
    }
    
}
 |