<?php
require_once 'Zend/Session/Namespace.php';
class MessageController extends Zend_Controller_Action
{	
   
	public function init()
	{	Zend_Loader::loadClass('Message');
		$this->smarty=Zend_Registry::get('smarty');
		$this->config=Zend_Registry::get('config');
		$this->smarty->assign('baseUrl',$this->_request->getBaseUrl());
		
	}
	
	public function indexAction()
	{	$message=new Message();
		
		$db=$message->getAdapter();
		$page=$this->_request->getParam('page');
		$condition="".TABLE_PRE."message.pid=0";
		$sortby="".TABLE_PRE."message.id DESC";
		Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
		$basePageIndex=0;
		$Pagers = new Custom_Controller_Plugin_Pager($message,$page,'8',$condition,$sortby,$basePageIndex) ;
		
		$pageInfo = $Pagers->getNavBar(WEB_ROOT.'message/index/');
		$arrMessages = $Pagers->findAll()->toArray();
		$arrReview=$message->fetchAll(TABLE_PRE."message.pid!=0",TABLE_PRE."message.id DESC")->toArray();
		//var_dump($review);
		$this->smarty->assign("offsetnums",$Pagers->offsetnums);
		$this->smarty->assign('arrMessages',$arrMessages);
		$this->smarty->assign('arrReview',$arrReview);
    	$this->smarty->assign("pageInfo",$pageInfo);
		$this->smarty->display('header.phtml');  
		$this->smarty->display('message/index.phtml');
		$this->smarty->display('footer.phtml');
	}
	public function feedAction()
	{
		

	require_once 'Zend/Feed.php';

	
    $baseUrl = 'http://books.clubreading.com';

   

    $feedArray = array(
                'title' => 'ClubReading Books',
                'link' => 'http://books.clubreading.com/index/rss',
                'description' => 'An online community for the avid book lover.',
                'language' => 'zh-cn',
                'charset' => 'UTF-8',
                'pubDate' => '1218721648',
                'generator' => 'Zend Framework Zend_Feed',
                'entries' => array()
            );

	 for($i=1;$i<4;$i++){
	    $feedArray['entries'][] = array(
	                'title' => 'aaaaaaaaa',
	                'link' => $baseUrl . '/book/bookdetail/book_id/1',
	                'guid' => $baseUrl . '/book/bookdetail/book_id/1' ,
	                'description' => 'by: asdf, entered by:',
	                'pubDate' => '1218721648'
	                );
	            
	 }
	//$aaaa=new Zend_Feed_Builder($array);
	//$atomFeedFromArray = Zend_Feed::importArray($array, 'rss');
	$feed = Zend_Feed::importArray($feedArray, 'rss');
	
	//$atomFeedFromArray = Zend_Feed::importBuilder($array);
       
	//$feed->saveXML();
	$feed->send();

	
	}

	public function pdfAction()
	{
		require_once 'Zend/Pdf.php';
        // Create new PDF if file doesn't exist
   
		// Create new PDF 

		
		// Draw something on a page 

		// Create new PDF 
		$pdf = new Zend_Pdf(); 
		
		// Add new page to the document 
		$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); 
		$pdf->pages[] = $page; 
		//Zend_Pdf_Font::fontWithPath("D:/www/blog/library/Custom/Controller/Plugin/fpnf.ttf") ;
		// Set font 
		$page->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 20); 
		$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
		$font=Zend_Pdf_Font::fontWithPath("D:/www/blog/library/Custom/Controller/Plugin/simkai.ttf") ;
		// Apply font and draw text
		$page->setFont($font,12);
		$page->setFillColor(Zend_Pdf_Color_Html::color('#9999cc'));
		//$text = utf8_encode("");
		$text='哈哈...我的PDF文档';
		$text1='用PHP生成PDF文件with FPDF - PHP编程- 中国站长站
 - 繁 - [ 转为简体网页 2008年5月4日 - Translate this page ]
2008年5月4日 ... 您的位置：主页>网络编程>PHP编程>阅读资讯：用PHP生成PDF文件with FPDF .... Zend Framework 入门——页面布局; · Zend Framework 入门——错误处理 ...
big5.chinaz.com:88/chinaz.com/Program/PHP/05042QL2008.html - 25k - Cached - Similar pages - Note this';
		//$page->drawText('aasdfsfsd', '100', '200');
		
		$page->drawText($text,'0', '800',"UTF-8");
		$page->drawText($text1,'0', '600',"UTF-8");
		
		
		
				
		// Get PDF document as a string 
		$pdfData = $pdf->render(); 
		
		header("Content-Disposition: inline; filename=result.pdf"); 
		header("Content-type: application/x-pdf"); 
		echo $pdfData; 
		
		
			
		/*取到txt文件.并生成PDF文档
		$handle = fopen("D:/www/b2b/text.txt", "rb");
		$i=0;
		while (!feof($handle)) {
		    $buffer = fgets($handle, 4096);
		    $buffer = iconv("gb2312", "UTF-8", $buffer);
		    
		    $page->drawText($buffer,10,820-$i, 'UTF-8');
		   
		    $i=$i+10;
		
		 
		    echo $buffer.'<br />';
		}
		fclose($handle);
	 	
		$pdfData = $pdf->render(); 
		$pdf->save("test.pdf");
	  */
		        
				
	}
	
	public function mailAction()
	{	

       	Zend_Loader::loadClass('Zend_Mail');
		Zend_Loader::loadClass('Zend_Mail_Transport_Smtp');
 
  
		$mail = new Zend_Mail("UTF-8");
		$config = array('auth'=>'login',
        'username'=>"kylingood",
        'password'=>"goodbye77181681",
		'ssl'=>"ssl"
        );   
		$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$config);
		$mail->setDefaultTransport($transport); 
		
		$mail->setBodyHtml("我邮件.哈哈..来自google的.");
		$mail->setFrom('kylingood@gmail.com', 'goodbye77181681');
		$mail->addTo('kylingood@gmail.com', '37067087');
		$title="将utf-8编码的我邮件.哈哈我邮件将utf-8编码的我我邮件.哈哈我邮件哈哈邮件标题和内容转换为gb23将utf-8编码的我邮件.哈哈我邮件.哈哈邮件标题和内容转换为gb23将utf-8编码的我邮件.哈哈我邮件.哈哈邮件标题和内容转换为gb23.哈哈邮件标题和内容转换为gb2312";
		//$title=iconv("gb2312","",$title);
		$mail->setSubject($title);
		$mail->send();
	}
	
	public function messageAction()
	{
	    
    	require_once 'Zend/Session/Namespace.php';
        if($this->_request->isPost()){
    		Zend_Loader::loadClass('Zend_Filter_StripTags');
    		$filter=new Zend_Filter_StripTags();
    		$username=$filter->filter($this->_request->getPost('username'));
    		$email=$filter->filter($this->_request->getPost('email'));
    		$content=$filter->filter($this->_request->getPost('content'));
    		$title=$filter->filter($this->_request->getPost('title'));
    		$messageid=$filter->filter($this->_request->getPost('messageid'));
    		$imgcode=$filter->filter($this->_request->getPost('imgcode'));
    		$authCode = new Zend_Session_Namespace('Auth_Code');
    		$headimg=$filter->filter($this->_request->getPost('headimg'));
    		$dateFormat =$this->config->Format->dateFormat;
        	$message=new Message();
        	$db=$message->getAdapter();
        	
    		if($authCode->imagecode==$imgcode){
	    		if($username!=''&&$email!=''&&$messageid!=''&&$content!=''){
					require_once 'Zend/Validate/EmailAddress.php';
					$validator = new Zend_Validate_EmailAddress();
					if ($validator->isValid($email)) {
						Zend_Loader::loadClass('Custom_Controller_Plugin_IpLocation');
				    	Zend_Loader::loadClass('Custom_Controller_Plugin_Ipaddress');
				    	$Ipaddress=new Custom_Controller_Plugin_Ipaddress();
				    	$IP=$Ipaddress->getIP();
				    	$dir= ROOT_DIR."/blog/public/QQWry2.Dat";
				    	$iplocation=new Custom_Controller_Plugin_IpLocation($dir);
						$location=$iplocation->getlocation($IP);
						$address=iconv("gb2312","utf-8",$location['country']);
						$data=array(
						'title'=>$title,
						'author'=>$username,
		    			'pid'=>$messageid,
						'headimg'=>$headimg,
						'email'=>$email,
						'show'=>'1',
						'content'=>$content,
		    			'ip'=>$IP,
		    			'address'=>$address,
						'addtime'=>date($dateFormat),
						'updatetime'=>date($dateFormat)
						);
						$message->insert($data);
						$db->lastInsertId();
						unset($data);
						//echo "您的留言发表成功!";
						
						/*取到留言*/
					
						$page=$this->_request->getParam('page');
						$condition="".TABLE_PRE."message.pid=0";
						$sortby="".TABLE_PRE."message.id DESC";
						Zend_Loader::loadClass('Custom_Controller_Plugin_Pager');
						$basePageIndex=0;
						$Pagers = new Custom_Controller_Plugin_Pager($message,$page,'8',$condition,$sortby,$basePageIndex) ;
						
						$pageInfo = $Pagers->getNavBar(WEB_ROOT.'message/index/');
						$arrMessages = $Pagers->findAll()->toArray();
						$arrReview=$message->fetchAll(TABLE_PRE."message.pid!=0",TABLE_PRE."message.id DESC")->toArray();
						//var_dump($review);
						$this->smarty->assign('arrMessages',$arrMessages);
						$this->smarty->assign('arrReview',$arrReview);
				    	$this->smarty->assign("pageInfo",$pageInfo);
				    	$this->smarty->assign("offsetnums",$Pagers->offsetnums);
				    	$this->smarty->assign('flag','0');
				    	$this->smarty->assign('message','您的留言发表成功!');
				    	$this->smarty->display('message/message.phtml');
		
					} else {
					    //echo "对不起!您填写有电子邮箱地址有误!";
					    $this->smarty->assign('flag','5');
				    	$this->smarty->assign('message','对不起!您填写有电子邮箱地址有误!');
					    $this->smarty->display('message/message.phtml');
					}
						
	    		}elseif($username==''){
	    			
	    			$this->smarty->assign('flag','1');
				    $this->smarty->assign('message','对不起!您的大名不能为空!');
	    			$this->smarty->display('message/message.phtml');
	    		}elseif($messageid==''){
	    			
	    			$this->smarty->assign('flag','2');
				    $this->smarty->assign('message','对不起!回复留言编号不能为空!');
	    			$this->smarty->display('message/message.phtml');
	    		}elseif($content==''){
	    			
	    			$this->smarty->assign('flag','3');
				    $this->smarty->assign('message','对不起!您填写的留言内容不能为空!');
	    			$this->smarty->display('message/message.phtml');
	    		}
    		}else{
    			$this->smarty->assign('flag','4');
				$this->smarty->assign('message','对不起!您填写的验证码错误或超时!');
    			
    			$this->smarty->display('message/message.phtml');
    		}
        }else{
        	
        	$this->smarty->display('login.phtml');
        }
       
    
	}
}