actionURL == "" ) { $this->actionURL = $PHP_SELF; } $name = $this->ctClass; $this->ct = new $name; } /* public: construct valid inputs from GET or POST variables * requires track_vars = on */ function validateInputs() { global $HTTP_POST_VARS; global $HTTP_GET_VARS; $vars = count($HTTP_POST_VARS) > 0 ? $HTTP_POST_VARS : (count($HTTP_GET_VARS) > 0 ? $HTTP_GET_VARS : array() ); $cleanvars = array(); reset($vars); while( list($var, $value) = each($vars) ) { $cleanvars[$var] = $this->validateInput($value); } return $cleanvars; } /* private: sanitise a single variable */ function validateInput($var) { if (get_magic_quotes_gpc()) { $var = stripslashes($var); } $var = strip_tags($var); $var = htmlspecialchars($var); $var = str_replace("\n", " ", $var); $var = str_replace("\r", " ", $var); $var = trim($var); return $var; } /* public: renderPage() does the work of producing the required html page. * It produces body text only, which must be wrapped by other html to * form a complete page. The effect of renderPage() depends on the * variables passed to it. */ function renderPage( $vars ) { // $this->ct->checkDataVersion(); $this->htmlStr = ""; $this->ct->resetError(); /* handle database update actions */ if(isset($vars['action'])) { $this->handleAction($vars); /* if we have just deleted something, don't carry on and try to render it */ if(strstr($vars['action'],'delete')) { $this->renderTopic(); return $this->htmlStr; } } /* return an appropriate page body */ /* first, try the cache */ if (!$this->admin && $this->enableCache && $this->cacheRead(md5(serialize($vars)))) { return $this->htmlStr; } /* no cache hit (or cache not enabled) so render the page body */ if(isset($vars['TopicID'])) { if(isset($vars['sort'])) { $this->renderTopic($vars['TopicID'], $vars['sort']); } else { $this->renderTopic($vars['TopicID']); } } else if(isset($vars['RollID']) && isset($vars['FrameID'])) { $this->renderPicture($vars['RollID'], $vars['FrameID']); } else if(isset($vars['RollID'])) { $this->renderRoll($vars['RollID']); } else if(isset($vars['search'])) { $this->renderSearch($vars['search']); } else { /* default action is render top-level topics */ $this->renderTopic(); } /* save the page in the cache for next time */ if (!$this->admin && $this->enableCache) { $this->cacheWrite(md5(serialize($vars))); } return $this->htmlStr; } /* High-level functions to render page bodies */ /* private: render a page body for a given topic */ function renderTopic( $topicID = "/", $sort="" ) { $topic = $this->ct->getTopic($topicID); $this->showTopic($topic); $subTopics = $this->ct->getTopicsInParentTopic($topicID); $this->showTopics($subTopics); $pictures = $this->ct->getPicturesInTopic($topicID); if ($sort != "") { $this->showSortType($sort); $pictures = $this->sortPictures($pictures, $sort, $this->sortThreshold); } $this->showThumbs($pictures); if ( (count($pictures) > $this->sortThreshold) || $sort!="" ) $this->showSort($topicID); if ($this->admin) { $this->showFormUpdateTopic($topicID); if ($topicID == "/") { $this->showFormAddTopic(); $this->showRolls(); $this->showFormAddRoll(); } else { $this->showFormDeleteTopic($topicID); } } if ($topicID != "/") { $this->showNav($topicID); } $this->showFormSearch(); } /* private: render a page body for a given picture */ function renderPicture( $rollID, $frameID ) { if ($picture = $this->ct->getPicture($rollID, $frameID)) { $topic = $this->ct->getTopic($picture['ParentTopicID']); $this->showTopic($topic, false); $this->showPicture($picture); $comments = $this->ct->getCommentsInPicture($rollID, $frameID); $this->showNav($picture['ParentTopicID']); if ($this->enableNav) { $this->showNavigation($picture); } $this->showComments($comments); if ($this->enableComments) { $this->showFormAddComment($rollID, $frameID); } if ($this->enableRatings) { $this->showFormAddRating($rollID, $frameID, $picture['Rating']); } if ($this->admin) { $this->showFormUpdatePicture($rollID, $frameID); $this->showFormDeletePicture($rollID, $frameID); } // if conditions are appropriate, increment times a picture has been viewed if (!$this->admin && $this->enableViews) { global $REMOTE_ADDR; // where are they coming from? if ( !strlen($this->ownIPSubstring) || !strstr($REMOTE_ADDR, $this->ownIPSubstring) ) $this->ct->incrementPictureViews($rollID, $frameID, $picture); } } } /* private: render a page body for a given roll */ function renderRoll( $rollID ) { $this->showRoll($rollID); $pictures = $this->ct->getPicturesInRoll($rollID); $this->showThumbs($pictures); $this->showNav(); if ($this->admin) { // $this->showFormUpdateRoll($rollID); $this->showFormDeleteRoll($rollID); $this->showFormAddPicture($rollID); $this->showFormAddAllPictures($rollID); } } /* private: render a page body for a given search */ function renderSearch( $search ) { $this->showSearch($search); $pictures = $this->ct->getPicturesInSearch($search); $this->showThumbs($pictures); $this->showNav(); $this->showFormSearch(); } /* High-level function to handle database updates */ /* private: update the appropriate database table */ function handleAction($vars) { global $REMOTE_ADDR; if ($this->admin) { /* add, update and delete Topics */ if($vars['action'] == 'updateTopic') { if (!isset($vars['ParentTopicID'])) $vars['ParentTopicID'] = ''; $topic = array( 'TopicID' => $vars['TopicID'], 'ParentTopicID' => $vars['ParentTopicID'], 'Description' => $vars['Description'], 'Summary' => $vars['Summary']); $this->ct->updateTopic($topic); } else if($vars['action'] == 'addTopic') { $topic = array( 'TopicID' => $vars['TopicID'], 'ParentTopicID' => $vars['ParentTopicID'], 'Description' => $vars['Description'], 'Summary' => $vars['Summary']); $this->ct->addTopic($topic); } else if($vars['action'] == 'deleteTopic') { if (!$this->ct->deleteTopic($vars['TopicID'])) $this->show_error($this->ct->getError()); } /* add, update and delete Pictures */ else if($vars['action'] == 'updatePicture') { $picture = array( 'RollID' => $vars['RollID'], 'FrameID' => $vars['FrameID'], 'ParentTopicID' => $vars['ParentTopicID'], 'Description' => $vars['Description'] ); $this->ct->updatePicture($picture); } else if($vars['action'] == 'addPicture') { $picture = array( 'RollID' => $vars['RollID'], 'FrameID' => $vars['FrameID'], 'ParentTopicID' => $vars['ParentTopicID'], 'Description' => $vars['Description'], 'Views' => '0', 'Rating' => '0'); $this->ct->addPicture($picture); } else if($vars['action'] == 'addAllPictures') { $this->addAllPictures($vars['RollID'], $vars['ParentTopicID']); } else if($vars['action'] == 'deletePicture') { $this->ct->deletePicture($vars['RollID'], $vars['FrameID']); } /* add, update and delete Rolls */ else if($vars['action'] == 'updateRoll') { $roll = array( 'RollID' => $vars['RollID'] ); $this->ct->updateRoll($roll); } else if($vars['action'] == 'addRoll') { $roll = array( 'RollID' => $vars['RollID'] ); $this->ct->addRoll($roll); } else if($vars['action'] == 'deleteRoll') { if (!$this->ct->deleteRoll($vars['RollID'])) $this->show_error($this->ct->getError()); } /* delete Comments */ else if($vars['action'] == 'deleteComment') { $this->ct->deleteComment($vars['CommentID']); } } // endif admin enabled if ($this->enableComments) { /* add Comments */ if($vars['action'] == 'addComment') { if($vars['Comment'] != "") { $comment = array( 'CommentID' => uniqid(""), 'RollID' => $vars['RollID'], 'FrameID' => $vars['FrameID'], 'Comment' => $vars['Comment'], 'IP' => $REMOTE_ADDR); $this->ct->addComment($comment); } } } // endif comments enabled if ($this->enableRatings) { /* add Ratings */ if($vars['action'] == 'addRating') { if(($vars['Rating'] >= -2) && ($vars['Rating'] <= 2)) { $rating = array( 'RollID' => $vars['RollID'], 'FrameID' => $vars['FrameID'], 'Rating' => $vars['Rating'], 'IP' => $REMOTE_ADDR, 'RateTime' => time() ); $this->ct->addRating($rating); } } } // endif ratings enabled /* Action has potentially changed pages - clear the cache */ if ($this->enableCache) { $this->cacheClear(); } } /* private: addAllPictures() searches for and adds all pictures in a roll */ function addAllPictures( $rollID, $parentTopicID ) { if ($handle = opendir($this->basePath . $rollID)) { while ($file = readdir($handle)) { /* if not thumb */ if (!ereg('(.+)'.$this->thumb_suffix.'$', $file)) { /* if is picture */ if(ereg('(.+)'.$this->picture_suffix.'$', $file, $arr)) { /* add picture if not already present */ $frameID = $arr[1]; if (!$this->ct->getPicture($rollID, $frameID)) { $picture = array( 'RollID' => $rollID, 'FrameID' => $frameID, 'ParentTopicID' => $parentTopicID, 'Description' => '', 'Views' => '0', 'Rating' => '0' ); $this->ct->addPicture($picture); } } } } closedir($handle); } } /* private: sortPictures() delivers a sorted subset of pictures */ function sortPictures($pictures, $sort, $num) { if ($sort == 'views') usort($pictures, 'cmpViews' ); if ($sort == 'rating') usort($pictures, 'cmpRating' ); reset($pictures); $subset = array(); $count = 0; while ((list($key,$value) = each($pictures)) && $count < $num) { $count += 1; $subset[$key] = $value; } return $subset; } /* * Low-level functions to render individual page elements. * These are the only functions that actually generate any HTML. */ /* private: show a single topic title */ function showTopic($topic, $showSummary = true) { if(is_array($topic)) { $desc = $this->defaultText($topic['Description'], SLZ_STR_NO_DESC); $this->show("
".$topic['Summary'] . "
"); } } /* private: show a list of all the topics in $topics */ function showTopics( $topics ) { if(is_array($topics) && count($topics) > 0 ) { $this->show("actionURL . "\">" . $desc . ""); } else { $this->showNav($topic['ParentTopicID']); $this->show(" : actionURL . "?TopicID=" . urlencode($topic['TopicID']) . "\">" . $desc . ""); } } } /* private: show a navigation bar with hyperlinks for First | Prev | Next | Last */ function showNavigation ( $picture ) { $pictures = $this->ct->getPicturesInTopic($picture['ParentTopicID']); $currentPictureIndex = -1; /* spin through pictures in topic and get our index */ while (list ($key, $val) = each ($pictures)) { // PHP4 if ($val == $picture) $currentPictureIndex = $key; if (($val['RollID'] == $picture['RollID']) && ($val['FrameID'] == $picture['FrameID'])) { $currentPictureIndex = $key; } } $firstPictureIndex = 0; $lastPictureIndex = sizeof($pictures) - 1; $prevPictureIndex = $currentPictureIndex - 1; $nextPictureIndex = $currentPictureIndex + 1; $separator = " " . SLZ_STR_NAV_TEXT_SEPARATOR . " "; /* navBar is an HTML string with navigation links */ $navBar = "
" . $this->getNavigationLink(SLZ_STR_NAV_FIRST, ($firstPictureIndex == $currentPictureIndex) ? NULL : $pictures[$firstPictureIndex]); $navBar = $navBar . $separator; $navBar = $navBar . $this->getNavigationLink(SLZ_STR_NAV_PREVIOUS, ($prevPictureIndex >= 0) ? $pictures[$prevPictureIndex] : NULL); $navBar = $navBar . $separator; $navBar = $navBar . $this->getNavigationLink(SLZ_STR_NAV_NEXT, ($nextPictureIndex <= $lastPictureIndex) ? $pictures[$nextPictureIndex] : NULL); $navBar = $navBar . $separator; $navBar = $navBar . $this->getNavigationLink(SLZ_STR_NAV_LAST, ($lastPictureIndex == $currentPictureIndex) ? NULL : $pictures[$lastPictureIndex]); $navBar = $navBar . "
"; $this->show($navBar); } /* private: return a link to a picture, or plain text if no picture is specified */ function getNavigationLink ( $displayText, $picture ) { if (is_array($picture)) { $text = "actionURL . "?RollID=" . urlencode($picture['RollID']) . "&FrameID=" . urlencode($picture['FrameID']) . "\">" . $displayText . ""; } else { $text = $displayText; } return $text; } /* private: show a search result title */ function showSearch($search) { $this->show("".SLZ_STR_SEARCH_TERM.": " . $search . "
"); } /* private: show a single roll title */ function showRoll($rollID) { $this->show(""); while( list($key, $picture) = each($pictures)) { $this->showThumb($picture); } $this->show("
"); } } /* private: show thumbnail for a single picture */ function showThumb( $picture ) { if (is_array($picture) && count($picture) > 0) { /* make img tag with size and caption plus a link to the fullsize picture */ $fileName = $picture['RollID'] . "/" . $picture['FrameID'] . $this->thumb_suffix; if ($this->allPathsLower) $fileName = strtolower( $fileName ); $size = GetImageSize( $this->basePath . $fileName ); $description = $picture['Description']; if ($this->enableViews) $description .= ' [' . $picture['Views'] . ' ' . SLZ_STR_VIEWS . ']'; if ($this->enableRatings) $description .= ' ['.SLZ_STR_RATING.' ' . $this->ratingText($picture['Rating']) . ']'; $this->show("actionURL . "?RollID=" . urlencode($picture['RollID']) . "&FrameID=" . urlencode($picture['FrameID']) . "\">" . "baseURL . $fileName . "\" " . $size[3] . " alt=\"" . $description . "\">"); } } /* private: show a single full-size picture */ function showPicture( $picture ) { if (is_array($picture) && count($picture) > 0) { /* make img tag with size and caption */ $this->show("" .$picture['Description']. "
"); $fileName = $picture['RollID'] . "/" . $picture['FrameID'] . $this->picture_suffix; if ($this->allPathsLower) $fileName = strtolower( $fileName ); $size = GetImageSize( $this->basePath . $fileName ); $this->show("baseURL . $fileName . "\"" . $size[3] . " alt=\"" . $picture['Description'] . "\">
"); } } /* private: show all comments in $comments */ function showComments( $comments ) { if(is_array($comments) && count($comments) > 0) { reset($comments); while( list($key, $comment) = each($comments)) { $this->showComment($comment); } } } /* private: show a single comment */ function showComment( $comment ) { if (is_array($comment) && count($comment) > 0) { $this->show("".SLZ_STR_COMMENT.": "); // show IP of commenter if it exists, and we're in admin mode if ( ($this->admin || $this->showCommentIPs) && strlen($comment['IP']) ) $this->show("(". $comment['IP'] . ") "); $this->show($comment['Comment'] . "
"); if ($this->admin) { $this->showFormDeleteComment($comment['CommentID']); } } } /* private: show a form to update a topic */ function showFormUpdateTopic($topicID) { if ($topic = $this->ct->getTopic($topicID)) { $this->show("Error: ".$msg."
"); } /* private: write accumulated html to a cache file */ function cacheWrite($tag) { if ($fp = @fopen($this->cachePath . $tag . '.tmp', 'w')) { $comment = '' . $this->nl; fwrite($fp, $this->htmlStr . $comment); fclose($fp); } } /* private: read cache file to html string */ function cacheRead($tag) { if ($fp = @fopen($this->cachePath . $tag . '.tmp', 'r')) { $this->htmlStr .= fread($fp, 32768); fclose($fp); return TRUE; } return FALSE; } /* private: clear the cache */ function cacheClear() { if ($handle = opendir($this->cachePath)) { while ($file = readdir($handle)) { /* if is cache file */ if(ereg('.+\.tmp$', $file)) { unlink($this->cachePath . $file); // system('del "' . $this->cachePath . $file . '"'); /* Windows platforms */ } } closedir($handle); } } } /* end of class Slooze */ /* Private: cmpViews is a helper function for sortPictures */ /* It has to be outside Slooze class due to limitations of the usort function */ function cmpViews($a, $b) { if ($a['Views'] == $b['Views']) return 0; return ($a['Views'] > $b['Views']) ? -1 : 1; } /* Private: cmpRating is a helper function for sortPictures */ /* It has to be outside Slooze class due to limitations of the usort function */ function cmpRating($a, $b) { if ($a['Rating'] == $b['Rating']) return 0; return ($a['Rating'] > $b['Rating']) ? -1 : 1; } ?>