Altax\Module\Server\Resource\Node::getKeyOrDefault PHP Method

getKeyOrDefault() public method

public getKeyOrDefault ( )
    public function getKeyOrDefault()
    {
        $key = $this->key ? $this->key : $this->getDefaultKey();
        if (strpos($key, "~") !== false) {
            // replace ~ to home directory
            $key = preg_replace_callback('/^~(?:\\/|$)/', function ($m) {
                return str_replace('~', Env::get("homedir"), $m[0]);
            }, $key);
        }
        return $key;
    }

Usage Example

Ejemplo n.º 1
0
 public function testReplaceTilda()
 {
     $node = new Node();
     Env::set("homedir", "/home/your");
     $node->setKey("~/path/to/private_key");
     $this->assertEquals("~/path/to/private_key", $node->getKey());
     $this->assertEquals("/home/your/path/to/private_key", $node->getKeyOrDefault());
     $node->setKey("~user/path/to/private_key");
     $this->assertEquals("~user/path/to/private_key", $node->getKey());
     $this->assertEquals("~user/path/to/private_key", $node->getKeyOrDefault());
     $node->setKey("~");
     $this->assertEquals("~", $node->getKey());
     $this->assertEquals("/home/your", $node->getKeyOrDefault());
     Env::set("homedir", "/home/your\\0");
     $node->setKey("~/path/to/private_key");
     $this->assertEquals("~/path/to/private_key", $node->getKey());
     $this->assertEquals("/home/your\\0/path/to/private_key", $node->getKeyOrDefault());
     $node->setKey("/path/to/private_key~");
     $this->assertEquals("/path/to/private_key~", $node->getKey());
     $this->assertEquals("/path/to/private_key~", $node->getKeyOrDefault());
 }