/www/modules/mod_grabber/helper.php

https://github.com/amet17/webstar · PHP · 126 lines · 119 code · 6 blank · 1 comment · 9 complexity · c38ff15bd5decc6ed47abed23942fa1c MD5 · raw file

  1. <?php
  2. defined('_JEXEC') or die('Direct Access to this location is not allowed.');
  3. class ModGrabberHelper
  4. {
  5. function grabhtml($url, $start, $end, $show, $regexp, $mestype)
  6. {
  7. global $mainframe;
  8. set_time_limit(0); // ÷òîá óñïåëî äîãðàáèòü âñ¸
  9. ignore_user_abort(); // ãðàáèòü íå ñìîòðÿ íà îøèáêè êëèåíòà
  10. if (!function_exists("curl_init")) {
  11. @$file = file_get_contents( $url );
  12. }else{
  13. $agent = 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)';
  14. $referer = $url;
  15. $ch = curl_init();
  16. curl_setopt ($ch, CURLOPT_URL,$url);
  17. curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  18. curl_setopt($ch, CURLOPT_REFERER, $referer);
  19. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  20. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  21. $file = curl_exec ($ch);
  22. curl_close($ch);
  23. }
  24. if( $file )
  25. {
  26. if( preg_match_all($regexp, $file, $match) )
  27. {
  28. foreach( $match[0] as $html )
  29. {
  30. if( !$show )
  31. {
  32. $html = str_replace( $start, "", $html );
  33. $html = str_replace( $end, "", $html );
  34. return $html;
  35. }else{
  36. return $html;
  37. }
  38. }
  39. }else{
  40. switch ($mestype) {
  41. case 1:
  42. return JText::_('NOTAGS');
  43. break;
  44. case 2:
  45. $mailfrom = $mainframe->getCfg( 'mailfrom' );
  46. $fromname = $mainframe->getCfg( 'fromname' );
  47. $title = JText::_('NOTAGS MES');
  48. $message = $url.': '.JText::_('NOTAGS MES');
  49. return JUtility::sendMail($mailfrom, $fromname, $mailfrom, $title, $message);
  50. break;
  51. case 3:
  52. $mailfrom = $mainframe->getCfg( 'mailfrom' );
  53. $fromname = $mainframe->getCfg( 'fromname' );
  54. $title = JText::_('NOTAGS MES');
  55. $message = $url.': '.JText::_('NOTAGS MES');
  56. JUtility::sendMail($mailfrom, $fromname, $mailfrom, $title, $message);
  57. return JText::_('NOTAGS');
  58. break;
  59. }
  60. }
  61. }else{
  62. switch ($mestype) {
  63. case 1:
  64. return JText::_('NOSITE');
  65. break;
  66. case 2:
  67. $mailfrom = $mainframe->getCfg( 'mailfrom' );
  68. $fromname = $mainframe->getCfg( 'fromname' );
  69. $title = JText::_('NOSITE MES');
  70. $message = $url.': '.JText::_('NOSITE MES');
  71. return JUtility::sendMail($mailfrom, $fromname, $mailfrom, $title, $message);
  72. break;
  73. case 3:
  74. $mailfrom = $mainframe->getCfg( 'mailfrom' );
  75. $fromname = $mainframe->getCfg( 'fromname' );
  76. $title = JText::_('NOSITE MES');
  77. $message = $url.': '.JText::_('NOSITE MES');
  78. JUtility::sendMail($mailfrom, $fromname, $mailfrom, $title, $message);
  79. return JText::_('NOSITE');
  80. break;
  81. }
  82. }
  83. }
  84. function correct_charset($fromcharset, $tocharset, $html)
  85. {
  86. //(($html==JText::_('NOTAGS')) || ($html==JText::_('NOSITE'))) ? return $html : return iconv ($fromcharset, $tocharset, $html);
  87. if(($html==JText::_('NOTAGS')) || ($html==JText::_('NOSITE')))
  88. {
  89. return $html;
  90. }else{
  91. return iconv ($fromcharset, $tocharset, $html);
  92. }
  93. }
  94. function correct_links($linksrc, $oldlinksrc, $linkhref, $oldlinkhref, $html, $atr)
  95. {
  96. $oldlink = array($oldlinksrc, $oldlinkhref);
  97. $newlink = array($oldlinksrc.$linksrc, $atr.' '.$oldlinkhref.$linkhref);
  98. return str_replace($oldlink, $newlink, $html);
  99. }
  100. function create_cache_file($tmpfname, $html)
  101. {
  102. if (file_exists($tmpfname))
  103. {
  104. unlink($tmpfname);
  105. $handle = fopen($tmpfname, "a+");
  106. fwrite($handle, $html);
  107. fclose($handle);
  108. }else{
  109. $handle = fopen($tmpfname, "a+");
  110. fwrite($handle, $html);
  111. fclose($handle);
  112. }
  113. }
  114. function tmpl_parser($regex, $html_from_template_path)
  115. {
  116. preg_match("#{".$regex."}(.*?){/".$regex."}#s", $html_from_template_path, $matches);
  117. return $matches[1];
  118. }
  119. }
  120. ?>