PHP preprocessing to remove functionality from embedded files

I read about Phing and Ant, and I'm not sure which of these tools are most useful for this scenario.

These can be easily debug statements, etc., but I will give you our literal scanario.

We have a free and high-quality version of a downloadable PHP application, and instead of just including some kind of variable hidden somewhere, and then doing:

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

Obviously, someone could take the source code and change this variable, and hit - they stole our code. And something like Ioncube, etc. Just awesome in my experience, and hosting company support is just not good enough.

I would prefer something ... maybe it looks like this:

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

And then I would run two assemblies, one set the premium to true and one to false, which would generate two files simply:

 echo 'some additional functionality'; 

and

 echo 'basic functionality'; 

It would be very useful to be able to include only whole files based on the same condition passed to the build application.

I cannot find a way to do this, but I am open to any alternative ideas, if possible.

Help will be outstanding,

UPDATE

Using the C preprocessor is great and looks like it does everything I need. However, I cannot find how to do the following 3 things.

# 1 I need to delete comments generated in the output files. Below is an example of these.

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

I did not find an example of how to do this on the manual page with which you linked me.

# 2 I need it to recursively scan each detected file. When I run my current code, I get an error message: ../target/./folder/test.php: No such file or directory

So basically I have my “source” folder in which I was located, which contains a subfolder called “folder”, and it does not recreate this, nor the files inside it (test.php)

# 3 I'm sure this is easy - how can I get it to handle .js files and possibly .html to be safe? In one call, I mean. I assume I ran it on .jpg, etc. Files are a bad idea.

Thanks again!

+6
source share
6 answers

This is a pretty low-tech, but, of course, C preprocessor that does exactly what you want; just run a couple of makefiles to name it find or grep -R and you will get a simple, easy to understand solution with syntax that you probably know.

More details

You probably already have gcc installed on any * nix host. Otherwise, it will be a standard package. Some distributions provide it separately for gcc (for example, the Debian cpp package).

The program has some simple instructions; The wiki page is a good start, and the manual is more detailed than you need. In principle, it is a matter of calling it in each file with the -E option only for processing macros, and then for copying the output file of any assembly.

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

 #ifdef FULL <lines for paying customers> #endif 

UPDATE To make the tracks work beautifully, try this:

 #!/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 linear characters (# 1). Added line for copying directory structure (# 2). Changed file name match for js to 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/{} \; 
0
source

I apologize for this topic, but I had the same need, and the CPP approach had too many side effects for my use.

So, I developed a basic pre-processor filter for phing, which does the trick:

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

The filter is available on github: https://github.com/tmuguet/PreProcessorFilter

+4
source

I came up with the idea, the answer has been accepted before, but I want to share it anyway. So here is the deal:

Save all paid features in separate files. Using the hook class, you can check and add the necessary functions. Do not include paid features in the free version of the assembly. You will have 2 zip files, but one source.

Here is a very simple hook class that can help you:

 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); } } 

Then you can check them anywhere in your code. Example:

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

I know that it is very simple and needs to be developed in many other ways. But if you handle this:

  • You separate the functions. This way you can create different packages.
  • You will check the functions with the class. Even if the user sees the function name, it does not matter. Because he does not see the function code.
+1
source

Well, Phing is Ant for PHP. I have never used Phing, but I assume it is set up for such things.

Use Phing to create two separate packages: one premium application and one for a free application.

You can also use Ant, but Ant is configured for Java, and Phing is configured for PHP. The only reason you can use Ant on top of Phing is because Ant has much more resources available than Phing. But, if you are a PHP store, learn to use Phing, if nothing else, to look good in a resume. ("Yes, I'm a senior PHP developer, I even know Phing").

0
source

You can simply create such a preprocessor yourself - PHP has built-in support for parsing PHP files: http://www.php.net/manual/en/function.token-get-all.php

I used it to create an obfuscator and I must say that it is very easy to use. Just print the markers you want and remove the markers that are inside the ## if premium ## blocks.

UPDATE: I think the idea of ​​the C preprocessor is better. However, leave this answer complete. :)

0
source

Disclaimer: I am absolutely sure that this is not the most effective way to do this!

-

I do this in 4 parts:

  • Clone source directory structure to target
  • Parse PHP and JS files from source to target
  • Copy all jpg, gif, css, etc. files from source to target
  • Remove comments added by cpp call from all PHP and JS files in 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' \; 

-

As I said, I'm sure this is not very inefficient, but there is one .sh file that I execute and hit - I have my builds!

0
source

Source: https://habr.com/ru/post/889211/


All Articles