PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/objects/segue.inc.php

https://github.com/adamfranco/segue-1.x
PHP | 2187 lines | 1464 code | 256 blank | 467 comment | 344 complexity | a4c4c3a5b28c5715a67afafd0551fbe1 MD5 | raw file

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

  1. <? /* $Id$ */
  2. /******************************************************************************
  3. * Segue object - basis for all other section, page, and story objects
  4. ******************************************************************************/
  5. class segue {
  6. // var $permissions = array("everyone"=>array(3=>1),"institute"=>array(3=>1));
  7. var $permissions = array();
  8. // var $editors = array("everyone","institute");
  9. var $editors = array();
  10. var $editorsToDelete = array();
  11. var $editorsToDeleteInScope = array();
  12. var $changedpermissions = 0;
  13. var $cachedPermissions = array();
  14. var $builtPermissions=0;
  15. var $id = 0;
  16. var $data = array();
  17. var $changed = array();
  18. var $fetched = array();
  19. var $fetcheddown = 0;
  20. var $fetchedup = 0;
  21. var $tobefetched = 0;
  22. var $owning_site; var $owningSiteObj; // used by all types (including site for compatibility)
  23. var $owning_section; var $owningSectionObj; // only used for pages and stories
  24. var $owning_page; var $owningPageObj; // only used for stories
  25. var $_object_arrays = array("site"=>"sections","section"=>"pages","page"=>"stories"); // used for automatic functions like setFieldDown and setVarDown
  26. var $_tables = array("site"=>"sites","section"=>"sections","page"=>"pages","story"=>"stories"); // used for getField
  27. var $_encode = array("title","header","footer","shorttext","longertext","discussions","url");
  28. var $_parse = array("header","footer","shorttext","longertext");
  29. /******************************************************************************
  30. * siteExists - checks if the site/slot already exists with a certain name $name
  31. ******************************************************************************/
  32. function siteExists($site) {
  33. $query = "
  34. SELECT site_id
  35. FROM slot INNER JOIN site
  36. ON FK_site = site_id AND slot_name='".addslashes($site)."'
  37. ";
  38. // echo $query."<br />";
  39. if (db_num_rows(db_query($query))) return 1;
  40. return 0;
  41. }
  42. /******************************************************************************
  43. * siteNameValid - checks if a user is allowed to create a site of name $name
  44. ******************************************************************************/
  45. function siteNameValid($user,$name) {
  46. return 1;
  47. }
  48. /******************************************************************************
  49. * buildObjArrayFromSites($sites) - builds an array of objects from site names
  50. ******************************************************************************/
  51. function buildObjArrayFromSites($sites) {
  52. if (!is_array($sites)) return array();
  53. $a = array();
  54. foreach ($sites as $s) {
  55. $a[$s] =& new site($s);
  56. $a[$s]->fetchSiteAtOnceForeverAndEverAndDontForgetThePermissionsAsWell_Amen(0,0,true);
  57. }
  58. return $a;
  59. }
  60. /******************************************************************************
  61. * getAllSites - returns a list of all sites owned by $user
  62. ******************************************************************************/
  63. function getAllSites($user) {
  64. $sites = array();
  65. $query = "
  66. SELECT
  67. slot_name
  68. FROM
  69. slot
  70. INNER JOIN
  71. user
  72. ON FK_owner = user_id
  73. AND
  74. user_uname = '".addslashes($user)."'
  75. ";
  76. if (db_num_rows($r = db_query($query)))
  77. while ($a = db_fetch_assoc($r)) {
  78. $sites[] = $a[slot_name];
  79. }
  80. return $sites;
  81. }
  82. /******************************************************************************
  83. * getAllSitesWhereUserIsEditor - gets all sites where $user is an editor
  84. ******************************************************************************/
  85. function getAllSitesWhereUserIsEditor($user='') {
  86. global $dbhost, $dbuser, $dbpass, $dbdb;
  87. if ($user == '') $user = $_SESSION[auser];
  88. // first, get all sites for which the user is an editor
  89. $query = "
  90. SELECT
  91. slot_name
  92. FROM
  93. slot
  94. INNER JOIN
  95. site
  96. ON slot.FK_site = site_id
  97. INNER JOIN
  98. site_editors ON (
  99. site_id = site_editors.FK_site
  100. AND
  101. site_editors_type = 'user'
  102. )
  103. INNER JOIN
  104. user ON FK_editor = user_id AND user_uname='".addslashes($user)."'
  105. WHERE
  106. slot.FK_owner != user_id
  107. ";
  108. db_connect($dbhost, $dbuser, $dbpass, $dbdb);
  109. $r = db_query($query);
  110. $ar = array();
  111. if (db_num_rows($r))
  112. while ($a = db_fetch_assoc($r)) {
  113. $ar[] = $a[slot_name];
  114. }
  115. // now, if a user is a member of any groups, get all sites for which those groups are editors
  116. $query = "
  117. SELECT
  118. slot_name
  119. FROM
  120. slot
  121. INNER JOIN
  122. site
  123. ON slot.FK_site = site_id
  124. INNER JOIN
  125. site_editors ON (
  126. site_id = site_editors.FK_site
  127. AND
  128. site_editors_type = 'ugroup'
  129. )
  130. INNER JOIN
  131. ugroup ON FK_editor = ugroup_id
  132. INNER JOIN
  133. ugroup_user ON ugroup_id = FK_ugroup
  134. INNER JOIN
  135. user ON FK_user = user_id AND user_uname='".addslashes($user)."'
  136. ";
  137. $r = db_query($query);
  138. if (db_num_rows($r))
  139. while ($a = db_fetch_assoc($r)) {
  140. $ar[] = $a[slot_name];
  141. }
  142. // the two queries will return unique values, but their union could have non-unique entries.
  143. // therefore, uniquize it.
  144. return array_unique($ar);
  145. }
  146. /******************************************************************************
  147. * getAllSitesWhereUserIsSiteLevelEditor - gets all sites where $user is an editor
  148. * Like the previous function, but selects only the sites/slots
  149. * where the user has add, edit, and delete permission on the site level
  150. ******************************************************************************/
  151. function getSiteInfoWhereUserIsSiteLevelEditor($user='') {
  152. global $dbhost, $dbuser, $dbpass, $dbdb;
  153. if ($user == '') $user = $_SESSION[auser];
  154. $userId = db_get_value("user","user_id","user_uname='".addslashes($user)."'");
  155. $query = "
  156. SELECT
  157. slot_name,
  158. (site_id IS NOT NULL) AS site_exists,
  159. slot_owner.user_uname AS owner_uname,
  160. (site_id IS NOT NULL) AS site_exists,
  161. site_title,
  162. (classgroup_id IS NOT NULL) AS is_classgroup,
  163. createdby.user_uname AS site_addedby,
  164. site_created_tstamp,
  165. editedby.user_uname AS site_editedby,
  166. site_updated_tstamp,
  167. site_activate_tstamp,
  168. site_deactivate_tstamp,
  169. ( site_active = '1'
  170. AND (site_activate_tstamp = '00000000000000'
  171. OR site_activate_tstamp < CURRENT_TIMESTAMP())
  172. AND (site_deactivate_tstamp = '00000000000000'
  173. OR site_deactivate_tstamp > CURRENT_TIMESTAMP())
  174. ) AS is_active,
  175. permission_scope_type,
  176. permission_value
  177. FROM
  178. slot
  179. INNER JOIN
  180. site ON slot.FK_site = site_id
  181. INNER JOIN
  182. user AS slot_owner ON (
  183. slot.FK_owner != '".addslashes($userId)."'
  184. AND slot.FK_owner = slot_owner.user_id
  185. )
  186. INNER JOIN
  187. site_editors ON (
  188. site_id = site_editors.FK_site
  189. AND ((site_editors_type = 'ugroup')
  190. OR (site_editors_type = 'user'
  191. AND site_editors.FK_editor = '".addslashes($userId)."'))
  192. )
  193. LEFT JOIN
  194. ugroup_user ON (
  195. site_editors_type = 'ugroup'
  196. AND site_editors.FK_editor = FK_ugroup)
  197. INNER JOIN
  198. permission ON (
  199. permission_scope_type = 'site'
  200. AND permission.FK_scope_id = site_id
  201. AND FIND_IN_SET('a', permission_value) > 0
  202. AND FIND_IN_SET('e', permission_value) > 0
  203. AND FIND_IN_SET('d', permission_value) > 0
  204. AND (permission.FK_editor = FK_ugroup
  205. OR (permission.FK_editor = site_editors.FK_editor
  206. AND permission.FK_editor = '".addslashes($userId)."'))
  207. )
  208. LEFT JOIN
  209. classgroup ON slot_name = classgroup_name
  210. INNER JOIN
  211. user AS createdby ON site.FK_createdby = createdby.user_id
  212. INNER JOIN
  213. user AS editedby ON site.FK_updatedby = editedby.user_id
  214. WHERE
  215. (site_editors_type = 'ugroup'
  216. AND ugroup_user.FK_user = '".addslashes($userId)."')
  217. OR (site_editors_type = 'user'
  218. AND site_editors.FK_editor = '".addslashes($userId)."')
  219. ";
  220. $r = db_query($query);
  221. if (db_num_rows($r))
  222. while ($a = db_fetch_assoc($r))
  223. segue::addRowToSiteInfoArray($ar, $a);
  224. return $ar;
  225. }
  226. /******************************************************************************
  227. * getSiteInfoWhereUserIsEditor
  228. * Answers an array of site information for sites where $user is an editor
  229. *
  230. * The process is as follows:
  231. * - For every site where the user or one of their groups is listed as an editor...
  232. * - Get a list of all nodes in the site.
  233. * - For every node (site, section, page, story) get the permissions that apply
  234. * to the user or one of their groups.
  235. * - Return any site-level permissions found and the site info if the user
  236. * has more than just view and discuss permissions on any node in the site.
  237. *
  238. * Note (2006-12-19, Adam Franco):
  239. * This new query using sub-selects is about 12-times faster
  240. * than the previous query. Attempts were made to reformat the query by loading the
  241. * list of appropriate permissions, then finding the site id from there, but that
  242. * proved to be slower than the query below.
  243. *
  244. *
  245. ******************************************************************************/
  246. function getSiteInfoWhereUserIsEditor($user='') {
  247. global $dbhost, $dbuser, $dbpass, $dbdb;
  248. if ($user == '')
  249. $user = $_SESSION[auser];
  250. $userId = db_get_value("user","user_id","user_uname='".addslashes($user)."'");
  251. $query = "
  252. SELECT
  253. slot_name,
  254. slot_type,
  255. owner_uname,
  256. site_exists,
  257. site_title,
  258. is_classgroup,
  259. site_addedby,
  260. site_created_tstamp,
  261. site_editedby,
  262. site_updated_tstamp,
  263. site_activate_tstamp,
  264. site_deactivate_tstamp,
  265. ( site_active = '1'
  266. AND (site_activate_tstamp = '0000-00-00 00:00:00'
  267. OR site_activate_tstamp < CURRENT_TIMESTAMP())
  268. AND (site_deactivate_tstamp = '0000-00-00 00:00:00'
  269. OR site_deactivate_tstamp > CURRENT_TIMESTAMP())
  270. ) AS is_active,
  271. editor_id,
  272. permission_scope_type,
  273. permission_value
  274. FROM
  275. (SELECT
  276. slot_name,
  277. slot_type,
  278. slot_owner.user_uname AS owner_uname,
  279. (site_id IS NOT NULL) AS site_exists,
  280. site_title,
  281. (classgroup_id IS NOT NULL) AS is_classgroup,
  282. createdby.user_uname AS site_addedby,
  283. site_created_tstamp,
  284. editedby.user_uname AS site_editedby,
  285. site_updated_tstamp,
  286. site_activate_tstamp,
  287. site_deactivate_tstamp,
  288. site_active,
  289. site_id,
  290. section_id,
  291. page_id,
  292. story_id,
  293. site_editors.FK_editor AS editor_id
  294. FROM
  295. slot
  296. INNER JOIN
  297. site ON slot.FK_site = site_id
  298. INNER JOIN
  299. user AS slot_owner ON (
  300. slot.FK_owner = slot_owner.user_id
  301. AND slot.FK_owner != '".addslashes($userId)."'
  302. )
  303. INNER JOIN
  304. site_editors ON (
  305. site_id = site_editors.FK_site
  306. AND ((site_editors_type = 'ugroup')
  307. OR (site_editors_type = 'user'
  308. AND site_editors.FK_editor = '".addslashes($userId)."'))
  309. )
  310. LEFT JOIN
  311. ugroup_user ON (
  312. site_editors_type = 'ugroup'
  313. AND site_editors.FK_editor = FK_ugroup)
  314. LEFT JOIN
  315. section ON section.FK_site = site_id
  316. LEFT JOIN
  317. page ON page.FK_section = section_id
  318. LEFT JOIN
  319. story ON story.FK_page = page_id
  320. LEFT JOIN
  321. classgroup ON slot_name = classgroup_name
  322. LEFT JOIN
  323. user AS createdby ON site.FK_createdby = createdby.user_id
  324. LEFT JOIN
  325. user AS editedby ON site.FK_updatedby = editedby.user_id
  326. WHERE
  327. (site_editors_type = 'ugroup'
  328. AND ugroup_user.FK_user = '".addslashes($userId)."')
  329. OR (site_editors_type = 'user'
  330. AND site_editors.FK_editor = '".addslashes($userId)."')
  331. ) AS tmp_sites
  332. INNER JOIN
  333. permission ON (
  334. (permission.FK_editor = editor_id)
  335. AND ((permission_scope_type = 'site'
  336. AND permission.FK_scope_id = site_id)
  337. OR (permission_scope_type = 'section'
  338. AND permission.FK_scope_id = section_id)
  339. OR (permission_scope_type = 'page'
  340. AND permission.FK_scope_id = page_id)
  341. OR (permission_scope_type = 'story'
  342. AND permission.FK_scope_id = story_id))
  343. AND (FIND_IN_SET('a', permission_value) > 0
  344. OR FIND_IN_SET('e', permission_value) > 0
  345. OR FIND_IN_SET('d', permission_value) > 0)
  346. )
  347. GROUP BY
  348. slot_name,
  349. permission_value
  350. ORDER BY
  351. slot_name
  352. ";
  353. $r = db_query($query);
  354. if (db_num_rows($r)) {
  355. while ($a = db_fetch_assoc($r))
  356. segue::addRowToSiteInfoArray($ar, $a);
  357. }
  358. return $ar;
  359. }
  360. /******************************************************************************
  361. * getSiteInfoWhereUserIsEditor
  362. * Answers an array of site information for sites where $user is an editor
  363. *
  364. ******************************************************************************/
  365. function getSiteInfoWhereUserOwner($user='') {
  366. global $dbhost, $dbuser, $dbpass, $dbdb;
  367. if ($user == '') $user = $_SESSION[auser];
  368. $query = "
  369. SELECT
  370. slot_name,
  371. slot_type,
  372. slot_owner.user_uname AS owner_uname,
  373. (site_id IS NOT NULL) AS site_exists,
  374. site_title,
  375. (classgroup_id IS NOT NULL) AS is_classgroup,
  376. createdby.user_uname AS site_addedby,
  377. site_created_tstamp,
  378. editedby.user_uname AS site_editedby,
  379. site_updated_tstamp,
  380. site_activate_tstamp,
  381. site_deactivate_tstamp,
  382. ( site_active = '1'
  383. AND (site_activate_tstamp = '00000000000000'
  384. OR site_activate_tstamp < CURRENT_TIMESTAMP())
  385. AND (site_deactivate_tstamp = '00000000000000'
  386. OR site_deactivate_tstamp > CURRENT_TIMESTAMP())
  387. ) AS is_active
  388. FROM
  389. slot
  390. INNER JOIN
  391. user AS slot_owner ON (
  392. slot.FK_owner = slot_owner.user_id
  393. AND
  394. slot_owner.user_uname = '".addslashes($user)."'
  395. )
  396. INNER JOIN
  397. site ON slot.FK_site = site_id
  398. INNER JOIN
  399. user AS createdby ON site.FK_createdby = createdby.user_id
  400. INNER JOIN
  401. user AS editedby ON site.FK_updatedby = editedby.user_id
  402. LEFT JOIN
  403. classgroup ON slot_name = classgroup_name
  404. GROUP BY
  405. slot_name
  406. ";
  407. $r = db_query($query);
  408. if (db_num_rows($r)) {
  409. while ($a = db_fetch_assoc($r))
  410. segue::addRowToSiteInfoArray($ar, $a);
  411. }
  412. return $ar;
  413. }
  414. function addRowToSiteInfoArray( &$infoArray, &$a ) {
  415. if (!isset($infoArray[$a['slot_name']])) {
  416. $infoArray[$a['slot_name']] = array();
  417. $infoArray[$a['slot_name']]['slot_name'] = $a['slot_name'];
  418. $infoArray[$a['slot_name']]['slot_type'] = $a['slot_type'];
  419. $infoArray[$a['slot_name']]['slot_owner'] = $a['owner_uname'];
  420. $infoArray[$a['slot_name']]['site_exists'] = ($a['site_exists'] == '1')?true:false;
  421. $infoArray[$a['slot_name']]['site_title'] = stripslashes($a['site_title']);
  422. $infoArray[$a['slot_name']]['is_classgroup'] = ($a['is_classgroup'] == '1')?true:false;
  423. $infoArray[$a['slot_name']]['site_addedby'] = $a['site_addedby'];
  424. $infoArray[$a['slot_name']]['site_added_timestamp'] = $a['site_created_tstamp'];
  425. $infoArray[$a['slot_name']]['site_editedby'] = $a['site_editedby'];
  426. $infoArray[$a['slot_name']]['site_edited_timestamp'] = $a['site_updated_tstamp'];
  427. $infoArray[$a['slot_name']]['activatedate'] = $a['site_activate_tstamp'];
  428. $infoArray[$a['slot_name']]['deactivatedate'] = $a['site_deactivate_tstamp'];
  429. $infoArray[$a['slot_name']]['site_active'] = ($a['is_active'] == '1')?true:false;
  430. $infoArray[$a['slot_name']]['hasSitePermissionV'] = false;
  431. $infoArray[$a['slot_name']]['hasSitePermissionA'] = false;
  432. $infoArray[$a['slot_name']]['hasSitePermissionE'] = false;
  433. $infoArray[$a['slot_name']]['hasSitePermissionD'] = false;
  434. $infoArray[$a['slot_name']]['hasSitePermissionDI'] = false;
  435. $infoArray[$a['slot_name']]['hasPermissionDownV'] = false;
  436. $infoArray[$a['slot_name']]['hasPermissionDownA'] = false;
  437. $infoArray[$a['slot_name']]['hasPermissionDownE'] = false;
  438. $infoArray[$a['slot_name']]['hasPermissionDownD'] = false;
  439. $infoArray[$a['slot_name']]['hasPermissionDownDI'] = false;
  440. }
  441. if (ereg('v', $a['permission_value']) !== FALSE) {
  442. $infoArray[$a['slot_name']]['hasPermissionDownV'] = true;
  443. if ($a['permission_scope_type'] == 'site')
  444. $infoArray[$a['slot_name']]['hasSitePermissionV'] = true;
  445. }
  446. if (ereg('a', $a['permission_value']) !== FALSE) {
  447. $infoArray[$a['slot_name']]['hasPermissionDownA'] = true;
  448. if ($a['permission_scope_type'] == 'site')
  449. $infoArray[$a['slot_name']]['hasSitePermissionA'] = true;
  450. }
  451. if (ereg('e', $a['permission_value']) !== FALSE) {
  452. $infoArray[$a['slot_name']]['hasPermissionDownE'] = true;
  453. if ($a['permission_scope_type'] == 'site')
  454. $infoArray[$a['slot_name']]['hasSitePermissionE'] = true;
  455. }
  456. if (ereg('d([^i]*)', $a['permission_value']) !== FALSE) {
  457. $infoArray[$a['slot_name']]['hasPermissionDownD'] = true;
  458. if ($a['permission_scope_type'] == 'site')
  459. $infoArray[$a['slot_name']]['hasSitePermissionD'] = true;
  460. }
  461. if (ereg('di', $a['permission_value']) !== FALSE) {
  462. $infoArray[$a['slot_name']]['hasPermissionDownDI'] = true;
  463. if ($a['permission_scope_type'] == 'site')
  464. $infoArray[$a['slot_name']]['hasSitePermissionDI'] = true;
  465. }
  466. }
  467. /******************************************************************************
  468. * getAllValues - returns all values of $name in $scope in the current tree
  469. ******************************************************************************/
  470. function getAllValues($scope,$name) {
  471. if (!$this->fetcheddown) $this->fetchDown();
  472. $class = get_class($this);
  473. $ar = $this->_object_arrays[$class];
  474. // print "getting all values for $name in $class ".$this->getField("title")." with scope $scope<br />";
  475. if ($class==$scope) {
  476. if (($n = $this->getField($name)) != "")
  477. return array($n);
  478. else return array();
  479. }
  480. if ($ar) {
  481. $a = array();
  482. $oa = &$this->$ar;
  483. if ($oa) {
  484. foreach ($oa as $i=>$o) {
  485. // print "doing $i in $ar...<br />";
  486. $a = array_merge($a,$oa[$i]->getAllValues($scope,$name));
  487. }
  488. }
  489. }
  490. return $a;
  491. }
  492. function fetchData() {
  493. if ($fetched) return $this->data;
  494. else return 0;
  495. }
  496. function setData($data) {
  497. error("::setData() -- this function should not be used!");
  498. if (is_array($data)) {
  499. $this->data = $data;
  500. $this->changed = 1;
  501. $this->parseMediaTextForEdit("header");
  502. $this->parseMediaTextForEdit("footer");
  503. $this->parseMediaTextForEdit("shorttext");
  504. $this->parseMediaTextForEdit("longertext");
  505. }
  506. }
  507. /******************************************************************************
  508. * getField - Will return the value of a field in the data array.
  509. * $field should be the name of the field in the object, not the database
  510. *
  511. * If the value of the field has not yet been fetched from the database,
  512. * it is fetched from the database, otherwise it is simply returned from
  513. * the data array.
  514. *
  515. * Each class that extends segue has the following properties:
  516. *
  517. * An associative array called _datafields that associates the object
  518. * field name to a database join syntax and a database field name or pair of names.
  519. *
  520. * An array called _encode that holds the names of fields that need to
  521. * have slashes added and urlencoding to save them into the database
  522. ******************************************************************************/
  523. function getField ($field) {
  524. global $dbuser, $dbpass, $dbdb, $dbhost;
  525. if (ereg("^l%",$field))
  526. return $this->data[$field];
  527. if ($this->tobefetched && !$this->fetched[$field] && $this->id) { // we haven't allready gotten this data
  528. // and this object is in the database.
  529. // print "<pre>".get_class($this)." --$field---\n";
  530. // print_r ($this->_datafields[$field][1]);
  531. // print_r($this);
  532. // print "</pre>";
  533. // echo "<br />HERE: ".$field."<br />";
  534. $query = "
  535. SELECT
  536. ".implode(",",$this->_datafields[$field][1])."
  537. FROM
  538. ".$this->_datafields[$field][0]."
  539. WHERE
  540. ".$this->_table."_id=".$this->id."
  541. ORDER BY
  542. ".$this->_datafields[$field][2]."
  543. ";
  544. /* print $query; */
  545. if ($debug)
  546. print "-----------beginning---------$field<br /><pre>".$query;
  547. db_connect($dbhost,$dbuser,$dbpass, $dbdb);
  548. $r = db_query($query);
  549. if ($debug) {
  550. print mysql_error()."<br />Numrows = ".db_num_rows($r);
  551. print "\n\nresult arrays:\n";
  552. }
  553. if (!db_num_rows($r)) { // if we get no results
  554. if (in_array($field,$this->_object_arrays)) {
  555. // return an empty array
  556. $this->data[$field] = array();
  557. } else {
  558. return false;
  559. }
  560. }
  561. $valarray = array();
  562. while($a = db_fetch_assoc($r)) {
  563. // print_r($a);
  564. if (count($this->_datafields[$field][1]) == 1) {
  565. // We just want a single value
  566. $val = $a[$this->_datafields[$field][1][0]];
  567. $key = 0;
  568. } else {
  569. // we want a pair of values
  570. $val = $a[$this->_datafields[$field][1][0]];
  571. $key = $a[$this->_datafields[$field][1][1]];
  572. }
  573. // Decode this value if it is a member of _encode
  574. if (in_array($field,$this->_encode))
  575. $val = stripslashes(urldecode($val));
  576. if (count($this->_datafields[$field][1]) == 1) {
  577. $valarray[] = $val;
  578. } else {
  579. $valarray[$key] = $val;
  580. }
  581. /* print "<br />key = $key \nval = $val \nvalarray =\n"; */
  582. // print_r($valarray);
  583. }
  584. // only object_arrays should really be returning arrays to the data array.
  585. if (count($valarray) == 1 && !in_array($field,$this->_object_arrays))
  586. $this->data[$field] = $valarray[0];
  587. else
  588. $this->data[$field] = $valarray;
  589. $this->fetched[$field] = 1;
  590. if ($debug) {
  591. print "Valarray: ";
  592. print_r($valarray);
  593. print "\nInArray: \n$field";
  594. print_r($_object_arrays);
  595. print "<br />Is object?: ".((in_array($field,$this->_object_arrays))?"TRUE":"FALSE");
  596. print "</pre>----------end------------$field<br />";
  597. }
  598. }
  599. return $this->data[$field];
  600. }
  601. function fetchAllFields() {
  602. foreach ($this->_datafields as $key => $val) {
  603. $this->getField($key);
  604. }
  605. }
  606. function setField($name,$value) {
  607. $this->data[$name] = $value;
  608. $this->changed[$name] = 1;
  609. if ($name == "footer" || $name == "header" || $name == "shorttext" || $name == "longertext") {
  610. $this->parseMediaTextForEdit($name);
  611. }
  612. }
  613. function setFieldDown($name,$value) {
  614. if (!$this->fetcheddown) $this->fetchDown();
  615. $class=get_class($this);
  616. $ar = $this->_object_arrays[$class];
  617. $this->setField($name,$value);
  618. if ($ar) {
  619. $a = &$this->$ar;
  620. if ($a) {
  621. foreach ($a as $i=>$o) {
  622. $a[$i]->setFieldDown($name,$value);
  623. }
  624. }
  625. }
  626. }
  627. /* function setSiteNameDown($name) { */
  628. /* // if (!$this->fetcheddown) $this->fetchDown(); */
  629. /* $class=get_class($this); */
  630. /* $ar = $this->_object_arrays[$class]; */
  631. /* $this->owning_site = $name; */
  632. /* if ($class == "site") { */
  633. /* $this->name = $name; */
  634. /* $this->setField("name",$name); */
  635. /* } else { */
  636. /* $this->setField("site_id",$name); */
  637. /* } */
  638. /* if ($ar) { */
  639. /* $a = &$this->$ar; */
  640. /* foreach ($a as $i=>$o) { */
  641. /* $a[$i]->setSiteNameDown($name); */
  642. /* } */
  643. /* } */
  644. /* } */
  645. /******************************************************************************
  646. * copyObj - Copies an object to a new parent
  647. ******************************************************************************/
  648. function copyObj(&$newParent,$removeOrigional=1,$keepaddedby=0, $copyDiscussions=TRUE) {
  649. $_a = array("site"=>3,"section"=>2,"page"=>1,"story"=>0);
  650. // check that the newParent can be a parent
  651. $thisClass = get_class($this);
  652. $parentClass = get_class($newParent);
  653. /* print $this->id."$thisClass - $parentClass<br />"; */
  654. if (!($_a[$parentClass]-1 == $_a[$thisClass])) return 0;
  655. $this->fetchDown(1);
  656. /* print "<br /><br />Copying $thisClass ".$this->getField("title")." <br />"; */
  657. if ($thisClass == 'section') {
  658. $owning_site = $newParent->name;
  659. $this->id = 0; // createSQLArray uses this to tell if we are inserting or updating
  660. $this->insertDB(1, $owning_site, $removeOrigional, $keepaddedby, $copyDiscussions);
  661. }
  662. if ($thisClass == 'page') {
  663. $owning_site = $newParent->owning_site;
  664. $owning_section = $newParent->id;
  665. $this->id = 0; // createSQLArray uses this to tell if we are inserting or updating
  666. $this->insertDB(1, $owning_site, $owning_section, $removeOrigional, $keepaddedby, $copyDiscussions);
  667. }
  668. if ($thisClass == 'story') {
  669. $record_tags = get_record_tags($this->id);
  670. $owning_site = $newParent->owning_site;
  671. $owning_section = $newParent->owning_section;
  672. $owning_page = $newParent->id;
  673. $this->id = 0; // createSQLArray uses this to tell if we are inserting or updating
  674. /* print "insertDB: 1,$owning_site,$owning_section,$owning_page,$keepaddedby<br />"; */
  675. $this->insertDB(1, $owning_site, $owning_section, $owning_page, $removeOrigional, $keepaddedby, $copyDiscussions, $record_tags);
  676. }
  677. /* print_r($newParent); */
  678. return 1;
  679. }
  680. /******************************************************************************
  681. * getMediaIDs - returns an array of media ids found in a string
  682. ******************************************************************************/
  683. function getMediaIDs($field) {
  684. $string = stripslashes($this->getField($field));
  685. $ids = array();
  686. $string = explode("####",$string);
  687. for ($i=1; $i<count($string); $i=$i+2) {
  688. $ids[] = $string[$i];
  689. }
  690. return $ids;
  691. }
  692. /******************************************************************************
  693. * replaceMediaIDs - searches for and replaces each id in the string
  694. ******************************************************************************/
  695. function replaceMediaIDs($ids,$field,$newsite) {
  696. $string = $this->getField($field);
  697. foreach ($ids as $origID) {
  698. $newID = copy_media($origID,$newsite);
  699. $string = str_replace("####$origID####","####$newID####",$string);
  700. }
  701. $this->setField($field,$string);
  702. }
  703. /******************************************************************************
  704. * ACTIVATE/DEACTIVATE FUNCTIONS
  705. *
  706. * these functions handle de/activate dates in forms
  707. * - initFormDates() must be called upon edit session initialization
  708. * - outputDateForm() must be called where the HTML form data should be printed
  709. * - handleFormDates() must be called where all POST/GET data is processed
  710. ******************************************************************************/
  711. /******************************************************************************
  712. * handleFormDates - checks form fields for new de/activate dates and subsequently
  713. * sets the correct $_SESSION[settings][] variables
  714. ******************************************************************************/
  715. function handleFormDates() {
  716. // initialize the session vars.. if needed
  717. if (!isset($_SESSION[settings][activateyear]) || !isset($_SESSION[settings][deactivateyear])) {
  718. $this->initFormDates();
  719. }
  720. if ($_REQUEST[activateyear] != "") $_SESSION[settings][activateyear] = $_REQUEST[activateyear];
  721. if ($_REQUEST[activatemonth] != "") $_SESSION[settings][activatemonth] = $_REQUEST[activatemonth];
  722. if ($_REQUEST[activateday] != "") $_SESSION[settings][activateday] = $_REQUEST[activateday];
  723. if ($_REQUEST[deactivateyear] != "") $_SESSION[settings][deactivateyear] = $_REQUEST[deactivateyear];
  724. if ($_REQUEST[deactivatemonth] != "") $_SESSION[settings][deactivatemonth] = $_REQUEST[deactivatemonth];
  725. if ($_REQUEST[deactivateday] != "") $_SESSION[settings][deactivateday] = $_REQUEST[deactivateday];
  726. if ($_REQUEST[setformdates]) {
  727. if (/* !$_REQUEST[link] && */$_REQUEST[activatedate]) {
  728. $_SESSION[settings][activatedate] = 1;
  729. $this->setActivateDate($_REQUEST[activateyear],$_REQUEST[activatemonth],$_REQUEST[activateday]);
  730. } else {
  731. $_SESSION[settings][activatedate] = 0;
  732. $this->setActivateDate(-1);
  733. }
  734. if (/* !$_REQUEST[link] && */$_REQUEST[deactivatedate]) {
  735. $_SESSION[settings][deactivatedate] = 1;
  736. $this->setDeactivateDate($_REQUEST[deactivateyear],$_REQUEST[deactivatemonth],$_REQUEST[deactivateday]);
  737. } else {
  738. $_SESSION[settings][deactivatedate] = 0;
  739. $this->setDeactivateDate(-1);
  740. }
  741. }
  742. }
  743. /******************************************************************************
  744. * outputDateForm - outputs the HTML de/activate date form to be handled by above
  745. ******************************************************************************/
  746. function outputDateForm() {
  747. global $months, $months_values;
  748. // print_r($_SESSION[settings][activatedate]);
  749. printc("<input type='hidden' name='setformdates' value='1' />");
  750. printc("<table>");
  751. printc("<tr><td align='right'>");
  752. printc("Activate date:</td><td><input type='checkbox' name='activatedate' value='1'".(($_SESSION[settings][activatedate])?" checked='checked'":"")." /> <select name='activateday'>");
  753. for ($i=1;$i<=31;$i++) {
  754. printc("<option" . (($_SESSION[settings][activateday] == $i)?" selected":"") . ">".$i."\n");
  755. }
  756. printc("</select>");
  757. printc("<select name='activatemonth'>");
  758. for ($i=1; $i<13; $i++) {
  759. printc("<option value='$i'" . (($_SESSION[settings][activatemonth] == $i)?" selected":"") . ">".$months[$i-1]."\n");
  760. }
  761. printc("</select>\n<select name='activateyear'>");
  762. $curryear = date("Y");
  763. for ($i=$curryear; $i <= ($curryear+5); $i++) {
  764. printc("<option" . (($_SESSION[settings][activateyear] == $i)?" selected":"") . ">$i\n");
  765. }
  766. printc("</select>");
  767. printc("</td></tr>");
  768. printc("<tr><td align='right'>");
  769. printc("Deactivate date:</td><td><input type='checkbox' name='deactivatedate' value='1'".(($_SESSION[settings][deactivatedate])?" checked='checked'":"")." /> <select name='deactivateday'>");
  770. for ($i=1;$i<=31;$i++) {
  771. printc("<option" . (($_SESSION[settings][deactivateday] == $i)?" selected":"") . ">".$i."\n");
  772. }
  773. printc("</select>\n");
  774. printc("<select name='deactivatemonth'>");
  775. for ($i=1; $i<13; $i++) {
  776. printc("<option value='$i'" . (($_SESSION[settings][deactivatemonth] == $i)?" selected":"") . ">".$months[$i-1]."\n");
  777. }
  778. printc("</select>\n<select name='deactivateyear'>");
  779. for ($i=$curryear; $i <= ($curryear+5); $i++) {
  780. printc("<option" . (($_SESSION[settings][deactivateyear] == $i)?" selected":"") . ">$i\n");
  781. }
  782. printc("</select>");
  783. printc("</td></tr></table>");
  784. }
  785. /******************************************************************************
  786. * initFormDates - initializes necessary session vars for form date handling
  787. ******************************************************************************/
  788. function initFormDates() {
  789. $_SESSION[settings][activateyear] = "0000";
  790. $_SESSION[settings][activatemonth] = "00";
  791. $_SESSION[settings][activateday] = "00";
  792. $_SESSION[settings][activatedate] = 0;
  793. $_SESSION[settings][deactivateyear] = "0000";
  794. $_SESSION[settings][deactivatemonth] = "00";
  795. $_SESSION[settings][deactivateday] = "00";
  796. $_SESSION[settings][deactivatedate] = 0;
  797. list($_SESSION[settings][activateyear],$_SESSION[settings][activatemonth],$_SESSION[settings][activateday]) = explode("-",$this->getField("activatedate"));
  798. list($_SESSION[settings][deactivateyear],$_SESSION[settings][deactivatemonth],$_SESSION[settings][deactivateday]) = explode("-",$this->getField("deactivatedate"));
  799. // $_SESSION[settings][activatemonth]-=1;
  800. // $_SESSION[settings][deactivatemonth]-=1;
  801. /* echo $this->getField("activatedate")."<br />"; */
  802. $_SESSION[settings][activatedate]=($this->getField("activatedate")=='0000-00-00')?0:1;
  803. $_SESSION[settings][deactivatedate]=($this->getField("deactivatedate")=='0000-00-00')?0:1;
  804. }
  805. function setActivateDate($year,$month=0,$day=0) {
  806. // test to see if it's a valid date
  807. if ($year == -1) { // unset field
  808. $this->setField("activatedate","0000-00-00");
  809. return true;
  810. }
  811. if (!checkdate($month,$day,$year)) {
  812. error("The activate date you entered is invalid. It has not been set.");
  813. return false;
  814. }
  815. if ($month < 10) {
  816. $month = "0".$month;
  817. }
  818. if ($day < 10) {
  819. $day = "0".$day;
  820. }
  821. $this->setField("activatedate",$year."-".$month."-".$day);
  822. return true;
  823. }
  824. function setDeactivateDate($year,$month=0,$day=0) {
  825. // test to see if it's a valid date
  826. if ($year == -1) { // unset field
  827. $this->setField("deactivatedate","0000-00-00");
  828. return true;
  829. }
  830. if (!checkdate($month,$day,$year)) {
  831. error("The deactivate date you entered is invalid. It has not been set.");
  832. return false;
  833. }
  834. if ($month < 10) {
  835. $month = "0".$month;
  836. }
  837. if ($day < 10) {
  838. $day = "0".$day;
  839. }
  840. $this->setField("deactivatedate",$year."-".$month."-".$day);
  841. return true;
  842. }
  843. /******************************************************************************
  844. * cropString - crops a string to an appropriate length and adds elipses if
  845. * nessisary.
  846. ******************************************************************************/
  847. function cropString ($string, $maxChars) {
  848. $length = strlen($string);
  849. if ($length > $maxChars) {
  850. $length = $maxChars-3;
  851. $string = substr($string,0,$length)."...";
  852. }
  853. return $string;
  854. }
  855. /******************************************************************************
  856. * parseMediaTextForEdit - replaces ####<id>#### with appropriate filename info
  857. * -> used for inline images from the media library in text
  858. ******************************************************************************/
  859. function parseMediaTextForEdit($field) {
  860. if (!$this->getField("$field")) return false;
  861. $this->data[$field] = ereg_replace("src=('{0,1})####('{0,1})","####",$this->getField($field));
  862. $textarray1 = explode("####", $this->getField($field));
  863. if (count($textarray1) > 1) {
  864. for ($i=1; $i < count($textarray1); $i+=2) {
  865. $id = $textarray1[$i];
  866. $filename = db_get_value("media","media_tag","media_id='".addslashes($id)."'");
  867. $query = "
  868. SELECT
  869. slot_name
  870. FROM
  871. media
  872. INNER JOIN
  873. site ON media.FK_site = site_id
  874. INNER JOIN
  875. slot ON site_id = slot.FK_site
  876. WHERE
  877. media_id = '".addslashes($id)."'
  878. ";
  879. $a = db_fetch_assoc(db_query($query));
  880. $dir = $a[slot_name];
  881. $filepath = $uploadurl."/".$dir."/".$filename;
  882. $textarray1[$i] = "&&&& src='".$filepath."' @@@@".$id."@@@@ &&&&";
  883. }
  884. $this->data[$field] = implode("",$textarray1);
  885. }
  886. }
  887. /******************************************************************************
  888. * parseMediaTextForDB - does the exact opposite of above
  889. ******************************************************************************/
  890. function parseMediaTextForDB($field) {
  891. if (!$this->getField($field)) return false;
  892. $textarray1 = explode("&&&&", $this->getField($field));
  893. if (count($textarray1) > 1) {
  894. for ($i=1; $i<count($textarray1); $i=$i+2) {
  895. $textarray2 = explode("@@@@", $textarray1[$i]);
  896. $id = $textarray2[1];
  897. $textarray1[$i] = "src='####".$id."####'";
  898. }
  899. $this->data[$field] = implode("",$textarray1);
  900. }
  901. }
  902. /******************************************************************************
  903. * PERMISSIONS FUNCTIONS
  904. *
  905. * these functions handle part-specific permissions
  906. * isEditor($user) checks if $user is an editor for this part
  907. * addEditor($e) adds $e as an editor with default permissions (view only)
  908. * delEditor($e) removes all of $e's site permissions (ALL OF THEM)
  909. * getEditors() returns an array of editors for the site
  910. * setPermissions($p) set permissions to $p (a permission-formatted array)
  911. * getPermissions() returns a permission-formatted array of permissions
  912. * clearPermissions() flags all editor's scope-specific permissions to be removed
  913. * setUserPermissions($user,$add,$edit,$del,$view,$discuss)
  914. * sets $user's permissions to values of parameters (0 or 1)
  915. * setUserPermissionsFromArray($user,$p)
  916. * sets $user's permissions from permission-formatted array $p
  917. * buildPermissionsArray()
  918. * builds a permission-formatted array from the database
  919. * updatePermissionsDB()
  920. * updates the permissions database to reflect changes made above
  921. * canview($user) returns true/false depending on whether $user can view
  922. * this part of the site. takes into account de/activate dates
  923. * and active flag
  924. * hasPermission($perms,$user)
  925. * takes a formatted string $perms (ex, 'add and (edit or delete)')
  926. * and returns true/false if $user has those permissions
  927. * hasPermissionDown($perms,$user)
  928. * checks if someone has $perms anywhere down the line
  929. ******************************************************************************/
  930. function isEditor($user='') {
  931. if ($user=='')
  932. $user=$_SESSION[auser];
  933. if ($user == 'everyone' || $user == 'institute')
  934. return FALSE;
  935. if (!$this->builtPermissions && $this->id) $this->buildPermissionsArray();
  936. $this->fetchUp();
  937. $owner = $this->owningSiteObj->owner;
  938. /* print "owner: $owner"; */
  939. if (strtolower($user) == strtolower($owner)) return 1;
  940. $toCheck = array(strtolower($user));
  941. $toCheck = array_merge($toCheck,$this->returnEditorOverlap(getuserclasses($user,"all")));
  942. $toCheck = array_merge($toCheck, getusergroups($user,"all"));
  943. $toCheck = array_unique($toCheck);
  944. // printpre("-----------------------------\nDebugging:");
  945. // printpre(__FILE__.": ".__LINE__);
  946. // printpre($toCheck);
  947. // printpre($this->editors);
  948. // printpre("-----------------------------");
  949. foreach ($this->editors as $e) {
  950. if (in_array($e,$toCheck)) return 1;
  951. }
  952. return 0;
  953. }
  954. function addEditor($e) {
  955. /* print "<br />Adding editor $e<br />"; */
  956. // if ($e == 'institute' || $e == 'everyone') return false; // With the new permissions structure, this may be unwanted.
  957. if ($_SESSION[auser] == $e) { error("You do not need to add yourself as an editor."); return false; }
  958. if ($e && !in_array($e,$this->editors)) {
  959. $this->editors[]=$e;
  960. $this->setUserPermissions($e);
  961. $this->changedpermissions = 1;
  962. }
  963. }
  964. function delEditor($e) {
  965. $class=get_class($this);
  966. if ($e == 'institute' || $e == 'everyone') return false;
  967. if (!$e) return false;
  968. if (in_array($e,$this->editors)) {
  969. $n = array();
  970. foreach($this->editors as $v) {
  971. if ($v != $e) $n[]=$v;
  972. }
  973. $this->editors = $n;
  974. $this->setFieldDown("l%$e%add",0);
  975. $this->setFieldDown("l%$e%edit",0);
  976. $this->setFieldDown("l%$e%delete",0);
  977. $this->setFieldDown("l%$e%view",0);
  978. $this->setFieldDown("l%$e%discuss",0);
  979. $this->setUserPermissionDown("ADD",$e,0);
  980. $this->setUserPermissionDown("VIEW",$e,0);
  981. $this->setUserPermissionDown("EDIT",$e,0);
  982. $this->setUserPermissionDown("DELETE",$e,0);
  983. $this->setUserPermissionDown("DISCUSS",$e,0);
  984. $this->editorsToDelete[] = $e;
  985. }
  986. }
  987. function getEditors() {
  988. if (!$this->builtPermissions && $this->id)
  989. $this->buildPermissionsArray(0,0);
  990. return array_unique($this->editors);
  991. }
  992. /**
  993. * Answer the users who have add, edit, and delete permission at the site
  994. * level
  995. * retun array The editor ids
  996. */
  997. function getSiteLevelEditors () {
  998. if (!isset($this->_siteLevelEditors)) {
  999. $this->_siteLevelEditors = array();
  1000. foreach ($this->getEditors() as $editor) {
  1001. if ($this->owningSiteObj->hasPermission('add && edit && delete', $editor)) {
  1002. $this->_siteLevelEditors[] = $editor;
  1003. }
  1004. }
  1005. }
  1006. return $this->_siteLevelEditors;
  1007. }
  1008. function setPermissions($p) {
  1009. if (is_array($p)) {
  1010. $this->permissions = $p;
  1011. $this->editors = array_unique(array_merge(array_keys($p),$this->editors)); // add new editors from new permissions array
  1012. $this->changedpermissions = 1;
  1013. }
  1014. }
  1015. /* function setPermissionsDown($p) { */
  1016. /* if (!$this->fetcheddown) $this->fetchDown(); */
  1017. /* $class=get_class($this); */
  1018. /* $ar = $this->_object_arrays[$class]; */
  1019. /* $this->setPermissions($p); */
  1020. /* if ($ar) { */
  1021. /* $a = &$this->$ar; */
  1022. /* if ($a) { */
  1023. /* foreach ($a as $i=>$o) { */
  1024. /* $a[$i]->setPermissionsDown($p); */
  1025. /* } */
  1026. /* } */
  1027. /* } */
  1028. /* } */
  1029. function clearPermissions($editor = '') {
  1030. /* print "Editors: <pre>"; print_r($this->getEditors()); print "</pre>"; */
  1031. /* print "To Delete: <pre>"; print_r($this->editorsToDeleteInScope); print "</pre>"; */
  1032. $this->editors = array();
  1033. $this->permissions = array();
  1034. $this->changedpermissions = 1;
  1035. }
  1036. function setUserPermissions($user,$add=0,$edit=0,$del=0,$view=0,$discuss=0) {
  1037. $this->setUserPermissionsFromArray($user,array(ADD=>$add,EDIT=>$edit,DELETE=>$del,VIEW=>$view,DISCUSS=>$discuss));
  1038. }
  1039. function setUserPermissionsFromArray($user,$p) {
  1040. $this->permissions[$user] = $p;
  1041. $this->changedpermissions = 1;
  1042. }
  1043. function setUserPermissionDown($perm,$user,$val=1) {
  1044. $class=get_class($this);
  1045. $ar = $this->_object_arrays[$class];
  1046. $p = strtoupper($perm);
  1047. $c = permissions::$p();
  1048. if ($this->permissions[$user][$c] != $val) {
  1049. $this->permissions[$user][$c] = $val;
  1050. $this->cachedPermissions["onlyuser".$user.$perm] = $val; // Update the cached permissions array so that
  1051. // hasPermission doesn't get a fscked up
  1052. $this->cachedPermissions[$user.$perm] = $val; // Update the cached permissions array so that
  1053. // hasPermission doesn't get a fscked up
  1054. $this->changedpermissions=1;
  1055. }
  1056. /* if ($class == "site") $n = 0; */
  1057. /* else if ($class == "section")$n =4; */
  1058. /* else if ($class == "page")$n = 8; */
  1059. /* else $n=12; */
  1060. /* $i = 0; */
  1061. /* while($i <= $n) { */
  1062. /* print " &nbsp; "; */
  1063. /* $i++; */
  1064. /* } */
  1065. /* print $this->permissions[$user][$c]; */
  1066. /* print $class.": set -- has permission= -- should be: $val<br />"; */
  1067. /* print $this->permissions[$user][$c]; */
  1068. /* print "<pre>"; print_r($this->permissions[$user]); print "</pre>"; */
  1069. if ($ar) {
  1070. $a = &$this->$ar;
  1071. if ($a) {
  1072. foreach (array_keys($a) as $k=>$i) {
  1073. $a[$i]->setUserPermissionDown($perm,$user,$val);
  1074. $a[$i]->cachedPermissions["onlyuser".$user.$perm] = $val; // Update the cached permissions array so that
  1075. // hasPermission doesn't get a fscked up
  1076. $a[$i]->cachedPermissions[$user.$perm] = $val; // Update the cached permissions array so that
  1077. // hasPermission doesn't get a fscked up
  1078. }
  1079. }
  1080. }
  1081. }
  1082. function getPermissions() {
  1083. // returns an html-formable permissions array based on the permissions table
  1084. return $this->permissions;
  1085. }
  1086. function movePermission($action, $user, $origSite, $moveLevel) {
  1087. // determines whether user can move an object here
  1088. if ($this->getField("type") != get_class($this)) return 0;
  1089. if ($this->owning_site == $origSite) {
  1090. if ($action == "COPY") {
  1091. if ($this->hasPermission("add",$user)) return 1;
  1092. if ($moveLevel != get_class($this) && $this->hasPermissionDown("add",$user)) return 1;
  1093. } else {
  1094. if ($this->hasPermission("add or edit",$user)) return 1;
  1095. if ($moveLevel != get_class($this) && $this->hasPermissionDown("add or edit",$user)) return 1;
  1096. }
  1097. } else {
  1098. if ($this->hasPermission("add",$user)) return 1;
  1099. if ($moveLevel != get_class($this) && $this->hasPermissionDown("add",$user)) return 1;
  1100. }
  1101. return 0;
  1102. }
  1103. /******************************************************************************
  1104. * buildPermissionsArray - builds the permissions array for current obj from DB
  1105. ******************************************************************************/
  1106. function buildPermissionsArray($force=0,$down=0) {
  1107. if (!$force && $this->builtPermissions) return;
  1108. $scope = get_class($this);
  1109. $site = $this->owning_site;
  1110. $id = $this->id;
  1111. // the SQL queries for obtaining the permissions vary with the scope type. Thus, we have 4 cases, 1 for each scope type.
  1112. // editors can be either institute, everyone, a username or a ugroup name
  1113. // we need two queries for any one scope
  1114. // CASE 1: scope is SITE
  1115. if ($scope == 'site') {
  1116. $query = "
  1117. SELECT
  1118. user_uname as editor, ugroup_name as editor2, site_editors_type as editor_type,
  1119. MAKE_SET(IFNULL((permission_value+0),0), 'v', 'a', 'e', 'd', 'di') as permissions
  1120. FROM
  1121. site
  1122. INNER JOIN
  1123. site_editors ON
  1124. site_id = ".$this->id."
  1125. AND
  1126. site_id = FK_site
  1127. LEFT JOIN
  1128. user ON
  1129. site_editors.FK_editor = user_id
  1130. LEFT JOIN
  1131. ugroup ON
  1132. site_editors.FK_editor = ugroup_id
  1133. LEFT JOIN
  1134. permission ON
  1135. site_id = FK_scope_id
  1136. AND
  1137. permission_scope_type = 'site'
  1138. AND
  1139. permission.FK_editor <=> site_editors.FK_editor
  1140. AND
  1141. permission_editor_type = site_editors_type
  1142. ";
  1143. }
  1144. // CASE 2: scope is SECTION
  1145. else if ($scope == 'section') {
  1146. $query = "
  1147. SELECT
  1148. user_uname as editor, ugroup_name as editor2, site_editors_type as editor_type,
  1149. MAKE_SET(IFNULL((p1.permission_value+0),0) | IFNULL((p2.permission_value+0),0), 'v', 'a', 'e', 'd', 'di') as permissions
  1150. FROM
  1151. site
  1152. INNER JOIN
  1153. section
  1154. ON site_id = section.FK_site
  1155. AND
  1156. section_id = ".$this->id."
  1157. INNER JOIN
  1158. site_editors ON
  1159. site_id = site_editors.FK_site
  1160. LEFT JOIN
  1161. user ON
  1162. site_editors.FK_editor = user_id
  1163. LEFT JOIN
  1164. ugroup ON
  1165. site_editors.FK_editor = ugroup_id
  1166. LEFT JOIN
  1167. permission as p1 ON
  1168. site_id = p1.FK_scope_id
  1169. AND
  1170. p1.permission_scope_type = 'site'
  1171. AND
  1172. p1.FK_editor <=> site_editors.FK_editor
  1173. AND
  1174. p1.permission_editor_type = site_editors_type
  1175. LEFT JOIN
  1176. permission as p2 ON
  1177. section_id = p2.FK_scope_id
  1178. AND
  1179. p2.permission_scope_type = 'section'
  1180. AND
  1181. p2.FK_editor <=> site_editors.FK_editor
  1182. AND
  1183. p2.permission_editor_type = site_editors_type
  1184. ";
  1185. }
  1186. // CASE 3: scope is PAGE
  1187. else if ($scope == 'page') {
  1188. $query = "
  1189. SELECT
  1190. user_uname as editor, ugroup_name as editor2, site_editors_type as editor_type,
  1191. MAKE_SET(IFNULL((p1.permission_value+0),0) | IFNULL((p2.permission_value+0),0) | IFNULL((p3.permission_value+0),0), 'v', 'a', 'e', 'd', 'di') as permissions
  1192. FROM
  1193. site
  1194. INNER JOIN
  1195. section
  1196. ON site_id = section.FK_site
  1197. INNER JOIN
  1198. page
  1199. ON section_id = page.FK_section
  1200. AND
  1201. page_id = ".$this->id."
  1202. INNER JOIN
  1203. site_editors ON
  1204. site_id = site_editors.FK_site
  1205. LEFT JOIN
  1206. user ON
  1207. site_editors.FK_editor = user_id
  1208. LEFT JOIN
  1209. ugroup ON
  1210. site_editors.FK_editor = ugroup_id
  1211. LEFT JOIN
  1212. permission as p1 ON
  1213. site_id = p1.FK_scope_id
  1214. AND
  1215. p1.permission_scope_type = 'site'
  1216. AND
  1217. p1.FK_editor <=> site_editors.FK_editor
  1218. AND
  1219. p1.permission_editor_type = site_editors_type
  1220. LEFT JOIN
  1221. permission as p2 ON
  1222. section_id = p2.FK_scope_id
  1223. AND
  1224. p2.permission_scope_type = 'section'
  1225. AND
  1226. p2.FK_editor <=> site_editors.FK_editor
  1227. AND
  1228. p2.permission_editor_type = site_editors_type
  1229. LEFT JOIN
  1230. permission as p3 ON
  1231. page_id = p3.FK_scope_id
  1232. AND
  1233. p3.permission_scope_type = 'page'
  1234. AND
  1235. p3.FK_editor <=> site_editors.FK_editor
  1236. AND
  1237. p3.permission_editor_type = site_editors_type
  1238. ";
  1239. }
  1240. // CASE 4: scope is PAGE
  1241. else if ($scope == 'story') {
  1242. $query = "
  1243. SELECT
  1244. user_uname as editor, ugroup_name as editor2, site_editors_type as editor_type,
  1245. MAKE_SET(IFNULL((p1.permission_value+0),0) | IFNULL((p2.permission_value+0),0) | IFNULL((p3.permission_value+0),0) | IFNULL((p4.permission_value+0),0), 'v', 'a', 'e', 'd', 'di') as permissions
  1246. FROM
  1247. site
  1248. INNER JOIN
  1249. section
  1250. ON site_id = section.FK_site
  1251. INNER JOIN
  1252. page
  1253. ON section_id = page.FK_section
  1254. INNER JOIN
  1255. story
  1256. ON page_id = story.FK_page
  1257. AND
  1258. story_id = '".addslashes($this->id)."'
  1259. INNER JOIN
  1260. site_editors ON
  1261. site_id = site_editors.FK_site
  1262. LEFT JOIN
  1263. user ON
  1264. site_editors.FK_editor = user_id
  1265. LEFT JOIN
  1266. ugroup ON
  1267. site_editors.FK_editor = ugroup_id
  1268. LEFT JOIN
  1269. permission as p1 ON
  1270. site_id = p1.FK_scope_id
  1271. AND
  1272. p1.permission_scope_type = 'site'
  1273. AND
  1274. p1.FK_editor <=> site_editors.FK_editor
  1275. AND
  1276. p1.permission_editor_type = site_editors_type
  1277. LEFT JOIN
  1278. permission as p2 ON
  1279. section_id = p2.FK_scope_id
  1280. AND
  1281. p2.permission_scope_type = 'section'
  1282. AND
  1283. p2.FK_editor <=> site_editors.FK_editor
  1284. AND
  1285. p2.permission_editor_type = site_editors_type
  1286. LEFT JOIN
  1287. permission as p3 ON
  1288. page_id = p3.FK_scope_id
  1289. AND
  1290. p3.permission_scope_type = 'page'
  1291. AND
  1292. p3.FK_editor <=> site_editors.FK_editor
  1293. AND
  1294. p3.permission_editor_type = site_editors_type
  1295. LEFT JOIN
  1296. permission as p4 ON
  1297. story_id = p4.FK_scope_id
  1298. AND
  1299. p4.permission_scope_type = 'story'
  1300. AND
  1301. p4.FK_editor <=> site_editors.FK_editor
  1302. AND
  1303. p4.permission_editor_type = site_editors_type
  1304. ";
  1305. }
  1306. // execute the query
  1307. // echo $query;
  1308. $r = db_query($query);
  1309. //echo "Query result: ".$r."<br />";
  1310. // reset the editor array
  1311. if ($r) {
  1312. $this->editors = array();
  1313. $this->permissions = array();
  1314. }
  1315. // for every permisson entry, add it to the permissions array
  1316. while ($row=db_fetch_assoc($r)) {
  1317. // decode 'final_permissions';
  1318. // 'final_permissions' is a field returned by the query and contains a string of the form "'a','vi','e'" etc.
  1319. $a = array();
  1320. $dbPerms = explode(",", $row[permissions]);
  1321. $a[v] = in_array('v', $dbPerms);
  1322. $a[a] = in_array('a', $dbPerms);
  1323. $a[e] = in_array('e', $dbPerms);
  1324. $a[d] = in_array('d', $dbPerms);
  1325. $a[di] = in_array('di', $dbPerms);
  1326. // Trash the db perms variable.
  1327. $dbPerms = NULL;
  1328. unset ($dbPerms);
  1329. // if the editor is a user then the editor's name is just the user name
  1330. // if the editor is 'institute' or 'everyone' then set the editor's name correspondingly
  1331. if ($row[editor_type]=='user')
  1332. $t_editor = $row[editor];
  1333. else if ($row[editor_type]=='ugroup')
  1334. $t_editor = $row[editor2];
  1335. else
  1336. $t_editor = $row[editor_type];
  1337. // Everyone and institute can't have add, edit, or delete permissions.
  1338. // Somehow, these were added sometimes. If this is the case, prevent
  1339. // these from being set and reset those for the site.
  1340. if ($t_editor == 'everyone' || $t_editor == 'institute') {
  1341. // If we have a bad permission, do cleanup.
  1342. if ($a[a] || $a[e] || $a[d]) {
  1343. // Make sure that zeros get passed on.
  1344. $a[a] = 0;
  1345. $a[e] = 0;
  1346. $a[d] = 0;
  1347. // Clean up the permissions
  1348. $this->owningSiteObj->setUserPermissionDown('add', $t_editor, 0);
  1349. $this->owningSiteObj->s

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