ermission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Yes, most of the formatting used in this file is HORRIBLY BAD STYLE. However,
most of the action happens outside of this file, and I really wanted the code
to look as small as what it does. Basically. Oh, I just suck. :)
*/
// do not change this line, you fool. In fact, don't change anything! Ever!
define("WAKKA_VERSION", "0.1.1");
define("WIKINI_VERSION", "0.5.0");
require 'includes/constants.php';
include 'includes/urlutils.inc.php';
// start the compute time
list($g_usec, $g_sec) = explode(" ",microtime());
define ("t_start", (float)$g_usec + (float)$g_sec);
$t_SQL=0;
class Wiki
{
var $dblink;
var $page;
var $tag;
var $parameter = array();
var $queryLog = array();
var $interWiki = array();
var $VERSION;
var $CookiePath = '/';
var $inclusions = array();
/**
* an array containing all the actions that are implemented by an object
* @access private
*/
var $actionObjects;
// LinkTrackink
var $isTrackingLinks = false;
var $linktable = array();
var $pageCache = array();
var $_groupsCache = array();
var $_actionsAclsCache = array();
// constructor
function Wiki($config)
{
$this->config = $config;
// some host do not allow mysql_pconnect
$this->dblink = @mysql_connect (
$this->config["mysql_host"],
$this->config["mysql_user"],
$this->config["mysql_passworme);
}
return $result;
}
function LoadSingle($query) {
if ($data = $this->LoadAll($query)) return $data[0];
return null;
}
function LoadAll($query)
{
$data=array();
if ($r = $this->Query($query))
{
while ($row = mysql_fetch_assoc($r)) $data[] = $row;
mysql_free_result($r);
}
return $data;
}
// MISC
function GetMicroTime()
{
list($usec, $sec) = explode(" ",microtime()); return ((float)$usec + (float)$sec);
}
function IncludeBuffered($filename, $notfoundText = "", $vars = "", $path = "")
{
if ($path) $dirs = explode(":", $path);
else $dirs = array("");
foreach($dirs as $dir)
{
if ($dir) $dir .= "/";
$fullfilename = $dir.$filename;
if (file_exists($fullfilename))
{
if (is_array($vars)) extract($vars);
ob_start();
include($fullfilename);
$output = ob_get_contents();
ob_end_clean();
return $output;
}
}
if ($notfoundText) return $notfoundText;
else return false;
}
// VARIABLES
function GetPageTag() { return $this->tag; }
function GetPageTime() { return $this->page["time"]; }
function GetMethod() { return $this->method; }
function GetConfigValue($name) { return isset($this->config[$name]) ? trim($this->config[$name]) : ''; }
function GetWakkaName() { return $this->GetConfigValue("wakka_name"); }
function GetWakkaVersion() { return $this->VERSION; }
function GetWikiNiVersion() { return WIKINI_VERSION; }
/**
* Retrieves all the triples that match some criteria.
* This allows to search triples by their approximate resource or property names.
* The allowed operators are the sql LIKE and the sql =
* @param string $resource The resource of the triples
* @param string $property The property of the triple to retrieve or null
* @param string $res_op The operator of comparison between the effective resource and $resource (default: 'LIKE')
* @param string $prop_op The operator of comparison between the effective property and $property (default: '=')
* @return array The list of all the triples that match the asked criteria
*/
function GetMatchingTriples($resource, $property = null, $res_op = 'LIKE', $prop_op = '=')
{
static $operators = array('=', 'LIKE'); // we might want to add other operators later
$res_op = strtoupper($res_op);
if (!in_array($res_op, $operators)) $res_op = '=';
$sql = 'SELECT * FROM ' . $this->GetConfigValue('table_prefix') . 'triples '
. 'WHERE resource ' . $res_op . ' "' . addslashes($resource) . '"';
if ($property !== null)
{
$prop_op = strtoupper($prop_op);
if (!in_array($prop_op, $operators)) $prop_op = '=';
$sql .= ' AND property ' . $prop_op . ' "' . addslashes($property) . '"';
}
return $this->LoadAll($sql);
}
/**
* Retrieves all the values for a given couple (resource, property)
* @param string $resource The resource of the triples
* @param string $property The property of the triple to retrieve
* @param string $re_prefix The prefix to add to $resource (defaults to THISWIKI_PREFIX)
* @param string $prop_prefix The prefix to add to $property (defaults to WIKINI_VOC_PREFIX)
* @return array An array of the retrieved values, in the form
* array(
* 0 => array(id = 7 , 'value' => $value1),
* 1 => array(id = 34, 'value' => $value2),
* ...
* )
*/
function GetAllTriplesValues($resource, $property, $re_prefix = THISWIKI_PREFIX, $prop_prefix = WIKINI_VOC_PREFIX)
{
$sql = 'SELECT id, value FROM ' . $this->GetConfigValue('table_prefix') . 'triples '
. 'WHERE resource = "' . addslashes($re_prefix . $resource) . '" '
. 'AND property = "' . addslashes($prop_prefix . $property) . '" ';
return $this->LoadAll($sql);
}
/**
* Retrieves a single value for a given couple (resource, property)
* @param string $resource The resource of the triples
* @param string $property The property of the triple to retrieve
* @param string $re_prefix The prefix to add to $resource (defaults to THISWIKI_PREFIX)
* @param string $prop_prefix The prefix to add to $property (defaults to WIKINI_VOC_PREFIX)
* @return string The value corresponding to ($resource, $property) or null if
* there is no such couple in the triples table.
*/
function GetTripleValue($resource, $property, $re_prefix = THISWIKI_PREFIX, $prop_prefix = WIKINI_VOC_PREFIX)
{
$res = $this->GetAllTriplesValues($resource, $property, $re_prefix, $prop_prefix);
if ($res) return $res[0]['value'];
return null;
}
/**
* Checks whether a triple exists or not
* @param string $resource The resource of the triple to find
* @param string $property The property of the triple to find
* @param string $value The value of the triple to find
* @param string $re_prefix The prefix to add to $resource (defaults to THISWIKI_PREFIX)
* @param string $prop_prefix The prefix to add to $property (defaults to WIKINI_VOC_PREFIX)
* @param int The id of the found triple or 0 if there is no such triple.
*/
function TripleExists($resource, $property, $value, $re_prefix = THISWIKI_PREFIX, $prop_prefix = WIKINI_VOC_PREFIX)
{
$sql = 'SELECT id FROM ' . $this->GetConfigValue('table_prefix') . 'triples '
. 'WHERE resource = "' . addslashes($re_prefix . $resource) . '" '
. 'AND property = "' . addslashes($prop_prefix . $property) . '" '
. 'AND value = "' . addslashes($value) . '"';
$res = $this->LoadSingle($sql);
if (!$res) return 0;
return $res['id'];
}
/**
* Inserts a new triple ($resource, $property, $value) in the triples' table
* @param string $resource The resource of the triple to insert
* @param string $property The property of the triple to insert
* @param string $value The value of the triple to insert
* @param string $re_prefix The prefix to add to $resource (defaults to THISWIKI_PREFIX)
* @param string $prop_prefix The prefix to add to $property (defaults to WIKINI_VOC_PREFIX)
* @return int An error code: 0 (success), 1 (failure) or 3 (already exists)
*/
function InsertTriple($resource, $property, $value, $re_prefix = THISWIKI_PREFIX, $prop_prefix = WIKINI_VOC_PREFIX)
{
if ($this->Trinot exist)
* or 3 ($resource, $property, $newvalue already exists)
*/
function UpdateTriple($resource, $property, $oldvalue, $newvalue, $re_prefix = THISWIKI_PREFIX, $prop_prefix = WIKINI_VOC_PREFIX)
{
$id = $this->TripleExists($resource, $property, $oldvalue, $re_prefix, $prop_prefix);
if (!$id) return 2;
if ($this->TripleExists($resource, $property, $newvalue, $re_prefix, $prop_prefix))
{
return 3;
}
$sql = 'UPDATE ' . $this->GetConfigValue('table_prefix') . 'triples '
. 'SET value = "' . addslashes($newvalue) . '" '
. 'WHERE id = ' . $id;
return $this->Query($sql) ? 0 : 1;
}
/**
* Deletes a triple ($resource, $property, $value) from the triples' table
* @param string $resource The resource of the triple to delete
* @param string $property The property of the triple to delete
* @param string $value The value of the triple to delete. If set to null,
* deletes all the triples corresponding to ($resource, $property). (defaults to null)
* @param stECT * FROM ".$this->config["table_prefix"]."pages"
. " WHERE tag = '".mysql_escape_string($tag)."' AND "
. ($time ? "time = '".mysql_escape_string($time)."'" : "latest = 'Y'") . " LIMIT 1";
$page = $this->LoadSingle($sql);
// cache result
if (!$time) $this->CachePage($page, $tag);
}
return $page;
}
/**
* Retrieves the cached version of a page.
*
* Notice that this method null or false, use
* $this->GetCachedPage($tag) === false
* to check if a page is not in the cache.
* @return mixed The cached version of a page:
* - the page DB line if the page exists and is in cache
* - null if the cache knows that the page does not exists
* - false is the cache does not know the page
*/
function GetCachedPage($tag) {return (array_key_exists($tag, $this->pageCache) ? $this->pageCache[$tag] : false); }
/**
* Caches a page's DB line.
*
* @param array $page The page (full) DB line or null if the page does not exists
* @param string $pageTag The tag of the page to cache. Defaults to $page['tag'] but is mendatory when $page === null
*/
function CachePage($page, $pageTag = null) {
if ($pageTag === null)
{
$pageTag = $page["tag"];
}
$this->pageCache[$pageTag] = $page;
}
function SetPage($page) { $this->page = $page; if ($this->page["tag"]) $this->tag = $this->page["tag"]; }
function LoadPageById($id) { return $this->LoadSingle("select * from ".$this->config["table_prefix"]."pages where id = '".mysql_escape_string($id)."' limit 1"); }
function LoadRevisions($page) { return $this->LoadAll("select * from ".$this->config["table_prefix"]."pages where tag = '".mysql_escape_string($page)."' order by time desc"); }
function LoadPagesLinkingTo($tag) { return $this->LoadAll("select from_tag as tag from ".$this->config["table_prefix"]."links where to_tag = '".mysql_escape_string($tag)."' order by tag"); }
function LoadRecentlyChanged($limit=50)
{
$limit= (int) $limit;
if ($pages = $this->LoadAll("select id, tag, time, user, owner from ".$this->config["table_prefix"]."pages where latest = 'Y' and comment_on = '' order by time desc limit $limit"))
{
foreach ($pages as $page)
{
$this->CachePage($page);
}
return $pages;
}
}
function LoadAllPages() { return $this->LoadAll("select * from ".$this->config["table_prefix"]."pages where latest = 'Y' order by tag"); }
function FullTextSearch($phrase) { return $this->LoadAll("select * from ".$this->config["table_prefix"]."pages where latest = 'Y' and match(tag, body) against('".mysql_escape_string($phrase)."')"); }
function LoadWantedPages() {
$p = $this->config["table_prefix"];
$r = "SELECT ${p}links.to_tag AS tag, COUNT(${p}links.from_tag) AS count "
. "FROM ${p}links LEFT JOIN ${p}pages ON ${p}links.to_tag = ${p}pages.tag "
. "WHERE ${p}pages.tag IS NULL GROUP BY ${p}links.to_tag ORDER BY count DESC, tag ASC";
return $this->LoadAll($r);
}
function LoadOrphanedPages() { return $this->LoadAll("select distinct tag from ".$this->config["table_prefix"]."pages as p left join ".$this->config["tablthis->GetConfigValue("default_comment_acl")));
// current user is owner; if user is logged in! otherwise, no owner.
if ($this->GetUser()) $owner = $user;
else $owner = '';
}
else
{
// aha! page isn't new. keep owner!
$owner = $oldPage["owner"];
// ...and comment_on, eventualy?
if ($comment_on == '') $comment_on = $oldPage['comment_on'];
}
// set all other revisions to old
$this->Query("update ".$this->config["table_prefix"]."pages set latest = 'N' where tag = '".mysql_Escape_string($tag)."'");
// add new revision
$this->Query("insert into ".$this->config["table_prefix"]."pages set ".
"tag = '".mysql_escape_string($tag)."', ".
($comment_on ? "comment_on = '".mysql_escape_string($comment_on)."', " : "").
"time = now(), ".
"owner = '".mysql_escape_string($owner)."', ".
"user = '".mysql_escape_string($user)."', ".
"latest = 'Y', ".
"body = '".mysql_escape_string(chop($body))."'");
unset($this->pageCache[$tag]);
return 0;
$order = array("\r\n", "\n", "\r");
$replace = '\\n';
$content = str_replace($order, $replace, $content);
$contentToAppend = "\n" . date("Y-m-d H:i:s") . " . . . . " . $user . " . . . . " . $content . "\n";
$page = $page ? $page : "LogDesActionsAdministratives" . date("Ymd");
return $this->AppendContentToPage($contentToAppend, $page, true);
}
/**
* Make the purge of page versions that are older than the last version older than 3 "pages_purge_time"
* This method permits to allways keep a version that is older than that period.
*/
function PurgePages() {
if ($days = $this->GetConfigValue("pages_purge_time")) { // is purge active ?
// let's search which pages versions we have to remove
// this is necessary beacause even MySQL does not handel multi-tables deletes before version 4.0
$wnPages = $this->GetConfigValue('table_prefix') . 'pages';
$sql = 'SELECT DISTINCT a.id FROM ' . $wnPages . ' a,' . $wnPages . ' b WHERE a.latest = \'N\' AND a.time < date_sub(now(), INTERVAL \'' . addslashes($days) . '\' DAY) AND a.tag = b.tag AND a.time < b.time AND b.time < date_sub(now(), INTERVAL \'' . addslashes($days) . '\' DAY)';
$ids = $this->LoadAll($sql);
if (count($ids)) { // there are some versions to remove from DB
// let's build one big request, that's better...
$sql = 'DELETE FROM ' . $wnPages . ' WHERE id IN (';
foreach($ids as $key => $line){
$sql .= ($key ? ', ':'') . $line['id']; // NB.: id is an int, no need of quotes
}
$sql .= ')';
// ... and send it !
$this->Query($sql);
}
}
}
// COOKIES
function SetSessionCookie($name, $value)
{
SetCookie($name, $value, 0, $this->CookiePath);
$_COOKIE[$name] = $value;
}
function SetPersistentCookie($name, $value, $remember = 0)
{
SetCookie($name, $value, time() + ($remember ? 90*24*60*60 : 60 * 60), $this->CookiePath);
$_COOKIE[$name] = $value;
}
function DeleteCookie($name)
{
SetCookie($name, "", 1, $this->CookiePath);
$_COOKIE[$name] = "";
}
function GetCookie($name) { return $_COOKIE[$name]; }
// HTTP/REQUEST/LINK RELATED
function SetMessage($message) { $_SESSION["message"] = $message; }
function GetMessage()
{
if (isset($_SESSION["message"])) $message = $_SESSION["message"];
else $message = "";
$_SESSION["message"] = "";
return $message;
}
function Redirect($url)
{
header("Location: $url");
exit;
}
// returns just PageName[/method].
function MiniHref($method = "", $tag = "")
{
if (!$tag = trim($tag)) $tag = $this->tag;
return $tag.($method ? "/".$method : "");
}
// returns the full url to a page/method.
function Href($method = "", $tag = "", $params = "", $htmlspchars = true)
{
$href = $this->config["base_url"].$this->MiniHref($method, $tag);
if ($params)
{
$href .= ($this->config["rewrite_mode"] ? "?" : ($htmlspchars ? "&" : '&')).$params;
}
return $href;
}
function Link($tag, $method = "", $text = "", $track = 1)
{
$displayText = $text ? $text : $tag;
// is this an interwiki link?
if (preg_match('/^' . WN_INTERWIKI_CAPTURE . '$/', $tag, $matches))
{
if ($tagInterWiki = $this->GetInterWikiUrl($matches[1], $matches[2])) {
return ''
.htmlspecialchars($displayText).' (interwiki)';
}
else return ''
.htmlspecialchars($displayText).' (interwiki inconnu)';
}
// is this a full link? ie, does it contain non alpha-numeric characters?
// Note : [:alnum:] is equivalent [0-9A-Za-z]
// [^[:alnum:]] means : some caracters other than [0-9A-Za-z]
// For example : "www.adress.com", "mailto:adress@domain.com", "http://www.adress.com"
else if (preg_match("/[^[:alnum:]]/", $tag))
{
// check for various modifications to perform on $tag
if (preg_match("/^[\w.-]+\@[\w.-]+$/", $tag))
{ // email addresses
$tag = 'mailto:'.$tag;
}
// Note : in Perl regexp, (?: ... ) is a non-catching cluster
else if (preg_match('/^[[:alnum:]][[:alnum:].-]*(?:\/|$)/', $tag))
{ // protocol-less URLs
$tag = 'http://'.$tag;
}
// Finally, block script schemes (see RFC 3986 about
// schemes) and allow relative link & protocol-full URLs
else if (preg_match('/^[a-z0-9.+-]*script[a-z0-9.+-]*:/i', $tag)
|| !(preg_match('/^\.?\.?\//', $tag)
|| preg_match('/^[a-z0-9.+-]+:\/\//i', $tag)))
{
// If does't fit, we can't qualify $tag as an URL.
// There is a high risk that $tag is just XSS (bad
// javascript: code) or anything nasty. So we must not
// produce any link at all.
return htmlspecialchars($tag.($text ? ' '.$text : ''));
}
// Important: Here, we know that $tag is not something bad
// and that we must produce a link with it
// An inline image? (text!=tag and url ends by png,gif,jpeg)
if ($text and preg_match("/\.(gif|jpeg|png|jpg)$/i",$tag))
{
return '';
}
else
{
// Even if we know $tag is harmless, we MUST encode it
// in HTML with htmlspecialchars() before echoing it.
// This is not about being paranoiac. This is about
// being compliant to the HTML standard.
return ''
.htmlspecialchars($displayText).'';
}
}
else
{
// it's a Wiki link!
if (!empty($track)) $this->TrackLinkTo($tag);
if ($this->LoadPage($tag))
return ''
.htmlspecialchars($displayText).'';
else
return ''.htmlspecialchars($displayText)
.'?';
}
}
function ComposeLinkToPage($tag, $method = "", $text = "", $track = 1) {
if (!$text) $text = $tag;
$text = htmlspecialchars($text);
if ($track)
$this->TrackLinkTo($tag);
return ''.$text.'';
}
function IsWikiName($text) {
return preg_match('/^' . WN_CAMEL_CASE . '$/', $text);
}
// LinkTracking management
/**
* Tracks the link to a given page (only if the LinkTracking is activated)
* @param string $tag The tag (name) of the page to track a link to.
*/
function TrackLinkTo($tag) {
if ($this->LinkTracking()) $this->linktable[] = $tag;
}
/**
* @return array The current link tracking table
*/
function GetLinkTable() { return $this->linktable; }
/**
* Clears the link tracking table
*/
function ClearLinkTable() { $this->linktable = array(); }
/**
* Starts the LinkTracking
* @return bool The previous state of the link tracking
*/
function StartLinkTracking() {
return $this->LinkTracking(true);
}
/**
* Stops the LinkTracking
* @return bool The previous state of the link tracking
*/
function StopLinkTracking() {
return $this->LinkTracking(false);
}
/**
* Sets and/or retrieve the state of the LinkTracking
* @param bool $newStatus The new status of the LinkTracking
* (defaults to null which lets it unchanged)
* @return bool The previous state of the link tracking
*/
function LinkTracking($newStatus = null)
{
$old = $this->isTrackingLinks;
if ($newStatus !== null) $this->isTrackingLinks = $newStatus;
return $old;
}
function WriteLinkTable() {
// delete old link table
$this->Query("delete from ".$this->config["table_prefix"]."links where from_tag = '".mysql_escape_string($this->GetPageTag())."'");
if ($linktable = $this->GetLinkTable())
{
$from_tag = mysql_escape_string($this->GetPageTag());
foreach ($linktable as $to_tag)
{
$lower_to_tag = strtolower($to_tag);
if (!isset($written[$lower_to_tag]))
{
$this->Query("insert into ".$this->config["table_prefix"]."links set from_tag = '".$from_tag."', to_tag = '".mysql_escape_string($to_tag)."'");
$written[$lower_to_tag] = 1;
}
}
}
}
function Header() {
$action = $this->GetConfigValue("header_action");
if (($actionObj = &$this->GetActionObject($action)) && is_object($actionObj))
{
return $actionObj->GenerateHeader();
}
return $this->Action($action, 1);
}
function Footer() {
$action = $this->GetConfigValue("footer_action");
if (($actionObj = &$this->GetActionObject($action)) && is_object($actionObj))
{
return $actionObj->GenerateFooter();
}
return $this->Action($action, 1);
}
// FORMS
function FormOpen($method = "", $tag = "", $formMethod = "post") {
$result = "
true to allow it.
* @param array $vars An array of additionnal parameters to give to the action, in the form
* array( 'param' => 'value').
* This allows you to call Action() internally, setting $action to the name of the action
* you want to call and it's parameters in an array, wich is more efficient than
* the pattern-matching algorithm used to extract the parameters from $action.
* @return The output generated by the action.
*/
function Action($action, $forceLinkTracking = 0, $vars = array())
{
$cmd = trim($action);
// extract $action and $vars_temp ("raw" attributes)
if (!preg_match("/^([a-zA-Z-0-9]+)\/?(.*)$/", $cmd, $matches))
{
return 'Action invalide "' . htmlspecialchars($cmd) . '"';
}
list(, $action, $vars_temp) = $matches;
$vars[$vars_temp] = $vars_temp; // usefull for {{action/vars_temp}}
// now that we have the action's name, we can check if the user satisfies the ACLs
if (!$this->CheckModuleACL($action, 'action'))
{
return 'Erreur: vous n\'avez pas accès à l\'action ' . $action . '.';
}
// match all attributes (key and value)
// prepare an array for extract() to work with (in $this->IncludeBuffered())
if (preg_match_all("/([a-zA-Z0-9]*)=\"(.*)\"/U", $vars_temp, $matches))
{
for ($a = 0; $a < count($matches[1]); $a++)
{
$vars[$matches[1][$a]] = $matches[2][$a];
}
}
if (!$forceLinkTracking) $this->StopLinkTracking();
if ($actionObj = &$this->GetActionObject($action))
{
if (is_object($actionObj))
{
$result = $actionObj->PerformAction($vars, $cmd);
}
else // $actionObj is an error message
{
$result = $actionObj;
}
}
else // $actionObj == null (not found, no error message)
{
$this->parameter = &$vars;
$result = $this->IncludeBuffered(strtolower($action).".php", "Action inconnue "$action"", $vars, $this->config["action_path"]);
unset($this->parameter);
}
$this->StartLinkTracking(); // shouldn't we restore the previous status ?
return $result;
}
/**
* Finds the object corresponding to an action, if it exists.
* @param string $name The name of an action (should be alphanumeric)
* @return mixed
* - null if the corresponding file was not found or the corresponding class didn't exist after inclusion
* - an error string if the corresponding file was found but an error append while loading it
* - the object corresponding to this action if no problem happend
* To check the result, you should use is_object() on it.
* You should always assign the result of this method by referrence
* to avoid copying the object, which is the default beheviour in PHP4.
* @example
* $var = &$wiki->GetActionObject('actionname');
* if (is_object($var))
* {
* // normal behaviour
* }
* elseif ($var) // $var is not an object but an error string
* {
* // threat error
* }
* else // action was not found
* {
* // rescue from inexising action or sth
* }
*/
function &GetActionObject($name)
{
$name = strtolower($name);
$actionObj = null; // the generated object
if (!preg_match('/^[a-z0-9]+$/', $name)) // paranoiac
{
return $actionObj;
}
// already tried to load this object ? (may be null)
if (isset($this->actionObjects[$name]))
{
return $this->actionObjects[$name];
}
// object not loaded, try to load it
$filename = $name . '.class.php';
// load parent class for all action objects (only once)
require_once 'includes/action.class.php';
// include the action file, this should return an empty string
$result = $this->IncludeBuffered($filename, null, null, $this->GetConfigValue('action_path'));
if ($result) // the result was not an empty string, certainly an error message
{
$actionObj = $result;
}
elseif ($result !== false) // the result was empty but the file was found
{
$class = 'Action' . ucfirst($name);
if (class_exists($class))
{
$actionObj = &new $class($this);
if (!is_a($actionObj, 'WikiniAction'))
{
die("Action invalide '$name': classe incorrecte");
}
}
}
$this->actionObjects[$name] = &$actionObj;
return $actionObj;
}
/**
* Retrieves the list of existing actions
* @return array An unordered array of all the available actions.
*/
function GetActionsList()
{
$action_path = $this->GetConfigValue('action_path');
$list = array();
if ($dh = opendir($action_path))
{
while (($file = readdir($dh)) !== false)
{
if (preg_match('/^([a-zA-Z-0-9]+)(.class)?.php$/', $file, $matches))
{
$list[] = $matches[1];
}
}
}
return $list;
}
/**
* Retrieves the list of existing handlers
* @return array An unordered array of all the available handlers.
*/
function GetHandlersList()
{
$handler_path = $this->GetConfigValue('handler_path') . '/page/';
$list = array();
if ($dh = opendir($handler_path))
{
while (($file = readdir($dh)) !== false)
{
if (preg_match('/^([a-zA-Z-0-9]+)(.class)?.php$/', $file, $matches))
{
$list[] = $matches[1];
}
}
}
return $list;
}
function Method($method) {
if (!$handler = $this->page["handler"]) $handler = "page";
$methodLocation = $handler."/".$method.".php";
return $this->IncludeBuffered($methodLocation, "Méthode inconnue \"$methodLocation\"", "", $this->config["handler_path"]);
}
function Format($text, $formatter = "wakka") {
return $this->IncludeBuffered("formatters/".$formatter.".php", "Impossible de trouver le formateur \"$formatter\"", compact("text"));
}
// USERS
function LoadUser($name, $password = 0) { return $this->LoadSingle("select * from ".$this->config["table_prefix"]."users where name = '".mysql_escape_string($name)."' ".($password === 0 ? "" : "and password = '".mysql_escape_string($password)."'")." limit 1"); }
function LoadUsers() { return $this->LoadAll("select * from ".$this->config["table_prefix"]."users order by name"); }
function GetUserName() { if ($user = $this->GetUser()) $name = $user["name"]; else if (!$name = gethostbyaddr($_SERVER["REMOTE_ADDR"])) $name = $_SERVER["REMOTE_ADDR"]; return $name; }
function GetUser() { return (isset($_SESSION["user"]) ? $_SESSION["user"] : '');}
function SetUser($user, $remember=0) { $_SESSION["user"] = $user; $this->SetPersis/ all comment pages sorted by their first revision's (!) time. ugh!
// load ids of the first revisions of latest comments. err, huh?
if ($ids = $this->LoadAll("select min(id) as id from ".$this->config["table_prefix"]."pages where comment_on != '' group by tag order by id desc"))
{
// load complete comments
$num=0;
foreach ($ids as $id)
{
$comment = $this->LoadSingle("select * from ".$this->config["table_prefix"]."pages where id = '".$id["id"]."' limit 1");
if (!isset($comments[$comment["comment_on"]]) && $num < $limit)
{
$comments[$comment["comment_on"]] = $comment;
$num++;
}
}
// now load pages
if ($comments)
{
// now using these ids, load the actual pages
foreach ($comments as $comment)
{
$page = $this->LoadPage($comment["comment_on"]);
$page["comment_user"] = $comment["user"];
$page["comment_time"] = $comment["time"];
$page["comment_tag"] = $comment["tag"];
$pages[] = $page;
}
}
}
// load tags of pages
//return $this->LoadAll("select comment_on as tag, max(time) as time, tag as comment_tag, user from ".$this->config["table_prefix"]."pages where comment_on != '' group by comment_on order by time desc");
return $pages;
}
// ACCESS CONTROL
// returns true if logged in user is owner of current page, or page specified in $tag
function UserIsOwner($tag = "") {
// check if user is logged in
if (!$this->GetUser()) return false;
// set default tag
if (!$tag = trim($tag)) $tag = $this->GetPageTag();
// check if user is owner
if ($this->GetPageOwner($tag) == $this->GetUserName()) return true;
}
/**
* @param string $group The name of a group
* @return string the ACL associated with the group $gname
* @see UserIsInGroup to check if a user belongs to some group
*/
function GetGroupACL($group)
{
if (array_key_exists($group, $this->_groupsCache))
{
return $this->_groupsCache[$group];
}
return $this->_groupsCache[$group] = $this->GetTripleValue($group, WIKINI_VOC_ACLS, GROUP_PREFIX);
}
/**
* Checks if a new group acl is not defined recursively
* (this method expects that groups that are already defined are not themselves defined recursively...)
* @param string $gname The name of the group
* @param string $acl The new acl for that group
* @return boolean True iff the new acl defines the group recursively
*/
function MakesGroupRecursive($gname, $acl, $origin = null, $checked = array())
{
$gname = strtolower($gname);
if ($origin === null)
{
$origin = $gname;
}
elseif ($gname === $origin)
{
return true;
}
foreach (explode("\n", $acl) as $line)
{
if (!$line) continue;
if ($line[0] == '!')
{
$line = substr($line, 1);
}
if (!$line) continue;
if ($line[0] == '@')
{
$line = substr($line, 1);
if (!in_array($line, $checked))
{
if ($this->MakesGroupRecursive($line, $this->GetGroupACL($line), $origin, $checked))
{
return true;
}
}
}
}
$checked[] = $gname;
return false;
}
/**
* Sets a new ACL to a given group
* @param string $gname The name of a group
* @param string $acl The new ACL to associate with the group $gname
* @return int 0 if successful, a triple error code or a specific error code:
* 1000 if the new value would define the group recursively
* 1001 if $gname is not named with alphanumeric chars
* @see GetGroupACL
*/
function SetGroupACL($gname, $acl)
{
if (preg_match('/[^A-Za-z0-9]/', $gname))
{
return 1001;
}
$old = $this->GetGroupACL($gname);
if ($this->MakesGroupRecursive($gname, $acl))
{
return 1000;
}
$this->_groupsCache[$gname] = $acl;
if ($old === null)
{
return $this->InsertTriple($gname, WIKINI_VOC_ACLS, $acl, GROUP_PREFIX);
}
elseif ($old === $acl)
{
return 0; // nothing has changed
}
else
{
return $this->UpdateTriple($gname, WIKINI_VOC_ACLS, $old, $acl, GROUP_PREFIX);
}
}
/**
* @return array The list of all group names
*/
function GetGroupsList()
{
$res = $this->GetMatchingTriples(GROUP_PREFIX . '%', WIKINI_VOC_ACLS_URI);
$prefix_len = strlen(GROUP_PREFIX);
$list = array();
foreach ($res as $line)
{
$list[] = substr($line['resource'], $prefix_len);
}
return $list;
}
/**
* @param string $group The name of a group
* @return boolean true iff the user is in the given $group
*/
function UserIsInGroup($group, $user = null, $admincheck = true)
{
return $this->CheckACL($this->GetGroupACL($group), $user, $admincheck);
}
/**
* Checks if a given user is andministrator
* @param string $user The name of the user (defaults to the current user if not given)
* @return boolean true iff the user is an administrator
*/
function UserIsAdmin($user = null)
{
return $this->UserIsInGroup(ADMIN_GROUP, $user, false);
}
function GetPageOwner($tag = "", $time = "") { if (!$tag = trim($tag)) $tag = $this->GetPageTag(); if ($page = $this->LoadPage($tag, $time)) return $page["owner"]; }
function SetPageOwner($tag, $user) {
// check if user exists
if (!$this->LoadUser($user)) return;
// updated latest revision with new owner
$this->Query("update ".$this->config["table_prefix"]."pages set owner = '".mysql_escape_string($user)."' where tag = '".mysql_escape_string($tag)."' and latest = 'Y' limit 1");
}
function LoadAcl($tag, $privilege, $useDefaults = 1) {
if ((!$acl = $this->LoadSingle("select * from ".$this->config["table_prefix"]."acls where page_tag = '".mysql_escape_string($tag)."' and privilege = '".mysql_escape_string($privilege)."' limit 1")) && $useDefaults)
{
$acl = array("page_tag" => $tag, "privilege" => $privilege, "list" => $this->GetConfigValue("default_".$privilege."_acl"));
}
return $acl;
}
function SaveAcl($tag, $privilege, $list) {
if ($this->LoadAcl($tag, $privilege, 0)) $this->Query("update ".$this->config["table_prefix"]."acls set list = '".mysql_escape_string(trim(str_replace("\r", "", $list)))."' where page_tag = '".mysql_escape_string($tag)."' and privilege = '".mysql_escape_string($privilege)."' limit 1");
else $this->Query("insert into ".$this->config["table_prefix"]."acls set list = '".mysql_escape_string(trim(str_replace("\r", "", $list)))."', page_tag = '".mysql_escape_string($tag)."', privilege = '".mysql_escape_string($privilege)."'");
}
// returns true if $user (defaults to current user) has access to $privilege on $page_tag (defaults to current page)
function HasAccess($privilege, $tag = "", $user = "") {
// set defaults
if (!$tag = trim($tag)) $tag = $this->GetPageTag();
if (!$user)
{
// if current user is owner, return true. owner can do anything!
if ($this->UserIsOwner($tag)) return true;
$user = $this->GetUserName();
}
// TODO: we might want to check if a given $user (other than the current user)
// has access to a given page. If the $user is the owner of that page,
// this method might give a wrong result (because we can't check that)
// load acl
$acl = $this->LoadAcl($tag, $privilege);
// fine fine... now go through acl
return $this->CheckACL($acl["list"], $user);
}
/**
* Checks if some $user satisfies the given $acl
* @param string $acl The acl to check, in the same format than for pages ACL's
* @param string $user The name of the user that must satisfy the ACL. By default
* the current remote user.
* @return bool True if the $user satisfies the $acl, false otherwise
*/
function CheckACL($acl, $user = null, $admincheck = true)
{
if (!$user)
{
$user = $this->GetUserName();
}
if ($admincheck && $this->UserIsAdmin($user))
{
return true;
}
foreach (explode("\n", $acl) as $line)
{
$line = trim($line);
// check for inversion character "!"
if (preg_match("/^[!](.*)$/", $line, $matches))
{
$negate = 1;
$line = $matches[1];
}
else
{
$negate = 0;
}
// if there's still anything left... lines with just a "!" don't count!
if ($line)
{
switch ($line[0])
{
case "#": // comments
break;
case "*": // everyone
return !$negate;
case "+": // registered users
if (!$this->LoadUser($user))
{
return $negate;
}
else
{
return !$negate;
}
case '@': // groups
$gname = substr($line, 1);
// paranoiac: avoid line = '@'
if ($gname && $this->UserIsInGroup($gname, $user, false /* we have allready checked if user was an admin */))
{
return !$negate;
}
break;
default: // simple user entry
if ($line == $user)
{
return !$negate;
}
}
}
}
// tough luck.
return false;
}
/**
* Loads the module ACL for a certain module
* @param string $module The name of the module
* @param string $module_type The type of module: 'action' or 'handler'
* @return mixed The ACL for the given module or null if no such
* ACL was found (which should probably be interpreted as '*').
*/
function GetModuleACL($module, $module_type)
{
$module = strtolower($module);
switch ($module_type)
{
case 'action':
if (array_key_exists($module, $this->_actionsAclsCache))
{
$acl = $this->_actionsAclsCache[$module];
break;
}
$this->_actionsAclsCache[$module] = $acl = $this->GetTripleValue($module, WIKINI_VOC_ACLS, WIKINI_VOC_ACTIONS_PREFIX);
if ($acl === null)
{
$action = &$this->GetActionObject($module);
if (is_object($action))
{
return $this->_actionsAclsCache[$module] = $action->GetDefaultACL();
}
}
break;
case 'handler':
$acl = $this->GetTripleValue($module, WIKINI_VOC_ACLS, WIKINI_VOC_HANDLERS_PREFIX);
break;
default:
return null; // TODO error msg ?
}
return $acl === null ? '*' : $acl;
}
/**
* Sets the $acl for a given $module
* @param string $module The name of the module
* @param string $module_type The type of module ('action' or 'handler')
* @param string $acl The new ACL for that module
* @return 0 on success, > 0 on error (see InsertTriple and UpdateTriple)
*/
function SetModuleACL($module, $module_type, $acl)
{
$module = strtolower($module);
$voc_prefix = $module_type == 'action' ? WIKINI_VOC_ACTIONS_PREFIX : WIKINI_VOC_HANDLERS_PREFIX;
$old = $this->GetTripleValue($module, WIKINI_VOC_ACLS, $voc_prefix);
if ($module_type == 'action')
{
$this->_actionsAclsCache[$module] = $acl;
}
if ($old === null)
{
return $this->InsertTriple($module, WIKINI_VOC_ACLS, $acl, $voc_prefix);
}
elseif ($old === $acl)
{
return 0; // nothing has changed
}
else
{
return $this->UpdateTriple($module, WIKINI_VOC_ACLS, $old, $acl, $voc_prefix);
}
}
/**
* Checks if a $user satisfies the ACL to access a certain $module
* @param string $module The name of the module to access
* @param string $module_type The type of the module ('action' or 'handler')
* @param string $user The name of the user. By default
* the current remote user.
* @return bool True if the $user has access to the given $module, false otherwise.
*/
function CheckModuleACL($module, $module_type, $user = null)
{
$acl = $this->GetModuleACL($module, $module_type);
if ($acl === null) return true; // undefined ACL means everybody has access
return $this->CheckACL($acl, $user);
}
// MAINTENANCE
function Maintenance() {
// purge referrers
$this->PurgeReferrers();
// purge old page revisions
$this->PurgePages();
}
// THE BIG EVIL NASTY ONE!
function Run($tag, $method = "") {
if(!($this->GetMicroTime()%9)) $this->Maintenance();
$this->ReadInterWikiConfig();
// do our stuff!
if (!$this->method = trim($method)) $this->method = "show";
if (!$this->tag = trim($tag)) $this->Redirect($this->href("", $this->config["root_page"]));
if ((!$this->GetUser() && isset($_COOKIE["name"])) && ($user = $this->LoadUser($_COOKIE["name"], $_COOKIE["password"]))) $this->SetUser($user, $_COOKIE["remember"]);
$this->SetPage($this->LoadPage($tag, (isset($_REQUEST["time"]) ? $_REQUEST["time"] :'')));
$this->LogReferrer();
//correction pour un support plus facile de nouveaux handlers
if ($this->CheckModuleACL($this->method, 'handler'))
{
echo $this->Method($this->method);
}
else
{
ecikini",
"mysql_user" => "wikini",
"mysql_password" => '',
"table_prefix" => "wikini_",
"root_page" => "PagePrincipale",
"wakka_name" => "MonSiteWikiNi",
"base_url" => computeBaseURL($_rewrite_mode),
"rewrite_mode" => $_rewrite_mode,
'meta_keywords' => '',
'meta_description' => '',
"action_path" => "actions",
"handler_path" => "handlers",
"header_action" => "header",
"footer_action" => "footer",
"navigation_links" => "DerniersChangements :: DerniersCommentaires :: ParametresUtilisateur",
"referrers_purge_time" => 24,
"pages_purge_time" => 90,
"default_write_acl" => "*",
"default_read_acl" => "*",
"default_comment_acl" => "*",
"preview_before_save" => 0,
'allow_raw_html' => false);
unset($_rewrite_mode);
// load config
if (!$configfile = GetEnv("WAKKA_CONFIG")) $configfile = "wakka.config.php";
if (file_exists($configfile)) include($configfile);
$wakkaConfigLocation = $configfile;
$wakkaConfig = array_merge($wakkaDefaultConfig, $wakkaConfig);
// check for locking
if (file_exists("locked")) {
// read password from lockfile
$lines = file("locked");
$lockpw = trim($lines[0]);
// is authentification given?
if (isset($_SERVER["PHP_AUTH_USER"])) {
if (!(($_SERVER["PHP_AUTH_USER"] == "admin") && ($_SERVER["PHP_AUTH_PW"] == $lockpw))) {
$ask = 1;
}
} else {
$ask = 1;
}
if ($ask) {
header("WWW-Authenticate: Basic realm=\"".$wakkaConfig["wakka_name"]." Install/Upgrade Interface\"");
header("HTTP/1.0 401 Unauthorized");
echo "Ce site est en cours de mise à jour. Veuillez essayer plus tard." ;
exit;
}
}
// compare versions, start installer if necessary
if ($wakkaConfig["wakka_version"] && (!$wakkaConfig["wikini_version"])) { $wakkaConfig["wikini_version"]=$wakkaConfig["wakka_version"]; }
if (($wakkaConfig["wakka_version"] != WAKKA_VERSION) || ($wakkaConfig["wikini_version"] != WIKINI_VERSION)) {
// start installer
if (!isset($_REQUEST["installAction"]) OR !$installAction = trim($_REQUEST["installAction"])) $installAction = "default";
include("setup/header.php");
if (file_exists("setup/".$installAction.".php")) include("setup/".$installAction.".php"); else echo "Invalid action" ;
include("setup/footer.php");
exit;
}
// Check if the server is configured to automatically compress the output
if (!ini_get('zlib.output_compression') && !ini_get('zlib.output_handler'))
{
// Check if we can use ob_gzhandler (requires the zlib extension)
if (function_exists('ob_gzhandler'))
{
// let ob_gzhandler do the dirty job
// NB.: this must be done BEFORE session_start() when session.use_trans_sid is on
ob_start('ob_gzhandler');
}
// else lets do the dirty job by ourselves...
elseif (!empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') && function_exists('gzencode'))
{
ob_start ('gzencode');
// Tell the browser the content is compressed with gzip
header ("Content-Encoding: gzip");
}
}
// configuration du cookie de session
// determine le chemin pour les cookies
$a = parse_url($wakkaConfig['base_url'])te wiki object
$wiki = new Wiki($wakkaConfig);
// check for database access
if (!$wiki->dblink)
{
echo "Pour des raisons indépendantes de notre volonté, ". "le contenu de ce Wiki est temporairement inaccessible. Veuillez ". "réessayer ultérieurement, merci de votre ". "compréhension.
"; // Log error (useful to find the buggy server in a load balancing platform) trigger_error("WikiNi : DB connection failed"); exit; } // go! if (!isset($method)) $method=''; // Security (quick hack) : Check method syntax if (!(preg_match('#^[A-Za-z0-9_]*$#',$method))) { $method=''; } $wiki->Run($page, $method); ?>