scdevsumit 2016 - become a jedi with php streams

64
BECOME A JEDI WITH PHP STREAMS

Upload: matheus-marabesi

Post on 21-Apr-2017

1.442 views

Category:

Internet


3 download

TRANSCRIPT

Page 1: scdevsumit 2016 - Become a jedi with php streams

BECOME A JEDI WITH PHP STREAMS

Page 2: scdevsumit 2016 - Become a jedi with php streams

marabesi

@MatheusMarabesi

Page 3: scdevsumit 2016 - Become a jedi with php streams
Page 4: scdevsumit 2016 - Become a jedi with php streams
Page 5: scdevsumit 2016 - Become a jedi with php streams

ZENDSC10X

10/jun

Page 6: scdevsumit 2016 - Become a jedi with php streams
Page 7: scdevsumit 2016 - Become a jedi with php streams

WHAT IS A STREAM ?

Page 8: scdevsumit 2016 - Become a jedi with php streams

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

Page 9: scdevsumit 2016 - Become a jedi with php streams
Page 10: scdevsumit 2016 - Become a jedi with php streams

??????

Page 11: scdevsumit 2016 - Become a jedi with php streams

DATA FLOW

Page 12: scdevsumit 2016 - Become a jedi with php streams

INPUT

OUTPUT

DATA

Page 13: scdevsumit 2016 - Become a jedi with php streams

PHP

Page 14: scdevsumit 2016 - Become a jedi with php streams

F* FILE*

Page 15: scdevsumit 2016 - Become a jedi with php streams

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

Page 16: scdevsumit 2016 - Become a jedi with php streams

fopenfpassthrufputcsvfputsfreadfscanffseekfstatftellftruncatefwrite

file_ existsfile_ get_ contentsfile_ put_ contentsfilefileatimefilectimefilegroupfileinodefilemtimefileownerfileperms

RESOURCE FILE PATH

Page 17: scdevsumit 2016 - Become a jedi with php streams

fopen(‘file.txt’, ‘r+’);file_ get_ contents( ‘file.txt’);

Page 18: scdevsumit 2016 - Become a jedi with php streams

WRAPPERS

Page 19: scdevsumit 2016 - Become a jedi with php streams

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

Page 20: scdevsumit 2016 - Become a jedi with php streams

1) file://

Page 21: scdevsumit 2016 - Become a jedi with php streams

fopen( ‘file.txt’, ‘r+’);

fopen( ‘file://file.txt’, ‘r+’);

!= ?

Page 22: scdevsumit 2016 - Become a jedi with php streams

2) http://

Page 23: scdevsumit 2016 - Become a jedi with php streams

print file_get_contents(

'http://marabesi.

com'

);

Page 24: scdevsumit 2016 - Become a jedi with php streams
Page 25: scdevsumit 2016 - Become a jedi with php streams

$contexto = stream_context_create([

'http' => [

'method' => 'POST'

'header' => 'Content-Type:

application/x-www-form-urlencoded',

'content' => 'livro=php'

]

]);

Page 26: scdevsumit 2016 - Become a jedi with php streams

print file_get_contents(

http://marabesi.com',

false,

$context

);

Page 27: scdevsumit 2016 - Become a jedi with php streams

$fp = fopen(

'http://marabesi.com',

'r',

false,

$context

);

print fgets($fp);

Page 28: scdevsumit 2016 - Become a jedi with php streams

http://php.net/manual/en/context.php

Page 29: scdevsumit 2016 - Become a jedi with php streams

3) ftp://

Page 30: scdevsumit 2016 - Become a jedi with php streams

$fp = fopen(

'ftp://usuario:senha' .

'@meu_servidor.com' .

'/home/matheusmarabesi/index.php',

'r'

);

Page 31: scdevsumit 2016 - Become a jedi with php streams
Page 32: scdevsumit 2016 - Become a jedi with php streams

4) php://

Page 33: scdevsumit 2016 - Become a jedi with php streams

$put = fopen(

'php://stdin, 'r'

);

print fgets($put);

Page 34: scdevsumit 2016 - Become a jedi with php streams
Page 35: scdevsumit 2016 - Become a jedi with php streams

5) zlib://

Page 36: scdevsumit 2016 - Become a jedi with php streams

$arquivoComprimido = fopen('compress.zlib:

//arquivo.tar.gz', 'r');

while(feof($arquivoComprimido) !== true) {

print fgets($compressedFile);

}

Page 37: scdevsumit 2016 - Become a jedi with php streams

file_put_contents(

'compress.zlib:///var/www/arquivo.txt.gz',

'Vou ser comprimido!'

);

Page 38: scdevsumit 2016 - Become a jedi with php streams

arquivo.

txt000077700000000000000

000000000211257332276101

1244 0ustar

rootrootmatheusmarabesi

Page 39: scdevsumit 2016 - Become a jedi with php streams

file_put_contents(

'compress.zlib:///var/www/arquivo.txt.gz',

'Vou ser comprimido!'

);

Page 40: scdevsumit 2016 - Become a jedi with php streams

6) data://

Page 41: scdevsumit 2016 - Become a jedi with php streams

print file_get_contents(

'data://text/plain, Utilizando o

wrapper data://'

);

Page 42: scdevsumit 2016 - Become a jedi with php streams

print file_get_contents(

'data://text/plain, Utilizando o

wrapper data://'

);

Page 43: scdevsumit 2016 - Become a jedi with php streams

<img src="data:

image/jpeg;base64,

YXNodXNhdXNhdWlzYXVzYWl1"

/>

Page 44: scdevsumit 2016 - Become a jedi with php streams

print file_get_contents( 'data:

//text/plain;base64,

VXRpbGl6YW5kbyBzdHJlYW1zIGVtIFBIUCAh');

Page 45: scdevsumit 2016 - Become a jedi with php streams

7) glob://

Page 46: scdevsumit 2016 - Become a jedi with php streams
Page 47: scdevsumit 2016 - Become a jedi with php streams

$diretorio = new

\RecursiveDirectoryIterator(

'/var/www'

);

1

Page 48: scdevsumit 2016 - Become a jedi with php streams

$iterator = new

\RecursiveIteratorIterator(

$diretorio

);

2

Page 49: scdevsumit 2016 - Become a jedi with php streams

$arquivos = new \RegexIterator(

$iterator, '/^.+\.php/',

\RecursiveRegexIterator::GET_MATCH

);

3

Page 50: scdevsumit 2016 - Become a jedi with php streams
Page 51: scdevsumit 2016 - Become a jedi with php streams

$diretorio = new

\RecursiveDirectoryIterator(

'glob://var/www/*.php'

);

foreach ($diretorio as $arquivos) {

print $arquivos->getFilename();

}

Page 52: scdevsumit 2016 - Become a jedi with php streams

8) ogg://

Page 53: scdevsumit 2016 - Become a jedi with php streams
Page 54: scdevsumit 2016 - Become a jedi with php streams
Page 55: scdevsumit 2016 - Become a jedi with php streams

2) ogg://

Page 56: scdevsumit 2016 - Become a jedi with php streams

9) mandinga://

Page 57: scdevsumit 2016 - Become a jedi with php streams

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

Page 58: scdevsumit 2016 - Become a jedi with php streams

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;

}

}

Page 59: scdevsumit 2016 - Become a jedi with php streams

public function stream_read($bytes)

{

return fread($this->file,

$bytes);

}

public function stream_eof()

{

return feof($this->file);

}

Page 60: scdevsumit 2016 - Become a jedi with php streams

stream_register_wrapper

stream_wrapper_register

!= ?

Page 61: scdevsumit 2016 - Become a jedi with php streams

stream_register_wrapper(

'mandinga', 'Mandinga'

);

Page 62: scdevsumit 2016 - Become a jedi with php streams

stream_register_wrapper(

'mandinga', 'Mandinga'

);

ALIAS

CLASS NAME

Page 63: scdevsumit 2016 - Become a jedi with php streams

print file_get_contents(

mandinga://pessoas.txt'

);

Page 64: scdevsumit 2016 - Become a jedi with php streams

marabesi

@MatheusMarabesi