/[drupal]/contributions/modules/commentluv/commentluv.module
ViewVC logotype

Contents of /contributions/modules/commentluv/commentluv.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.4 - (show annotations) (download) (as text)
Thu Nov 20 19:55:33 2008 UTC (12 months ago) by lomz
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +1 -0 lines
File MIME type: text/x-php
#239337 by Lomz: Checkboxes ticked by default.
1 <?php
2 function commentluv_help($section) {
3 switch ($section) {
4 case 'admin/modules#description':
5 return t('Comment Luv');
6 break;
7 }
8 }
9
10 function LL_TextBetween($s1,$s2,$s){
11 $s1 = strtolower($s1);
12 $s2 = strtolower($s2);
13 $L1 = strlen($s1);
14 $scheck = strtolower($s);
15 if($L1>0){$pos1 = strpos($scheck,$s1);} else {$pos1=0;}
16 if($pos1 !== false){
17 if($s2 == '') return substr($s,$pos1+$L1);
18 $pos2 = strpos(substr($scheck,$pos1+$L1),$s2);
19 if($pos2!==false) return substr($s,$pos1+$L1,$pos2);
20 }
21 return '';
22 }
23
24 // find feedburner feed function (parses a users page for a feed link)
25 function findfeedburner($page_url){
26 // can't open default wordpress feed, use curl to parse users page for a relative link feed
27 if(function_exists(curl_init)) {
28 $ch=curl_init();
29 $timeout = 10; // set to zero for no timeout
30 curl_setopt ($ch, CURLOPT_URL, $page_url );
31 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
32 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
33 $data=curl_exec($ch);
34 curl_close($ch);
35 $lines=explode("\n",$data);
36 // look for feedburner url
37 foreach($lines as $line){
38 if(strstr($line,"alternate")&&(strstr($line,"rss")||strstr($line,"xml"))){
39 $pos=strpos($line,"href");
40 $cut=substr($line,$pos+5);
41 $feed_url=LL_TextBetween("\"","\"",$cut);
42 break;
43 }
44 }
45 }
46 else // no curl here, borrow mine!
47 {
48 $rss=fetch_rss("http://www.commentluv.com/commentluvinc/cl_feedfind.php?url=$page_url");
49 $items= array_slice($rss->items,0,1);
50 foreach($items as $item){
51 $feed_post=$item['link'];
52 }
53 return $feed_post;
54 }
55 return $feed_url;
56 }
57
58 function commentluv_form_alter($form_id, &$form) {
59
60 if ($form_id != 'comment_form') {
61 return;
62 }
63
64 global $user;
65 if($user->uid == 0)
66 {
67 $form['luv'] = array(
68 '#type' => 'checkbox',
69 '#title' => t('Comment Luv'),
70 '#description' => t('This blog uses the <a href="http://www.fiddyp.co.uk/commentluv-wordpress-plugin">CommentLuv</a> <a href="http://imafish.co.uk/commentluv">Drupal</a> plugin which will try and parse your sites feed and display a link to your last post, please be patient while it tries to find it for you.'),
71 '#default_value' => '1',
72 );
73 }
74 }
75
76 function commentluv_comment($comment, $op){
77 switch ($op) {
78 case 'insert':
79
80 $manual_feed=0;
81 $luv = $comment['luv']; // get checkbox value for commentluv
82
83 // don't parse for admin posting comment reply,pingback or trackback and checks if last post already added and check for luv box checked
84 global $user;
85 if ($luv!='1' || $user->uid > 0) {
86 return;
87 }
88 // get author url
89 $author_url=$comment['homepage'];
90 // if no author url given, return
91 if(!$author_url){
92 return;
93 }
94 // clean up author url if it has a trailing forward slash
95 if(substr($author_url,-1)=="/") {
96 $author_url = substr($author_url, 0, -1); // remove trailing slash
97 }
98
99 // ***********************
100 // *** fun starts here ***
101 // ***********************
102 // check for magpie timeout constant
103 if(!defined('MAGPIE_FETCH_TIME_OUT')){
104 define('MAGPIE_FETCH_TIME_OUT',5);
105 }
106 // set cache age to 5 minutes so it doesn't show an old last post if a commenter makes a new post and returns to comment again
107 if(!defined('MAGPIE_CACHE_AGE')){
108 define('MAGPIE_CACHE_AGE',300);
109 }
110
111 // use wp internal rss.php function (wp 2.1+ only)
112 include_once('rss_fetch.inc');
113
114 // **************************
115 // *** identify blog type ***
116 // **************************
117 // try and determine blog type and locate default location for feed.
118 if(strstr($author_url,"blogspot")){ // blogspot blog
119 $feed_url="$author_url/feeds/posts/default/";
120
121 } elseif(strstr($author_url,"typepad")){ // typepad blog
122 $feed_url="$author_url/atom.xml";
123
124 } elseif(strstr($author_url,"livejournal")){ // livejournal
125 $feed_url="$author_url/data/rss";
126
127 } elseif(strstr($author_url,"web-log")){ // web-log blog
128 // take only the name of the author of http://xxx.web-log.nl
129 preg_match('|http://(.*?).web-log.nl|is', $author_url, $authorid);
130 $feed_url = $author_url . "/" . $authorid[1] . "/rss.xml";
131 } else {
132 $feed_url="$author_url/feed/"; // own domain or wordpress blog
133
134 }
135
136 // ***************************
137 // *** detect manual entry ***
138 // ***************************
139 // here we see if user manually entered their own feed url
140 if(strstr($comment['comment'],"[feed]")){
141 $feed_url=LL_TextBetween("[feed]","[/feed]",$comment['comment']);
142 // now strip feed bit from comment
143 $manual_feed_pos_start=strpos($comment['comment'],"[feed]");
144 $comment['comment']=substr($comment['comment'],0,$manual_feed_pos_start);
145 $manual_feed=1;
146 }
147
148 // *******************************
149 // *** time to do the fetching ***
150 // *******************************
151 // fetch feed with WP function
152 $rss=fetch_rss("$feed_url");
153
154 // couldn't find it try to parse users page if curl enabled
155 if(!$rss && !$manual_feed){
156 $feed_url=findfeedburner($author_url);
157 $rss=fetch_rss("$feed_url");
158 }
159
160 // couldn't find it! look in other places
161 if(!$rss && !$manual_feed){
162 $feed_url="$author_url/?feed=rss";
163 $rss=fetch_rss("$feed_url");
164 // try own domain blogspot
165 if(!$rss){
166 $feed_url="$author_url/feeds/posts/default";
167 $rss=fetch_rss("$feed_url");
168 }
169 // try typepad own domain
170 if(!$rss) {
171 $feed_url="$author_url/atom.xml";
172 $rss=fetch_rss("$feed_url");
173 }
174 }
175
176 // **************************
177 // *** do the parse dance ***
178 // **************************
179 // now we must have a feed to parse, get last post title and link
180 $items= array_slice($rss->items,0,1);
181 foreach($items as $item){
182 $feed_title=$item['title'];
183 $feed_post=$item['link'];
184 }
185
186 // try and fix any single quotes that got changed to question marks
187 $search = array("?s", "?t", "?l", "?v", "?m", "?d");
188 $replace = array("'s", "'t", "'l", "'v", "'m", "'d");
189 $feed_title=str_replace($search,$replace,$feed_title);
190 $feed_title = utf8_encode($feed_title);
191
192 // ****************************
193 // *** append the last post ***
194 // ****************************
195 // insert last post data onto the end of the comment content
196 if($feed_title && $feed_post){ // only output if last post found
197 $author_excerpt="\n\n<em>".$comment['name']. t("'s last blog post...") ." <a href='$feed_post'> $feed_title</a></em>";
198 $comment['comment']=substr_replace($comment['comment'], $author_excerpt,strlen($comment['comment']),0);
199 }
200 $result = comment_save($comment);
201 //drupal_mail(1,'petejw@gmail.com','bla', $comment[luv] ,null);
202 break;
203 }
204 return $comment;
205 }
206

  ViewVC Help
Powered by ViewVC 1.1.2