Автор Тема: Помогите пожайлуста ламеру!  (Прочитано 3373 раз)

0 Пользователей и 1 Гость просматривают эту тему.

Оффлайн Vit10

  • Заглянувший
  • Новичок
  • *
  • Сообщений: 4
  • +0/-0
  • 0
    • Просмотр профиля
    • http://
Помогите пожайлуста ламеру!
« : 31 Августа 2003, 13:01:13 »
Доброе время суток!
Есть скрипт индексатора  FTP сайтов , но не могу справиться с лагом "Русские имена файлов на  FTP  серверах".
При индексации, в MySQL заносятся иероглифы в место русских букв.
Помогите пожайлуста ламеру!!!

С уважением,
                                        Виталий.

#!/usr/bin/php -f

 $conf_path  = "config/";
 $funct_path = "functions/";
 $class_path = "classes/";
 $templ_path = "templates/";
 include($conf_path."templates.php");
 include($conf_path."display.php");
 include($conf_path.\'mysql.php\');
 include($conf_path.\'indexer.php\');
 include($funct_path."encode.php");
 include($class_path."class.FastTemplate.php3");
   class ftp {
   var $dirqueue;
   var $u;
   var $p;
   var $h;
   var $d;
   var $s_per;
   var $c;
   var $ftpidx;
   var $host;
   var $user;
   var $pass;
   var $port;
   var $sock;
   var $pasv;
   var $sql;
   var $syst;
   var $numfiles;
   var $numdirs;
   var $id;
   var $rgood;
   var $rbad;
   var $ttime;
   var $funct_path;
   var $first_timer;
   var $email;
   var $notify;
   var $tpl;
   var $disco;
   var $sleepretry;
   var $retry;
   var $mfils;
   var $mdirs;
   var $mwild;
   var $match;
   var $debug;

   function init($id) {
      $this->id=$id;
      $fid=\'init\';
      $this->debug($fid, \'initializing...\');
      if ( $this->sqlconnect() == 1 ) {
         $this->debug($fid, \'checking queue\');
         $query=\'SELECT COUNT(idx) FROM ftps WHERE upd+sch < \'.time();
         $r=mysql_query($query, $this->c);
         $r=mysql_fetch_row($r);
         $r=$r[0];
         if ( $r > 0 ) {
            $this->debug($fid, \'updating pending ftp...\');
            $query=$query=\'SELECT * FROM ftps WHERE upd+sch < \'.time().\' LIMIT 1\';
            $r=mysql_query($query, $this->c);
            $r=mysql_fetch_assoc($r);
            if ( $r[\'upd\'] < 1 ) {
               $this->first_timer=1;
            } else {
               $this->first_timer=0;
            }
            $this->ftpidx=$r[\'idx\'];
            $this->host=decode($r[\'hos\']);
            $this->user=decode($r[\'uss\']);
            $this->pass=decode($r[\'pas\']);
            $this->port=decode($r[\'pos\']);
            $this->email=decode($r[\'email\']);
            $this->debug($fid, \'Found http://ftp://\'.$this->user.\':\'.$this->pass.\'@\'.$this->host.\':\'.$this->port);
            $this->connect();
            $query=\'UPDATE ftps SET upd=\'.time().\' WHERE idx=\'.$this->ftpidx;
            mysql_query($query, $this->c);
            $query=\'UPDATE idx SET delid=\'.$this->ftpidx.\' WHERE IDXftp=\'.$this->ftpidx;
            mysql_query($query, $this->c);
            $this->dirqueue[]=\'/\';
         } else {
            $this->debug($fid, \'nothing to do...\');
         }
      }
   }

   function finish() {
      if ( $this->numdirs > 0 || $this->numfiles > 0 ) {
         $fid=\'finish\';
         $this->debug($fid, \'Begin Decomposing\');
         $this->debug($fid, \'Updating Time Entries\');
         $query=\'UPDATE ftps SET time=\'.$this->ttime.\' WHERE idx=\'.$this->ftpidx;
         $null=mysql_query($query, $this->c);
         $query=\'UPDATE ftps SET upd=\'.time().\' WHERE idx=\'.$this->ftpidx;
         $null=mysql_query($query, $this->c);
         $this->debug($fid, \'Updating Accounting Information\');
         $this->debug($fid, $this->numfiles.\' files...\');
         $query=\'UPDATE ftps SET files=\'.$this->numfiles.\' WHERE idx=\'.$this->ftpidx;
         $null=mysql_query($query, $this->c);
         $this->debug($fid, $this->numdirs.\' dirs...\');
         $query=\'UPDATE ftps SET dirs=\'.$this->numdirs.\' WHERE idx=\'.$this->ftpidx;
         $null=mysql_query($query, $this->c);
         $query=\'DELETE FROM idx WHERE delid=\'.$this->ftpidx;
         $null=mysql_query($query, $this->c);
         if ( $this->first_timer == 1 ) {
            if ( !empty($this->email) ) {
               $this->notify[]=$this->email;
            }
            if ( is_array($this->notify) ) {
               $this->tpl->assign(NumFils, $this->numfiles);
               $this->tpl->assign(NumDirs, $this->numdirs);
               $this->tpl->assign(FtpUser, $this->user);
               $this->tpl->assign(FtpPass, $this->pass);
               $this->tpl->assign(FtpPort, $this->port);
               $this->tpl->assign(FtpHost, $this->host);
               $this->tpl->parse(PageB, "notification");
               foreach ( $this->notify as $idx => $addr ) {
                  if ( !empty($addr) ) {
                     $this->debug($fid, \'Emailing notification to: \'.$addr);
                     mail($addr, "FTP Indexer E-Mail Notofication", $this->tpl->fetch(PageB));
                  }
               }
            } else {
               $this->debug($fid, \'No one to notify...\');
            }
         }
      } else {
         $this->debug($fid, \'no finishing nessecary...\');
         $query=\'UPDATE idx SET delid=NULL WHERE IDXftp=\'.$this->ftpidx;
         $null=mysql_query($query, $this->c);
      }
   }

   function add_file($dn, $fn, $size) {
      $fid=\'add_file\';
      if ( isset($fn) && $fn != \'\' ) {
         if ( $this->mfils == 1 || $this->mdirs == 1 ) {
            $this->debug($fid, \'Checking VS [\'.(($this->mfils + $this->mdirs) * count($this->match)).\'] Deny Rules [wild=\'.$this->mwild.\']...\');
            foreach ( $this->match as $match ) {
               if ( $this->mfils == 1 ) {
                  if ( $this->mwildf == 1 ) {
                     if ( eregi($match, $fn) ) {
                        $this->debug($fid, \'File Cited By Deny Rule\');
                        return(1);
                     }
                  } else {
                     if ( strtolower($match) == strtolower($fn) ) {
                        $this->debug($fid, \'File Cited By Deny Rule\');
                        return(1);
                     }
                  }
               }
               if ( $this->mdirs == 1 ) {
                  if ( $this->mwildd == 1 ) {
                     if ( eregi($match, $dn) ) {
                        $this->debug($fid, \'Directory Cited By Deny Rule\');
                        return(1);
                     }
                  } else {
                     if ( strtolower($match) == strtolower($dn) ) {
                        $this->debug($fid, \'Directory Cited By Deny Rule\');
                        return(1);
                     }
                  }
               }
            }
         }
         $this->debug($fid, \'checking for file entry in files table\');
         $query=\'SELECT COUNT(idx) FROM files WHERE dstring="\'.$fn.\'"\';
         $r=mysql_query($query, $this->c);
         $r=mysql_fetch_row($r);
         $r=$r[0];
         if ( $r == 0 ) {
            $this->debug($fid, \'adding entry to files table\');
            $query=\'INSERT INTO files (dstring) VALUES ("\'.$fn.\'")\';
            mysql_query($query, $this->c);
         }
         $this->debug($fid, \'aquiring IDXfil\');
         $query=\'SELECT idx FROM files WHERE dstring="\'.$fn.\'"\';
         $r=mysql_query($query, $this->c);
         $r=mysql_fetch_row($r);
         $f=$r[0];      

         $this->debug($fid, \'checing for directory entry in dirs table\');
         $query=\'SELECT COUNT(idx) FROM dirs WHERE dstring="\'.$dn.\'"\';
         $r=mysql_query($query, $this->c);
         $r=mysql_fetch_row($r);
         $r=$r[0];
         if ( $r == 0 ) {
            $this->debug($fid, \'adding entry to dirs table\');
            $query=\'INSERT INTO dirs (dstring) VALUES ("\'.$dn.\'")\';
            mysql_query($query, $this->c);
         }
         $this->debug($fid, \'aquiring IDXdir\');
         $query=\'SELECT idx FROM dirs WHERE dstring="\'.$dn.\'"\';
         $r=mysql_query($query, $this->c);
         $r=mysql_fetch_row($r);
         $d=$r[0];

         $this->debug($fid, \'adding file entry into idx table\');
         $query=\'INSERT INTO idx (IDXftp,IDXdir,IDXfil,IDXsiz,IDXtim) VALUES ("\'.$this->ftpidx.\'","\'.$d.\'","\'.$f.\'","\'.$size.\'","\'.time().\'")\';
         mysql_query($query, $this->c);

         $this->debug($fid, \'updating ftps table entry for this ftpid\');
         $query=\'UPDATE ftps SET upd="\'.time().\'" WHERE idx=\'.$this->ftpidx;
         mysql_query($query, $this->c);
      }
   }

   function sqlconnect() {
      $conf_path  = "config/";
      include($conf_path.\'mysql.php\');
      $fid=\'sqlconnect\';
      $this->u=$mysql[\'user\'];
      $this->p=$mysql[\'pass\'];
      $this->h=$mysql[\'host\'];
      $this->d=$mysql[\'dbas\'];
      $this->debug($fid, \'connecting...\');
      $this->debug($fid, \'using persistant connection...\');
      $this->c=@mysql_pconnect($this->h, $this->u, $this->p);
      @mysql_select_db($this->d);
      if ( $this->c && $this->d ) {
         $this->debug($fid, \'connected!\');
         return(1);
      } else {
         $this->debug($fid, \'connection failed!\');
         return(0);
      }
   }

   function ftp($notify, $tpl, $sleepretry, $retry, $array) {
      $this->mfils=$array[\'matchfiles\'];
      $this->mdirs=$array[\'matchdirs\'];
      $this->mwildd=$array[\'matchwildd\'];
      $this->mwildf=$array[\'matchwildf\'];
      $this->match=$array[\'match\'];
      $this->debug=$array[\'debug\'];
      $this->notify=$notify;
      $this->retry=$retry;
      $this->sleepretry=$sleepretry;
      $this->tpl=$tpl;
      if (!extension_loaded(\'ftp\')) {
          if (!dl(\'ftp.so\') && !dl(\'ftp.dll\')) {
              die(\'PHP FTP Extension needs to be enabled!\');
          }
      }
   }
   
   function set($host, $port=21, $user=\'anonymous\', $pass=\'anonymous@e-mail.com\') {
      $this->host=$host;
      $this->port=$port;
      $this->user=$user;
      $this->pass=$pass;
   }
   
   function connect($test=0) {
      $fid=\'connect\';
      $this->debug($fid, \'connecting...\');
      $test=@fsockopen($this->host, $this->port);
      if ( $test ) {
         $this->debug($fid, \'port open on remote server...\');
         fclose($test);
      } else {
         $this->debug($fid, \'port NOT open on remote server...\');
         return(-1);
      }
      $this->sock=@ftp_connect($this->host, $this->port);
      if ( $this->sock ) {
         $this->debug($fid, \'connected...\');
         $login=@ftp_login($this->sock, $this->user, $this->pass);
         if ( $login ) {
            $this->debug($fid, \'logged in...\');
            ftp_pasv($this->sock, $this->pasv);
            $this->debug($fid, \'using passive mode...\');
            $this->syst=ftp_systype($this->sock);
            $this->disco=0;
            return(1);
         } else {
            $this->debug($fid, \'login failed!\');
            return(0);
         }
      } else {
         $this->debug($fid, \'connection failed!\');
         return(-1);
      }
   }
   
   function disconnect() {
      $fid=\'disconnect\';
      $this->debug($fid, \'dosconnecting...\');
      ftp_close($this->sock);
      return(1);
   }
   
   function analysedir($dirline) {
      $fid=\'analysedir\';
      $this->debug($fid, \'parsing line...\');
      if ( strtolower(substr($dirline,0,5)) == "total" ) {
         $dirinfo[0] = -1;
      } else {
         if ( $this->syst == "Windows_NT" ) {
            if(ereg("[-0-9]+ *[0-9:]+[PA]?M? + {10}(.*)",$dirline,$regs)) {
               $dirinfo[0] = 1;
               $dirinfo[1] = 0;
               $dirinfo[2] = $regs[1];
            } elseif(ereg("[-0-9]+ *[0-9:]+[PA]?M? +([0-9]+) (.*)",$dirline,$regs)) {
               $dirinfo[0] = 0;
               $dirinfo[1] = $regs[1];
               $dirinfo[2] = $regs[2];
            }
         } elseif ($this->syst=="UNIX") {
            if(ereg("([-ld])[rwxst-]{9}.* ([0-9]*) [a-zA-Z]+ [0-9: ]*[0-9] (.+)",$dirline,$regs)) {
               if($regs[1]=="d") {
                  $dirinfo[0] = 1;
               } else if ( $regs[1]=="l" ) {
                  $dirinfo[0] = 2;
               } else {
                  $dirinfo[0] = 0;
               }
               $dirinfo[1] = $regs[2];
               $dirinfo[2] = $regs[3];
            }
         }
         if ( ($dirinfo[2]==".") || ($dirinfo[2]=="..") ) {
            $dirinfo[0]=\'-1\';
         }
      }
      return $dirinfo;
   }

   function checkdir ($dir, $attempt=1) {
      $fid=\'checkdir\';
      while ( !@ftp_pwd($this->sock) ) {
         $this->disco++;
         if ( $this->disco <= $this->retry ) {
            $this->debug($fid, \'Communications lost, retrying [\'.$this->disco.\'/\'.$this->retry.\'] in \'.$this->sleepretry.\' seconds\');
            set_time_limit($this->sleepretry);
            sleep($this->sleepretry);
            $this->connect();
         } else {
            unset($this->dirqueue);
            $this->debug($fid, \'Comms lost, more than \'.$this->retry.\' times, removing this ftp sites remaining queues\');
            if ( !empty($this->email) ) {
               $this->notify[]=$this->email;
            }
            if ( is_array($this->notify) ) {
               $this->tpl->assign(NumFils, $this->numfiles);
               $this->tpl->assign(NumDirs, $this->numdirs);
               $this->tpl->assign(FtpUser, $this->user);
               $this->tpl->assign(FtpPass, $this->pass);
               $this->tpl->assign(FtpPort, $this->port);
               $this->tpl->assign(FtpHost, $this->host);
               $this->tpl->parse(PageB, "notification-fail");
               foreach ( $this->notify as $nidx => $addr ) {
                  if ( !empty($addr) ) {
                     $this->debug($fid, \'Emailing FAILURE notification to: \'.$addr.\' [\'.($nidx + 1).\'/\'.count($this->notify).\']\');
                     mail($addr, "FTP Indexer E-Mail Failure Notofication", $this->tpl->fetch(PageB));
                  }
               }
            } else {
               $this->debug($fid, \'FTP Failed, but no notification sent due to lack of e-mail addresses\');
            }
            return(0);
         }
      }
      $this->debug($fid, \'indexing \'.$dir);
      if ( $attempt == 2 ) {
         $odir=$dir;
         $dir=\'"\'.$dir.\'"\';
      }
      if ( @ftp_chdir($this->sock, $dir) ) {
         if ( $attempt == 2 ) {
            $dir=$odir;
            unset($odir);
         }
         $this->debug($fid, \'getting RAWLIST from host\');
         $dirlist = ftp_rawlist($this->sock,"-al");
         if ( ! is_array($dirlist) ) {
            $this->debug($fid, \'Ftp Disconnected, Reconnecting\');
            $this->connect();
            $dirlist = ftp_rawlist($this->sock,"-al");
         }
         if ( is_array($dirlist) ) {
            $this->debug($fid, \'analyzing \'.count($dirlist).\' lines\');
         }
         for( $i=0; $i < count($dirlist); $i++ ) {
            $dirinfo = $this->analysedir($dirlist[$i]);
            flush();
            if ( $dirinfo[0] == 1 ) {
               $this->debug($fid, \'found directory, added to dirqueue: "\'.$dirinfo[2].\'"\');
               $this->numdirs++;
               if ( $dir != \'/\' ) {
                  $this->dirqueue[]=$dir.\'/\'.$dirinfo[2];
               } else {
                  $this->dirqueue[]=$dir.$dirinfo[2];
               }
            } else if ( $dirinfo[0] == 2 ) {
               $this->debug($fid, \'found symbolic link, ignoring: "\'.$dirinfo[2].\'"\');
            } else if ( $dirinfo[0] == 0 ) {
               $pass=0;
               if ( is_array($this->rgood) ) {
                  foreach ( $this->rgood as $idx => $val ) {
                     if ( $val != \'\' ) {
                        if ( eregi($val, $dirinfo[2]) || eregi($val, $dir) ) {
                           $this->debug($fid, \'file passed on passrule: \'.$val);
                           $pass=1;
                           break;
                        }
                     } else if ( $idx == 0 ) {
                        $pass=1;
                     }
                  }
               } else {
                  $this->debug($fid, \'file passed by default on passrules\');
                  $pass=1;
               }
               if ( is_array($this->rbad) ) {
                  foreach ( $this->rbad as $idx => $val ) {
                     if ( $val != \'\' ) {
                        if ( eregi($val, $dirinfo[2]) || eregi($val, $dir ) ) {
                           $this->debug($fid, \'file failed on denyrule: \'.$val);
                           $pass=0;
                           break;
                        }
                     }
                  }
               }
               if ( $pass == 1 ) {
                  $this->debug($fid, \'found file, adding to index: [\'.$dir.\']"\'.$dirinfo[2].\'" [\'.$dirinfo[1].\']\');
                  if ( $dir != \'/\' ) {
                     $d=encode($dir.\'/\');
                  } else {
                     $d=encode($dir);
                  }
                  $f=encode($dirinfo[2]);
                  $this->add_file($d, $f, $dirinfo[1]);
                  $this->numfiles++;
               } else {
                  $this->debug($fid, \'found file, ignoring based on rules!\');
               }
            }
         }
      } else {
         if ( $attempt == 1 ) {
            $this->checkdir($dir, 2);
         } else if ( $attempt == 2 ) {
            $this->checkdir($odir, 3);
         } else if ( $attempt == 3 ) {
            return(0);
         }
      }
   }

   function execute() {
      $fid=\'execute\';
      if ( count($this->dirqueue) > 0 ) {
         $this->debug($fid, \'indexing next directory in queueu\');
         $this->checkdir(array_pop($this->dirqueue));
         $this->debug($fid, \'finished index\');
      } else {
         $this->debug($fid, \'no more directories in queue\');
      }
   }

   function debug($f, $m) {
      if ( $this->debug == 1 ) {
         echo \'(\'.time().\')[\'.$this->id.\']@{\'.$f.\'()} : \'.$m.chr(10);
      }
   }
}


   $pswaux_loop=1;
   $pswaux_set=0;
   set_time_limit(0);

Оффлайн Vit10

  • Заглянувший
  • Новичок
  • *
  • Сообщений: 4
  • +0/-0
  • 0
    • Просмотр профиля
    • http://
Помогите пожайлуста ламеру!
« Ответ #1 : 31 Августа 2003, 13:01:55 »
продолжение скрипта
   function getmicrotime(){
      list($usec, $sec) = explode(" ",microtime());
      return ((float)$usec + (float)$sec);
   }

   set_time_limit($indexer[timeout]);

   $tpl=new FastTemplate($templ_path);
   $tpl->define($Templates);
   $tpl->assign($Display);

        while ( $pswaux_loop == 1 ) {
      set_time_limit($indexer[timeoutinc]);
                if ( $pswaux_set == 0 ) {
                        while ( $pswaux_loop == 1 ) {
                                $pswaux_obj[$pswaux_set]=new ftp($indexer[\'notify\'], $tpl, $indexer[\'sleepretry\'], $indexer[\'retry\'], $indexer);
                                $pswaux_set++;
                                if ( $pswaux_set >= $indexer[threads] ) {
                                        $pswaux_set=1;
                                        break;
                                }
                        }
                   foreach ( $pswaux_obj as $idx => $null ) {
            $pswaux_obj[$idx]->init($idx);
         }
                }

                foreach ( $pswaux_obj as $idx => $null ) {
         set_time_limit($indexer[timeoutinc]);
         $pswaux_obj[$idx]->debug(\'daemon\', \'PROCESS NEXT OBJECT\');
         if ( count($pswaux_obj[$idx]->dirqueue) > 0 ) {
            $pswaux_obj[$idx]->debug(\'daemon\', \'EXECUTING LOOP\');
            $t=getmicrotime();
            $pswaux_obj[$idx]->execute();
            $t=(getmicrotime() - $t);
            $pswaux_obj[$idx]->ttime=$pswaux_obj[$idx]->ttime+$t;
            $pswaux_obj[$idx]->debug(\'daemon\', \'ADDED \'.$t.\' TO TOTAL TIME SPENT\');
            usleep($indexer[lag]);
         } else {
            $pswaux_obj[$idx]->finish();
            $pswaux_obj[$idx]->debug(\'daemon\', \'DECOMPOSING OBJECT (NO QUEUES)\');
            unset($pswaux_obj[$idx]);
            usleep($indexer[lag]);
         }
                }

      if ( count($pswaux_obj) == 0 ) {
         if ( $indexer[daemon] == 1 ) {
            set_time_limit($indexer[timeoutinc]);
            sleep($indexer[sleep]);
            set_time_limit($indexer[timeoutinc]);
            $pswaux_set=0;
         } else {
            break;
         }
      }         
        }

?>

Оффлайн Макс

  • vir magni ingenii
  • Глобальный модератор
  • Ветеран
  • *****
  • Сообщений: 3534
  • +0/-0
  • 2
    • Просмотр профиля
Помогите пожайлуста ламеру!
« Ответ #2 : 31 Августа 2003, 19:00:43 »
Vit10
ты же не думаешь, что кто-то будет весь этот код изучать.
Тестируй, ищи на каком этапе происходит сбой.
First learn computer science and all the theory. Next develop a programming style. Then forget all that and just hack. ( George Carrette )

Оффлайн Меняздесьдавнонет

  • новичЕк
  • Глобальный модератор
  • Ветеран
  • *****
  • Сообщений: 5698
  • +0/-0
  • 2
    • Просмотр профиля
    • http://
Помогите пожайлуста ламеру!
« Ответ #3 : 31 Августа 2003, 21:17:48 »
ГЕНИАЛЬНО!!!
Оно еще и с продолжением!

Оффлайн DiJay

  • Завсегдатай
  • Пользователь
  • **
  • Сообщений: 65
  • +0/-0
  • 0
    • Просмотр профиля
    • http://
Помогите пожайлуста ламеру!
« Ответ #4 : 01 Сентября 2003, 09:17:45 »
А ты смотрел какая кодировка в MySQL по умолчанию стоит? Может в этом проблема?

Оффлайн Metaller

  • Мелкое злобное существо
  • Ветеран
  • *****
  • Сообщений: 538
  • +0/-0
  • 2
    • Просмотр профиля
    • http://michael.pp.ru
Помогите пожайлуста ламеру!
« Ответ #5 : 01 Сентября 2003, 10:08:02 »
очень хорошо...
Everybody\'s Got A Ticket To Ride Except For Me And My Lightning...

Оффлайн Vit10

  • Заглянувший
  • Новичок
  • *
  • Сообщений: 4
  • +0/-0
  • 0
    • Просмотр профиля
    • http://
Помогите пожайлуста ламеру!
« Ответ #6 : 05 Сентября 2003, 20:22:52 »
спасибо сам нашел

 

Sitemap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28