/lib/OpenWiki.class.php

https://github.com/andreassolberg/dokuwikiadmin · PHP · 302 lines · 193 code · 62 blank · 47 comment · 19 complexity · 2de212b70624d600d0126138075ead72 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. */
  5. class OpenWiki {
  6. private $identifier;
  7. private $name;
  8. private $descr;
  9. private $owner;
  10. /**
  11. * 0 Private
  12. * 1 All feide users can read, no anonymous access
  13. * 2 Anonymous users can read
  14. * 3 Feide users can write, no anonymous access
  15. * 4 Feide users can write, anonymous users can read
  16. */
  17. private $access = 0;
  18. private $customacl = array();
  19. private $loadedFromDB = false;
  20. function __construct($identifier, $db = null) {
  21. $this->identifier = $identifier;
  22. if (!empty($db)) {
  23. $this->db = $db;
  24. $this->loadFromDB();
  25. }
  26. }
  27. function setInfo($name, $descr, $owner, $access) {
  28. $this->name = $name;
  29. $this->descr = $descr;
  30. $this->owner = $owner;
  31. $this->access = $access;
  32. }
  33. public function isLoaded() {
  34. return $this->loadedFromDB;
  35. }
  36. public function loadFromDB() {
  37. $sql ="SELECT * FROM openwiki WHERE id = '" . $this->getIdentifier() . "'";
  38. $result = mysql_query($sql, $this->db);
  39. if(!$result){
  40. throw new Exception ("Could not successfully run query ($sql) fromDB:" . mysql_error());
  41. }
  42. if(mysql_num_rows($result) > 0){
  43. $row = mysql_fetch_assoc($result);
  44. $this->setInfo($row['name'], $row['descr'], $row['owner'], $row['access']);
  45. $this->loadACLfromDB();
  46. $this->loadedFromDB = true;
  47. }
  48. mysql_free_result($result);
  49. }
  50. private function loadACLfromDB() {
  51. $link = $this->getDBhandle();
  52. $sql ="SELECT *
  53. FROM acl
  54. WHERE wikiid='" . $this->getIdentifier() . "'
  55. ORDER BY priority";
  56. $result = mysql_query($sql, $this->db);
  57. if(!$result){
  58. throw new Exception ("Could not successfully run query ($sql) from DB:" . mysql_error());
  59. }
  60. if(mysql_num_rows($result) > 0){
  61. while($row = mysql_fetch_assoc($result)){
  62. $this->addACL($row['name'], $row['access']);
  63. }
  64. }
  65. mysql_free_result($result);
  66. }
  67. public function setDBhandle($db) {
  68. $this->db = $db;
  69. }
  70. // TODO: addslashes
  71. public function savetoDB() {
  72. /*
  73. id varchar(100) NOT NULL PRIMARY KEY,
  74. name tinytext,
  75. descr text,
  76. owner tinytext,
  77. access int
  78. */
  79. $link = $this->getDBhandle();
  80. if ($this->isLoaded() ) {
  81. $sql = "UPDATE openwiki SET
  82. name ='" . addslashes($this->getName()) . "',
  83. descr ='" . addslashes($this->getDescr()) . "',
  84. owner = '" . addslashes($this->getOwner()) . "',
  85. access = " . addslashes($this->getAccess()) . " WHERE id = '" . addslashes($this->getIdentifier()) . "'";
  86. $res = mysql_query($sql, $this->db);
  87. if(mysql_error()){
  88. throw new Exception('Invalid query: ' . mysql_error());
  89. }
  90. $this->deleteACLinDB();
  91. } else {
  92. $res = mysql_query("INSERT INTO openwiki (id, name, descr, owner, access) values ('" .
  93. addslashes($this->getIdentifier()) . "','" . addslashes($this->getName()) .
  94. "', '" . addslashes($this->getDescr()) . "', '" .
  95. addslashes($this->getOwner()) . "', " . addslashes($this->getAccess()) . ")", $this->db);
  96. if(mysql_error()){
  97. throw new Exception('Invalid query: ' . mysql_error());
  98. }
  99. }
  100. $this->saveACLtoDB();
  101. }
  102. // TODO: addslashes
  103. private function deleteACLinDB() {
  104. $link = $this->getDBhandle();
  105. $res = mysql_query("DELETE FROM acl WHERE wikiid='" . addslashes($this->getIdentifier()) . "'", $this->db);
  106. if(mysql_error()){
  107. throw new Exception('Invalid query: ' . mysql_error());
  108. }
  109. }
  110. // TODO: addslashes
  111. private function saveACLtoDB( ) {
  112. /*
  113. id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
  114. wikiid varchar(100) NOT NULL,
  115. name tinytext,
  116. access int,
  117. priority int
  118. */
  119. $link = $this->getDBhandle();
  120. foreach ($this->customacl AS $priority => $entry) {
  121. $res = mysql_query("INSERT INTO acl (wikiid, name, access, priority) values ('" .
  122. addslashes($this->getIdentifier()) . "','" . addslashes($entry[0]) . "', " .
  123. addslashes($entry[1]) . ", " . addslashes($priority) . ")", $this->db);
  124. if(mysql_error()){
  125. throw new Exception('Invalid query: ' . mysql_error());
  126. }
  127. }
  128. }
  129. private function getDBhandle() {
  130. return $this->db;
  131. }
  132. public function getIdentifier() {
  133. return $this->identifier;
  134. }
  135. public function getName() {
  136. return $this->name;
  137. }
  138. public function getDescr() {
  139. return $this->descr;
  140. }
  141. public function getOwner() {
  142. return $this->owner;
  143. }
  144. public function setOwner($owner) {
  145. $this->owner = $owner;
  146. }
  147. public function getAccess() {
  148. return $this->access;
  149. }
  150. public function publicACL() {
  151. $aclmap = array(
  152. 0 => 0,
  153. 1 => 0,
  154. 2 => 1,
  155. 3 => 0,
  156. 4 => 1
  157. );
  158. return $aclmap[$this->getAccess()];
  159. }
  160. public function feideACL() {
  161. $aclmap = array(
  162. 0 => 0,
  163. 1 => 1,
  164. 2 => 1,
  165. 3 => 32,
  166. 4 => 32
  167. );
  168. return $aclmap[$this->getAccess()];
  169. }
  170. public function addACL($groupid, $level) {
  171. if ($level > 32) throw new Exception('Invalid authentication level');
  172. $this->customacl[] = array($groupid, $level);
  173. }
  174. public function removeACL($no) {
  175. $newacl = array();
  176. foreach ($this->customacl AS $key => $entry) {
  177. if ($key != $no) $newacl[] = $entry;
  178. }
  179. $this->customacl = $newacl;
  180. }
  181. public function getCustomACL() {
  182. return $this->customacl;
  183. }
  184. public function swapACL($no) {
  185. $temp = $this->customacl[$no];
  186. $this->customacl[$no] = $this->customacl[$no+1];
  187. $this->customacl[$no+1] = $temp;
  188. }
  189. /**
  190. * Does nothing, but throws an exception when user is not the owner
  191. * of this wiki.
  192. */
  193. public function needAdminAccess($username) {
  194. if ($username != $this->getOwner())
  195. throw new Exception($username . ' is not the owner of this wiki.');
  196. }
  197. /**
  198. * Encode ASCII special chars
  199. *
  200. * Some auth backends allow special chars in their user and groupnames
  201. * The special chars are encoded with this function. Only ASCII chars
  202. * are encoded UTF-8 multibyte are left as is (different from usual
  203. * urlencoding!).
  204. *
  205. * Decoding can be done with rawurldecode
  206. *
  207. * @author Andreas Gohr <gohr@cosmocode.de>
  208. * @see rawurldecode()
  209. */
  210. private function auth_nameencode($name,$skip_group=false){
  211. global $cache_authname;
  212. $cache =& $cache_authname;
  213. $name = (string) $name;
  214. if (!isset($cache[$name][$skip_group])) {
  215. if($skip_group && $name{0} =='@'){
  216. $cache[$name][$skip_group] = '@'.preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e',
  217. "'%'.dechex(ord('\\1'))",substr($name,1));
  218. }else{
  219. $cache[$name][$skip_group] = preg_replace('/([\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f])/e',
  220. "'%'.dechex(ord('\\1'))",$name);
  221. }
  222. }
  223. return $cache[$name][$skip_group];
  224. }
  225. public function getACLdefinition() {
  226. $def = '# Wiki: ' . $this->getIdentifier() . "\r\n";
  227. $def .= $this->getIdentifier() . ':* @ALL ' . $this->publicACL() . "\r\n";
  228. $def .= $this->getIdentifier() . ':* @feideusers ' . $this->feideACL() . "\r\n";
  229. #$def .= $this->getIdentifier() . ':* @feideusers ' . '0' . "\r\n";
  230. foreach ($this->getCustomACL() AS $aclentry) {
  231. $def .= $this->getIdentifier() . ':* ' . $this->auth_nameencode($aclentry[0], true) . ' ' . $aclentry[1] . "\r\n";
  232. #$def .= $this->getIdentifier() . ':* ' . $this->auth_nameencode($aclentry[0], true) . ' ' . '1' . "\r\n";
  233. }
  234. #$def .= $this->getIdentifier() . ':* ' . $this->auth_nameencode($this->getOwner()) . ' 1' . "\r\n";
  235. $def .= $this->getIdentifier() . ':* ' . $this->auth_nameencode($this->getOwner()) . ' 32' . "\r\n";
  236. $def .= "\r\n\r\n";
  237. return $def;
  238. }
  239. }
  240. ?>