예제 #1
0
 public function encrypt($string)
 {
     return $this->encryptor->encrypt($string);
 }
 /**
  * Process (encrypt/decrypt) entities fields
  *
  * @param Object $entity doctrine entity
  * @param Boolean $isEncryptOperation If true - encrypt, false - decrypt entity
  *
  * @throws \RuntimeException
  *
  * @return object|null
  */
 public function processFields($entity, $isEncryptOperation = true)
 {
     if (!empty($this->encryptor)) {
         //Check which operation to be used
         $encryptorMethod = $isEncryptOperation ? 'encrypt' : 'decrypt';
         //Get the real class, we don't want to use the proxy classes
         if (strstr(get_class($entity), "Proxies")) {
             $realClass = ClassUtils::getClass($entity);
         } else {
             $realClass = get_class($entity);
         }
         //Get ReflectionClass of our entity
         $reflectionClass = new ReflectionClass($realClass);
         $properties = $this->getClassProperties($realClass);
         //Foreach property in the reflection class
         foreach ($properties as $refProperty) {
             /**
              * If followed standards, method name is getPropertyName, the propertyName is lowerCamelCase
              * So just uppercase first character of the property, later on get and set{$methodName} wil be used
              */
             $methodName = ucfirst($refProperty->getName());
             /**
              * If property is an normal value and contains the Encrypt tag, lets encrypt/decrypt that property
              */
             if ($this->annReader->getPropertyAnnotation($refProperty, self::ENCRYPTED_ANN_NAME)) {
                 /**
                  * If it is public lets not use the getter/setter
                  */
                 if ($refProperty->isPublic()) {
                     $propName = $refProperty->getName();
                     $entity->{$propName} = $this->encryptor->{$encryptorMethod}($refProperty->getValue());
                 } else {
                     //If private or protected check if there is an getter/setter for the property, based on the $methodName
                     if ($reflectionClass->hasMethod($getter = 'get' . $methodName) && $reflectionClass->hasMethod($setter = 'set' . $methodName)) {
                         //Get the information (value) of the property
                         try {
                             $getInformation = $entity->{$getter}();
                         } catch (\Exception $e) {
                             $getInformation = null;
                         }
                         /**
                          * Then decrypt, encrypt the information if not empty, information is an string and the <ENC> tag is there (decrypt) or not (encrypt).
                          * The <ENC> will be added at the end of an encrypted string so it is marked as encrypted. Also protects against double encryption/decryption
                          */
                         if ($encryptorMethod == "decrypt") {
                             if (!is_null($getInformation) and !empty($getInformation)) {
                                 if (substr($getInformation, -5) == "<ENC>") {
                                     $this->decryptCounter++;
                                     $currentPropValue = $this->encryptor->decrypt(substr($getInformation, 0, -5));
                                     $entity->{$setter}($currentPropValue);
                                 }
                             }
                         } else {
                             if (!is_null($getInformation) and !empty($getInformation)) {
                                 if (substr($entity->{$getter}(), -5) != "<ENC>") {
                                     $this->encryptCounter++;
                                     $currentPropValue = $this->encryptor->encrypt($entity->{$getter}());
                                     $entity->{$setter}($currentPropValue);
                                 }
                             }
                         }
                     }
                 }
             }
         }
         return $entity;
     }
     return null;
 }