PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/objects/site.inc.php

https://github.com/adamfranco/segue-1.x
PHP | 1283 lines | 965 code | 146 blank | 172 comment | 164 complexity | 048a312269eb87e26202693628562711 MD5 | raw file
  1. <? /* $Id$ */
  2. class site extends segue {
  3. var $canview; // an array of editors that can view this site
  4. // (i.e. have view permissions somewhere in the hierarchy)
  5. var $owner, $owneremail, $ownerfname;
  6. var $title;
  7. var $sections;
  8. var $name;
  9. var $_allfields = array("name","title","theme","themesettings","header","footer",
  10. "addedby","addedbyfull", "editedby","editedtimestamp","addedtimestamp",
  11. "activatedate","deactivatedate","active","sections",
  12. "listed","type");
  13. // fields listed in $_datafields are stored in the database.
  14. // the first element is the table join syntax required to pull the data.
  15. // the second element is an array of the database fields we will be selecting
  16. // the third element is the database field by which we will sort
  17. var $_datafields = array(
  18. "id" => array(
  19. "site",
  20. array("site_id"),
  21. "site_id"
  22. ),
  23. "name" => array(
  24. "site
  25. INNER JOIN
  26. slot
  27. ON site_id = FK_site",
  28. array("slot_name"),
  29. "site_id"
  30. ),
  31. "type" => array(
  32. "site
  33. INNER JOIN
  34. slot
  35. ON site_id = FK_site",
  36. array("slot_type"),
  37. "site_id"
  38. ),
  39. "title" => array(
  40. "site",
  41. array("site_title"),
  42. "site_id"
  43. ),
  44. "activatedate" => array(
  45. "site",
  46. array("DATE_FORMAT(site_activate_tstamp, '%Y-%m-%d')"),
  47. "site_id"
  48. ),
  49. "deactivatedate" => array(
  50. "site",
  51. array("DATE_FORMAT(site_deactivate_tstamp, '%Y-%m-%d')"),
  52. "site_id"
  53. ),
  54. "active" => array(
  55. "site",
  56. array("site_active"),
  57. "site_id"
  58. ),
  59. "listed" => array(
  60. "site",
  61. array("site_listed"),
  62. "site_id"
  63. ),
  64. "theme" => array(
  65. "site",
  66. array("site_theme"),
  67. "site_id"
  68. ),
  69. "themesettings" => array(
  70. "site",
  71. array("site_themesettings"),
  72. "site_id"
  73. ),
  74. "header" => array(
  75. "site",
  76. array("site_header"),
  77. "site_id"
  78. ),
  79. "footer" => array(
  80. "site",
  81. array("site_footer"),
  82. "site_id"
  83. ),
  84. "editedby" => array(
  85. "site
  86. INNER JOIN
  87. user
  88. ON FK_updatedby = user_id",
  89. array("user_uname"),
  90. "site_id"
  91. ),
  92. "editedtimestamp" => array(
  93. "site",
  94. array("site_updated_tstamp"),
  95. "site_id"
  96. ),
  97. "addedby" => array(
  98. "site
  99. INNER JOIN
  100. user
  101. ON FK_createdby = user_id",
  102. array("user_uname"),
  103. "site_id"
  104. ),
  105. "addedbyfull" => array(
  106. "site
  107. INNER JOIN
  108. user
  109. ON FK_createdby = user_id",
  110. array("user_fname"),
  111. "site_id"
  112. ),
  113. "addedtimestamp" => array(
  114. "site",
  115. array("site_created_tstamp"),
  116. "site_id"
  117. ),
  118. "sections" => array(
  119. "site
  120. INNER JOIN
  121. section
  122. ON site_id = FK_site",
  123. array("section_id"),
  124. "section_order"
  125. )
  126. );
  127. var $_table = "site";
  128. function site($name) {
  129. // find if a site with this name already exists in the databse, and if yes, get site_id
  130. global $dbuser, $dbpass, $dbdb, $dbhost;
  131. db_connect($dbhost,$dbuser,$dbpass, $dbdb);
  132. $query = "
  133. SELECT
  134. site_id, site_title, user_email, user_uname, user_fname
  135. FROM
  136. site
  137. INNER JOIN
  138. slot
  139. ON
  140. site_id = FK_site AND slot_name = '".addslashes($name)."'
  141. INNER JOIN
  142. user
  143. ON
  144. user_id = FK_owner
  145. ";
  146. // printpre($query);
  147. $r = db_query($query);
  148. if (db_num_rows($r)) {
  149. $a = db_fetch_assoc($r);
  150. $this->id = $a[site_id];
  151. $this->owner = $a[user_uname];
  152. $this->ownerfname = $a[user_fname];
  153. $this->owneremail = $a[user_email];
  154. $this->title = $a[site_title];
  155. } else
  156. $this->site_does_not_exist = true;
  157. $this->name = $name;
  158. $this->owning_site = $name;
  159. $this->owningSiteObj =& $this;
  160. $this->fetchedup = 1;
  161. $this->sections = array();
  162. $this->data = array();
  163. // initialize the data array
  164. $this->data[name] = $name;
  165. $this->data[type] = "personal";
  166. $this->data[title] = "";
  167. $this->data[activatedate] = "0000-00-00";
  168. $this->data[deactivatedate] = "0000-00-00";
  169. $this->data[active] = 1;
  170. $this->data[listed] = 1;
  171. $this->data[theme] = "minimal";
  172. $this->data[themesettings] = "";
  173. $this->data[header] = "";
  174. $this->data[footer] = "";
  175. $this->data[sections] = array();
  176. // $this->data[sections][] = 'FUCKFUCKFUCK';
  177. }
  178. // ************************************************************************************************
  179. // ************************************************************************************************
  180. // description: just look at the function name
  181. // THIS IS A BAD ASS FUNCTION. BUT IS IS FAST!!!
  182. // @param $section_id, $page_id If these are specified the function will fetch along them
  183. // ************************************************************************************************
  184. // ************************************************************************************************
  185. function fetchSiteAtOnceForeverAndEverAndDontForgetThePermissionsAsWell_Amen($_section_id = 0, $_page_id = 0, $quick = false) {
  186. if ($this->site_does_not_exist) return false;
  187. if ($this->fetched_forever_and_ever) return $this->id;
  188. // no $full or $force here, always fetch everything, be strong and stubborn damnit!
  189. // connect to db and initialize data array
  190. global $dbuser, $dbpass, $dbdb, $dbhost;
  191. db_connect($dbhost,$dbuser,$dbpass, $dbdb);
  192. // delete temporary tables if they already exist
  193. $query = "DROP TABLE IF EXISTS t_sites";
  194. db_query($query);
  195. $query = "DROP TABLE IF EXISTS t_sections";
  196. db_query($query);
  197. $query = "DROP TABLE IF EXISTS t_pages";
  198. db_query($query);
  199. $query = "DROP TABLE IF EXISTS t_stories";
  200. db_query($query);
  201. // now, create the temporary tables. each table stores all siteunit ids for this site.
  202. // all stories for this site
  203. $query = "
  204. CREATE TEMPORARY TABLE t_stories(
  205. UNIQUE uniq (site_id,section_id,page_id,story_id),
  206. KEY site_id (site_id),
  207. KEY section_id (section_id),
  208. KEY page_id (page_id),
  209. KEY story_id (story_id)
  210. ) TYPE=MyISAM
  211. SELECT
  212. site_id, section_id, page_id, story_id, section_order, page_order, story_order
  213. FROM
  214. site
  215. LEFT JOIN
  216. section ON FK_site = site_id
  217. LEFT JOIN
  218. page ON FK_section = section_id
  219. LEFT JOIN
  220. story ON FK_page = page_id
  221. WHERE
  222. site_id = '".addslashes($this->id)."'
  223. ";
  224. db_query($query);
  225. // all pages for this site
  226. $query = "
  227. CREATE TEMPORARY TABLE t_pages (
  228. UNIQUE uniq (site_id, section_id, page_id),
  229. KEY site_id (site_id),
  230. KEY section_id (section_id),
  231. KEY page_id (page_id)
  232. )
  233. SELECT
  234. DISTINCT site_id, section_id, page_id, section_order, page_order
  235. FROM
  236. t_stories
  237. ";
  238. db_query($query);
  239. // all sections for this site
  240. $query = "
  241. CREATE TEMPORARY TABLE t_sections (
  242. UNIQUE uniq (site_id, section_id),
  243. KEY site_id (site_id),
  244. KEY section_id (section_id)
  245. )
  246. SELECT
  247. DISTINCT site_id, section_id, section_order
  248. FROM
  249. t_pages
  250. ";
  251. db_query($query);
  252. // all sites for this site, i.e. just this site
  253. $query = "
  254. CREATE TEMPORARY TABLE t_sites (
  255. UNIQUE uniq (site_id),
  256. KEY site_id (site_id)
  257. )
  258. SELECT
  259. DISTINCT site_id
  260. FROM
  261. t_sections
  262. ";
  263. db_query($query);
  264. // create the object hierarchy
  265. $this->data = array();
  266. $query = "SELECT site_id, section_id FROM t_sections ORDER BY section_order";
  267. $r = db_query($query);
  268. while ($a = db_fetch_assoc($r))
  269. if ($a[section_id] != null) {
  270. $section =& new section($this->name,$a[section_id],$this);
  271. $this->sections[$a[section_id]] =& $section;
  272. $this->data[sections][] = $a[section_id];
  273. $this->fetched[sections] = 1;
  274. }
  275. $query = "SELECT site_id, section_id, page_id FROM t_pages ORDER BY page_order";
  276. $r = db_query($query);
  277. while ($a = db_fetch_assoc($r))
  278. if ($a[section_id] != null && $a[page_id] != null) {
  279. $section =& $this->sections[$a[section_id]];
  280. $page =& new page($this->name,$a[section_id],$a[page_id],$section);
  281. $section->pages[$a[page_id]] =& $page;
  282. $section->data[pages][] = $a[page_id];
  283. $section->fetched[pages] = 1;
  284. }
  285. $query = "SELECT site_id, section_id, page_id, story_id FROM t_stories ORDER BY story_order";
  286. $r = db_query($query);
  287. while ($a = db_fetch_assoc($r))
  288. if ($a[section_id] != null && $a[page_id] != null && $a[story_id] != null) {
  289. $section =& $this->sections[$a[section_id]];
  290. $page =& $section->pages[$a[page_id]];
  291. $story =& new story($this->name,$a[section_id],$a[page_id],$a[story_id],$page);
  292. $page->stories[$a[story_id]] =& $story;
  293. $page->data[stories][] = $a[story_id];
  294. $page->fetched[stories] = 1;
  295. }
  296. // first, fetch the site
  297. $query = "
  298. SELECT site_title AS title, DATE_FORMAT(site_activate_tstamp, '%Y-%m-%d') AS activatedate, DATE_FORMAT(site_deactivate_tstamp, '%Y-%m-%d') AS deactivatedate,
  299. site_active AS active, site_listed AS listed, ".
  300. (($quick) ? "" : "site_theme AS theme, site_themesettings AS themesettings, site_header AS header, site_footer AS footer, ")
  301. ."site_updated_tstamp AS editedtimestamp, site_created_tstamp AS addedtimestamp,
  302. user_createdby.user_uname AS addedby, user_updatedby.user_uname AS editedby, slot_name as name, slot_type AS type
  303. FROM
  304. t_sites
  305. INNER JOIN
  306. site
  307. ON t_sites.site_id = site.site_id
  308. INNER JOIN
  309. user AS user_createdby
  310. ON FK_createdby = user_createdby.user_id
  311. INNER JOIN
  312. user AS user_updatedby
  313. ON FK_updatedby = user_updatedby.user_id
  314. INNER JOIN
  315. slot
  316. ON site.site_id = slot.FK_site
  317. ";
  318. $r = db_query($query);
  319. $a = db_fetch_assoc($r);
  320. array_change_key_case($a); // make all keys lower case
  321. // for each field returned by the query
  322. foreach ($a as $field => $value)
  323. // make sure we have defined this field in the _allfields array
  324. if (in_array($field,$this->_allfields)) {
  325. // decode if necessary
  326. if (in_array($field,$this->_encode))
  327. $value = stripslashes(urldecode($value));
  328. $this->data[$field] = $value;
  329. $this->fetched[$field] = 1;
  330. }
  331. else
  332. echo "ERROR: field $field not in _allfields!!!<br />";
  333. $this->fetcheddown = 1;
  334. $this->fetched_forever_and_ever = 1;
  335. // now, create section objects and fetch them
  336. $query = "
  337. SELECT
  338. section.section_id AS section_id".
  339. (($quick) ? " " :
  340. ", section_display_type AS type, section_title AS title, DATE_FORMAT(section_activate_tstamp, '%Y-%m-%d') AS activatedate, DATE_FORMAT(section_deactivate_tstamp, '%Y-%m-%d') AS deactivatedate,
  341. section_active AS active, section_locked AS locked, section_updated_tstamp AS editedtimestamp,
  342. section_created_tstamp AS addedtimestamp,
  343. user_createdby.user_uname AS addedby, user_updatedby.user_uname AS editedby, '".addslashes($this->name)."' as site_id,
  344. media_tag AS url ")
  345. ."FROM
  346. t_sections
  347. INNER JOIN
  348. section
  349. ON t_sections.section_id = section.section_id
  350. INNER JOIN
  351. user AS user_createdby
  352. ON section.FK_createdby = user_createdby.user_id
  353. INNER JOIN
  354. user AS user_updatedby
  355. ON section.FK_updatedby = user_updatedby.user_id
  356. LEFT JOIN
  357. media
  358. ON FK_media = media_id
  359. ";
  360. $r = db_query($query);
  361. while ($a = db_fetch_assoc($r)) {
  362. $section =& $this->sections[$a[section_id]];
  363. foreach ($a as $field => $value)
  364. // make sure we have defined this field in the _allfields array
  365. if ($field == 'section_id' || in_array($field,$section->_allfields)) {
  366. // decode if necessary
  367. if (in_array($field,$section->_encode))
  368. $value = stripslashes(urldecode($value));
  369. $section->data[$field] = $value;
  370. $section->fetched[$field] = 1;
  371. }
  372. else
  373. echo "ERROR: field $field not in _allfields!!!<br />";
  374. $section->fetcheddown = 1;
  375. $section->fetched_forever_and_ever = 1;
  376. }
  377. // now, create page objects and fetch them
  378. $query = "
  379. SELECT
  380. t_pages.section_id AS section_id, page.page_id AS page_id".
  381. (($quick) ? " " :
  382. ",
  383. page_display_type AS type,
  384. page_title AS title,
  385. page_text AS text,
  386. DATE_FORMAT(page_activate_tstamp, '%Y-%m-%d') AS activatedate,
  387. DATE_FORMAT(page_deactivate_tstamp, '%Y-%m-%d') AS deactivatedate,
  388. page_active AS active,
  389. page_story_order AS storyorder,
  390. page_show_creator AS showcreator,
  391. page_show_date AS showdate,
  392. page_show_hr AS showhr,
  393. page_archiveby AS archiveby,
  394. page_locked AS locked,
  395. page_updated_tstamp AS editedtimestamp,
  396. page_created_tstamp AS addedtimestamp,
  397. page_ediscussion AS ediscussion,
  398. user_createdby.user_uname AS addedby,
  399. user_updatedby.user_uname AS editedby,
  400. '".addslashes($this->name)."' as site_id,
  401. media_tag AS url,
  402. page_location AS location,
  403. page_show_editor AS showeditor")
  404. ."
  405. FROM
  406. t_pages
  407. INNER JOIN
  408. page
  409. ON t_pages.page_id = page.page_id
  410. INNER JOIN
  411. user AS user_createdby
  412. ON page.FK_createdby = user_createdby.user_id
  413. INNER JOIN
  414. user AS user_updatedby
  415. ON page.FK_updatedby = user_updatedby.user_id
  416. LEFT JOIN
  417. media
  418. ON page.FK_media = media_id
  419. ";
  420. if ($_section_id) $query = $query." WHERE section_id = '".addslashes($_section_id)."'";
  421. $r = db_query($query);
  422. while ($a = db_fetch_assoc($r)) {
  423. array_change_key_case($a); // make all keys lower case
  424. $page =& $this->sections[$a[section_id]]->pages[$a[page_id]];
  425. foreach ($a as $field => $value)
  426. // make sure we have defined this field in the _allfields array
  427. if ($field == 'page_id' || in_array($field,$page->_allfields)) {
  428. // decode if necessary
  429. if (in_array($field,$page->_encode))
  430. $value = stripslashes(urldecode($value));
  431. $page->data[$field] = $value;
  432. $page->fetched[$field] = 1;
  433. }
  434. else
  435. echo "ERROR: field $field not in _allfields!!!<br />";
  436. $page->fetcheddown = 1;
  437. $page->fetched_forever_and_ever = 1;
  438. }
  439. // now, create story objects and fetch them
  440. $query = "
  441. SELECT
  442. t_stories.section_id AS section_id,
  443. t_stories.page_id AS page_id,
  444. story.story_id AS story_id".
  445. (($quick) ? " " :
  446. ", story_display_type AS type,
  447. story_title AS title,
  448. DATE_FORMAT(story_activate_tstamp, '%Y-%m-%d') AS activatedate,
  449. DATE_FORMAT(story_deactivate_tstamp, '%Y-%m-%d') AS deactivatedate,
  450. story_active AS active,
  451. story_locked AS locked,
  452. story_updated_tstamp AS editedtimestamp,
  453. story_created_tstamp AS addedtimestamp,
  454. story_discussable AS discuss,
  455. story_discussemail AS discussemail,
  456. story_discusslabel AS discusslabel,
  457. story_discussdisplay AS discussdisplay,
  458. story_discussauthor AS discussauthor,
  459. story_category AS category,
  460. story_text_type AS texttype,
  461. story_text_short AS shorttext,
  462. story_text_long AS longertext,
  463. media_tag AS url,
  464. user_createdby.user_uname AS addedby,
  465. user_updatedby.user_uname AS editedby,
  466. '".$this->name."' as site_id ")
  467. ."FROM
  468. t_stories
  469. INNER JOIN
  470. story
  471. ON t_stories.story_id = story.story_id
  472. INNER JOIN
  473. user AS user_createdby
  474. ON story.FK_createdby = user_createdby.user_id
  475. INNER JOIN
  476. user AS user_updatedby
  477. ON story.FK_updatedby = user_updatedby.user_id
  478. LEFT JOIN
  479. media
  480. ON story.FK_media = media_id
  481. ";
  482. if ($_section_id) {
  483. $query = $query." WHERE section_id = '".addslashes($_section_id)."'";
  484. if ($_page_id) $query = $query." AND page_id = '".addslashes($_page_id)."'";
  485. }
  486. $r = db_query($query);
  487. while ($a = db_fetch_assoc($r)) {
  488. array_change_key_case($a); // make all keys lower case
  489. $story =& $this->sections[$a[section_id]]->pages[$a[page_id]]->stories[$a[story_id]];
  490. foreach ($a as $field => $value)
  491. // make sure we have defined this field in the _allfields array
  492. if ($field == 'story_id' || in_array($field,$story->_allfields)) {
  493. // decode if necessary
  494. if (in_array($field,$story->_encode))
  495. $value = stripslashes(urldecode($value));
  496. $story->data[$field] = $value;
  497. $story->fetched[$field] = 1;
  498. }
  499. else
  500. echo "ERROR: field $field not in _allfields!!!<br />";
  501. $story->fetcheddown = 1;
  502. $story->fetched_forever_and_ever = 1;
  503. }
  504. $query = "
  505. SELECT
  506. user_uname as editor, ugroup_name as editor2, site_editors_type as editor_type,
  507. MAKE_SET(IFNULL((permission_value+0),0), 'v', 'a', 'e', 'd', 'di') as permissions
  508. FROM
  509. t_sites
  510. INNER JOIN
  511. site_editors ON
  512. site_id = FK_site
  513. LEFT JOIN
  514. user
  515. ON site_editors.FK_editor = user_id
  516. LEFT JOIN
  517. ugroup
  518. ON site_editors.FK_editor = ugroup_id
  519. LEFT JOIN
  520. permission ON
  521. site_id = FK_scope_id
  522. AND
  523. permission_scope_type = 'site'
  524. AND
  525. permission.FK_editor <=> site_editors.FK_editor
  526. AND
  527. permission_editor_type = site_editors_type
  528. ";
  529. $r = db_query($query);
  530. $this->editors = array();
  531. $this->permissions = array();
  532. $this->cachedPermissions = array();
  533. // for every permisson entry, add it to the permissions array
  534. while ($row=db_fetch_assoc($r)) {
  535. // decode 'final_permissions';
  536. // 'final_permissions' is a field returned by the query and contains a string of the form "'a','vi','e'" etc.
  537. $a = array();
  538. $a[a] = (strpos($row[permissions],'a') !== false) ? 1 : 0; // look for 'a' in 'final_permissions'
  539. $a[e] = (strpos($row[permissions],'e') !== false) ? 1 : 0; // !== is very important here, because a position 0 is interpreted by != as FALSE
  540. $a[d] = (strpos($row[permissions],'d') !== false && (strpos($row[permissions],'d') !== strpos($row[permissions],'di'))) ? 1 : 0;
  541. $a[v] = (strpos($row[permissions],'v') !== false) ? 1 : 0;
  542. $a[di] = (strpos($row[permissions],'di') !== false) ? 1 : 0;
  543. // if the editor is a user then the editor's name is just the user name
  544. // if the editor is 'institute' or 'everyone' then set the editor's name correspondingly
  545. if ($row[editor_type]=='user')
  546. $t_editor = $row[editor];
  547. else if ($row[editor_type]=='ugroup')
  548. $t_editor = $row[editor2];
  549. else
  550. $t_editor = $row[editor_type];
  551. // echo "<br /><br />Editor: $t_editor; Add: $a[a]; Edit: $a[e]; Delete: $a[d]; View: $a[v]; Discuss: $a[di]; On the Site";
  552. // set the permissions for this editor
  553. $this->permissions[$t_editor] = array(
  554. permissions::ADD()=>($a[a] || ($this->permissions[$t_editor] && $this->permissions[$t_editor][permissions::ADD()])),
  555. permissions::EDIT()=>($a[e] || ($this->permissions[$t_editor] && $this->permissions[$t_editor][permissions::EDIT()])),
  556. permissions::DELETE()=>($a[d] || ($this->permissions[$t_editor] && $this->permissions[$t_editor][permissions::DELETE()])),
  557. permissions::VIEW()=>($a[v] || ($this->permissions[$t_editor] && $this->permissions[$t_editor][permissions::VIEW()])),
  558. permissions::DISCUSS()=>($a[di] || ($this->permissions[$t_editor] && $this->permissions[$t_editor][permissions::DISCUSS()]))
  559. );
  560. if ($a[v])
  561. $this->canview[$t_editor] = 1;
  562. /* $this->cachedPermissions = array(
  563. $t_editor."ADD"=>$a[a],
  564. $t_editor."EDIT"=>$a[e],
  565. $t_editor."DELETE"=>$a[d],
  566. $t_editor."VIEW"=>$a[v],
  567. $t_editor."DISCUSS"=>$a[di]
  568. );*/
  569. // now add the editor to the editor array
  570. $this->editors[]=$t_editor;
  571. }
  572. // now, inherit the permissions to the children
  573. foreach (array_keys($this->sections) as $key => $section_id) {
  574. $this->sections[$section_id]->editors = $this->editors;
  575. $this->sections[$section_id]->permissions = $this->permissions;
  576. }
  577. $this->builtPermissions=1;
  578. $query = "
  579. SELECT
  580. section_id, user_uname as editor, ugroup_name as editor2, site_editors_type as editor_type,
  581. MAKE_SET(IFNULL((permission_value+0),0), 'v', 'a', 'e', 'd', 'di') as permissions
  582. FROM
  583. t_sections
  584. INNER JOIN
  585. site_editors ON
  586. site_id = site_editors.FK_site
  587. LEFT JOIN
  588. user ON
  589. site_editors.FK_editor = user_id
  590. LEFT JOIN
  591. ugroup ON
  592. site_editors.FK_editor = ugroup_id
  593. INNER JOIN
  594. permission ON
  595. section_id = FK_scope_id
  596. AND
  597. permission_scope_type = 'section'
  598. AND
  599. permission.FK_editor <=> site_editors.FK_editor
  600. AND
  601. permission_editor_type = site_editors_type
  602. ";
  603. $r = db_query($query);
  604. // for every permisson entry, add it to the permissions array
  605. while ($row=db_fetch_assoc($r)) {
  606. // decode 'final_permissions';
  607. // 'final_permissions' is a field returned by the query and contains a string of the form "'a','vi','e'" etc.
  608. $a = array();
  609. if (strpos($row[permissions],'a') !== false) $a[permissions::ADD()] = 1; // look for 'a' in 'final_permissions'
  610. if (strpos($row[permissions],'e') !== false) $a[permissions::EDIT()] = 1; // !== is very important here, because a position 0 is interpreted by != as FALSE
  611. if (strpos($row[permissions],'d') !== false && (strpos($row[permissions],'d') !== strpos($row[permissions],'di'))) $a[permissions::DELETE()] = 1;
  612. if (strpos($row[permissions],'v') !== false) $a[permissions::VIEW()] = 1;
  613. if (strpos($row[permissions],'di') !== false) $a[permissions::DISCUSS()] = 1;
  614. // if the editor is a user then the editor's name is just the user name
  615. // if the editor is 'institute' or 'everyone' then set the editor's name correspondingly
  616. if ($row[editor_type]=='user')
  617. $t_editor = $row[editor];
  618. else if ($row[editor_type]=='ugroup')
  619. $t_editor = $row[editor2];
  620. else
  621. $t_editor = $row[editor_type];
  622. // echo "<br /><br />Editor: $t_editor; Add: $a[a]; Edit: $a[e]; Delete: $a[d]; View: $a[v]; Discuss: $a[di]; on Section ".$row[section_id];
  623. foreach ($a as $key => $value)
  624. $this->sections[$row[section_id]]->permissions[$t_editor][$key] = 1;
  625. if ($a[permissions::VIEW()] && !$this->canview[$t_editor])
  626. $this->canview[$t_editor] = 1;
  627. }
  628. // now, inherit the permissions to the children
  629. foreach (array_keys($this->sections) as $key1 => $section_id) {
  630. foreach(array_keys($this->sections[$section_id]->pages) as $key2 => $page_id) {
  631. $this->sections[$section_id]->pages[$page_id]->editors = $this->sections[$section_id]->editors;
  632. $this->sections[$section_id]->pages[$page_id]->permissions = $this->sections[$section_id]->permissions;
  633. }
  634. $this->sections[$section_id]->builtPermissions=1;
  635. }
  636. $query = "
  637. SELECT
  638. section_id, page_id, user_uname as editor, ugroup_name as editor2, site_editors_type as editor_type,
  639. MAKE_SET(IFNULL((permission_value+0),0), 'v', 'a', 'e', 'd', 'di') as permissions
  640. FROM
  641. t_pages
  642. INNER JOIN
  643. site_editors ON
  644. site_id = site_editors.FK_site
  645. LEFT JOIN
  646. user ON
  647. site_editors.FK_editor = user_id
  648. LEFT JOIN
  649. ugroup ON
  650. site_editors.FK_editor = ugroup_id
  651. INNER JOIN
  652. permission ON
  653. page_id = FK_scope_id
  654. AND
  655. permission_scope_type = 'page'
  656. AND
  657. permission.FK_editor <=> site_editors.FK_editor
  658. AND
  659. permission_editor_type = site_editors_type
  660. ";
  661. $r = db_query($query);
  662. // for every permisson entry, add it to the permissions array
  663. while ($row=db_fetch_assoc($r)) {
  664. // decode 'final_permissions';
  665. // 'final_permissions' is a field returned by the query and contains a string of the form "'a','vi','e'" etc.
  666. $a = array();
  667. if (strpos($row[permissions],'a') !== false) $a[permissions::ADD()] = 1; // look for 'a' in 'final_permissions'
  668. if (strpos($row[permissions],'e') !== false) $a[permissions::EDIT()] = 1; // !== is very important here, because a position 0 is interpreted by != as FALSE
  669. if (strpos($row[permissions],'d') !== false && (strpos($row[permissions],'d') !== strpos($row[permissions],'di'))) $a[permissions::DELETE()] = 1;
  670. if (strpos($row[permissions],'v') !== false) $a[permissions::VIEW()] = 1;
  671. if (strpos($row[permissions],'di') !== false) $a[permissions::DISCUSS()] = 1;
  672. // if the editor is a user then the editor's name is just the user name
  673. // if the editor is 'institute' or 'everyone' then set the editor's name correspondingly
  674. if ($row[editor_type]=='user')
  675. $t_editor = $row[editor];
  676. else if ($row[editor_type]=='ugroup')
  677. $t_editor = $row[editor2];
  678. else
  679. $t_editor = $row[editor_type];
  680. // echo "<br /><br />Editor: $t_editor; Add: $a[a]; Edit: $a[e]; Delete: $a[d]; View: $a[v]; Discuss: $a[di];";
  681. foreach ($a as $key => $value)
  682. $this->sections[$row[section_id]]->pages[$row[page_id]]->permissions[$t_editor][$key] = 1;
  683. if ($a[permissions::VIEW()] && !$this->canview[$t_editor])
  684. $this->canview[$t_editor] = 1;
  685. }
  686. // now, inherit the permissions to the children
  687. foreach (array_keys($this->sections) as $key1 => $section_id)
  688. foreach(array_keys($this->sections[$section_id]->pages) as $key2 => $page_id) {
  689. foreach(array_keys($this->sections[$section_id]->pages[$page_id]->stories) as $key3 => $story_id) {
  690. $this->sections[$section_id]->pages[$page_id]->stories[$story_id]->editors = $this->sections[$section_id]->pages[$page_id]->editors;
  691. $this->sections[$section_id]->pages[$page_id]->stories[$story_id]->permissions = $this->sections[$section_id]->pages[$page_id]->permissions;
  692. $this->sections[$section_id]->pages[$page_id]->stories[$story_id]->builtPermissions=1;
  693. }
  694. $this->sections[$section_id]->pages[$page_id]->builtPermissions=1;
  695. }
  696. $query = "
  697. SELECT
  698. section_id, page_id, story_id, user_uname as editor, ugroup_name as editor2, site_editors_type as editor_type, permission_id,
  699. MAKE_SET(IFNULL((permission_value+0),0), 'v', 'a', 'e', 'd', 'di') as permissions
  700. FROM
  701. t_stories
  702. INNER JOIN
  703. site_editors ON
  704. site_id = site_editors.FK_site
  705. LEFT JOIN
  706. user ON
  707. site_editors.FK_editor = user_id
  708. LEFT JOIN
  709. ugroup ON
  710. site_editors.FK_editor = ugroup_id
  711. INNER JOIN
  712. permission ON
  713. story_id = FK_scope_id
  714. AND
  715. permission_scope_type = 'story'
  716. AND
  717. permission.FK_editor <=> site_editors.FK_editor
  718. AND
  719. permission_editor_type = site_editors_type
  720. ";
  721. $r = db_query($query);
  722. // for every permisson entry, add it to the permissions array
  723. while ($row=db_fetch_assoc($r)) {
  724. // decode 'final_permissions';
  725. // 'final_permissions' is a field returned by the query and contains a string of the form "'a','vi','e'" etc.
  726. $a = array();
  727. // printpre($row);
  728. // if the editor is a user then the editor's name is just the user name
  729. // if the editor is 'institute' or 'everyone' then set the editor's name correspondingly
  730. if ($row[editor_type]=='user')
  731. $t_editor = $row[editor];
  732. else if ($row[editor_type]=='ugroup')
  733. $t_editor = $row[editor2];
  734. else
  735. $t_editor = $row[editor_type];
  736. // Everyone and institute can't have add, edit, or delete permissions.
  737. // Somehow, these were added sometimes. If this is the case, prevent
  738. // these from being set and reset those for the site.
  739. if ($t_editor == 'everyone' || $t_editor == 'institute') {
  740. // If we have a bad permission, do cleanup.
  741. if ((strpos($row[permissions],'a') !== false)
  742. || (strpos($row[permissions],'e') !== false)
  743. ||(strpos($row[permissions],'d') !== false && (strpos($row[permissions],'d') !== strpos($row[permissions],'di')))
  744. ) {
  745. printError ("Invalid add, edit, or delete permissions for $t_editor: permissionId - ".$row[permission_id]."; permission - ".$row[permissions]."\n<br />Cleaning up Database.");
  746. // Clean up the permissions
  747. $this->owningSiteObj->setUserPermissionDown('add', $t_editor, 0);
  748. $this->owningSiteObj->setUserPermissionDown('edit', $t_editor, 0);
  749. $this->owningSiteObj->setUserPermissionDown('delete', $t_editor, 0);
  750. $this->owningSiteObj->updatePermissionsDB(TRUE);
  751. if (is_numeric($row[permission_id])) {
  752. if ($row[permissions]!='d')
  753. $cleanupQuery = "UPDATE permission SET permission_value='di' WHERE permission_id='".addslashes($row[permission_id])."'";
  754. else
  755. $cleanupQuery = "DELETE FROM permission WHERE permission_id='".addslashes($row[permission_id])."'";
  756. $cleanupResult = db_query($cleanupQuery);
  757. }
  758. }
  759. }
  760. // Assign Add, edit, and delete permissions as needed.
  761. else {
  762. if (strpos($row[permissions],'a') !== false) $a[permissions::ADD()] = 1; // look for 'a' in 'final_permissions'
  763. if (strpos($row[permissions],'e') !== false) $a[permissions::EDIT()] = 1; // !== is very important here, because a position 0 is interpreted by != as FALSE
  764. if (strpos($row[permissions],'d') !== false && (strpos($row[permissions],'d') !== strpos($row[permissions],'di'))) $a[permissions::DELETE()] = 1;
  765. }
  766. if (strpos($row[permissions],'v') !== false) $a[permissions::VIEW()] = 1;
  767. if (strpos($row[permissions],'di') !== false) $a[permissions::DISCUSS()] = 1;
  768. // print_r($a); //debug
  769. // echo "<br /><br />Editor: $t_editor; Add: $a[a]; Edit: $a[e]; Delete: $a[d]; View: $a[v]; Discuss: $a[di]; On story id ".$row[story_id];
  770. foreach ($a as $key => $value)
  771. $this->sections[$row[section_id]]->pages[$row[page_id]]->stories[$row[story_id]]->permissions[$t_editor][$key] = 1;
  772. if ($a[permissions::VIEW()] && !$this->canview[$t_editor])
  773. $this->canview[$t_editor] = 1;
  774. }
  775. return $this->id;
  776. }
  777. function fetchDown($full=0) {
  778. if (!$this->fetcheddown || $full) {
  779. /* print "site fetchdown ".$this->name."<br />"; */
  780. if (!$this->tobefetched) $this->fetchFromDB($full);
  781. foreach ($this->getField("sections") as $s) {
  782. $this->sections[$s] =& new section($this->name,$s,$this);
  783. $this->sections[$s]->fetchDown($full);
  784. }
  785. $this->fetcheddown = 1;
  786. }
  787. }
  788. function fetchUp() {
  789. if (!$this->fetchedup) {
  790. $this->owningSiteObj = &$this;
  791. $this->fetchedup = 1;
  792. }
  793. }
  794. function fetchFromDB($force=0) {
  795. if ($this->site_does_not_exist) return false;
  796. global $dbuser, $dbpass, $dbdb, $dbhost;
  797. global $cfg;
  798. // take this out when appropriate & replace occurences;
  799. global $uploaddir;
  800. $this->tobefetched=1;
  801. //$this->id = $this->getField("id"); // why need to do this?
  802. if ($force) {
  803. // the code below is inefficient! why fetch each field separately when we can fetch all fields at same time
  804. // thus we can cut the number of queries significantly
  805. /* foreach ($this->_allfields as $f) {
  806. $this->getField($f);
  807. }
  808. */
  809. // connect to db and initialize data array
  810. db_connect($dbhost,$dbuser,$dbpass, $dbdb);
  811. $this->data = array();
  812. // first fetch all fields that are not part of a 1-to-many relationship
  813. $query = "
  814. SELECT site_title AS title, DATE_FORMAT(site_activate_tstamp, '%Y-%m-%d') AS activatedate, DATE_FORMAT(site_deactivate_tstamp, '%Y-%m-%d') AS deactivatedate,
  815. site_active AS active, site_listed AS listed, site_theme AS theme, site_themesettings AS themesettings,
  816. site_header AS header, site_footer AS footer, site_updated_tstamp AS editedtimestamp, site_created_tstamp AS addedtimestamp,
  817. user_createdby.user_uname AS addedby, user_updatedby.user_uname AS editedby, slot_name as name, slot_type AS type
  818. FROM
  819. site
  820. INNER JOIN
  821. user AS user_createdby
  822. ON FK_createdby = user_createdby.user_id
  823. INNER JOIN
  824. user AS user_updatedby
  825. ON FK_updatedby = user_updatedby.user_id
  826. INNER JOIN
  827. slot
  828. ON site_id = FK_site
  829. WHERE site_id = '".addslashes($this->id)."'";
  830. /* print "<pre>"; */
  831. /* print_r ($this); */
  832. /* print "</pre>"; */
  833. /* print "\$query=<br />$query<br />"; */
  834. $r = db_query($query);
  835. /* print "\$r=".$r."<br />"; */
  836. // if the site does not exist in the database
  837. if (!db_num_rows($r)) return false;
  838. $a = db_fetch_assoc($r);
  839. /* print "\$a=$a"; */
  840. array_change_key_case($a); // make all keys lower case
  841. // for each field returned by the query
  842. foreach ($a as $field => $value)
  843. // make sure we have defined this field in the _allfields array
  844. if (in_array($field,$this->_allfields)) {
  845. // decode if necessary
  846. if (in_array($field,$this->_encode))
  847. $value = stripslashes(urldecode($value));
  848. $this->data[$field] = $value;
  849. $this->fetched[$field] = 1;
  850. }
  851. else
  852. echo "ERROR: field $field not in _allfields!!!<br />";
  853. // now fetch the sections (they are part of a 1-to-many relationship and therefore
  854. // we cannot fetch them along with the other fields)
  855. $query = "
  856. SELECT
  857. section_id
  858. FROM
  859. site
  860. INNER JOIN
  861. section
  862. ON site_id = FK_site
  863. WHERE site_id = '".addslashes($this->id)."'
  864. ORDER BY
  865. section_order
  866. ";
  867. $r = db_query($query);
  868. $this->data[sections] = array();
  869. while ($a = db_fetch_assoc($r))
  870. $this->data[sections][] = $a[section_id];
  871. $this->fetched[sections] = 1;
  872. }
  873. return $this->id;
  874. }
  875. function applyTemplate ($template) {
  876. $templateObj =& new site($template);
  877. $templateObj->fetchDown(1);
  878. /* print "<pre>"; print_r($this); print_r($templateObj); print "</pre>"; */
  879. if (!$this->name) {
  880. print ("Site doesn't exist. Can't add template to it. Please contact the administrator with the steps that you did to get to this point.");
  881. exit;
  882. }
  883. // Make a list of all of the parts in the site
  884. makeSiteHash($templateObj);
  885. foreach ($templateObj->sections as $i=>$oldSection) {
  886. $oldSectionId = $oldSection->id;
  887. // Flag our old Id as the one that will be set in the next call to
  888. // section::updateDB(). This is awful, but the way it works.
  889. $GLOBALS['__site_hash']['sections'][$oldSectionId] = 'NEXT';
  890. // Do the copy, this will change the id of the object to the new one
  891. // and in the process, update the global __site_hash relationship.
  892. $oldSection->copyObj($this);
  893. $newSectionId = $oldSection->id;
  894. // re-fetch the site to get a reference to a clean instance of our new
  895. // site and section.
  896. $newSiteObj =& new site($this->name);
  897. $newSiteObj->fetchSiteAtOnceForeverAndEverAndDontForgetThePermissionsAsWell_Amen();
  898. $newPartObj =& $newSiteObj->sections[$newSectionId];
  899. // Convert internall links to our section based on the __site_hash
  900. updateSiteLinksFromHash($newSiteObj, $newPartObj);
  901. $newSiteObj->updateDB(1,1);
  902. }
  903. }
  904. function setSiteName($name, $copySite=0) {
  905. if ($this->tobefetched && !$copySite) { // we are trying to change the name of an existing site!! bad.
  906. return 0;
  907. }
  908. $this->name = $this->owning_site = $name;
  909. $this->setField("name",$name);
  910. return 1;
  911. }
  912. /******************************************************************************
  913. * copySite - clearPermissions currently has no effect. All permissions are cleared.
  914. ******************************************************************************/
  915. function copySite($newName, $clearPermissions=1, $copyDiscussions=FALSE) {
  916. if ($newName == $this->name) return FALSE;
  917. if ($newName == "" || !$newName) return FALSE;
  918. $oldName = $this->name;
  919. $this->fetchSiteAtOnceForeverAndEverAndDontForgetThePermissionsAsWell_Amen();
  920. // Make a hash array of site, section, and page ids so that
  921. makeSiteHash($this);
  922. $newSiteObj = $this;
  923. $newSiteObj->setSiteName($newName, 1);
  924. // Since we are specifying TRUE for the 'copy' option, each
  925. // part should add its new id to the global hash
  926. $newSiteObj->insertDB(1, 1, 0, $copyDiscussions);
  927. // Copy all the media
  928. $query = "
  929. SELECT
  930. media_id
  931. FROM
  932. media
  933. INNER JOIN
  934. slot
  935. ON
  936. media.FK_site = slot.FK_site
  937. WHERE
  938. slot_name='".addslashes($oldName)."'
  939. AND
  940. media_type != 'other'
  941. ";
  942. $r = db_query($query);
  943. while ($a = db_fetch_assoc($r)) {
  944. copy_media($a['media_id'], $newName);
  945. }
  946. $newSiteObj = NULL;
  947. unset($newSiteObj);
  948. $newSiteObj = new site($newName);
  949. $newSiteObj->fetchSiteAtOnceForeverAndEverAndDontForgetThePermissionsAsWell_Amen();
  950. // Remove the permissions if we are clearing them.
  951. if ($clearPermissions) {
  952. $editors = $newSiteObj->getEditors();
  953. foreach ($editors as $editor) {
  954. $newSiteObj->delEditor($editor);
  955. }
  956. }
  957. // Parse through all the text for links refering to parts of the
  958. // old site and update them with the new ids.
  959. updateSiteLinksFromHash($newSiteObj, $newSiteObj);
  960. $newSiteObj->updateDB(1,1);
  961. // Delete any editors that we wanted to delete.
  962. $newSiteObj->deletePendingEditors();
  963. }
  964. function updateDB($down=0, $force=0, $keepEditHistory=FALSE) {
  965. if (count($this->changed)) {
  966. // the easy step: update the fields in the table
  967. $a = $this->createSQLArray();
  968. if ($keepEditHistory) {
  969. $a[] = $this->_datafields[editedtimestamp][1][0]."='".addslashes($this->getField("editedtimestamp"))."'";
  970. } else
  971. $a[] = "FK_updatedby='".addslashes($_SESSION[aid])."'";
  972. $query = "UPDATE site SET ".implode(",",$a)." WHERE site_id='".addslashes($this->id)."'";
  973. /* print "site->updateDB: $query<br />"; */
  974. db_query($query);
  975. /* print mysql_error()."<br />"; */
  976. // the hard step: update the fields in the JOIN tables
  977. // first update 'slot_name' in the slot table, if the latter has changed
  978. if ($this->changed[name] && $this->data[name]) {
  979. $new_name = $this->data[name];
  980. $query = "UPDATE slot SET slot_name = '$new_name' WHERE FK_site='".addslashes($this->id)."'";
  981. db_query($query);
  982. }
  983. /* // now update all the section ids in the children, if the latter have changed */
  984. /* if ($this->changed[sections]) { */
  985. /* // first, a precautionary step: reset the parent of every section that used to have this site object as the parent */
  986. /* // we do this, because we might have removed a certain section from the array of sections of a site object */
  987. /* $query = "UPDATE section SET FK_site=0 WHERE FK_site=".$this->id; */
  988. /* db_query($query); */
  989. /* */
  990. /* // now, update all sections */
  991. /* foreach ($this->data['sections'] as $k=>$v) { */
  992. /* $query = "UPDATE section SET FK_site=".$this->id.", section_order=$k WHERE section_id=".$v; */
  993. /* db_query($query); */
  994. /* } */
  995. /* */
  996. /* } */
  997. }
  998. // now update the permissions
  999. $this->updatePermissionsDB($force);
  1000. // add log entry
  1001. /* log_entry("edit_site",$this->name,"","","$_SESSION[auser] edited ".$this->name); */
  1002. // update down
  1003. if ($down) {
  1004. if ($this->fetcheddown && $this->sections) {
  1005. foreach (array_keys($this->sections) as $k=>$i) $this->sections[$i]->updateDB($down, $force, $keepEditHistory);
  1006. }
  1007. }
  1008. return 1;
  1009. }
  1010. function insertDB($down=0,$copysite=0,$importing=0, $keepDiscussions=0) {
  1011. $a = $this->createSQLArray(1);
  1012. if (!$importing) {
  1013. $a[] = "FK_createdby='".addslashes($_SESSION[aid])."'";
  1014. $a[] = $this->_datafields[addedtimestamp][1][0]."=NOW()";
  1015. $a[] = "FK_updatedby='".addslashes($_SESSION[aid])."'";
  1016. } else {
  1017. $a[] = "FK_createdby=".db_get_value("user","user_id","user_uname='".addslashes($this->data[addedby])."'");
  1018. $a[] = $this->_datafields[addedtimestamp][1][0]."='".addslashes($this->getField("addedtimestamp"))."'";
  1019. $a[] = "FK_updatedby=".db_get_value("user","user_id","user_uname='".addslashes($this->data[editedby])."'");
  1020. $a[] = $this->_datafields[editedtimestamp][1][0]."='".addslashes($this->getField("editedtimestamp"))."'";
  1021. }
  1022. // insert into the site table
  1023. $query = "INSERT INTO site SET ".implode(",",$a).";";
  1024. /* print "<br />query = $query<br />"; */
  1025. db_query($query);
  1026. $this->id = lastid();
  1027. /* print "<H1>ID = ".$this->id."</H1>"; */
  1028. // in order to insert a site, the active user must own a slot
  1029. // update the name for that slot
  1030. if (slot::exists($this->data[name])) {
  1031. $query = "UPDATE slot";
  1032. $where = " WHERE slot_name = '".addslashes($this->data[name])."' AND FK_owner = '".addslashes($_SESSION[aid])."'";
  1033. } else {
  1034. $query = "INSERT INTO slot";
  1035. $where = "";
  1036. }
  1037. $query .= "
  1038. SET
  1039. slot_name = '".addslashes($this->data[name])."',
  1040. FK_owner = '".addslashes($_SESSION[aid])."',
  1041. slot_type = '".addslashes($this->data[type])."',
  1042. FK_site = '".addslashes($this->id)."'".$where;
  1043. /* echo $query."<br />"; */
  1044. db_query($query);
  1045. // See if there is a site hash (meaning that we are being copied).
  1046. // If so, try to match our id with the hash entry for 'NEXT'.
  1047. if ($GLOBALS['__site_hash']['site']
  1048. && $oldId = array_search('NEXT', $GLOBALS['__site_hash']['site']))
  1049. {
  1050. $GLOBALS['__site_hash']['site'][$oldId] = $this->name;
  1051. }
  1052. // the sections haven't been created yet, so we don't have to insert data[sections] for now
  1053. // add new permissions entry.. force update
  1054. $this->updatePermissionsDB(1);
  1055. // add log entry
  1056. /* log_entry("add_site",$this->name,"","","$_SESSION[auser] added ".$this->name); */
  1057. // insert down (insert sections)
  1058. if ($down && $this->fetcheddown && $this->sections) {
  1059. foreach (array_keys($this->sections) as $id) {
  1060. // Mark our Id as the next one to set
  1061. if (is_array($GLOBALS['__site_hash']['sections']))
  1062. $GLOBALS['__site_hash']['sections'][$id] = 'NEXT';
  1063. $this->sections[$id]->id = 0; // createSQLArray uses this to tell if we are inserting or updating
  1064. $this->sections[$id]->insertDB(1,$this->name,$copysite, $importing, $keepDiscussions);
  1065. }
  1066. }
  1067. return 1;
  1068. }
  1069. function addSection($id) {
  1070. if (!is_array($this->getField("sections"))) $this->data[sections] = array();
  1071. /* print "<br />adding section $id to ".$this->name."<br />"; //debug */
  1072. array_push($this->data[sections],$id);
  1073. $this->changed[sections] = 1;
  1074. /* print "<pre>this: "; print_r($this->data[sections]); print "</pre>"; */
  1075. }
  1076. function delSection($id,$delete=1) {
  1077. $d = array();
  1078. foreach ($this->getField("sections") as $n)
  1079. if ($n != $id) $d[] = $n;
  1080. $this->data[sections] = $d;
  1081. $this->changed[sections] = 1;
  1082. if ($delete) {
  1083. $section =& new section($this->name,$id,$this);
  1084. $section->delete();
  1085. }
  1086. }
  1087. function delete() { // delete from db
  1088. global $cfg;
  1089. if (!$this->id) return false;
  1090. $this->fetchDown();
  1091. $siteName = $this->getField("name");
  1092. $query = "DELETE FROM site WHERE site_id= '".addslashes($this->id)."'";
  1093. db_query($query);
  1094. $query = "DELETE FROM permission WHERE FK_scope_id='".addslashes($this->id)."' AND permission_scope_type='site';";
  1095. db_query($query);
  1096. $query = "DELETE FROM media WHERE FK_site='".addslashes($this->id)."'";
  1097. db_query($query);
  1098. $query = " UPDATE slot SET FK_site=NULL WHERE FK_site='".addslashes($this->id)."'";
  1099. db_query($query);
  1100. // remove sections
  1101. if ($this->sections) {
  1102. foreach ($this->sections as $s=>$o) {
  1103. $o->delete();
  1104. }
  1105. }
  1106. /* print "<pre>this: "; print_r($this); print "</pre>"; */
  1107. $this->clearPermissions();
  1108. /* print "<pre>this: "; print_r($this); print "</pre>"; */
  1109. $this->updatePermissionsDB();
  1110. // remove all editors from db
  1111. // echo $query = "DELETE FROM site_editors WHERE FK_site = ".$this->id;
  1112. db_query($query);
  1113. // exit(0);
  1114. // delete the userfiles
  1115. if (ereg("[0-9a-zA-Z]", $siteName)) {
  1116. $file_path = $cfg['uploaddir']."/".$siteName;
  1117. deletePath($file_path);
  1118. }
  1119. }
  1120. function createSQLArray($all=0) {
  1121. $this->parseMediaTextForDB("header");
  1122. $this->parseMediaTextForDB("footer");
  1123. $d = $this->data;
  1124. $a = array();
  1125. if ($all || $this->changed[title]) $a[] = $this->_datafields[title][1][0]."='".addslashes($d[title])."'";
  1126. if ($all || $this->changed[listed]) $a[] = $this->_datafields[listed][1][0]."='".addslashes($d[listed])."'";
  1127. if ($all || $this->changed[activatedate]) $a[] = "site_activate_tstamp ='".addslashes(ereg_replace("-","",$d[activatedate]))."'"; // remove dashes to make a tstamp
  1128. if ($all || $this->changed[deactivatedate]) $a[] = "site_deactivate_tstamp ='".addslashes(ereg_replace("-","",$d[deactivatedate]))."'"; // remove dashes to make a tstamp
  1129. if ($all || $this->changed[active]) $a[] = $this->_datafields[active][1][0]."='".addslashes($d[active])."'";
  1130. // if ($all || $this->changed[type]) $a[] = $this->_datafields[type][1][0]."='$d[type]'";
  1131. if ($all || $this->changed[theme]) $a[] = $this->_datafields[theme][1][0]."='".addslashes($d[theme])."'";
  1132. if ($all || $this->changed[themesettings]) $a[] = $this->_datafields[themesettings][1][0]."='".addslashes($d[themesettings])."'";
  1133. if ($all || $this->changed[header]) $a[] = $this->_datafields[header][1][0]."='".addslashes(urlencode($d[header]))."'";
  1134. if ($all || $this->changed[footer]) $a[] = $this->_datafields[footer][1][0]."='".addslashes(urlencode($d[footer]))."'";
  1135. return $a;
  1136. }
  1137. }