| 1 | <?php
|
| 2 | #############################################################################
|
| 3 | # Written by Jeremy Jacobs
|
| 4 | # Fitness Plus Equipment Data Sources, Inc.
|
| 5 | # http://www.FitnessRepairParts.com
|
| 6 | # This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
|
| 7 | # the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
|
| 8 | #############################################################################
|
| 9 |
|
| 10 | require_once 'DB.php';
|
| 11 | define("AMP_CONF", "/etc/amportal.conf");
|
| 12 |
|
| 13 | $amp_conf = parse_amportal_conf(AMP_CONF);
|
| 14 | if (count($amp_conf) == 0)
|
| 15 | {
|
| 16 | fatal("FAILED");
|
| 17 | }
|
| 18 |
|
| 19 | function parse_amportal_conf($filename)
|
| 20 | {
|
| 21 | $file = file($filename);
|
| 22 | foreach ($file as $line)
|
| 23 | {
|
| 24 | if (preg_match("/^\s*([a-zA-Z0-9_]+)\s*=\s*(.*)\s*([;#].*)?/",$line,$matches))
|
| 25 | {
|
| 26 | $conf[ $matches[1] ] = $matches[2];
|
| 27 | }
|
| 28 | }
|
| 29 | return $conf;
|
| 30 | }
|
| 31 |
|
| 32 | $dsn = array(
|
| 33 | 'phptype' => 'mysql',
|
| 34 | 'username' => $amp_conf['AMPDBUSER'],
|
| 35 | 'password' => $amp_conf['AMPDBPASS'],
|
| 36 | 'hostspec' => $amp_conf['AMPDBHOST'],
|
| 37 | 'database' => $amp_conf['AMPENGINE'],
|
| 38 | );
|
| 39 | $options = array();
|
| 40 | $db =& DB::connect($dsn, $options);
|
| 41 | if (PEAR::isError($db))
|
| 42 | {
|
| 43 | die($db->getMessage());
|
| 44 | }
|
| 45 |
|
| 46 | $selected_source = (isset($_REQUEST['selected_source'])) ? $_REQUEST['selected_source'] : '';
|
| 47 | $src_up = '';
|
| 48 | $src_down = '';
|
| 49 | if(isset($_REQUEST['src_up']))
|
| 50 | {
|
| 51 | $src_up = $_REQUEST['src_up'];
|
| 52 | $selected_source = (trim($src_up) != '') ? trim($src_up) : $selected_source;
|
| 53 | }
|
| 54 | if(isset($_REQUEST['src_down']))
|
| 55 | {
|
| 56 | $src_down = $_REQUEST['src_down'];
|
| 57 | $selected_source = (trim($src_down) != '') ? trim($src_down) : $selected_source;
|
| 58 | }
|
| 59 | $source_param_form = (isset($_REQUEST['source_param_form'])) ? $_REQUEST['source_param_form'] : '';
|
| 60 | $usage_mode = 'UI Display';
|
| 61 | $first_run = (isset($_REQUEST['first_run'])) ? $_REQUEST['first_run'] : '';
|
| 62 | $scheme = (isset($_REQUEST['scheme'])) ? $_REQUEST['scheme'] : '';
|
| 63 | $check_updates = (isset($_REQUEST['check_updates'])) ? $_REQUEST['check_updates'] : '';
|
| 64 | $update_file = (isset($_REQUEST['update_file'])) ? $_REQUEST['update_file'] : '';
|
| 65 | $delete_file = (isset($_REQUEST['delete_file'])) ? $_REQUEST['delete_file'] : '';
|
| 66 | $revert_file = (isset($_REQUEST['revert_file'])) ? $_REQUEST['revert_file'] : '';
|
| 67 | $src_print = array();
|
| 68 | $src_on = array();
|
| 69 | $src_cnt = 1;
|
| 70 | $src_files = array();
|
| 71 | $update_site_unavailable = false;
|
| 72 |
|
| 73 | //process updates from online server first
|
| 74 | if($update_file != '')
|
| 75 | {
|
| 76 | $parsed_url = parse_url($update_file);
|
| 77 | $parsed_path = pathinfo($parsed_url['path']);
|
| 78 |
|
| 79 | //rename and keep old file if it exists
|
| 80 | if(is_file("bin/".$parsed_path['basename']))
|
| 81 | {
|
| 82 | rename("bin/".$parsed_path['basename'],"bin/old_".$parsed_path['basename']);
|
| 83 | }
|
| 84 | copy($update_file,"bin/".$parsed_path['basename']);
|
| 85 | }
|
| 86 |
|
| 87 | //delete file if requested.
|
| 88 | if($delete_file != '')
|
| 89 | {
|
| 90 | //right now we're keeping and "old_" files just in case the user wants to revert back in the future
|
| 91 | if(is_file("bin/source-".$delete_file.".php"))
|
| 92 | {
|
| 93 | unlink("bin/source-".$delete_file.".php");
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | //revert to old file if requested
|
| 98 | if($revert_file != '')
|
| 99 | {
|
| 100 | if(is_file("bin/old_source-".$revert_file.".php"))
|
| 101 | {
|
| 102 | if(is_file("bin/source-".$revert_file.".php"))
|
| 103 | {
|
| 104 | unlink("bin/source-".$revert_file.".php");
|
| 105 | }
|
| 106 | rename("bin/old_source-".$revert_file.".php","bin/source-".$revert_file.".php");
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | //get a list of the files that are on this local server
|
| 111 | foreach (glob("bin/source-*.php") as $filename)
|
| 112 | {
|
| 113 | if($filename != '')
|
| 114 | {
|
| 115 | $source_desc = '';
|
| 116 | $source_param = array();
|
| 117 | include($filename);
|
| 118 | $this_source_name = substr(substr($filename,11),0,-4);
|
| 119 | $src_files[$this_source_name]['desc'] = $source_desc;
|
| 120 | $src_files[$this_source_name]['param'] = $source_param;
|
| 121 |
|
| 122 | //update the database if this source was the last displayed form.
|
| 123 | if($source_param_form == $this_source_name)
|
| 124 | {
|
| 125 | foreach($source_param as $key=>$val)
|
| 126 | {
|
| 127 | $sql = "REPLACE INTO superfectaconfig (source,field,value) VALUES('".substr($scheme,5).'_'.$this_source_name."','$key','".mysql_escape_string($_REQUEST[$key])."')";
|
| 128 | $db->query($sql);
|
| 129 | }
|
| 130 | }
|
| 131 | }
|
| 132 | }
|
| 133 |
|
| 134 | //go through previously enabled sources
|
| 135 | $sql = "SELECT value FROM superfectaconfig WHERE source='$scheme' AND field='sources'";
|
| 136 | $res = $db->getOne($sql);
|
| 137 | $res_src = explode(',',$res);
|
| 138 | foreach($res_src as $val)
|
| 139 | {
|
| 140 | if(($val != '') && array_key_exists($val,$src_files))
|
| 141 | {
|
| 142 | eval('$this_val = (isset($_REQUEST["'.$val.'"])) ? $_REQUEST["'.$val.'"] : "";');
|
| 143 | if(($this_val == 1) || ($first_run == 1))
|
| 144 | {
|
| 145 | $this_cnt = $src_cnt;
|
| 146 | if($val == $src_up)
|
| 147 | {
|
| 148 | $this_cnt = $src_cnt - 3;
|
| 149 | }
|
| 150 | else if($val == $src_down)
|
| 151 | {
|
| 152 | $this_cnt = $src_cnt + 3;
|
| 153 | }
|
| 154 |
|
| 155 | $src_print[$this_cnt]['name'] = $val;
|
| 156 | $src_print[$this_cnt]['value'] = 1;
|
| 157 | $src_cnt = $src_cnt + 2;
|
| 158 | $src_on[] = $val;
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | $enabled_cnt = count($src_print);
|
| 164 |
|
| 165 | //tack on the disabled sources at the end.
|
| 166 | foreach($src_files as $key=>$val)
|
| 167 | {
|
| 168 | if(!in_array($key,$src_on))
|
| 169 | {
|
| 170 | eval('$this_val = (isset($_REQUEST["'.$key.'"])) ? $_REQUEST["'.$key.'"] : "";');
|
| 171 | if($this_val == 1)
|
| 172 | {
|
| 173 | //this source just got enabled.
|
| 174 | $src_print[$src_cnt]['name'] = $key;
|
| 175 | $src_print[$src_cnt]['value'] = 1;
|
| 176 | $enabled_cnt++;
|
| 177 | $selected_source = $key;
|
| 178 | }
|
| 179 | else
|
| 180 | {
|
| 181 | $src_print[($src_cnt+200)]['name'] = $key;
|
| 182 | $src_print[($src_cnt+200)]['value'] = 0;
|
| 183 | }
|
| 184 |
|
| 185 | $src_cnt++;
|
| 186 | }
|
| 187 | }
|
| 188 |
|
| 189 | ksort($src_print);
|
| 190 |
|
| 191 | if(($check_updates == 'on')&&($debug != 'yes'))
|
| 192 | {
|
| 193 | $update_array = array();
|
| 194 | $update_content = get_url_contents('http://projects.colsolgrp.net/projects/list_files/superfecta');
|
| 195 | if(($update_content == '') || (strpos($update_content,'The system is currently in Maintenance Mode. Please try again later.') !== false))
|
| 196 | {
|
| 197 | //site un-available, give error.
|
| 198 | $update_site_unavailable = true;
|
| 199 | $check_updates = 'off';
|
| 200 | }
|
| 201 | else
|
| 202 | {
|
| 203 | $update_content = html2text($update_content);
|
| 204 | $update_content = substr($update_content,(strpos($update_content,'Caller ID Superfecta Source Files') + 33));
|
| 205 | $update_content = substr($update_content,0,strpos($update_content,'[LINK: /versions/show'));
|
| 206 | $update_content = str_replace("\t","|||",$update_content);
|
| 207 | $update_content = str_replace("]","|||",$update_content);
|
| 208 | $update_content = str_replace("\n","",$update_content);
|
| 209 | $tmp_array = explode("[LINK: ",$update_content);
|
| 210 | foreach($tmp_array as $val)
|
| 211 | {
|
| 212 | $tmp2_array = explode("|||",$val);
|
| 213 | if(!empty($tmp2_array[0]))
|
| 214 | {
|
| 215 | $this_source_name = substr(substr(trim($tmp2_array[1]),7),0,-4);
|
| 216 | $update_array[$this_source_name]['link'] = "http://projects.colsolgrp.net".trim($tmp2_array[0]);
|
| 217 | $update_array[$this_source_name]['date'] = strtotime(trim($tmp2_array[2])) - (60*60*24); //to correct for time zones, give a time that is 24 hours older than the file actually is.
|
| 218 | }
|
| 219 | }
|
| 220 | //print_r($update_array);
|
| 221 | }
|
| 222 | }
|
| 223 |
|
| 224 | print '<input type="hidden" name="src_up" value="">
|
| 225 | <input type="hidden" name="src_down" value="">
|
| 226 | <input type="hidden" name="selected_source" value="">
|
| 227 | <input type="hidden" name="update_file" value="">
|
| 228 | <input type="hidden" name="delete_file" value="">
|
| 229 | <input type="hidden" name="revert_file" value=""><font size=2>
|
| 230 | <input type="checkbox" name="check_updates" value="yes" ';
|
| 231 | if($check_updates == 'on') { print ' checked'; }
|
| 232 | print ' onClick="document.forms.CIDSources.submit();"> Check for Data Source File updates online.<br></font>';
|
| 233 | if($update_site_unavailable)
|
| 234 | {
|
| 235 | //print a message displaying a site unavailable message.
|
| 236 | print '<span style="color:red;">The update site is currently unavailable.</span>';
|
| 237 | }
|
| 238 | print '<table border="0" id="table1" cellspacing="0" cellpadding="2">
|
| 239 | <tr>
|
| 240 | <td> </td>
|
| 241 | <td> </td>
|
| 242 | <td> </td>
|
| 243 | <td> </td>
|
| 244 | <td><strong>Data Source Name</strong></td>
|
| 245 | <td align="center"><strong>Disabled</strong></td>
|
| 246 | <td align="center"><strong>Enabled</strong></td>';
|
| 247 | if(($selected_source != '') && !empty($src_files[$selected_source]['param']))
|
| 248 | {
|
| 249 | print '<td rowspan="40" bgcolor="#E0E0E0" valign="top" align="center" width="350">
|
| 250 | <strong>'.str_replace('_',' ',$selected_source).' Options</strong><br>';
|
| 251 | if(empty($src_files[$selected_source]['param']))
|
| 252 | {
|
| 253 | print '<br><br>Nothing to configure';
|
| 254 | }
|
| 255 | else
|
| 256 | {
|
| 257 | $value_array = array();
|
| 258 | $sql = "SELECT field,value FROM superfectaconfig WHERE source='".substr($scheme,5).'_'.$selected_source."'";
|
| 259 | $res = $db->query($sql);
|
| 260 | while ($row = $res->fetchRow())
|
| 261 | {
|
| 262 | $value_array[$row[0]] = $row[1];
|
| 263 | }
|
| 264 | print '<input type="hidden" name="source_param_form" value="'.$selected_source.'">
|
| 265 | <table border="0">';
|
| 266 | foreach($src_files[$selected_source]['param'] as $key=>$val)
|
| 267 | {
|
| 268 | //set default value if one is specified and the parameter currently has no value.
|
| 269 | $value_array[$key] = (empty($value_array[$key]) && !empty($val['default'])) ? $val['default'] : $value_array[$key];
|
| 270 |
|
| 271 | print '<tr>
|
| 272 | <td valign="top" align="right"><a href="javascript: return false;" class="info">'.str_replace('_',' ',$key).':<span>'.$val['desc'].'</span></a></td>
|
| 273 | <td align="left">';
|
| 274 | if($val['type'] == 'select')
|
| 275 | {
|
| 276 | print '<select name="'.$key.'">';
|
| 277 | foreach($val['option'] as $key2=>$val2)
|
| 278 | {
|
| 279 | print '<option ';
|
| 280 | if($key2 == $value_array[$key]) { print 'selected="" '; }
|
| 281 | print ' value="'.$key2.'"';
|
| 282 | print '>'.$val2.'</option>';
|
| 283 | }
|
| 284 | print '</select>';
|
| 285 | }
|
| 286 | else if($val['type'] == 'number')
|
| 287 | {
|
| 288 | print '<input type="text" size="10" maxlength="10" name="'.$key.'" value="'.$value_array[$key].'">';
|
| 289 | }
|
| 290 | else if($val['type'] == 'textarea')
|
| 291 | {
|
| 292 | print '<textarea rows="5" cols="25" name="'.$key.'">'.$value_array[$key].'</textarea>';
|
| 293 | }
|
| 294 | else if($val['type'] == 'checkbox')
|
| 295 | {
|
| 296 | print '<input type="checkbox" name="'.$key.'"';
|
| 297 | if($value_array[$key] == 'on') { print ' checked'; }
|
| 298 | print '>';
|
| 299 | }
|
| 300 | else
|
| 301 | {
|
| 302 | print '<input type="'.$val['type'].'" size="23" maxlength="255" name="'.$key.'" value="'.$value_array[$key].'">';
|
| 303 | }
|
| 304 | print '</td>
|
| 305 | </tr>';
|
| 306 | }
|
| 307 | print '</table><br><br>
|
| 308 | <input type="submit" value="Apply">';
|
| 309 | }
|
| 310 | print '</td>';
|
| 311 | }
|
| 312 | print '</tr>';
|
| 313 | $comma = '';
|
| 314 | $src_list = '';
|
| 315 | $enabled_src_list = '';
|
| 316 | $count = 0;
|
| 317 | foreach($src_print as $val)
|
| 318 | {
|
| 319 | $count++;
|
| 320 | $src_list .= $comma.$val['name'];
|
| 321 | if($val['value'] == 1)
|
| 322 | {
|
| 323 | $enabled_src_list .= $comma.$val['name'];
|
| 324 | }
|
| 325 |
|
| 326 | if($val['name'] == $selected_source)
|
| 327 | {
|
| 328 | print '<tr style="background-color:#E0E0E0;">';
|
| 329 | }
|
| 330 | else
|
| 331 | {
|
| 332 | print '<tr>';
|
| 333 | }
|
| 334 | print '<td>';
|
| 335 | if(($comma != '') && ($val['value'] == 1))
|
| 336 | {
|
| 337 | print '<a href="javascript:document.forms.CIDSources.src_up.value=\''.$val['name'].'\';document.forms.CIDSources.submit();"><img src="images/scrollup.gif" border="0" alt="Up Arrow" title="Move Up List"></a>';
|
| 338 | }
|
| 339 | else
|
| 340 | {
|
| 341 | print ' ';
|
| 342 | }
|
| 343 | print '</td>
|
| 344 | <td>';
|
| 345 | if(($val['value'] == 1) && ($count < $enabled_cnt))
|
| 346 | {
|
| 347 | print '<a href="javascript:document.forms.CIDSources.src_down.value=\''.$val['name'].'\';document.forms.CIDSources.submit();"><img src="images/scrolldown.gif" border="0" alt="Down Arrow" title="Move Down List"></a>';
|
| 348 | }
|
| 349 | else
|
| 350 | {
|
| 351 | print ' ';
|
| 352 | }
|
| 353 | print '</td>
|
| 354 | <td>
|
| 355 | <a href="javascript:document.forms.CIDSources.delete_file.value=\''.$val['name'].'\';document.forms.CIDSources.submit();"><img src="modules/superfecta/delete.gif" border="0" alt="Delete Button" title="Delete This Source File"></a>
|
| 356 | </td>
|
| 357 | <td>';
|
| 358 | if(is_file("bin/old_source-".$val['name'].".php"))
|
| 359 | {
|
| 360 | print '<a href="javascript:document.forms.CIDSources.revert_file.value=\''.$val['name'].'\';document.forms.CIDSources.submit();"><img src="modules/superfecta/revert.gif" border="0" alt="Revert Button" title="Revert to previous version of this file."></a>';
|
| 361 | }
|
| 362 | else
|
| 363 | {
|
| 364 | print ' ';
|
| 365 | }
|
| 366 | print '</td>
|
| 367 | <td>
|
| 368 | <a href="javascript:document.forms.CIDSources.selected_source.value=\''.$val['name'].'\';document.forms.CIDSources.submit();" class="info">'.str_replace('_',' ',$val['name']).': ';
|
| 369 | if(!empty($src_files[$val['name']]['param']))
|
| 370 | {
|
| 371 | print ' ==>';
|
| 372 | }
|
| 373 | print '<span>'.$src_files[$val['name']]['desc'].'</span></a>';
|
| 374 | //check to see if there are updates.
|
| 375 | if($check_updates == 'on')
|
| 376 | {
|
| 377 | if(key_exists($val['name'],$update_array))
|
| 378 | {
|
| 379 | $this_last_update = filemtime("bin/source-".$val['name'].".php");
|
| 380 | if($update_array[$val['name']]['date'] > $this_last_update)
|
| 381 | {
|
| 382 | print ' <a href="javascript:document.forms.CIDSources.update_file.value=\''.$update_array[$val['name']]['link'].'\';document.forms.CIDSources.submit();">update available</a>';
|
| 383 | }
|
| 384 | }
|
| 385 | else
|
| 386 | {
|
| 387 | print ' unsupported module';
|
| 388 | }
|
| 389 | }
|
| 390 | print '</td>
|
| 391 | <td align="center"><input type="radio" value="0" name="'.$val['name'].'"'.(($val['value'] == 0) ? ' checked' : '').' onclick="document.forms.CIDSources.submit();"></td>
|
| 392 | <td align="center"><input type="radio" value="1" name="'.$val['name'].'"'.(($val['value'] == 1) ? ' checked' : '').' onclick="document.forms.CIDSources.submit();"></td>
|
| 393 | </tr>';
|
| 394 | $comma = ',';
|
| 395 | }
|
| 396 |
|
| 397 | //create a list of source files that can still be added to the server.
|
| 398 | if($check_updates == 'on')
|
| 399 | {
|
| 400 | $options_list = '';
|
| 401 | foreach($update_array as $key=>$val)
|
| 402 | {
|
| 403 | $in_array = false;
|
| 404 | foreach($src_print as $val2)
|
| 405 | {
|
| 406 | if($val2['name'] == $key)
|
| 407 | {
|
| 408 | $in_array = true;
|
| 409 | break;
|
| 410 | }
|
| 411 | }
|
| 412 | if(!$in_array)
|
| 413 | {
|
| 414 | $options_list .= '<OPTION value="'.$val['link'].'">'.str_replace('_',' ',$key).'</OPTION>';
|
| 415 | }
|
| 416 | }
|
| 417 |
|
| 418 | if(!empty($options_list))
|
| 419 | {
|
| 420 | print '<tr>
|
| 421 | <td>
|
| 422 | <a href="javascript:document.forms.CIDSources.update_file.value=document.forms.CIDSources.add_source_file.value;document.forms.CIDSources.submit();"><img src="images/scrollup.gif" border="0" alt="Up Arrow" title="Move Up List"></a>
|
| 423 | </td>
|
| 424 | <td> </td>
|
| 425 | <td> </td>
|
| 426 | <td> </td>
|
| 427 | <td>
|
| 428 | <SELECT name="add_source_file">
|
| 429 | <OPTION value="">Select One</OPTION>'.$options_list.'
|
| 430 | </SELECT>
|
| 431 | </td>
|
| 432 | <td> </td>
|
| 433 | <td> </td>
|
| 434 | </tr>';
|
| 435 | }
|
| 436 | }
|
| 437 | print '</table>
|
| 438 | <input type="hidden" name="src_list" value="'.$src_list.'">';
|
| 439 |
|
| 440 | $sql = "REPLACE INTO superfectaconfig (source,field,value) VALUES('$scheme','sources','$enabled_src_list')";
|
| 441 | $db->query($sql);
|
| 442 |
|
| 443 | /**
|
| 444 | Returns the content of a URL.
|
| 445 | */
|
| 446 | function get_url_contents($url)
|
| 447 | {
|
| 448 | $crl = curl_init();
|
| 449 | $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1";
|
| 450 | curl_setopt($crl,CURLOPT_USERAGENT,$useragent);
|
| 451 | curl_setopt($crl,CURLOPT_URL,$url);
|
| 452 | curl_setopt($crl,CURLOPT_RETURNTRANSFER,true);
|
| 453 | curl_setopt($crl,CURLOPT_CONNECTTIMEOUT,5);
|
| 454 | curl_setopt($crl,CURLOPT_FAILONERROR,true);
|
| 455 | curl_setopt($crl,CURLOPT_TIMEOUT,5);
|
| 456 | $ret = trim(curl_exec($crl));
|
| 457 |
|
| 458 | if($ret === false)
|
| 459 | {
|
| 460 | $ret = '';
|
| 461 | }
|
| 462 | //something in curl is causing a return of "1" if the page being called is valid, but completely empty.
|
| 463 | //to get rid of this, I'm doing a nasty hack of just killing results of "1".
|
| 464 | if($ret == '1')
|
| 465 | {
|
| 466 | $ret = '';
|
| 467 | }
|
| 468 | curl_close($crl);
|
| 469 | return $ret;
|
| 470 | }
|
| 471 |
|
| 472 | function html2text($badStr)
|
| 473 | {
|
| 474 | //remove PHP if it exists
|
| 475 | while(substr_count( $badStr, '<'.'?' ) && substr_count( $badStr, '?'.'>' ) && strpos( $badStr, '?'.'>', strpos( $badStr, '<'.'?' ) ) > strpos( $badStr, '<'.'?' ))
|
| 476 | {
|
| 477 | $badStr = substr( $badStr, 0, strpos( $badStr, '<'.'?' ) ) . substr( $badStr, strpos( $badStr, '?'.'>', strpos( $badStr, '<'.'?' ) ) + 2 );
|
| 478 | }
|
| 479 |
|
| 480 | //remove comments
|
| 481 | while( substr_count( $badStr, '<!--' ) && substr_count( $badStr, '-->' ) && strpos( $badStr, '-->', strpos( $badStr, '<!--' ) ) > strpos( $badStr, '<!--' ) )
|
| 482 | {
|
| 483 | $badStr = substr( $badStr, 0, strpos( $badStr, '<!--' ) ) . substr( $badStr, strpos( $badStr, '-->', strpos( $badStr, '<!--' ) ) + 3 );
|
| 484 | }
|
| 485 |
|
| 486 | //now make sure all HTML tags are correctly written (> not in between quotes)
|
| 487 | for( $x = 0, $goodStr = '', $is_open_tb = false, $is_open_sq = false, $is_open_dq = false; strlen( $chr = $badStr{$x} ); $x++ )
|
| 488 | {
|
| 489 | //take each letter in turn and check if that character is permitted there
|
| 490 | switch($chr)
|
| 491 | {
|
| 492 | case '<':
|
| 493 | if( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 5 ) ) == 'style' )
|
| 494 | {
|
| 495 | $badStr = substr( $badStr, 0, $x ) . substr( $badStr, strpos( strtolower( $badStr ), '</style>', $x ) + 7 ); $chr = '';
|
| 496 | }
|
| 497 | elseif( !$is_open_tb && strtolower( substr( $badStr, $x + 1, 6 ) ) == 'script' )
|
| 498 | {
|
| 499 | $badStr = substr( $badStr, 0, $x ) . substr( $badStr, strpos( strtolower( $badStr ), '</script>', $x ) + 8 ); $chr = '';
|
| 500 | }
|
| 501 | elseif( !$is_open_tb )
|
| 502 | {
|
| 503 | $is_open_tb = true;
|
| 504 | }
|
| 505 | else
|
| 506 | {
|
| 507 | $chr = '<';
|
| 508 | }
|
| 509 | break;
|
| 510 | case '>':
|
| 511 | if( !$is_open_tb || $is_open_dq || $is_open_sq )
|
| 512 | {
|
| 513 | $chr = '>';
|
| 514 | }
|
| 515 | else
|
| 516 | {
|
| 517 | $is_open_tb = false;
|
| 518 | }
|
| 519 | break;
|
| 520 | case '"':
|
| 521 | if( $is_open_tb && !$is_open_dq && !$is_open_sq )
|
| 522 | {
|
| 523 | $is_open_dq = true;
|
| 524 | }
|
| 525 | elseif( $is_open_tb && $is_open_dq && !$is_open_sq )
|
| 526 | {
|
| 527 | $is_open_dq = false;
|
| 528 | }
|
| 529 | else
|
| 530 | {
|
| 531 | $chr = '"';
|
| 532 | }
|
| 533 | break;
|
| 534 | case "'":
|
| 535 | if( $is_open_tb && !$is_open_dq && !$is_open_sq )
|
| 536 | {
|
| 537 | $is_open_sq = true;
|
| 538 | }
|
| 539 | elseif( $is_open_tb && !$is_open_dq && $is_open_sq )
|
| 540 | {
|
| 541 | $is_open_sq = false;
|
| 542 | }
|
| 543 | }
|
| 544 | $goodStr .= $chr;
|
| 545 | }
|
| 546 |
|
| 547 | //now that the page is valid (I hope) for strip_tags, strip all unwanted tags
|
| 548 | $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>' );
|
| 549 |
|
| 550 | //strip extra whitespace except between <pre> and <textarea> tags
|
| 551 | $badStr = preg_split( "/<\/?pre[^>]*>/i", $goodStr );
|
| 552 | for( $x = 0; is_string( $badStr[$x] ); $x++ )
|
| 553 | {
|
| 554 | if( $x % 2 )
|
| 555 | {
|
| 556 | $badStr[$x] = '<pre>'.$badStr[$x].'</pre>';
|
| 557 | }
|
| 558 | else
|
| 559 | {
|
| 560 | $goodStr = preg_split( "/<\/?textarea[^>]*>/i", $badStr[$x] );
|
| 561 | for( $z = 0; is_string( $goodStr[$z] ); $z++ )
|
| 562 | {
|
| 563 | if($z % 2)
|
| 564 | {
|
| 565 | $goodStr[$z] = '<textarea>'.$goodStr[$z].'</textarea>';
|
| 566 | }
|
| 567 | else
|
| 568 | {
|
| 569 | $goodStr[$z] = preg_replace( "/\s+/", ' ', $goodStr[$z] );
|
| 570 | }
|
| 571 | }
|
| 572 | $badStr[$x] = implode('',$goodStr);
|
| 573 | }
|
| 574 | }
|
| 575 | $goodStr = implode('',$badStr);
|
| 576 | //remove all options from select inputs
|
| 577 | $goodStr = preg_replace( "/<option[^>]*>[^<]*/i", '', $goodStr );
|
| 578 | //replace all tags with their text equivalents
|
| 579 | $goodStr = preg_replace( "/<(\/title|hr)[^>]*>/i", "\n --------------------\n", $goodStr );
|
| 580 | $goodStr = preg_replace( "/<(h|div|p)[^>]*>/i", "\n\n", $goodStr );
|
| 581 | $goodStr = preg_replace( "/<sup[^>]*>/i", '^', $goodStr );
|
| 582 | $goodStr = preg_replace( "/<(ul|ol|br|dl|dt|table|caption|\/textarea|tr[^>]*>\s*<(td|th))[^>]*>/i", "\n", $goodStr );
|
| 583 | $goodStr = preg_replace( "/<li[^>]*>/i", "\n· ", $goodStr );
|
| 584 | $goodStr = preg_replace( "/<dd[^>]*>/i", "\n\t", $goodStr );
|
| 585 | $goodStr = preg_replace( "/<(th|td)[^>]*>/i", "\t", $goodStr );
|
| 586 | $goodStr = preg_replace( "/<a[^>]* href=(\"((?!\"|#|javascript:)[^\"#]*)(\"|#)|'((?!'|#|javascript:)[^'#]*)('|#)|((?!'|\"|>|#|javascript:)[^#\"'> ]*))[^>]*>/i", "[LINK: $2$4$6] ", $goodStr );
|
| 587 | $goodStr = preg_replace( "/<img[^>]* alt=(\"([^\"]+)\"|'([^']+)'|([^\"'> ]+))[^>]*>/i", "[IMAGE: $2$3$4] ", $goodStr );
|
| 588 | $goodStr = preg_replace( "/<form[^>]* action=(\"([^\"]+)\"|'([^']+)'|([^\"'> ]+))[^>]*>/i", "\n[FORM: $2$3$4] ", $goodStr );
|
| 589 | $goodStr = preg_replace( "/<(input|textarea|button|select)[^>]*>/i", "[INPUT] ", $goodStr );
|
| 590 | //strip all remaining tags (mostly closing tags)
|
| 591 | $goodStr = strip_tags( $goodStr );
|
| 592 | //convert HTML entities
|
| 593 | $goodStr = strtr( $goodStr, array_flip( get_html_translation_table( HTML_ENTITIES ) ) );
|
| 594 | preg_replace( "/&#(\d+);/me", "chr('$1')", $goodStr );
|
| 595 | //wordwrap
|
| 596 | $goodStr = wordwrap( $goodStr );
|
| 597 | //make sure there are no more than 3 linebreaks in a row and trim whitespace
|
| 598 | 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 ) ) ) ) );
|
| 599 | }
|
| 600 | ?> |