Geek magazine hacker daily blog

7 years, 3 months ago
Only one thing — stupid FTP of some hosters induced to write this code me.
No, files are uploaded with maximum speed, but here between the end of loading of one file and the beginning of loading of the following passes seconds 30.
As I suffer from a crap now I impose and I put dzhomla in shock quantities, loading of 5000+ files leads to a reading of a habra and a guglrider to holes that however too is unhealthy.
With the purpose to correct siyo annoying omission it is also written small nakolenochny skriptik.



Skriptik as it is known piece such that him not only on the server, but also from the console it is possible to start, so it is necessary to provide operability of both options.
For determination of the console isset ($argv) is used
Actually code of packaging and unpacking is not interesting.
And here on what I would like to pay attention of %username of % so to that how exactly the packer is organized.
For this purpose I tell about one function and one constant about which not only school students and Hindus do not know.

It is function __ compiler_halt and a constant __ COMPILER_HALT_OFFSET __, both are entered into PHP since 5.1
When parsing the file if php comes across on __ compiler_halt (); that it completes parsing and exposes in __ COMPILER_HALT_OFFSET __ number of the byte following a semicolon after function name
It is important to understand that any?> it is not required after this construction any more
What does it give us?
And it gives us the chance to store any rubbish random data in our php a code
And these data very easily are read out: file_get_contents (__FILE__,null,null,__COMPILER_HALT_OFFSET__);
If it to use the file it turns out broken into 2 sections — section of a code and section of data

The packer contains in code section a packaging code, and in section of data an unpacking code
Raspakovshchik in section of a code supports himself favourite, and in section of data — zip archive with data

Without bothering with further verbalizations I give a code:
<font color="#000000"> <font color="#0000BB"><?php<br /> <br /> $packname </font><font color="#007700">= </font><font color="#0000BB">getcwd</font><font color="#007700">().</font><font color="#DD0000">'/'</font><font color="#007700">.</font><font color="#0000BB">basename</font><font color="#007700">(</font><font color="#0000BB">__FILE__</font><font color="#007700">,</font><font color="#DD0000">'.php'</font><font color="#007700">).</font><font color="#DD0000">'.packed.php'</font><font color="#007700">;<br /> </font><font color="#0000BB">$zip </font><font color="#007700">= new </font><font color="#0000BB">ZipArchive</font><font color="#007700">();<br /> </font><font color="#0000BB">$zip</font><font color="#007700">-></font><font color="#0000BB">open</font><font color="#007700">(</font><font color="#0000BB">$packname</font><font color="#007700">, </font><font color="#0000BB">ZIPARCHIVE</font><font color="#007700">::</font><font color="#0000BB">CREATE</font><font color="#007700">|</font><font color="#0000BB">ZIPARCHIVE</font><font color="#007700">::</font><font color="#0000BB">OVERWRITE</font><font color="#007700">);<br /> </font><font color="#0000BB">$iterator </font><font color="#007700">= new </font><font color="#0000BB">RecursiveIteratorIterator</font><font color="#007700">(new </font><font color="#0000BB">RecursiveDirectoryIterator</font><font color="#007700">(</font><font color="#DD0000">'./'</font><font color="#007700">));<br /> </font><font color="#0000BB">$packer </font><font color="#007700">= </font><font color="#0000BB">realpath</font><font color="#007700">(</font><font color="#0000BB">__FILE__</font><font color="#007700">);<br /> foreach(</font><font color="#0000BB">$iterator </font><font color="#007700">as </font><font color="#0000BB">$key</font><font color="#007700">=></font><font color="#0000BB">$value</font><font color="#007700">)<br /> {<br />     </font><font color="#0000BB">$file </font><font color="#007700">= </font><font color="#0000BB">realpath</font><font color="#007700">(</font><font color="#0000BB">$key</font><font color="#007700">);<br />     if(</font><font color="#0000BB">$file</font><font color="#007700">!=</font><font color="#0000BB">$packer</font><font color="#007700">)<br />     </font><font color="#0000BB">$zip</font><font color="#007700">-></font><font color="#0000BB">addFile</font><font color="#007700">(</font><font color="#0000BB">$file</font><font color="#007700">,</font><font color="#0000BB">$key</font><font color="#007700">);<br /> }<br /> </font><font color="#0000BB">$zip</font><font color="#007700">-></font><font color="#0000BB">close</font><font color="#007700">();<br /> <br /> </font><font color="#0000BB">$data </font><font color="#007700">= </font><font color="#0000BB">file_get_contents</font><font color="#007700">(</font><font color="#0000BB">__FILE__</font><font color="#007700">,</font><font color="#0000BB">null</font><font color="#007700">,</font><font color="#0000BB">null</font><font color="#007700">,</font><font color="#0000BB">__COMPILER_HALT_OFFSET__</font><font color="#007700">);<br /> </font><font color="#0000BB">$zipped </font><font color="#007700">= @</font><font color="#0000BB">file_get_contents</font><font color="#007700">(</font><font color="#0000BB">$packname</font><font color="#007700">);<br /> if(</font><font color="#0000BB">$zipped</font><font color="#007700">==</font><font color="#DD0000">''</font><font color="#007700">) die();<br /> </font><font color="#0000BB">file_put_contents</font><font color="#007700">(</font><font color="#0000BB">$packname</font><font color="#007700">,</font><font color="#0000BB">$data</font><font color="#007700">.</font><font color="#0000BB">$zipped</font><font color="#007700">);<br /> <br /> __halt_compiler();<?</font><font color="#0000BB">php<br /> $mode </font><font color="#007700">= </font><font color="#DD0000">''</font><font color="#007700">;<br /> if(isset(</font><font color="#0000BB">$argv</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">]))<br /> {<br />     </font><font color="#0000BB">$mode </font><font color="#007700">= </font><font color="#0000BB">$argv</font><font color="#007700">[</font><font color="#0000BB">1</font><font color="#007700">];<br /> }<br /> if(isset(</font><font color="#0000BB">$_REQUEST</font><font color="#007700">[</font><font color="#DD0000">'mode'</font><font color="#007700">]))<br /> {<br />     </font><font color="#0000BB">$mode </font><font color="#007700">= </font><font color="#0000BB">$_REQUEST</font><font color="#007700">[</font><font color="#DD0000">'mode'</font><font color="#007700">];<br /> }<br /> function </font><font color="#0000BB">extract_archive</font><font color="#007700">()<br /> {<br />     </font><font color="#0000BB">file_put_contents</font><font color="#007700">(</font><font color="#0000BB">getcwd</font><font color="#007700">().</font><font color="#DD0000">'/'</font><font color="#007700">.</font><font color="#0000BB">basename</font><font color="#007700">(</font><font color="#0000BB">__FILE__</font><font color="#007700">).</font><font color="#DD0000">'.zip'</font><font color="#007700">,</font><font color="#0000BB">file_get_contents</font><font color="#007700">(</font><font color="#0000BB">__FILE__</font><font color="#007700">,</font><font color="#0000BB">null</font><font color="#007700">,</font><font color="#0000BB">null</font><font color="#007700">,</font><font color="#0000BB">__COMPILER_HALT_OFFSET__</font><font color="#007700">));<br /> }<br /> </font><font color="#0000BB">$file </font><font color="#007700">= </font><font color="#0000BB">basename</font><font color="#007700">(</font><font color="#0000BB">__FILE__</font><font color="#007700">);<br /> if(!isset(</font><font color="#0000BB">$argv</font><font color="#007700">))<br /> echo <<<HEREDOC<br /> </font><font color="#0000BB"><html><br /> <head><br /> <title>Упакованный архив $file</title><br /> </head><br /> <body style="font-size:24px;"><br /> </font><font color="#007700">HEREDOC;<br /> switch(</font><font color="#0000BB">$mode</font><font color="#007700">)<br /> {<br />     case </font><font color="#DD0000">'extract'</font><font color="#007700">:<br />     </font><font color="#0000BB">extract_archive</font><font color="#007700">();<br />     echo </font><font color="#DD0000">"Извлечён\n"</font><font color="#007700">;<br />     break;<br />     case </font><font color="#DD0000">'unpack'</font><font color="#007700">:<br />     </font><font color="#0000BB">extract_archive</font><font color="#007700">();<br />     </font><font color="#0000BB">$zip </font><font color="#007700">= new </font><font color="#0000BB">ZipArchive</font><font color="#007700">;<br />     </font><font color="#0000BB">$zip</font><font color="#007700">-></font><font color="#0000BB">open</font><font color="#007700">(</font><font color="#0000BB">getcwd</font><font color="#007700">().</font><font color="#DD0000">'/'</font><font color="#007700">.</font><font color="#0000BB">basename</font><font color="#007700">(</font><font color="#0000BB">__FILE__</font><font color="#007700">).</font><font color="#DD0000">'.zip'</font><font color="#007700">);<br />     </font><font color="#0000BB">$zip</font><font color="#007700">-></font><font color="#0000BB">extractTo</font><font color="#007700">(</font><font color="#DD0000">'./'</font><font color="#007700">);<br />     </font><font color="#0000BB">$zip</font><font color="#007700">-></font><font color="#0000BB">close</font><font color="#007700">();<br />     </font><font color="#0000BB">unlink</font><font color="#007700">(</font><font color="#0000BB">getcwd</font><font color="#007700">().</font><font color="#DD0000">'/'</font><font color="#007700">.</font><font color="#0000BB">basename</font><font color="#007700">(</font><font color="#0000BB">__FILE__</font><font color="#007700">).</font><font color="#DD0000">'.zip'</font><font color="#007700">);<br />     </font><font color="#0000BB">unlink</font><font color="#007700">(</font><font color="#0000BB">__FILE__</font><font color="#007700">);<br />     echo </font><font color="#DD0000">"Распакован\n"</font><font color="#007700">;<br />     break;<br />     default:<br />     <br />     if(isset(</font><font color="#0000BB">$argv</font><font color="#007700">))<br />     {<br />     echo <<<HEREDOC<br /> </font><font color="#0000BB">Extract me with<br />     php $file extract<br /> Unpack me with<br />     php $file unpack<br /> <br /> </font><font color="#007700">HEREDOC;<br />     }<br />     else<br />     {<br />         echo </font><font color="#DD0000">'<a href="?mode=unpack">Распаковать архив</a><br><a href="?mode=extract">Извлечь архив</a>'</font><font color="#007700">;<br />     }<br /> }<br /> if(!isset(</font><font color="#0000BB">$argv</font><font color="#007700">)) echo </font><font color="#DD0000">'</body></html>'</font><font color="#007700">;<br /> __halt_compiler();</font> </font>

well, for today I will ask to popinat all this that I wanted to tell unless breaking traditions legs for jambs in the first post on Habré, otherwise will be late :)

This article is a translation of the original post at geektimes.ru/post/84197/
If you have any questions regarding the material covered in the article above, please, contact the original author of the post.
If you have any complaints about this article or you want this article to be deleted, please, drop an email here: sysmagazine.com@gmail.com.

We believe that the knowledge, which is available at the most popular Russian IT blog geektimes.ru, should be accessed by everyone, even though it is poorly translated.
Shared knowledge makes the world better.
Best wishes.