Caravel CMS PHP Cross Reference Content Management Systems

Source: /apps/Blog/Blog.php - 331 lines - 11970 bytes - Summary - Text - Print

   1  <?php
   2  
   3  require_once(T_APP_BASE . 'FolderViewer/FolderViewer.php');
   4  
   5  define('CV_BLOG_FOLDER', 'mnOD=Blogs,' . C_LDAP_BASE_FOLDER);
   6  
   7  /**
   8  * creates a blog
   9  *
  10  * The Blog app makes use of the Folder Viewer behind the scenes,
  11  * but provides a simpler settings page optimized for blogs.
  12  *
  13  * @author David Glick <davidig@caravelcms.org>
  14  * @date    2007.06.26
  15  */
  16  
  17  class Blog extends App {
  18      function dispatch() {
  19          // return configuration/editing screen if we're in that mode
  20          if ( isset($this->httpVars['cv_submit']) )
  21              {
  22              // close window if necessary
  23              $submit = str_replace('&amp;', '&', $this->httpVars['cv_submit']);
  24              if (T_SINGLE_BLOCK and ($submit == 'Save & Preview' or $submit == 'Save & Close'))
  25                  return Utility::getCloseRefreshJavascript();
  26              else if(T_SINGLE_BLOCK and ($submit == 'Save & Return'))
  27                  return Utility::getCloseRefreshJavascript('' , true, false);
  28              }
  29          
  30          if ($this->edit && $this->can('config'))
  31              {
  32              if (method_exists($this, 'getConfig'))
  33                  return $this->getConfig();
  34              else
  35                  return $this->getContent();
  36              }
  37          else if($this->edit)
  38              return $this->getBlogPostOnlyConfig();
  39                  
  40          return $this->getContent();
  41      }
  42  
  43  	function onInit() {
  44          // default to the current user's groups, if the blog hasn't been saved yet
  45          if (!isset($this->settingsVars['groups']))
  46              $this->settingsVars['groups'] = array_keys(Security::getUserSiteGroups($_SESSION['auth']->userDN, T_ORG_DN, $this->ldap));
  47          if (!isset($this->settingsVars['commentGroups']))
  48              $this->settingsVars['commentGroups'] = array_keys(Security::getUserSiteGroups($_SESSION['auth']->userDN, T_ORG_DN, $this->ldap));
  49  
  50          $this->blog_dn = $this->_blog_name_to_dn($this->params['blogName']);
  51          $this->mode = isset($this->httpVars['mode']) ? $this->httpVars['mode'] : 'blog';
  52      }
  53      
  54  	function getHeader() {
  55          if ($this->mode == 'blog_post') {
  56              require_once(T_APP_BASE . 'Editor/Editor.php');
  57              return Editor::getAllHeaders('Editor');
  58          }
  59          return App::getAllHeaders('Comments');
  60      }
  61  
  62  	function getContent() {
  63          $blog_folder = $this->_blog_folder();    
  64          if (!isset($this->settingsVars['blogName']))
  65              return 'This blog needs to be configured.';
  66          $o = '';
  67  
  68          $o .= '<div class="cvBlogTitle">' . $this->settingsVars['blogName'] . '</div>';
  69                  
  70          if ($this->_can_post() && !$this->dynamic_content) {
  71              $o .= Widget::form_look('', '', 'beginning');
  72              $link = str_replace('&amp;', '&', Utility::link($this->sectionPathArray, array($this->paramName('edit') => 1, $this->paramName('mode') => 'blog_post', '_single' => $this->blockID)));
  73              $o .= "<a href=\"#\" onclick=\"popup('$link', 'post', 800, 700);\">Create a new blog post...</a>";
  74              $o .= Widget::form_look('', 'Blog', 'end');
  75          }
  76              
  77          // display the folder viewer
  78          $this->_init_folder_viewer();
  79          $this->fv->dynamic_content = $this->dynamic_content;
  80          $this->fv->settingsVars['listTemplate'] = 'a~title~aend~by ~author~br~date~ at ~time~br~adminlink~brief~' . (@$this->settingsVars['showCategories'] ? 'categories~' : '') . (@$this->settingsVars['comments'] ? 'commentslink~' : '') . '<br /><br /><hr /><br />';
  81          $this->fv->settingsVars['fileTemplate'] = 'title~by ~author~br~date~ at ~time~br~adminlink~contents~' . (@$this->settingsVars['showCategories'] ? 'categories~' : '') . 'prev~list~next~' . (@$this->settingsVars['comments'] ? 'comments~' : '' );
  82          $o .= $this->fv->getContent();
  83          
  84          return $o;
  85      }
  86      
  87  	function getRSS() {
  88          $this->_init_folder_viewer();
  89          return $this->fv->getRSS();
  90      }
  91      
  92  	function _init_folder_viewer() {
  93          // set up our Folder Viewer
  94          $set = array(
  95              'folderDN' => $this->blog_dn,
  96              'listType' => 'brief',
  97              'limitType' => 'date',
  98              'limit' => '1 month ago',
  99              'sortStyle' => 'date',    
 100              'linkTarget' => '_self',
 101              // hide the file we use to keep track of the categories
 102              'hidden' => 'categories.txt:;:off',
 103              // the following are for the comments sub-app
 104              'postingGroups' => @$this->settingsVars['commentGroups'],
 105          );
 106          $this->fv = new FolderViewer;
 107          $fv_request = $this->httpVars;
 108          unset($fv_request['cv_submit']);
 109          $this->fv->init('FolderViewer', $this->blockID, $this->ldapDN, $this->pageDN, $this->section, $fv_request, $set, $this->siteVars, $this->sectionPathArray, App::getDir('FolderViewer'), $this->edit);
 110          $this->fv->mode = 'blog';
 111      }
 112      
 113  	function onSave() {
 114          if ($this->mode == 'blog_post' && $this->_can_post()) {
 115              require_once(T_APP_BASE . 'Editor/Editor.php');
 116              $ret = Editor::onSave();
 117              if($this->settingsVars['originalEditorOnly'])
 118                  $this->edoc->permissionUpdate(array('cvFilePriv' => '211'));
 119              return $ret;
 120          }
 121  
 122          $blog_name = isset($this->httpVars['blog_name']) ? $this->httpVars['blog_name'] : '_';
 123          if ($blog_name[0] != '_') {
 124              $blog_dn = (strpos($blog_name, ',mnOD=') !== false) ? $blog_name : $this->_blog_name_to_dn($blog_name);
 125              $blog_name = DN::dnToName($blog_dn);
 126  
 127              // create the new blog folder if necessary
 128              if (!$this->ldap->dnExists($blog_dn)) {
 129                  $this->_ensure_blog_folder_exists();
 130                  oDirectory::create($this->ldap, $blog_dn);
 131              }
 132              
 133              $this->settingsVars['blogName'] = $blog_name;
 134          } else {
 135              unset($this->settingsVars['blogName']);
 136          }
 137          
 138          $this->settingsVars['groups'] = $this->httpVars['groups'];
 139          // update privileges for files in the blog folder to match the groups who have been given privileges
 140          if (isset($blog_dn)) {
 141              $dir = oDirectory::load($this->ldap, $blog_dn);
 142              $attrs = array('cvFileGroup' => $this->httpVars['groups']);
 143              $dir->permissionUpdate($attrs, true);
 144              $this->blog_dn = $blog_dn;
 145          }
 146          
 147          $this->settingsVars['comments'] = isset($this->httpVars['comments']);
 148          $this->settingsVars['commentGroups'] = $this->httpVars['commentGroups'];
 149          $this->settingsVars['showCategories'] = isset($this->httpVars['showCategories']);
 150          $this->settingsVars['originalEditorOnly'] = isset($this->httpVars['originalEditorOnly']);
 151          
 152          $this->saveSettings($this->settingsVars);
 153      }
 154      
 155  	function getBlogPostOnlyConfig() {
 156           if ($this->mode == 'blog_post' && $this->_can_post()) {
 157              require_once(T_APP_BASE . 'Editor/Editor.php');
 158              unset($this->settingsVars['title']);
 159              unset($this->settingsVars['description']);
 160              $this->editorVars['defaultFolder'] = substr($this->blog_dn, 0, - strlen(','.T_ORG_DN));
 161              $this->editorVars['defaultExtension'] = 'html';
 162              Editor::onInit();
 163              
 164              if ( isset($this->httpVars['cv_submit']) && $this->httpVars['cv_submit'] != "")
 165                  {
 166                  Editor::onSave();
 167  
 168                  // close window if necessary
 169                  $submit = str_replace('&amp;', '&', $this->httpVars['cv_submit']);
 170  
 171                  if (T_SINGLE_BLOCK and ($submit == 'Save & Preview' or $submit == 'Save & Close'))
 172                      return Utility::getCloseRefreshJavascript();
 173                  }         
 174              
 175              return Editor::getConfig();
 176          }     
 177      }
 178      
 179  	function getConfig() {
 180          if ($this->mode == 'blog_post' && $this->_can_post()) {
 181              require_once(T_APP_BASE . 'Editor/Editor.php');
 182              unset($this->settingsVars['title']);
 183              unset($this->settingsVars['description']);
 184              $this->editorVars['defaultFolder'] = substr($this->blog_dn, 0, - strlen(','.T_ORG_DN));
 185              $this->editorVars['defaultExtension'] = 'html';
 186              Editor::onInit();
 187              return Editor::dispatch();
 188          }
 189      
 190          $blog_folder = $this->_blog_folder();
 191          
 192          $f = FormWidget::init();
 193          $f->name = 'main';
 194          $f->section();
 195          
 196          $f->field('Blog Name:');
 197          $selected_blog = @$this->settingsVars['blogName'] ? $this->settingsVars['blogName'] : '';
 198          $selected_blog_dn = $this->_blog_name_to_dn($selected_blog);
 199          $blogs = array();
 200          if ($this->ldap->dnExists($blog_folder)) {
 201              $blogs = array(
 202                  ($selected_blog ? $selected_blog : '_existing') => array('Existing blog: ' . FormWidget::folderList('existing_blog_name', $selected_blog_dn, '', array('rows' => 10), $blog_folder, false)),
 203                  '_new' => array('New blog: ' . FormWidget::text('new_blog_name', '', 25))
 204                  );
 205              $f->radio('blog_name', $blogs, $selected_blog ? $selected_blog : '_new');
 206          } else {
 207              $f->text('blog_name', '', 25);
 208          }
 209          
 210          $f->field('Privileges:');
 211          $f->html('Members of these groups will be allowed to post on this blog.<br/><span style="width: 3em;">&nbsp;</span>');
 212          $f->groupList('groups[]', @$this->settingsVars['groups'], 0, 1, 10);
 213          $f->html('<br />(Hold the Ctrl key while clicking to select multiple groups.)');
 214          
 215          $f->field('Group Blog:');
 216          $f->checkbox('originalEditorOnly', $this->settingsVars['originalEditorOnly'], 'Only the original poster can edit the files.');
 217  
 218          $f->field('Comments:');
 219          $f->checkbox('comments', @$this->settingsVars['comments'], 'Enable comments');
 220          $f->html('<br/><br/>Members of these groups will be allowed to comment on posts on this blog.<br/><span style="width: 3em;">&nbsp;</span>');
 221          $f->groupList('commentGroups[]', @$this->settingsVars['commentGroups'], 0, 1, 10);
 222          
 223          $f->field('Categories:');
 224          $f->checkbox('showCategories', @$this->settingsVars['showCategories'], 'Show categories');
 225          
 226          $f->field();
 227          $f->defaultButtons();
 228          return $f->get();
 229      }
 230      
 231      /**
 232      * returns a list of the categories for this blog
 233      *
 234      * @return    array    an associative array -- the keys are the category names and the values are the count of posts in that category
 235      */
 236  	function getCategories() {
 237          $cat_file_dn = Blog::_categories_file();
 238          $cats = array();
 239          if ($this->ldap->dnExists($cat_file_dn)) {
 240              $cat_file = oFile::load($this->ldap, $cat_file_dn);
 241              $cat_data = array_map('rtrim', file($cat_file->file->name));
 242              foreach($cat_data as $line) {
 243                  $tmp = explode('|', $line);
 244                  $cats[$tmp[0]] = $tmp[1];
 245              }
 246          }
 247  
 248          if (!count($cats))
 249              $cats = array('uncategorized' => 0);
 250              
 251          return $cats;
 252      }
 253  
 254      /**
 255      * updates the list of categories (stored in a Caravel file)
 256      *
 257      * @param    array    $old_cats    the old list of categories for a particular post
 258      * @param    array    $new_cats    the new list of categories for a particular post
 259      * @return    null
 260      */    
 261  	function updateCategories($old_cats, $new_cats) {
 262          $stored_cats = $this->getCategories();
 263          
 264          // decrement count for categories that were removed
 265          foreach($old_cats as $old_cat) {
 266              if (!in_array($old_cat, $new_cats)) {
 267                  $stored_cats[$old_cat]--;
 268                  if ($stored_cats[$old_cat] <= 0) {
 269                      unset($stored_cats[$old_cat]);
 270                  }
 271              }
 272          }
 273          
 274          // increment count for categories that were added
 275          foreach ($new_cats as $new_cat) {
 276              if (!in_array($new_cat, $old_cats)) {
 277                  $stored_cats[$new_cat]++;
 278              }
 279          }
 280  
 281          // sort the list by category name and serialize the list
 282          uksort($stored_cats, 'strnatcasecmp');
 283          $cat_list = '';
 284          foreach($stored_cats as $cat => $count) {
 285              $cat_list .= "$cat|$count\n";
 286          }
 287  
 288          // save the list 
 289          $cat_file_dn = $this->_categories_file();
 290          if ($this->ldap->dnExists($cat_file_dn)) {
 291              $cat_file = oFile::load($this->ldap, $cat_file_dn);
 292          } else {
 293              $cat_file = oFile::create($this->ldap, $cat_file_dn, '', 'text/plain');
 294          }
 295          $cat_file->write($cat_list);
 296      }
 297      
 298  	function _blog_folder() {
 299          return CV_BLOG_FOLDER . ',' . T_ORG_DN;
 300      }
 301      
 302  	function _ensure_blog_folder_exists() {
 303          $dn = $this->_blog_folder();
 304          if(!$this->ldap->dnExists($dn))
 305              oDirectory::create($this->ldap, $dn);
 306      }
 307      
 308  	function _categories_file() {
 309          return 'mnF=categories.txt,' . $this->blog_dn;
 310      }
 311      
 312  	function _blog_name_to_dn($name) {
 313          return 'mnOD=' . $name . ',' . Blog::_blog_folder();
 314      }
 315      
 316  	function _can_post() {
 317          return (T_SUPERUSER || (Security::isUserInGroups($_SESSION['auth']->userDN, $this->settingsVars['groups'], $this->ldap)));
 318      }
 319  
 320      /**
 321      * returns an array of the block ID and blog name for the (first) blog on the page, or false if none is found
 322      */
 323  	function _auto_detect_blog() {
 324          foreach ($this->section->blocks as $block) {
 325              if ($block->appName == 'Blog')
 326                  return array($block->id, $block->blockParams['blogName']);
 327          }
 328      }
 329  }
 330  
 331  ?>

title

Description

title

Description

title

Description

title

title

Body