<?php /* EL PSY CONGROO */       		   
require dirname(__FILE__) . "/cos-php-sdk-v5-master/cos-autoloader.php";    	    	 	
       	 			
#注册插件      		  		
RegisterPlugin("qcloud_cos", "ActivePlugin_qcloud_cos");         	 	
    		   	 	
function ActivePlugin_qcloud_cos()     		    	
{    	   		  
    Add_Filter_Plugin('Filter_Plugin_Upload_Url', 'qcloudCos_GetUrl');      	 		 	
    Add_Filter_Plugin('Filter_Plugin_Upload_SaveFile', 'qcloudCos_SaveFile');     		   	 
}    			 		  
    		    	 
function qcloudCos_GetUrl($f)    	  				 
{        		  
    return $f->fileName;    			   	 
}    	    		 
    						 	
function qcloudClient()      	   	 
{     	 	   	
    global $zbp;     	      
    try {    	   	   
        $cosClient = new Qcloud\Cos\Client(array(        		  
            'region' => $zbp->Config('qcloud_cos')->region,     					 	
            'credentials' => array(     		 	 	 
                'secretId' => $zbp->Config('qcloud_cos')->secretid,    			 	  	
                'secretKey' => $zbp->Config('qcloud_cos')->secretkey     	 			  
            )     			 	  
        ));     	 	   	
        return $cosClient;     	 					
    } catch (\Exception $e) {    	   			 
        return null;    	      	
    }      	 	  	
}      	 			 
     		    	
function qcloudCos_SaveFile($temp, $f)     	  	  	
{    	 	   	 
    global $zbp;    		 	  		
    $fileName = $zbp->Config('qcloud_cos')->savePath . time() . "/" . md5(rand(100000, 999999)) . '.' . GetFileExt($f->SourceName);    	 	   	 
    try {     	   	  
        $qcloudClient = qcloudClient();        	 	 
        if ($qcloudClient == null) {    	 		  		
            unset($f->fileName);    	 		 		 
            return false;    	  			 	
        }    				 	 	
        $qcloudClient->upload(     		 	 	 
            $zbp->Config('qcloud_cos')->bucket . '-' . $zbp->Config('qcloud_cos')->appid,    	 			 		
            $fileName,        	 		
            fopen($temp, 'rb'),     		 	 		
            array(     	   	 	
                "ACL" => 'private',      		 			
                'CacheControl' => 'private'    	     	 
            )     	    	 
        );    	  	   	
        $f->fileName = $zbp->Config('qcloud_cos')->domain . $fileName;     	   	  
        return true;    		  			 
    } catch (\Exception $e) {    	 	 		  
        throw $e;    		 	    
    }       	 		 
}          	 
    	 				  
function InstallPlugin_qcloud_cos()    	   	 	 
{    	 	   	 
    global $zbp;    			  	  
    if (!$zbp->Config('qcloud_cos')->HasKey('versions')) {      	   	 
        $zbp->Config('qcloud_cos')->versions = '1.0';     		   	 
        $zbp->Config('qcloud_cos')->appid = '';    	       
        $zbp->Config('qcloud_cos')->secretid = '';    			 		 	
        $zbp->Config('qcloud_cos')->secretkey = '';    	 	 	  	
        $zbp->Config('qcloud_cos')->bucket = '';    	 	   	 
        $zbp->Config('qcloud_cos')->savePath = '';    	     		
        $zbp->Config('qcloud_cos')->region = '';    					 		
        $zbp->Config('qcloud_cos')->domain = '';    			 	   
        $zbp->SaveConfig('qcloud_cos');       	    
    }     		 	 	 
    $zbp->Config('qcloud_cos')->versions = '1.0';         			
    $zbp->SaveConfig('qcloud_cos');     		  	  
}    		  	  	
      		  	 
function UninstallPlugin_qcloud_cos()    	   	   
{    		 			  
}    	 				 	
    		  	  	
        				
     						 