@@ -71,6 +71,15 @@ class SimplePie_Sanitize
7171 var $ useragent = '' ;
7272 var $ force_fsockopen = false ;
7373 var $ replace_url_attributes = null ;
74+ var $ registry ;
75+
76+ /**
77+ * List of domains for which to force HTTPS.
78+ * @see SimplePie_Sanitize::set_https_domains()
79+ * Array is a tree split at DNS levels. Example:
80+ * array('biz' => true, 'com' => array('example' => true), 'net' => array('example' => array('www' => true)))
81+ */
82+ var $ https_domains = array ();
7483
7584 public function __construct ()
7685 {
@@ -241,6 +250,68 @@ public function set_url_replacements($element_attribute = null)
241250 $ this ->replace_url_attributes = (array ) $ element_attribute ;
242251 }
243252
253+ /**
254+ * Set the list of domains for which to force HTTPS.
255+ * @see SimplePie_Misc::https_url()
256+ * Example array('biz', 'example.com', 'example.org', 'www.example.net');
257+ */
258+ public function set_https_domains ($ domains )
259+ {
260+ $ this ->https_domains = array ();
261+ foreach ($ domains as $ domain )
262+ {
263+ $ domain = trim ($ domain , ". \t\n\r\0\x0B" );
264+ $ segments = array_reverse (explode ('. ' , $ domain ));
265+ $ node =& $ this ->https_domains ;
266+ foreach ($ segments as $ segment )
267+ {//Build a tree
268+ if ($ node === true )
269+ {
270+ break ;
271+ }
272+ if (!isset ($ node [$ segment ]))
273+ {
274+ $ node [$ segment ] = array ();
275+ }
276+ $ node =& $ node [$ segment ];
277+ }
278+ $ node = true ;
279+ }
280+ }
281+
282+ /**
283+ * Check if the domain is in the list of forced HTTPS.
284+ */
285+ protected function is_https_domain ($ domain )
286+ {
287+ $ domain = trim ($ domain , '. ' );
288+ $ segments = array_reverse (explode ('. ' , $ domain ));
289+ $ node =& $ this ->https_domains ;
290+ foreach ($ segments as $ segment )
291+ {//Explore the tree
292+ if (isset ($ node [$ segment ]))
293+ {
294+ $ node =& $ node [$ segment ];
295+ }
296+ else
297+ {
298+ break ;
299+ }
300+ }
301+ return $ node === true ;
302+ }
303+
304+ /**
305+ * Force HTTPS for selected Web sites.
306+ */
307+ public function https_url ($ url )
308+ {
309+ return (strtolower (substr ($ url , 0 , 7 )) === 'http:// ' ) &&
310+ $ this ->is_https_domain (parse_url ($ url , PHP_URL_HOST )) ?
311+ substr_replace ($ url , 's ' , 4 , 0 ) : //Add the 's' to HTTPS
312+ $ url ;
313+ }
314+
244315 public function sanitize ($ data , $ type , $ base = '' )
245316 {
246317 $ data = trim ($ data );
@@ -443,6 +514,7 @@ public function replace_urls($document, $tag, $attributes)
443514 $ value = $ this ->registry ->call ('Misc ' , 'absolutize_url ' , array ($ element ->getAttribute ($ attribute ), $ this ->base ));
444515 if ($ value !== false )
445516 {
517+ $ value = $ this ->https_url ($ value );
446518 $ element ->setAttribute ($ attribute , $ value );
447519 }
448520 }
0 commit comments