Advertisement
terorama

parser / grabpics.php

Aug 21st, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.01 KB | None | 0 0
  1. <?php
  2.  
  3. if ($_SERVER["SERVER_NAME"]=='localhost')
  4.    require 'alinc.php';
  5. else
  6.    require 'statusparse.inc.php';
  7.    
  8.    
  9. define('ALLPARSED','../zallinf.txt');
  10. define('TMDIR','../imgtmbs/');
  11.    
  12. //--------------------------------------------------
  13. function get_addr (&$item, $key) {
  14.    preg_match('/src="([^"]*?)("|$)/ims',$item, $out);
  15.    @$item=$out[1];
  16. }
  17. //-----------------------------------------------------------------
  18. //                  main entrace
  19. //-----------------------------------------------------------------
  20.  
  21. if (!isset($_GET["step"])) {
  22.    
  23.       session_start();
  24.       session_destroy();
  25.       session_commit();
  26.    }
  27.    //---------------------------------
  28. session_start();
  29.  
  30.    //---------------------------------
  31.    if (isset($_SESSION["statusparse"]))
  32.       $statusparse=$_SESSION["statusparse"];
  33.    //else
  34.    
  35.    //-----------------------start processing
  36.    if (isset($_GET['start'])) {
  37.    
  38.       $statusparse= new StatusParse(true);
  39.       //-----------------------
  40.       $allpgs = file(ALLPARSED);
  41.      
  42.       $statusparse->urls=$allpgs;
  43.       $statusparse->initquery='stage_1';
  44.      
  45.       $_SESSION["statusparse"]=$statusparse;
  46.       session_commit();
  47.       header('Location: '.$_SERVER["PHP_SELF"].'?step');
  48.      
  49.    }
  50.    //--------------------------------------------processing
  51.    else
  52.    if (isset($_GET['step'])) {
  53.    
  54.       $processing=false;
  55.      
  56.       //----------------------------status
  57.       if (isset($_GET["status"])) {
  58.      
  59.           if ($statusparse->initquery!='fin') {
  60.              echo 'current stage: '.$statusparse->initquery.'<br />';
  61.              echo 'current step: '.$statusparse->step.'<br />';
  62.              
  63.              if ($statusparse->initquery=='stage_1')
  64.                 echo 'current el: '.
  65.                 $statusparse->urls[$statusparse->step].'<br />';
  66.              else
  67.                 echo 'current el: '.
  68.                 $statusparse->elems[$statusparse->step].'<br />';
  69.                
  70.              
  71.              echo 'elems: '.count($statusparse->urls).'<br />';
  72.              
  73.              }
  74.            else
  75.               echo('parse complete');
  76.              
  77.            exit();  
  78.        }
  79.       //------------------------------------------stage 1
  80.       if ($statusparse->initquery=='stage_1') {
  81.          //--------------------------
  82.          if ($statusparse->step<count($statusparse->urls)) {
  83.          
  84.             $processing=true;
  85.             $statusparse->elems=array_merge($statusparse->elems,
  86.             file($statusparse->urls[$statusparse->step]));
  87.            
  88.             $statusparse->step++;
  89.          }
  90.          else {
  91.             //-------------prep to stage 5
  92.             $processing=true;
  93.            
  94.             array_walk($statusparse->elems,'get_addr');
  95.            
  96.             $statusparse->initquery='stage_5';
  97.             $statusparse->step=0;
  98.          }
  99.       }
  100.      
  101.       //------------------------------------------stage 5
  102.       if ($statusparse->initquery=='stage_5') {
  103.          if ($statusparse->step<count($statusparse->elems)) {
  104.          
  105.             $processing=true;
  106.            
  107.             $el=trim($statusparse->elems[$statusparse->step]);
  108.             $tname=array_pop(explode('/',$el));
  109.             //---------------------------------------
  110.  
  111.             /*if ($ch=curl_init()) {
  112.                $statusparse->log_res($el);
  113.  
  114.                curl_setopt($ch, CURLOPT_URL,$el);
  115.                curl_setopt($ch, CURLOPT_HEADER, true);
  116.                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  117.                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  118.                
  119.                curl_setopt($ch, CURLOPT_USERAGENT,
  120.                   'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko)'.
  121.                   ' Chrome/20.0.1132.57 Safari/536.11');
  122.                  
  123.                $data=curl_exec($ch);
  124.                $statusparse->log_res($data);
  125.                curl_close($ch);
  126.                
  127.             } else {
  128.                $statusparse->log_res('curl init error');
  129.             }*/
  130.             //---------------------------------------
  131.            
  132.             //$ss=file_get_contents($el);
  133.            
  134.             list($w,$h)=getimagesize($el);
  135.             $nh=200;
  136.             $nw=$w*($nh/$h);
  137.        
  138.             $thumb=imagecreatetruecolor($nw,$nh);
  139.             $source=imagecreatefromjpeg($el);
  140.             imagecopyresampled($thumb,$source,0,0,0,0,$nw,$nh,$w,$h);
  141.        
  142.             imagejpeg($thumb,TMDIR.$tname,100);
  143.        
  144.             imagedestroy($source);
  145.             imagedestroy($thumb);
  146.                        
  147.             $statusparse->log_res($el.'|ABC|'.TMDIR.$tname);           
  148.            
  149.             $statusparse->step++;
  150.          }
  151.          else {
  152.             $processing=true;
  153.             $statusparse->initquery='fin';         
  154.          }
  155.       }
  156.          
  157.       //--------------------------------------------continue;
  158.       if ($processing) {
  159.      
  160.          $statusparse->n_redirects++;
  161.          if ($statusparse->n_redirects>15)
  162.             $statusparse->n_redirects=0;
  163.          
  164.           //-------------------
  165.           $_SESSION["statusparse"]=$statusparse;
  166.           session_commit();
  167.        
  168.           if ($statusparse->n_redirects==0) {        
  169.              echo '<head><script type="text/javascript">'.
  170.              'setTimeout(function() {location.replace("'.$_SERVER["PHP_SELF"].'?step");},1000)'.
  171.              '</script></head><body>';       
  172.              
  173.              echo 'parsing suspended to prevent error 310 <br />';
  174.              echo '<a href="'.$_SERVER["PHP_SELF"].'?step">continue</a></body>';             
  175.              }           
  176.          else
  177.             header('Location: '.$_SERVER["PHP_SELF"].'?step');
  178.       }
  179.       else {
  180.             header('Content-type:text/plain; charset=windows-1251');
  181.             $logstr=file_get_contents($statusparse->resfile);
  182.             echo $logstr;
  183.             }
  184.       //------------------------------------------------         
  185.    }
  186.    else
  187.       show_form();
  188.  
  189. //--------------------------------------------------------
  190. function show_form() {   
  191. ?>
  192.  
  193. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  194. <html xmlns="http://www.w3.org/1999/xhtml">
  195. <head>
  196.    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  197.    <meta http-equiv="Cache-Control" content="no-cache" />
  198.    
  199.    <title>Grab thumbs</title>
  200.    
  201.    <style type="text/css">
  202.       #statframe {
  203.          position:relative;
  204.          top:10px;
  205.          left:65%;
  206.          width:800px;
  207.          height:300px;
  208.          margin-top:20px;
  209.          margin-left:-400px;
  210.  
  211.       }
  212.      
  213.       #statframe iframe {
  214.          border:solid 1px black;
  215.          width:100%;
  216.          height:100%;
  217.          
  218.       }
  219.    </style>
  220.    
  221.    <script type="text/javascript" src="js/jquery-1.7.2.js"></script>  
  222.    <script type="text/javascript">
  223.        var rr;
  224.       function go(el) {
  225.      
  226.       var inf=<?='"'.array_pop(explode("/",$_SERVER['PHP_SELF'])).'"'?>;
  227.      
  228.       zWin=open(inf+'?start','zgoinf');
  229.      
  230.       var x=frames['statifr'];
  231.       x.document.body.innerHTML='';
  232.      
  233.       rr=setInterval(function() {
  234.          x.location.replace(inf+'?step&status');
  235.        
  236.          if (x.document.body.innerHTML.indexOf('parse complete')!=-1) {
  237.             clearInterval(rr);
  238.          }
  239.       },1000)
  240.          
  241.    
  242.       }
  243.       //------------------------------------------
  244.       function stop_refr() {
  245.          clearInterval(rr);
  246.       }
  247.    </script>
  248. </head>
  249.  
  250. <body>
  251.    <form  onsubmit="go(this);return false;" name="parseform" method="post" enctype="application/x-www-form-urlencoded" >
  252.       <input type="submit" name="submit" value="grab" />
  253.       <input type="button" name="zzu" value="stop" onclick="stop_refr();return false;" />
  254.    </form>
  255.    
  256.    <div id="statframe">
  257.       <iframe name="statifr"></iframe>  
  258.    </div>
  259. </body>
  260.  
  261. <?PHP
  262. }
  263. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement