root/trunk/owa_httpRequest.php

Revision 433, 5.8 kB (checked in by padams, 13 months ago)

- added new owa_error class replacing old. Errors are now buffered untill an error handler is specified from DB. This now means that one can
log errors without worrying about what error handler might or might not be set.
- refactored caller constructor to account for new approach to logging errors and re-organized a bit
- removed references to old error class factory
- added new error class factory to coreAPI

Line 
1<?php
2
3//
4// Open Web Analytics - An Open Source Web Analytics Framework
5//
6// Copyright 2006 Peter Adams. All rights reserved.
7//
8// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// $Id$
17//
18
19if(!class_exists('snoopy')):
20        require_once(OWA_INCLUDE_DIR.'/Snoopy.class.php');
21endif;
22
23/**
24 * Wrapper for Snoopy http request class
25 *
26 * @author      Peter Adams <peter@openwebanalytics.com>
27 * @copyright   Copyright &copy; 2006 Peter Adams <peter@openwebanalytics.com>
28 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
29 * @category    owa
30 * @package     owa
31 * @version             $Revision$           
32 * @since               owa 1.0.0
33 */
34
35class owa_http extends Snoopy {
36       
37        /**
38         * Configuration
39         *
40         * @var array
41         */
42        var $config;
43       
44        /**
45         * Error handler
46         *
47         * @var object
48         */
49        var $e;
50       
51        /**
52         * The length of text contained in the snippet
53         *
54         * @var string
55         */
56        var $snip_len = 100;
57       
58        /**
59         * The string that is added to the beginning and
60         * end of snippet text.
61         *
62         * @var string
63         */
64        var $snip_str = '...';
65       
66        /**
67         * Anchor information for a particular link
68         *
69         * @var array
70         */
71        var $anchor_info;
72       
73        function owa_http() {
74               
75                $c = &owa_coreAPI::configSingleton();
76                $this->config = $c->fetch('base');
77                $this->e = &owa_coreAPI::errorSingleton();
78                $this->agent = $this->config['owa_user_agent'];
79               
80                return;
81        }
82       
83        /**
84         * Searches a fetched html document for the anchor of a specific url
85         *
86         * @param string $link
87         */
88        function extract_anchor($link) {
89               
90                $matches = '';
91                $regex = '/<a[^>]*href=\"%s\"[^>]*>(.*?)<\/a>/i';
92               
93                //$escaped_link = str_replace(array("/", "?"), array("\/", "\?"), $link);
94
95                $pattern = trim(sprintf($regex, preg_quote($link, '/')));
96                $search = preg_match($pattern, $this->results, $matches);
97                //$this->e->debug('pattern: '.$pattern);
98                //$this->e->debug('link: '.$link);
99               
100               
101                if (empty($matches)):
102                        if (substr($link, -1) === '/'):
103                                $link = substr($link, 0, -1);
104                                $pattern = trim(sprintf($regex, preg_quote($link, '/')));
105                                $search = preg_match($pattern, $this->results, $matches);
106                                //$this->e->debug('pattern: '.$pattern);
107                                //$this->e->debug('link: '.$link);
108                        endif;
109                endif;
110               
111                $this->e->debug('ref search: '.$search);
112                //$this->e->debug('ref matches: '.print_r($this->results, true));
113                //$this->e->debug('ref matches: '.print_r($matches, true));
114                               
115                $this->anchor_info =  array('anchor_tag' => $matches[0], 'anchor_text' => owa_lib::inputFilter($matches[0]));
116               
117                $this->e->debug('Anchor info: '.print_r($this->anchor_info, true));
118               
119                return;
120        }
121       
122        /**
123         * Creates a text snippet of the portion of page where the
124         * specific link is found.
125         *
126         * Takes fully qualified URL for the link to search for.
127         *
128         * @param string $link
129         * @return string
130         */
131        function extract_anchor_snippet($link){
132               
133                // Search the page for a specific anchor
134                $this->extract_anchor($link);
135       
136                if(!empty($this->anchor_info['anchor_tag'])):
137                       
138                        // drop certain HTML entitities and their content
139                        $this->results = $this->strip_selected_tags($this->results, array('title', 'head', 'script', 'object', 'style', 'meta', 'link', 'rdf:'), true);
140                       
141                        //$this->e->debug('Refering page content after certain html entities were dropped: '.$this->results);
142               
143                        // strip html from doc
144                        $nohtml = $this->results;
145                       
146                        // calc len of the anchor text
147                        $atext_len = strlen($this->anchor_info['anchor_tag']);
148                       
149                        // find position within document of the anchor text
150                        $start = strpos($nohtml, $this->anchor_info['anchor_tag']);
151                       
152                        if ($start < $this->snip_len):
153                                $part1_start_pos = 0;
154                                $part1_snip_len = $start;
155                        else:
156                                $part1_start_pos = $start;
157                                $part1_snip_len = $this->snip_len;
158                        endif;
159                       
160                       
161                        // Create first segment of snippet
162                        $first_part = substr($nohtml, 0, $part1_start_pos);
163                        $first_part = str_replace(array('\r\n', '\n\n', '\t', '\r', '\n'), '', $first_part); 
164                        $first_part = strip_tags(owa_lib::inputFilter($first_part));
165                        //$part1 = trim(substr($nohtml, $part1_start_pos, $part1_snip_len));
166                        $part1 = substr($first_part,-$part1_snip_len, $part1_snip_len);
167                       
168                        //$part1 = str_replace(array('\r\n', '\n\n', '\t', '\r', '\n'), '', $part1);
169                        //$part1 = owa_lib::inputFilter($part1);
170                        // Create second segment of snippet
171                        $part2 = trim(substr($nohtml, $start + $atext_len, $this->snip_len+300));
172                        $part2 = str_replace(array('\r\n', '\n\n', '\t', '\r', '\n'), '', $part2);
173                        $part2 = substr(strip_tags(owa_lib::inputFilter($part2)),0, $this->snip_len);
174
175                        // Put humpty dumpy back together again and create actual snippet
176                        $snippet =  $this->snip_str.$part1.' <span class="snippet_anchor">'.owa_lib::inputFilter($this->anchor_info['anchor_tag']).'</span> '.$part2.$this->snip_str;
177               
178                else:
179               
180                        $snippet = '';
181                       
182                endif;
183               
184                return $snippet;
185               
186        }
187       
188        function extract_title() {
189               
190                preg_match('~(</head>|<body>|(<title>\s*(.*?)\s*</title>))~i', $this->results, $m);
191               
192                $this->e->debug("referer title extract: ". print_r($m, true));
193               
194        return $m[3];
195        }
196       
197         function strip_selected_tags($str, $tags = array(), $stripContent = false) {
198
199       foreach ($tags as $k => $tag){
200       
201           if ($stripContent == true) {
202                        $pattern = sprintf('#(<%s.*?>)(.*?)(<\/%s.*?>)#is', preg_quote($tag), preg_quote($tag));
203               $str = preg_replace($pattern,"",$str);
204           }
205           $str = preg_replace($pattern, ${2},$str);
206       }
207       
208       return $str;
209   }
210       
211}
212
213
214?>
Note: See TracBrowser for help on using the browser.