pssht  latest
SSH server library written in PHP
File.php
1 <?php
2 
3 /*
4 * This file is part of pssht.
5 *
6 * (c) François Poirotte <clicky@erebot.net>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
13 
16 
20 class File
21 {
23  protected $store;
24 
33  public function __construct(\fpoirotte\Pssht\KeyStore $store = null)
34  {
35  if ($store === null) {
36  $store = new \fpoirotte\Pssht\KeyStore();
37  }
38 
39  $this->store = $store;
40  }
41 
57  public function load($user, $file)
58  {
59  if (!is_string($user)) {
60  throw new \InvalidArgumentException();
61  }
62 
63  if (!is_string($file)) {
64  throw new \InvalidArgumentException();
65  }
66 
67  $logging = \Plop\Plop::getInstance();
68 
69  $lines = file($file);
70  if ($lines === false) {
71  $logging->debug(
72  'Ignoring unreadable file "%(file)s"',
73  array(
74  'user' => $user,
75  'file' => $file,
76  )
77  );
78  return $this;
79  }
80 
81  $count = 0;
82  try {
83  $this->store->add($user, Putty::loadPublic(implode('', $lines)));
84  $count++;
85  } catch (\InvalidArgumentException $e) {
86  foreach ($lines as $line) {
87  // Ignore empty lines and lines
88  // starting with '#' (comments).
89  if (trim($line) === '' || $line[0] === '#') {
90  continue;
91  }
92  $this->store->add($user, Openssh::loadPublic(rtrim($line)));
93  $count++;
94  }
95  }
96  $logging->debug(
97  'Imported %(count)d identities for "%(user)s" from "%(file)s"',
98  array(
99  'count' => $count,
100  'user' => $user,
101  'file' => $file,
102  )
103  );
104  return $this;
105  }
106 
119  public function loadBulk(array $bulk)
120  {
121  foreach ($bulk as $user => $files) {
122  if (!is_string($user)) {
123  throw new \InvalidArgumentException();
124  }
125 
126  if (!is_array($files) && !is_string($files)) {
127  throw new \InvalidArgumentException();
128  }
129 
130  $files = (array) $files;
131  foreach ($files as $file) {
132  $this->load($user, $file);
133  }
134  }
135  return $this;
136  }
137 
144  public function getStore()
145  {
146  return $this->store;
147  }
148 }
__construct(\fpoirotte\Pssht\KeyStore $store=null)
Definition: File.php:33
$store
Storage object for the keys.
Definition: File.php:23