<?php

    include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
    include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
    
    /**
     * Plugin that offers features to 
     */
    class PluginRightsChooser extends PluginBase
    {
        var $prefix;
        var $pluginEnabled;       
    
        function PluginRightsChooser()
        {
            $this->PluginBase();
  
            $this->id = "rightschooser";
            $this->author = "Bill Yuan-yao Shih";
            $this->desc = "This plugin allows you to select and apply a license to your individual blogs. Displays the button and metadata automatically.";
  
            $this->prefix = Db::getPrefix();
            
            $this->locales = Array( "en_UK" , "zh_TW" , "zh_CN" );
            
            $this->init();            
        }

		function init()
		{
            $this->registerAdminAction( "rightschooser", "PluginRightsChooserConfigAction" );
			$this->registerAdminAction( "updateRightsChooserConfig", "PluginRightsChooserUpdateConfigAction" );
			
			$menu =& Menu::getMenu();
						
            $this->addMenuEntry( "/menu/controlCenter/manageSettings", "RightsChooser", "?op=rightschooser", "" );            
		}

		function register()
		{
		    $blogSettings = $this->blogInfo->getSettings();
		    $this->pluginEnabled = $blogSettings->getValue( "plugin_rightschooser_enabled" );
	        $this->RightsIcon = $blogSettings->getValue( "plugin_rightschooser_rightsicon" );
	        $this->RightsText = $blogSettings->getValue( "plugin_rightschooser_rightstext" );
	        $this->RightsCode = $blogSettings->getValue( "plugin_rightschooser_rightscode" );
	        $this->RightsUrl = $blogSettings->getValue( "plugin_rightschooser_rightsurl" );
	    }
	    
	    function isEnabled()
	    {
	        return $this->pluginEnabled;
	    }
	    
	    /**
         * Show the data of the rights
         */
        function showCustomHtml() {
        	$rightsHtmlTag="<!-- Creative Commons License -->
						<a href=\"". $this->RightsUrl ."\" title=\"Creative Commons License : ".$this->RightsText."\">
						<img alt=\"Creative Commons License\" border=\"0\" src=\"". $this->RightsIcon ."\" /></a>
						<br />
						This work is licensed under a <a href=\"". $this->RightsUrl ."\">Creative Commons License</a>.
						<!-- /Creative Commons License -->";
			
			return $rightsHtmlTag;
		}
		
		function showCustomRdf(){
			$rightsRdfTag="<!--
						<rdf:RDF xmlns=\"http://web.resource.org/cc/\"
    					xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
    					xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">
						<Work rdf:about=\"\">
						<license rdf:resource=\"". $this->RightsUrl ."\" />
						</Work>
						</rdf:RDF>
						-->";
			return $rightsRdfTag;
		}
		
		function getHtml(){
			
			$url = "http://creativecommons.org/license/get-html?license_url=".$this->RightsUrl."";
   			$url_parsed = parse_url($url);
   			$host = $url_parsed["host"];
   			$port = $url_parsed["port"];
   			if ($port==0)
   			$port = 80;
   			$path = $url_parsed["path"];
   			if ($url_parsed["query"] != "")
   			$path .= "?".$url_parsed["query"];

   			$out = "GET $path HTTP/1.0\r\nHost: $host\r\nAccept-Language: ".$_SERVER['HTTP_ACCEPT_LANGUAGE']."\r\n\r\n";

   			$fp = fsockopen($host, $port, $errno, $errstr, 30);
   			if (!$fp)
   				return "$errstr ($errno)<br />\n";

   			fwrite($fp, $out);
   			$body = false;
   			while (!feof($fp)) {
       			$s = fgets($fp, 1024);
       			if ( $body )
           			$in .= $s;
       			if ( $s == "\r\n" )
           			$body = true;
   			}
  
   			fclose($fp);
  
   			return $in;
   		}
    }

?>