Created
June 18, 2013 08:58
-
-
Save funivan/5803779 to your computer and use it in GitHub Desktop.
Save pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| error_reporting(E_ALL); | |
| ini_set('display_errors', 1); | |
| class ExportHtml { | |
| const SAVE_DIR = '/home/ivan/Ubuntu One/saved'; | |
| public function __construct() { | |
| $header = array( | |
| "User-Agent: Opera/9.62 (Windows NT 5.1; U; ru) Presto/2.1.1", | |
| "Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1", | |
| "Accept-Language: ru-RU,ru;q=0.9,en;q=0.8", | |
| "Accept-Charset: iso-8859-1, utf-8, utf-16, *;q=0.1", | |
| "Referer: http://translate.google.com/translate_t", | |
| "Connection: Keep-Alive" | |
| ); | |
| $this->ch = curl_init(); // init curl | |
| curl_setopt($this->ch, CURLOPT_COOKIEJAR, "/tmp/cookie"); | |
| curl_setopt($this->ch, CURLOPT_COOKIEFILE, "/tmp/cookie"); | |
| curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header); // set our headers information | |
| curl_setopt($this->ch, CURLOPT_HEADER, 0); // set headers (0 = no headers in result) | |
| curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); // type of transfer (1 = to string) | |
| curl_setopt($this->ch, CURLOPT_TIMEOUT, 10); // time to wait in | |
| curl_setopt($this->ch, CURLOPT_POST, 0); | |
| } | |
| public function prepareHtml($html) { | |
| $newHtml = $html; | |
| preg_match_all('!<img.*src\s*=\s*[\"\'](.*)[\"\'].*>!Uui', $html, $images); | |
| foreach ($images[0] as $index => $string) { | |
| $url = $images[1][$index]; | |
| curl_setopt($this->ch, CURLOPT_URL, $url); // set the url to fetch | |
| $data = curl_exec($this->ch); | |
| $info = curl_getinfo($this->ch); | |
| if (strpos($info['content_type'], 'image/') === 0 and $info['http_code'] == 200) { | |
| $newHtml = str_replace($string, '<img src="data:' . $info['content_type'] . ';base64,' . base64_encode($data) . '" />', $newHtml); | |
| } | |
| } | |
| if (!empty($_SERVER['HTTP_REFERER'])) { | |
| $newHtml .= '<br><br><br><a href="' . $_SERVER['HTTP_REFERER'] . '">' . $_SERVER['HTTP_REFERER'] . '</a>'; | |
| } | |
| return $newHtml; | |
| } | |
| public function saveHtml($html, $tags) { | |
| $fileDir = '/' . date('Y-m') . '/' . date('d'); | |
| $file = '/' . md5(time()) . '.html'; | |
| if (!is_dir(static::SAVE_DIR . $fileDir)) { | |
| mkdir(static::SAVE_DIR . $fileDir, 0755, true); | |
| } | |
| $html = '<div class="tags">' . $tags . '</div>' . $html; | |
| file_put_contents(static::SAVE_DIR . $fileDir . $file, $html); | |
| $partialFile = $fileDir . $file; | |
| $this->updateMainFile($tags, $partialFile); | |
| return $partialFile; | |
| } | |
| protected function updateMainFile($tags, $filePath) { | |
| $filePath = ltrim($filePath, '/'); | |
| $yearFile = static::SAVE_DIR . '/' . date('Y') . '.html'; | |
| if (is_file($yearFile)) { | |
| $data = file_get_contents($yearFile); | |
| } else { | |
| $data = ''; | |
| } | |
| $data = '<div data-tags="' . $tags . '"> | |
| <span>' . strftime("%F %T") . '</span> - <span>' . $tags . '</span> | |
| <br> | |
| <a href="' . $filePath . '">' . $filePath . '</a> | |
| </div>' . $data; | |
| file_put_contents($yearFile, $data); | |
| } | |
| } | |
| $exportHtml = new ExportHtml(); | |
| ?> | |
| <? | |
| if (!empty($_POST['html'])) { | |
| $tags = !empty($_POST['tags']) ? $_POST['tags'] : ''; | |
| $html = $_POST['html']; | |
| $html = $exportHtml->prepareHtml($html); | |
| $file = $exportHtml->saveHtml($html, $tags); | |
| $yearFileUrl = 'file://' . ExportHtml::SAVE_DIR . '/' . date('Y') . '.html'; | |
| ?> | |
| Done: <?= $file ?><br> | |
| <br> | |
| <?= $yearFileUrl ?> | |
| <? if (!empty($_SERVER['HTTP_REFERER'])) { ?> | |
| <br> | |
| <br> | |
| <a href="<?= $_SERVER['HTTP_REFERER'] ?>"><?= $_SERVER['HTTP_REFERER'] ?></a> | |
| <? } ?> | |
| <? | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment