PageRenderTime 32ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/core/inc/bigtree/admin.php

https://github.com/philp/BigTree-CMS
PHP | 5582 lines | 2839 code | 674 blank | 2069 comment | 667 complexity | 0afe53e60456baae952d671ba41e975a MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?
  2. /*
  3. Class: BigTreeAdmin
  4. The main class used by the admin for manipulating and retrieving data.
  5. */
  6. class BigTreeAdmin {
  7. var $PerPage = 15;
  8. // !View Types
  9. var $ViewTypes = array(
  10. "searchable" => "Searchable List",
  11. "draggable" => "Draggable List",
  12. "images" => "Image List",
  13. "grouped" => "Grouped List",
  14. "images-grouped" => "Grouped Image List"
  15. );
  16. // !Reserved Column Names
  17. var $ReservedColumns = array(
  18. "id",
  19. "position",
  20. "archived",
  21. "approved"
  22. );
  23. // !View Actions
  24. var $ViewActions = array(
  25. "approve" => array(
  26. "key" => "approved",
  27. "name" => "Approve",
  28. "class" => "icon_approve icon_approve_on"
  29. ),
  30. "archive" => array(
  31. "key" => "archived",
  32. "name" => "Archive",
  33. "class" => "icon_archive"
  34. ),
  35. "feature" => array(
  36. "key" => "featured",
  37. "name" => "Feature",
  38. "class" => "icon_feature icon_feature_on"
  39. ),
  40. "edit" => array(
  41. "key" => "id",
  42. "name" => "Edit",
  43. "class" => "icon_edit"
  44. ),
  45. "delete" => array(
  46. "key" => "id",
  47. "name" => "Delete",
  48. "class" => "icon_delete"
  49. )
  50. );
  51. /*
  52. Constructor:
  53. Initializes the user's permissions.
  54. */
  55. function __construct() {
  56. if (isset($_SESSION["bigtree"]["email"])) {
  57. $this->ID = $_SESSION["bigtree"]["id"];
  58. $this->User = $_SESSION["bigtree"]["email"];
  59. $this->Level = $_SESSION["bigtree"]["level"];
  60. $this->Name = $_SESSION["bigtree"]["name"];
  61. $this->Permissions = $_SESSION["bigtree"]["permissions"];
  62. } elseif (isset($_COOKIE["bigtree"]["email"])) {
  63. $user = mysql_escape_string($_COOKIE["bigtree"]["email"]);
  64. $pass = mysql_escape_string($_COOKIE["bigtree"]["password"]);
  65. $f = sqlfetch(sqlquery("SELECT * FROM bigtree_users WHERE email = '$user' AND password = '$pass'"));
  66. if ($f) {
  67. $this->ID = $f["id"];
  68. $this->User = $user;
  69. $this->Level = $f["level"];
  70. $this->Name = $f["name"];
  71. $this->Permissions = json_decode($f["permissions"],true);
  72. $_SESSION["bigtree"]["id"] = $f["id"];
  73. $_SESSION["bigtree"]["email"] = $f["email"];
  74. $_SESSION["bigtree"]["level"] = $f["level"];
  75. $_SESSION["bigtree"]["name"] = $f["name"];
  76. $_SESSION["bigtree"]["permissions"] = $this->Permissions;
  77. }
  78. }
  79. }
  80. /*
  81. Function: archivePage
  82. Archives a page.
  83. Parameters:
  84. page - Either a page id or page entry.
  85. Returns:
  86. true if successful. false if the logged in user doesn't have permission.
  87. See Also:
  88. <archivePageChildren>
  89. */
  90. function archivePage($page) {
  91. global $cms;
  92. if (is_array($page)) {
  93. $page = mysql_real_escape_string($page["id"]);
  94. } else {
  95. $page = mysql_real_escape_string($page);
  96. }
  97. $access = $this->getPageAccessLevel($page);
  98. if ($access == "p" && $this->canModifyChildren($cms->getPage($page))) {
  99. sqlquery("UPDATE bigtree_pages SET archived = 'on' WHERE id = '$page'");
  100. $this->archivePageChildren($page);
  101. $this->growl("Pages","Archived Page");
  102. $this->track("bigtree_pages",$page,"archived");
  103. return true;
  104. }
  105. return false;
  106. }
  107. /*
  108. Function: archivePageChildren
  109. Archives a page's children and sets the archive status to inherited.
  110. Parameters:
  111. page - A page id.
  112. See Also:
  113. <archivePage>
  114. */
  115. function archivePageChildren($page) {
  116. $page = mysql_real_escape_string($page);
  117. $q = sqlquery("SELECT * FROM bigtree_pages WHERE parent = '$page'");
  118. while ($f = sqlfetch($q)) {
  119. if (!$f["archived"]) {
  120. sqlquery("UPDATE bigtree_pages SET archived = 'on', archived_inherited = 'on' WHERE id = '".$f["id"]."'");
  121. $this->track("bigtree_pages",$f["id"],"archived");
  122. $this->archivePageChildren($f["id"]);
  123. }
  124. }
  125. }
  126. /*
  127. Function: autoIPL
  128. Automatically converts links to internal page links.
  129. Parameters:
  130. html - A string of contents that may contain URLs
  131. Returns:
  132. A string with hard links converted into internal page links.
  133. */
  134. function autoIPL($html) {
  135. // If this string is actually just a URL, IPL it.
  136. if (substr($html,0,7) == "http://" || substr($html,0,8) == "https://") {
  137. $html = $this->makeIPL($html);
  138. // Otherwise, switch all the image srcs and javascripts srcs and whatnot to {wwwroot}.
  139. } else {
  140. $html = preg_replace_callback('/href="([^"]*)"/',create_function('$matches','
  141. global $cms;
  142. $href = str_replace("{wwwroot}",$GLOBALS["www_root"],$matches[1]);
  143. if (strpos($href,$GLOBALS["www_root"]) !== false) {
  144. $command = explode("/",rtrim(str_replace($GLOBALS["www_root"],"",$href),"/"));
  145. list($navid,$commands) = $cms->getNavId($command);
  146. $page = $cms->getPage($navid,false);
  147. if ($navid && (!$commands[0] || substr($page["template"],0,6) == "module" || substr($commands[0],0,1) == "#")) {
  148. $href = "ipl://".$navid."//".base64_encode(json_encode($commands));
  149. }
  150. }
  151. $href = str_replace($GLOBALS["www_root"],"{wwwroot}",$href);
  152. return \'href="\'.$href.\'"\';'
  153. ),$html);
  154. $html = str_replace($GLOBALS["www_root"],"{wwwroot}",$html);
  155. }
  156. return $html;
  157. }
  158. /*
  159. Function: canAccessGroup
  160. Returns whether or not the logged in user can access a module group.
  161. Utility for form field types / views -- we already know module group permissions are enabled so we skip some overhead
  162. Parameters:
  163. module - A module entry.
  164. group - A group id.
  165. Returns:
  166. true if the user can access this group, otherwise false.
  167. */
  168. function canAccessGroup($module,$group) {
  169. if ($this->Level > 0) {
  170. return true;
  171. }
  172. $id = $module["id"];
  173. if ($this->Permissions["module"][$id] && $this->Permissions["module"][$id] != "n") {
  174. return true;
  175. }
  176. if (is_array($this->Permissions["module_gbp"][$id])) {
  177. $gp = $this->Permissions["module_gbp"][$id][$group];
  178. if ($gp && $gp != "n") {
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. /*
  185. Function: canModifyChildren
  186. Checks whether the logged in user can modify all child pages or a page.
  187. Assumes we already know that we're a publisher of the parent.
  188. Parameters:
  189. page - The page entry to check children for.
  190. Returns:
  191. true if the user can modify all the page children, otherwise false.
  192. */
  193. function canModifyChildren($page) {
  194. if ($this->Level > 0) {
  195. return true;
  196. }
  197. $q = sqlquery("SELECT id FROM bigtree_pages WHERE path LIKE '".mysql_real_escape_string($page["path"])."%'");
  198. while ($f = sqlfetch($q)) {
  199. $perm = $this->Permissions["page"][$f["id"]];
  200. if ($perm == "n" || $perm == "e") {
  201. return false;
  202. }
  203. }
  204. return true;
  205. }
  206. /*
  207. Function: changePassword
  208. Changes a user's password via a password change hash and redirects to a success page.
  209. Paramters:
  210. hash - The unique hash generated by <forgotPassword>.
  211. password - The user's new password.
  212. See Also:
  213. <forgotPassword>
  214. */
  215. function changePassword($hash,$password) {
  216. global $config;
  217. $hash = mysql_real_escape_string($hash);
  218. $user = sqlfetch(sqlquery("SELECT * FROM bigtree_users WHERE change_password_hash = '$hash'"));
  219. $phpass = new PasswordHash($config["password_depth"], TRUE);
  220. $password = mysql_real_escape_string($phpass->HashPassword($password));
  221. sqlquery("UPDATE bigtree_users SET password = '$password', change_password_hash = '' WHERE id = '".$user["id"]."'");
  222. header("Location: ".$GLOBALS["admin_root"]."login/reset-success/");
  223. die();
  224. }
  225. /*
  226. Function: checkAccess
  227. Determines whether the logged in user has access to a module or not.
  228. Parameters:
  229. module - Either a module id or module entry.
  230. Returns:
  231. true if the user can access the module, otherwise false.
  232. */
  233. function checkAccess($module) {
  234. if (is_array($module)) {
  235. $module = $module["id"];
  236. }
  237. if ($this->Level > 0) {
  238. return true;
  239. }
  240. if ($this->Permissions["module"][$module] && $this->Permissions["module"][$module] != "n") {
  241. return true;
  242. }
  243. if (is_array($this->Permissions["module_gbp"][$module])) {
  244. foreach ($this->Permissions["module_gbp"][$module] as $p) {
  245. if ($p != "n") {
  246. return true;
  247. }
  248. }
  249. }
  250. return false;
  251. }
  252. /*
  253. Function: checkHTML
  254. Checks a block of HTML for broken links/images
  255. Parameters:
  256. relative_path - The starting path of the page containing the HTML (so that relative links, i.e. "good/" know where to begin)
  257. html - A string of HTML
  258. external - Whether to check external links (slow) or not
  259. Returns:
  260. An array of errors.
  261. */
  262. function checkHTML($relative_path,$html,$external = false) {
  263. if (!$html) {
  264. return array();
  265. }
  266. $errors = array();
  267. $doc = new DOMDocument();
  268. $doc->loadHTML($html);
  269. // Check A tags.
  270. $links = $doc->getElementsByTagName("a");
  271. foreach ($links as $link) {
  272. $href = $link->getAttribute("href");
  273. $href = str_replace(array("{wwwroot}","%7Bwwwroot%7D"),$GLOBALS["www_root"],$href);
  274. if (substr($href,0,4) == "http" && strpos($href,$GLOBALS["www_root"]) === false) {
  275. // External link, not much we can do but alert that it's dead
  276. if ($external) {
  277. if (strpos($href,"#") !== false)
  278. $href = substr($href,0,strpos($href,"#")-1);
  279. if (!$this->urlExists($href)) {
  280. $errors["a"][] = $href;
  281. }
  282. }
  283. } elseif (substr($href,0,6) == "ipl://") {
  284. if (!$this->iplExists($href)) {
  285. $errors["a"][] = $href;
  286. }
  287. } elseif (substr($href,0,7) == "mailto:" || substr($href,0,1) == "#" || substr($href,0,5) == "data:") {
  288. // Don't do anything, it's a page mark, data URI, or email address
  289. } elseif (substr($href,0,4) == "http") {
  290. // It's a local hard link
  291. if (!$this->urlExists($href)) {
  292. $errors["a"][] = $href;
  293. }
  294. } else {
  295. // Local file.
  296. $local = $relative_path.$href;
  297. if (!$this->urlExists($local)) {
  298. $errors["a"][] = $local;
  299. }
  300. }
  301. }
  302. // Check IMG tags.
  303. $images = $doc->getElementsByTagName("img");
  304. foreach ($images as $image) {
  305. $href = $image->getAttribute("src");
  306. $href = str_replace(array("{wwwroot}","%7Bwwwroot%7D"),$GLOBALS["www_root"],$href);
  307. if (substr($href,0,4) == "http" && strpos($href,$GLOBALS["www_root"]) === false) {
  308. // External link, not much we can do but alert that it's dead
  309. if ($external) {
  310. if (!$this->urlExists($href)) {
  311. $errors["img"][] = $href;
  312. }
  313. }
  314. } elseif (substr($href,0,6) == "ipl://") {
  315. if (!$this->iplExists($href)) {
  316. $errors["a"][] = $href;
  317. }
  318. } elseif (substr($href,0,5) == "data:") {
  319. // Do nothing, it's a data URI
  320. } elseif (substr($href,0,4) == "http") {
  321. // It's a local hard link
  322. if (!$this->urlExists($href)) {
  323. $errors["img"][] = $href;
  324. }
  325. } else {
  326. // Local file.
  327. $local = $relative_path.$href;
  328. if (!$this->urlExists($local)) {
  329. $errors["img"][] = $local;
  330. }
  331. }
  332. }
  333. return array($errors);
  334. }
  335. /*
  336. Function: clearCache
  337. Removes all files in the cache directory.
  338. */
  339. function clearCache() {
  340. $d = opendir($GLOBALS["server_root"]."cache/");
  341. while ($f = readdir($d)) {
  342. if ($f != "." && $f != ".." && !is_dir($GLOBALS["server_root"]."cache/".$f)) {
  343. unlink($GLOBALS["server_root"]."cache/".$f);
  344. }
  345. }
  346. }
  347. /*
  348. Function: createCallout
  349. Creates a callout and its files.
  350. Parameters:
  351. id - The id.
  352. name - The name.
  353. description - The description.
  354. level - Access level (0 for everyone, 1 for administrators, 2 for developers).
  355. resources - An array of resources.
  356. */
  357. function createCallout($id,$name,$description,$level,$resources) {
  358. // If we're creating a new file, let's populate it with some convenience things to show what resources are available.
  359. $file_contents = '<?
  360. /*
  361. Resources Available:
  362. ';
  363. $cached_types = $this->getCachedFieldTypes();
  364. $types = $cached_types["callout"];
  365. $clean_resources = array();
  366. foreach ($resources as $resource) {
  367. if ($resource["id"] && $resource["id"] != "type") {
  368. $options = json_decode($resource["options"],true);
  369. foreach ($options as $key => $val) {
  370. if ($key != "title" && $key != "id" && $key != "type") {
  371. $resource[$key] = $val;
  372. }
  373. }
  374. $file_contents .= ' $'.$resource["id"].' = '.$resource["title"].' - '.$types[$resource["type"]]."\n";
  375. $resource["id"] = htmlspecialchars($resource["id"]);
  376. $resource["title"] = htmlspecialchars($resource["title"]);
  377. $resource["subtitle"] = htmlspecialchars($resource["subtitle"]);
  378. unset($resource["options"]);
  379. $clean_resources[] = $resource;
  380. }
  381. }
  382. $file_contents .= ' */
  383. ?>';
  384. // Clean up the post variables
  385. $id = mysql_real_escape_string(htmlspecialchars($id));
  386. $name = mysql_real_escape_string(htmlspecialchars($name));
  387. $description = mysql_real_escape_string(htmlspecialchars($description));
  388. $level = mysql_real_escape_string($level);
  389. $resources = mysql_real_escape_string(json_encode($clean_resources));
  390. if (!file_exists($GLOBALS["server_root"]."templates/callouts/".$id.".php")) {
  391. file_put_contents($GLOBALS["server_root"]."templates/callouts/".$id.".php",$file_contents);
  392. chmod($GLOBALS["server_root"]."templates/callouts/".$id.".php",0777);
  393. }
  394. sqlquery("INSERT INTO bigtree_callouts (`id`,`name`,`description`,`resources`,`level`) VALUES ('$id','$name','$description','$resources','$level')");
  395. }
  396. /*
  397. Function: createFeed
  398. Creates a feed.
  399. Parameters:
  400. name - The name.
  401. description - The description.
  402. table - The data table.
  403. type - The feed type.
  404. options - The feed type options.
  405. fields - The fields.
  406. Returns:
  407. The route to the new feed.
  408. */
  409. function createFeed($name,$description,$table,$type,$options,$fields) {
  410. global $cms;
  411. // Options were encoded before submitting the form, so let's get them back.
  412. $options = json_decode($options,true);
  413. if (is_array($options)) {
  414. foreach ($options as &$option) {
  415. $option = str_replace($www_root,"{wwwroot}",$option);
  416. }
  417. }
  418. // Get a unique route!
  419. $route = $cms->urlify($name);
  420. $x = 2;
  421. $oroute = $route;
  422. $f = $cms->getFeedByRoute($route);
  423. while ($f) {
  424. $route = $oroute."-".$x;
  425. $f = $cms->getFeedByRoute($route);
  426. $x++;
  427. }
  428. // Fix stuff up for the db.
  429. $name = mysql_real_escape_string(htmlspecialchars($name));
  430. $description = mysql_real_escape_string(htmlspecialchars($description));
  431. $table = mysql_real_escape_string($table);
  432. $type = mysql_real_escape_string($type);
  433. $options = mysql_real_escape_string(json_encode($options));
  434. $fields = mysql_real_escape_string(json_encode($fields));
  435. $route = mysql_real_escape_string($route);
  436. sqlquery("INSERT INTO bigtree_feeds (`route`,`name`,`description`,`type`,`table`,`fields`,`options`) VALUES ('$route','$name','$description','$type','$table','$fields','$options')");
  437. return $route;
  438. }
  439. /*
  440. Function: createFieldType
  441. Creates a field type and its files.
  442. Parameters:
  443. id - The id of the field type.
  444. name - The name.
  445. pages - Whether it can be used as a page resource or not ("on" is yes)
  446. modules - Whether it can be used as a module resource or not ("on" is yes)
  447. callouts - Whether it can be used as a callout resource or not ("on" is yes)
  448. */
  449. function createFieldType($id,$name,$pages,$modules,$callouts) {
  450. $id = mysql_real_escape_string($id);
  451. $name = mysql_real_escape_string(htmlspecialchars($name));
  452. $author = mysql_real_escape_string($this->Name);
  453. $pages = mysql_real_escape_string($pages);
  454. $modules = mysql_real_escape_string($modules);
  455. $callouts = mysql_real_escape_string($callouts);
  456. $file = "$id.php";
  457. sqlquery("INSERT INTO bigtree_field_types (`id`,`name`,`pages`,`modules`,`callouts`) VALUES ('$id','$name','$pages','$modules','$callouts')");
  458. // Make the files for draw and process and options if they don't exist.
  459. if (!file_exists($GLOBALS["server_root"]."custom/admin/form-field-types/draw/$file")) {
  460. BigTree::touchFile($GLOBALS["server_root"]."custom/admin/form-field-types/draw/$file");
  461. file_put_contents($GLOBALS["server_root"]."custom/admin/form-field-types/draw/$file",'<? include BigTree::path("admin/form-field-types/draw/text.php"); ?>');
  462. chmod($GLOBALS["server_root"]."custom/admin/form-field-types/draw/$file",0777);
  463. }
  464. if (!file_exists($GLOBALS["server_root"]."custom/admin/form-field-types/process/$file")) {
  465. BigTree::touchFile($GLOBALS["server_root"]."custom/admin/form-field-types/process/$file");
  466. file_put_contents($GLOBALS["server_root"]."custom/admin/form-field-types/process/$file",'<? $value = $data[$key]; ?>');
  467. chmod($GLOBALS["server_root"]."custom/admin/form-field-types/process/$file",0777);
  468. }
  469. if (!file_exists($GLOBALS["server_root"]."custom/admin/ajax/developer/field-options/$file")) {
  470. BigTree::touchFile($GLOBALS["server_root"]."custom/admin/ajax/developer/field-options/$file");
  471. chmod($GLOBALS["server_root"]."custom/admin/ajax/developer/field-options/$file",0777);
  472. }
  473. unlink($GLOBALS["server_root"]."cache/form-field-types.btc");
  474. }
  475. /*
  476. Function: createMessage
  477. Creates a message in message center.
  478. Parameters:
  479. subject - The subject line.
  480. message - The message.
  481. recipients - The recipients.
  482. in_response_to - The message being replied to.
  483. */
  484. function createMessage($subject,$message,$recipients,$in_response_to = 0) {
  485. // Clear tags out of the subject, sanitize the message body of XSS attacks.
  486. $subject = mysql_real_escape_string(htmlspecialchars(strip_tags($subject)));
  487. $message = mysql_real_escape_string(strip_tags($message,"<p><b><strong><em><i><a>"));
  488. $in_response_to = mysql_real_escape_string($in_response_to);
  489. // We build the send_to field this way so that we don't have to create a second table of recipients.
  490. // Is it faster database wise using a LIKE over a JOIN? I don't know, but it makes for one less table.
  491. $send_to = "|";
  492. foreach ($recipients as $r) {
  493. // Make sure they actually put in a number and didn't try to screw with the $_POST
  494. $send_to .= intval($r)."|";
  495. }
  496. $send_to = mysql_real_escape_string($send_to);
  497. sqlquery("INSERT INTO bigtree_messages (`sender`,`recipients`,`subject`,`message`,`date`,`response_to`) VALUES ('".$this->ID."','$send_to','$subject','$message',NOW(),'$in_response_to')");
  498. }
  499. /*
  500. Function: createModule
  501. Creates a module and its class file.
  502. Parameters:
  503. name - The name of the module.
  504. group - The group for the module.
  505. class - The module class to create.
  506. table - The table this module relates to.
  507. permissions - The group-based permissions.
  508. Returns:
  509. The new module id.
  510. */
  511. function createModule($name,$group,$class,$table,$permissions) {
  512. global $cms;
  513. // Find an available module route.
  514. $route = $cms->urlify($name);
  515. // Go through the hard coded modules
  516. $existing = array();
  517. $d = opendir($GLOBALS["server_root"]."core/admin/modules/");
  518. while ($f = readdir($d)) {
  519. if ($f != "." && $f != "..") {
  520. $existing[] = $f;
  521. }
  522. }
  523. // Go through the directories (really ajax, css, images, js)
  524. $d = opendir($GLOBALS["server_root"]."core/admin/");
  525. while ($f = readdir($d)) {
  526. if ($f != "." && $f != "..") {
  527. $existing[] = $f;
  528. }
  529. }
  530. // Go through the hard coded pages
  531. $d = opendir($GLOBALS["server_root"]."core/admin/pages/");
  532. while ($f = readdir($d)) {
  533. if ($f != "." && $f != "..") {
  534. // Drop the .php
  535. $existing[] = substr($f,0,-4);
  536. }
  537. }
  538. // Go through already created modules
  539. $q = sqlquery("SELECT route FROM bigtree_modules");
  540. while ($f = sqlfetch($q)) {
  541. $existing[] = $f["route"];
  542. }
  543. // Get a unique route
  544. $x = 2;
  545. $oroute = $route;
  546. while (in_array($route,$existing)) {
  547. $route = $oroute."-".$x;
  548. $x++;
  549. }
  550. $name = mysql_real_escape_string(htmlspecialchars($name));
  551. $route = mysql_real_escape_string($route);
  552. $class = mysql_real_escape_string($class);
  553. $group = mysql_real_escape_string($group);
  554. $gbp = mysql_real_escape_string(json_encode($permissions));
  555. sqlquery("INSERT INTO bigtree_modules (`name`,`route`,`class`,`group`,`gbp`) VALUES ('$name','$route','$class','$group','$gbp')");
  556. $id = sqlid();
  557. if ($class) {
  558. // Create class module.
  559. $f = fopen($GLOBALS["server_root"]."custom/inc/modules/$route.php","w");
  560. fwrite($f,"<?\n");
  561. fwrite($f," class $class extends BigTreeModule {\n");
  562. fwrite($f,"\n");
  563. fwrite($f,' var $Table = "'.$table.'";'."\n");
  564. fwrite($f,' var $Module = "'.$id.'";'."\n");
  565. fwrite($f," }\n");
  566. fwrite($f,"?>\n");
  567. fclose($f);
  568. chmod($GLOBALS["server_root"]."custom/inc/modules/$route.php",0777);
  569. // Remove cached class list.
  570. unlink($GLOBALS["server_root"]."cache/module-class-list.btc");
  571. }
  572. return $id;
  573. }
  574. /*
  575. Function: createModuleAction
  576. Creates a module action.
  577. Parameters:
  578. module - The module to create an action for.
  579. name - The name of the action.
  580. route - The action route.
  581. in_nav - Whether the action is in the navigation.
  582. icon - The icon class for the action.
  583. form - Optional auto module form id.
  584. view - Optional auto module view id.
  585. */
  586. function createModuleAction($module,$name,$route,$in_nav,$icon,$form = 0,$view = 0) {
  587. $module = mysql_real_escape_string($module);
  588. $route = mysql_real_escape_string(htmlspecialchars($route));
  589. $in_nav = mysql_real_escape_string($in_nav);
  590. $icon = mysql_real_escape_string($icon);
  591. $name = mysql_real_escape_string(htmlspecialchars($name));
  592. $form = mysql_real_escape_string($form);
  593. $view = mysql_real_escape_string($view);
  594. $oroute = $route;
  595. $x = 2;
  596. while ($f = sqlfetch(sqlquery("SELECT * FROM bigtree_module_actions WHERE module = '$module' AND route = '$route'"))) {
  597. $route = $oroute."-".$x;
  598. $x++;
  599. }
  600. sqlquery("INSERT INTO bigtree_module_actions (`module`,`name`,`route`,`in_nav`,`class`,`form`,`view`) VALUES ('$module','$name','$route','$in_nav','$icon','$form','$view')");
  601. }
  602. /*
  603. Function: createModuleForm
  604. Creates a module form.
  605. Parameters:
  606. title - The title of the form.
  607. table - The table for the form data.
  608. fields - The form fields.
  609. javascript - Optional Javascript file to include in the form.
  610. css - Optional CSS file to include in the form.
  611. callback - Optional callback function to run after the form processes.
  612. default_position - Default position for entries to the form (if the view is positioned).
  613. Returns:
  614. The new form id.
  615. */
  616. function createModuleForm($title,$table,$fields,$javascript = "",$css = "",$callback = "",$default_position = "") {
  617. $title = mysql_real_escape_string(htmlspecialchars($title));
  618. $table = mysql_real_escape_string($table);
  619. $fields = mysql_real_escape_string(json_encode($fields));
  620. $javascript - mysql_real_escape_string(htmlspecialchars($javascript));
  621. $css - mysql_real_escape_string(htmlspecialchars($css));
  622. $callback - mysql_real_escape_string($callback);
  623. $default_position - mysql_real_escape_string($default_position);
  624. sqlquery("INSERT INTO bigtree_module_forms (`title`,`table`,`fields`,`javascript`,`css`,`callback`,`default_position`) VALUES ('$title','$table','$fields','$javascript','$css','$callback','$default_position')");
  625. return sqlid();
  626. }
  627. /*
  628. Function: createModuleGroup
  629. Creates a module group.
  630. Parameters:
  631. name - The name of the group.
  632. package - The (optional) package id the group originated from.
  633. Returns:
  634. The id of the newly created group.
  635. */
  636. function createModuleGroup($name,$in_nav,$package = 0) {
  637. global $cms;
  638. $name = mysql_real_escape_string($name);
  639. $packge = mysql_real_escape_string($package);
  640. // Get a unique route
  641. $x = 2;
  642. $route = $cms->urlify($name);
  643. $oroute = $route;
  644. while ($this->getModuleGroupByRoute($route)) {
  645. $route = $oroute."-".$x;
  646. $x++;
  647. }
  648. // Just to be safe
  649. $route = mysql_real_escape_string($route);
  650. sqlquery("INSERT INTO bigtree_module_groups (`name`,`route`,`in_nav`,`package`) VALUES ('$name','$route','$in_nav','$package')");
  651. return sqlid();
  652. }
  653. /*
  654. Function: createModuleView
  655. Creates a module view.
  656. Parameters:
  657. title - View title.
  658. description - Description.
  659. table - Data table.
  660. type - View type.
  661. options - View options array.
  662. fields - Field array.
  663. actions - Actions array.
  664. suffix - Add/Edit suffix.
  665. uncached - Don't cache the view.
  666. preview_url - Optional preview URL.
  667. Returns:
  668. The id for view.
  669. */
  670. function createModuleView($title,$description,$table,$type,$options,$fields,$actions,$suffix,$uncached = "",$preview_url = "") {
  671. $title = mysql_real_escape_string(htmlspecialchars($title));
  672. $description = mysql_real_escape_string(htmlspecialchars($description));
  673. $table = mysql_real_escape_string($table);
  674. $type = mysql_real_escape_string($type);
  675. $options = mysql_real_escape_string(json_encode($options));
  676. $fields = mysql_real_escape_string(json_encode($fields));
  677. $actions = mysql_real_escape_string(json_encode($actions));
  678. $suffix = mysql_real_escape_string($suffix);
  679. $uncached = mysql_real_escape_string($uncached);
  680. $preview_url = mysql_real_escape_string(htmlspecialchars($preview_url));
  681. sqlquery("INSERT INTO bigtree_module_views (`title`,`description`,`type`,`fields`,`actions`,`table`,`options`,`suffix`,`uncached`,`preview_url`) VALUES ('$title','$description','$type','$fields','$actions','$table','$options','$suffix','$uncached','$preview_url')");
  682. return sqlid();
  683. }
  684. /*
  685. Function: createPage
  686. Creates a page.
  687. Does not check permissions.
  688. Parameters:
  689. data - An array of page information.
  690. Returns:
  691. The id of the newly created page.
  692. */
  693. function createPage($data) {
  694. global $cms;
  695. // Loop through the posted data, make sure no session hijacking is done.
  696. foreach ($data as $key => $val) {
  697. if (substr($key,0,1) != "_") {
  698. if (is_array($val)) {
  699. $$key = mysql_real_escape_string(json_encode($val));
  700. } else {
  701. $$key = mysql_real_escape_string($val);
  702. }
  703. }
  704. }
  705. // If there's an external link, make sure it's a relative URL
  706. if ($external) {
  707. $external = $this->makeIPL($external);
  708. }
  709. // Who knows what they may have put in for a route, so we're not going to use the mysql_real_escape_string version.
  710. $route = $data["route"];
  711. if (!$route) {
  712. // If they didn't specify a route use the navigation title
  713. $route = $cms->urlify($data["nav_title"]);
  714. } else {
  715. // Otherwise sanitize the one they did provide.
  716. $route = $cms->urlify($route);
  717. }
  718. // We need to figure out a unique route for the page. Make sure it doesn't match a directory in /site/
  719. $original_route = $route;
  720. $x = 2;
  721. // Reserved paths.
  722. if ($parent == 0) {
  723. while (file_exists($GLOBALS["server_root"]."site/".$route."/")) {
  724. $route = $original_route."-".$x;
  725. $x++;
  726. }
  727. }
  728. // Make sure it doesn't have the same route as any of its siblings.
  729. $f = sqlfetch(sqlquery("SELECT * FROM bigtree_pages WHERE `route` = '$route' AND parent = '$parent'"));
  730. while ($f) {
  731. $route = $original_route."-".$x;
  732. $f = sqlfetch(sqlquery("SELECT * FROM bigtree_pages WHERE `route` = '$route' AND parent = '$parent'"));
  733. $x++;
  734. }
  735. // If we have a parent, get the full navigation path, otherwise, just use this route as the path since it's top level.
  736. if ($parent) {
  737. $path = $this->getFullNavigationPath($parent)."/".$route;
  738. } else {
  739. $path = $route;
  740. }
  741. // If we set a publish at date, make it the proper MySQL format.
  742. if ($publish_at) {
  743. $publish_at = "'".date("Y-m-d",strtotime($publish_at))."'";
  744. } else {
  745. $publish_at = "NULL";
  746. }
  747. // If we set an expiration date, make it the proper MySQL format.
  748. if ($expire_at) {
  749. $expire_at = "'".date("Y-m-d",strtotime($expire_at))."'";
  750. } else {
  751. $expire_at = "NULL";
  752. }
  753. // Make the title, navigation title, description, keywords, and external link htmlspecialchar'd -- these are all things we'll be echoing in the HTML so we might as well make them valid now instead of at display time.
  754. $title = htmlspecialchars($title);
  755. $nav_title = htmlspecialchars($nav_title);
  756. $meta_description = htmlspecialchars($meta_description);
  757. $meta_keywords = htmlspecialchars($meta_keywords);
  758. $external = htmlspecialchars($external);
  759. // Make the page!
  760. sqlquery("INSERT INTO bigtree_pages (`parent`,`nav_title`,`route`,`path`,`in_nav`,`title`,`template`,`external`,`new_window`,`resources`,`callouts`,`meta_keywords`,`meta_description`,`last_edited_by`,`created_at`,`updated_at`,`publish_at`,`expire_at`,`max_age`) VALUES ('$parent','$nav_title','$route','$path','$in_nav','$title','$template','$external','$new_window','$resources','$callouts','$meta_keywords','$meta_description','".$this->ID."',NOW(),NOW(),$publish_at,$expire_at,'$max_age')");
  761. $id = sqlid();
  762. // Handle tags
  763. if (is_array($data["_tags"])) {
  764. foreach ($data["_tags"] as $tag) {
  765. sqlquery("INSERT INTO bigtree_tags_rel (`module`,`entry`,`tag`) VALUES ('0','$id','$tag')");
  766. }
  767. }
  768. // If there was an old page that had previously used this path, dump its history so we can take over the path.
  769. sqlquery("DELETE FROM bigtree_route_history WHERE old_route = '$path'");
  770. // Dump the cache, we don't really know how many pages may be showing this now in their nav.
  771. $this->clearCache();
  772. // Let search engines know this page now exists.
  773. $this->pingSearchEngines();
  774. // Audit trail.
  775. $this->track("bigtree_pages",$id,"created");
  776. return $id;
  777. }
  778. /*
  779. Function: createPendingChange
  780. Creates a pending change.
  781. Parameters:
  782. table - The table the change applies to.
  783. item_id - The entry the change applies to's id.
  784. changes - The changes to the fields in the entry.
  785. mtm_changes - Many to Many changes.
  786. tags_changes - Tags changes.
  787. module - The module id for the change.
  788. Returns:
  789. The change id.
  790. */
  791. function createPendingChange($table,$item_id,$changes,$mtm_changes = array(),$tags_changes = array(),$module = 0) {
  792. $table = mysql_real_escape_string($table);
  793. $item_id = mysql_real_escape_string($item_id);
  794. $changes = mysql_real_escape_string(json_encode($changes));
  795. $mtm_changes = mysql_real_escape_string(json_encode($mtm_changes));
  796. $tags_changes = mysql_real_escape_string(json_encode($tags_changes));
  797. $module = mysql_real_escape_string($module);
  798. sqlquery("INSERT INTO bigtree_pending_changes (`user`,`date`,`table`,`item_id`,`changes`,`mtm_changes`,`tags_changes`,`module`) VALUES ('".$this->ID."',NOW(),'$table','$item_id','$changes','$mtm_changes','$tags_changes','$module')");
  799. return sqlid();
  800. }
  801. /*
  802. Function: createPendingPage
  803. Creates a pending page entry in bigtree_pending_changes
  804. Parameters:
  805. data - An array of page information.
  806. Returns:
  807. The id of the pending change.
  808. */
  809. function createPendingPage($data) {
  810. global $cms;
  811. // Make a relative URL for external links.
  812. if ($data["external"]) {
  813. $data["external"] = $this->makeIPL($data["external"]);
  814. }
  815. // Save the tags, then dump them from the saved changes array.
  816. $tags = mysql_real_escape_string(json_encode($data["_tags"]));
  817. unset($data["_tags"]);
  818. // Make the nav title, title, external link, keywords, and description htmlspecialchar'd for displaying on the front end / the form again.
  819. $data["nav_title"] = htmlspecialchars($data["nav_title"]);
  820. $data["title"] = htmlspecialchars($data["title"]);
  821. $data["external"] = htmlspecialchars($data["external"]);
  822. $data["meta_keywords"] = htmlspecialchars($data["meta_keywords"]);
  823. $data["meta_description"] = htmlspecialchars($data["meta_description"]);
  824. $parent = mysql_real_escape_string($data["parent"]);
  825. // JSON encode the changes and stick them in the database.
  826. unset($data["MAX_FILE_SIZE"]);
  827. unset($data["ptype"]);
  828. $data = mysql_real_escape_string(json_encode($data));
  829. sqlquery("INSERT INTO bigtree_pending_changes (`user`,`date`,`title`,`table`,`changes`,`tags_changes`,`type`,`module`,`pending_page_parent`) VALUES ('".$this->ID."',NOW(),'New Page Created','bigtree_pages','$data','$tags','NEW','','$parent')");
  830. $id = sqlid();
  831. // Audit trail
  832. $this->track("bigtree_pages","p$id","created-pending");
  833. return $id;
  834. }
  835. /*
  836. Function: createResource
  837. Creates a resource.
  838. Parameters:
  839. folder - The folder to place it in.
  840. file - The file path.
  841. name - The file name.
  842. type - The file type.
  843. is_image - Whether the resource is an image.
  844. height - The image height (if it's an image).
  845. width - The image width (if it's an image).
  846. thumbs - An array of thumbnails (if it's an image).
  847. list_thumb_margin - The margin for the list thumbnail (if it's an image).
  848. Returns:
  849. The new resource id.
  850. */
  851. function createResource($folder,$file,$name,$type,$is_image = "",$height = 0,$width = 0,$thumbs = array(),$list_thumb_margin = 0) {
  852. $folder = mysql_real_escape_string($folder);
  853. $file = mysql_real_escape_string($file);
  854. $name = mysql_real_escape_string(htmlspecialchars($name));
  855. $type = mysql_real_escape_string($type);
  856. $is_image = mysql_real_escape_string($is_image);
  857. $height = intval($height);
  858. $width = intval($width);
  859. $thumbs = mysql_real_escape_string(json_encode($thumbs));
  860. $list_thumb_margin = intval($list_thumb_margin);
  861. sqlquery("INSERT INTO bigtree_resources (`file`,`date`,`name`,`type`,`folder`,`is_image`,`height`,`width`,`thumbs`,`list_thumb_margin`) VALUES ('$file',NOW(),'$name','$type','$folder','$is_image','$height','$width','$thumbs','$list_thumb_margin')");
  862. return sqlid();
  863. }
  864. /*
  865. Function: createResourceFolder
  866. Creates a resource folder.
  867. Checks permissions.
  868. Paremeters:
  869. parent - The parent folder.
  870. name - The name of the new folder.
  871. Returns:
  872. The new folder id.
  873. */
  874. function createResourceFolder($parent,$name) {
  875. $perm = $this->getResourceFolderPermission($parent);
  876. if ($perm != "p") {
  877. die("You don't have permission to make a folder here.");
  878. }
  879. $parent = mysql_real_escape_string($parent);
  880. $name = mysql_real_escape_string(htmlspecialchars($name));
  881. sqlquery("INSERT INTO bigtree_resource_folders (`name`,`parent`) VALUES ('$name','$parent')");
  882. return sqlid();
  883. }
  884. /*
  885. Function: createSetting
  886. Creates a setting.
  887. Parameters:
  888. data - An array of settings information. Available fields: "id", "name", "description", "type", "locked", "module", "encrypted", "system"
  889. Returns:
  890. True if successful, false if a setting already exists with the ID given.
  891. */
  892. function createSetting($data) {
  893. // Avoid _SESSION hijacking.
  894. foreach ($data as $key => $val) {
  895. if (substr($key,0,1) != "_" && !is_array($val)) {
  896. $$key = mysql_real_escape_string(htmlspecialchars($val));
  897. }
  898. }
  899. // We don't want this encoded since it's a WYSIWYG field.
  900. $description = mysql_real_escape_string($data["description"]);
  901. // See if there's already a setting with this ID
  902. $r = sqlrows(sqlquery("SELECT id FROM bigtree_settings WHERE id = '$id'"));
  903. if ($r) {
  904. return false;
  905. }
  906. sqlquery("INSERT INTO bigtree_settings (`id`,`name`,`description`,`type`,`locked`,`encrypted`,`system`) VALUES ('$id','$name','$description','$type','$locked','$encrypted','$system')");
  907. // Audit trail.
  908. $this->track("bigtree_settings",$id,"created");
  909. return true;
  910. }
  911. /*
  912. Function: createTag
  913. Creates a new tag, or returns the id of an existing one.
  914. Parameters:
  915. tag - The tag.
  916. Returns:
  917. If the tag exists, returns the existing tag's id.
  918. Otherwise, returns the new tag id.
  919. */
  920. function createTag($tag) {
  921. global $cms;
  922. $tag = strtolower(html_entity_decode($tag));
  923. // Check if the tag exists already.
  924. $f = sqlfetch(sqlquery("SELECT * FROM bigtree_tags WHERE tag = '".mysql_real_escape_string($tag)."'"));
  925. if (!$f) {
  926. $meta = metaphone($tag);
  927. $route = $cms->urlify($tag);
  928. $oroute = $route;
  929. $x = 2;
  930. while ($f = sqlfetch(sqlquery("SELECT * FROM bigtree_tags WHERE route = '$route'"))) {
  931. $route = $oroute."-".$x;
  932. $x++;
  933. }
  934. sqlquery("INSERT INTO bigtree_tags (`tag`,`metaphone`,`route`) VALUES ('".mysql_real_escape_string($tag)."','$meta','$route')");
  935. $id = sqlid();
  936. } else {
  937. $id = $f["id"];
  938. }
  939. return $id;
  940. }
  941. /*
  942. Function: createTemplate
  943. Creates a template and its default files/directories.
  944. Paremeters:
  945. id - Id for the template.
  946. name - Name
  947. description - Description
  948. routed - Basic ("") or Routed ("on")
  949. level - Access level (0 for everyone, 1 for administrators, 2 for developers)
  950. module - Related module id
  951. image - Image
  952. callouts_enabled - "on" for yes
  953. resources - An array of resources
  954. */
  955. function createTemplate($id,$name,$description,$routed,$level,$module,$image,$callouts_enabled,$resources) {
  956. // If we're creating a new file, let's populate it with some convenience things to show what resources are available.
  957. $file_contents = "<?\n /*\n Resources Available:\n";
  958. $types = $this->getCachedFieldTypes();
  959. $types = $types["template"];
  960. $clean_resources = array();
  961. foreach ($resources as $resource) {
  962. if ($resource["id"]) {
  963. $options = json_decode($resource["options"],true);
  964. foreach ($options as $key => $val) {
  965. if ($key != "title" && $key != "id" && $key != "type") {
  966. $resource[$key] = $val;
  967. }
  968. }
  969. $file_contents .= ' $'.$resource["id"].' = '.$resource["title"].' - '.$types[$resource["type"]]."\n";
  970. $resource["id"] = htmlspecialchars($resource["id"]);
  971. $resource["title"] = htmlspecialchars($resource["title"]);
  972. $resource["subtitle"] = htmlspecialchars($resource["subtitle"]);
  973. unset($resource["options"]);
  974. $clean_resources[] = $resource;
  975. }
  976. }
  977. $file_contents .= ' */
  978. ?>';
  979. if ($routed == "on") {
  980. if (!file_exists($GLOBALS["server_root"]."templates/routed/".$id)) {
  981. mkdir($GLOBALS["server_root"]."templates/routed/".$id);
  982. chmod($GLOBALS["server_root"]."templates/routed/".$id,0777);
  983. }
  984. if (!file_exists($GLOBALS["server_root"]."templates/routed/".$id."/default.php")) {
  985. file_put_contents($GLOBALS["server_root"]."templates/routed/".$id."/default.php",$file_contents);
  986. chmod($GLOBALS["server_root"]."templates/routed/".$id."/default.php",0777);
  987. }
  988. } else {
  989. if (!file_exists($GLOBALS["server_root"]."templates/basic/".$id.".php")) {
  990. file_put_contents($GLOBALS["server_root"]."templates/basic/".$id.".php",$file_contents);
  991. chmod($GLOBALS["server_root"]."templates/basic/".$id.".php",0777);
  992. }
  993. }
  994. $id = mysql_real_escape_string($id);
  995. $name = mysql_real_escape_string(htmlspecialchars($name));
  996. $description = mysql_real_escape_string(htmlspecialchars($description));
  997. $module = mysql_real_escape_string($module);
  998. $resources = mysql_real_escape_string(json_encode($clean_resources));
  999. $image = mysql_real_escape_string($image);
  1000. $level = mysql_real_escape_string($level);
  1001. $callouts_enabled = mysql_real_escape_string($callouts_enabled);
  1002. $routed = mysql_real_escape_string($routed);
  1003. sqlquery("INSERT INTO bigtree_templates (`id`,`name`,`module`,`resources`,`image`,`description`,`level`,`callouts_enabled`,`routed`) VALUES ('$id','$name','$module','$resources','$image','$description','$level','$callouts_enabled','$routed')");
  1004. }
  1005. /*
  1006. Function: createUser
  1007. Creates a user.
  1008. Checks for developer access.
  1009. Parameters:
  1010. data - An array of user data. ("email", "password", "name", "company", "level", "permissions")
  1011. Returns:
  1012. id of the newly created user or false if a user already exists with the provided email.
  1013. */
  1014. function createUser($data) {
  1015. global $config;
  1016. // Safely go through the post data
  1017. foreach ($data as $key => $val) {
  1018. if (substr($key,0,1) != "_" && !is_array($val)) {
  1019. $$key = mysql_real_escape_string($val);
  1020. }
  1021. }
  1022. // See if the user already exists
  1023. $r = sqlrows(sqlquery("SELECT * FROM bigtree_users WHERE email = '$email'"));
  1024. if ($r > 0) {
  1025. return false;
  1026. }
  1027. $permissions = mysql_real_escape_string(json_encode($data["permissions"]));
  1028. // If the user is trying to create a developer user and they're not a developer, then… no.
  1029. if ($level > $this->Level) {
  1030. $level = $this->Level;
  1031. }
  1032. // Hash the password.
  1033. $phpass = new PasswordHash($config["password_depth"], TRUE);
  1034. $password = mysql_real_escape_string($phpass->HashPassword($data["password"]));
  1035. sqlquery("INSERT INTO bigtree_users (`email`,`password`,`name`,`company`,`level`,`permissions`) VALUES ('$email','$password','$name','$company','$level','$permissions')");
  1036. $id = sqlid();
  1037. // Audit trail.
  1038. $this->track("bigtree_users",$id,"created");
  1039. return $id;
  1040. }
  1041. /*
  1042. Function: deleteCallout
  1043. Deletes a callout and removes its file.
  1044. Parameters:
  1045. id - The id of the callout.
  1046. */
  1047. function deleteCallout($id) {
  1048. $id = mysql_real_escape_string($id);
  1049. sqlquery("DELETE FROM bigtree_callouts WHERE id = '$id'");
  1050. unlink($GLOBALS["server_root"]."templates/callouts/$id.php");
  1051. }
  1052. /*
  1053. Function: deleteFeed
  1054. Deletes a feed.
  1055. Parameters:
  1056. id - The id of the feed.
  1057. */
  1058. function deleteFeed($id) {
  1059. $id = mysql_real_escape_string($id);
  1060. sqlquery("DELETE FROM bigtree_feeds WHERE id = '$id'");
  1061. }
  1062. /*
  1063. Function: deleteFieldType
  1064. Deletes a field type and erases its files.
  1065. Parameters:
  1066. id - The id of the field type.
  1067. */
  1068. function deleteFieldType($id) {
  1069. unlink($GLOBALS["server_root"]."custom/admin/form-field-types/draw/$id.php");
  1070. unlink($GLOBALS["server_root"]."custom/admin/form-field-types/process/$id.php");
  1071. sqlquery("DELETE FROM bigtree_field_types WHERE id = '".mysql_real_escape_string($id)."'");
  1072. }
  1073. /*
  1074. Function: deleteModule
  1075. Deletes a module.
  1076. Parameters:
  1077. id - The id of the module.
  1078. */
  1079. function deleteModule($id) {
  1080. $id = mysql_real_escape_string($id);
  1081. // Get info and delete the class.
  1082. $module = $this->getModule($id);
  1083. unlink($GLOBALS["server_root"]."custom/inc/modules/".$module["route"].".php");
  1084. // Delete all the related auto module actions
  1085. $actions = $this->getModuleActions($id);
  1086. foreach ($actions as $action) {
  1087. if ($action["form"]) {
  1088. sqlquery("DELETE FROM bigtree_module_forms WHERE id = '".$action["form"]."'");
  1089. }
  1090. if ($action["view"]) {
  1091. sqlquery("DELETE FROM bigtree_module_views WHERE id = '".$action["view"]."'");
  1092. }
  1093. }
  1094. // Delete actions
  1095. sqlquery("DELETE FROM bigtree_module_actions WHERE module = '$id'");
  1096. // Delete the module
  1097. sqlquery("DELETE FROM bigtree_modules WHERE id = '$id'");
  1098. }
  1099. /*
  1100. Function: deleteModuleAction
  1101. Deletes a module action.
  1102. Also deletes the related form or view if no other action is using it.
  1103. Parameters:
  1104. id - The id of the action to delete.
  1105. */
  1106. function deleteModuleAction($id) {
  1107. $id = mysql_real_escape_string($id);
  1108. $a = $this->getModuleAction($id);
  1109. if ($a["form"]) {
  1110. // Only delete the auto-ness if it's the only one using it.
  1111. if (sqlrows(sqlquery("SELECT * FROM bigtree_module_actions WHERE form = '".$a["form"]."'")) == 1) {
  1112. sqlquery("DELETE FROM bigtree_module_forms WHERE id = '".$a["form"]."'");
  1113. }
  1114. }
  1115. if ($a["view"]) {
  1116. // Only delete the auto-ness if it's the only one using it.
  1117. if (sqlrows(sqlquery("SELECT * FROM bigtree_module_actions WHERE view = '".$a["view"]."'")) == 1) {
  1118. sqlquery("DELETE FROM bigtree_module_views WHERE id = '".$a["view"]."'");
  1119. }
  1120. }
  1121. sqlquery("DELETE FROM bigtree_module_actions WHERE id = '$id'");
  1122. }
  1123. /*
  1124. Function: deleteModuleForm
  1125. Deletes a module form and its related actions.
  1126. Parameters:
  1127. id - The id of the module form.
  1128. */
  1129. function deleteModuleForm($id) {
  1130. $id = mysql_real_escape_string($id);
  1131. sqlquery("DELETE FROM bigtree_module_forms WHERE id = '$id'");
  1132. sqlquery("DELETE FROM bigtree_module_actions WHERE form = '$id'");
  1133. }
  1134. /*
  1135. Function: deleteModuleGroup
  1136. Deletes a module group. Sets modules in the group to Misc.
  1137. Parameters:
  1138. id - The id of the module group.
  1139. */
  1140. function deleteModuleGroup($id) {
  1141. $id = mysql_real_escape_string($id);
  1142. sqlquery("DELETE FROM bigtree_module_groups WHERE id = '$id'");
  1143. sqlquery("UPDATE bigtree_modules SET `group` = '0' WHERE `group` = '$id'");
  1144. }
  1145. /*
  1146. Function: deleteModuleView
  1147. Deletes a module view and its related actions.
  1148. Parameters:
  1149. id - The id of the module view.
  1150. */
  1151. function deleteModuleView($id) {
  1152. $id = mysql_real_escape_string($id);
  1153. sqlquery("DELETE FROM bigtree_module_views WHERE id = '$id'");
  1154. sqlquery("DELETE FROM bigtree_module_actions WHERE view = '$id'");
  1155. }
  1156. /*
  1157. Function: deletePage
  1158. Deletes a page or a pending page.
  1159. Checks permissions.
  1160. Parameters:
  1161. page - A page id or a pending page id prefixed with a "p"
  1162. Returns:
  1163. true if successful. Stops page execution if permission issues occur.
  1164. */
  1165. function deletePage($page) {
  1166. global $cms;
  1167. $page = mysql_real_escape_string($page);
  1168. $r = $this->getPageAccessLevel($page);
  1169. if ($r == "p" && $this->canModifyChildren($cms->getPage($page))) {
  1170. // If the page isn't numeric it's most likely prefixed by the "p" so it's pending.
  1171. if (!is_numeric($page)) {
  1172. sqlquery("DELETE FROM bigtree_pending_changes WHERE id = '".mysql_real_escape_string(substr($page,1))."'");
  1173. $this->growl("Pages","Deleted Page");
  1174. $this->track("bigtree_pages","p$page","deleted-pending");
  1175. } else {
  1176. sqlquery("DELETE FROM bigtree_pages WHERE id = '$page'");
  1177. // Delete the children as well.
  1178. $this->deletePageChildren($page);
  1179. $this->growl("Pages","Deleted Page");
  1180. $this->track("bigtree_pages",$page,"deleted");
  1181. }
  1182. return true;
  1183. }
  1184. $this->stop("You do not have permission to delete this page.");
  1185. }
  1186. /*
  1187. Function: deletePageChildren
  1188. Deletes the children of a page and recurses downward.
  1189. Does not check permissions.
  1190. Parameters:
  1191. id - The parent id to delete children for.
  1192. */
  1193. function deletePageChildren($id) {
  1194. $q = sqlquery("SELECT * FROM bigtree_pages WHERE parent = '$id'");
  1195. while ($f = sqlfetch($q)) {
  1196. $this->deletePageChildren($f["id"]);
  1197. }
  1198. sqlquery("DELETE FROM bigtree_pages WHERE parent = '$id'");
  1199. $this->track("bigtree_pages",$id,"deleted");
  1200. }
  1201. /*
  1202. Function: deletePageDraft
  1203. Deletes a page draft.
  1204. Checks permissions.
  1205. Parameters:
  1206. id - The page id to delete the draft for.
  1207. */
  1208. function deletePageDraft($id) {
  1209. $id = mysql_real_escape_string($id);
  1210. // Get the version, check if the user has access to the page the version refers to.
  1211. $access = $this->getPageAccessLevel($id);
  1212. if ($access != "p") {
  1213. $this->stop("You must be a publisher to manage revisions.");
  1214. }
  1215. // Delete draft copy
  1216. sqlquery("DELETE FROM bigtree_pending_changes WHERE `table` = 'bigtree_pages' AND `item_id` = '$id'");
  1217. }
  1218. /*
  1219. Function: deletePageRevision
  1220. Deletes a page revision.
  1221. Checks permissions.
  1222. Parameters:
  1223. id - The page version id.
  1224. */
  1225. function deletePageRevision($id) {
  1226. // Get the version, check if the user has access to the page the version refers to.
  1227. $revision = $this->getPageRevision($id);
  1228. $access = $this->getPageAccessLevel($revision["page"]);
  1229. if ($access != "p") {
  1230. $this->stop("You must be a publisher to manage revisions.");
  1231. }
  1232. // Delete the revision
  1233. sqlquery("DELETE FROM bigtree_page_revisions WHERE id = '".$revision["id"]."'");
  1234. }
  1235. /*
  1236. Function: deletePendingChange
  1237. Deletes a pending change.
  1238. Parameters:
  1239. id - The id of the change.
  1240. */
  1241. function deletePendingChange($id) {
  1242. $id = mysql_real_escape_string($id);
  1243. sqlquery("DELETE FROM bigtree_pending_changes WHERE id = '$id'");
  1244. }
  1245. /*
  1246. Function: deleteSetting
  1247. Deletes a setting.
  1248. Parameters:
  1249. id - The id of the setting.
  1250. */
  1251. function deleteSetting($id) {
  1252. $id = mysql_real_escape_string($id);
  1253. sqlquery("DELETE FROM bigtree_settings WHERE id = '$id'");
  1254. }
  1255. /*
  1256. Function: deleteTemplate
  1257. Deletes a template.
  1258. Parameters:
  1259. id - The id of the template.
  1260. */
  1261. function deleteTemplate($id) {
  1262. $id = mysql_real_escape_string($id);
  1263. sqlquery("DELETE FROM bigtree_templates WHERE id = '$id'");
  1264. }
  1265. /*
  1266. Function: deleteUser
  1267. Deletes a user.
  1268. Checks for developer access.
  1269. Parameters:
  1270. id - The user id to delete.
  1271. Returns:
  1272. true if successful. false if the logged in user does not have permission to delete the user.
  1273. */
  1274. function deleteUser($id) {
  1275. $id = mysql_real_escape_string($id);
  1276. // If this person has higher access levels than the person trying to update them, fail.
  1277. $current = $this->getUser($id);
  1278. if ($current["level"] > $this->Level) {
  1279. return false;
  1280. }
  1281. sqlquery("DELETE FROM bigtree_users WHERE id = '$id'");
  1282. // Audit trail
  1283. $this->track("bigtree_users",$id,"deleted");
  1284. return true;
  1285. }
  1286. /*
  1287. Function: doesModuleEditActionExist
  1288. Determines whether there is already an edit action for a module.
  1289. Parameters:
  1290. module - The module id to check.
  1291. Returns:
  1292. 1 or 0, for true or false.
  1293. */
  1294. function doesModuleEditActionExist($mo

Large files files are truncated, but you can click here to view the full file