Erebot  latest
A modular IRC bot for PHP 7.0+
XGlobStream.php
1 <?php
2 /*
3  This file is part of Erebot, a modular IRC bot written in PHP.
4 
5  Copyright © 2010 François Poirotte
6 
7  Erebot is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Erebot is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Erebot. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 // @codingStandardsIgnoreFile
22 namespace Erebot;
23 
32 class XGlobStream extends \Erebot\StreamWrapperBase
33 {
35  public $context;
36 
38  protected $position;
39 
41  protected $content;
42 
44  const XMLNS = 'http://www.erebot.net/xmlns/xglob';
46  const TAG = 'wrapping';
47 
48  public function stream_tell()
49  {
50  return $this->position;
51  }
52 
53  public function stream_eof()
54  {
55  return ($this->position >= strlen($this->content));
56  }
57 
58  public function stream_close()
59  {
60  $this->position = 0;
61  $this->content = '';
62  }
63 
64  public function stream_open($path, $mode, $options, &$openedPath)
65  {
66  $this->position = 0;
67  $pos = strpos($path, '://');
68  if ($pos === false) {
69  return false;
70  }
71 
72  $path = substr($path, $pos + 3);
73 
74  if (strlen($path) < 1) {
75  return false;
76  }
77 
78  if (in_array($path[0], array("/", DIRECTORY_SEPARATOR))) {
79  $abs = '';
80  } elseif (!strncasecmp(PHP_OS, "Win", 3) && strlen($path) > 1 && $path[1] == ':') {
81  $abs = '';
82  } else {
83  $abs = getcwd() . DIRECTORY_SEPARATOR;
84  }
85 
86  $abs .= str_replace("/", DIRECTORY_SEPARATOR, $path);
87  $matches = glob($abs, 0);
88  if ($matches === false) {
89  $matches = array();
90  }
91 
92  $content = '<xglob:'.self::TAG.' xmlns:xglob="'.self::XMLNS.'">';
93  foreach ($matches as $absname) {
94  if (!is_file($absname)) {
95  continue;
96  }
97  $cnt = file_get_contents($absname);
98  if ($cnt !== false) {
99  $content .= $cnt;
100  }
101  }
102  $content .= '</xglob:'.self::TAG.'>';
103  $this->content = $content;
104  return true;
105  }
106 
107  public function stream_read($count)
108  {
109  $ret = substr($this->content, $this->position, $count);
110  $this->position += strlen($ret);
111  return $ret;
112  }
113 
114  public function stream_seek($offset, $whence)
115  {
116  switch ($whence) {
117  case SEEK_SET:
118  if ($offset < strlen($this->content) && $offset >= 0) {
119  $this->position = $offset;
120  return true;
121  }
122  return false;
123 
124  case SEEK_CUR:
125  if ($offset >= 0) {
126  $this->position += $offset;
127  return true;
128  }
129  return false;
130 
131  case SEEK_END:
132  $len = strlen($this->content);
133  if ($len + $offset >= 0) {
134  $this->position = $len + $offset;
135  return true;
136  }
137  return false;
138 
139  default:
140  return false;
141  }
142  }
143 }
A stream wrapper which performs a glob on files and returns the content of all matching files...
Definition: XGlobStream.php:32
$content
Content of the stream.
Definition: XGlobStream.php:41
$context
Stream context, set automatically by PHP.
Definition: XGlobStream.php:35
$position
Current position in the stream.
Definition: XGlobStream.php:38