ALIOSS::get_bucket_acl PHP Method

get_bucket_acl() public method

Get Bucket's ACL
Since: 2011-11-14
public get_bucket_acl ( string $bucket, array $options = null ) : ResponseCore
$bucket string (Required)
$options array (Optional)
return ResponseCore
    public function get_bucket_acl($bucket, $options = null)
    {
        //options
        $this->validate_options($options);
        if (!$options) {
            $options = array();
        }
        //bucket
        $this->is_empty($bucket, OSS_BUCKET_IS_NOT_ALLOWED_EMPTY);
        $options[self::OSS_BUCKET] = $bucket;
        $options[self::OSS_METHOD] = 'GET';
        $options[self::OSS_OBJECT] = '/';
        $options[self::OSS_SUB_RESOURCE] = 'acl';
        $response = $this->auth($options);
        return $response;
    }

Usage Example

Example #1
0
function oss_setting_page()
{
    $options = array();
    if ($_POST['bucket']) {
        $options['bucket'] = trim(stripslashes($_POST['bucket']));
    }
    if ($_POST['ak']) {
        $options['ak'] = trim(stripslashes($_POST['ak']));
    }
    if ($_POST['sk']) {
        $options['sk'] = trim(stripslashes($_POST['sk']));
    }
    if ($_POST['host']) {
        $options['host'] = trim(stripslashes($_POST['host']));
    }
    if ($_POST['nothumb']) {
        $options['nothumb'] = isset($_POST['nothumb']) ? 'true' : 'false';
    }
    if ($_POST['nolocalsaving']) {
        $options['nolocalsaving'] = isset($_POST['nolocalsaving']) ? 'true' : 'false';
    }
    if ($_POST['upload_url_path']) {
        //仅用于插件卸载时比较使用
        $options['upload_url_path'] = trim(stripslashes($_POST['upload_url_path']));
    }
    //检查提交的AK/SK是否有管理该bucket的权限
    $flag = 0;
    if ($_POST['bucket'] && $_POST['ak'] && $_POST['sk']) {
        try {
            if (!is_object($aliyun_oss)) {
                $aliyun_oss = new ALIOSS($options['ak'], $options['sk'], $options['host']);
            }
            $oss_option = array(ALIOSS::OSS_CONTENT_TYPE => 'text/xml');
            $response = $aliyun_oss->get_bucket_acl($options['bucket'], $oss_option);
            if ($response->status == 200) {
                $flag = 1;
                if (preg_match('/<Grant>public-read-write<\\/Grant>/i', $response->body) > 0) {
                    $flag = -11;
                } elseif (preg_match('/<Grant>private<\\/Grant>/i', $response->body) > 0) {
                    $flag = -12;
                }
            } elseif ($response->status == 403 && preg_match('/<Endpoint>/i', $response->body) > 0) {
                $flag = -2;
            }
        } catch (Exception $ex) {
            $flag = -1;
        }
    }
    if ($options !== array()) {
        //更新数据库
        update_option('oss_options', $options);
        $upload_path = trim(trim(stripslashes($_POST['upload_path'])), '/');
        $upload_path = $upload_path == '' ? 'wp-content/uploads' : $upload_path;
        update_option('upload_path', $upload_path);
        $upload_url_path = trim(trim(stripslashes($_POST['upload_url_path'])), '/');
        update_option('upload_url_path', $upload_url_path);
        ?>
<div class="updated"><p><strong>设置已保存!
<?php 
        if ($flag == 0) {
            echo '<span style="color:#F00">注意:您的AK/SK没有管理该Bucket的权限,因此不能正常使用!</span>';
        } elseif ($flag == -1) {
            echo '<span style="color:#F00">注意:网络通信错误,未能校验您的AK/SK是否对该bucket是否具有管理权限</span>';
        } elseif ($flag == -11) {
            echo '<span style="color:#F00">注意:该BUCKET现在处于“公开读写”状态,会有安全隐患哦!设置成“公开读”就足够了。</span>';
        } elseif ($flag == -12) {
            echo '<span style="color:#F00">注意:该BUCKET现在处于“私有”状态,不能被其他人访问哦!建议将BUKET权限设置成“公开读”。</span>';
        } elseif ($flag == -2) {
            echo '<span style="color:#F00">注意:该BUCKET的“存储地域”或“HOST主机”可能搞错了,请再次确认下。</span>';
        }
        ?>
</strong></p></div>
<?php 
    }
    $oss_options = get_option('oss_options', TRUE);
    $upload_path = get_option('upload_path');
    $upload_url_path = get_option('upload_url_path');
    $oss_bucket = attribute_escape($oss_options['bucket']);
    $oss_ak = attribute_escape($oss_options['ak']);
    $oss_sk = attribute_escape($oss_options['sk']);
    $oss_host = attribute_escape($oss_options['host']);
    if ($oss_host == null || $oss_host == '') {
        $oss_host = 'oss.aliyuncs.com';
    }
    $oss_nothumb = attribute_escape($oss_options['nothumb']);
    $oss_nothumb = $oss_nothumb == 'true';
    $oss_nolocalsaving = attribute_escape($oss_options['nolocalsaving']);
    $oss_nolocalsaving = $oss_nolocalsaving == 'true';
    ?>
<div class="wrap" style="margin: 10px;">
    <h2>阿里云附件 v2.0 设置</h2>
    <form name="form1" method="post" action="<?php 
    echo wp_nonce_url('./options-general.php?page=' . OSS_BASEFOLDER . '/oss-support.php');
    ?>
">
		<table class="form-table">
			<tr>
				<th><legend>Bucket 设置</legend></th>
				<td>
					<input type="text" name="bucket" value="<?php 
    echo $oss_bucket;
    ?>
" size="50" placeholder="BUCKET"/>
					<p>请先访问 <a href="http://i.aliyun.com/dashboard?type=oss" target="_blank">阿里云存储</a> 创建 <code>bucket</code> ,再填写以上内容。</p>
				</td>
			</tr>
			<tr>
				<th><legend>Access Key</legend></th>
				<td><input type="text" name="ak" value="<?php 
    echo $oss_ak;
    ?>
" size="50" placeholder="Access Key"/></td>
			</tr>
			<tr>
				<th><legend>Secret Key</legend></th>
				<td>
					<input type="text" name="sk" value="<?php 
    echo $oss_sk;
    ?>
" size="50" placeholder="Secret Key"/>
					<p>访问 <a href="https://ak-console.aliyun.com/#/accesskey" target="_blank">阿里云 密钥管理页面</a>,获取 <code>AK/SK</code> 。</p>
				</td>
			</tr>
			<tr>
				<th><legend>HOST主机</legend></th>
				<td>
					<select id="hosts_set" onchange="hostChange()">
						<option value="oss.aliyuncs.com">杭州节点(外网)</option>
						<option value="oss-internal.aliyuncs.com">杭州节点(内网)</option>
						<option value="oss-cn-beijing.aliyuncs.com">北京节点(外网)</option>
						<option value="oss-cn-beijing-internal.aliyuncs.com">北京节点(内网)</option>
						<option value="oss-cn-shenzhen.aliyuncs.com">深圳节点(外网)</option>
						<option value="oss-cn-shenzhen-internal.aliyuncs.com">深圳节点(内网)</option>
						<option value="oss-cn-qingdao.aliyuncs.com">青岛节点(外网)</option>
						<option value="oss-cn-qingdao-internal.aliyuncs.com">青岛节点(内网)</option>
						<option value="oss-cn-hongkong.aliyuncs.com">香港节点(外网)</option>
						<option value="oss-cn-hongkong-internal.aliyuncs.com">香港节点(内网)</option>
						<option value="">自定义设置</option>
					</select>
					<input type="text" id="oss_host" name="host" value="<?php 
    echo $oss_host;
    ?>
" size="29" placeholder="oss.aliyuncs.com" onchange="initSelectSet()"/>
					<p>默认 <code>杭州节点(外网)</code> ,其他节点需要更改。<a href="http://help.aliyun.com/view/11108271_13438690.html" target="_blank">【查看详情】</a></p>
					<script type="text/javascript">
						function initSelectSet() {
							var hostname = document.getElementById("oss_host").value;
							var hostSet = document.getElementById("hosts_set");
							var i = 0;
							for (i = 0; i < hostSet.options.length; i++) {
								if (hostSet.options[i].value == hostname) {
									hostSet.selectedIndex = i;
									break;
								}
							}
							if(i == hostSet.options.length) {
								hostSet.selectedIndex = i-1;
							}
						}
						function hostChange(){
							var host = document.getElementById("hosts_set").value;
							document.getElementById("oss_host").value = host;
						}
						initSelectSet();
					</script>
				</td>
			</tr><tr>
				<th><legend>不上传缩略图</legend></th>
				<td><input type="checkbox" name="nothumb" <?php 
    if ($oss_nothumb) {
        echo 'checked="TRUE"';
    }
    ?>
 /></td>
			</tr><tr>
				<th><legend>不在本地保留备份</legend></th>
				<td><input type="checkbox" name="nolocalsaving" <?php 
    if ($oss_nolocalsaving) {
        echo 'checked="TRUE"';
    }
    ?>
 /></td>
			</tr><tr>
				<th><legend>本地文件夹:</legend></th>
				<td>
					<input type="text" name="upload_path" value="<?php 
    echo $upload_path;
    ?>
" size="50" placeholder="请输入上传文件夹"/>
					<p>附件在服务器上的存储位置,例如: <code>wp-content/uploads</code> (注意不要以“/”开头和结尾),根目录请输入<code>.</code>。</p>
				</td>
			</tr>
			<tr>
				<th><legend>URL前缀:</legend></th>
				<td>
					<input type="text" name="upload_url_path" value="<?php 
    echo $upload_url_path;
    ?>
" size="50" placeholder="请输入URL前缀"/>
					<p><b>注意:</b></p>
					<p>1)URL前缀的格式为 <code>http://{OSS域名}</code> (“本地文件夹”为 <code>.</code> 时),或者 <code>http://{OSS域名}/{本地文件夹}</code> ,“本地文件夹”务必与上面保持一致(结尾无 <code>/</code> )。</p>
					<p>2)OSS中的存放路径(即“文件夹”)与上述 <code>本地文件夹</code> 中定义的路径是相同的(出于方便切换考虑)。</p>
					<p>3)如果需要使用 <code>独立域名</code> ,直接将 <code>{OSS域名}</code> 替换为 <code>独立域名</code> 即可。</p>
				</td>
			</tr>
			<tr>
				<th><legend>更新选项</legend></th>
				<td><input type="submit" name="submit" value="更新" /></td>
			</tr>
		</table>
    </form>
</div>
<?php 
}