Ejemplo n.º 1
0
 /**
  * Tries to add the database $kdbxfile to KeePassPHP, using the ID $dbid,
  * the internal password $internalpwd, and the master key composed of the
  * keys $keys. If the ID already exists, the corresponding database will
  * be overriden.
  * $kdbxfile should be the temporary filename of the file, as just uploaded
  * by PHP. KeePassPHP will perform itself move_uploaded_file. Likewise, the
  * key filenames in $keys should be the temporary filenames as just uploaded
  * by PHP, and will be moved by KeePassPHP.
  * The internal password is used to encrypt the internal data kept by
  * KeePassPHP, whereas the passwords in $keys are used to build the master
  * key to decrypt the KeePass database file. The internal password may be
  * part of the master key (this is even recommended for the sake of
  * simplicity).
  * @param string $kdbxfile The temporary filename of the KeePass database.
  * @param string $dbid The ID to use.
  * @param string $internalpwd The internal password.
  * @param array $keys The keys composing the master key of the database.
  * @return boolean Returns true in case of success, false otherwise.
  */
 public static function tryAdd($kdbxfile, $dbid, $internalpwd, array $keys)
 {
     if (!self::$started) {
         self::raiseError("KeepassPHP is not started !");
         return false;
     }
     $nkeys = array();
     foreach ($keys as $k) {
         if ($k[0] == self::KEY_PWD) {
             $nkeys[] = array(self::KEY_PWD);
         } elseif ($k[0] == self::KEY_FILE) {
             $h = KeePassPHP::addKeyFile($k[1]);
             if ($h == null) {
                 self::raiseError("Key file upload failed.");
                 return false;
             }
             $nkeys[] = array(KeePassPHP::KEY_FILE, $h);
         }
     }
     $hashname = KeePassPHP::addKdbxFile($kdbxfile);
     if ($hashname == null) {
         self::raiseError("Database file upload failed.");
         return false;
     }
     if (KeePassPHP::addInternal($dbid, $internalpwd, $hashname, $nkeys, array(), true) == null) {
         self::raiseError("Internal database write failed.");
         return false;
     }
     return true;
 }