proxy.php 1.73 KB
<?php

class proxy extends \Phalcon\Mvc\Model
{
    public $id;
    public $proxy;
    public $status;
    static $instance;
    private $next_proxy;
    private $proxy_active;

    public static function getInstance(){
        if(empty(self::$instance)){
            self::$instance = new proxy();
        }
        return self::$instance;
    }


    public function getProxy(){
        $this->proxy_active = self::find('status = 1')->toArray();
        if( $this->proxy_active ){
            $count = count($this->proxy_active);
            if(!$this->next_proxy){
                $this->next_proxy = 0;
                print('use proxy '.$this->proxy_active[$this->next_proxy++]['proxy']);
                return $this->proxy_active[$this->next_proxy++]['proxy'];
            }else {
                if($this->next_proxy >= $count-1){
                    $this->next_proxy = 0;
                    print('use proxy '.$this->proxy_active[$this->next_proxy++]['proxy']);
                    return $this->proxy_active[$this->next_proxy]['proxy'];
                } else {
                    print('use proxy '.$this->proxy_active[$this->next_proxy++]['proxy']);
                    return $this->proxy_active[$this->next_proxy++]['proxy'];
                }
            }
        } else {
            die('you don\'t have proxy');
        }


    }

    public function deleteProxy(){
        $activ_proxy = $this->next_proxy? $this->next_proxy - 1: $this->next_proxy;
        print('delete proxy '.$this->proxy_active[$activ_proxy]['proxy']);
        $non_active_proxy = self::findFirst('id = '.$this->proxy_active[$activ_proxy]['id']);
        $non_active_proxy->status = 0;
        $non_active_proxy->save();
    }

    public function initialize()
    {

    }


}