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

      function init() {
      
         $this->initView();
         $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 'content';
	   }
	   
      /**
       * function indexAction
       */
      public function indexAction() {
      
         $file = $this->getRequest()->getParam('feed');
         
         // save filename in session
         $this->defaultNamespace->filename = ($file=='' ? 'index.rdf' : $file);
         
         
         $rdfString = '';
         
         if( !file_exists( 'files/feeds/' . $file)) {
            $this->defaultNamespace->filename = 'index.rdf';
            $rdfString = '';
            $rdfString .= '<' . '?xml version="1.0" encoding="utf-8"?' . '>';
            $rdfString .= '<rdf:RDF';
            $rdfString .= '  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"';
            $rdfString .= '  xmlns:dc="http://purl.org/dc/elements/1.1/"';
            $rdfString .= '  xmlns="http://purl.org/rss/1.0/"';
            $rdfString .= '>';
            $rdfString .= '  <channel rdf:about="http://pcai042.informatik.uni-leipzig.de/~tr-07-2/">';
            $rdfString .= '    <dc:source></dc:source> ';
            $rdfString .= '    <dc:publisher>atuin</dc:publisher> ';
            $rdfString .= '    <dc:creator>ATUIN</dc:creator> ';
            $rdfString .= '    <dc:date>00-00-0000T00:00:00</dc:date>';
            $rdfString .= '    <title>Requested resource no longer exists</title>';
            $rdfString .= '    <link></link>';
            $rdfString .= "    <description>We're sorry to tell you that the requested resource no longer exists.\n\nYour ATUIN-Team</description>";
            $rdfString .= '    <items>';
            $rdfString .= '      <rdf:Seq>';
            $rdfString .= '      </rdf:Seq>';
            $rdfString .= '    </items>';
            $rdfString .= '  </channel>';
            $rdfString .= '</rdf:RDF>';
         }
         else {
            $rdfString = file_get_contents('files/feeds/' . $file);
         }
         
         $rdfParser = new RDFParser();
         $rdfModel = $rdfParser->parse($rdfString);
         
         $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('file', (string) $file);
         $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('content/index.php');
      }
      
      /**
       * function addAction
       */
/*
      public function addAction() {
         if(empty($this->defaultNamespace->identity)) {
            echo"Welcome Guest, Please Log in.";
            exit();
         } 
         $filename = $this->defaultNamespace->filename;       
         if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            // collect the data from the user
            $filter = new Zend_Filter_StripTags();
            $title = $filter->filter($this->_request->getPost('title'));
            $description = $filter->filter($this->_request->getPost('description'));
            // generate link to item
            $date = date("Y-m-d-h_i_s-a");
            $url = "content?feed=";
            $rdfAbout = $url . $filename . '#' . $date;
            // rdfAbout = link
            $link = $date;
            
            if (empty($title) || empty($description)) {
               echo"Please go <a href=\"javascript:history.back();\">back</a> 
               and fill out title and description!";
               exit();
            }
            else {
               // save new item
               $rdfString = file_get_contents('files/feeds/' . $filename);
               $rdfParser = new RDFParser();
               $rdfModel = $rdfParser->parse($rdfString);
               // get channel
               $channel = $rdfModel->channels[0];
               // build item
               $item = new RDFNodeItem( );
               $item->rdfAbout = $rdfAbout;
               $item->title = $title;
               $item->link = $link;
               $item->description = $description;
               // add item to channel
               $channel->addItem($item);
               // build changed feed
               $rdf = new RDFDocumentModel();
               $rdf->addChannel($channel);
               // save feed
               $output = $rdf->toString();
               file_put_contents('files/feeds/' . $filename, $output);
               
               // redirect to new item
               $this->_redirect($rdfAbout);               
            }
         }
      }
*/
      

      /**
       * function deleteAction
       */
      public function deleteAction() {
      
         $acl = new AclPlugin( );

         if( empty($this->defaultNamespace->identity)) {
            echo 'Sorry, you are not allowed to delete this Reference.';
            exit( 0);
         }
         
         $user = $this->defaultNamespace->identity['username'];
         $role = $this->defaultNamespace->identity['realm'];
         
         if( $role!='admin' && !$acl->isAllowed( $role, 'content', 'delete')) {
            echo 'Sorry, you are not allowed to delete this Reference.';
            exit( 0);
         }

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

         // get name of item
         $id = $this->getRequest()->getParam('id');
         
         $rdfString = file_get_contents('files/feeds/' . $filename);
         $rdfParser = new RDFParser();
         $rdfModel = $rdfParser->parse($rdfString);
         

         // create new channel and fill it
         $channel = new RDFNodeChannel();
         $channel->rdfAbout = $rdfModel->channels[0]->rdfAbout;
         $channel->dcSource = $rdfModel->channels[0]->dcSource;
         $channel->dcCreator = $rdfModel->channels[0]->dcCreator;
         $channel->dcPublisher = $rdfModel->channels[0]->dcPublisher;
         $channel->dcDate = $rdfModel->channels[0]->dcDate;
         $channel->title = $rdfModel->channels[0]->title;
         $channel->link = $rdfModel->channels[0]->link;
         $channel->description = $rdfModel->channels[0]->description;
         $channel->image = $rdfModel->channels[0]->image;

         // select item and delete it
         foreach($rdfModel->channels[0]->items as $item) {
            if($item->link != $id) {
               $channel->addItem($item);
            }
           else if( $item->dcCreator!=$this->defaultNamespace->identity['username']) {
               echo 'Sorry, you are not allowed to delete this Reference.';
               exit( 0);
            }
         }
         // build changed feed
         $rdf = new RDFDocumentModel();
         $rdf->addChannel($channel);
         // save feed
         $output = $rdf->toString();
         file_put_contents('files/feeds/' . $filename, $output);
         
         // redirect to changed feed
         $this->_redirect($this->cfg->baseUrl . 'content?feed=' . $filename);
      }

      /**
       * deletes a complete problem feed
       */
      public function unlinkAction( ) {
      
         $acl = new AclPlugin( );

         if( empty($this->defaultNamespace->identity)) {
            echo 'Sorry, you are not allowed to delete this Problem feed.';
            exit( 0);
         }
         
         $user = $this->defaultNamespace->identity['username'];
         $role = $this->defaultNamespace->identity['realm'];
         
         if( !$acl->isAllowed( $role, 'content', 'delete')) {
            echo 'Sorry, you are not allowed to delete this Reference.';
            exit( 0);
         }

         $filename = $this->defaultNamespace->filename;
		   $rdfString = '';

         if( file_exists( 'files/feeds/' . $filename)) {
            $rdfString = file_get_contents('files/feeds/' . $filename);
         }
         else {
            $this->_redirect( $this->cfg->baseUrl);
         }
         $rdfParser = new RDFParser();
         $rdfModel = $rdfParser->parse($rdfString);
         $channel = $rdfModel->channels[0];
         
         if( $role!='admin' && $user!=$channel->dcCreator) {
            echo 'Sorry <b>'. $this->defaultNamespace->identity['username'] . '</b>, you are not allowed to delete this Feed.';
            exit( 0);
         }

         $rdfAbout = $channel->rdfAbout;
                  
         // unregister keywords
         $keywordfile = 'files/db/' . preg_replace( '/.rdf/', '.db', $filename);
         $dbapi = new DBApi( $keywordfile);         
         $keywords = $dbapi->getKeysByRow( 'keyword');
         $keywordstr = '';
         foreach( $keywords as $item) {
            $keywordstr .= ' ' . $item;
         }

         $sapi = new SearchAPI( );
         $sapi->unregister( $channel->title . ' ' . $channel->description . ' ' . $keywordstr, $this->cfg->baseUrl . 'content?feed=' . $filename);

         if( file_exists( $keywordfile)) {
            unlink( $keywordfile);
         }
         
         $dbfile = preg_replace( '/.db/', '.tree', $keywordfile);
         if( file_exists( $dbfile)) {
            unlink( $dbfile);
         }
         
         if( file_exists( 'files/feeds/' . $filename)) {
            unlink( 'files/feeds/' . $filename); 
         }

         /* update parentfeed */

         $parentfeed = 'index.rdf';

         if( $channel->dcSource!='') {
            $parentfeed = preg_replace( '/.*feed=/', '', $channel->dcSource);
         }

         if( file_exists( 'files/feeds/' . $parentfeed)) {
            $rdfString = file_get_contents( 'files/feeds/' . $parentfeed);
         }
         else {
            $this->_redirect( $this->cfg->baseUrl);
         }
         $rdfModel = $rdfParser->parse( $rdfString);
         $channel = $rdfModel->channels[0];
         
         $doc = new RDFDocumentModel( );
         $c = new RDFNodeChannel( );
         $c->rdfAbout = $channel->rdfAbout;
         $c->dcCreator = $channel->dcCreator;
         $c->dcPublisher = $channel->dcPublisher;
         $c->dcDate = $channel->dcDate;
         $c->title = $channel->title;
         $c->link = $channel->link;
         $c->description = $channel->description;         
          
         foreach( $channel->items as $item) {
            if( $item->rdfAbout!=$rdfAbout) {
               $c->addItem( $item);
            }
         }

         $doc->addChannel( $c);
         
         $output = $doc->toString( );
         file_put_contents( 'files/feeds/' . $parentfeed, $output);
                  
         /* redirect */
         if( $parentfeed=='index.rdf') {
            $this->_redirect( $this->cfg->baseUrl);
         }
         $this->_redirect( $this->cfg->baseUrl . 'content?feed=' . $parentfeed);
      }
      
      /**
       * function editAction
       */
      public function editAction() {
         $redirect = $this->getRequest()->getParam('redirect_to');
         if(empty($this->defaultNamespace->identity)) {
            echo "Sorry, you are not allowed to edit this Feed.";
            exit( 0);
         }
          
         $filename = $this->defaultNamespace->filename;

         if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
 
            // collect the data from the user
            $filter = new Zend_Filter_StripTags();
            $description = $filter->filter($this->_request->getPost('description'));
            $keywords = $filter->filter($this->_request->getPost('keywords'));
           
            if( empty($description)) {
               echo"Please go <a href=\"javascript:history.back();\">back</a> 
               and fill out title and description!";
               exit();
            }

            $fd = preg_replace( '/.rdf/', '', $filename);
            
            unlink( 'files/db/' . $fd . '.db');
            unlink( 'files/db/' . $fd . '.tree');

            // setup keywords for this entry
            $sapi = new SearchAPI( );   
            $keyword_arr = $sapi->extractKeywords( $keywords);
            $dbapi = new DBApi( 'files/db/' . $fd . '.db');
            $datadef = array( 'keyword' => 'CHR[32]');
            $dbapi->createDB( $datadef);
            foreach( $keyword_arr as $k) {
               $kword = array( 'keyword' => $k);
               $dbapi->addEntry( $kword);
            }
            
            $rdfString = file_get_contents('files/feeds/' . $filename);
            $rdfParser = new RDFParser();
            $rdfModel = $rdfParser->parse($rdfString);
               
            // create new channel and fill it
            $channel = new RDFNodeChannel();
            $channel->rdfAbout = $rdfModel->channels[0]->rdfAbout;
            $channel->title = $rdfModel->channels[0]->title;
            $channel->link = $rdfModel->channels[0]->link;
            $channel->description = $description;
            $channel->image = $rdfModel->channels[0]->image;
            $channel->dcSource = $rdfModel->channels[0]->dcSource;
            $channel->dcPublisher = $rdfModel->channels[0]->dcPublisher;
            $channel->dcCreator = $rdfModel->channels[0]->dcCreator;
            $channel->dcDate = $rdfModel->channels[0]->dcDate;

            // select item and delete it
            foreach($rdfModel->channels[0]->items as $item) {
               $channel->addItem($item);
            }
            // build changed feed
            $rdf = new RDFDocumentModel();
            $rdf->addChannel($channel);
            // save feed
            $output = $rdf->toString();
            file_put_contents('files/feeds/' . $filename, $output);
            // redirect to changed feed
            $this->_redirect( $this->cfg->baseUrl . 'content?feed=' . $filename);
         }

         // get keywords
         $keywordfile = 'files/db/' . preg_replace( '/\.rdf/', '.db', $filename);
         $dbapi = new DBApi( $keywordfile);         
         $keywords = $dbapi->getKeysByRow( 'keyword');
         $keywordstr = '';
         foreach( $keywords as $item) {
            $keywordstr .= $item . ' ';
         }

         // fill in old content
         $rdfString = file_get_contents('files/feeds/' . $filename);
         $rdfParser = new RDFParser( );
         $rdfModel = $rdfParser->parse( $rdfString);

         $title = $rdfModel->channels[0]->title;
         $description = $rdfModel->channels[0]->description;
         $author = $rdfModel->channels[0]->dcCreator;
         
         $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('redirect', (string) $redirect);
         $view->assign('title', (string) $title);
         $view->assign('description', (string) $description);
         $view->assign('keywords', (string) $keywordstr);
         $view->assign('author', (string) $author);
         $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);
         echo $view->render('content/edit.php');
      }

      public function searchAction( ) {

         $keywords = '';
         $denywords = '';
         $logic = '';
         $sapi = new SearchAPI( );
         $matches = null;

         $title = $this->_request->getPost('title');
         if( empty( $title)) {
            $this->defaultNamespace->filename = 'index.rdf';
         }

         if( isset( $this->defaultNamespace->title)) {
            unset( $this->defaultNamespace->title);
         }
         
         $merke = '';
         
         if( $title!='') { 
            $this->defaultNamespace->title = str_replace( "\'", "'", $title); 
            $matches = $sapi->search( $this->defaultNamespace->title, '', 'and');
         }
         else {
            $_KEYWORDS = '';
            $_DENYWORDS = '';
            if( empty( $keywords)) {
               $merke = str_replace( "\'", "'", $this->_request->getPost( 'keywords'));
               $keywords = $merke;
               $keywords = preg_replace( '/ +/  ', ' ', $keywords);
               $keywords = preg_replace( '/ *\+ */  ', '+', $keywords);
               $keywords = preg_replace( '/ *- */  ', '-', $keywords);
//               $keywords = preg_replace( "\'", "'", $keywords);
               $K = preg_split( '/[ |+|-]/', $keywords);
               $str = preg_split( '//', $keywords);
            
               foreach( $K as $k) {
                  @$pos = strpos( $keywords, $k, 0);
                  if( $pos!==false) {
                     if($pos==0) {
                        $_KEYWORDS .= ' ' . $k;
                     }
                     else {
                        switch( $str[$pos]) {
                           case '-':
                              $_DENYWORDS .= ' ' . $k;
                              break;
                           default:
                              $_KEYWORDS .= ' ' . $k;
                              break;
                        }
                     }
                  }
               }
            }
            $matches = $sapi->search( $_KEYWORDS, $_DENYWORDS, 'or');
         }

         $view = new Zend_View( );
         $acl = new AclPlugin();
         $view->assign('acl', $acl);
         $view->setScriptPath('../app/views/scripts');
         $view->assign('matches', (array)$matches);         
         $view->assign('baseUrl', (string)$this->cfg->baseUrl);
         $view->assign('domain', (string) $this->cfg->domain);
         $view->assign('identity', (array)$this->defaultNamespace->identity);
         $view->assign('keywords', (string)$merke);
         $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('filename', (string)$this->defaultNamespace->filename);
         echo $view->render( 'content/search.php');
      }
      
      public function joinAction( ) {

         $acl = new AclPlugin( );
         $role = empty( $this->defaultNamespace->identity) ? 'guest' : $this->defaultNamespace->identity['realm'];

         if( !$acl->isAllowed( $role, 'index', 'create')) {
            echo "Sorry, you're not allowed to manipulate contents.";
            exit( 0);
         }

         $resource = $this->_request->getPost('reference');
         if(empty($resource)) {
             $resource = $this->getRequest()->getParam('reference');
         }

         // check only if file is an rss-feed
         $isRss = preg_match( '/.*\.rdf/i', $resource);
         
         if( !$isRss) {
            echo "The URI you entered is not a RSS feed.";
            exit( 0);
         }

         $resource2 = substr($resource, 0, strlen($this->cfg->baseUrl));
         if($resource2 == $this->cfg->baseUrl) {
            $resource = substr($resource, strlen($this->cfg->baseUrl));
            $sourcefeed = file_get_contents( $resource, "r");
         }
         else {
            $ch = curl_init($resource);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $sourcefeed = curl_exec($ch);
            curl_close($ch);
         }

         if( empty( $sourcefeed)) {
            echo 'Sorry, the resource you want to refer to is not available.';
            exit( 0);
         }

         $rdfParser = new RDFParser( );
         $rdfModel = $rdfParser->parse( $sourcefeed); 
         
         $channel = $rdfModel->channels[0];
         
         if( $channel->dcPublisher!='atuin') {
            echo 'Sorry, this RSS feed does not belong to the ATUIN network.';
            exit( 0);
         }

         $filename = $this->defaultNamespace->filename;
         $title = $channel->title;
         
         unset( $rdfModel);

         $rdfString = file_get_contents( 'files/feeds/' . $filename);
         $rdfModel = $rdfParser->parse( $rdfString);
         // get channel
         $channel = $rdfModel->channels[0];
         // build item
         $item = new RDFNodeItem( );
         $item->rdfAbout = preg_replace( '/files.feeds./', 'content?feed=', $resource);
         $item->title = $title;
         $item->link = $item->rdfAbout;
         $item->description = 'extern';
         $item->dcCreator = $this->defaultNamespace->identity['username'];
         $item->dcDate = date("Y-m-d") . 'T' . date("h:i:s");

         // add item to channel
         $channel->addItem($item);
         // build changed feed
         $rdf = new RDFDocumentModel();
         $rdf->addChannel( $channel);

         // save feed
         $output = $rdf->toString();
         file_put_contents('files/feeds/' . $filename, $output);

         // redirect to created feed
         $this->_redirect( $this->cfg->baseUrl . 'content?feed=' . $filename);               
      }
   }
?>
Return current item: ATUIN