(.*?)<\/div>/sm', $response, $matches);
665 |
666 | if (@$matches[0][0]) {
667 | // We have some folders.
668 | preg_match_all('/href="(.*?)"/sm', $matches[0][0], $matches);
669 |
670 | foreach ($matches[1] as &$match) {
671 | $match = 'http://chomikuj.pl' . $match;
672 | }
673 |
674 | // Downloading subfolders' files.
675 | $this->downloadFiles($matches[1], $destinationFolder, $recursive, $structure, $overwrite);
676 | }
677 | }
678 | }
679 |
680 | return $filesInfo;
681 | }
682 |
683 | /**
684 | * Creates and invokes a GET/POST request.
685 | *
686 | * @param string $url
687 | * URL.
688 | *
689 | * @param array $params
690 | * GET params array.
691 | *
692 | * @param string $method
693 | * Request method, e.g. "GET", "POST", "DELETE".
694 | *
695 | * @param string $data
696 | * Data string.
697 | *
698 | * @param array $headers
699 | * Custom HTTP headers.
700 | */
701 | public function request ($url, $params, $method = 'POST', $data = "", $headers = array()) {
702 |
703 | $curl = curl_init($url);
704 |
705 | if ($method == 'POST')
706 | curl_setopt($curl, CURLOPT_POST, 1);
707 | else
708 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
709 |
710 | curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
711 | curl_setopt($curl, CURLOPT_HEADER, false);
712 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
713 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
714 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
715 | curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
716 |
717 | if (!empty($data))
718 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
719 |
720 | $result = curl_exec($curl);
721 |
722 | curl_close($curl);
723 |
724 | preg_match('/
(\d+)<\/stamp>/', $result, $matches);
725 |
726 | if (!empty($matches[1])) {
727 | $this->stamp = $matches[1] + 1000;
728 | }
729 |
730 | return $result;
731 | }
732 | }
733 |
734 | /**
735 | * CLI Support.
736 | */
737 |
738 | if (php_sapi_name() === 'cli') {
739 |
740 | /**
741 | * parseArgs Command Line Interface (CLI) utility function.
742 | * @author Patrick Fisher
743 | * @see http://github.com/pwfisher/CommandLine.php
744 | */
745 | function parseArgs($argv) {
746 | $argv = $argv ? $argv : $_SERVER['argv']; array_shift($argv); $o = array();
747 | foreach ($argv as $a) {
748 | if (substr($a, 0, 2) == '--') { $eq = strpos($a, '=');
749 | if ($eq !== false) { $o[substr($a, 2, $eq - 2)] = substr($a, $eq + 1); }
750 | else { $k = substr($a, 2); if (!isset($o[$k])) { $o[$k] = true; } } }
751 | else if (substr($a, 0, 1) == '-') {
752 | if (substr($a, 2, 1) == '=') { $o[substr($a, 1, 1)] = substr($a, 3); }
753 | else { foreach (str_split(substr($a, 1)) as $k) { if (!isset($o[$k])) { $o[$k] = true; } } } }
754 | else { $o[] = $a; } }
755 | return $o;
756 | }
757 |
758 | $args = parseArgs($argv);
759 |
760 | if (empty($args['recursive']))
761 | $args['recursive'] = isset($args['r']) ? $args['r'] : FALSE;
762 |
763 | if (empty($args['structure']))
764 | $args['structure'] = isset($args['s']) ? $args['s'] : FALSE;
765 |
766 | if (empty($args['overwrite']))
767 | $args['overwrite'] = isset($args['o']) ? $args['o'] : FALSE;
768 |
769 | if (empty($args['noprogress']))
770 | $args['noprogress'] = isset($args['n']) ? $args['n'] : FALSE;
771 |
772 | if (empty($args['help']))
773 | $args['help'] = isset($args['h']) ? $args['h'] : FALSE;
774 |
775 | if (empty($args['help']))
776 | $args['help'] = in_array('/?', $args, TRUE);
777 |
778 | if (empty($args) || !empty($args['help']) || empty($args['user']) || (empty($args['password']) && empty($args['hash'])) || empty($args['url'])) {
779 | echo
780 | "\nChomikuj Downloader\nVersion 0.1\n\nUsage:\n" .
781 | " php " . $argv[0] . " --user=USER --password=PASSWORD --url=\"http://chomikuj.pl/PATH\" [optional options] destination\n" .
782 | " php " . $argv[0] . " --user=USER --hash=MD5_PASSWORD --url=\"http://chomikuj.pl/PATH\" [optional options] destination\n\n" .
783 | "Required Options:\n" .
784 | " --user=USER Uses specified user name for authentication.\n" .
785 | " --password=PASSWORD Uses specified user password for authentication.\n" .
786 | " --hash=HASH Uses specified user password hash for authentication.\n" .
787 | " --url=URL Downloads files from the specified URL.\n\n" .
788 | "Optional Options:\n" .
789 | " -r, --recursive Downloads also all subdirectories.\n" .
790 | " -s, --structure Creates full folder structure.\n" .
791 | " -o, --overwrite Overwrites existing files.\n" .
792 | " -n, --noprogress Do not print progress.\n" .
793 | " --ext=EXTENSIONS Downloads files only with the specified extensions, separated by comma.\n" .
794 | " --max-limit=SIZE Do not download files with size greater than specified max (in bytes).\n" .
795 | " -h, --help, /? Shows this help.\n\n" .
796 | "NOTE:\n" .
797 | " - To log in you may use password OR hash.\n" .
798 | " - URLs must start with \"http://chomikuj.pl/\" and must NOT end with a slash.\n" .
799 | " - By default the script downloads files into the folder from where it was run.\n\n" .
800 | "Examples:\n" .
801 | " php " . $argv[0] . " --user=chomikoryba --password=ryba123 --url=\"http://chomikuj.pl/chomikoryba\" -r -s downloads\n" .
802 | " php " . $argv[0] . " --user=chomikoryba --hash=233b5774ada09b36458f69a04c9718e9 --url=\"http://chomikuj.pl/chomikoryba/Myriad+Pro+%28CE%29-TTF\" -r --ext=\"ttf,otf\" fonts\n" .
803 | "\n";
804 |
805 | exit;
806 | }
807 |
808 | if (empty($args[0]))
809 | // No destination folder.
810 | $args[0] = './';
811 |
812 | $chomikuj = new Chomikuj($args);
813 |
814 | $chomikuj->downloadFiles(
815 | // List of URLs. Single URL accepted.
816 | array($args['url']),
817 |
818 | // Destination folder.
819 | $args[0],
820 |
821 | // Recurse into subdirectories.
822 | !empty($args['recursive']),
823 |
824 | // Create folder structure.
825 | !empty($args['structure']),
826 |
827 | // Overwrite existing files.
828 | !empty($args['overwrite']),
829 |
830 | // Do not print progress
831 | !empty($args['noprogress']),
832 |
833 | // Pass original arguments.
834 | $args
835 | );
836 | }
837 |
--------------------------------------------------------------------------------