Location: PHPKode > projects > ATUIN > atuin/app/controllers/IndexController.php
<?php
   /**
    * class IndexController
    */
   class IndexController extends Zend_Controller_Action implements Zend_Acl_Resource_Interface {

      function init() {
         $this->initView();
         $this->view->setEncoding('UTF-8');
         $this->view->baseUrl = $this->_request->getBaseUrl();
         $this->defaultNamespace = new Zend_Session_Namespace('Default');
         
         /* load "general" configuration from settings.ini */
         $this->cfg = new Zend_Config_Ini('../app/config/settings.ini', 'general');
      }
      
      public function getResourceId() {
   		 return 'index';
	  }
	  
      /**
       * function indexAction
       */
      public function indexAction() {
      
         $this->defaultNamespace->filename = 'index.rdf';
         $this->defaultNamespace->title = '';

         $rdfString = file_get_contents( 'files/feeds/index.rdf');
         $rdfParser = new RDFParser( );
         $rdfModel = $rdfParser->parse( $rdfString);
         
         $view = new Zend_View();
         $view->setScriptPath('../app/views/scripts');
         $view->setEncoding('UTF-8');
         // the controller script assign necessary variables to the view
         // before it hands over control to the view script
         $acl = new AclPlugin();
         $view->assign('acl', $acl);
         $view->assign('channels', (array) $rdfModel->channels);
         $view->assign('baseUrl', (string) $this->cfg->baseUrl);
         $view->assign('domain', (string) $this->cfg->domain);
         $view->assign('identity', (array) $this->defaultNamespace->identity);
         $view->assign('pagetitle', (string) $this->cfg->pagetitle);
         $view->assign('pagesubtitle', (string) $this->cfg->pagesubtitle);
         $view->assign('layout', (string) $this->cfg->layout);
         // render a script
         echo $view->render('index/index.php');
      }
      
     /**
	  * parses the title of a RSS-Feed
	  * @param $title string   feedtitle to be parsed
	  * @return string   parsed title
	  */ 
      private function titleParser($title) {
         $title = strtolower(utf8_decode( $title));
	      $charArr = preg_split('//', $title);	
	      $title = '';
	      foreach($charArr as $ch) {
/*
// debug
echo '"' . $ch . '" - ' . (int)$ch . '<br />';
exit( 0); 
*/
		      $isAlpha = $ch>='a' && $ch<='z';	
		  	   $isNum = $ch>='0' && $ch<='9';
		  	   if($isAlpha || $isNum) $title .= $ch;
		  	   if($ch == ' ' || $ch == '-' || $ch == '_')
               $title .= '-';
		  	   if($ch == 'ä')
               $title .= 'ae';
		  	   if($ch == 'ö')
               $title .= 'oe';
		  	   if($ch == 'ü')
               $title .= 'ue';
		  	   if($ch == 'ß')
               $title .= 'ss';
		  }
		  if( file_exists( 'files/feeds/' . $title . '.rdf')) {
           $i = 1;
		     while( file_exists( 'files/feeds/' . $title . '-' . $i . '.rdf')) {
		       $i++;
		     }
		     return $title . '-' . $i;
		  }
		  return $title;
	  }
      
      /**
       * function createAction
       */
      public function createAction() {
         if( $this->_request->getParam('feed')!='') {
            $this->defaultNamespace->parentfeed = $this->_request->getParam('feed');
         }

         if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {

            $parentfeed = $this->defaultNamespace->filename;

            // collect the data from the user

            $title = $this->_request->getPost('title');
            $this->defaultNamespace->title = '';
            $description = $this->_request->getPost('description');
            $keywords_str = $this->_request->getPost('keywords');
            $attach = $this->_request->getPost('attach');
            $type = $this->_request->getPost( 'type');
            $dcSource = $parentfeed;
    
            $dcCreator = $this->defaultNamespace->identity['username'];
            $dcDate = date("Y-m-d") . 'T' . date("h:i:s");

   		   $filename = $this->titleParser($title);

            // generate link to feed
            $url = $this->cfg->baseUrl . 'content?feed=';
            $link = $url . $filename . '.rdf';

            $rdfAbout = $link;
            
            if (empty($title) || empty($description)) {
               echo"Please go <a href=\"javascript:history.back();\">back</a> 
               and fill out title, description and keywords!";
               exit();
            }

            /*========================================
               experimental -- register contents
               within title, description (searchapi)
            ========================================*/

            $str = $title . ' ' . $description . ' ' . $keywords_str;
            $sapi = new SearchAPI( );
            $sapi->register( $str, $link);

            $keyword_arr = $sapi->extractKeywords( $keywords_str);

            // setup keywords for this entry
               
            if( count( $keyword_arr)>0) {
               $dbapi = new DBApi( 'files/db/' . $filename . '.db');
               $datadef = array( 'keyword' => 'CHR[32]');
               $dbapi->createDB( $datadef);
               foreach( $keyword_arr as $k) {
                  $kword = array( 'keyword' => $k);
                  $dbapi->addEntry( $kword);
               }
            }
/*
echo 'parentfeed:' . $parentfeed . '<br />';
echo 'filename:' . $filename . '<br />';
echo 'keywords:' . $keywords_str . '<br />';
exit(0);
*/
            /**********************************
             create and save new feed
             **********************************/
            // build channel
            $channel = new RDFNodeChannel();
            $channel->rdfAbout = $rdfAbout;
            $channel->title = $title;
            $channel->link = $link;
            $channel->description = $description;
            $channel->dcCreator = $dcCreator;
             
            if( !empty( $dcSource) && !empty( $attach)) {
               if( $parentfeed!='index.rdf') {
                  $channel->dcSource = $this->defaultNamespace->baseUrl . 'content?feed=' . $dcSource;
               }
            }

            $channel->dcPublisher = 'atuin';
            $channel->dcDate = $dcDate;

            // build feed
            $rdf = new RDFDocumentModel();
            $rdf->addChannel($channel);

            $output = $rdf->toString();
            $savepath = 'files/feeds/';

            file_put_contents($savepath . $filename . '.rdf', $output);

            /************************************
             save new feed as item in parentfeed
             ***********************************/
            if( $attach!=1 || $parentfeed=='') {
               $parentfeed = 'index.rdf';  
            }
            
            unset( $rdfModel);
            $rdfString = file_get_contents('files/feeds/' . $parentfeed);
            $rdfParser = new RDFParser();
            $rdfModel = $rdfParser->parse($rdfString);
            // get channel
            $channel = $rdfModel->channels[0];
            // just show the 10 newest feeds appended to index.rdf
            $items = $channel->items;
            if(count($items) == 10 && $parentfeed=='index.rdf') {
               unset( $items[0]);
            }
            $channel->items = $items;
            // build item
            $item = new RDFNodeItem( );
            $item->rdfAbout = $rdfAbout;
            $item->title = $title;
            $item->link = $link;
            $item->description = $type;
            // avoid storing redundant data here
            // $item->description = $description;
            $item->dcCreator = $dcCreator;
            $item->dcDate = $dcDate;
            // add item to channel
            $discard = false;
            foreach( $channel->items as $it) {
               if( $it->rdfAbout==$item->rdfAbout) {
                  $discard = true;
               }
            }
            if( !$discard) {
               $channel->addItem($item);
               // build changed feed
               $rdf = new RDFDocumentModel();
               $rdf->addChannel($channel);
               // save feed
               $output = $rdf->toString();
                file_put_contents('files/feeds/' . $parentfeed, $output);
            }

            // redirect to created feed
            if( $filename=='index.rdf' || $filename=='') {
               $this->_redirect($this->cfg->baseUrl);               
            }
            $this->_redirect($this->cfg->baseUrl . 'content?feed=' . $filename . '.rdf');               
         }

         $attach = $this->_request->getParam( 'attach');
         
         $view = new Zend_View();
         $view->setScriptPath('../app/views/scripts');
         // the controller script assign necessary variables to the view
         // before it hands over control to the view script
         $acl = new AclPlugin();
         $view->assign('acl', $acl);
         $view->assign('baseUrl', (string) $this->cfg->baseUrl);
         $view->assign('domain', (string) $this->cfg->domain);
         $view->assign('identity', (array) $this->defaultNamespace->identity);
         $view->assign('pagetitle', (string) $this->cfg->pagetitle);
         $view->assign('pagesubtitle', (string) $this->cfg->pagesubtitle);
         $view->assign('layout', (string) $this->cfg->layout);
         $view->assign('title', (string) $this->defaultNamespace->title);
         $view->assign('parentfeed', (string) $this->defaultNamespace->filename);
         if( $attach==1) {
            $view->assign('attach', (int)1);
         }
         else {
            $view->assign('attach', (int)0);
         }
         // render a script
         echo $view->render('index/create.php');
      }
      
      public function feedAction() {
         $tmp = 'files/feeds/' . $this->getRequest()->getParam('feed') . '.rdf';
         if( file_exists($tmp)) {
            file_get_contents($tmp);
         }
         else
            $this->_redirect('/');
      }
}
?>
Return current item: ATUIN