cb4d97e1c6a096cc55e4ec1cbef0889287ef914f
[atutor.git] / mods / wiki / plugins / sql / arrowadmin.php
1 <?
2 /////////////////////////////////////////////
3 /*
4 Small wiki admin for ErfurtWiki using mysql
5 can delete pages, and rename pages, with the option of replacing the renamed page with a reference to the new one
6 made for The Arrow Project site.
7 Several parts of this are Arrow Specific. 
8 I've commented them with ARROW in the line so people can adapt it to use outside arrow.
9
10 In the spirit of ErfurtWiki this page is Public Domain.
11
12 Menno Lodder
13 menno_lodder at hotmail dot com
14 www.arrowproject.net
15
16 */
17 //////////////////////////////////////////////
18
19
20 //ARROW some protection to ensure libraries aren't called directly
21 define('IN_ARROW', true);
22
23 //ARROW includes libraries and shows the admin section headers, also opens html tags upto <body> 
24 require_once('header.php');
25
26 //ARROW if statement checks if the persion has permission
27 if(!has_perm("WIKI_ADMIN"))
28 {
29         echo("No permission");
30         include('footer.php');
31         exit();
32 }
33 //**************************************
34 //ARROW to use this outside of arrow, comment out the above and the footer.php include at the bottom
35 //      then uncomment the following 2 methods, and set the settings variables.
36 //**************************************
37
38 $wikitable = "wiki"; //the table name of the table containing the wiki info (ewiki by default)
39 $wikiurl = "../wiki.php"; //the relative URL of the wiki pages to the admin page, this is used to allow linking to the wiki pages
40
41 /*
42 These functions are untested
43
44 //logs a message
45 function logMsg($type, $message)
46 {
47         //log whatever you want here, or do nothing
48 }
49
50 //connects to the db
51 function connect_db()
52 {
53         //set these variables
54         $db = "dbname";
55         $dbhost = "localhost";
56         $dbuser = "username";
57         $dbpass = "password";
58         
59         $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die("Error - Connection to database is not established !");
60         @mysql_select_db($db, $conn) or die("Error - Can't open the database !");       
61 }
62 */
63
64
65 $showdetails = false;
66 $showindex = false;
67
68 //set the page in advance, so it can be changed by the functions below
69 $page = false;
70 if(isset($_REQUEST['page']))
71 {
72         $page = $_REQUEST['page'];
73 }
74
75 if(isset($_REQUEST['function']))
76 {
77         if($_REQUEST['function'] == "details")
78         {
79                 $showdetails = true;
80         }
81         else if(($_REQUEST['function'] == "rename") && isset($_REQUEST['oldname']) && isset($_REQUEST['newname']))
82         {
83                 $old = $_REQUEST['oldname'];
84                 $new = $_REQUEST['newname'];
85
86                 $shadow = isset($_REQUEST['leaveshadow']);
87                 
88                 $shadowtext = "not making shadow topic";
89                 if($shadow)
90                 {
91                         $shadowtext = "making shadow topic";
92                 }
93                 //ARROW makes a database connection to the mysql database (which is from now on the default one)
94                 connect_db();
95                 //check if the name already exists
96                 $checkresult = mysql_query("SELECT * FROM $wikitable WHERE pagename = '".addslashes($new)."'") or die("Error check if name already exists: ".mysql_error());
97                 if(!mysql_fetch_assoc($checkresult))
98                 {
99                         
100                         //get the last version of the old page
101                         $oldresult = mysql_query("SELECT * FROM $wikitable WHERE pagename = '".addslashes($old)."' ORDER BY version DESC LIMIT 1") or die("Error check if name already exists: ".mysql_error());
102                         
103                         if($oldpage = mysql_fetch_assoc($oldresult))
104                         {
105                                 //old page exists
106                                 mysql_query("UPDATE $wikitable SET pagename= '".addslashes($new)."' WHERE pagename= '".addslashes($old)."'") or die("Error renaming the page: ".mysql_error());
107                                 echo("<b>Renamed $old to $new $shadowtext</b>,<br />");
108                                 
109                                 //ARROW logs a message under the header "wikiadmin"
110                                 logMsg("wikiadmin",     "Renamed $old to $new $shadowtext");
111                                         
112                                 if($shadow)
113                                 {
114                                         $shadowcontent = "This page was renamed to [$new]";
115                                         //this just takes the last author and meta info, cause thats rather complicated to change
116                                         mysql_query("INSERT INTO $wikitable (pagename, version, flags, content, author, created, lastmodified, refs, meta, hits)".
117                                                                 " VALUES ('".addslashes($old)."', '".($oldpage['version']+1)."', '".$oldpage['flags']."', '".
118                                                                 addslashes($shadowcontent)."', '".$oldpage['author']."', '".$oldpage['created']."', '".time()."', '"."\n\n".addslashes($new)."\n\n\n"."', '".
119                                                                 $oldpage['meta']."', '".$oldpage['hits']."')") or die("Error making shadow page: ".mysql_error());
120                                 }
121                 }
122                 else
123                 {
124                         echo("<b>Page named $old not found.</b><br />");
125                 }
126                 }
127                 else
128                 {
129                         //name already exists
130                         echo("<b>A page named $new already exists</b><br />");
131         }
132         
133         //change the page to show
134         $page = $new;
135         $showdetails = true;
136         }
137         elseif(($_REQUEST['function'] == "delete") && isset($_REQUEST['page']))
138         {
139                 $page = $_REQUEST['page'];
140                 
141                 mysql_query("DELETE FROM $wikitable WHERE pagename = '".addslashes($page)."'") or die("Error deleting page: ".mysql_error());
142                 
143                 echo("<br /><b>Deleted $page</b>");
144         }
145         else
146         {
147                 echo("Unknown function or not enough parameters");
148         }
149         
150 }
151 else
152 {
153         //no function show index
154         $showindex = true;
155 }
156
157 if($showdetails)
158 {
159         //show the details of a page
160         
161         //check if a page is given
162         if($page)
163         {
164                 connect_db();
165                 //select the pages with the given name, first is the most recent one
166                 $pagequery = "SELECT * FROM $wikitable WHERE pagename = '".addslashes($page)."' ORDER BY version DESC";
167                 $pageresult = mysql_query($pagequery) or die("Error getting page: ".mysql_error());
168                 
169                 if($recentpagerow =  mysql_fetch_assoc($pageresult))
170                 {
171                         echo("<br /><a href=\"wikiadmin.php\">Wiki Index</a>");
172                         echo("<br /><h2><a target=\"blank\" href=\"$wikiurl?id=".urlencode($recentpagerow['pagename'])."\">".$recentpagerow['pagename']."</a></h2>\n");
173                         echo("<b>Rename</b>\n");
174                         echo("<form method=\"post\"><input type=\"text\" name=\"newname\" size=\"30\" value=\"".$recentpagerow['pagename']."\" />&nbsp;\n");
175                         echo("<input type=\"checkbox\" name=\"leaveshadow\" value=\"true\" checked=\"checked\" />Leave shadow page&nbsp;\n");
176                         echo("<input value=\"Rename\" type=\"submit\" /><input type=\"hidden\" name=\"oldname\" value=\"".$recentpagerow['pagename']."\" /><input type=\"hidden\" name=\"function\" value=\"rename\" /></form>\n");
177                         echo("<small>Shadow page is a page in the place of the old name, that points to the new name.</small>");
178                         echo("<br /><br />\n");
179                         echo("<form method=\"post\" onSubmit=\"return confirm('Are you sure you want permanently delete this page?');\">\n");
180                         echo("<input value=\"Delete\"  type=\"submit\" /><input type=\"hidden\" name=\"function\" value=\"delete\" />\n");
181                         echo("<input type=\"hidden\" name=\"page\" value=\"".$recentpagerow['pagename']."\" />\n");
182                         echo("</form>");
183                         //put pointer back to start of results
184                         mysql_data_seek($pageresult, 0);
185                 }
186                 else
187                 {
188                         echo("No page found with that title: ". $page); 
189         }
190         }
191         else
192         {
193                 //no pagename given
194                 echo("No page field found");
195         }       
196 }
197
198 if($showindex)
199 {
200         //show index
201
202         //ARROW makes a database connection to the mysql database (which is from now on the default one)
203         connect_db();
204         $pagesquery = "SELECT pagename, MAX(version) AS version, MAX(lastmodified) AS lastmodified FROM $wikitable GROUP BY pagename";
205         $pagesresult = mysql_query($pagesquery) or die("Error getting pages: ".mysql_error());
206         
207         //echo the table
208         echo("<table>\n");
209         echo("<tr>\n");
210         echo("<th>Name</th><th>view</th><th>Version</th><th>LastUpdate</th>\n");
211         echo("</tr>\n");
212         while($row = mysql_fetch_assoc($pagesresult))
213         {
214                 $name = $row['pagename'];
215                 $time = timeString($row['lastmodified']);
216                 $version = $row['version'];
217                 
218                 echo("<tr>\n");
219                 echo("<td><a href=\"wikiadmin.php?function=details&page=".urlencode($name)."\">".htmlspecialchars($name)."</a></td>\n");
220                 //ARROW ../wiki.php?id= is the base of the link to the added wiki page.
221                 echo("<td><a target=\"_blank\" href=\"$wikiurl?id=".urlencode($name)."\">view</a></td>\n");
222                 echo("<td>".$version."</td>\n");
223                 echo("<td>".$time."</td>\n");           
224                 echo("</tr>\n");
225         }
226         echo("</table>\n");
227 }       
228
229
230 //ARROW closes all html of the header.php
231 include('footer.php');
232 ?>