Вход Регистрация
Файл: onlinepoisk.wm-scripts.ru/vendor/Art/SearchMusic.php
Строк: 231
<?php

namespace SearchMusic;
// file caching
class Cache {
  public static 
$cacheRoot = "/assets";

  public static function 
set($key, $value, $time = 60) {
    
$data = new ModelCache;
    
$data->key = $key;
    
$data->data = $value;
    
$data->expiredat = date("Y-m-d H:i:s", time() + $time);

    
$data->save();
  }

  public static function 
get($key) {
    
$data = ModelCache::find('one', array('conditions' => array(
      
'`expiredAt` > ? AND `key` = ?', date("Y-m-d H:i:s"), $key
    
)));

    return 
$data ? $data->data : null;
  }

  public static function 
clear($key) {
    
$cache = ModelCache::find('one', array('conditions' => array(
      
'`expiredAt` > ? AND `key` = ?', date("Y-m-d H:i:s"), $key
    
)));

    return 
$cache ? $cache->delete() : false;
  }

  public static function 
clearAll() {
    
$caches = ModelCache::all();
    foreach (
$caches as $cache) {
      
$cache->delete();
    }
  }
}

class 
Core {
  private 
$api_key;
  public function 
__construct($api_key) {
    
$this->api_key = $api_key;
  }


/*
$filename - file path to captcha
$apikey   - account's API key
$rtimeout - delay between captcha status checks
$mtimeout - captcha recognition timeout

$is_verbose - false(commenting OFF),  true(commenting ON)

additional custom parameters for each captcha:
$is_phrase - 0 OR 1 - captcha has 2 or more words
$is_regsense - 0 OR 1 - captcha is case sensetive
$is_numeric -  0 OR 1 - captcha has digits only
$min_len    -  0 is no limit, an integer sets minimum text length
$max_len    -  0 is no limit, an integer sets maximum text length
$is_russian -  0 OR 1 - with flag = 1 captcha will be given to a Russian-speaking worker

usage examples:
$text=recognize("/path/to/file/captcha.jpg","YOUR_KEY_HERE",true, "antigate.com");

$text=recognize("/path/to/file/captcha.jpg","YOUR_KEY_HERE",false, "antigate.com");  

$text=recognize("/path/to/file/captcha.jpg","YOUR_KEY_HERE",false, "antigate.com",1,0,0,5);  

*/



public function recognize(
            
$filename,
            
$apikey,
            
$is_verbose = true,
            
$domain="antigate.com",
            
$rtimeout = 5,
            
$mtimeout = 120,
            
$is_phrase = 0,
            
$is_regsense = 0,
            
$is_numeric = 0,
            
$min_len = 0,
            
$max_len = 0,
            
$is_russian = 0
            
)
{
  if (!
file_exists($filename))
  {
    if (
$is_verbose) echo "file $filename not foundn";
    return 
false;
  }
    
$postdata = array(
        
'method'    => 'post', 
        
'key'       => $apikey, 
        
'file'      => '@'.$filename, //������ ���� � �����
        
'phrase'  => $is_phrase,
        
'regsense'  => $is_regsense,
        
'numeric' => $is_numeric,
        
'min_len' => $min_len,
        
'max_len' => $max_len,
        
    );
    
$ch = curl_init();
    
curl_setopt($ch, CURLOPT_URL,             "http://$domain/in.php");
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER,     1);
    
curl_setopt($ch, CURLOPT_TIMEOUT,             60);
    
curl_setopt($ch, CURLOPT_POST,                 1);
    
curl_setopt($ch, CURLOPT_POSTFIELDS,         $postdata);
    
$result = curl_exec($ch);
    if (
curl_errno($ch)) 
    {
      if (
$is_verbose) echo "CURL returned error: ".curl_error($ch)."n";
        return 
false;
    }
    
curl_close($ch);
    if (
strpos($result, "ERROR")!==false)
    {
      if (
$is_verbose) echo "server returned error: $resultn";
        return 
false;
    }
    else
    {
        
$ex = explode("|", $result);
        
$captcha_id = $ex[1];
      if (
$is_verbose) echo "captcha sent, got captcha ID $captcha_idn";
        
$waittime = 0;
        if (
$is_verbose) echo "waiting for $rtimeout secondsn";
        
sleep($rtimeout);
        while(
true)
        {
            
$result = file_get_contents("http://$domain/res.php?key=".$apikey.'&action=get&id='.$captcha_id);
            if (
strpos($result, 'ERROR')!==false)
            {
              if (
$is_verbose) echo "server returned error: $resultn";
                return 
false;
            }
            if (
$result=="CAPCHA_NOT_READY")
            {
              if (
$is_verbose) echo "captcha is not ready yetn";
              
$waittime += $rtimeout;
              if (
$waittime>$mtimeout) 
              {
                if (
$is_verbose) echo "timelimit ($mtimeout) hitn";
                break;
              }
            if (
$is_verbose) echo "waiting for $rtimeout secondsn";
              
sleep($rtimeout);
            }
            else
            {
              
$ex = explode('|', $result);
              if (
trim($ex[0])=='OK') return trim($ex[1]);
            }
        }
        
        return 
false;
    }
}

  public function 
vk_api_wrapper($params){
        
$http_query = http_build_query($params);
    
$result = file_get_contents("http://api.wm-scripts.ru/api.php?" . $http_query . "&key=" . $this->api_key);
    return 
$result;
  }
    
  public function 
popSearch($page = 0, $count = 10, $cacheTime = 0) {

    if (!
$page || $page <= 0)
      
$page = 0;
    else
      
$page *= $count;
        
    
$params = array(
            
'method' => 'audio.getPopular',
            
'only_eng' => '0',
            
'offset' => $page,
            
'count' => $count,
            
'format' => 'json'
        
);

        
$result = $this->vk_api_wrapper($params);
 
        
    
$result = json_decode($result);
    if ( isset(
$result->response) ) {
      
$result = $result->response;
      
$count = $result[0];
      unset(
$result[0]);
      foreach (
$result as $key => $value) {
        
$result[$key] = (array) $value;
      }
    } else {
      
$result = array();
      
$count = 0;
    }
            
    return array(
      
'count' => $count,
      
'result' => $result
    
);
  }
  
  
  public function 
videoSearch($q, $page = 0, $count = 10, $cacheTime = 186400) {

    
$q = str_replace("&", " ", $q);
    if (!
$page || $page <= 0)
      
$page = 0;
    else
      
$page *= $count;
        
    
$params = array(
        
'method' => 'video.search',
        
'count' => $count,
        
'offset' => $page,
        
'q' => $q,
        
'format' => 'json');
     
        
$result = $this->vk_api_wrapper($params);
        
    
$result = json_decode($result);
    if ( isset(
$result->response->items) ) {
      
$result = $result->response->items;
      
$count = $result[0];
      unset(
$result[0]);
      foreach (
$result as $key => $value) {
        
$result[$key] = (array) $value;
      }
    } else {
      
$result = array();
      
$count = 0;
    }
            
    return array(
      
'count' => $count,
      
'result' => $result
    
);
  }
  
  public function 
videoget($vkId) {

    
$params = array(
      
'method' => 'video.get',
      
'format' => 'json',
      
'videos' => $vkId
    
);


    
$result = $this->vk_api_wrapper($params);


    
$result = json_decode($result);
    
$result = $result->response->items;
            
    return 
$result[0];
  }
  
  public function 
audioSearch($q, $page = 0, $count = 10, $cacheTime = 86400) {
   
$q = str_replace("&", " ", $q);
    if (!
$page || $page <= 0)
      
$page = 0;
    else
      
$page *= $count;
        
    
        
$params = array(
            
'method' => 'audio.search',
            
'count' => $count,
            
'offset' => $page,
            
'q' => $q,
            
'format' => 'json');
        
        
$result = $this->vk_api_wrapper($params);

             
    
$result = json_decode($result);
    if ( isset(
$result->response) ) {
      
$result = $result->response;
      
$count = $result[0];
      unset(
$result[0]);
      foreach (
$result as $key => $value) {
        
$result[$key] = (array) $value;
      }
    } else {
      
$result = array();
      
$count = 0;
    }
            
    return array(
      
'count' => $count,
      
'result' => $result
    
);
  }

  public function 
audioGetById($vkId) {
   
$params = array(
      
'method' => 'audio.getById',
      
'format' => 'json',
      
'audios' => $vkId
    
);


        
$result = $this->vk_api_wrapper($params);


    
$result = json_decode($result);
    
$result = $result->response;
            
    return 
$result[0];
  }

  public function 
getTrack($vkId) {
    
$song = $this->audioGetById($vkId);
    
$url = 'http://api.wm-scripts.ru/api.php?method=audio.download&key='.$this->api_key.'&data='.base64_encode(base64_encode($song->url));

    return array(
      
'url' => $url,
      
'fname' => str_replace(" ", "_", "{$song->artist} — {$song->title}.mp3"),
      
'size' => $this->remoteFilesize($url)
    );
  }

  public function 
audioGetLyrics($id) {
   
$params = array(
      
'method' => 'audio.getLyrics',
      
'format' => 'json',
      
'lyrics_id' => $id
    
);

        
$result = $this->vk_api_wrapper($params);

    
$result = json_decode($result);
    
$result = $result->response;

    return 
$result->text;
  }

  function 
remoteFilesize($url) {
    
$head = get_headers($url, 1);
    return isset(
$head['Content-Length']) ? $head['Content-Length'] : "unknown";
  }

  public function 
curl_redirect_exec($ch, &$redirects = 0, $curloptHeader = false) {
    
curl_setopt($ch, CURLOPT_HEADER, true);
    
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
$data = curl_exec($ch);
    
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if (
in_array($httpCode, array(301, 302))) {
      list(
$header) = explode("rnrn", $data, 2);
      
$matches = array();

      
preg_match("/(Location:|URI:)[^(n)]*/", $header, $matches);
      
$url = trim(str_replace($matches[1], "", $matches[0]));

      
$urlParsed = parse_url($url);
      if (isset(
$urlParsed)) {
        
curl_setopt($ch, CURLOPT_URL, $url);
        
$redirects++;
        return 
$this->curl_redirect_exec($ch, $redirects);
      }
    }

    if (
$curloptHeader) {
      return 
$data;
    } else {
      
$ttt = explode("rnrn", $data, 2);
      return isset(
$ttt[1]) ? $ttt[1] : null;
    }
  }

}

/*
Техническая поддержка и обновления
http://wm-scripts.ru

*/
Онлайн: 2
Реклама