<?php
/**
 * 上传文件到cdn源服务器
 *
 * 用法示范:
 * $sourceFile = "/data/wwwroot/nineke/admin/json/msg.json"; //本地目录
 * $res = oo::cdn()->uploadFile($sourceFile, 'json');
 * var_dump($res);
 */

class cdnHelper
{
	protected $key = ''; //与cdn上的php通信的秘钥
	protected $host = 'd147wns3pm1voh.cloudfront.net'; //源地址
	protected $url = '173.192.176.202/recvfile.php';
	
	protected $baseDir = '';
	
	public function __construct()
	{
//		date_default_timezone_set('Asia/Bangkok');
        date_default_timezone_set('Asia/Shanghai');
		
		$this->key = 'G8tvxS60UTGAoMgFaYvax1jzDkPPuW'.md5(date('YmdH').'^&_NINEKE_&^');
		
		$this->baseDir = oo::$config['cdnBaseDir'];
	}
	
	/**
	 * 上传文件到cdn
	 *
	 * sourceFile: 本地文件全路径 /data/wwwroot/nineke/admin/json/A.json
	 * dstDir: cdn目标文件夹(相对路径) json/
	 * 		   泰语版本会上传到: /disk1/wwwroot/ipoker_cdnroot/static/nineke/nineke/json/A.json
	 *
	 * 返回值 ret=0:正常, 其余失败
	 */
	public function uploadFile($sourceFile, $dstDir)
	{
		if (!$this->baseDir)
		{
			return -100;
		}
		
		$filename = basename($sourceFile);
		
		$path = "{$this->baseDir}{$dstDir}";
		
		$cmd = "curl -H'Host: {$this->host}' -F'f=@{$sourceFile}' -F'path={$path}' -F'key={$this->key}' -F'file_name={$filename}' {$this->url}";
		return shell_exec($cmd);
	}
}


	    

	
