2

我制作了一些类,其中包含许多使用 PHP 正确记录的方法(类似于库)。

现在,其他开发人员要做的只是需要我在他们的代码中创建的 PHP 库并使用其中的预定义函数。

是否可以向其他 PHP 开发人员(需要文件)隐藏 PHP 代码(我制作的库),只向他们显示函数名称、参数及其文档而不显示其中的代码?我不是在谈论可以可逆的混淆,我是在谈论防止用户实际看到任何代码。

例如。

/**
 * 
 * CREATE A NEW THREAD
 * @param unknown_type $uid User ID of person who is creating the thread
 * @param unknown_type $participant An array having collection of UID of people who are participating in this conversation 
 * @param unknown_type $msgtype Message Type Flags (1-normal, 2-chat, 3-sent as email, 4-profile post, 5-group post, 6-customer support)
 * @param unknown_type $subject Subject of the thread
 * @param unknown_type $tname Thread Name
 * @param unknown_type $tpic Thread Cover Picture (Defaults to "")
 * @param unknown_type $tflag Thread Flag (1-allowed,2-under review,3-blocked) (Defaults to 1)
 * @return string|Ambigous <string, unknown> Thread ID on success, "" on failure
 */
public function createthread($uid,$participant,$msgtype,$subject,$tname,$tpic="",$tflag="1")
{
    $randobj=new uifriend();
    $tid=$randobj->randomstring(30,DB_MESSAGE,MSG_OUTLINE,msgoutline_tid);
    $socialobj=new socialoperations();
    $listid=$socialobj->createlist("threadlist_".$tid, "2",$msgtype,"1",$uid);
    if($socialobj->addtolist($participant, $listid, $uid)!="SUCCESS")
    {
        return "";
    }
    if($listid=="")
    {
        $lasterror="An error occured in creating thread! Unable to Create Lists!";return "";
    }
    $dbobj=new dboperations();
    $res=$dbobj->dbinsert("INSERT INTO ".MSG_OUTLINE." (".msgoutline_tid.",".msgoutline_subject.",".msgoutline_fid.",".msgoutline_participantid.",".msgoutline_msgtype.",".msgoutline_threadpic.",".msgoutline_threadflag.") VALUES
            ('$tid','$subject','$uid',$listid,'$msgtype','$tpic','$tflag')",DB_MESSAGE);
    if($res=="SUCCESS")
    {
        return $tid;
    }
    else
    {
        $lasterror="Unable to create Thread!";return "";
    }
}

其他开发人员必须只能看到我在函数上方编写的带有函数名称和参数的文档,但他们不能以任何方式访问代码。

为什么我想要这个:我的 PHP 文件中有很多安全代码,我不想向其他开发人员展示,但仍然允许他们调用函数并读取返回值。

4

2 回答 2

4

如果你想让他们直接调用你的函数,你不能对其他开发者隐藏你的代码。您可以做的是制作一个Web 服务并将其文档提供给其他开发人员。

于 2013-04-07T13:26:49.193 回答
3

因为我有一个元帖子所以重新打开另一个元帖子来格式化这个问题,我会尽力正确回答这个问题。请注意,这只是执行此操作的一种方式,其限制在帖子末尾说明。

API

远程服务器

您可以在不同的域中创建Web API并从您的主域访问它。我认为解释它是如何工作的最好方法是用一个实际的例子。想象一下,您的库包含函数“joinstrings()”,它接受 2 个参数。然后你把它放在你分离的网络中:

http://apiweb.com/functions.php

<?php
// Your API. I hope the real one is more complex than this (;
function joinstrings($s1, $s2)
  {
  return $s1 . $s2;
  }
// More functions

远程服务器访问点

这是公共(但需要密钥)可访问页面。

http://apiweb.com/joinstrings/index.php

<?php
// Check if the key is valid and if $v1 and $v2 aren't empty. Else, 'exit;'
include '../validate.php';
// Your API
include '../functions.php';
// The called function
echo joinstrings(urldecode($_GET['v1']), urldecode($_GET['v2']));

包装器

现在你可以要求你所有的程序员学习如何使用这个 API。或者,如果你更愿意做正确的事,你会做一个让他们的生活更轻松的包装。您将拥有一个包含您想要访问的所有方法的类。你可以用函数来做这个包装器,但我认为使用对象和方法更容易更好:

htpp://web.com/library.php

<?php
class DevelopersLibrary
  {
  private $Url = "http://apiweb.com/";
  // Press your hand against the keyboard. A-Z0-9. Copy it in http://apiweb.com/validate.php
  private $Key = "g139h0854g76dqfdbgng";

  // Accesible method
  public joinstrings($v1, $v2)
    {
    // Encode only the user input. You don't want to encode '?' nor '&'
    if ($Return = file_get_contents($this->Url . 'joinstring'
                                    '?key=' . $this->Key .
                                    '&v1=' . urlencode($v1) .
                                    '&v2=' . urlencode($v2)))
      {
      return $Return;
      }
    }
  }

开发者代码

最后,您的开发人员会做什么:

http://web.com/index.php

<?php
include './library.php';
$Lib = new DevelopersLibrary();
echo $Lib->joinstrings("Are you sure this is better", "than giving your developers access to the code?");

没有任何代码经过测试,因此您应该预料到会出现一些拼写错误。

限制

我可以想到大多数限制的解决方案,但不能扩展(更多)这篇文章,我不会在这里写它们。如果您在评论中需要它,请寻求限制的解决方案,我会尽力而为。在正常情况下使用,这些限制都不是那么重要

  • 传递的参数。使用如上所述的这种方法,您只能将数字或字符串作为函数参数传递。查看json_encoding()以传递其他类型。

  • API 中存在错误或传递的参数时返回错误的值。如果 API 有 bug,开发者无法修复,返回值可能是错误的。现在这似乎微不足道,但如果他们试图检索 2 个字符串的连接并检索另一个包含错误文本的 [错误] 字符串怎么办?注意:考虑返回有效的XML,然后在包装器中解析它。

  • 只有一个唯一的密钥用于防止随机用户使用您的 API,而不是对开发人员隐藏。

  • 速度较慢。我认为这甚至不需要解释。

  • 开发人员的额外工作。这是通过包装器的实现解决的。

  • 网址长度大多数浏览器都有 2000 个字符的 url 长度限制,尽管我在PHP 手册中没有找到file_get_contents() 的任何内容。阅读此 SO 问题以获取有关 GET 的更多信息

  • 当然还有更多,但这些是我能想到的主要内容。

我希望这个长长的答案对您或某人有用。

于 2013-04-08T14:39:36.760 回答