Skip to content

Commit

Permalink
Template.class.inc: do not pass vars by reference
Browse files Browse the repository at this point in the history
See smrealms#317.

We should never need to modify a variable that was assigned to
a template (because templates are for display only, not processing).
Therefore, we should only pass variables by reference to the template
to improve both performance and safety.

* `assign` and `display` no longer use references
* `assignByRef` has been removed
  • Loading branch information
hemberger committed Apr 3, 2018
1 parent 24893e9 commit b287aa5
Show file tree
Hide file tree
Showing 59 changed files with 124 additions and 130 deletions.
2 changes: 1 addition & 1 deletion admin/Default/1.6/universe_create.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
while ($db->nextRecord()) {
$games[] = SmrGame::getGame($db->getInt('game_id'));
}
$template->assignByRef('EditGames',$games);
$template->assign('EditGames',$games);
?>
8 changes: 4 additions & 4 deletions admin/Default/1.6/universe_create_locations.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
$totalLocs[$sectorLocation->getTypeID()]++;
} unset($sectorLocation);
} unset($sector);
$template->assignByRef('TotalLocs', $totalLocs);
$template->assign('TotalLocs', $totalLocs);

$galaxy =& SmrGalaxy::getGalaxy($var['game_id'],$var['gal_on']);
$template->assignByRef('Galaxy', $galaxy);
$template->assign('Galaxy', $galaxy);

// Though we expect a location to be only in one category, it is possible to
// edit a location in the Admin Tools so that it is in two or more categories.
Expand Down Expand Up @@ -87,8 +87,8 @@ public function added($locID) {

$locText[$location->getTypeID()] = $location->getName() . $extra;
} unset($location);
$template->assignByRef('LocText', $locText);
$template->assignByRef('LocTypes', $categories->locTypes);
$template->assign('LocText', $locText);
$template->assign('LocTypes', $categories->locTypes);

// Form to make location changes
$container = create_container('1.6/universe_create_save_processing.php',
Expand Down
6 changes: 3 additions & 3 deletions admin/Default/1.6/universe_create_planets.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
while ($db->nextRecord()) {
$allowedTypes[$db->getInt('planet_type_id')] = $db->getField('planet_type_name');
}
$template->assignByRef('AllowedTypes', $allowedTypes);
$template->assign('AllowedTypes', $allowedTypes);

// Initialize all planet counts to zero
$numberOfPlanets = array();
Expand All @@ -24,8 +24,8 @@
}
}

$template->assignByRef('Galaxy', $galaxy);
$template->assignByRef('NumberOfPlanets', $numberOfPlanets);
$template->assign('Galaxy', $galaxy);
$template->assign('NumberOfPlanets', $numberOfPlanets);

$numberOfNpcPlanets = (isset($planet_info['NPC']) ? $planet_info['NPC'] : 0);
$template->assign('NumberOfNpcPlanets', $numberOfNpcPlanets);
Expand Down
8 changes: 4 additions & 4 deletions admin/Default/1.6/universe_create_sectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
}


$template->assignByRef('Galaxy', $galaxy);
$template->assignByRef('Galaxies', $galaxies);
$template->assignByRef('MapSectors',$mapSectors);
$template->assignByRef('Message',$var['message']);
$template->assign('Galaxy', $galaxy);
$template->assign('Galaxies', $galaxies);
$template->assign('MapSectors',$mapSectors);
$template->assign('Message',$var['message']);
SmrSession::updateVar('message',null); // Only show message once

if (isset($_REQUEST['connect']) && $_REQUEST['connect'] > 0) {
Expand Down
4 changes: 2 additions & 2 deletions admin/Default/account_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
'validation_code LIKE ' . $db->escape_string($var['val_code']));
if ($db->nextRecord()) {
$curr_account =& SmrAccount::getAccount($db->getField('account_id'));
$template->assignByRef('EditingAccount', $curr_account);
$template->assign('EditingAccount', $curr_account);
$template->assign('EditFormHREF', SmrSession::getNewHREF(create_container('account_edit_processing.php', '', array('account_id' => $curr_account->getAccountID()))));
}
else {
Expand All @@ -64,7 +64,7 @@
while ($db->nextRecord()) {
$games[] = SmrGame::getGame($db->getInt('game_id'));
}
$template->assignByRef('Games', $games);
$template->assign('Games', $games);
}
else {
$editingPlayers = array();
Expand Down
4 changes: 2 additions & 2 deletions admin/Default/admin_message_send.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
while ($db->nextRecord()) {
$activeGames[] = SmrGame::getGame($db->getInt('game_id'));
}
$template->assignByRef('ActiveGames', $activeGames);
$template->assign('ActiveGames', $activeGames);
}
else {
$container = create_container('admin_message_send_processing.php');
Expand All @@ -28,7 +28,7 @@
while ($db->nextRecord()) {
$gamePlayers[]= array('AccountID' => $db->getField('account_id'), 'PlayerID' => $db->getField('player_id'), 'Name' => $db->getField('player_name'));
}
$template->assignByRef('GamePlayers',$gamePlayers);
$template->assign('GamePlayers',$gamePlayers);
}
if(isset($var['preview'])) {
$template->assign('Preview', $var['preview']);
Expand Down
4 changes: 2 additions & 2 deletions admin/Default/box_reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
transfer('game_id');
transfer('sender_id');
$template->assign('BoxReplyFormHref',SmrSession::getNewHREF($container));
$template->assignByRef('Sender',SmrPlayer::getPlayer($var['sender_id'], $var['game_id']));
$template->assignByRef('SenderAccount',SmrAccount::getAccount($var['sender_id']));
$template->assign('Sender',SmrPlayer::getPlayer($var['sender_id'], $var['game_id']));
$template->assign('SenderAccount',SmrAccount::getAccount($var['sender_id']));
if(isset($var['Preview']))
$template->assign('Preview', $var['Preview']);
if(isset($var['BanPoints']))
Expand Down
10 changes: 5 additions & 5 deletions admin/Default/location_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
}


$template->assignByRef('Location',$location);
$template->assignByRef('Ships',AbstractSmrShip::getAllBaseShips($var['game_type_id']));
$template->assignByRef('Weapons',SmrWeapon::getAllWeapons($var['game_type_id']));
$template->assign('Location',$location);
$template->assign('Ships',AbstractSmrShip::getAllBaseShips($var['game_type_id']));
$template->assign('Weapons',SmrWeapon::getAllWeapons($var['game_type_id']));


$db->query('SELECT * FROM hardware_type');
Expand All @@ -42,9 +42,9 @@
$hardware[$db->getField('hardware_type_id')] = array('ID' => $db->getField('hardware_type_id'),
'Name' => $db->getField('hardware_name'));
}
$template->assignByRef('AllHardware',$hardware);
$template->assign('AllHardware',$hardware);
}
else {
$template->assignByRef('Locations',SmrLocation::getAllLocations());
$template->assign('Locations',SmrLocation::getAllLocations());
}
?>
4 changes: 2 additions & 2 deletions admin/Default/log_console.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
$db->query('SELECT log_type_id FROM log_type');
while ($db->nextRecord())
$logTypes[] = $db->getInt('log_type_id');
$template->assignByRef('LogTypes', $logTypes);
$template->assign('LogTypes', $logTypes);

$template->assign('LogConsoleFormHREF', SmrSession::getNewHREF(create_container('skeleton.php', 'log_console_detail.php')));
$template->assign('AnonAccessHREF', SmrSession::getNewHREF(create_container('skeleton.php', 'log_anonymous_account.php')));
}
$template->assignByRef('LoggedAccounts',$loggedAccounts);
$template->assign('LoggedAccounts',$loggedAccounts);
?>
8 changes: 4 additions & 4 deletions admin/Default/notify_reply.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
$offender =& getMessagePlayer($var['offender'],$var['game_id']);
$offended =& getMessagePlayer($var['offended'],$var['game_id']);
if(is_object($offender))
$template->assignByRef('OffenderAccount', SmrAccount::getAccount($var['offender']));
$template->assign('OffenderAccount', SmrAccount::getAccount($var['offender']));
if(is_object($offended))
$template->assignByRef('OffendedAccount', SmrAccount::getAccount($var['offended']));
$template->assignByRef('Offender', $offender);
$template->assignByRef('Offended', $offended);
$template->assign('OffendedAccount', SmrAccount::getAccount($var['offended']));
$template->assign('Offender', $offender);
$template->assign('Offended', $offended);

if(isset($var['PreviewOffender']))
$template->assign('PreviewOffender', $var['PreviewOffender']);
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/alliance_message.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
$threads[$j]['ViewHref'] = SmrSession::getNewHREF($container);
}
}
$template->assignByRef('Threads',$threads);
$template->assign('Threads',$threads);

if ($mbWrite || in_array($player->getAccountID(), Globals::getHiddenPlayers())) {
$container = create_container('alliance_message_add_processing.php');
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/alliance_message_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
$container['thread_index'] = $thread_index;
$thread['CreateThreadReplyFormHref'] = SmrSession::getNewHREF($container);
}
$template->assignByRef('Thread',$thread);
$template->assign('Thread',$thread);
if(isset($var['preview'])) {
$template->assign('Preview', $var['preview']);
}
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/alliance_roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
$container['alliance_id'] = $alliance->getAllianceID();
$allianceRoles[$roleID]['HREF'] = SmrSession::getNewHREF($container);
}
$template->assignByRef('AllianceRoles',$allianceRoles);
$template->assign('AllianceRoles',$allianceRoles);
$container = create_container('alliance_roles_processing.php');
$container['alliance_id'] = $alliance->getAllianceID();

Expand Down
6 changes: 3 additions & 3 deletions engine/Default/alliance_roster.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}

$alliance =& SmrAlliance::getAlliance($var['alliance_id'],$player->getGameID());
$template->assignByRef('Alliance', $alliance);
$template->assign('Alliance', $alliance);

$template->assign('PageTopic', $alliance->getAllianceName(false, true));
require_once(get_file_loc('menu.inc'));
Expand All @@ -30,7 +30,7 @@
while ($db->nextRecord()) {
$roles[$db->getInt('role_id')] = $db->getField('role');
}
$template->assignByRef('Roles', $roles);
$template->assign('Roles', $roles);

$container=create_container('alliance_roles_save_processing.php');
$container['alliance_id'] = $alliance->getAllianceID();
Expand Down Expand Up @@ -65,7 +65,7 @@
$template->assign('CanChangeRoles', $allowed);

$alliancePlayers =& SmrPlayer::getAlliancePlayers($player->getGameID(),$alliance->getAllianceID());
$template->assignByRef('AlliancePlayers', $alliancePlayers);
$template->assign('AlliancePlayers', $alliancePlayers);

if ($alliance->getAllianceID() == $player->getAllianceID()) {
// Alliance members get to see active/inactive status of members
Expand Down
4 changes: 2 additions & 2 deletions engine/Default/alliance_stat.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
$db->query('SELECT * FROM alliance_has_roles WHERE alliance_id = ' . $db->escapeNumber($alliance_id) . ' AND game_id = ' . $db->escapeNumber($player->getGameID()) . ' AND role_id = ' . $db->escapeNumber($role_id));
$db->nextRecord();

$template->assignByRef('Form', $form);
$template->assignByRef('Alliance', $alliance);
$template->assign('Form', $form);
$template->assign('Alliance', $alliance);

$template->assign('CanChangeDescription', $db->getBoolean('change_mod') || $account->hasPermission(PERMISSION_EDIT_ALLIANCE_DESCRIPTION));
$template->assign('CanChangePassword', $db->getBoolean('change_pass'));
Expand Down
6 changes: 3 additions & 3 deletions engine/Default/bank_alliance.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
}
}
$template->assignByRef('AlliedAllianceBanks', $alliedAllianceBanks);
$template->assign('AlliedAllianceBanks', $alliedAllianceBanks);

$db->query('SELECT transaction, sum(amount) as total FROM alliance_bank_transactions
WHERE alliance_id = ' . $db->escapeNumber($alliance->getAllianceID()) . ' AND game_id = ' . $db->escapeNumber($alliance->getGameID()) . ' AND payee_id = ' . $db->escapeNumber($player->getAccountID()) . '
Expand Down Expand Up @@ -137,7 +137,7 @@
'Exempt' => $db->getInt('exempt') == 1
);
}
$template->assignByRef('BankTransactions', $bankTransactions);
$template->assign('BankTransactions', $bankTransactions);

$template->assign('MinValue', $minValue);
$template->assign('MaxValue', $maxValue);
Expand All @@ -150,7 +150,7 @@
$container['maxVal'] = $maxValue;
$template->assign('ExemptTransactionsFormHREF',SmrSession::getNewHREF($container));

$template->assignByRef('Alliance', $alliance);
$template->assign('Alliance', $alliance);
}

$container=create_container('skeleton.php', 'bank_report.php');
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/bounty_view.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
$bountyPlayer =& SmrPlayer::getPlayer($var['id'], $player->getGameID());
$template->assign('PageTopic', 'Viewing ' . $bountyPlayer->getPlayerName());
$template->assignByRef('BountyPlayer', $bountyPlayer);
$template->assign('BountyPlayer', $bountyPlayer);
?>
2 changes: 1 addition & 1 deletion engine/Default/buy_message_notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
$messageBox['BuyHref'] = SmrSession::getNewHREF($container);
$messageBoxes[] = $messageBox;
}
$template->assignByRef('MessageBoxes', $messageBoxes);
$template->assign('MessageBoxes', $messageBoxes);

?>
4 changes: 2 additions & 2 deletions engine/Default/chess.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
while ($db->nextRecord()) {
$players[$db->getInt('player_id')] = $db->getField('player_name');
}
$template->assignByRef('PlayerList',$players);
$template->assign('PlayerList',$players);

if(ENABLE_NPCS_CHESS) {
$npcs = array();
$db->query('SELECT player_id, player.player_name FROM player JOIN account USING(account_id) JOIN npc_logins USING(login) WHERE validated = ' . $db->escapeBoolean(true) . ' AND game_id = ' . $db->escapeNumber($player->getGameID()) . ' AND account_id NOT IN (' . $db->escapeArray(array_keys($playersChallenged)) . ') ORDER BY player_name');
while ($db->nextRecord()) {
$npcs[$db->getInt('player_id')] = $db->getField('player_name');
}
$template->assignByRef('NPCList',$npcs);
$template->assign('NPCList',$npcs);
}

?>
2 changes: 1 addition & 1 deletion engine/Default/chess_move_processing.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
require_once(get_file_loc('ChessGame.class.inc'));
$chessGame =& ChessGame::getChessGame($var['ChessGameID']);
$template->assignByRef('ChessGame',$chessGame);
$template->assign('ChessGame',$chessGame);
if(is_numeric($_REQUEST['x']) && is_numeric($_REQUEST['y']) && is_numeric($_REQUEST['toX']) && is_numeric($_REQUEST['toY'])) {
$x = $_REQUEST['x'];
$y = $_REQUEST['y'];
Expand Down
2 changes: 1 addition & 1 deletion engine/Default/chess_play.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
require_once(get_file_loc('ChessGame.class.inc'));
$template->assignByRef('ChessGame',ChessGame::getChessGame($var['ChessGameID']));
$template->assign('ChessGame',ChessGame::getChessGame($var['ChessGameID']));
$template->assign('ChessMoveHREF',SmrSession::getNewHREF(create_container('chess_move_processing.php','',array('AJAX' => true, 'ChessGameID' => $var['ChessGameID']))));
?>
2 changes: 1 addition & 1 deletion engine/Default/combat_log_viewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
$template->assign('CombatLogTimestamp',date(DATE_FULL_SHORT,$db->getField('timestamp')));
$results = unserialize(gzuncompress($db->getField('result')));
$template->assign('CombatResultsType',$db->getField('type'));
$template->assignByRef('CombatResults',$results);
$template->assign('CombatResults',$results);
}
else {
create_error('Combat log not found');
Expand Down
6 changes: 3 additions & 3 deletions engine/Default/combat_simulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

for(;$i<=10;++$i)
$attackers[$i] = null;
$template->assignByRef('Attackers',$attackers);
$template->assign('Attackers',$attackers);

$i=1;
$realDefenders = array();
Expand All @@ -50,7 +50,7 @@

for(;$i<=10;++$i)
$defenders[$i] = null;
$template->assignByRef('Defenders',$defenders);
$template->assign('Defenders',$defenders);

$template->assign('Duplicates',$duplicates);

Expand Down Expand Up @@ -89,6 +89,6 @@ function runAnAttack($realAttackers,$realDefenders) {
$results['Defenders']['Traders'][] =& $playerResults;
$results['Defenders']['TotalDamage'] += $playerResults['TotalDamage'];
} unset($teamPlayer);
$template->assignByRef('TraderCombatResults',$results);
$template->assign('TraderCombatResults',$results);
}
?>
2 changes: 1 addition & 1 deletion engine/Default/configure_hardware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
while ($db->nextRecord()) {
$ships[$db->getField('ship_type_id')] = $db->getField('ship_name');
}
$template->assignByRef('IllusionShips',$ships);
$template->assign('IllusionShips',$ships);
$container['action'] = 'Disable Illusion';
$template->assign('DisableIllusionHref',SmrSession::getNewHREF($container));
}
Expand Down
6 changes: 3 additions & 3 deletions engine/Default/council_politics.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
}
}

$template->assignByRef('PeaceRaces', $peaceRaces);
$template->assignByRef('NeutralRaces', $neutralRaces);
$template->assignByRef('WarRaces', $warRaces);
$template->assign('PeaceRaces', $peaceRaces);
$template->assign('NeutralRaces', $neutralRaces);
$template->assign('WarRaces', $warRaces);

?>
6 changes: 3 additions & 3 deletions engine/Default/current_sector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

$sector =& $player->getSector();

$template->assignByRef('ThisSector',$sector);
$template->assign('ThisSector',$sector);
$template->assign('SpaceView',true);

$template->assign('PageTopic','Current Sector: ' . $player->getSectorID() . ' (' .$sector->getGalaxyName() . ')');
Expand Down Expand Up @@ -46,7 +46,7 @@
}
}

$template->assignByRef('Sectors',$links);
$template->assign('Sectors',$links);

doTickerAssigns($template, $player, $db);

Expand Down Expand Up @@ -169,7 +169,7 @@ function checkForAttackMessage(&$msg) {
if($player->getSectorID()==$db->getField('sector_id')) {
$results = unserialize(gzuncompress($db->getField('result')));
$template->assign('AttackResultsType',$db->getField('type'));
$template->assignByRef('AttackResults',$results);
$template->assign('AttackResults',$results);
}
}
}
Expand Down
Loading

0 comments on commit b287aa5

Please sign in to comment.