/
sqlicontentutils.php
107 lines (95 loc) · 3.99 KB
/
sqlicontentutils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* File containing SQLIContentUtils class
* @copyright Copyright (C) 2010 - SQLi Agency. All rights reserved
* @licence http://www.gnu.org/licenses/gpl-2.0.txt GNU GPLv2
* @author Jerome Vieilledent
* @version @@@VERSION@@@
* @package sqliimport
* @subpackage content
*/
/**
* Utility class aggregating several helper methods related to content
*/
class SQLIContentUtils
{
/**
* Downloads a remote file in the temp folder defined in site.ini.
* Returns the local path of the downloaded file.
* Will throw an exception if download fails.
* Proxy settings defined in site.ini will be used if they are provided
* @param string $url File URL
* @param array $httpAuth Array (numerical indexed) containing HTTP authentication infos. Provide it only if needed (default is null)
* - First element is username.
* - Second element is password.
* @param bool $debug Flag indicating whether to activate cURL debug or not
* @param bool $allowProxyUse Flag indicating whether to allow proxy use or not, allowing to override proxy settings from site.ini
* @return string
* @throws SQLIContentException
*/
public static function getRemoteFile( $url, array $httpAuth = null, $debug = false, $allowProxyUse = true )
{
$url = trim( $url );
$ini = eZINI::instance();
$importINI = eZINI::instance( 'sqliimport.ini' );
$localPath = $ini->variable( 'FileSettings', 'TemporaryDir' ).'/'.basename( $url );
$timeout = $importINI->variable( 'ImportSettings', 'StreamTimeout' );
$ch = curl_init( $url );
$fp = fopen( $localPath, 'w+' );
curl_setopt( $ch, CURLOPT_HEADER, false );
curl_setopt( $ch, CURLOPT_FILE, $fp );
curl_setopt( $ch, CURLOPT_TIMEOUT, (int)$timeout );
curl_setopt( $ch, CURLOPT_FAILONERROR, true );
if ( $debug )
{
curl_setopt( $ch, CURLOPT_VERBOSE, true );
curl_setopt( $ch, CURLOPT_NOPROGRESS, false );
}
// Should we use proxy ?
$proxy = $ini->variable( 'ProxySettings', 'ProxyServer' );
if ( $proxy && $allowProxyUse )
{
curl_setopt( $ch, CURLOPT_PROXY, $proxy );
$userName = $ini->variable( 'ProxySettings', 'User' );
$password = $ini->variable( 'ProxySettings', 'Password' );
if ( $userName )
{
curl_setopt( $ch, CURLOPT_PROXYUSERPWD, "$userName:$password" );
}
}
// Should we use HTTP Authentication ?
if( is_array( $httpAuth ) )
{
if( count( $httpAuth ) != 2 )
throw new SQLIContentException( __METHOD__.' => HTTP Auth : Wrong parameter count in $httpAuth array' );
list( $httpUser, $httpPassword ) = $httpAuth;
curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY );
curl_setopt( $ch, CURLOPT_USERPWD, $httpUser.':'.$httpPassword );
}
$result = curl_exec( $ch );
if ( $result === false )
{
$error = curl_error( $ch );
$errorNum = curl_errno( $ch );
curl_close( $ch );
throw new SQLIContentException( "Failed downloading remote file '$url'. $error", $errorNum);
}
curl_close( $ch );
fclose( $fp );
return trim($localPath);
}
/**
* Returns eZXML content to insert into XML blocks (ezxmltext datatype)
* eZXML is generated from HTML content provided as argument
* @param string $htmlContent Input HTML string
* @return string Generated eZXML string
*/
public static function getRichContent( $htmlContent )
{
$htmlParser = new SQLIXMLInputParser();
$htmlParser->setParseLineBreaks( true );
$document = $htmlParser->process( $htmlContent );
$richContent = eZXMLTextType::domString( $document );
return $richContent;
}
}