scdevsumit 2016 - become a jedi with php streams
Post on 10-Jan-2017
1.317 Views
Preview:
TRANSCRIPT
BECOME A JEDI WITH PHP STREAMS
marabesi
@MatheusMarabesi
ZENDSC10X
10/jun
WHAT IS A STREAM ?
A stream is a body of water[1] with a current, confined within a bed and stream banks. Depending on its location or certain characteristics, a stream may be referred to as a branch, brook, beck, burn, creek, crick, gill (occasionally ghyll), kill, lick, mill race, rill, river, syke, bayou, rivulet, streamage, wash, run, or runnel.
https://en.wikipedia.org/wiki/Stream
??????
DATA FLOW
INPUT
OUTPUT
DATA
PHP
F* FILE*
fopen
fpassthru
fputcsv
fputs
fread
fscanf
fseek
fstat
ftell
ftruncate
fwrite
file_ exists
file_ get_ contents
file_ put_ contents
file
fileatime
filectime
filegroup
fileinode
filemtime
fileowner
fileperms
fopenfpassthrufputcsvfputsfreadfscanffseekfstatftellftruncatefwrite
file_ existsfile_ get_ contentsfile_ put_ contentsfilefileatimefilectimefilegroupfileinodefilemtimefileownerfileperms
RESOURCE FILE PATH
fopen(‘file.txt’, ‘r+’);file_ get_ contents( ‘file.txt’);
WRAPPERS
file:// — Accessing local filesystem
http:// — Accessing HTTP(s) URLs
ftp:// — Accessing FTP(s) URLs
php:// — Accessing various I/O streams
zlib:// — Compression Streams
data:// — Data (RFC 2397)
glob:// — Find pathnames matching pattern
phar:// — PHP Archive
ssh2:// — Secure Shell 2
rar:// — RAR
ogg:// — Audio streams
1) file://
fopen( ‘file.txt’, ‘r+’);
fopen( ‘file://file.txt’, ‘r+’);
!= ?
2) http://
print file_get_contents(
'http://marabesi.
com'
);
$contexto = stream_context_create([
'http' => [
'method' => 'POST'
'header' => 'Content-Type:
application/x-www-form-urlencoded',
'content' => 'livro=php'
]
]);
print file_get_contents(
http://marabesi.com',
false,
$context
);
$fp = fopen(
'http://marabesi.com',
'r',
false,
$context
);
print fgets($fp);
http://php.net/manual/en/context.php
3) ftp://
$fp = fopen(
'ftp://usuario:senha' .
'@meu_servidor.com' .
'/home/matheusmarabesi/index.php',
'r'
);
4) php://
$put = fopen(
'php://stdin, 'r'
);
print fgets($put);
5) zlib://
$arquivoComprimido = fopen('compress.zlib:
//arquivo.tar.gz', 'r');
while(feof($arquivoComprimido) !== true) {
print fgets($compressedFile);
}
file_put_contents(
'compress.zlib:///var/www/arquivo.txt.gz',
'Vou ser comprimido!'
);
arquivo.
txt000077700000000000000
000000000211257332276101
1244 0ustar
rootrootmatheusmarabesi
file_put_contents(
'compress.zlib:///var/www/arquivo.txt.gz',
'Vou ser comprimido!'
);
6) data://
print file_get_contents(
'data://text/plain, Utilizando o
wrapper data://'
);
print file_get_contents(
'data://text/plain, Utilizando o
wrapper data://'
);
<img src="data:
image/jpeg;base64,
YXNodXNhdXNhdWlzYXVzYWl1"
/>
print file_get_contents( 'data:
//text/plain;base64,
VXRpbGl6YW5kbyBzdHJlYW1zIGVtIFBIUCAh');
7) glob://
$diretorio = new
\RecursiveDirectoryIterator(
'/var/www'
);
1
$iterator = new
\RecursiveIteratorIterator(
$diretorio
);
2
$arquivos = new \RegexIterator(
$iterator, '/^.+\.php/',
\RecursiveRegexIterator::GET_MATCH
);
3
$diretorio = new
\RecursiveDirectoryIterator(
'glob://var/www/*.php'
);
foreach ($diretorio as $arquivos) {
print $arquivos->getFilename();
}
8) ogg://
2) ogg://
9) mandinga://
streamWrapper {
public resource $context ;
__construct ( void )
__destruct ( void )
public bool dir_closedir ( void )
public bool dir_opendir ( string $path , int $options )
public string dir_readdir ( void )
public bool dir_rewinddir ( void )
public bool mkdir ( string $path , int $mode , int $options )
public bool rename ( string $path_from , string $path_to )
public bool rmdir ( string $path , int $options )
public resource stream_cast ( int $cast_as )
public void stream_close ( void )
public bool stream_eof ( void )
public bool stream_flush ( void )
public bool stream_lock ( int $operation )
public bool stream_metadata ( string $path , int $option , mixed $value )
public bool stream_open ( string $path , string $mode , int $options , string &$opened_path
)
public string stream_read ( int $count )
public bool stream_seek ( int $offset , int $whence = SEEK_SET )
public bool stream_set_option ( int $option , int $arg1 , int $arg2 )
public array stream_stat ( void )
THERE IS MORE
class Mandinga {
private $arquivo;
public function stream_open($arquivo, $modo)
{
if (!file_exists($arquivo)) {
throw new \Exception(
'O arquivo informado não existe'
);
$this->arquivo = fopen($arquivo, $modo);
return true;
}
}
public function stream_read($bytes)
{
return fread($this->file,
$bytes);
}
public function stream_eof()
{
return feof($this->file);
}
stream_register_wrapper
stream_wrapper_register
!= ?
stream_register_wrapper(
'mandinga', 'Mandinga'
);
stream_register_wrapper(
'mandinga', 'Mandinga'
);
ALIAS
CLASS NAME
print file_get_contents(
mandinga://pessoas.txt'
);
marabesi
@MatheusMarabesi
top related