dongpouda6700 2013-01-16 17:07 采纳率: 100%
浏览 32
已采纳

将这些位掩码定义从pawn语言转换为PHP的可能方法有哪些?

Well, to clarify, pawn is a language that is pretty similar to C++. I'm programming an Admin Control Panel that requires bit mask permissions (in php) and I actually don't know if I converted the permissions properly (since it's something I have never done) to PHP. I used define() to define permissions. Here's how it originally is (in pawn):

//Pawn
enum EAdminFlags (<<= 1) {
    EAdminFlags_None = 0, 
    EAdminFlags_BasicAdmin = 1, //kick, fine, etc 0
    EAdminFlags_CanGiveGuns, //1
    EAdminFlags_HouseAdmin, //2
    EAdminFlags_IRCAdmin, //3
    EAdminFlags_BusinessAdmin, //4
    EAdminFlags_AdminManage, //5 can see invisible admins/manipulate admin flags
    EAdminFlags_Scripter, //getpvars, reloadcmds, stuff only scripters should need 6
    EAdminFlags_HelperManage, // 7 manage helpers/teachers
    EAdminFlags_Unbannable, //unbanable under ANY circumstances 8
    EAdminFlags_CanRefundSelf, //can refund himself
    EAdminFlags_VehicleAdmin, //10 can manipulate player vehicles
    EAdminFlags_AntiCheat, //immune to the anticheat 11
    EAdminFlags_BigEars, //can /bigears, /bigfamilyears, /bigmouth
    EAdminFlags_SetName, //13
    EAdminFlags_CanHide, //can toggle/untoggle hidden on themselves 14
    EAdminFlags_Invisible, //Invisible on admins list(like 9999s before) 15
    EAdminFlags_GiveDrugs, //server manager, can restart the server/do rcon cmds, create job cars 16
    EAdminFlags_Unban, //can unban, oban, oprison people 17
    EAdminFlags_WeatherManage, //can manage weather/time 18
    EAdminFlags_GiveRestrictedGun, //can give restircted guns 19
    EAdminFlags_TeleportOthers, //can teleport other people 20
    EAdminFlags_CanBanAdmins, //can ban other admins(or any punishments) 21
    EAdminFlags_GiveMats, //can give materials 22
    EAdminFlags_StatsManage, //23 /setstat, /sethp, /setarmor
    EAdminFlags_FamilyAdmin, //24
    EAdminFlags_FactionAdmin, //25
    EAdminFlags_MassCmds, //26 can do /masstphere, /massmoney, /masskick, /massnuke
    EAdminFlags_Nuke, //27 can do things like /nuke, sparta, rangeban
    EAdminFlags_RangeBan, //28 can range ban
    EAdminFlags_GiveMoney, //29 can /givemoney /money
    EAdminFlags_Donations, //30 can give DPS/set donate rank
    EAdminFlags_ServerManager, //can restart the server, etc, use /noooc, /nopms, /nonewb
    EAdminFlags_All = -1,
};

Now this is how I defined it in PHP:

    define("EAdminFlags_None",0);
    define("EAdminFlags_BasicAdmin",1);
    define("EAdminFlags_CanGiveGuns",1 << 0);
    define("EAdminFlags_HouseAdmin",1 << 1);
    define("EAdminFlags_IRCAdmin",1 << 2);
    define("EAdminFlags_BusinessAdmin",1 << 3);
    define("EAdminFlags_AdminManage",1 << 4);
    define("EAdminFlags_Scripter",1 << 5);
    define("EAdminFlags_HelperManage",1 << 6);
    define("EAdminFlags_Unbannable",1 << 7); 
    define("EAdminFlags_CanRefundSelf",1 << 8);
    define("EAdminFlags_VehicleAdmin",1 << 9);
    define("EAdminFlags_AntiCheat",1 << 10);
    define("EAdminFlags_BigEars",1 << 11);
    define("EAdminFlags_SetName",1 << 12);
    define("EAdminFlags_CanHide",1 << 13);
    define("EAdminFlags_Invisible",1 << 14);
    define("EAdminFlags_GiveDrugs",1 << 15);
    define("EAdminFlags_Unban",1 << 16);
    define("EAdminFlags_WeatherManage",1 << 17);
    define("EAdminFlags_GiveRestrictedGun",1 << 18);
    define("EAdminFlags_TeleportOthers",1 << 19);
    define("EAdminFlags_CanBanAdmins",1 << 20);
    define("EAdminFlags_GiveMats",1 << 21);
    define("EAdminFlags_StatsManage",1 << 22);
    define("EAdminFlags_FamilyAdmin",1 << 23);
    define("EAdminFlags_FactionAdmin",1 << 24);
    define("EAdminFlags_MassCmds",1 << 25);
    define("EAdminFlags_Nuke",1 << 26);
    define("EAdminFlags_RangeBan",1 << 27);
    define("EAdminFlags_GiveMoney",1 << 28);
    define("EAdminFlags_Donations",1 << 29);
    define("EAdminFlags_ServerManager",1 << 30);
    define("EAdminFlags_All",-1);

I compared the bitflags and most of the time it just recognizes the EAdminFlags_All permission along with EAdminFlags_None and EAdminFlags_BasicAdmin. So there is probably an issue with how I defined this in PHP. The permissions are stored in a database and they are saved as a raw integer value for example: 51531777 Then it's loaded into PHP and compared like so:

$account = new Account(Auth::getUserID());
echo "Welcome ".$account->getUsername()."<br>";
if($account->getAdminPerms(EAdminFlags_FamilyAdmin)) { 
    echo "You are a family admin."; //testing purposes
    //If the person has those permissions set then display he / she is a family admin but even if the permissions are set to EAdminFlags_BasicAdmin, it will still display it wrong
}
public function getAdminLevel() {
    return $this->adminlevel; //This holds the integer value "51531777"
}
public function getAdminPerms($n) {
    return ($this->adminlevel & (1 << $n)) != 0;
}

Thank you for your help beforehand, I really appreciate it.

  • 写回答

2条回答 默认 最新

  • doupingtang9627 2013-01-16 17:32
    关注

    Assuming that your shift operations on the constants are right, there is an error in your detection of a particular rights flag:

    public function getAdminPerms($n) {
        return ($this->adminlevel & (1 << $n)) != 0;
    }
    

    Input is the flag value, which is only a 1 bit set at the correct position. You incorrectly shift again, but all you need to do is AND the integer value with the one read from the database:

    public function getAdminPerms($n) {
        return ($this->adminlevel & $n)) != 0;
    }
    

    The way back works the same. Starting from a zero value, you can add all admin rights flags with bit-OR-ing all the constant values without shifting:

    $newAdmin = EAdminFlags_AntiCheat | EAdminFlags_CanGiveGuns;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度