Changeset 60

Show
Ignore:
Timestamp:
28/05/05 11:07:06 (4 years ago)
Author:
rollercow
Message:

More tidying,
blog listings done the right way (look no last updated feild!)
Everything in blog.lib now checks it has data to grab from the database before trying and generate an error of failing

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • admin.lib.php

    r59 r60  
    300300                echo "</div>\n"; 
    301301        } 
     302        //counts the number of comments  
     303        function commentCount($entry) { 
     304                $sql = db_query("SELECT count(id) from comments where post = ".$entry.";");              
     305                $sqlRow = db_getrow($sql); 
     306                return $sqlRow['count']; 
     307        } 
     308 
    302309} 
  • blog.lib.php

    r59 r60  
    114114        function printEntryAndComments($id)  
    115115        { 
    116                 $id  = $this->makeCleanString($id); 
    117116                $sql = db_query("SELECT id, category, subject, body, timestamp from entries where shortsubject='".$id."' and user_id = ".$this->id." LIMIT 1;"); 
    118117                $sqlNum = db_num_rows($sql); 
     
    124123                        $this->printEntry($sqlRow, false, false); 
    125124                        $this->printComments($sqlRow['id']); 
    126                         $this->printCommentForm($id); 
     125                        $this->printCommentForm($sqlRow['id']); 
    127126                } 
    128127        } 
     
    151150                echo "<p class=\"entryfoot\">[ " .strftime($this->longDateFormat,strtotime($row['timestamp'])). " ]</p>\n"; 
    152151                echo "</div>\n"; 
    153         } 
    154  
    155         //counts the number of comments  
    156         function commentCount($entry) { 
    157                 $sql = db_query("SELECT count(id) from comments where post = ".$entry.";");              
    158                 $sqlRow = db_getrow($sql); 
    159                 return $sqlRow['count']; 
    160152        } 
    161153         
     
    214206        function newComment($id)  
    215207        { 
    216                 $id  = $this->makeCleanString($id); 
    217  
     208                $id  = (int)$id; 
    218209                $author = ""; 
    219210                $email = ""; 
     
    248239                } 
    249240                //check the post exists, and is part of this blog 
    250                 $sql = db_query("SELECT subject,id from entries where user_id = ".$this->id." and shortsubject = '".$id."';"); 
    251                 $sqlNum = db_num_rows($sql); 
    252                 $row = db_getrow($sql); 
    253                 $entry_id = $row['id']; 
    254                 if ($sqlNum != 1 || $entry_id<=0) { //wtf? 
     241                $sql = db_query("SELECT subject from entries where user_id = ".$this->id." and id = '".$id."';"); 
     242                $sqlNum = db_num_rows($sql); 
     243                if ($sqlNum != 1) { 
    255244                        $this->commenterror = "Invalid blog entry, This entry may have been removed..?"; 
    256245                } 
    257246                //if no errors have been raised so far commit to the db 
    258247                if ($this->commentError == "") { 
    259                         $query = "INSERT INTO comments (post, name, email, body, host) VALUES ('{$entry_id}','{$author}','{$email}','{$comment}','{$host}')"; 
     248                        $query = "INSERT INTO comments (post, name, email, body, host) VALUES ('{$id}','{$author}','{$email}','{$comment}','{$host}')"; 
    260249                        if(!db_query($query)) { 
    261250                                error(2,"Database commit failed -".db_error()); 
    262251                        } 
    263252                } 
    264                 $this->printEntryAndComments($id); 
    265         } 
    266          
     253                $this->printEntryAndComments($this->IDToShortSubject($id)); 
     254        } 
     255         
     256        //reutrns the id of the message assosiated with a short subject 
    267257        function shortSubjectToID($shortsubject) 
    268258        { 
    269                 $sql = db_getrow(db_query("SELECT id from entries where user_id = ".$this->id." and shortsubject = '".$shortsubject."';")); 
    270                 return $sql['id']; 
    271         } 
    272          
     259                $sql = db_query("SELECT id from entries where user_id = ".$this->id." and shortsubject = '".$shortsubject."';"); 
     260                $sqlNum = db_num_rows($sql); 
     261                if ($sqlNum == 1) { 
     262                        $sqlRow = db_getrow($sql); 
     263                        return $sqlRow['id']; 
     264                } 
     265                else { 
     266                        error(3,"No such post"); 
     267                } 
     268        } 
     269         
     270        //reutrns the short subject of the message given message  
     271        function IDToShortSubject($id) 
     272        { 
     273                $sql = db_query("SELECT shortsubject from entries where user_id = ".$this->id." and id = '".$id."';"); 
     274                $sqlNum = db_num_rows($sql); 
     275                if ($sqlNum == 1) { 
     276                        $sqlRow = db_getrow($sql); 
     277                        return $sqlRow['shortsubject']; 
     278                } 
     279                else { 
     280                        error(3,"No such post"); 
     281                } 
     282        }        
    273283} 
    274284 
    275285// a pseudo-class to list all blog users 
    276286class bloglist { 
    277     var $title; 
    278     var $description; 
    279     var $httpPath; 
    280     var $cssFile; 
    281      
    282     // don't do anything apart from setting up default variables 
    283     function bloglist()  
    284     { 
    285         $this->title = "Bloggers"; 
    286         $this->description = "Listing of all users"; 
    287         $this->httpPath = dirname($_SERVER['SCRIPT_NAME'])."/"; 
    288         $this->cssFile = "blog.css"; 
    289     } 
    290  
    291     // print a nice list of blog users and when they last updated 
    292     function listBlogs() 
    293     { 
    294         $result = db_query("SELECT id, username, name, title, description FROM users ORDER BY username ASC;"); 
    295         $baseurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."/"; 
    296  
    297         while($row = db_getrow($result)){ 
    298             $r = db_getrow(db_query("SELECT timestamp FROM entries WHERE user_id=".$row['id'])); 
    299             echo "<p class=\"bloglist\">".$row['name'].": <a href=\"".$baseurl.$row['username']."\" title=\"".$row['description']."\">".$row['title']."</a> (last updated ".timeDiff(strtotime($r['timestamp']))." ago)</p>\n"; 
    300         } 
    301     } 
     287        var $title; 
     288        var $description; 
     289        var $httpPath; 
     290        var $cssFile; 
     291 
     292        // don't do anything apart from setting up default variables 
     293        function bloglist()  
     294        { 
     295                $this->title = "Bloggers"; 
     296                $this->description = "Listing of all users"; 
     297                $this->httpPath = dirname($_SERVER['SCRIPT_NAME'])."/"; 
     298                $this->cssFile = "blog.css"; 
     299        } 
     300 
     301        // print a nice list of blog users and when they last updated 
     302        function listBlogs() 
     303        { 
     304                $baseurl = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."/"; 
     305                $sql = db_query("SELECT max(entries.timestamp) as timestamp, users.username, users.name, users.title from entries left join users ON entries.user_id = users.id group by users.username, users.name, users.title;"); 
     306                $sqlNum = db_num_rows($sql); 
     307                if ($sqlNum > 0) { 
     308                        while($row = db_getrow($sql)) { 
     309                                echo "<p class=\"bloglist\">".$row['name'].": <a href=\"".$baseurl.$row['username']."\" title=\"".$row['description']."\">".$row['title']."</a> (last updated ".$this->timeDiff(strtotime($row['timestamp']))." ago)</p>\n"; 
     310                        } 
     311                } 
     312                else { 
     313                        error(3,"No blogs"); 
     314                } 
     315        } 
     316        //reutuns a textrual diff between two time stamps 
     317        function timeDiff($first, $second=0) 
     318        { 
     319                if($second == 0) { 
     320                        $second = time(); 
     321                } 
     322 
     323                $diff = max($first, $second) - min($first, $second); 
     324 
     325                if($diff>604800) { 
     326                        $ret = round($diff/604800); 
     327                        return $ret.(($ret>1)? " weeks" : " week"); 
     328                }  
     329                elseif($diff>86400) { 
     330                        $ret = round($diff/86400); 
     331                        return $ret.(($ret>1)? " days" : " day"); 
     332                }  
     333                elseif($diff>3600) { 
     334                        $ret = round($diff/3600); 
     335                        return $ret.(($ret>1)? " hours" : " hour"); 
     336                }  
     337                else { 
     338                        $ret = round($diff/60); 
     339                        return $ret.(($ret>1)? " minutes" : " minute"); 
     340                } 
     341        } 
    302342} 
    303  
    304 /** pretty print the difference between two times (passed in as UNIX timestamps) 
    305  *  order of arguments isn't important, and second defaults to current time if not specified */ 
    306 function timeDiff($first, $second=0) 
    307 { 
    308     if($second == 0) { 
    309         $second = time(); 
    310     } 
    311  
    312     $diff = max($first, $second) - min($first, $second); 
    313  
    314     if($diff>604800) { 
    315         $ret = round($diff/604800); 
    316         return $ret.(($ret>1)? " weeks" : " week"); 
    317     } elseif($diff>86400) { 
    318         $ret = round($diff/86400); 
    319         return $ret.(($ret>1)? " days" : " day"); 
    320     } elseif($diff>3600) { 
    321         $ret = round($diff/3600); 
    322         return $ret.(($ret>1)? " hours" : " hour"); 
    323     } else { 
    324         $ret = round($diff/60); 
    325         return $ret.(($ret>1)? " minutes" : " minute"); 
    326     } 
    327 } 
  • index.php

    r56 r60  
    4242                        break; 
    4343                case "postcomment": 
    44                         $blog->newComment(array_shift($request)) ;  
     44                        $blog->newComment((int)array_shift($request)) ;  
    4545                        break; 
    4646                case "list":