6

我一直在阅读有关 Phing 和 Ant 的文章,但我不确定这些工具中的哪一个(如果有的话)对这种情况最有用。

它可能很容易是调试语句等,但我会给你我们的文字扫描。

我们有一个可下载的 PHP 应用程序的免费和高级版本,而不是仅仅包含一个隐藏在某处的变量然后执行:

if($premium == true) {
   echo 'some additional functionality';
} else {
    echo 'basic functionality';
}

显然,有人可以获取源代码并更改该变量,然后砰——他们窃取了我们的代码。在我的经验中,像 Ioncube 之类的东西完全不实用,而且对托管公司的支持还不够好。

我更喜欢一些东西..也许类似于:

## if premium ##
echo 'some additional functionality';
## else ##
echo 'basic functionality';
## endif ##

然后我会运行两个构建,一个将溢价设置为 true,一个设置为 false,这将生成两个简单的文件:

echo 'some additional functionality';

echo 'basic functionality';

能够仅包含基于传递给构建应用程序的相同条件的整个文件也将非常有帮助。

我找不到这样做的方法,但如果可能的话,我对任何替代想法持开放态度。

帮助将是杰出的,

更新

使用 C 预处理器很棒,看起来它可以满足我的一切需求。但是,我找不到如何做以下三件事。

#1我需要删除生成到输出文件中的注释。下面是其中的一个例子。

# 1 "./index.php"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "./index.php"

在您链接到的手册页中,我没有找到如何执行此操作的示例。

#2我需要让它递归地遍历每个发现的文件。当我运行当前代码时,出现错误:../target/./folder/test.php: No such file or directory

所以基本上我有我所在的“源”文件夹,其中包含一个名为“文件夹”的子文件夹,它不会重新创建它,也不会重新创建其中的文件(test.php)

#3我敢肯定这很容易——我怎样才能让它处理 .js 文件和可能 .html 只是为了安全?一通电话,我的意思是。我认为在 .jpg 等文件上运行它是一个坏主意..

再次感谢!

4

6 回答 6

4

很抱歉挖掘了这个话题,但我有同样的需求,而且 CPP 方法对我的使用有太多的副作用。

所以我为 phing 开发了一个基本的预处理器过滤器来解决问题:

#ifdef premium
  echo 'some additional functionality';
#else
  echo 'basic functionality';
#endif

该过滤器在 github 上可用:https ://github.com/tmuguet/PreProcessorFilter

于 2012-11-28T20:56:32.423 回答
1

我有了一个想法,在此之前的答案被接受,但我还是想分享它。所以这里是交易:

将所有付费功能存储在单独的文件中。使用钩子类,您可以检查并添加所需的功能。不要在免费版本的构建中包含付费功能。您将拥有 2 个 zip 文件,但只有一个来源。

这是一个非常简单的 Hook 类,可能会对您有所帮助:

Class Hook {
    var $features_enabled;
    public function __construct() {
        // Construction codes goes here.
        // You can check features files. If there is, require them once
        // if there isnt any, define a variable:
        if (file_exists("./features/feature1.php")) {
            require_once './features/feature1.php';

            // set this true. so we can check the features and run it
            $this->features_enabled = TRUE;
        }
        else {
            // there is no feature avaliable. So there is no need to check functions.
            $this->features_enabled = FALSE;
        }
    }

    /**
     * Check the Feature.
     *
     * @param string The feature name
     * @param array|string|... The arguments that you want to pass.
     */
    public function check($feature_name, $args = NULL) {
        // if features cannot be included on constructor, do not need to check.
        if ($this->features_enabled == FALSE)
            return;

        // if feature is a function: check it then run it
        if (function_exists($feature_name))
            $feature_name($args);

        // if feature is a Class: create it then return it.
        if (class_exists($feature_name))
            return new $feature_name($args);
    }
}

然后,您可以在代码的任何位置检查它们。例子:

$hook = new Hook();

//.... Codes goes here.

// This will check for a function of class. If there is, execute it
$hook->check('feature_badges');

我知道这很简单,需要以许多其他方式进行开发。但如果你能管理它:

  • 您将分离特征。这样,您可以创建不同的包。
  • 您将使用类检查功能。即使用户看到功能名称,也没关系。因为他看不到功能的代码。
于 2011-05-27T22:47:44.323 回答
0

嗯,Phing 是 PHP 的 Ant。我从未使用过 Phing,但我认为它已针对此类内容进行了调整。

使用 Phing 创建两个单独的包:一个是高级应用程序,一个是免费应用程序。

您也可以使用 Ant,但 Ant 针对 Java 进行了调整,而 Phing 针对 PHP 进行了调整。您可能希望在 Phing 上使用 Ant 的唯一原因是 Ant 可用的资源比 Phing 多得多。但是,如果您是一家 PHP 商店,那么学习使用 Phing 只是为了在简历上看起来不错。(“是的,我是一名高级 PHP 开发人员。我什至知道 Phing”)。

于 2011-05-27T14:55:13.643 回答
0

It's pretty low-tech, but there is of course the C preprocessor which does exactly what you want; just bang in a couple of makefiles to call it with find or grep -R and you get a simple, easy-to-understand solution with syntax you probably know.

More detail

You probably have gcc installed already on any *nix host. Otherwise, it'll be a standard package. Some distributions provide it separately to to gcc (like Debian's cpp package).

The program has some simple instructions; the wiki page is a good start, and the manual has more detail than you need. Basically, it's a matter of calling it on each file with the -E option just to do the macro processing, and then copying the output some build directory.

You can write a one-liner script to do that with find, along of the lines of find <proj dir> -type f -name '*.php' -exec cpp -E -D <FULL or RESTRICTED> {} -o <build dir>/{} \; and reference the macros FULL and RESTRICTED in your PHP, like

#ifdef FULL
    <lines for paying customers>
#endif

UPDATE To make the paths work nicely, try this out:

#!/bin/bash
cd /.../phing/source/
find . -type f -name '*.php' -exec cpp -E -D FULL {} -o ../target/{} \;

Then ../target/{} should be expanded to ../target/./index.php.

UPDATE

Added -P to remove the linemarkers (#1). Added a line to copy directory structure (#2). Changed filename match to run on js on html (#3).

#!/bin/bash
cd /.../phing/source/
find . -type d -exec mkdir -p ../target/{} \;
find . -type f -regex '.*\.(php|html|js)' -exec cpp -E -P -D FULL {} -o ../target/{} \;
于 2011-05-27T14:15:34.923 回答
0

您可以自己构建这样的预处理器 - PHP 内置支持解析 PHP 文件:http ://www.php.net/manual/en/function.token-get-all.php

我已经用它构建了一个混淆器,并且必须说它非常易于使用。只需打印您想要的令牌并删除“## if premium ##”块内的令牌。

更新:我猜 C 预处理器的想法更好。不过为了完整起见,请留下这个答案。:)

于 2011-05-30T10:27:23.270 回答
0

免责声明:我完全确定这不是最有效的方法!

--

我分 4 部分执行此操作:

  • source将目录结构克隆到target
  • 将 PHP 和 JS 文件sourcetarget
  • 将所有 jpg、gif、css 等文件复制sourcetarget
  • 从所有 PHP 和 JS 文件中删除 cpp 调用添加的注释target

--

find ./ -type d -exec mkdir ../target/{} \;
find ./ -regextype posix-awk -regex "(.*.php|.*.js)" -exec cpp -E -D COMMERCIAL {} -o ../target/{} \;
find ./ -type f -regextype posix-extended -regex "(.*.jpg|.*.gif|.*.png|.*.css|.*.html|.*.txt|.*.xml|.*.ttf)" -exec cp -p --parents "{}" ../target \;
cd ../target
find ./ -regextype posix-awk -regex "(.*.php|.*.js)" -exec sed -i ./{} -e 's/#.*//;/^\s*$/d' \;

--

正如我所说,我确信这并不是非常低效,但我执行并敲击了一个 .sh 文件 - 我有我的构建!

于 2011-06-02T14:36:52.253 回答