PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wiki/index.php

https://bitbucket.org/claws96/public_stuff
PHP | 507 lines | 295 code | 127 blank | 85 comment | 20 complexity | 868182933a88d708d363ec26041d23b3 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /*
  3. 11/09/04 - 1st recorded revision. Still not complete, but mostly
  4. working (PNM).
  5. 11/12/04 - Added delete functionality (PNM).
  6. 11/13/04 - Fixed delete function. Added tentative error handling (PNM).
  7. 4/1/06 - Finished the darn thing. Put refreshes in place after the additions/deletions
  8. so that it's not so dumb.
  9. 4/9/06 - Moved some stuff around. Created a master includes file (includes.php), added dates
  10. to topic index.
  11. 4/11/06 - Put "number of entries" listing in main wiki index for each topic.
  12. 4/16/06 - Started to add mimetypes feature
  13. 6/7/06 - Cleaned up SQL injection stuff
  14. 6/12/06 - More SQL injection fixes
  15. 6/17/06 - Search function
  16. 10/5/06 - htmlspecialchars additions
  17. 7/07 - CSS style makeover ( font, background, textboxes )
  18. 11/12 - CSS conversion from tables to DIV
  19. */
  20. require_once( "includes.php" );
  21. $db = "clawshos_wiki";
  22. $host = "localhost";
  23. $port = "3306";
  24. $user = "clawshos_wikiuser";
  25. $pass = "demo99";
  26. $dbRes = connectDB( $host . ":" . $port, $user, $pass, $db );
  27. /* Make sure we don't cut off any text: */
  28. $_POST[ 'entry_text' ] = htmlspecialchars( $_POST[ 'entry_text' ] );
  29. $_POST[ 'wiki_topic_text' ] = htmlspecialchars( $_POST[ 'wiki_topic_text' ] );
  30. sanitizeInput();
  31. switch( $_POST[ 'wikiaction' ] ) {
  32. case "addnewentry":
  33. wikiNew( "savenewentry", sprintf( "%d", $_POST[ 'wikitopicid' ] ) );
  34. break;
  35. case "delentry":
  36. wikiDeleteEntry( sprintf( "%d", $_POST[ 'wikitopicid' ] ) );
  37. break;
  38. case "changeentry":
  39. print( "Change goes here." );
  40. break;
  41. case "pushattachment":
  42. wikiSpawnDownload( $null, $null );
  43. break;
  44. case "showdetail":
  45. if( sprintf( "%d", $_POST[ 'topicID' ] ) == 0 )
  46. wikiNew( "savenew", 0 );
  47. else
  48. wikiDetail( sprintf( "%d", $_POST[ 'topicID' ] ) );
  49. break;
  50. case "savenew":
  51. wikiSaveEntry( wikiSaveTopic( ) );
  52. break;
  53. case "savenewentry":
  54. wikiSaveEntry( sprintf( "%d", $_POST[ 'wikitopicid' ] ) );
  55. break;
  56. case "showsearchform":
  57. print( wikiForm( "dosearch", "s", "", "Search" ) );
  58. break;
  59. case "dosearch":
  60. //$qt = "SELECT print_r( $_POST );
  61. die();
  62. break;
  63. default:
  64. wikiIndex( );
  65. break;
  66. }
  67. function wikiPageHeader( $section, $sheetName ) {
  68. $return = ("<head>
  69. <title>
  70. Wiki Database - {$section}
  71. </title>
  72. <link rel='stylesheet' href='{$sheetName}'>
  73. </head>");
  74. return $return;
  75. }
  76. function wikiDeleteEntry( $topicEntryID ) {
  77. global $db;
  78. /* Find the parent topic id, for the next step */
  79. $query = mysql_query( "SELECT id,topic_id from {$db}.wiki_entry WHERE id='{$topicEntryID}' LIMIT 1" );
  80. $topicID = array_pop( mysql_fetch_row( $query ) );
  81. /**/
  82. $query = "DELETE FROM {$db}.wiki_entry WHERE id='{$topicEntryID}' LIMIT 1";
  83. $successFlag = 0; /* Yuck */
  84. if( mysql_query( $query ) ) {
  85. /* Is the topic empty of entries? */
  86. $query = "SELECT id,topic_id FROM {$db}.wiki_entry WHERE
  87. topic_id='{$topicID}' and 1=1";
  88. if( mysql_num_rows( mysql_query( $query ) ) == 0 ) {
  89. /* If so, delete the topic */
  90. $q = "DELETE FROM {$db}.wiki_topic WHERE id='{$topicID}' LIMIT 1";
  91. if( mysql_query( $q ) )
  92. $successFlag += 1;
  93. }
  94. else
  95. $successFlag += 1;
  96. if( $successFlag == 1 ) {
  97. header( "Refresh: 2; URL=\"{$myUrl}\"" );
  98. print( "<h5>Entry deleted successfully.</h5>" );
  99. }
  100. else
  101. errorMessage( );
  102. }
  103. else
  104. errorMessage( );
  105. }
  106. function wikiSaveTopic( ) {
  107. global $db;
  108. /* Saves topic and returns the resulting id for adding
  109. entries under that topic.
  110. */
  111. $date = getUnixtime( );
  112. $topicQuery = "INSERT INTO {$db}.wiki_topic SET id='',
  113. text='{$_POST[ 'wiki_topic_text' ]}',
  114. date='{$date}'";
  115. $topicResult = mysql_query( $topicQuery );
  116. return mysql_insert_id( );
  117. }
  118. function wikiSaveEntry( $topicID ) {
  119. global $db;
  120. /* Put new entry into database */
  121. $date = getUnixtime( );
  122. if( $topicID > 0 ) { /* Error handling...should never be zero */
  123. $contentQuery = "INSERT INTO {$db}.wiki_entry SET id='',
  124. topic_id='{$topicID}',
  125. text='{$_POST[ 'entry_text' ]}',
  126. date='{$date}'";
  127. $contentResult = mysql_query( $contentQuery );
  128. }
  129. /*
  130. if( isset( $_FILES ) && $_FILES[ 'error' ] == 0 ) {
  131. //uploadFile( $_FILES[ 'entry_file'], mysql_insert_id(), "wiki", "wiki_upload" );
  132. uploadFile( $_FILES[ 'entry_file'], 1, "wiki", "wiki_upload" );
  133. }
  134. if( isset( $_FILES[ 'error' ] ) ) { die( $_FILES[ 'error' ] ); }
  135. }
  136. */
  137. if( $topicID > 0 && $contentResult ) {
  138. header( "Refresh: 2; URL=\"{$_SERVER[ 'PHP_SELF' ]}\"" );
  139. print( "<h5>Entry " .mysql_insert_id() . " added successfully.</h5>\n" );
  140. }
  141. else
  142. errorMessage();
  143. }
  144. function wikiForm( $action, $topicID, $formData, $buttonText = "Add" ) {
  145. /* Draws the actual input form and related Javascript so that data can be entered/edited */
  146. /* Use JS to enter return data in hidden form vars, so we can use
  147. POST method. The check is currently broken in regard to
  148. the handling of the textarea (always returns true, to allow
  149. form to work.
  150. */
  151. $returnVal = ( "<script type='text/javascript' language='Javascript'>\n
  152. <!---\n
  153. function validateThis() {\n
  154. if( document.new_wiki.wiki_topic_text.value &&
  155. document.new_wiki.entry_text.value ) {\n
  156. document.new_wiki.wikiaction.value='{$action}';\n
  157. document.new_wiki.submit();\n
  158. }\n
  159. else {\n
  160. alert( 'Missing form value!' );\n
  161. }\n
  162. }\n
  163. //-->\n
  164. </script>\n" );
  165. /* The formdata[] array contains two indexes, 0 == wiki topic text,
  166. and 1 == wiki content text for the current topic
  167. */
  168. //$buttonText = ( strlen( $formData[ 1 ] ) == 0 ) ? "Add" : "Modify"; /* (Think that'll get it) */
  169. if( $topicID < 1 )
  170. $formData[ 0 ] = "(new entry)";
  171. else
  172. $disabledFlag = "DISABLED";
  173. $returnVal .= ( "<FORM name='new_wiki'
  174. method='post'
  175. action='{$_SERVER[ 'PHP_SELF' ]}'
  176. enctype='multipart/form-data'>\n
  177. <DIV style='width:840px;'>
  178. <SPAN style='float:left;'>
  179. <INPUT TYPE='text' NAME='wiki_topic_text' VALUE='{$formData[0]}' {$disabledFlag} >
  180. </SPAN>
  181. <SPAN style='float:right;'>
  182. <INPUT TYPE='button' VALUE='{$buttonText}' onClick='validateThis()'>
  183. </SPAN>\n
  184. <SPAN style='float:clear;'>
  185. <TEXTAREA name='entry_text' class='newentry'
  186. rows='10'
  187. cols='80'>{$formData[ 1 ]}</TEXTAREA>
  188. <INPUT TYPE='hidden' NAME='wikiaction' VALUE=''>
  189. <INPUT TYPE='hidden' NAME='wikitopicid' VALUE='{$topicID}'>
  190. </SPAN>
  191. </DIV>" );
  192. /* Save for later
  193. <TR>
  194. <TD>
  195. Upload Content:
  196. </TD>
  197. <TD>
  198. <INPUT TYPE='file' NAME='entry_file'>
  199. <INPUT TYPE='hidden' NAME='MAX_FILE_SIZE' VALUE='20000000'>
  200. </TD>
  201. </TR>
  202. */
  203. return( $returnVal );
  204. }
  205. function wikiNew( $action, $topicID ) {
  206. global $db;
  207. /* Create a new topic */
  208. if( $topicID > 0 ) {
  209. $topicQ = "SELECT id,text,date FROM {$db}.wiki_topic WHERE id='{$topicID}' and 1=1";
  210. $topic = mysql_fetch_assoc( mysql_query( $topicQ ) );
  211. }
  212. print( "<HTML>\n" .
  213. wikiPageHeader( "New Entry", "wiki.css" ) .
  214. "<BODY>\n" );
  215. /* Topic won't have detail, since it's **new** */
  216. print( wikiForm( $action, $topicID, array( $topic[ 'text' ], "" ) ) );
  217. print( "</BODY></HTML>\n" );
  218. }
  219. function wikiIndex( ) {
  220. global $db;
  221. /* Displays topics in database */
  222. $res = mysql_query( "SELECT id,text,date FROM {$db}.wiki_topic ORDER BY text" );
  223. print( "<HTML>\n" .
  224. wikiPageHeader( "New Entry", "wiki.css" ) .
  225. "<BODY>\n" );
  226. while ( $row = mysql_fetch_assoc( $res ) ) {
  227. $numEntries = mysql_num_rows( mysql_query( "SELECT id,topic_id FROM {$db}.wiki_entry
  228. WHERE topic_id='{$row[ 'id' ]}' ORDER BY id DESC" ) );
  229. print( "<DIV style='width:840px;'>
  230. <A CLASS='topic' HREF=\"javascript:jumpLink('{$row[ 'id' ]}')\">
  231. {$row['text']}
  232. </A> ( " .
  233. showTimeStamp( $row[ 'date' ] ) . ", " . $numEntries . " entries )
  234. </DIV>\n
  235. ");
  236. }
  237. print( "<P>\n" );
  238. print( "<A CLASS='topic' HREF=\"javascript:jumpLink('0')\">New wiki topic</A>\n" );
  239. print( "<A CLASS='topic' HREF=\"javascript:jumpLink('s')\">Search</A>\n" );
  240. print( "<FORM ACTION='{$_SERVER[ 'PHP_SELF' ]}' METHOD='post' NAME='wikiIndex'>\n
  241. <INPUT TYPE='hidden' NAME='wikiaction'>\n
  242. <INPUT TYPE='hidden' NAME='topicID'>\n
  243. </FORM>\n" );
  244. print( "<script type='text/javascript' language='Javascript'>\n
  245. <!---\n
  246. function jumpLink( linkID ) {\n
  247. document.wikiIndex.topicID.value = linkID;\n
  248. if( linkID == 's' ) {\n
  249. document.wikiIndex.wikiaction.value = 'showsearchform';\n
  250. }\n
  251. else {
  252. document.wikiIndex.wikiaction.value = 'showdetail';\n
  253. }\n
  254. document.wikiIndex.submit();\n
  255. }\n
  256. -->\n
  257. </script>\n
  258. \n" );
  259. print( "</BODY></HTML>\n" );
  260. }
  261. function wikiDetail( $indexID ) {
  262. global $db;
  263. /* Displays entries in wiki database according to topic. */
  264. $indexID = sprintf( "%d", $indexID );
  265. $detailQ = "SELECT id,topic_id,text,date FROM {$db}.wiki_entry WHERE topic_id='{$indexID}' ORDER BY id DESC";
  266. $detailRes = mysql_query( $detailQ );
  267. $topicQ = "SELECT id,text,date FROM {$db}.wiki_topic WHERE id='{$indexID}' and 1=1";
  268. $topic = mysql_fetch_array( mysql_query( $topicQ ) );
  269. $numEntries = mysql_num_rows( $detailRes );
  270. $dateStamp = showTimeStamp( $topic[ 'date' ] );
  271. print( "<HTML>\n" .
  272. wikiPageHeader( "Topic Headings", "wiki.css" ) .
  273. "<BODY>\n"
  274. );
  275. /* Set up the form for the topic heading, and the JS for the
  276. various functions.
  277. */
  278. print( "<FORM action='' method='post' name='wikiDetail'>
  279. <INPUT type='hidden' name='wikitopicid' value=''>
  280. <INPUT type='hidden' name='wikiaction' value=''>
  281. </FORM>
  282. <DIV style='width:840px;'>
  283. <SPAN class='header_div'>
  284. {$topic[ 'text' ]}
  285. </SPAN>
  286. <SPAN class='header_div'>
  287. {$dateStamp}
  288. </SPAN>
  289. <SPAN class='header_div'>
  290. {$numEntries} entries
  291. </SPAN>
  292. <SPAN class='rightbutton_div'>
  293. <A HREF='{$_SERVER[ 'PHP_SELF' ]}'>Main</A>
  294. </SPAN>
  295. <SPAN class='rightbutton_div'>
  296. <A HREF='javascript:newEntry({$indexID})'>Add new entry</A>
  297. </SPAN>
  298. </DIV>\n" );
  299. /* Now for the entries, sorted in reverse order (newest first) */
  300. $rowNum = $numEntries;
  301. while ( $row = mysql_fetch_array( $detailRes ) ) {
  302. $text = stripslashes( $row[ 'text' ] ) . "\n";
  303. $dateStamp = showTimeStamp( $row[ 'date' ] );
  304. print( "<DIV class='spacer'>&nbsp;</DIV>\n" );
  305. print( "<DIV style='width:840px;'>
  306. <SPAN class='header_div'>
  307. Entry {$rowNum}/{$numEntries}, ${dateStamp}
  308. </SPAN>
  309. <SPAN class='rightbutton_div'>
  310. <A HREF='javascript:editEntry({$row[ 'id' ]})'>Edit</A>
  311. </SPAN>
  312. <SPAN class='rightbutton_div'>
  313. <A HREF='javascript:deleteEntry({$row[ 'id' ]})'>Delete</A>
  314. </SPAN>
  315. <SPAN class='rightbutton_div'>
  316. <A HREF='javascript:explodeEntry(\"entry{$row[ 'id' ]}\")'>Show in new window</A>
  317. </SPAN>
  318. <TEXTAREA class='newentry' ROWS='20' COLS='80' NAME='entry{$row[ 'id' ]}'>{$text}</TEXTAREA>
  319. </DIV>
  320. \n" );
  321. --$rowNum;
  322. }
  323. print( "<script type='text/javascript' language='Javascript'>
  324. <!---
  325. function newEntry( id ) {
  326. document.wikiDetail.wikiaction.value = 'addnewentry';
  327. document.wikiDetail.wikitopicid.value = '{$indexID}';
  328. document.wikiDetail.submit();
  329. }
  330. function editEntry( id ) {
  331. document.wikiDetail.wikiaction.value = 'changeentry';
  332. document.wikiDetail.wikitopicid.value = '{$indexID}';
  333. document.wikiDetail.submit();
  334. }
  335. function explodeEntry( textAreaName ) {
  336. win = window.open( \"menubar=no,location=no,resizable=no,scrollbars=yes,status=no,width=400,height=300,screenX=150,screenY=150,top=150,left=150\" );
  337. win.document.writeln( '<PRE>' );
  338. win.document.writeln( document.getElementsByName( textAreaName )[0].value );
  339. win.document.writeln( '</PRE>' );
  340. }
  341. function deleteEntry( id ) {
  342. document.wikiDetail.wikiaction.value = 'delentry';
  343. document.wikiDetail.wikitopicid.value = id;
  344. document.wikiDetail.submit();
  345. }
  346. function pushDownload( id ) {
  347. document.wikiDetail.wikiaction.value = 'delentry';
  348. document.wikiDetail.wikitopicid.value = id;
  349. document.wikiDetail.submit();
  350. }
  351. -->
  352. </script>\n" );
  353. print( "</BODY></HTML>\n" );
  354. }
  355. function wikiSearchEntry( ) {
  356. print("");
  357. }
  358. function wikiSpawnDownload( $data, $contentType ) {
  359. echo "for later.";
  360. }
  361. ?>