includes/pageutils.php
changeset 1252 e34c23a35dc9
parent 1227 bdac73ed481e
equal deleted inserted replaced
1251:d543689ed2eb 1252:e34c23a35dc9
  1337  	* @return string Javascript code
  1337  	* @return string Javascript code
  1338  	*/
  1338  	*/
  1339  	
  1339  	
  1340 	public static function catedit($page_id, $namespace)
  1340 	public static function catedit($page_id, $namespace)
  1341 	{
  1341 	{
  1342 		$d = PageUtils::catedit_raw($page_id, $namespace);
  1342 		list($js, $html) = PageUtils::catedit_raw($page_id, $namespace);
  1343 		return $d[0] . ' /* BEGIN CONTENT */ document.getElementById("ajaxEditContainer").innerHTML = unescape(\''.rawurlencode($d[1]).'\');';
  1343 		return $js . ' /* BEGIN CONTENT */ document.getElementById("ajaxEditContainer").innerHTML = unescape(\''.rawurlencode($html).'\');';
  1344 	}
  1344 	}
  1345 	
  1345 	
  1346 	/**
  1346 	/**
  1347  	* Does the actual HTML/javascript generation for cat editing, but returns an array
  1347  	* Does the actual HTML/javascript generation for cat editing, but returns an array
  1348  	* @access private
  1348  	* @access private
  1351 	public static function catedit_raw($page_id, $namespace)
  1351 	public static function catedit_raw($page_id, $namespace)
  1352 	{
  1352 	{
  1353 		global $db, $session, $paths, $template, $plugins; // Common objects
  1353 		global $db, $session, $paths, $template, $plugins; // Common objects
  1354 		global $lang;
  1354 		global $lang;
  1355 		
  1355 		
  1356 		ob_start();
  1356 		// notes
  1357 		$_ob = '';
  1357 		// span class is catCheck
  1358 		$e = $db->sql_query('SELECT category_id FROM ' . table_prefix.'categories WHERE page_id=\'' . $paths->page_id . '\' AND namespace=\'' . $paths->namespace . '\'');
  1358 		// return array(jsblob, innerHTML)
  1359 		if(!$e) jsdie('Error selecting category information for current page: '.$db->get_error());
  1359 		/*
  1360 		$cat_current = Array();
  1360 		$perms = $session->fetch_page_acl($cat_info[$i]['urlname_nons'], 'Category');
  1361 		while($r = $db->fetchrow())
  1361 		$cat_is_protected = ( !$session->get_permissions('edit_cat') || !$perms->get_permissions('edit_cat') ||
  1362 		{
  1362  				( $cat_info[$i]['really_protected'] && !$perms->get_permissions('even_when_protected') ) )
  1363 			$cat_current[] = $r;
  1363 		*/
  1364 		}
  1364 		
  1365 		$db->free_result();
  1365 		// two buffers: one is HTML and one is Javascript.
  1366 		
  1366 		$js = $html = '';
  1367 		$cat_all = array();
  1367 		
  1368 		$q = $db->sql_query('SELECT * FROM ' . table_prefix . 'pages WHERE namespace = \'Category\';');
  1368 		// page permissions
       
  1369 		$page_perms = $session->fetch_page_acl($page_id, $namespace);
       
  1370 		
       
  1371 		// Pull the list of categories this page is in
       
  1372 		$cats_member_of = array();
       
  1373 		$q = $db->sql_query('SELECT category_id FROM ' . table_prefix . 'categories WHERE page_id = \'' . $db->escape($page_id) . '\' AND namespace = \'' . $db->escape($namespace) . '\';');
  1369 		if ( !$q )
  1374 		if ( !$q )
  1370 			$db->_die();
  1375 			$db->_die();
  1371 		
       
  1372 		while ( $row = $db->fetchrow() )
  1376 		while ( $row = $db->fetchrow() )
  1373 		{
  1377 		{
  1374 			$cat_all[] = Namespace_Default::bake_cdata($row);
  1378 			$cats_member_of[] = $row['category_id'];
  1375 		}
  1379 		}
  1376 		
  1380 		
  1377 		// Make $cat_all an associative array, like $paths->pages
  1381 		// Get a list of all categories on the site
  1378 		$sz = sizeof($cat_all);
  1382 		$q = $db->sql_query('SELECT * FROM ' . table_prefix . 'pages WHERE namespace = \'Category\' ORDER BY name ASC;');
  1379 		for($i=0;$i<$sz;$i++)
  1383 		if ( !$q )
  1380 		{
  1384 			$db->_die();
  1381 			$cat_all[$cat_all[$i]['urlname_nons']] = $cat_all[$i];
  1385 		
  1382 		}
  1386 		$categories = array();
  1383 		// Now, the "zipper" function - join the list of categories with the list of cats that this page is a part of
  1387 		while ( $row = $db->fetchrow($q) )
  1384 		$cat_info = $cat_all;
  1388 		{
  1385 		for($i=0;$i<sizeof($cat_current);$i++)
  1389 			// bake page information
  1386 		{
  1390 			$row = Namespace_Default::bake_cdata($row);
  1387 			$un = $cat_current[$i]['category_id'];
  1391 			// add our own info
  1388 			$cat_info[$un]['member'] = true;
  1392 			$row['checked'] = in_array($row['urlname_nons'], $cats_member_of);
  1389 		}
  1393 			$row['exists'] = true;
  1390 		// Now copy the information we just set into the numerically named keys
  1394 			$row['perms'] = $session->fetch_page_acl($row['urlname_nons'], 'Category');
  1391 		for($i=0;$i<sizeof($cat_info)/2;$i++)
  1395 			$row['disabled'] = (
  1392 		{
  1396 					// no permissions to edit categorization in this category, or
  1393 			$un = $cat_info[$i]['urlname_nons'];
  1397 					!$row['perms']->get_permissions('edit_cat') ||
  1394 			$cat_info[$i] = $cat_info[$un];
  1398 					// category is protected, and no protect override permissions
  1395 		}
  1399 					( $row['really_protected'] && !$row['perms']->get_permissions('even_when_protected') )
  1396 		
  1400 				);
  1397 		echo 'catlist = new Array();'; // Initialize the client-side category list
  1401 			// append to array
  1398 		$_ob .= '<h3>' . $lang->get('catedit_title') . '</h3>
  1402 			$categories[ $row['urlname_nons'] ] = $row;
  1399  						<form name="mdgCatForm" action="'.makeUrlNS($namespace, $page_id, 'do=catedit').'" method="post">';
  1403 		}
  1400 		if ( sizeof($cat_info) < 1 )
  1404 		
  1401 		{
  1405 		// fabricate information on categories that don't exist.
  1402 			$_ob .= '<p>' . $lang->get('catedit_no_categories') . '</p>';
  1406 		foreach ( $cats_member_of as $category )
  1403 		}
  1407 		{
  1404 		for ( $i = 0; $i < sizeof($cat_info) / 2; $i++ )
  1408 			if ( isset($categories[$category]) )
  1405 		{
  1409 				// already have it in the array, skip
  1406 			// Protection code added 1/3/07
  1410 				continue;
  1407 			// Updated 3/4/07
  1411 			// create page metadata
  1408 			$is_prot = false;
  1412 			$row = Namespace_Default::bake_cdata(array(
  1409 			$perms = $session->fetch_page_acl($cat_info[$i]['urlname_nons'], 'Category');
  1413 					'urlname' => $category,
  1410 			if ( !$session->get_permissions('edit_cat') || !$perms->get_permissions('edit_cat') ||
  1414 					'namespace' => 'Category'
  1411  				( $cat_info[$i]['really_protected'] && !$perms->get_permissions('even_when_protected') ) )
  1415 				));
  1412  				$is_prot = true;
  1416 			// we know it's in this category
  1413 			$prot = ( $is_prot ) ? ' disabled="disabled" ' : '';
  1417 			$row['checked'] = true;
  1414 			$prottext = ( $is_prot ) ? ' <img alt="(protected)" width="16" height="16" src="'.scriptPath.'/images/lock16.png" />' : '';
  1418 			// we know it doesn't exist
  1415 			echo 'catlist[' . $i . '] = \'' . $cat_info[$i]['urlname_nons'] . '\';';
  1419 			$row['exists'] = false;
  1416 			$_ob .= '<span class="catCheck"><input ' . $prot . ' name="' . $cat_info[$i]['urlname_nons'] . '" id="mdgCat_' . $cat_info[$i]['urlname_nons'] . '" type="checkbox"';
  1420 			$row['perms'] = $session->fetch_page_acl($category, 'Category');
  1417 			if(isset($cat_info[$i]['member'])) $_ob .= ' checked="checked"';
  1421 			$row['disabled'] = (
  1418 			$_ob .= '/>  <label for="mdgCat_' . $cat_info[$i]['urlname_nons'] . '">' . $cat_info[$i]['name'].$prottext.'</label></span><br />';
  1422 					// no permissions to edit categorization in this category (honor inheritance and everything)
  1419 		}
  1423 					!$row['perms']->get_permissions('edit_cat')
  1420 		
  1424 					// not checking protection because it's defaulted to off
  1421 		$disabled = ( sizeof($cat_info) < 1 ) ? 'disabled="disabled"' : '';
  1425 				);
  1422 			
  1426 			// append
  1423 		$_ob .= '<div style="border-top: 1px solid #CCC; padding-top: 5px; margin-top: 10px;"><input name="__enanoSaveButton" ' . $disabled . ' style="font-weight: bold;" type="submit" onclick="ajaxCatSave(); return false;" value="' . $lang->get('etc_save_changes') . '" /> <input name="__enanoCatCancel" type="submit" onclick="ajaxReset(); return false;" value="' . $lang->get('etc_cancel') . '" /></div></form>';
  1427 			$categories[ $category ] = $row;
  1424 		
  1428 		}
  1425 		$cont = ob_get_contents();
  1429 		
  1426 		ob_end_clean();
  1430 		// spit out the form
  1427 		return Array($cont, $_ob);
  1431 		$html .= '<h3>' . $lang->get('catedit_title') . '</h3>';
       
  1432 		$html .= '<form name="mdgCatForm" action="'.makeUrlNS($namespace, $page_id, 'do=catedit').'" method="post" enctype="multipart/form-data">';
       
  1433 		foreach ( $categories as $category )
       
  1434 		{
       
  1435 			$html .= '<span class="catCheck"><label>';
       
  1436 			if ( !$category['exists'] )
       
  1437 				$html .= '<del>';
       
  1438 			$html .= '<input type="checkbox" name="categories[]" value="' . htmlspecialchars($category['urlname_nons']) . '" ';
       
  1439 			if ( $category['checked'] )
       
  1440 				$html .= 'checked="checked" ';
       
  1441 			if ( $category['disabled'] )
       
  1442 				$html .= 'disabled="disabled" ';
       
  1443 			$html .= ' />' . htmlspecialchars($category['name']);
       
  1444 			// lock icon
       
  1445 			if ( $category['protected'] > 0 )
       
  1446 				$html .= ' <img alt="' . $lang->get('catedit_msg_protected_tip') . '" width="16" height="16" src="' . cdnPath . '/images/lock16.png" />';
       
  1447 			if ( !$category['exists'] )
       
  1448 				$html .= '</del>';
       
  1449 			$html .= '</label></span><br />';
       
  1450 		}
       
  1451 		if ( count($categories) < 1 )
       
  1452 			$html .= '<p>' . $lang->get('catedit_no_categories') . '</p>';
       
  1453 		// submit buttons
       
  1454 		$save_disabled = ( count($categories) < 1 ) ? 'disabled="disabled"' : '';
       
  1455 		$html .= '<div style="border-top: 1px solid #CCC; padding-top: 5px; margin-top: 10px;">
       
  1456 					<input name="save" ' . $save_disabled . ' style="font-weight: bold;" type="submit" onclick="ajaxCatSave(); return false;" value="' . $lang->get('etc_save_changes') . '" />
       
  1457 					<input name="cancel" type="submit" onclick="ajaxReset(); return false;" value="' . $lang->get('etc_cancel') . '" />
       
  1458 				  </div>';
       
  1459 		
       
  1460 		$html .= '</form>';
       
  1461 		
       
  1462 		return array($js, $html);
  1428 	}
  1463 	}
  1429 	
  1464 	
  1430 	/**
  1465 	/**
  1431  	* Saves category information
  1466  	* Saves category information
  1432  	* WARNING: If $which_cats is empty, all the category information for the selected page will be nuked!
  1467  	* WARNING: If $which_cats is empty, all the category information for the selected page will be nuked!
  1442 		if(!$session->get_permissions('edit_cat')) return('Insufficient privileges to change category information');
  1477 		if(!$session->get_permissions('edit_cat')) return('Insufficient privileges to change category information');
  1443 		
  1478 		
  1444 		$page_perms = $session->fetch_page_acl($page_id, $namespace);
  1479 		$page_perms = $session->fetch_page_acl($page_id, $namespace);
  1445 		$ns = namespace_factory($page_id, $namespace);
  1480 		$ns = namespace_factory($page_id, $namespace);
  1446 		$page_data = $ns->get_cdata();
  1481 		$page_data = $ns->get_cdata();
  1447 		
  1482 		if ( !$page_perms->get_permissions('edit_cat') ||
  1448 		$cat_all = array();
  1483 				( $page_data['really_protected'] && !$page_perms->get_permissions('even_when_protected') ) )
  1449 		$q = $db->sql_query('SELECT * FROM ' . table_prefix . 'pages WHERE namespace = \'Category\';');
  1484 			return 'Insufficient privileges';
       
  1485 		
       
  1486 		// Pull the list of categories this page is in
       
  1487 		$cats_member_of = array();
       
  1488 		$q = $db->sql_query('SELECT category_id FROM ' . table_prefix . 'categories WHERE page_id = \'' . $db->escape($page_id) . '\' AND namespace = \'' . $db->escape($namespace) . '\';');
  1450 		if ( !$q )
  1489 		if ( !$q )
  1451 			$db->_die();
  1490 			$db->_die();
  1452 		
       
  1453 		while ( $row = $db->fetchrow() )
  1491 		while ( $row = $db->fetchrow() )
  1454 		{
  1492 		{
  1455 			$cat_all[] = Namespace_Default::bake_cdata($row);
  1493 			$cats_member_of[] = $row['category_id'];
  1456 		}
  1494 		}
  1457 		
  1495 		
  1458 		// Make $cat_all an associative array, like $paths->pages
  1496 		// Get a list of all categories on the site
  1459 		$sz = sizeof($cat_all);
  1497 		$q = $db->sql_query('SELECT * FROM ' . table_prefix . 'pages WHERE namespace = \'Category\' ORDER BY name ASC;');
  1460 		for($i=0;$i<$sz;$i++)
  1498 		if ( !$q )
  1461 		{
  1499 			$db->_die();
  1462 			$cat_all[$cat_all[$i]['urlname_nons']] = $cat_all[$i];
  1500 		
  1463 		}
  1501 		$categories = array();
  1464 		
  1502 		while ( $row = $db->fetchrow($q) )
  1465 		$rowlist = Array();
  1503 		{
  1466 		
  1504 			// bake page information
  1467 		for($i=0;$i<sizeof($cat_all)/2;$i++)
  1505 			$row = Namespace_Default::bake_cdata($row);
  1468 		{
  1506 			// add our own info
  1469 			$auth = true;
  1507 			$row['checked'] = in_array($row['urlname_nons'], $cats_member_of);
  1470 			$perms = $session->fetch_page_acl($cat_all[$i]['urlname_nons'], 'Category');
  1508 			$row['exists'] = true;
  1471 			if ( !$session->get_permissions('edit_cat') || !$perms->get_permissions('edit_cat') ||
  1509 			$row['perms'] = $session->fetch_page_acl($row['urlname_nons'], 'Category');
  1472  				( $cat_all[$i]['really_protected'] && !$perms->get_permissions('even_when_protected') ) ||
  1510 			$row['disabled'] = (
  1473  				( !$page_perms->get_permissions('even_when_protected') && $page_data['protected'] == '1' ) )
  1511 					// no permissions to edit categorization in this category, or
  1474  				$auth = false;
  1512 					!$row['perms']->get_permissions('edit_cat') ||
  1475 			if(!$auth)
  1513 					// category is protected, and no protect override permissions
       
  1514 					( $row['really_protected'] && !$row['perms']->get_permissions('even_when_protected') )
       
  1515 				);
       
  1516 			// append to array
       
  1517 			$categories[ $row['urlname_nons'] ] = $row;
       
  1518 		}
       
  1519 		
       
  1520 		// fabricate information on categories that don't exist.
       
  1521 		foreach ( $cats_member_of as $category )
       
  1522 		{
       
  1523 			if ( isset($categories[$category]) )
       
  1524 				// already have it in the array, skip
       
  1525 				continue;
       
  1526 			// create page metadata
       
  1527 			$row = Namespace_Default::bake_cdata(array(
       
  1528 					'urlname' => $category,
       
  1529 					'namespace' => 'Category'
       
  1530 				));
       
  1531 			// we know it's in this category
       
  1532 			$row['checked'] = true;
       
  1533 			// we know it doesn't exist
       
  1534 			$row['exists'] = false;
       
  1535 			$row['perms'] = $session->fetch_page_acl($category, 'Category');
       
  1536 			$row['disabled'] = (
       
  1537 					// no permissions to edit categorization in this category (honor inheritance and everything)
       
  1538 					!$row['perms']->get_permissions('edit_cat')
       
  1539 					// not checking protection because it's defaulted to off, and we know we are using the defaults
       
  1540 					// because we made it past the check above ;)
       
  1541 				);
       
  1542 			// append
       
  1543 			$categories[ $category ] = $row;
       
  1544 		}
       
  1545 		
       
  1546 		$to_insert = $to_delete = array();
       
  1547 		// go through categories and mark needed changes
       
  1548 		foreach ( $categories as $cat_id => $category )
       
  1549 		{
       
  1550 			// allowed to change it?
       
  1551 			if ( $category['disabled'] )
       
  1552 				continue;
       
  1553 			
       
  1554 			if ( $category['checked'] && !in_array($cat_id, $which_cats) )
  1476 			{
  1555 			{
  1477 				// Find out if the page is currently in the category
  1556 				// delete
  1478 				$q = $db->sql_query('SELECT * FROM ' . table_prefix.'categories WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';');
  1557 				$to_delete[] = $cat_id;
  1479 				if(!$q)
       
  1480 					return 'MySQL error: ' . $db->get_error();
       
  1481 				if($db->numrows() > 0)
       
  1482 				{
       
  1483 					$auth = true;
       
  1484 					$which_cats[$cat_all[$i]['urlname_nons']] = true; // Force the category to stay in its current state
       
  1485 				}
       
  1486 				$db->free_result();
       
  1487 			}
  1558 			}
  1488 			if(isset($which_cats[$cat_all[$i]['urlname_nons']]) && $which_cats[$cat_all[$i]['urlname_nons']] == true /* for clarity ;-) */ && $auth ) $rowlist[] = '(\'' . $page_id . '\', \'' . $namespace . '\', \'' . $cat_all[$i]['urlname_nons'] . '\')';
  1559 			else if ( !$category['checked'] && in_array($cat_id, $which_cats) )
  1489 		}
  1560 			{
  1490 		if(sizeof($rowlist) > 0)
  1561 				// insert
  1491 		{
  1562 				$to_insert[] = $cat_id;
  1492 			$val = implode(',', $rowlist);
  1563 			}
  1493 			$q = 'INSERT INTO ' . table_prefix.'categories(page_id,namespace,category_id) VALUES' . $val . ';';
  1564 			else
  1494 			$e = $db->sql_query('DELETE FROM ' . table_prefix.'categories WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';');
  1565 			{
  1495 			if(!$e) $db->_die('The old category data could not be deleted.');
  1566 				// no change
  1496 			$e = $db->sql_query($q);
  1567 			}
  1497 			if(!$e) $db->_die('The new category data could not be inserted.');
  1568 		}
  1498 			return('GOOD');
  1569 		
  1499 		}
  1570 		// commit changes
  1500 		else
  1571 		if ( !empty($to_insert) )
  1501 		{
  1572 		{
  1502 			$e = $db->sql_query('DELETE FROM ' . table_prefix.'categories WHERE page_id=\'' . $page_id . '\' AND namespace=\'' . $namespace . '\';');
  1573 			$rows = array();
  1503 			if(!$e) $db->_die('The old category data could not be deleted.');
  1574 			foreach ( $to_insert as $cat_id )
  1504 			return('GOOD');
  1575 			{
  1505 		}
  1576 				$rows[] = "('{$db->escape($page_id)}', '{$db->escape($namespace)}', '{$db->escape($cat_id)}')";
       
  1577 			}
       
  1578 			$q = $db->sql_query("INSERT INTO " . table_prefix . "categories(page_id, namespace, category_id) VALUES\n  "
       
  1579 									. implode(",\n  ", $rows) . ";");
       
  1580 			if ( !$q )
       
  1581 				$db->_die();
       
  1582 		}
       
  1583 		if ( !empty($to_delete) )
       
  1584 		{
       
  1585 			$entries = array();
       
  1586 			foreach ( $to_delete as $cat_id )
       
  1587 			{
       
  1588 				$entries[] = "category_id = '{$db->escape($cat_id)}'";
       
  1589 			}
       
  1590 			$q = $db->sql_query("DELETE FROM " . table_prefix . "categories WHERE page_id = '{$db->escape($page_id)}' AND namespace = '{$db->escape($namespace)}'\n"
       
  1591 									. "  AND ( " . implode(' OR ', $entries) . " );");
       
  1592 			if ( !$q )
       
  1593 				$db->_die();
       
  1594 		}
       
  1595 		
       
  1596 		
       
  1597 		return 'GOOD';
  1506 	}
  1598 	}
  1507 	
  1599 	
  1508 	/**
  1600 	/**
  1509  	* Sets the wiki mode level for a page.
  1601  	* Sets the wiki mode level for a page.
  1510  	* @param $page_id string the page ID
  1602  	* @param $page_id string the page ID