blogger_user.php

Go to the documentation of this file.
00001 <?PHP
00002 require_once( "$module_folder/lastrss/lastRSS.php" );
00003 
00004 class blogger_user
00005 {
00006         var $parentEntries;               
00007         var $username;                    
00008         
00009         var $entries;                     
00010         
00011         var $externalCache;               
00012         
00013         // Settings
00014         var $allowsviewpersonal;
00015         var $viewlevel;
00016         var $commentlevel;
00017         var $personal;
00018         var $categories;
00019         var $defaultCategories;
00020         
00021         function blogger_user( $entries, $username )
00022         {
00023                 $this->parentEntries = &$entries;
00024                 $this->username = $username;
00025                 
00026                 $this->load();
00027         }
00028         
00029         function load()
00030         {
00031                 global $userinfo_folder;
00032                 $this->entries = array();
00033                 // cycle through all files in user's blog dir
00034                 if( file_exists( $userinfo_folder . "/" . $this->username . "/blogger" ) )
00035                 {
00036                         $fp = opendir( $userinfo_folder . "/" . $this->username .  "/blogger" );
00037                         while ($file = readdir($fp))
00038                         {
00039                         // create blogger_entry instances for each file, but only if they're called somethingorother.php (then they're not a lock file and not a directory entry)
00040                                 $suffix = substr($file, -4);
00041                                 $title = substr($file, 0, -4);
00042                                 if( $suffix == ".php" )
00043                                 {
00044                                         $theEntry = new blogger_entry( &$this, $title );
00045                                         if( $theEntry->personal == false || userAllows( $this->username, $this->getParam("allowsviewpersonal") ) )
00046                                         {
00047                                                 $showEntry = false;
00048                                                 if( $this->parentEntries->categories == null )
00049                                                         $showEntry = true;
00050                                                 else
00051                                                 {
00052                                                         foreach( $this->parentEntries->categories as $key => $value )
00053                                                                 if( in_array( $value, $theEntry->categories ) )
00054                                                                         $showEntry = true;
00055                                                 }
00056                                                 
00057                                                 if( $showEntry )
00058                                                         $this->entries[$title] = $theEntry;
00059                                         }
00060                                 }
00061                         }
00062                         closedir($fp);
00063                 }
00064                 
00065                 $this->fetchExternal();
00066                 krsort( $this->entries );
00067         }
00068         
00069         function fetchExternal()
00070         {
00071                 global $userinfo_folder, $module_folder;
00072                 $timelapse = 900; // Minimum a quarter of an hour between each update
00073                 $oldTime = $this->getParam( "oldTime" );
00074                 
00075                 if( $oldTime == null || time() - $oldTime > $timelapse )
00076                 {
00077                         $update = false;
00078                         // Create lastRSS object
00079                         $rss = new lastRSS;
00080                         
00081                         // Set cache dir and cache time limit (1200 seconds)
00082                         // (don't forget to chmod cahce dir to 777 to allow writing)
00083                         $rss->cache_dir = "$module_folder/lastrss/temp";
00084                         $rss->cache_time = 1200;
00085                         
00086                         // Update the external cache
00087                         $this->getParam("externalCache");
00088                         $external = $this->getParam("external");
00089                         foreach( $external as $key => $entry )
00090                         {
00091                                 if( $rs = $rss->get( $entry ) )
00092                                 {
00093                                         if( is_array( $rs["items"] ) )
00094                                         {
00095                                                 foreach( $rs["items"] as $item )
00096                                                 {
00097                                                         // Cache items by way of their URL ($item["link"])
00098                                                         if( ! array_key_exists( $item["link"], $this->externalCache[$entry] ) )
00099                                                         {
00100                                                                 $newEntry = new blogger_entry( &$this );
00101                                                                 $newEntry->title = $item["title"];
00102                                                                 // Should probably add the link (in a sort of "This came from here (link)" kind of way)
00103                                                                 $newEntry->content = $item["description"];
00104                                                                 
00105                                                                 // If there is no timestamp - we fix it...
00106                                                                 if( $item["pubDate"] == false )
00107                                                                 {
00108                                                                         $timestamp = time();
00109                                                                         while( array_key_exists( $timestamp, $this->entries ) )
00110                                                                                 $timestamp++;
00111                                                                 }
00112                                                                 else
00113                                                                         $timestamp = strtotime( $item["pubDate"] );
00114                                                                 $newEntry->timestamp = $timestamp;
00115                                                                 
00116                                                                 $newEntry->personal = $this->getParam("personal");
00117                                                                 $newEntry->viewLevel = $this->getParam("viewlevel");
00118                                                                 $newEntry->commentLevel = $this->getParam("commentlevel");
00119                                                                 $newEntry->categories = $this->getParam("defaultCategories");
00120                                                                 $newEntry->published = true;
00121                                                                 
00122                                                                 $newEntry->createFilename();
00123                                                                 $newEntry->save();
00124                                                                 $this->entries[$newEntry->timestamp] = $newEntry;
00125                                                                 // Let's futureproof this a bit - we might want to prune this by added-time at a later point in time, who knows ;)
00126                                                                 $this->externalCache[$entry][$item["link"]] = time();
00127                                                                 $updated = true;
00128                                                         }
00129                                                 }
00130                                         }
00131                                 }
00132                         }
00133                         $this->setParam( "oldTime", time() );
00134                         $this->setParam( "externalCache", $this->externalCache );
00135                         if( $updated )
00136                                 header( "Location: " . thisPageURL() );
00137                 }
00138         }
00139         
00140         function save()
00141         {
00142                 // Dummy entry... we don't actually have anything saveable per se...
00143         }
00144         
00145         function getParam( $paramName )
00146         {
00147                 if( $this->$paramName == null )
00148                 {
00149                         $this->$paramName = getUserInfo( $this->username, "blogger_" . $paramName );
00150                         if( $paramName == "categories" || $paramName == "external" || $paramName == "defaultCategories" || $paramName == "externalCache" )
00151                         {
00152                                 $this->$paramName = unserialize( $this->$paramName );
00153                                 if( !is_array( $this->$paramName ) )
00154                                         $this->$paramName = array();
00155                         }
00156                         if( $this->$paramName == i18n("No ##0## information\n", array( "blogger_" . $paramName ) ) )
00157                                 $this->$paramName = null;
00158                 }
00159                 return $this->$paramName;
00160         }
00161         
00162         function setParam( $paramName, $value )
00163         {
00164                 $this->$paramName = $value;
00165                 saveUserInfo( $this->username, "blogger_" . $paramName, $value );
00166         }
00167         
00168         function getGlobalID( $aswhat = "view" )
00169         {
00170                 if( $aswhat == "view" )
00171                         $aswhat = "";
00172                 else
00173                         $aswhat = "/" . $aswhat;
00174                 
00175                 return "user/" . $this->username . "/blogger" . $aswhat;
00176         }
00177         
00178         function renderEditor()
00179         {
00180                 global $globalID;
00181                 // user/(username)/blogger/edit/(section)/(item)/(action)
00182                 $splitID = explode( "/", $globalID );
00183                 switch( $splitID[4] )
00184                 {
00185                 // section: options
00186                 case "options":
00187                         $dialog = new blogger_userOptions( &$this, $splitID[5], $splitID[6] );
00188                         if( array_key_exists( 7, $splitID ) )
00189                                 $dialog->item = $splitID[7];
00190                         $renderedContent = $dialog->render();
00191                         break;
00192                 // section: entry (and none)
00193                 case "entry":
00194                 default:
00195                         //  action: new
00196                         if( $splitID[5] == "new" )
00197                         {
00198                                 $entry = new blogger_entry( &$this );
00199                                 $entry->title = i18n("New entry");
00200                                 $entry->content = i18n("Enter content for your blog entry here.");
00201                                 $entry->personal = $this->getParam("personal");
00202                                 $entry->viewLevel = $this->getParam("viewlevel");
00203                                 $entry->commentLevel = $this->getParam("commentlevel");
00204                                 $entry->categories = $this->getParam("defaultCategories");
00205                                 $entry->save();
00206                                 header( "Location:" . globalIDtoURL( $this->getGlobalID( "edit/entry/" . $entry->timestamp ) ) );
00207                         }
00208                         //  otherwise pass on to blogger_entry instance
00209                         else if( is_numeric( $splitID[5] ) )
00210                         {
00211                                 $entry = &$this->entries[$splitID[5]];
00212                                 $renderedContent = $entry->renderEditor();
00213                         }
00214                         // ARGH! BAIL! (or, rather, return back up one level...)
00215                         else if( $splitID[5] == "entry" )
00216                         {
00217                                 header( "Location: " . $this->getGlobalID("edit") );
00218                         }
00219                         // Just list entries in block format...
00220                         else
00221                         {
00222                                 if( count($this->entries) < 1 )
00223                                 {
00224                                         $renderedContent = renderInformationBox( i18n("No entries"), i18n("There are no blog entries to show. Please click on ##0## to create a new entry, or go to the ##1## dialog to change your options before you begin", array( drawCommand( i18n("New entry"), i18n("Create a new blog entry"), globalIDtoURL( $this->getGlobalID("edit/entry/new") ) ), drawCommand( i18n("Options"), i18n("Change the options for the blogging module"), globalIDtoURL( $this->getGlobalID("edit/options") ) ) ) ) );
00225                                 }
00226                                 else
00227                                 {
00228                                         foreach( $this->entries as $key => $entry )
00229                                                 $renderedContent .= $this->entries[$key]->renderTiny();
00230                                 }
00231                         }
00232                         break;
00233                 }
00234                 return $renderedContent;
00235         }
00236         
00237         function renderFeed()
00238         {
00239         }
00240         
00241         function render( $timestamp = null )
00242         {
00243                 if( $this->parentEntries->edit )
00244                         $renderedContent = $this->renderEditor();
00245                 else
00246                 {
00247                         if( $timestamp == null )
00248                                 $renderedContent = i18n("ERROR!");
00249                         else
00250                                 $renderedContent = $this->entries[$timestamp]->render();
00251                 }
00252                 return $renderedContent;
00253         }
00254 }
00255 ?>

Generated on Sun Oct 26 20:33:12 2008 for The Travelsized Content Management System by  doxygen 1.5.5