includes/pageutils.php
changeset 345 4ccdfeee9a11
parent 335 67bd3121a12e
child 351 8978cb3541ca
equal deleted inserted replaced
344:be6c5fdd9203 345:4ccdfeee9a11
    26     global $db, $session, $paths, $template, $plugins; // Common objects
    26     global $db, $session, $paths, $template, $plugins; // Common objects
    27     $name = str_replace('_', ' ', $name);
    27     $name = str_replace('_', ' ', $name);
    28     $q = $db->sql_query('SELECT username FROM ' . table_prefix.'users WHERE username=\'' . $db->escape(rawurldecode($name)) . '\'');
    28     $q = $db->sql_query('SELECT username FROM ' . table_prefix.'users WHERE username=\'' . $db->escape(rawurldecode($name)) . '\'');
    29     if ( !$q )
    29     if ( !$q )
    30     {
    30     {
    31       die(mysql_error());
    31       die($db->get_error());
    32     }
    32     }
    33     if ( $db->numrows() < 1)
    33     if ( $db->numrows() < 1)
    34     {
    34     {
    35       $db->free_result(); return('good');
    35       $db->free_result(); return('good');
    36     }
    36     }
   378     $message = RenderMan::preprocess_text($message, false, false);
   378     $message = RenderMan::preprocess_text($message, false, false);
   379     
   379     
   380     $msg = $db->escape($message);
   380     $msg = $db->escape($message);
   381     
   381     
   382     $minor = $minor ? ENANO_SQL_BOOLEAN_TRUE : ENANO_SQL_BOOLEAN_FALSE;
   382     $minor = $minor ? ENANO_SQL_BOOLEAN_TRUE : ENANO_SQL_BOOLEAN_FALSE;
   383     $q='INSERT INTO ' . table_prefix.'logs(log_type,action,time_id,date_string,page_id,namespace,page_text,char_tag,author,edit_summary,minor_edit) VALUES(\'page\', \'edit\', '.time().', \''.date('d M Y h:i a').'\', \'' . $paths->page_id . '\', \'' . $paths->namespace . '\', ' . ENANO_SQL_MULTISTRING_PRFIX . '\'' . $msg . '\', \'' . $uid . '\', \'' . $session->username . '\', \'' . $db->escape(htmlspecialchars($summary)) . '\', ' . $minor . ');';
   383     $q='INSERT INTO ' . table_prefix.'logs(log_type,action,time_id,date_string,page_id,namespace,page_text,char_tag,author,edit_summary,minor_edit) VALUES(\'page\', \'edit\', '.time().', \''.enano_date('d M Y h:i a').'\', \'' . $paths->page_id . '\', \'' . $paths->namespace . '\', ' . ENANO_SQL_MULTISTRING_PRFIX . '\'' . $msg . '\', \'' . $uid . '\', \'' . $session->username . '\', \'' . $db->escape(htmlspecialchars($summary)) . '\', ' . $minor . ');';
   384     if(!$db->sql_query($q)) $db->_die('The history (log) entry could not be inserted into the logs table.');
   384     if(!$db->sql_query($q)) $db->_die('The history (log) entry could not be inserted into the logs table.');
   385     
   385     
   386     $q = 'UPDATE ' . table_prefix.'page_text SET page_text=' . ENANO_SQL_MULTISTRING_PRFIX . '\'' . $msg . '\',char_tag=\'' . $uid . '\' WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';';
   386     $q = 'UPDATE ' . table_prefix.'page_text SET page_text=' . ENANO_SQL_MULTISTRING_PRFIX . '\'' . $msg . '\',char_tag=\'' . $uid . '\' WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';';
   387     $e = $db->sql_query($q);
   387     $e = $db->sql_query($q);
   388     if(!$e) $db->_die('Enano was unable to save the page contents. Your changes have been lost <tt>:\'(</tt>.');
   388     if(!$e) $db->_die('Enano was unable to save the page contents. Your changes have been lost <tt>:\'(</tt>.');
   470     
   470     
   471     $paths->add_page($page_data);
   471     $paths->add_page($page_data);
   472     
   472     
   473     $qa = $db->sql_query('INSERT INTO ' . table_prefix.'pages(name,urlname,namespace,visible,protected,delvote_ips) VALUES(\'' . $db->escape($name) . '\', \'' . $db->escape($page_id) . '\', \'' . $namespace . '\', '. ( $visible ? '1' : '0' ) .', ' . $prot . ', \'' . $db->escape(serialize($ips)) . '\');');
   473     $qa = $db->sql_query('INSERT INTO ' . table_prefix.'pages(name,urlname,namespace,visible,protected,delvote_ips) VALUES(\'' . $db->escape($name) . '\', \'' . $db->escape($page_id) . '\', \'' . $namespace . '\', '. ( $visible ? '1' : '0' ) .', ' . $prot . ', \'' . $db->escape(serialize($ips)) . '\');');
   474     $qb = $db->sql_query('INSERT INTO ' . table_prefix.'page_text(page_id,namespace) VALUES(\'' . $db->escape($page_id) . '\', \'' . $namespace . '\');');
   474     $qb = $db->sql_query('INSERT INTO ' . table_prefix.'page_text(page_id,namespace) VALUES(\'' . $db->escape($page_id) . '\', \'' . $namespace . '\');');
   475     $qc = $db->sql_query('INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,author,page_id,namespace) VALUES('.time().', \''.date('d M Y h:i a').'\', \'page\', \'create\', \'' . $session->username . '\', \'' . $db->escape($page_id) . '\', \'' . $namespace . '\');');
   475     $qc = $db->sql_query('INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,author,page_id,namespace) VALUES('.time().', \''.enano_date('d M Y h:i a').'\', \'page\', \'create\', \'' . $session->username . '\', \'' . $db->escape($page_id) . '\', \'' . $namespace . '\');');
   476     
   476     
   477     if($qa && $qb && $qc)
   477     if($qa && $qb && $qc)
   478       return 'good';
   478       return 'good';
   479     else
   479     else
   480     {
   480     {
   512     }
   512     }
   513     
   513     
   514     switch($level)
   514     switch($level)
   515     {
   515     {
   516       case 0:
   516       case 0:
   517         $q = 'INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,author,page_id,namespace,edit_summary) VALUES('.time().', \''.date('d M Y h:i a').'\', \'page\', \'unprot\', \'' . $session->username . '\', \'' . $page_id . '\', \'' . $namespace . '\', \'' . $db->escape(htmlspecialchars($reason)) . '\');';
   517         $q = 'INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,author,page_id,namespace,edit_summary) VALUES('.time().', \''.enano_date('d M Y h:i a').'\', \'page\', \'unprot\', \'' . $session->username . '\', \'' . $page_id . '\', \'' . $namespace . '\', \'' . $db->escape(htmlspecialchars($reason)) . '\');';
   518         break;
   518         break;
   519       case 1:
   519       case 1:
   520         $q = 'INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,author,page_id,namespace,edit_summary) VALUES('.time().', \''.date('d M Y h:i a').'\', \'page\', \'prot\', \'' . $session->username . '\', \'' . $page_id . '\', \'' . $namespace . '\', \'' . $db->escape(htmlspecialchars($reason)) . '\');';
   520         $q = 'INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,author,page_id,namespace,edit_summary) VALUES('.time().', \''.enano_date('d M Y h:i a').'\', \'page\', \'prot\', \'' . $session->username . '\', \'' . $page_id . '\', \'' . $namespace . '\', \'' . $db->escape(htmlspecialchars($reason)) . '\');';
   521         break;
   521         break;
   522       case 2:
   522       case 2:
   523         $q = 'INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,author,page_id,namespace,edit_summary) VALUES('.time().', \''.date('d M Y h:i a').'\', \'page\', \'semiprot\', \'' . $session->username . '\', \'' . $page_id . '\', \'' . $namespace . '\', \'' . $db->escape(htmlspecialchars($reason)) . '\');';
   523         $q = 'INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,author,page_id,namespace,edit_summary) VALUES('.time().', \''.enano_date('d M Y h:i a').'\', \'page\', \'semiprot\', \'' . $session->username . '\', \'' . $page_id . '\', \'' . $namespace . '\', \'' . $db->escape(htmlspecialchars($reason)) . '\');';
   524         break;
   524         break;
   525       default:
   525       default:
   526         return 'PageUtils::protect(): Invalid value for $level';
   526         return 'PageUtils::protect(): Invalid value for $level';
   527         break;
   527         break;
   528     }
   528     }
   805               return "You don't have permission to edit pages, so rolling back edits can't be allowed either.";
   805               return "You don't have permission to edit pages, so rolling back edits can't be allowed either.";
   806             $t = $db->escape($rb['page_text']);
   806             $t = $db->escape($rb['page_text']);
   807             $e = $db->sql_query('UPDATE ' . table_prefix.'page_text SET page_text=\'' . $t . '\',char_tag=\'' . $rb['char_tag'] . '\' WHERE page_id=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
   807             $e = $db->sql_query('UPDATE ' . table_prefix.'page_text SET page_text=\'' . $t . '\',char_tag=\'' . $rb['char_tag'] . '\' WHERE page_id=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
   808             if ( !$e )
   808             if ( !$e )
   809             {
   809             {
   810               return("An error occurred during the rollback operation.\nMySQL said: ".mysql_error()."\n\nSQL backtrace:\n".$db->sql_backtrace());
   810               return("An error occurred during the rollback operation.\nMySQL said: ".$db->get_error()."\n\nSQL backtrace:\n".$db->sql_backtrace());
   811             }
   811             }
   812             else
   812             else
   813             {
   813             {
   814               return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been rolled back to the state it was in on ' . $rb['date_string'] . '.';
   814               return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been rolled back to the state it was in on ' . $rb['date_string'] . '.';
   815             }
   815             }
   819               return "You don't have permission to rename pages, so rolling back renames can't be allowed either.";
   819               return "You don't have permission to rename pages, so rolling back renames can't be allowed either.";
   820             $t = $db->escape($rb['edit_summary']);
   820             $t = $db->escape($rb['edit_summary']);
   821             $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET name=\'' . $t . '\' WHERE urlname=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
   821             $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET name=\'' . $t . '\' WHERE urlname=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
   822             if ( !$e )
   822             if ( !$e )
   823             {
   823             {
   824               return "An error occurred during the rollback operation.\nMySQL said: ".mysql_error()."\n\nSQL backtrace:\n".$db->sql_backtrace();
   824               return "An error occurred during the rollback operation.\nMySQL said: ".$db->get_error()."\n\nSQL backtrace:\n".$db->sql_backtrace();
   825             }
   825             }
   826             else
   826             else
   827             {
   827             {
   828               return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been rolled back to the name it had ("' . $rb['edit_summary'] . '") before ' . $rb['date_string'] . '.';
   828               return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been rolled back to the name it had ("' . $rb['edit_summary'] . '") before ' . $rb['date_string'] . '.';
   829             }
   829             }
   831           case "prot":
   831           case "prot":
   832             if ( !$perms->get_permissions('protect') )
   832             if ( !$perms->get_permissions('protect') )
   833               return "You don't have permission to protect pages, so rolling back protection can't be allowed either.";
   833               return "You don't have permission to protect pages, so rolling back protection can't be allowed either.";
   834             $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET protected=0 WHERE urlname=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
   834             $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET protected=0 WHERE urlname=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
   835             if ( !$e )
   835             if ( !$e )
   836               return "An error occurred during the rollback operation.\nMySQL said: ".mysql_error()."\n\nSQL backtrace:\n".$db->sql_backtrace();
   836               return "An error occurred during the rollback operation.\nMySQL said: ".$db->get_error()."\n\nSQL backtrace:\n".$db->sql_backtrace();
   837             else
   837             else
   838               return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been unprotected according to the log created at ' . $rb['date_string'] . '.';
   838               return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been unprotected according to the log created at ' . $rb['date_string'] . '.';
   839             break;
   839             break;
   840           case "semiprot":
   840           case "semiprot":
   841             if ( !$perms->get_permissions('protect') )
   841             if ( !$perms->get_permissions('protect') )
   842               return "You don't have permission to protect pages, so rolling back protection can't be allowed either.";
   842               return "You don't have permission to protect pages, so rolling back protection can't be allowed either.";
   843             $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET protected=0 WHERE urlname=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
   843             $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET protected=0 WHERE urlname=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
   844             if ( !$e )
   844             if ( !$e )
   845               return "An error occurred during the rollback operation.\nMySQL said: ".mysql_error()."\n\nSQL backtrace:\n".$db->sql_backtrace();
   845               return "An error occurred during the rollback operation.\nMySQL said: ".$db->get_error()."\n\nSQL backtrace:\n".$db->sql_backtrace();
   846             else
   846             else
   847               return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been unprotected according to the log created at ' . $rb['date_string'] . '.';
   847               return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been unprotected according to the log created at ' . $rb['date_string'] . '.';
   848             break;
   848             break;
   849           case "unprot":
   849           case "unprot":
   850             if ( !$perms->get_permissions('protect') )
   850             if ( !$perms->get_permissions('protect') )
   851               return "You don't have permission to protect pages, so rolling back protection can't be allowed either.";
   851               return "You don't have permission to protect pages, so rolling back protection can't be allowed either.";
   852             $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET protected=1 WHERE urlname=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
   852             $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET protected=1 WHERE urlname=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\'');
   853             if ( !$e )
   853             if ( !$e )
   854               return "An error occurred during the rollback operation.\nMySQL said: ".mysql_error()."\n\nSQL backtrace:\n".$db->sql_backtrace();
   854               return "An error occurred during the rollback operation.\nMySQL said: ".$db->get_error()."\n\nSQL backtrace:\n".$db->sql_backtrace();
   855             else
   855             else
   856               return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been protected according to the log created at ' . $rb['date_string'] . '.';
   856               return 'The page "' . $paths->pages[$paths->nslist[$rb['namespace']].$rb['page_id']]['name'].'" has been protected according to the log created at ' . $rb['date_string'] . '.';
   857             break;
   857             break;
   858           case "delete":
   858           case "delete":
   859             if ( !$perms->get_permissions('history_rollback_extra') )
   859             if ( !$perms->get_permissions('history_rollback_extra') )
   860               return 'Administrative privileges are required for page undeletion.';
   860               return 'Administrative privileges are required for page undeletion.';
   861             if ( isset($paths->pages[$paths->cpage['urlname']]) )
   861             if ( isset($paths->pages[$paths->cpage['urlname']]) )
   862               return 'You cannot raise a dead page that is alive.';
   862               return 'You cannot raise a dead page that is alive.';
   863             $name = str_replace('_', ' ', $rb['page_id']);
   863             $name = str_replace('_', ' ', $rb['page_id']);
   864             $e = $db->sql_query('INSERT INTO ' . table_prefix.'pages(name,urlname,namespace) VALUES( \'' . $name . '\', \'' . $rb['page_id'] . '\',\'' . $rb['namespace'] . '\' )');if(!$e) return("An error occurred during the rollback operation.\nMySQL said: ".mysql_error()."\n\nSQL backtrace:\n".$db->sql_backtrace());
   864             $e = $db->sql_query('INSERT INTO ' . table_prefix.'pages(name,urlname,namespace) VALUES( \'' . $name . '\', \'' . $rb['page_id'] . '\',\'' . $rb['namespace'] . '\' )');if(!$e) return("An error occurred during the rollback operation.\nMySQL said: ".$db->get_error()."\n\nSQL backtrace:\n".$db->sql_backtrace());
   865             $e = $db->sql_query('SELECT page_text,char_tag FROM ' . table_prefix.'logs WHERE page_id=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\' AND log_type=\'page\' AND action=\'edit\' ORDER BY time_id DESC;'); if(!$e) return("An error occurred during the rollback operation.\nMySQL said: ".mysql_error()."\n\nSQL backtrace:\n".$db->sql_backtrace());
   865             $e = $db->sql_query('SELECT page_text,char_tag FROM ' . table_prefix.'logs WHERE page_id=\'' . $rb['page_id'] . '\' AND namespace=\'' . $rb['namespace'] . '\' AND log_type=\'page\' AND action=\'edit\' ORDER BY time_id DESC;'); if(!$e) return("An error occurred during the rollback operation.\nMySQL said: ".$db->get_error()."\n\nSQL backtrace:\n".$db->sql_backtrace());
   866             $r = $db->fetchrow();
   866             $r = $db->fetchrow();
   867             $e = $db->sql_query('INSERT INTO ' . table_prefix.'page_text(page_id,namespace,page_text,char_tag) VALUES(\'' . $rb['page_id'] . '\',\'' . $rb['namespace'] . '\',\'' . $db->escape($r['page_text']) . '\',\'' . $r['char_tag'] . '\')'); if(!$e) return("An error occurred during the rollback operation.\nMySQL said: ".mysql_error()."\n\nSQL backtrace:\n".$db->sql_backtrace());
   867             $e = $db->sql_query('INSERT INTO ' . table_prefix.'page_text(page_id,namespace,page_text,char_tag) VALUES(\'' . $rb['page_id'] . '\',\'' . $rb['namespace'] . '\',\'' . $db->escape($r['page_text']) . '\',\'' . $r['char_tag'] . '\')'); if(!$e) return("An error occurred during the rollback operation.\nMySQL said: ".$db->get_error()."\n\nSQL backtrace:\n".$db->sql_backtrace());
   868             return 'The page "' . $name . '" has been undeleted according to the log created at ' . $rb['date_string'] . '.';
   868             return 'The page "' . $name . '" has been undeleted according to the log created at ' . $rb['date_string'] . '.';
   869             break;
   869             break;
   870           case "reupload":
   870           case "reupload":
   871             if ( !$session->get_permissions('history_rollback_extra') )
   871             if ( !$session->get_permissions('history_rollback_extra') )
   872             {
   872             {
   873               return 'Administrative privileges are required for file rollbacks.';
   873               return 'Administrative privileges are required for file rollbacks.';
   874             }
   874             }
   875             $newtime = time();
   875             $newtime = time();
   876             $newdate = date('d M Y h:i a');
   876             $newdate = enano_date('d M Y h:i a');
   877             if(!$db->sql_query('UPDATE ' . table_prefix.'logs SET time_id=' . $newtime . ',date_string=\'' . $newdate . '\' WHERE time_id=' . $id))
   877             if(!$db->sql_query('UPDATE ' . table_prefix.'logs SET time_id=' . $newtime . ',date_string=\'' . $newdate . '\' WHERE time_id=' . $id))
   878               return 'Error during query: '.mysql_error();
   878               return 'Error during query: '.$db->get_error();
   879             if(!$db->sql_query('UPDATE ' . table_prefix.'files SET time_id=' . $newtime . ' WHERE time_id=' . $id))
   879             if(!$db->sql_query('UPDATE ' . table_prefix.'files SET time_id=' . $newtime . ' WHERE time_id=' . $id))
   880               return 'Error during query: '.mysql_error();
   880               return 'Error during query: '.$db->get_error();
   881             return 'The file has been rolled back to the version uploaded on '.date('d M Y h:i a', (int)$id).'.';
   881             return 'The file has been rolled back to the version uploaded on '.enano_date('d M Y h:i a', (int)$id).'.';
   882             break;
   882             break;
   883           default:
   883           default:
   884             return('Rollback of the action "' . $rb['action'] . '" is not yet supported.');
   884             return('Rollback of the action "' . $rb['action'] . '" is not yet supported.');
   885             break;
   885             break;
   886         }
   886         }
   921     $name = $session->user_logged_in ? RenderMan::preprocess_text($session->username) : RenderMan::preprocess_text($name);
   921     $name = $session->user_logged_in ? RenderMan::preprocess_text($session->username) : RenderMan::preprocess_text($name);
   922     $subj = RenderMan::preprocess_text($subject);
   922     $subj = RenderMan::preprocess_text($subject);
   923     if(getConfig('approve_comments')=='1') $appr = '0'; else $appr = '1';
   923     if(getConfig('approve_comments')=='1') $appr = '0'; else $appr = '1';
   924     $q = 'INSERT INTO ' . table_prefix.'comments(page_id,namespace,subject,comment_data,name,user_id,approved,time) VALUES(\'' . $page_id . '\',\'' . $namespace . '\',\'' . $subj . '\',\'' . $text . '\',\'' . $name . '\',' . $session->user_id . ',' . $appr . ','.time().')';
   924     $q = 'INSERT INTO ' . table_prefix.'comments(page_id,namespace,subject,comment_data,name,user_id,approved,time) VALUES(\'' . $page_id . '\',\'' . $namespace . '\',\'' . $subj . '\',\'' . $text . '\',\'' . $name . '\',' . $session->user_id . ',' . $appr . ','.time().')';
   925     $e = $db->sql_query($q);
   925     $e = $db->sql_query($q);
   926     if(!$e) die('alert(unescape(\''.rawurlencode('Error inserting comment data: '.mysql_error().'\n\nQuery:\n' . $q) . '\'))');
   926     if(!$e) die('alert(unescape(\''.rawurlencode('Error inserting comment data: '.$db->get_error().'\n\nQuery:\n' . $q) . '\'))');
   927     else $_ob .= '<div class="info-box">Your comment has been posted.</div>';
   927     else $_ob .= '<div class="info-box">Your comment has been posted.</div>';
   928     return PageUtils::comments($page_id, $namespace, false, Array(), $_ob);
   928     return PageUtils::comments($page_id, $namespace, false, Array(), $_ob);
   929   }
   929   }
   930   
   930   
   931   /**
   931   /**
   960           $s = $db->escape($flags['subj']);
   960           $s = $db->escape($flags['subj']);
   961           $t = $db->escape($flags['text']);
   961           $t = $db->escape($flags['text']);
   962           $q = 'DELETE FROM ' . table_prefix.'comments WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND name=\'' . $n . '\' AND subject=\'' . $s . '\' AND comment_data=\'' . $t . '\' LIMIT 1;';
   962           $q = 'DELETE FROM ' . table_prefix.'comments WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND name=\'' . $n . '\' AND subject=\'' . $s . '\' AND comment_data=\'' . $t . '\' LIMIT 1;';
   963         }
   963         }
   964         $e=$db->sql_query($q);
   964         $e=$db->sql_query($q);
   965         if(!$e) die('alert(unesape(\''.rawurlencode('Error during query: '.mysql_error().'\n\nQuery:\n' . $q) . '\'));');
   965         if(!$e) die('alert(unesape(\''.rawurlencode('Error during query: '.$db->get_error().'\n\nQuery:\n' . $q) . '\'));');
   966         break;
   966         break;
   967       case "approve":
   967       case "approve":
   968         if(isset($flags['id']))
   968         if(isset($flags['id']))
   969         {
   969         {
   970           $where = 'comment_id='.intval($flags['id']);
   970           $where = 'comment_id='.intval($flags['id']);
   974           $t = $db->escape($flags['text']);
   974           $t = $db->escape($flags['text']);
   975           $where = 'name=\'' . $n . '\' AND subject=\'' . $s . '\' AND comment_data=\'' . $t . '\'';
   975           $where = 'name=\'' . $n . '\' AND subject=\'' . $s . '\' AND comment_data=\'' . $t . '\'';
   976         }
   976         }
   977         $q = 'SELECT approved FROM ' . table_prefix.'comments WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND ' . $where . ' LIMIT 1;';
   977         $q = 'SELECT approved FROM ' . table_prefix.'comments WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND ' . $where . ' LIMIT 1;';
   978         $e = $db->sql_query($q);
   978         $e = $db->sql_query($q);
   979         if(!$e) die('alert(unesape(\''.rawurlencode('Error selecting approval status: '.mysql_error().'\n\nQuery:\n' . $q) . '\'));');
   979         if(!$e) die('alert(unesape(\''.rawurlencode('Error selecting approval status: '.$db->get_error().'\n\nQuery:\n' . $q) . '\'));');
   980         $r = $db->fetchrow();
   980         $r = $db->fetchrow();
   981         $db->free_result();
   981         $db->free_result();
   982         $a = ( $r['approved'] ) ? '0' : '1';
   982         $a = ( $r['approved'] ) ? '0' : '1';
   983         $q = 'UPDATE ' . table_prefix.'comments SET approved=' . $a . ' WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND ' . $where . ';';
   983         $q = 'UPDATE ' . table_prefix.'comments SET approved=' . $a . ' WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND ' . $where . ';';
   984         $e=$db->sql_query($q);
   984         $e=$db->sql_query($q);
   985         if(!$e) die('alert(unesape(\''.rawurlencode('Error during query: '.mysql_error().'\n\nQuery:\n' . $q) . '\'));');
   985         if(!$e) die('alert(unesape(\''.rawurlencode('Error during query: '.$db->get_error().'\n\nQuery:\n' . $q) . '\'));');
   986         if($a=='1') $v = $lang->get('comment_btn_mod_unapprove');
   986         if($a=='1') $v = $lang->get('comment_btn_mod_unapprove');
   987         else $v = $lang->get('comment_btn_mod_approve');
   987         else $v = $lang->get('comment_btn_mod_approve');
   988         echo 'document.getElementById("mdgApproveLink'.intval($_GET['id']).'").innerHTML="' . $v . '";';
   988         echo 'document.getElementById("mdgApproveLink'.intval($_GET['id']).'").innerHTML="' . $v . '";';
   989         break;
   989         break;
   990       }
   990       }
  1009                   FROM ' . table_prefix.'comments AS c
  1009                   FROM ' . table_prefix.'comments AS c
  1010                   LEFT JOIN ' . table_prefix.'users AS u
  1010                   LEFT JOIN ' . table_prefix.'users AS u
  1011                     ON c.user_id=u.user_id
  1011                     ON c.user_id=u.user_id
  1012                   WHERE page_id=\'' . $page_id . '\'
  1012                   WHERE page_id=\'' . $page_id . '\'
  1013                   AND namespace=\'' . $namespace . '\' ORDER BY c.time ASC;');
  1013                   AND namespace=\'' . $namespace . '\' ORDER BY c.time ASC;');
  1014     if(!$lq) _die('The comment text data could not be selected. '.mysql_error());
  1014     if(!$lq) _die('The comment text data could not be selected. '.$db->get_error());
  1015     $_ob .= '<h3>' . $lang->get('comment_heading') . '</h3>';
  1015     $_ob .= '<h3>' . $lang->get('comment_heading') . '</h3>';
  1016     
  1016     
  1017     $n = ( $session->get_permissions('mod_comments')) ? $db->numrows() : $num_app;
  1017     $n = ( $session->get_permissions('mod_comments')) ? $db->numrows() : $num_app;
  1018     
  1018     
  1019     $subst = array(
  1019     $subst = array(
  1060         $s = $row['subject'];
  1060         $s = $row['subject'];
  1061         if(!$row['approved']) $s .= ' <span style="color: #D84308">' . $lang->get('comment_msg_note_unapp') . '</span>';
  1061         if(!$row['approved']) $s .= ' <span style="color: #D84308">' . $lang->get('comment_msg_note_unapp') . '</span>';
  1062         $strings['SUBJECT'] = $s;
  1062         $strings['SUBJECT'] = $s;
  1063         
  1063         
  1064         // Date and time
  1064         // Date and time
  1065         $strings['DATETIME'] = date('F d, Y h:i a', $row['time']);
  1065         $strings['DATETIME'] = enano_date('F d, Y h:i a', $row['time']);
  1066         
  1066         
  1067         // User level
  1067         // User level
  1068         switch($row['user_level'])
  1068         switch($row['user_level'])
  1069         {
  1069         {
  1070           default:
  1070           default:
  1245     if(!$session->get_permissions('mod_comments')) // allow mods to edit comments
  1245     if(!$session->get_permissions('mod_comments')) // allow mods to edit comments
  1246     {
  1246     {
  1247       if(!$session->user_logged_in) _die('AJAX comment save safety check failed because you are not logged in. Sometimes this can happen because you are using a browser that does not send cookies as part of AJAX requests.<br /><br />Please log in and try again.');
  1247       if(!$session->user_logged_in) _die('AJAX comment save safety check failed because you are not logged in. Sometimes this can happen because you are using a browser that does not send cookies as part of AJAX requests.<br /><br />Please log in and try again.');
  1248       $q = 'SELECT c.name FROM ' . table_prefix.'comments c, ' . table_prefix.'users u WHERE comment_data=\'' . $old_text . '\' AND subject=\'' . $old_subject . '\' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND u.user_id=c.user_id;';
  1248       $q = 'SELECT c.name FROM ' . table_prefix.'comments c, ' . table_prefix.'users u WHERE comment_data=\'' . $old_text . '\' AND subject=\'' . $old_subject . '\' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND u.user_id=c.user_id;';
  1249       $s = $db->sql_query($q);
  1249       $s = $db->sql_query($q);
  1250       if(!$s) _die('SQL error during safety check: '.mysql_error().'<br /><br />Attempted SQL:<br /><pre>'.htmlspecialchars($q).'</pre>');
  1250       if(!$s) _die('SQL error during safety check: '.$db->get_error().'<br /><br />Attempted SQL:<br /><pre>'.htmlspecialchars($q).'</pre>');
  1251       $r = $db->fetchrow($s);
  1251       $r = $db->fetchrow($s);
  1252       $db->free_result();
  1252       $db->free_result();
  1253       if($db->numrows() < 1 || $r['name'] != $session->username) _die('Safety check failed, probably due to a hacking attempt.');
  1253       if($db->numrows() < 1 || $r['name'] != $session->username) _die('Safety check failed, probably due to a hacking attempt.');
  1254     }
  1254     }
  1255     $s = RenderMan::preprocess_text($subject);
  1255     $s = RenderMan::preprocess_text($subject);
  1268     {
  1268     {
  1269       return 'result="BAD"; error=unescape("'.rawurlencode('Enano encountered a problem whilst saving the comment.
  1269       return 'result="BAD"; error=unescape("'.rawurlencode('Enano encountered a problem whilst saving the comment.
  1270       Performed SQL:
  1270       Performed SQL:
  1271       ' . $sql . '
  1271       ' . $sql . '
  1272     
  1272     
  1273       Error returned by MySQL: '.mysql_error()).'");';
  1273       Error returned by MySQL: '.$db->get_error()).'");';
  1274     }
  1274     }
  1275   }
  1275   }
  1276   
  1276   
  1277   /**
  1277   /**
  1278    * Updates comment data using the comment_id column instead of the old, messy way
  1278    * Updates comment data using the comment_id column instead of the old, messy way
  1294     if(!$session->get_permissions('mod_comments')) // allow mods to edit comments
  1294     if(!$session->get_permissions('mod_comments')) // allow mods to edit comments
  1295     {
  1295     {
  1296       if(!$session->user_logged_in) _die('AJAX comment save safety check failed because you are not logged in. Sometimes this can happen because you are using a browser that does not send cookies as part of AJAX requests.<br /><br />Please log in and try again.');
  1296       if(!$session->user_logged_in) _die('AJAX comment save safety check failed because you are not logged in. Sometimes this can happen because you are using a browser that does not send cookies as part of AJAX requests.<br /><br />Please log in and try again.');
  1297       $q = 'SELECT c.name FROM ' . table_prefix.'comments c, ' . table_prefix.'users u WHERE comment_id=' . $id . ' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND u.user_id=c.user_id;';
  1297       $q = 'SELECT c.name FROM ' . table_prefix.'comments c, ' . table_prefix.'users u WHERE comment_id=' . $id . ' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND u.user_id=c.user_id;';
  1298       $s = $db->sql_query($q);
  1298       $s = $db->sql_query($q);
  1299       if(!$s) _die('SQL error during safety check: '.mysql_error().'<br /><br />Attempted SQL:<br /><pre>'.htmlspecialchars($q).'</pre>');
  1299       if(!$s) _die('SQL error during safety check: '.$db->get_error().'<br /><br />Attempted SQL:<br /><pre>'.htmlspecialchars($q).'</pre>');
  1300       $r = $db->fetchrow($s);
  1300       $r = $db->fetchrow($s);
  1301       if($db->numrows() < 1 || $r['name'] != $session->username) _die('Safety check failed, probably due to a hacking attempt.');
  1301       if($db->numrows() < 1 || $r['name'] != $session->username) _die('Safety check failed, probably due to a hacking attempt.');
  1302       $db->free_result();
  1302       $db->free_result();
  1303     }
  1303     }
  1304     $s = RenderMan::preprocess_text($subject);
  1304     $s = RenderMan::preprocess_text($subject);
  1309     return 'good';
  1309     return 'good';
  1310     else return 'Enano encountered a problem whilst saving the comment.
  1310     else return 'Enano encountered a problem whilst saving the comment.
  1311     Performed SQL:
  1311     Performed SQL:
  1312     ' . $sql . '
  1312     ' . $sql . '
  1313     
  1313     
  1314     Error returned by MySQL: '.mysql_error();
  1314     Error returned by MySQL: '.$db->get_error();
  1315   }
  1315   }
  1316   
  1316   
  1317   /**
  1317   /**
  1318    * Deletes a comment.
  1318    * Deletes a comment.
  1319    * @param $page_id the page ID
  1319    * @param $page_id the page ID
  1341     if(!$session->get_permissions('mod_comments')) // allows mods to delete comments
  1341     if(!$session->get_permissions('mod_comments')) // allows mods to delete comments
  1342     {
  1342     {
  1343       if(!$session->user_logged_in) _die('AJAX comment save safety check failed because you are not logged in. Sometimes this can happen because you are using a browser that does not send cookies as part of AJAX requests.<br /><br />Please log in and try again.');
  1343       if(!$session->user_logged_in) _die('AJAX comment save safety check failed because you are not logged in. Sometimes this can happen because you are using a browser that does not send cookies as part of AJAX requests.<br /><br />Please log in and try again.');
  1344       $q = 'SELECT c.name FROM ' . table_prefix.'comments c, ' . table_prefix.'users u WHERE comment_data=\'' . $t . '\' AND subject=\'' . $s . '\' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND u.user_id=c.user_id;';
  1344       $q = 'SELECT c.name FROM ' . table_prefix.'comments c, ' . table_prefix.'users u WHERE comment_data=\'' . $t . '\' AND subject=\'' . $s . '\' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND u.user_id=c.user_id;';
  1345       $s = $db->sql_query($q);
  1345       $s = $db->sql_query($q);
  1346       if(!$s) _die('SQL error during safety check: '.mysql_error().'<br /><br />Attempted SQL:<br /><pre>'.htmlspecialchars($q).'</pre>');
  1346       if(!$s) _die('SQL error during safety check: '.$db->get_error().'<br /><br />Attempted SQL:<br /><pre>'.htmlspecialchars($q).'</pre>');
  1347       $r = $db->fetchrow($s);
  1347       $r = $db->fetchrow($s);
  1348       if($db->numrows() < 1 || $r['name'] != $session->username) _die('Safety check failed, probably due to a hacking attempt.');
  1348       if($db->numrows() < 1 || $r['name'] != $session->username) _die('Safety check failed, probably due to a hacking attempt.');
  1349       $db->free_result();
  1349       $db->free_result();
  1350     }
  1350     }
  1351     $q = 'DELETE FROM ' . table_prefix.'comments WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND name=\'' . $n . '\' AND subject=\'' . $s . '\' AND comment_data=\'' . $t . '\' LIMIT 1;';
  1351     $q = 'DELETE FROM ' . table_prefix.'comments WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND name=\'' . $n . '\' AND subject=\'' . $s . '\' AND comment_data=\'' . $t . '\' LIMIT 1;';
  1352     $e=$db->sql_query($q);
  1352     $e=$db->sql_query($q);
  1353     if(!$e) return('alert(unesape(\''.rawurlencode('Error during query: '.mysql_error().'\n\nQuery:\n' . $q) . '\'));');
  1353     if(!$e) return('alert(unesape(\''.rawurlencode('Error during query: '.$db->get_error().'\n\nQuery:\n' . $q) . '\'));');
  1354     return('good');
  1354     return('good');
  1355   }
  1355   }
  1356   
  1356   
  1357   /**
  1357   /**
  1358    * Deletes a comment in a cleaner fashion.
  1358    * Deletes a comment in a cleaner fashion.
  1375     if(!$session->get_permissions('mod_comments')) // allows mods to delete comments
  1375     if(!$session->get_permissions('mod_comments')) // allows mods to delete comments
  1376     {
  1376     {
  1377       if(!$session->user_logged_in) _die('AJAX comment save safety check failed because you are not logged in. Sometimes this can happen because you are using a browser that does not send cookies as part of AJAX requests.<br /><br />Please log in and try again.');
  1377       if(!$session->user_logged_in) _die('AJAX comment save safety check failed because you are not logged in. Sometimes this can happen because you are using a browser that does not send cookies as part of AJAX requests.<br /><br />Please log in and try again.');
  1378       $q = 'SELECT c.name FROM ' . table_prefix.'comments c, ' . table_prefix.'users u WHERE comment_id=' . $id . ' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND u.user_id=c.user_id;';
  1378       $q = 'SELECT c.name FROM ' . table_prefix.'comments c, ' . table_prefix.'users u WHERE comment_id=' . $id . ' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND u.user_id=c.user_id;';
  1379       $s = $db->sql_query($q);
  1379       $s = $db->sql_query($q);
  1380       if(!$s) _die('SQL error during safety check: '.mysql_error().'<br /><br />Attempted SQL:<br /><pre>'.htmlspecialchars($q).'</pre>');
  1380       if(!$s) _die('SQL error during safety check: '.$db->get_error().'<br /><br />Attempted SQL:<br /><pre>'.htmlspecialchars($q).'</pre>');
  1381       $r = $db->fetchrow($s);
  1381       $r = $db->fetchrow($s);
  1382       if($db->numrows() < 1 || $r['name'] != $session->username) _die('Safety check failed, probably due to a hacking attempt.');
  1382       if($db->numrows() < 1 || $r['name'] != $session->username) _die('Safety check failed, probably due to a hacking attempt.');
  1383       $db->free_result();
  1383       $db->free_result();
  1384     }
  1384     }
  1385     $q = 'DELETE FROM ' . table_prefix.'comments WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND comment_id=' . $id . ' LIMIT 1;';
  1385     $q = 'DELETE FROM ' . table_prefix.'comments WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\' AND comment_id=' . $id . ' LIMIT 1;';
  1386     $e=$db->sql_query($q);
  1386     $e=$db->sql_query($q);
  1387     if(!$e) return('alert(unesape(\''.rawurlencode('Error during query: '.mysql_error().'\n\nQuery:\n' . $q) . '\'));');
  1387     if(!$e) return('alert(unesape(\''.rawurlencode('Error during query: '.$db->get_error().'\n\nQuery:\n' . $q) . '\'));');
  1388     return('good');
  1388     return('good');
  1389   }
  1389   }
  1390   
  1390   
  1391   /**
  1391   /**
  1392    * Renames a page.
  1392    * Renames a page.
  1410     {
  1410     {
  1411       return($lang->get('ajax_rename_too_short'));
  1411       return($lang->get('ajax_rename_too_short'));
  1412     }
  1412     }
  1413     if( ( $session->get_permissions('rename') && ( ( $prot && $session->get_permissions('even_when_protected') ) || !$prot ) ) && ( $paths->namespace != 'Special' && $paths->namespace != 'Admin' ))
  1413     if( ( $session->get_permissions('rename') && ( ( $prot && $session->get_permissions('even_when_protected') ) || !$prot ) ) && ( $paths->namespace != 'Special' && $paths->namespace != 'Admin' ))
  1414     {
  1414     {
  1415       $e = $db->sql_query('INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,page_id,namespace,author,edit_summary) VALUES('.time().', \''.date('d M Y h:i a').'\', \'page\', \'rename\', \'' . $db->escape($paths->page_id) . '\', \'' . $paths->namespace . '\', \'' . $db->escape($session->username) . '\', \'' . $db->escape($paths->cpage['name']) . '\')');
  1415       $e = $db->sql_query('INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,page_id,namespace,author,edit_summary) VALUES('.time().', \''.enano_date('d M Y h:i a').'\', \'page\', \'rename\', \'' . $db->escape($paths->page_id) . '\', \'' . $paths->namespace . '\', \'' . $db->escape($session->username) . '\', \'' . $db->escape($paths->cpage['name']) . '\')');
  1416       if ( !$e )
  1416       if ( !$e )
  1417       {
  1417       {
  1418         $db->_die('The page title could not be updated.');
  1418         $db->_die('The page title could not be updated.');
  1419       }
  1419       }
  1420       $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET name=\'' . $db->escape($name) . '\' WHERE urlname=\'' . $db->escape($page_id) . '\' AND namespace=\'' . $db->escape($namespace) . '\';');
  1420       $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET name=\'' . $db->escape($name) . '\' WHERE urlname=\'' . $db->escape($page_id) . '\' AND namespace=\'' . $db->escape($namespace) . '\';');
  1467       $e = $db->sql_query('SELECT page_text,char_tag FROM ' . table_prefix.'page_text WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';');
  1467       $e = $db->sql_query('SELECT page_text,char_tag FROM ' . table_prefix.'page_text WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';');
  1468       if(!$e) $db->_die('The current page text could not be selected; as a result, creating the backup of the page failed. Please make a backup copy of the page by clicking Edit this page and then clicking Save Changes.');
  1468       if(!$e) $db->_die('The current page text could not be selected; as a result, creating the backup of the page failed. Please make a backup copy of the page by clicking Edit this page and then clicking Save Changes.');
  1469       $row = $db->fetchrow();
  1469       $row = $db->fetchrow();
  1470       $db->free_result();
  1470       $db->free_result();
  1471       $minor_edit = ( ENANO_DBLAYER == 'MYSQL' ) ? 'false' : '0';
  1471       $minor_edit = ( ENANO_DBLAYER == 'MYSQL' ) ? 'false' : '0';
  1472       $q='INSERT INTO ' . table_prefix.'logs(log_type,action,time_id,date_string,page_id,namespace,page_text,char_tag,author,edit_summary,minor_edit) VALUES(\'page\', \'edit\', '.time().', \''.date('d M Y h:i a').'\', \'' . $page_id . '\', \'' . $namespace . '\', \'' . $db->escape($row['page_text']) . '\', \'' . $row['char_tag'] . '\', \'' . $session->username . '\', \''."Automatic backup created when logs were purged".'\', '.$minor_edit.');';
  1472       $q='INSERT INTO ' . table_prefix.'logs(log_type,action,time_id,date_string,page_id,namespace,page_text,char_tag,author,edit_summary,minor_edit) VALUES(\'page\', \'edit\', '.time().', \''.enano_date('d M Y h:i a').'\', \'' . $page_id . '\', \'' . $namespace . '\', \'' . $db->escape($row['page_text']) . '\', \'' . $row['char_tag'] . '\', \'' . $session->username . '\', \''."Automatic backup created when logs were purged".'\', '.$minor_edit.');';
  1473       if(!$db->sql_query($q)) $db->_die('The history (log) entry could not be inserted into the logs table.');
  1473       if(!$db->sql_query($q)) $db->_die('The history (log) entry could not be inserted into the logs table.');
  1474     }
  1474     }
  1475     return $lang->get('ajax_clearlogs_success');
  1475     return $lang->get('ajax_clearlogs_success');
  1476   }
  1476   }
  1477   
  1477   
  1492     if ( empty($x) )
  1492     if ( empty($x) )
  1493     {
  1493     {
  1494       return $lang->get('ajax_delete_need_reason');
  1494       return $lang->get('ajax_delete_need_reason');
  1495     }
  1495     }
  1496     if(!$perms->get_permissions('delete_page')) return('Administrative privileges are required to delete pages, you loser.');
  1496     if(!$perms->get_permissions('delete_page')) return('Administrative privileges are required to delete pages, you loser.');
  1497     $e = $db->sql_query('INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,page_id,namespace,author,edit_summary) VALUES('.time().', \''.date('d M Y h:i a').'\', \'page\', \'delete\', \'' . $page_id . '\', \'' . $namespace . '\', \'' . $session->username . '\', \'' . $db->escape(htmlspecialchars($reason)) . '\')');
  1497     $e = $db->sql_query('INSERT INTO ' . table_prefix.'logs(time_id,date_string,log_type,action,page_id,namespace,author,edit_summary) VALUES('.time().', \''.enano_date('d M Y h:i a').'\', \'page\', \'delete\', \'' . $page_id . '\', \'' . $namespace . '\', \'' . $session->username . '\', \'' . $db->escape(htmlspecialchars($reason)) . '\')');
  1498     if(!$e) $db->_die('The page log entry could not be inserted.');
  1498     if(!$e) $db->_die('The page log entry could not be inserted.');
  1499     $e = $db->sql_query('DELETE FROM ' . table_prefix.'categories WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\'');
  1499     $e = $db->sql_query('DELETE FROM ' . table_prefix.'categories WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\'');
  1500     if(!$e) $db->_die('The page categorization entries could not be deleted.');
  1500     if(!$e) $db->_die('The page categorization entries could not be deleted.');
  1501     $e = $db->sql_query('DELETE FROM ' . table_prefix.'comments WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\'');
  1501     $e = $db->sql_query('DELETE FROM ' . table_prefix.'comments WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\'');
  1502     if(!$e) $db->_die('The page comments could not be deleted.');
  1502     if(!$e) $db->_die('The page comments could not be deleted.');
  1660     global $lang;
  1660     global $lang;
  1661     
  1661     
  1662     ob_start();
  1662     ob_start();
  1663     $_ob = '';
  1663     $_ob = '';
  1664     $e = $db->sql_query('SELECT category_id FROM ' . table_prefix.'categories WHERE page_id=\'' . $paths->page_id . '\' AND namespace=\'' . $paths->namespace . '\'');
  1664     $e = $db->sql_query('SELECT category_id FROM ' . table_prefix.'categories WHERE page_id=\'' . $paths->page_id . '\' AND namespace=\'' . $paths->namespace . '\'');
  1665     if(!$e) jsdie('Error selecting category information for current page: '.mysql_error());
  1665     if(!$e) jsdie('Error selecting category information for current page: '.$db->get_error());
  1666     $cat_current = Array();
  1666     $cat_current = Array();
  1667     while($r = $db->fetchrow())
  1667     while($r = $db->fetchrow())
  1668     {
  1668     {
  1669       $cat_current[] = $r;
  1669       $cat_current[] = $r;
  1670     }
  1670     }
  1818       return('Invalid mode string');
  1818       return('Invalid mode string');
  1819     }
  1819     }
  1820     $q = $db->sql_query('UPDATE ' . table_prefix.'pages SET wiki_mode=' . $level . ' WHERE urlname=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';');
  1820     $q = $db->sql_query('UPDATE ' . table_prefix.'pages SET wiki_mode=' . $level . ' WHERE urlname=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';');
  1821     if ( !$q )
  1821     if ( !$q )
  1822     {
  1822     {
  1823       return('Error during update query: '.mysql_error()."\n\nSQL Backtrace:\n".$db->sql_backtrace());
  1823       return('Error during update query: '.$db->get_error()."\n\nSQL Backtrace:\n".$db->sql_backtrace());
  1824     }
  1824     }
  1825     return('GOOD');
  1825     return('GOOD');
  1826   }
  1826   }
  1827   
  1827   
  1828   /**
  1828   /**
  1854       // sha1('') = da39a3ee5e6b4b0d3255bfef95601890afd80709
  1854       // sha1('') = da39a3ee5e6b4b0d3255bfef95601890afd80709
  1855       $p = '';
  1855       $p = '';
  1856     $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET password=\'' . $p . '\' WHERE urlname=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';');
  1856     $e = $db->sql_query('UPDATE ' . table_prefix.'pages SET password=\'' . $p . '\' WHERE urlname=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';');
  1857     if ( !$e )
  1857     if ( !$e )
  1858     {
  1858     {
  1859       die('PageUtils::setpass(): Error during update query: '.mysql_error()."\n\nSQL Backtrace:\n".$db->sql_backtrace());
  1859       die('PageUtils::setpass(): Error during update query: '.$db->get_error()."\n\nSQL Backtrace:\n".$db->sql_backtrace());
  1860     }
  1860     }
  1861     // Is the new password blank?
  1861     // Is the new password blank?
  1862     if ( $p == '' )
  1862     if ( $p == '' )
  1863     {
  1863     {
  1864       return $lang->get('ajax_password_disable_success');
  1864       return $lang->get('ajax_password_disable_success');
  1918       return $lang->get('etc_access_denied');
  1918       return $lang->get('etc_access_denied');
  1919     if(!preg_match('#^([0-9]+)$#', (string)$id1) ||
  1919     if(!preg_match('#^([0-9]+)$#', (string)$id1) ||
  1920        !preg_match('#^([0-9]+)$#', (string)$id2  )) return 'SQL injection attempt';
  1920        !preg_match('#^([0-9]+)$#', (string)$id2  )) return 'SQL injection attempt';
  1921     // OK we made it through security
  1921     // OK we made it through security
  1922     // Safest way to make sure we don't end up with the revisions in wrong columns is to make 2 queries
  1922     // Safest way to make sure we don't end up with the revisions in wrong columns is to make 2 queries
  1923     if(!$q1 = $db->sql_query('SELECT page_text,char_tag,author,edit_summary FROM ' . table_prefix.'logs WHERE time_id=' . $id1 . ' AND log_type=\'page\' AND action=\'edit\' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';')) return 'MySQL error: '.mysql_error();
  1923     if(!$q1 = $db->sql_query('SELECT page_text,char_tag,author,edit_summary FROM ' . table_prefix.'logs WHERE time_id=' . $id1 . ' AND log_type=\'page\' AND action=\'edit\' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';')) return 'MySQL error: '.$db->get_error();
  1924     if(!$q2 = $db->sql_query('SELECT page_text,char_tag,author,edit_summary FROM ' . table_prefix.'logs WHERE time_id=' . $id2 . ' AND log_type=\'page\' AND action=\'edit\' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';')) return 'MySQL error: '.mysql_error();
  1924     if(!$q2 = $db->sql_query('SELECT page_text,char_tag,author,edit_summary FROM ' . table_prefix.'logs WHERE time_id=' . $id2 . ' AND log_type=\'page\' AND action=\'edit\' AND page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';')) return 'MySQL error: '.$db->get_error();
  1925     $row1 = $db->fetchrow($q1);
  1925     $row1 = $db->fetchrow($q1);
  1926     $db->free_result($q1);
  1926     $db->free_result($q1);
  1927     $row2 = $db->fetchrow($q2);
  1927     $row2 = $db->fetchrow($q2);
  1928     $db->free_result($q2);
  1928     $db->free_result($q2);
  1929     if(sizeof($row1) < 1 || sizeof($row2) < 2) return 'Couldn\'t find any rows that matched the query. The time ID probably doesn\'t exist in the logs table.';
  1929     if(sizeof($row1) < 1 || sizeof($row2) < 2) return 'Couldn\'t find any rows that matched the query. The time ID probably doesn\'t exist in the logs table.';
  1930     $text1 = $row1['page_text'];
  1930     $text1 = $row1['page_text'];
  1931     $text2 = $row2['page_text'];
  1931     $text2 = $row2['page_text'];
  1932     $time1 = date('F d, Y h:i a', $id1);
  1932     $time1 = enano_date('F d, Y h:i a', $id1);
  1933     $time2 = date('F d, Y h:i a', $id2);
  1933     $time2 = enano_date('F d, Y h:i a', $id2);
  1934     $_ob = "
  1934     $_ob = "
  1935     <p>" . $lang->get('history_lbl_comparingrevisions') . " {$time1} &rarr; {$time2}</p>
  1935     <p>" . $lang->get('history_lbl_comparingrevisions') . " {$time1} &rarr; {$time2}</p>
  1936     ";
  1936     ";
  1937     // Free some memory
  1937     // Free some memory
  1938     unset($row1, $row2, $q1, $q2);
  1938     unset($row1, $row2, $q1, $q2);
  2025                     ON a.target_id=u.user_id
  2025                     ON a.target_id=u.user_id
  2026                   WHERE a.target_type='.ACL_TYPE_USER.'
  2026                   WHERE a.target_type='.ACL_TYPE_USER.'
  2027                     AND u.username=\'' . $db->escape($parms['target_id']) . '\'
  2027                     AND u.username=\'' . $db->escape($parms['target_id']) . '\'
  2028                     ' . $page_where_clause . ';');
  2028                     ' . $page_where_clause . ';');
  2029               if(!$q)
  2029               if(!$q)
  2030                 return(Array('mode'=>'error','error'=>mysql_error()));
  2030                 return(Array('mode'=>'error','error'=>$db->get_error()));
  2031               if($db->numrows() < 1)
  2031               if($db->numrows() < 1)
  2032               {
  2032               {
  2033                 $return['type'] = 'new';
  2033                 $return['type'] = 'new';
  2034                 $q = $db->sql_query('SELECT user_id FROM ' . table_prefix.'users WHERE username=\'' . $db->escape($parms['target_id']) . '\';');
  2034                 $q = $db->sql_query('SELECT user_id FROM ' . table_prefix.'users WHERE username=\'' . $db->escape($parms['target_id']) . '\';');
  2035                 if(!$q)
  2035                 if(!$q)
  2036                   return(Array('mode'=>'error','error'=>mysql_error()));
  2036                   return(Array('mode'=>'error','error'=>$db->get_error()));
  2037                 if($db->numrows() < 1)
  2037                 if($db->numrows() < 1)
  2038                   return Array('mode'=>'error','error'=>$lang->get('acl_err_user_not_found'));
  2038                   return Array('mode'=>'error','error'=>$lang->get('acl_err_user_not_found'));
  2039                 $row = $db->fetchrow();
  2039                 $row = $db->fetchrow();
  2040                 $return['target_name'] = $return['target_id'];
  2040                 $return['target_name'] = $return['target_id'];
  2041                 $return['target_id'] = intval($row['user_id']);
  2041                 $return['target_id'] = intval($row['user_id']);
  2042                 $return['current_perms'] = $session->acl_types;
  2042                 $return['current_perms'] = array();
  2043               }
  2043               }
  2044               else
  2044               else
  2045               {
  2045               {
  2046                 $return['type'] = 'edit';
  2046                 $return['type'] = 'edit';
  2047                 $row = $db->fetchrow();
  2047                 $row = $db->fetchrow();
  2048                 $return['target_name'] = $return['target_id'];
  2048                 $return['target_name'] = $return['target_id'];
  2049                 $return['target_id'] = intval($row['user_id']);
  2049                 $return['target_id'] = intval($row['user_id']);
  2050                 $return['current_perms'] = $session->acl_merge($perms_obj->acl_types, $session->string_to_perm($row['rules']));
  2050                 $return['current_perms'] = $session->string_to_perm($row['rules']);
  2051               }
  2051               }
  2052               $db->free_result();
  2052               $db->free_result();
  2053               // Eliminate types that don't apply to this namespace
  2053               // Eliminate types that don't apply to this namespace
  2054               if ( $namespace && $namespace != '__PageGroup' )
  2054               if ( $namespace && $namespace != '__PageGroup' )
  2055               {
  2055               {
  2072                     ON a.target_id=g.group_id
  2072                     ON a.target_id=g.group_id
  2073                   WHERE a.target_type='.ACL_TYPE_GROUP.'
  2073                   WHERE a.target_type='.ACL_TYPE_GROUP.'
  2074                     AND g.group_id=\''.intval($parms['target_id']).'\'
  2074                     AND g.group_id=\''.intval($parms['target_id']).'\'
  2075                     ' . $page_where_clause . ';');
  2075                     ' . $page_where_clause . ';');
  2076               if(!$q)
  2076               if(!$q)
  2077                 return(Array('mode'=>'error','error'=>mysql_error()));
  2077                 return(Array('mode'=>'error','error'=>$db->get_error()));
  2078               if($db->numrows() < 1)
  2078               if($db->numrows() < 1)
  2079               {
  2079               {
  2080                 $return['type'] = 'new';
  2080                 $return['type'] = 'new';
  2081                 $q = $db->sql_query('SELECT group_id,group_name FROM ' . table_prefix.'groups WHERE group_id=\''.intval($parms['target_id']).'\';');
  2081                 $q = $db->sql_query('SELECT group_id,group_name FROM ' . table_prefix.'groups WHERE group_id=\''.intval($parms['target_id']).'\';');
  2082                 if(!$q)
  2082                 if(!$q)
  2083                   return(Array('mode'=>'error','error'=>mysql_error()));
  2083                   return(Array('mode'=>'error','error'=>$db->get_error()));
  2084                 if($db->numrows() < 1)
  2084                 if($db->numrows() < 1)
  2085                   return Array('mode'=>'error','error'=>$lang->get('acl_err_bad_group_id'));
  2085                   return Array('mode'=>'error','error'=>$lang->get('acl_err_bad_group_id'));
  2086                 $row = $db->fetchrow();
  2086                 $row = $db->fetchrow();
  2087                 $return['target_name'] = $row['group_name'];
  2087                 $return['target_name'] = $row['group_name'];
  2088                 $return['target_id'] = intval($row['group_id']);
  2088                 $return['target_id'] = intval($row['group_id']);
  2089                 $return['current_perms'] = $session->acl_types;
  2089                 $return['current_perms'] = array();
  2090               }
  2090               }
  2091               else
  2091               else
  2092               {
  2092               {
  2093                 $return['type'] = 'edit';
  2093                 $return['type'] = 'edit';
  2094                 $row = $db->fetchrow();
  2094                 $row = $db->fetchrow();
  2095                 $return['target_name'] = $row['group_name'];
  2095                 $return['target_name'] = $row['group_name'];
  2096                 $return['target_id'] = intval($row['group_id']);
  2096                 $return['target_id'] = intval($row['group_id']);
  2097                 $return['current_perms'] = $session->acl_merge($session->acl_types, $session->string_to_perm($row['rules']));
  2097                 $return['current_perms'] = $session->string_to_perm($row['rules']);
  2098               }
  2098               }
  2099               $db->free_result();
  2099               $db->free_result();
  2100               // Eliminate types that don't apply to this namespace
  2100               // Eliminate types that don't apply to this namespace
  2101               if ( $namespace && $namespace != '__PageGroup' )
  2101               if ( $namespace && $namespace != '__PageGroup' )
  2102               {
  2102               {
  2127             return Array('mode'=>'error','error'=>$lang->get('acl_err_demo'));
  2127             return Array('mode'=>'error','error'=>$lang->get('acl_err_demo'));
  2128           }
  2128           }
  2129           $q = $db->sql_query('DELETE FROM ' . table_prefix.'acl WHERE target_type='.intval($parms['target_type']).' AND target_id='.intval($parms['target_id']).'
  2129           $q = $db->sql_query('DELETE FROM ' . table_prefix.'acl WHERE target_type='.intval($parms['target_type']).' AND target_id='.intval($parms['target_id']).'
  2130             ' . $page_where_clause_lite . ';');
  2130             ' . $page_where_clause_lite . ';');
  2131           if(!$q)
  2131           if(!$q)
  2132             return Array('mode'=>'error','error'=>mysql_error());
  2132             return Array('mode'=>'error','error'=>$db->get_error());
       
  2133           if ( sizeof ( $parms['perms'] ) < 1 )
       
  2134           {
       
  2135             // As of 1.1.x, this returns success because the rule length is zero if the user selected "inherit" in all columns
       
  2136             return Array(
       
  2137               'mode' => 'success',
       
  2138               'target_type' => $parms['target_type'],
       
  2139               'target_id' => $parms['target_id'],
       
  2140               'target_name' => $parms['target_name'],
       
  2141               'page_id' => $page_id,
       
  2142               'namespace' => $namespace,
       
  2143             );
       
  2144           }
  2133           $rules = $session->perm_to_string($parms['perms']);
  2145           $rules = $session->perm_to_string($parms['perms']);
  2134           if ( sizeof ( $rules ) < 1 )
       
  2135           {
       
  2136             return array(
       
  2137                 'mode' => 'error', 
       
  2138                 'error' => $lang->get('acl_err_zero_list')
       
  2139               );
       
  2140           }
       
  2141           $q = ($page_id && $namespace) ? 'INSERT INTO ' . table_prefix.'acl ( target_type, target_id, page_id, namespace, rules )
  2146           $q = ($page_id && $namespace) ? 'INSERT INTO ' . table_prefix.'acl ( target_type, target_id, page_id, namespace, rules )
  2142                                              VALUES( '.intval($parms['target_type']).', '.intval($parms['target_id']).', \'' . $db->escape($page_id) . '\', \'' . $db->escape($namespace) . '\', \'' . $db->escape($rules) . '\' )' :
  2147                                              VALUES( '.intval($parms['target_type']).', '.intval($parms['target_id']).', \'' . $db->escape($page_id) . '\', \'' . $db->escape($namespace) . '\', \'' . $db->escape($rules) . '\' )' :
  2143                                           'INSERT INTO ' . table_prefix.'acl ( target_type, target_id, rules )
  2148                                           'INSERT INTO ' . table_prefix.'acl ( target_type, target_id, rules )
  2144                                              VALUES( '.intval($parms['target_type']).', '.intval($parms['target_id']).', \'' . $db->escape($rules) . '\' )';
  2149                                              VALUES( '.intval($parms['target_type']).', '.intval($parms['target_id']).', \'' . $db->escape($rules) . '\' )';
  2145           if(!$db->sql_query($q)) return Array('mode'=>'error','error'=>mysql_error());
  2150           if(!$db->sql_query($q)) return Array('mode'=>'error','error'=>$db->get_error());
  2146           return Array(
  2151           return Array(
  2147               'mode' => 'success',
  2152               'mode' => 'success',
  2148               'target_type' => $parms['target_type'],
  2153               'target_type' => $parms['target_type'],
  2149               'target_id' => $parms['target_id'],
  2154               'target_id' => $parms['target_id'],
  2150               'target_name' => $parms['target_name'],
  2155               'target_name' => $parms['target_name'],
  2158             return Array('mode'=>'error','error'=>$lang->get('acl_err_demo'));
  2163             return Array('mode'=>'error','error'=>$lang->get('acl_err_demo'));
  2159           }
  2164           }
  2160           $q = $db->sql_query('DELETE FROM ' . table_prefix.'acl WHERE target_type='.intval($parms['target_type']).' AND target_id='.intval($parms['target_id']).'
  2165           $q = $db->sql_query('DELETE FROM ' . table_prefix.'acl WHERE target_type='.intval($parms['target_type']).' AND target_id='.intval($parms['target_id']).'
  2161             ' . $page_where_clause_lite . ';');
  2166             ' . $page_where_clause_lite . ';');
  2162           if(!$q)
  2167           if(!$q)
  2163             return Array('mode'=>'error','error'=>mysql_error());
  2168             return Array('mode'=>'error','error'=>$db->get_error());
  2164           return Array(
  2169           return Array(
  2165               'mode' => 'delete',
  2170               'mode' => 'delete',
  2166               'target_type' => $parms['target_type'],
  2171               'target_type' => $parms['target_type'],
  2167               'target_id' => $parms['target_id'],
  2172               'target_id' => $parms['target_id'],
  2168               'target_name' => $parms['target_name'],
  2173               'target_name' => $parms['target_name'],
  2315         $parser = $template->makeParserText( $response['template']['acl_field_item'] );
  2320         $parser = $template->makeParserText( $response['template']['acl_field_item'] );
  2316         $cls = 'row2';
  2321         $cls = 'row2';
  2317         foreach ( $response['acl_types'] as $acl_type => $value )
  2322         foreach ( $response['acl_types'] as $acl_type => $value )
  2318         {
  2323         {
  2319           $vars = Array(
  2324           $vars = Array(
       
  2325               'FIELD_INHERIT_CHECKED' => '',
  2320               'FIELD_DENY_CHECKED' => '',
  2326               'FIELD_DENY_CHECKED' => '',
  2321               'FIELD_DISALLOW_CHECKED' => '',
  2327               'FIELD_DISALLOW_CHECKED' => '',
  2322               'FIELD_WIKIMODE_CHECKED' => '',
  2328               'FIELD_WIKIMODE_CHECKED' => '',
  2323               'FIELD_ALLOW_CHECKED' => '',
  2329               'FIELD_ALLOW_CHECKED' => '',
  2324             );
  2330             );
  2325           $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
  2331           $cls = ( $cls == 'row1' ) ? 'row2' : 'row1';
  2326           $vars['ROW_CLASS'] = $cls;
  2332           $vars['ROW_CLASS'] = $cls;
  2327           
  2333           
  2328           switch ( $response['current_perms'][$acl_type] )
  2334           switch ( $response['current_perms'][$acl_type] )
  2329           {
  2335           {
       
  2336             case 'i':
       
  2337             default:
       
  2338               $vars['FIELD_INHERIT_CHECKED'] = 'checked="checked"';
       
  2339               break;
  2330             case AUTH_ALLOW:
  2340             case AUTH_ALLOW:
  2331               $vars['FIELD_ALLOW_CHECKED'] = 'checked="checked"';
  2341               $vars['FIELD_ALLOW_CHECKED'] = 'checked="checked"';
  2332               break;
  2342               break;
  2333             case AUTH_WIKIMODE:
  2343             case AUTH_WIKIMODE:
  2334               $vars['FIELD_WIKIMODE_CHECKED'] = 'checked="checked"';
  2344               $vars['FIELD_WIKIMODE_CHECKED'] = 'checked="checked"';
  2335               break;
  2345               break;
  2336             case AUTH_DISALLOW:
  2346             case AUTH_DISALLOW:
  2337             default:
       
  2338               $vars['FIELD_DISALLOW_CHECKED'] = 'checked="checked"';
  2347               $vars['FIELD_DISALLOW_CHECKED'] = 'checked="checked"';
  2339               break;
  2348               break;
  2340              case AUTH_DENY:
  2349              case AUTH_DENY:
  2341               $vars['FIELD_DENY_CHECKED'] = 'checked="checked"';
  2350               $vars['FIELD_DENY_CHECKED'] = 'checked="checked"';
  2342               break;
  2351               break;