Advertisement
terorama

parser / parser.php

Aug 21st, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.19 KB | None | 0 0
  1.  
  2. <?php
  3.  
  4. if ($_SERVER["SERVER_NAME"]=='localhost')
  5.    require 'alinc.php';
  6. else
  7.    require 'statusparse.inc.php';
  8.    
  9. //---------------------------
  10. function wr($instr) {
  11.  
  12.    global $statusparse;
  13.    
  14.    $statusparse->logt($instr);
  15. }
  16.  
  17. //-----------------------------------------------------------------
  18. //                     parsing
  19. //-----------------------------------------------------------------
  20. function parse() {
  21.  
  22.    global $statusparse;
  23.    
  24.    
  25.    $url8 = $statusparse->urls[$statusparse->step]["url"];
  26.    
  27.    $ztmuri=parse_url($url8);  
  28.    $hoststr=$ztmuri['scheme'].'://'.$ztmuri['host'];
  29.    $pathstr=$ztmuri['path'];
  30.    
  31.    $deep8 = $statusparse->urls[$statusparse->step]["deep"];
  32.    $regexp8= $statusparse->regarr[$deep8];
  33.     $pattern="/".$regexp8."/ims";
  34.    
  35.    wr('steps down='.$deep8);
  36.    wr('get address '.$url8);
  37.    wr('current pattern '.$regexp8);
  38.    
  39.    /*if ($deep8==count($statusparse->regarr)) {
  40.       wr('load content '.$url8);
  41.       //wr(file_get_contents('http://localhost/learn/picport/req4.php?reqg=postform&'.
  42.       //         'picaddr='.$url8));
  43.      
  44.       $zfl=file_get_contents($url8);
  45.       if ($zfl==false) wr('error');
  46.       $znn=array_pop(explode('/',$url8));
  47.       wr($znn);
  48.       file_put_contents('picn/'.$znn);
  49.       return;
  50.    }*/
  51.    //------------------------------------------------
  52.    //------------------------------------------------
  53.    $teststr=file_get_contents($url8);
  54.    
  55.    if (preg_match('/<meta[^>]*charset[^>]*win[^>]*1251[^>]*>/',$teststr)>0)
  56.       $teststr=iconv('windows-1251','UTF-8',$teststr);  
  57.          
  58.    preg_match_all($pattern,$teststr,$out,PREG_SET_ORDER); //FTG
  59.      
  60.     //$out=array(0=>$s.'A'.$regexp8[0].$n,1=>$s.'B'.$regexp8[0].$n,
  61.     //   2=>$s.'C'.$regexp8[0].$n,3=>$s.'D'.$regexp8[0].$n);
  62.    
  63.    //------------------------new urls
  64.    if ($deep8+1<count($statusparse->regarr)) {
  65.       for ($i=0;$i<count($out);$i++) {     
  66.          $tmp=Array();
  67.          wr('link: '.$out[$i][2]);
  68.          
  69.          if (substr($out[$i][2],0,4)!='http') {
  70.             if(substr($out[$i][2],0,1)=='?')
  71.                $tmp["url"]=$hoststr.$pathstr;
  72.             else
  73.                $tmp["url"]=$hoststr.
  74.                ((substr($out[$i][2],0,1)=='/') ? '' : '/');
  75.          }
  76.          else {
  77.             $tmp["url"]='';
  78.          };
  79.                            
  80.          $tmp["url"].=$out[$i][2];
  81.          $tmp["deep"]=$deep8+1;
  82.          $statusparse->urls[]=$tmp;      
  83.       }
  84.    }
  85.    
  86.    
  87.    if ($deep8+1==count($statusparse->regarr)) {
  88.       for ($i=0;$i<count($out);$i++) { 
  89.          wr('el: '.$out[$i][2]);
  90.          
  91.          if (substr($out[$i][2],0,4)!='http') {
  92.             if(substr($out[$i][2],0,1)=='?')
  93.                $ztmpstr=$hoststr.$pathstr;
  94.             else
  95.                $ztmpstr=$hoststr.
  96.                ((substr($out[$i][2],0,1)=='/') ? '' : '/');
  97.             }
  98.          else {
  99.                $ztmpstr='';
  100.          };
  101.          
  102.          $statusparse->elems[]=$ztmpstr.$out[$i][2];
  103.          $statusparse->log_res($ztmpstr.$out[$i][2]);
  104.          }
  105.     }
  106.    
  107.    
  108. }
  109.  
  110. //-----------------------------------------------------------------
  111. //                    output
  112. //-----------------------------------------------------------------
  113. function output() {
  114.  
  115.    global $statusparse;
  116.    if (isset($_GET["file8"])) {
  117.       $statusparse->elems=file($_GET["file8"]);
  118.    }
  119.    
  120.    //-------------------------------------------
  121.    //         formatted output
  122.    //-------------------------------------------
  123.    if (@$statusparse->formatted) {
  124.       if (@$statusparse->json_format)
  125.          //--------------------------------------------
  126.          //         json output
  127.          //--------------------------------------------
  128.          {
  129.          }
  130.          else {
  131.          
  132.          //--------------------------------------
  133.          //         html output
  134.          //--------------------------------------
  135.             if (@$statusparse->show_query) {
  136.                echo '<textarea cols="40" rows="15">'.
  137.                $statusparse->initquery.
  138.                '</textarea>';
  139.                }
  140.      
  141.             $frm_str=$statusparse->formatstring;
  142.          
  143.             for ($i=0;$i<count($statusparse->elems);$i++) {
  144.            
  145.                $tmp_str=$frm_str;
  146.                $el=$statusparse->elems[$i];
  147.                            
  148.                $tmp_str=str_replace('$uu$', $el,$tmp_str);
  149.                
  150.                echo $tmp_str;
  151.             }
  152.          }
  153.       }
  154.       //--------------------------------
  155.       //   unformatted output
  156.       //--------------------------------
  157.       else {     
  158.          header('Content-type:text/plain; charset=utf-8'); 
  159.          echo print_r($statusparse->elems);
  160.       }
  161.      
  162. }
  163.  
  164. //-----------------------------------------------------------------
  165. //                  main entrace
  166. //-----------------------------------------------------------------
  167.  
  168. if (!isset($_GET["step"])) {
  169.    
  170.       session_start();
  171.       session_destroy();
  172.       session_commit();
  173.    }
  174.    //---------------------------------
  175. session_start();
  176.  
  177.    //---------------------------------
  178.    if (isset($_SESSION["statusparse"]))
  179.       $statusparse=$_SESSION["statusparse"];
  180.    //else
  181.    //   $statusparse= new StatusParse(true);
  182.      
  183.    //---------------------------------- continue processing
  184.    if (isset($_GET["step"])) {
  185.    
  186.        //----------------async get current status
  187.        if (isset($_GET["status"])) {
  188.           if ($statusparse->step<count($statusparse->urls)) {
  189.              echo 'current step: '.$statusparse->step.'<br />';
  190.              echo 'current el: '.
  191.                 $statusparse->urls[$statusparse->step]["url"].'<br />';
  192.              echo 'current depth: '.$statusparse->urls[$statusparse->step]["deep"].'<br />';
  193.              echo 'found elems: '.count($statusparse->urls).'<br />';
  194.              
  195.              }
  196.            else
  197.               echo('parse complete');
  198.              
  199.            exit();  
  200.        }
  201.        //----------------processing
  202.        if ($statusparse->step<count($statusparse->urls)) {
  203.           parse();
  204.           $statusparse->step++;
  205.           $statusparse->n_redirects++;
  206.          
  207.           if ($statusparse->n_redirects>15)
  208.              $statusparse->n_redirects=0;
  209.              
  210.           //----------------------
  211.           $_SESSION["statusparse"]=$statusparse;         
  212.           session_commit();
  213.          
  214.           if ($statusparse->n_redirects==0) {
  215.          
  216.              echo '<head><script type="text/javascript">'.
  217.              'setTimeout(function() {location.replace("'.$_SERVER["PHP_SELF"].'?step");},1000)'.
  218.              '</script></head><body>';       
  219.              
  220.              echo 'parsing suspended to prevent error 310 <br />';
  221.              echo '<a href="'.$_SERVER["PHP_SELF"].'?step">continue</a></body>';
  222.              
  223.  
  224.              }           
  225.           else
  226.              header('Location: '.$_SERVER["PHP_SELF"].'?step');
  227.        } else
  228.        //-----------------------
  229.        {
  230.           output();
  231.        }
  232.        
  233.        //-------------------------------
  234.        
  235.    } else    
  236.       //--------------------------------- start processing
  237.       if (isset($_GET["regexp8"])) {
  238.      
  239.           $statusparse= new StatusParse(true);
  240.          
  241.           wr('start processing...');
  242.      
  243.           $statusparse->regarr=explode('|ABC|',$_GET["regexp8"]);
  244.           $statusparse->base_url=$_GET["url8"];
  245.           $statusparse->formatstring=isset($_GET["formstr8"]) ? $_GET["formstr8"] : '';
  246.          
  247.           $statusparse->formatted=($statusparse->formatstring!='');
  248.           $statusparse->json_format=isset($_GET["json8"]);
  249.           $statusparse->show_query=!isset($_GET["nta"]);     
  250.      
  251.           //$statusparse->initquery= 'http://'.$_SERVER["SERVER_NAME"].
  252.           //     $_SERVER["REQUEST_URI"].'&nta';
  253.          
  254.           $statusparse->initquery='http://'.$_SERVER["SERVER_NAME"].
  255.              $_SERVER["PHP_SELF"].'?file8='.
  256.              urlencode($statusparse->resfile).'&formstr8='.
  257.              urlencode($statusparse->formatstring);
  258.          
  259.          
  260.           $tmp=Array();
  261.           $tmp["url"]=$statusparse->base_url;
  262.           $tmp["deep"]=0;
  263.          
  264.           $statusparse->urls[]=$tmp;
  265.  
  266.           //-------------------------------
  267.           $_SESSION["statusparse"]=$statusparse;
  268.           session_commit();
  269.           header('Location: '.$_SERVER["PHP_SELF"].'?step');
  270.       }
  271.    else
  272.       if (isset($_GET["file8"])) {
  273.          $statusparse->formatstring=isset($_GET["formstr8"]) ? $_GET["formstr8"] : '';
  274.          $statusparse->formatted=($statusparse->formatstring!='');
  275.          output();
  276.       }
  277.       else
  278.          show_form();
  279.    //---------------------------------------------------------------
  280.      
  281.          
  282.     //-------------------------------------------
  283.       //               parsing
  284.       //-------------------------------------------
  285.     /*  wr('parsing...');
  286.       wr(print_r($regarr));
  287.      
  288.       error_reporting(E_ERROR | E_WARNING | E_PARSE);
  289.       $rez= parse($regarr,$_GET["url8"]);
  290.       error_reporting(-1);    */
  291.    //-------------------------------------------
  292.  
  293.    
  294. //-----------------------------------------------------------------  
  295. function show_form() {
  296. ?>
  297.  
  298. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  299. <html xmlns="http://www.w3.org/1999/xhtml">
  300. <head>
  301.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  302.    <meta http-equiv="Cache-Control" content="no-cache" />
  303.    
  304.    <title>Parser</title>
  305.    
  306.    
  307.    <style type="text/css">
  308.  
  309.       #formatted {
  310.          position:absolute;
  311.          width:390px;
  312.          height:600px;
  313.          left:10px;
  314.          top:10px;
  315.          border:solid 1px black;
  316.          overflow:hidden;
  317.          padding:10px;
  318.          
  319.          font-family:verdana;
  320.          font-size:10px;
  321.       }
  322.    
  323.    
  324.       #zform {
  325.          position:relative;
  326.          left:65%;
  327.          top:10px;
  328.          margin-left:-400px;
  329.          width:800px;
  330.        
  331.       }
  332.       label {
  333.          width: 6em;
  334.          float:left;
  335.          text-align:right;
  336.          margin-right:1em;
  337.          display:block;
  338.          font-family:verdana;
  339.          font-size:0.9em;
  340.       }
  341.      
  342.       form  p{
  343.          padding:3px;
  344.          margin:3px;
  345.        
  346.       }
  347.      
  348.       input {
  349.          font-family:courier,monospace;
  350.          
  351.       }
  352.       .submit input {
  353.          position:relative;
  354.          left:4.5em;         
  355.       }
  356.          
  357.       fieldset {
  358.          border: solid 1px black;
  359.          width:50em;
  360.          padding:0em;
  361.       }
  362.      
  363.       legend {
  364.          border:solid 1px black;
  365.          padding:2px 6px;
  366.       }
  367.       /*---------------------------*/
  368.       #statframe {
  369.          position:relative;
  370.          top:10px;
  371.          left:65%;
  372.          width:800px;
  373.          height:300px;
  374.          margin-top:20px;
  375.          margin-left:-400px;
  376.  
  377.       }
  378.      
  379.       #statframe iframe {
  380.          border:solid 1px black;
  381.          width:100%;
  382.          height:100%;
  383.          
  384.       }
  385.    </style>
  386.    
  387.    <script type="text/javascript" src="js/jquery-1.7.2.js"></script>
  388.    <script type="text/javascript">
  389.       var rr;
  390.       function go(el) {
  391.      
  392.       var inf=<?='"'.array_pop(explode("/",$_SERVER['PHP_SELF'])).'"'?>;
  393.  
  394.      
  395.       zWin=open(inf+'?regexp8='+encodeURIComponent(el.regexp8.value)+'&'+
  396.       'url8='+encodeURIComponent(el.url8.value)+'&'+
  397.       'formstr8='+encodeURIComponent(el.formstr8.value),'zgoinf');
  398.      
  399.       var x=frames['statifr'];
  400.       x.document.body.innerHTML='';
  401.      
  402.       rr=setInterval(function() {
  403.          x.location.replace(inf+'?step&status');
  404.          //alert(x.document.body.innerHTML);
  405.          if (x.document.body.innerHTML.indexOf('parse complete')!=-1) {
  406.             clearInterval(rr);
  407.          }
  408.       },1000)
  409.          
  410.       //x.location.replace(inf+'?regexp8='+encodeURIComponent(el.regexp8.value)+'&'+
  411.       //;
  412.         /* $.ajax ( {
  413.          type: "GET",
  414.          url: inf,
  415.          data: {regexp8: el.regexp8.value,
  416.                 url8: el.url8.value, formstr8: el.formstr8.value,
  417.                 submit: true}}).done(
  418.             function(html) {
  419.                $('#formatted').html(html);
  420.             });*/
  421.  
  422.       }
  423.       //------------------------------------------
  424.       function stop_refr() {
  425.          clearInterval(rr);
  426.       }
  427.    </script>
  428. </head>
  429. <body>
  430.    <div id="formatted"></div>
  431.    <div id="zform">
  432.    <form  onsubmit="go(this);return false;" name="parseform" method="post" enctype="application/x-www-form-urlencoded" >
  433.       <fieldset>
  434.       <legend>Parser go</legend>
  435.       <p><label for="regexp8">RegExp</label>
  436.          <textarea type="text" name="regexp8" cols="68"
  437.          autocomplete="off"
  438.          rows="8"></textarea></p>
  439.          
  440.       <p><label for="url8">Target url</label>
  441.          <input type="text" name="url8" size="70" autocomplete="off"  /></p>
  442.          
  443.       <p><label for="formstr8">format string</label>
  444.      
  445.          <textarea type="text" name="formstr8" cols="68" autocomplete="off" rows="4"></textarea></p>
  446.          
  447.       <p class="submit"><input type="submit" name="submit" value="parse" />
  448.       <input type="reset" name="reset" value="reset" />
  449.       <input type="button" name="zzu" value="stop" onclick="stop_refr();return false;" />
  450.       </p>
  451.       </fieldset>
  452.    </form>
  453.    </div>
  454.    <div id="statframe">
  455.       <iframe name="statifr"></iframe>  
  456.    </div>
  457. </body>
  458. </html>  
  459.  
  460.  
  461. <?PHP
  462. }
  463. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement