Implements a interface to run downloads with wget, intended to be used in shell scripts but also usable in a web enviroment.
© 2012, Tobias Baeumer Tobias@gmai l.com Baeumer
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see http://www.gnu.org/licenses/.
wgetHandler | Implements a interface to run downloads with wget, intended to be used in shell scripts but also usable in a web enviroment. |
Variables | |
strPathTowget | Path to wget binary to use. |
strParams | Commandline params/options to use when launching wget. |
strUserAgent | HTTP UserAgent to send. |
arrErrorDescriptions | Descriptions of wget return values, where key == return and vice versa. |
arrCookies | Cookies to send with download requests. |
intReturnValue | Will get wgets return value when doDownload finished. |
strDownloadPath | Path (full or relative) to target directory, without trailing slash, for downloaded files. |
arrOutputLines | Will be filled with stdout (and stderr, since we use output redirection) from wget. |
objDownloadResult | Will get the download results |
arrIndicatorChars | Each element contains one char, used in order of appearance in array for intLiveMode=3 param in doDownload. |
Functions | |
__construct() | Initialize objDownloadResult |
__destruct() | Just delete the cookies.txt file, it it exists. |
_saveCookieFile | Save cookies from arrCookies to cookies.txt |
_parseOutput | Takes an array with output lines of wget and get average download speed, time needed and file size from it. |
doDownload | Downloads a given url |
public $strParams
Commandline params/options to use when launching wget.
Dont add ‘--referer’ or ‘--user-agent’ here, set them through strReferer (see <doDownload>) and strUserAgent instead, else your values would be overwritten by those.
string | -e robots=off --follow-tags=meta --follow-ftp --max-redirect 5 --span-hosts --cookies=on --load-cookies=cookies.txt |
public $arrErrorDescriptions
Descriptions of wget return values, where key == return and vice versa. These strings will just be used as return value for doDownload on fail and can be translated.
Same like intReturnValue: Dont trust this value when using `intLiveMode` for doDownload.
array | English descriptions, see wget manpage for details. |
public $intReturnValue
Will get wgets return value when doDownload finished.
Dont trust this value when using `intLiveMode` for doDownload.
integer | 99 (as placeholder for init, no meaning) |
public $strDownloadPath
Path (full or relative) to target directory, without trailing slash, for downloaded files. Must be writeable of course, so maybe a chmod is needed.
Will also be used for cookies.txt (used for passing cookies to wget) and .wgetout (used for intLiveMode in doDownload)
string | ./downloads |
private $arrOutputLines
Will be filled with stdout (and stderr, since we use output redirection) from wget. Used by _parseOutput.
This is also available, despite using “to file” output redirection and detached exec(), when using intLiveMode in doDownload.
array | Empty array |
public $arrIndicatorChars
Each element contains one char, used in order of appearance in array for intLiveMode=3 param in doDownload.
array | |, /, -, \, |, /, -, \ |
public function __construct()
Initialize objDownloadResult
private function _saveCookieFile( $strDomainCookieScope )
Save cookies from arrCookies to cookies.txt
string strDomainCookieScope | the domain/host for which these cookies are valid |
boolean | true, if save was successful or |
boolean | false, if an error occurred (most likely because of permission problems) |
private function _parseOutput()
Takes an array with output lines of wget and get average download speed, time needed and file size from it.
object
or
boolean | false, if server file didn’t changed |
public function doDownload( $strURL, $strTargetFile = "", $strReferer = "", $intLiveMode = 0, $bDontDeleteTargetFile = false )
Downloads a given url
string strURL | URL to download |
string strTargetFile | target file for download (defaults to server given name) |
string strReferer | HTTP referer to send (defaults to none) |
integer intLiveMode | If greater then 0, will run wget in background and print live status for which this value is taken as mode switch. See Notes. |
boolean bDontDeleteTargetFile | defaults to false, will skip target file deletion if set to true. |
boolean | true, if download was successful |
or
string | containing error description |
intLiveMode can be 1, 2 or 3. These modes have the following meaning:
_saveCookieFile, strPathTowget, strParams, arrOutputLines, intReturnValue, objDownloadResult, _parseOutput, arrErrorDescriptions, arrIndicatorChars
Path to wget binary to use.
public $strPathTowget
Commandline params/options to use when launching wget.
public $strParams
HTTP UserAgent to send.
public $strUserAgent
Descriptions of wget return values, where key == return and vice versa.
public $arrErrorDescriptions
Cookies to send with download requests.
public $arrCookies
Will get wgets return value when doDownload finished.
public $intReturnValue
Downloads a given url
public function doDownload( $strURL, $strTargetFile = "", $strReferer = "", $intLiveMode = 0, $bDontDeleteTargetFile = false )
Path (full or relative) to target directory, without trailing slash, for downloaded files.
public $strDownloadPath
Will be filled with stdout (and stderr, since we use output redirection) from wget.
private $arrOutputLines
Will get the download results
public $objDownloadResult
Each element contains one char, used in order of appearance in array for intLiveMode=3 param in doDownload.
public $arrIndicatorChars
Initialize objDownloadResult
public function __construct()
Just delete the cookies.txt file, it it exists.
public function __destruct()
Save cookies from arrCookies to cookies.txt
private function _saveCookieFile( $strDomainCookieScope )
Takes an array with output lines of wget and get average download speed, time needed and file size from it.
private function _parseOutput()