3 require_once('library/HTML5/Parser.php');
4 require_once('library/HTMLPurifier.auto.php');
6 function arr_add_hashes(&$item,$k) {
10 function parse_url_content(&$a) {
16 $url = trim(hex2bin($_GET['binurl']));
18 $url = trim($_GET['url']);
21 $title = strip_tags(trim($_GET['title']));
23 if($_GET['description'])
24 $text = strip_tags(trim($_GET['description']));
27 $arr_tags = str_getcsv($_GET['tags']);
28 if(count($arr_tags)) {
29 array_walk($arr_tags,'arr_add_hashes');
30 $str_tags = '<br />' . implode(' ',$arr_tags) . '<br />';
34 logger('parse_url: ' . $url);
37 $template = "<br /><a class=\"bookmark\" href=\"%s\" >%s</a>%s<br />";
40 $arr = array('url' => $url, 'text' => '');
42 call_hooks('parse_link', $arr);
44 if(strlen($arr['text'])) {
50 if($url && $title && $text) {
52 $text = '<br /><br /><blockquote>' . $text . '</blockquote><br />';
53 $title = str_replace(array("\r","\n"),array('',''),$title);
55 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
57 logger('parse_url (unparsed): returns: ' . $result);
71 // logger('parse_url: data: ' . $s, LOGGER_DATA);
74 echo sprintf($template,$url,$url,'') . $str_tags;
79 $c = preg_match('/\<head(.*?)\>(.*?)\<\/head\>/ism',$s,$matches);
81 // logger('parse_url: header: ' . $matches[2], LOGGER_DATA);
83 $domhead = HTML5_Parser::parse($matches[2]);
84 } catch (DOMException $e) {
85 logger('scrape_dfrn: parse error: ' . $e);
88 logger('parsed header');
92 if(strpos($s,'<title>')) {
93 $title = substr($s,strpos($s,'<title>')+7,64);
94 if(strpos($title,'<') !== false)
95 $title = strip_tags(substr($title,0,strpos($title,'<')));
99 $config = HTMLPurifier_Config::createDefault();
100 $config->set('Cache.DefinitionImpl', null);
101 $purifier = new HTMLPurifier($config);
102 $s = $purifier->purify($s);
104 // logger('purify_output: ' . $s);
107 $dom = HTML5_Parser::parse($s);
108 } catch (DOMException $e) {
109 logger('scrape_dfrn: parse error: ' . $e);
113 echo sprintf($template,$url,$url,'') . $str_tags;
117 $items = $dom->getElementsByTagName('title');
120 foreach($items as $item) {
121 $title = trim($item->textContent);
128 $divs = $dom->getElementsByTagName('div');
130 foreach($divs as $div) {
131 $class = $div->getAttribute('class');
132 if($class && (stristr($class,'article') || stristr($class,'content'))) {
133 $items = $div->getElementsByTagName('p');
135 foreach($items as $item) {
136 $text = $item->textContent;
137 if(stristr($text,'<script')) {
141 $text = strip_tags($text);
142 if(strlen($text) < 100) {
146 $text = substr($text,0,250) . '...' ;
157 $items = $dom->getElementsByTagName('p');
159 foreach($items as $item) {
160 $text = $item->textContent;
161 if(stristr($text,'<script'))
163 $text = strip_tags($text);
164 if(strlen($text) < 100) {
168 $text = substr($text,0,250) . '...' ;
176 logger('parsing meta');
177 $items = $domhead->getElementsByTagName('meta');
179 foreach($items as $item) {
180 $property = $item->getAttribute('property');
181 if($property && (stristr($property,':description'))) {
183 $text = $item->getAttribute('content');
184 if(stristr($text,'<script')) {
188 $text = strip_tags($text);
191 $text = substr($text,0,250) . '...' ;
193 if($property && (stristr($property,':image'))) {
195 $image = $item->getAttribute('content');
196 if(stristr($text,'<script')) {
200 $image = strip_tags($image);
202 $i = fetch_url($image);
204 require_once('include/Photo.php');
206 if($ph->is_valid()) {
207 if($ph->getWidth() > 300 || $ph->getHeight() > 300) {
208 $ph->scaleImage(300);
209 $new_width = $ph->getWidth();
210 $new_height = $ph->getHeight();
211 $image = '<br /><br /><img height="' . $new_height . '" width="' . $new_width . '" src="' .$image . '" alt="photo" />';
214 $image = '<br /><br /><img src="' . $image . '" alt="photo" />';
226 $text = '<br /><br /><blockquote>' . $text . '</blockquote><br />';
230 $text = $image . '<br />' . $text;
232 $title = str_replace(array("\r","\n"),array('',''),$title);
234 $result = sprintf($template,$url,($title) ? $title : $url,$text) . $str_tags;
236 logger('parse_url: returns: ' . $result);