Exemple #1
0
 public function show()
 {
     $upyun = $this->upyun->get_upyun_access_token();
     $data = $dat = array();
     $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
     $oauth->setAccessToken($upyun['access_token']);
     $info = $oauth->request('/accounts/profile/', 'GET', $data);
     if ($info['error']) {
         $info = array();
     } else {
         $sql = 'SELECT * from ' . DB_PREFIX . 'cdn_account WHERE 1 ';
         $account = $this->db->query_first($sql);
         if (defined('UpYun_Username') && UpYun_Username && defined('UpYun_Password') && UpYun_Password) {
             $info['username'] = UpYun_Username;
             $info['password'] = UpYun_Password;
         } else {
             $info['username'] = $account['username'];
             $info['password'] = $account['password'];
         }
         if (!$account) {
             $dat = array('username' => $info['username'], 'real_name' => $info['realname'], 'password' => $info['password'], 'account_type' => $info['account_type'], 'company_name' => $info['company_name'], 'email' => $info['email'], 'mobile' => $info['mobile'], 'im' => $info['im'], 'website' => $info['website']);
             $account_id = $this->obj->create('cdn_account', $dat);
         }
     }
     $this->addItem($info);
     $this->output();
 }
Exemple #2
0
 function delete()
 {
     $upyun = $this->upyun->get_upyun_access_token();
     $data = array('bucket_name' => $this->input['bucket_name'], 'domain' => $this->input['id']);
     $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
     $oauth->setAccessToken($upyun['access_token']);
     $info = $oauth->request('/buckets/domains/', 'DELETE', $data);
     if ($info['error']) {
         $this->errorOutput($info['error']);
     }
     $this->addItem('ture');
     $this->output();
 }
 /**
  * {@inheritdoc}
  */
 function refreshToken()
 {
     // have an access token?
     if ($this->api->access_token) {
         // have to refresh?
         if ($this->api->refresh_token && $this->api->access_token_expires_at) {
             // expired?
             if ($this->api->access_token_expires_at <= time()) {
                 $response = $this->api->refreshToken(array("refresh_token" => $this->api->refresh_token));
                 if (!isset($response->access_token) || !$response->access_token) {
                     // set the user as disconnected at this point and throw an exception
                     $this->setUserUnconnected();
                     throw new Exception("The Authorization Service has return an invalid response while requesting a new access token. " . (string) $response->error);
                 }
                 // set new access_token
                 $this->api->access_token = $response->access_token;
                 if (isset($response->refresh_token)) {
                     $this->api->refresh_token = $response->refresh_token;
                 }
                 if (isset($response->expires_in)) {
                     $this->api->access_token_expires_in = $response->expires_in;
                     // even given by some idp, we should calculate this
                     $this->api->access_token_expires_at = time() + $response->expires_in;
                 }
             }
         }
         // re store tokens
         $this->token("access_token", $this->api->access_token);
         $this->token("refresh_token", $this->api->refresh_token);
         $this->token("expires_in", $this->api->access_token_expires_in);
         $this->token("expires_at", $this->api->access_token_expires_at);
     }
 }
Exemple #4
0
 public function show()
 {
     $data = array();
     if ($this->input['type'] && $this->input['type'] != '-1') {
         $data['type'] = $this->input['type'];
     }
     $upyun = $this->upyun->get_upyun_access_token();
     $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
     $oauth->setAccessToken($upyun['access_token']);
     $info = $oauth->request('/logs/', 'GET', $data);
     $return = $info['result']['data'];
     if ($return && is_array($return)) {
         foreach ($return as $k => $v) {
             if ($v['type']) {
                 switch ($v['type']) {
                     case 'auth':
                         $type = '登陆';
                         break;
                     case 'account':
                         $type = '帐号';
                         break;
                     case 'bucket':
                         $type = '空间';
                         break;
                     case 'operator':
                         $type = '操作员';
                         break;
                     case 'file':
                         $type = '文件';
                         break;
                     default:
                         break;
                 }
             }
             $row['name'] = $v['log'];
             $row['type'] = $type;
             $row['ip'] = $v['ip'];
             $row['create_time'] = date("Y-m-d H:i:s", $v['created_at']);
             $re[] = $row;
         }
     }
     $this->addItem($re);
     $this->output();
 }
Exemple #5
0
 public function detail()
 {
     $data = array('bucket_name' => $this->input['id']);
     $upyun = $this->upyun->get_upyun_access_token();
     $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
     $oauth->setAccessToken($upyun['access_token']);
     $info = $oauth->request('/buckets/info/', 'GET', $data);
     $re = array();
     $re = array('bucket_name' => $info['bucket_name'], 'type' => $info['type']);
     $typeinfo = $info[$info['type']];
     $re['domain'] = $typeinfo['domain'];
     $re['ip_tel'] = $typeinfo['ip_tel'];
     $re['ip_cnc'] = $typeinfo['ip_cnc'];
     if ($info['error']) {
         $this->errorOutput($info['message']);
     }
     $this->addItem($re);
     $this->output();
 }
 public function api($path, $method = 'GET', $params = array())
 {
     try {
         return parent::api($path, $method, $params);
     } catch (OAuth2Exception $e) {
         //once and only once, try to get use the refresh token to get a fresh token
         if ($e->getMessage() == 'INVALID_SESSION_ID') {
             $this->refreshToken();
             return parent::api($path, $method, $params);
         } else {
             throw $e;
         }
     }
 }
Exemple #7
0
 public function __construct($config = array())
 {
     $configured_scopes = array('offline');
     if (isset($config['scope'])) {
         $config_scopes = array_map('trim', explode(',', $config['scope']));
         foreach ($config_scopes as $scope) {
             if (in_array($scope, $this->_allowed_scopes)) {
                 $configured_scopes[] = $scope;
             }
         }
         unset($config['scope']);
     }
     $this->setVariable('scope', implode(',', $configured_scopes));
     parent::__construct($config);
 }
Exemple #8
0
 public function update_upyun_email($data)
 {
     $upyun = $this->upyun->get_upyun_access_token();
     $data_ = array('password' => $data['password'], 'email' => $data['email']);
     $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
     $oauth->setAccessToken($data['access_token']);
     $info = $oauth->request('/accounts/email/', 'POST', $data_);
     return $info;
 }
Exemple #9
0
 public function get_domains($domain)
 {
     $domains = array();
     $upyun = $this->get_upyun_access_token();
     $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
     $oauth->setAccessToken($upyun['access_token']);
     $info = $oauth->request('/buckets/', 'GET');
     if ($info['buckets'] && is_array($info['buckets'])) {
         foreach ($info['buckets'] as $k => $v) {
             $data = array('bucket_name' => $v['bucket_name']);
             $info2 = $oauth->request('/buckets/info/', 'GET', $data);
             if ($info2['approval_domains'] && is_array($info2['approval_domains'])) {
                 foreach ($info2['approval_domains'] as $ke => $va) {
                     $domains[] = $va;
                 }
             }
         }
     }
     preg_match('@^(?:http://)?([^/]+)@i', $domain, $matches);
     $host = $matches[1];
     if (in_array($host, $domains)) {
         return 1;
     } else {
         return 0;
     }
 }
Exemple #10
0
 public function show()
 {
     $data = $return = $buckets_name = $domain = $name = array();
     $upyun = $this->upyun->get_upyun_access_token();
     $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
     $oauth->setAccessToken($upyun['access_token']);
     $buckets = $oauth->request('/buckets/', 'GET');
     if ($buckets && is_array($buckets)) {
         foreach ($buckets['buckets'] as $k => $v) {
             $buckets_name[$v['bucket_name']] = $v['bucket_name'];
         }
     }
     if ($this->input['bucket'] && $this->input['bucket'] != '-1') {
         $data_['bucket_name'] = $data['bucket_name'] = $this->input['bucket'];
     } else {
         $bkeys = array_keys($buckets_name);
         $data_['bucket_name'] = $data['bucket_name'] = $bkeys[0];
     }
     if ($data_['bucket_name']) {
         $doms = $oauth->request('/buckets/info/', 'GET', $data_);
     }
     if ($doms['approval_domains'] && is_array($doms['approval_domains'])) {
         foreach ($doms['approval_domains'] as $k => $v) {
             $domain[$v] = $v;
         }
     }
     if ($doms['approvaling_domains'] && is_array($doms['approvaling_domains'])) {
         foreach ($doms['approvaling_domains'] as $k => $v) {
             $domain[$v] = $v;
         }
     }
     if ($this->input['domain'] && $domain[$this->input['domain']]) {
         $data['domain'] = $this->input['domain'];
     } else {
         $dokeys = array_keys($domain);
         $data['domain'] = $dokeys[0];
     }
     if ($this->input['start_time']) {
         $data['date'] = $this->input['start_time'];
     } else {
         $data['date'] = date('Y-m-d', TIMENOW - 24 * 3600);
     }
     $data['type'] = $this->input['type'] ? $this->input['type'] : 'url';
     $info = $oauth->request('/analysis/', 'GET', $data);
     if ($info['data'] && is_array($info['data'])) {
         foreach ($info['data'] as $k => $v) {
             $re = array('content' => $v['content']);
             $flow = $this->get_bytes($v['flow']);
             if ($this->input['type'] == 'size') {
                 $re['reqs'] = $this->get_bytes($v['reqs']);
             } else {
                 $re['reqs'] = $v['reqs'];
             }
             $re['flow'] = $flow;
             $return[] = $re;
         }
     }
     if ($this->input['type']) {
         switch ($this->input['type']) {
             case 'url':
                 $name = 'URL';
                 break;
             case 'ip':
                 $name = 'IP';
                 break;
             case 'referer':
                 $name = '引用页面';
                 break;
             case 'user_agent':
                 $name = '客户端';
                 break;
             case 'http_status':
                 $name = 'HTTP 状态';
                 break;
             case 'size':
                 $name = 'URL';
                 break;
             default:
                 break;
         }
     }
     $cdn_flow[] = $return;
     $cdn_flow['buckets'] = $buckets_name;
     $cdn_flow['domain'] = $domain;
     $cdn_flow['name'] = $name;
     $cdn_flow['type'] = $this->input['type'];
     $this->addItem($cdn_flow);
     $this->output();
 }
Exemple #11
0
 public function delete()
 {
     $sql = 'SELECT * from ' . DB_PREFIX . 'cdn_account WHERE 1 ';
     $account = $this->db->query_first($sql);
     $data = array('bucket_name' => $this->input['id'], 'password' => $account['password']);
     $upyun = $this->upyun->get_upyun_access_token();
     $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
     $oauth->setAccessToken($upyun['access_token']);
     $info = $oauth->request('/buckets/', 'DELETE', $data);
     if ($info['error_code']) {
         $this->errorOutput($info['message']);
     }
     $this->addItem('ture');
     $this->output();
 }
Exemple #12
0
 public function show()
 {
     //file_put_contents('0',var_export($this->input,1));
     $data = array();
     $upyun = $this->upyun->get_upyun_access_token();
     $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
     $oauth->setAccessToken($upyun['access_token']);
     $buckets = $oauth->request('/buckets/', 'GET');
     if ($buckets && is_array($buckets)) {
         foreach ($buckets['buckets'] as $k => $v) {
             $buckets_name[$v['bucket_name']] = $v['bucket_name'];
         }
     }
     if ($this->input['bucket'] && $this->input['bucket'] != '-1') {
         $data_['bucket_name'] = $data['bucket_name'] = $this->input['bucket'];
     }
     $doms = $oauth->request('/buckets/info/', 'GET', $data_);
     if ($doms['approval_domains'] && is_array($doms['approval_domains'])) {
         foreach ($doms['approval_domains'] as $k => $v) {
             $domain[$v] = $v;
         }
     }
     if ($doms['approvaling_domains'] && is_array($doms['approvaling_domains'])) {
         foreach ($doms['approvaling_domains'] as $k => $v) {
             $domain[$v] = $v;
         }
     }
     if ($this->input['domain'] && $domain[$this->input['domain']]) {
         $data['domain'] = $this->input['domain'];
     }
     if ($this->input['start_time'] && !$this->input['end_time']) {
         $start_date = date("Y-m-d");
         $end_date = $this->input['start_time'];
         $ds = strtotime($start_date);
         $de = strtotime($end_date);
         $data['period'] = round(($ds - $de) / 3600 / 24) + 1;
     }
     if ($this->input['start_time'] && $this->input['end_time']) {
         $start_date = $this->input['start_time'];
         $end_date = $this->input['end_time'];
         $ds = strtotime($start_date);
         $de = strtotime($end_date);
         $data['period'] = round(($de - $ds) / 3600 / 24) + 1;
         $data['start_day'] = $this->input['end_time'];
     }
     if ($this->input['date_search']) {
         switch (intval($this->input['date_search'])) {
             case 1:
                 //今天
                 break;
             case 2:
                 //最近3天
                 $data['period'] = 3;
                 break;
             case 3:
                 //最近7天
                 $data['period'] = 7;
                 break;
             case 4:
                 //最近15天
                 $data['period'] = 15;
                 break;
             case 5:
                 //最近30天
                 $data['period'] = 30;
                 break;
             default:
                 //所有时间段
                 break;
         }
     }
     $info = $oauth->request('/stats/', 'GET', $data);
     if ($info['maxs'] && is_array($info['maxs'])) {
         if ($data['period']) {
             //$this->count($data['period']);
         }
         foreach ($info['maxs'] as $k => $v) {
             $re = array('data' => $k, 'pubtime' => $v['pubtime'], 'reqs' => $v['reqs']);
             $bytes = $this->get_bytes($v['bytes']);
             $sbytes = $this->get_bytes($v['sbytes']);
             $re['bytes'] = $bytes;
             $re['sbytes'] = $sbytes;
             $return[] = $re;
         }
     }
     $cdn_flow[] = $return;
     $cdn_flow['buckets'] = $buckets_name;
     $cdn_flow['domain'] = $domain;
     $cdn_flow['bandwidth'] = $info['bandwidth'];
     $cdn_flow['reqs'] = $info['reqs'];
     $cdn_flow['discharge'] = $info['discharge'];
     $cdn_flow['day_count'] = $info['day_count'];
     if ($info['bandwidth'] && is_array($info['bandwidth'])) {
         foreach ($info['bandwidth'] as $k => $v) {
             $cdn_flow['start_time'] = strtotime($k);
             if (!$cdn_flow['start_time']) {
                 $time = explode(' ', $k);
                 $cdn_flow['start_time'] = strtotime($time[0]);
             }
             break;
         }
     }
     $this->addItem($cdn_flow);
     $this->output();
 }
Exemple #13
0
 public function get_upyun_access_token()
 {
     include CUR_CONF_PATH . 'lib/OAuth2Client.class.php';
     $data = array('username' => UpYun_Username, 'password' => UpYun_Password, 'grant_type' => 'password', 'client_id' => OAUTH_CLIENT_ID, 'client_secret' => OAUTH_CLIENT_SECRET);
     $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
     $info = $oauth->request('/oauth/access_token/', 'POST', $data);
     if ($info['access_token']) {
         $upyun_str = serialize($info);
         $returnstr = "<?php\r\n";
         $returnstr .= "\$upyun_info = array(";
         $returnstr .= "'upyun'  => " . "'" . $upyun_str . "',";
         $returnstr .= ");\r\n?>";
         $filename = 'upyun.php';
         $name = '../cache/' . $filename;
         file_put_contents($name, $returnstr);
     } else {
         $this->errorOutput($info['message']);
     }
 }
Exemple #14
0
 public function get_domains($domain)
 {
     $cache_file = CACHE_DIR . 'buckets.info';
     $domains = array();
     if (!is_file($cache_file) || filemtime($cache_file) >= time() - 300) {
         $upyun = $this->get_upyun_access_token();
         $oauth = new OAuth2Client(OAUTH_CLIENT_ID, OAUTH_CLIENT_SECRET, OAUTH_BASE_URI, OAUTH_AUTHORIZE_URI, OAUTH_ACCESS_TOKEN_URI);
         $oauth->setAccessToken($upyun['access_token']);
         $info = $oauth->request('/buckets/?limit=100', 'GET');
         if ($info['buckets'] && is_array($info['buckets'])) {
             foreach ($info['buckets'] as $k => $v) {
                 $data = array('bucket_name' => $v['bucket_name']);
                 $info2 = $oauth->request('/buckets/info/', 'GET', $data);
                 if ($info2['approval_domains'] && is_array($info2['approval_domains'])) {
                     $tmp = array();
                     foreach ($info2['approval_domains'] as $ke => $va) {
                         $domains[] = $va;
                         $tmp[] = $va;
                     }
                     if ($domains) {
                         $info['buckets'][$k]['domain'] = implode(',', $tmp);
                     }
                     $info['buckets'][$k]['cname'] = $val['bucket_name'] . $this->btype[$val['type']]['domain'];
                     $info['buckets'][$k]['type'] = $this->btype[$val['type']]['name'];
                 }
             }
             file_put_contents($cache_file, json_encode($info['buckets']));
         }
     } else {
         $info['buckets'] = json_decode(file_get_contents($cache_file), 1);
         foreach ($info['buckets'] as $k => $v) {
             $d = explode(',', $v['domain']);
             $domains = array_merge($domains, $d);
         }
     }
     preg_match('@^(?:http://)?([^/]+)@i', $domain, $matches);
     $host = $matches[1];
     if (in_array($host, $domains)) {
         return 1;
     } else {
         return 0;
     }
 }