source: trunk/www.guidonia.net/wp/wp-content/plugins/odlinks/odl_main.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 14.3 KB
Line 
1<?php
2
3/*
4 * odl_main.php
5 * @author Mohammad Forgani
6 * wordpress plugin website directory project
7 * @copyright Copyright 2008, Oh Jung-Su
8 * @version 0.5
9 * @link http://www.forgani.com
10*/
11
12include_once(ABSPATH . 'wp-content/plugins/odlinks/includes/pagerank.php');
13function odlinksdisplay_index($msg){
14 global $_GET, $_POST, $table_prefix, $wpdb, $PHP_SELF, $odlinksversion;
15 $odlinkssettings=get_option('odlinksdata');
16 $includePath=get_bloginfo('wpurl') . "/wp-content/plugins/odlinks/includes/";
17 $SnaprHead.= "<script type=\"text/javascript\">var bubbleImagePath=\"".$includePath ."bg.png\"</script>\n";
18 $SnaprHead.= "<script type=\"text/javascript\" src=\"". $includePath ."previewbubble.js\"></script>\n";
19 print($SnaprHead);
20
21 if(!$page) $page=1;
22 $id=$_GET['id'];
23 if(!$id) $id=0;
24 $limit=$odlinkssettings[odlinks_num_links];
25 if (!$limit or $limit ==0) $limit = 10;
26 $tpl=new ODLTemplate();
27
28 $tpl->assign('odl_images', get_bloginfo('wpurl') . "/wp-content/plugins/odlinks");
29 //$odl_advanced=odlinkscreate_link("searchlink", array("name"=>'Advanced'));
30 //$tpl->assign('odl_advanced', $odl_advanced);
31 $odl_search_link=odlinkscreate_link("searchform", array());
32 $tpl->assign('odl_search_link', $odl_search_link);
33 $odl_main_link=odlinkscreate_link("index", array("name"=>"TOP"));
34 $odl_top_link=odlinkscreate_link("index", array("name"=>$odlinkssettings['page_link_title']));
35 $tpl->assign('odl_top_link', $odl_top_link);
36 $tpl->assign('odl_main_link', $odl_main_link);
37 $tpl->assign('odl_wpurl', get_bloginfo('url'));
38 if ($msg) $tpl->assign('error',$msg);
39
40
41 $tpl->assign('cat_id',$id);
42 list($navigationLinks, $addurl, $desc)=odl_create_navigation($id,"","","");
43 $tpl->assign('addurl_link', $addurl);
44 $navigationLinks=trim($navigationLinks);
45 $last=$navigationLinks{strlen($navigationLinks)-1};
46 if (!strcmp($last,":")){
47 $navigationLinks=rtrim($navigationLinks, ':');
48 }
49 $tpl->assign('navigation_link', $navigationLinks);
50 $tpl->assign('odl_navigation_description', $desc);
51 $sql="SELECT COUNT(l_id) as count FROM {$table_prefix}odlinks WHERE l_c_id='".$id."' AND l_hide ='visible'";
52 $result=$wpdb->get_results($sql);
53 for ($i=0; $i<count($result); $i++){
54 $row=$result[$i];
55 $NumberOfPages=ceil($row->count/$limit);
56 }
57 $from=($page-1)*$limit;
58 if($from <= 0){ $from=0; }
59 $sql="SELECT * FROM {$table_prefix}odlinks WHERE l_c_id='".$id."' AND l_hide ='visible'";
60 $results=$wpdb->get_results($sql);
61 $links=array();
62 for ($i=0; $i<count($results); $i++){
63 $result=$results[$i];
64
65 $PR=new ODLPagerank();
66 $rankImage=false;
67 $Url=trim($result->l_url);
68 if (($Url!='') AND ($Url!='http://')) {
69 if (isUrlValid($Url)) {
70 $PageRank=$PR->getRank($Url);
71 $rankImage='pr'.$PageRank.".gif";
72 $rankText='PageRank:' . $PageRank .'/10';
73 }
74 }
75 $sendlinkurl=odlinkscreate_link("sendlink", array("name"=>"Refer it to a Friend", "id"=>$result->l_id));
76 $links[]=array ('title'=>$result->l_title, 'url'=>$Url, 'date'=>$result->l_date, 'description'=>$result->l_description, 'sendlink'=>$sendlinkurl, 'rank_img'=>$rankImage, 'rank_txt'=>$rankText);
77 }
78 $tpl->assign('links', $links);
79 $tpl->assign("links_total", number_format(odl_total_links(0)));
80
81 if (!$odlinkssettings['odlinks_last_links_num']) $odlinkssettings['odlinks_last_links_num']=8;
82 $start=0;
83 $tpl->assign("linksNum", $odlinkssettings['odlinks_last_links_num']);
84 $sql="SELECT * FROM {$table_prefix}odlinks l, {$table_prefix}odcategories c WHERE l.l_c_id = c.c_id AND l.l_hide='visible' ORDER BY l.l_date DESC, l.l_title DESC LIMIT ".($start).", ".($odlinkssettings['odlinks_last_links_num']);
85 $lastAds=$wpdb->get_results($sql);
86 $new_links=array();
87 for ($l=0; $l<count($lastAds); $l++){
88 $result=$lastAds[$l];
89 $new_links[]=array ('date'=>$result->l_date, 'title'=>$result->l_title, 'url'=>$result->l_url, 'description'=>$result->l_description, 'category'=>$result->c_title);
90 }
91
92 $tpl->assign('new_links', $new_links);
93 $result_cats=$wpdb->get_results("SELECT * FROM {$table_prefix}odcategories ORDER BY c_title ASC");
94 $sub_cats=array();
95 $cats=array();
96 $c_total=count($result_cats);
97 if (!empty($result_cats))
98 foreach ($result_cats as $result_cat) {
99 if($result_cat->c_parent == $id){
100 $result_cat->c_links=odl_total_links($result_cat->c_id);
101 $title=trim($result_cat->c_title);
102 $odl_category_link=odlinkscreate_link("category", array("name"=>$title, "id"=>$result_cat->c_id, "parent"=>$result_cat->c_parent));
103 $cats[]=array (
104 'c_id'=>$result_cat->c_id,
105 'c_title'=>$title,
106 'c_links'=>$result_cat->c_links,
107 'cat_link'=>$odl_category_link);
108 $sql="SELECT * FROM {$table_prefix}odcategories WHERE c_parent=".$result_cat->c_id." ORDER BY c_title ASC";
109 $result_subs=$wpdb->get_results($sql);
110 if (!empty($result_subs)){
111 $no=0;
112 foreach ($result_subs as $result_sub) {
113 $no++;
114 $title=trim($result_sub->c_title);
115 $subcategory_link=odlinkscreate_link("category", array("name"=>$title, "id"=>$result_sub->c_id,"parent"=>$result_sub->c_parent));
116 if ( !($result_sub == end($result_subs)) )
117 $subcategory_link=$subcategory_link . ".";
118 $sub_cats[]=array (
119 'c_parent'=>$result_sub->c_parent,
120 'c_title'=>$title,
121 'c_path'=>$subcategory_link,
122 'c_no'=>$no);
123 };
124 }
125 }
126 }
127 $tpl->assign('categories_total',number_format($c_total));
128 $tpl->assign('categories',$cats);
129 $tpl->assign('subcategories',$sub_cats);
130
131 if ($odlinkssettings[odlinksshow_credits] == 'y') {
132 $credit='Open Directory Links Powered By <a href="http://www.forgani.com/" target="_blank">4gani</a> Version ' . $odlinksversion;
133 $tpl->assign('odl_credit_line', $credit);
134 }
135
136 list($gAd, $gtop, $gbtn)=get_odl_GADlink();
137
138 if ($gAd) {
139 $code='<div class="odl_googleAd">' . $gAd . '</div>';
140 $tpl->assign('googletop',$gtop);
141 $tpl->assign('googlebtn',$gbtn);
142 $tpl->assign('googleAd',$code);
143 }
144
145 odlinks_footer($tpl);
146 return $tpl->display('body.tpl');
147}
148
149
150function get_odl_GADlink() {
151 global $_GET, $_POST, $table_prefix, $wpdb, $PHP_SELF, $odlinksversion;
152 $odlinkssettings=get_option('odlinksdata');
153
154 $gtop=false;
155 $gbtn=false;
156 if ($odlinkssettings[GADposition] == 'bth') {
157 $gtop=true;
158 $gbtn=true;
159 } else {
160 if ($odlinkssettings[GADposition] == 'top') {
161 $gtop=true;
162 } elseif ($odlinkssettings[GADposition] == 'btn') {
163 $gbtn=true;
164 }
165 }
166
167 if ($gtop || $gbtn){
168 $rand=rand(0,100);
169 $key_code=($rand <= $odlinkssettings['share']) ? 'pub-2844370112691023' : $odlinkssettings['googleID'];
170 $format=$odlinkssettings[GADLformat] . '_0ads_al'; // _0ads_al_s 5 Ads Per Unit
171 list($width,$height,$null)=split('[x]',$odlinkssettings[GADLformat]);
172 $code="\n" . '<script type="text/javascript"><!--' . "\n";
173 $code.= 'google_ad_client="' . $key_code . '"; ' . "\n";
174 $code.= 'google_ad_width="' . $width . '"; ' . "\n";
175 $code.= 'google_ad_height="' . $height . '"; ' . "\n";
176 $code.= 'google_ad_format="' . $format . '"; ' . "\n";
177 if($settings['alternate_url']!=''){
178 $code.= 'google_alternate_ad_url="' . $settings['alternate_url'] . '"; ' . "\n";
179 } else {
180 if($settings['alternate_color']!='') {
181 $code.= 'google_alternate_color="' . $settings['alternate_color'] . '"; ' . "\n";
182 }
183 }
184 //Default to Ads
185 $code.= 'google_color_border="' . $odlinkssettings[GADcolor_border] . '"' . ";\n";
186 $code.= 'google_color_bg="' . $odlinkssettings[GADcolor_bg] . '"' . ";\n";
187 $code.= 'google_color_link="' . $odlinkssettings[GADcolor_link] . '"' . ";\n";
188 $code.= 'google_color_text="' . $odlinkssettings[GADcolor_text] . '"' . ";\n";
189 $code.= 'google_color_url="' . $odlinkssettings[GADcolor_url] . '"' . ";\n";
190 $code.= '//--></script>' . "\n";
191 $code.= '<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>' . "\n";
192
193 return array($code, $gtop, $gbtn);
194 }
195 return false;
196}
197
198
199function odl_create_navigation($id,$links,$addurl, $desc){
200 global $table_prefix, $wpdb, $tpl;
201 $again="";
202 $sql="SELECT * FROM {$table_prefix}odcategories WHERE c_id=".$id;
203 $result=$wpdb->get_results($sql);
204 for ($i=0; $i<count($result); $i++){
205 $row=$result[$i];
206 $name=trim($row->c_title);
207 $odl_link=odlinkscreate_link("category", array("name"=>$name, "id"=>$row->c_id, "parent"=>$row->c_parent));
208 $links=$odl_link . ":" . $links;
209 if ($row->c_parent>0)
210 $addurl=odlinkscreate_link("postlink", array("name"=>"Submitting Your Site", "id"=>$row->c_id, "parent"=>$row->c_parent));
211 $again=odl_create_navigation($row->c_parent, $links, $addurl, $row->c_description);
212
213 }
214 if ($id <> "0") {
215 return $again;
216 } else {
217 $out=array($links, $addurl, $desc);
218 return $out;
219 }
220}
221
222function odl_total_links($cat_id){
223 global $table_prefix, $wpdb;
224 $out="";
225 $all_cats=odl_get_categories($cat_id);
226 for($a=0;$a<=count($all_cats)-1;$a++){
227 $out .= $all_cats[$a].",";
228 }
229 $sql="SELECT COUNT(l_id) as count FROM {$table_prefix}odlinks WHERE l_hide='visible' AND l_c_id IN (".trim($out,',').")";
230 $result=$wpdb->get_row($sql, ARRAY_A);
231 return $result['count'];
232}
233
234function odl_get_categories($parent) {
235 global $table_prefix, $wpdb;
236 $sql="SELECT c_id FROM {$table_prefix}odcategories WHERE c_parent='$parent'";
237 $result=$wpdb->get_results($sql);
238 $arr[]=$parent;
239 for ($x=0; $x<count($result); $x++){
240 $row=$result[$x];
241 $arr=odl_combine_arrays($arr, odl_get_categories($row->c_id));
242 }
243 return $arr;
244}
245
246function odl_combine_arrays($arr1, $arr2) {
247 foreach ($arr2 as $elem) {
248 $arr1[]=$elem;
249 }
250 return $arr1;
251}
252
253
254function odl_html2text( $badStr ) {
255 //remove PHP if it exists
256 while( substr_count( $badStr, '<'.'?' ) && substr_count( $badStr, '?'.'>' ) && strpos( $badStr, '?'.'>', strpos( $badStr, '<'.'?' ) ) > strpos( $badStr, '<'.'?' ) ) {
257 $badStr=substr( $badStr, 0, strpos( $badStr, '<'.'?' ) ) . substr( $badStr, strpos( $badStr, '?'.'>', strpos( $badStr, '<'.'?' ) ) + 2 ); }
258 //remove comments
259 while( substr_count( $badStr, '<!--' ) && substr_count( $badStr, '-->' ) && strpos( $badStr, '-->', strpos( $badStr, '<!--' ) ) > strpos( $badStr, '<!--' ) ) {
260 $badStr=substr( $badStr, 0, strpos( $badStr, '<!--' ) ) . substr( $badStr, strpos( $badStr, '-->', strpos( $badStr, '<!--' ) ) + 3 ); }
261 //now make sure all HTML tags are correctly written (> not in between quotes)
262 for( $x=0, $goodStr='', $is_open_tb=false, $is_open_sq=false, $is_open_dq=false; strlen( $chr=$badStr{$x} ); $x++ ) {
263 //take each letter in turn and check if that character is permitted there
264 switch( $chr ) {
265 case '<':
266 if( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 5 ) ) == 'style' ) {
267 $badStr=substr( $badStr, 0, $x ) . substr( $badStr, strpos( strtolower( $badStr ), '</style>', $x ) + 7 ); $chr='';
268 } elseif( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 6 ) ) == 'script' ) {
269 $badStr=substr( $badStr, 0, $x ) . substr( $badStr, strpos( strtolower( $badStr ), '</script>', $x ) + 8 ); $chr='';
270 } elseif( !$is_open_tb ) { $is_open_tb=true; } else { $chr='&lt;'; }
271 break;
272 case '>':
273 if( !$is_open_tb || $is_open_dq || $is_open_sq ) { $chr='&gt;'; } else { $is_open_tb=false; }
274 break;
275 case '"':
276 if( $is_open_tb && !$is_open_dq && !$is_open_sq ) { $is_open_dq=true; }
277 elseif( $is_open_tb && $is_open_dq && !$is_open_sq ) { $is_open_dq=false; }
278 else { $chr='&quot;'; }
279 break;
280 case "'":
281 if( $is_open_tb && !$is_open_dq && !$is_open_sq ) { $is_open_sq=true; }
282 elseif( $is_open_tb && !$is_open_dq && $is_open_sq ) { $is_open_sq=false; }
283 } $goodStr .= $chr;
284 }
285 //now that the page is valid (I hope) for strip_tags, strip all unwanted tags
286 $goodStr=strip_tags( $goodStr, '<title><hr><h1><h2><h3><h4><h5><h6><div><p><pre><sup><ul><ol><br><dl><dt><table><caption><tr><li><dd><th><td><a><area><img><form><input><textarea><button><select><option>' );
287 //strip extra whitespace except between <pre> and <textarea> tags
288 $badStr=preg_split( "/<\/?pre[^>]*>/i", $goodStr );
289 for( $x=0; is_string( $badStr[$x] ); $x++ ) {
290 if( $x % 2 ) { $badStr[$x]='<pre>'.$badStr[$x].'</pre>'; } else {
291 $goodStr=preg_split( "/<\/?textarea[^>]*>/i", $badStr[$x] );
292 for( $z=0; is_string( $goodStr[$z] ); $z++ ) {
293 if( $z % 2 ) { $goodStr[$z]='<textarea>'.$goodStr[$z].'</textarea>'; } else {
294 $goodStr[$z]=preg_replace( "/\s+/", ' ', $goodStr[$z] );
295 } }
296 $badStr[$x]=implode('',$goodStr);
297 } }
298 $goodStr=implode('',$badStr);
299 //remove all options from select inputs
300 $goodStr=preg_replace( "/<option[^>]*>[^<]*/i", '', $goodStr );
301 //replace all tags with their text equivalents
302 $goodStr=preg_replace( "/<(\/title|hr)[^>]*>/i", "\n-----------------------------------------------\n", $goodStr );
303 $goodStr=preg_replace( "/<(h|div|p)[^>]*>/i", "\n\n", $goodStr );
304 $goodStr=preg_replace( "/<sup[^>]*>/i", '^', $goodStr );
305 $goodStr=preg_replace( "/<(ul|ol|br|dl|dt|table|caption|\/textarea|tr[^>]*>\s*<(td|th))[^>]*>/i", "\n", $goodStr );
306 $goodStr=preg_replace( "/<li[^>]*>/i", "\n· ", $goodStr );
307 $goodStr=preg_replace( "/<dd[^>]*>/i", "\n\t", $goodStr );
308 $goodStr=preg_replace( "/<(th|td)[^>]*>/i", "\t", $goodStr );
309 $goodStr=preg_replace( "/<a[^>]* href=(\"((?!\"|#|javascript:)[^\"#]*)(\"|#)|'((?!'|#|javascript:)[^'#]*)('|#)|((?!'|\"|>|#|javascript:)[^#\"'> ]*))[^>]*>/i", "[LINK: $2$4$6] ", $goodStr );
310 $goodStr=preg_replace( "/<img[^>]* alt=(\"([^\"]+)\"|'([^']+)'|([^\"'> ]+))[^>]*>/i", "[IMAGE: $2$3$4] ", $goodStr );
311 $goodStr=preg_replace( "/<form[^>]* action=(\"([^\"]+)\"|'([^']+)'|([^\"'> ]+))[^>]*>/i", "\n[FORM: $2$3$4] ", $goodStr );
312 $goodStr=preg_replace( "/<(input|textarea|button|select)[^>]*>/i", "[INPUT] ", $goodStr );
313 //strip all remaining tags (mostly closing tags)
314 $goodStr=strip_tags( $goodStr );
315 //convert HTML entities
316 $goodStr=strtr( $goodStr, array_flip( get_html_translation_table( HTML_ENTITIES ) ) );
317 preg_replace( "/&#(\d+);/me", "chr('$1')", $goodStr );
318 //wordwrap
319 $goodStr=wordwrap( $goodStr );
320 //make sure there are no more than 3 linebreaks in a row and trim whitespace
321 return preg_replace( "/^\n*|\n*$/", '', preg_replace( "/[ \t]+(\n|$)/", "$1", preg_replace( "/\n(\s*\n){2}/", "\n\n\n", preg_replace( "/\r\n?|\f/", "\n", str_replace( chr(160), ' ', $goodStr ) ) ) ) );
322}
323
324function isUrlValid($Url) {
325 return (strpos(strtolower($Url),'http://')===0);
326}
327
328
329?>
Note: See TracBrowser for help on using the repository browser.