PHP userland backwards compatibility layer that emulates PHP 5.5+ core functions.

⌈⌋ ⎇ branch:  upgrade.php


Check-in [6c573fe397]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:header_remove() accept empty param
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 6c573fe397a25841df3ee76ba826ca3ed5916d8a
User & Date: mario 2010-06-23 00:28:35
Context
2010-06-23
00:44
fix json_encode() escaping order in str_replace check-in: b36f178e27 user: mario tags: trunk
00:28
header_remove() accept empty param check-in: 6c573fe397 user: mario tags: trunk
2010-06-22
23:00
array_intersect_key() and _ukey() added, @untested check-in: 28059f030c user: mario tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to doc/devtools/doctests.

1
2
3
4
5
6
7
8
#!/usr/local/bin/php -qC
<?php
/*
   Makes small test scripts, which get extracted from the PHP manual
   on the fly (but not every function has one there, suddenly).
*/

#-- config
|







1
2
3
4
5
6
7
8
#!/usr/bin/php -q
<?php
/*
   Makes small test scripts, which get extracted from the PHP manual
   on the fly (but not every function has one there, suddenly).
*/

#-- config
20
21
22
23
24
25
26

27
28
29
30
31
32
33

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52


53


54
55
56
57


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

73
74
75
76

77
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
   Runs the examples from your locally installed PHP manual (any language
   will work, but get the latest!) with the given PHP interpreter version
   (use an older version to really test the emulated functions). You need
   lynx or w3m installed also. Use only under U*ix/Linux.

   Pipe the output through |less, |more or |most, there are built-in
   delays. Rarely example scripts may not run (they are examples only).


END;
}

#-- proceed
else {


   #-- args
   $php = $_SERVER["argv"][1];
   $php = trim(`which $php`);
   if (!$php) {
      die(__FILE__.": Given PHP interpreter not in your %PATH!\n");
   }
   $dir = $_SERVER["argv"][2];
   if (!is_dir($dir) || !file_exists("$dir/function.print.html")) {
      die(__FILE__.": PHP manual does not live under '$dir'.\n");
   }
   ($html = `which w3m`) or ($html = `which lynx`);
   if (!($html = trim($html))) {
      die(__FILE__.": lynx or w3m required.\n");
   }
   $tmp = "/tmp/upgrade.php.doctest.tmp";
   $tmpdir = $tmp;
   if ($MANY) { @unlink($tmpdir); @mkdir($tmpdir); }

   #-- load emulation script


   $emu = realpath(dirname(__FILE__) . "/../upgrade.php");


   $emu2 = dirname($emu)."/ext/array.php";
   $emu3 = dirname($emu)."/ext/mime.php";
   require($emu);



   #-- get function names
   $text = file_get_contents($emu);
   preg_match_all("/function[ ]+([_\w\d]+)\s*\(/", $text, $uu);
   $funcs = $uu[1];
   #-- or use user-specified list
   if (count($_SERVER["argv"]) >= 4) {
      $funcs = array_slice($_SERVER["argv"], 3);
   }

   #-- generate list of really emulated functions
   $simulated = `echo '<?php echo serialize(get_defined_functions()); ?>' | $php -q`;
   $simulated = unserialize($simulated);
   $simulated = $simulated["internal"];

   #-- all

   foreach ($funcs as $func) {
      if (file_exists($fn = "$dir/function.".strtr($func, "_", "-").".html")) {
         echo "checking function '\033[31m$func\033[37m'... ";


         #-- only run over emulated stuff
         if (in_array($func, $simulated)) {

            echo "NOT EMULATED (with your PHP version)... ";
            // continue;
         }

         #-- grep example scripts
         $text = `$html -dump $fn`;
         preg_match_all("/<\?php(.+?)\?".">/ms", $text, $uu);

         #-- exec each
         if ($n = count($uu[1])) {

            #-- note
            echo "$n examples:\n\n";

            #-- multiple scripts to run
            foreach ($uu[1] as $i=>$scr) {

               #-- fix output-less scripts: find last assigned-to variable name
               if (!strpos($scr, "echo") && !strpos($scr, "print")) {
                  if (preg_match('/^.+(\$[_\w\d\[\"\'\]]+)\s*=/s', $scr, $uu)) {
                     $scr .= "\n\n#-- auto-added\nprint_r($uu[1]);\n";
                  }
                  else {
                     $scr .= "\n\n#-- this script gives no useful output, or does it?";
                  }
               }

               #-- fix compatibility to older PHP versions
               $scr = preg_replace('/(\s)private(\s\$)/', '$1var$2', $scr);

               #-- output sample script text from doc
               if ($i) {
                  echo "\n++++++++++++++++++++++++++++\n\n";
                  sleep($PAUSE);
               }
               echo "\033[1;30m<?php $scr\n?".">\033[0;37m\n";

               #-- create temp script, run it
               if ($MANY) {
                  $tmp = "$tmpdir/$func#$i.php";
               }
               $scr = ltrim($scr);
               file_put_contents(
                  $tmp,
                  "<?php\n\n".
                  "#-- a test script for emulated function '$func'\n".
                  "if (function_exists('$func')) { echo \"ATTENTION: the native '$func' function will engage for this test\\n\"; }\n".
                  "include('$emu');\n".
                  "include('$emu2');\n".
                  "include('$emu3');\n".
                  "error_reporting(E_ALL);\n\n".
                  "#-- example[$i] as taken from PHP manual page '$fn'\n".
                  "$scr\n".
                  "\n?".">"
               );
               echo "==>\033[32m\n";
               passthru("$php $tmp");
               echo "\033[0;37m\n\n";
            }








>







>

<
|
<
<
<
|
<
<
<
<
|
<
<
<
|
|


>
>
|
>
>
|
|
|

>
>

<
<
|
<
<
<
|
<

|
|
|


>

<
<

>
|
|
>
|
<
|


|
<


|

<
<
<

<
|
<
<
<
<
|
<
|
|
<
|
<
<


|
<
<
<
<





|




|
|
|
|


|







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

37



38




39



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


56



57

58
59
60
61
62
63
64
65


66
67
68
69
70
71

72
73
74
75

76
77
78
79



80

81




82

83
84

85


86
87
88




89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
   Runs the examples from your locally installed PHP manual (any language
   will work, but get the latest!) with the given PHP interpreter version
   (use an older version to really test the emulated functions). You need
   lynx or w3m installed also. Use only under U*ix/Linux.

   Pipe the output through |less, |more or |most, there are built-in
   delays. Rarely example scripts may not run (they are examples only).


END;
}

#-- proceed
else {


   #-- args

   $php = php_interpreter();



   $dir = get_manual();




   $html = get_lynx();



   $tmpdir = $tmp = tmp();


   #-- load emulation script
   chdir(".");
   system("./doc/devtools/test-up");
   # dirname(dirname(__FILE__)) );
   $upgrade_php = "upgrade.php";
   $test_up = "test-up.php";
   $emu2 = "ext/array.php";
   $emu3 = "ext/mime.php";
   require($upgrade_php);



   #-- get function names


   $funcs = emulated_functions();





   #-- generate list of really emulated functions
   $simulated = simulated();



   #-- all
   $funcs = emulated_functions();
   foreach ($funcs as $func) {



      # read from php-manual
      if ($fn = func2fn($func))
#      and $ALL || in_array($func, $simulated) )
      {
         echo "checking function '\033[31m$func\033[37m'... ";



         #-- grep example scripts
         $tests = example_scripts($func);


         #-- exec each
         if ($n = count($tests)) {




            #-- multiple scripts to run

            foreach ($tests as $i=>$script) {






               add_output($script);   // add print or echo if missing


               up_version($script, $func);   // use test-up.php "up_functionname"



               #-- output sample script text from doc
               print_script($script);





               #-- create temp script, run it
               if ($MANY) {
                  $tmp = "$tmpdir/$func#$i.php";
               }
               $script = ltrim($script);
               file_put_contents(
                  $tmp,
                  "<?php\n\n".
                  "#-- a test script for emulated function '$func'\n".
#                  "if (function_exists('$func')) { echo \"ATTENTION: the native '$func' function will engage for this test\\n\"; }\n".
                  "include('$test_up');\n".
#                  "#include('$emu2');\n".
#                  "#include('$emu3');\n".
                  "error_reporting(E_ALL);\n\n".
                  "#-- example[$i] as taken from PHP manual page '$fn'\n".
                  "$script\n".
                  "\n?".">"
               );
               echo "==>\033[32m\n";
               passthru("$php $tmp");
               echo "\033[0;37m\n\n";
            }

151
152
153
154
155
156
157
158
159






























































































160
   #-- clean up
   if ($CLEAN) {
      unlink($tmp);
   }
}

#-- even more clean
if ($MANY && CLEAN) { @rmdir($tmpdir); }































































































?>







|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
   #-- clean up
   if ($CLEAN) {
      unlink($tmp);
   }
}

#-- even more clean
if ($MANY && $CLEAN) { @rmdir($tmpdir); }





   function emulated_functions($upgrade_php = "upgrade.php") {
      preg_match_all("/function[ ]+(?:up_)?([_\w\d]+)\s*\(/", file_get_contents($upgrade_php), $uu);
      return $uu[1];
   }


function php_interpreter() {
   $php = $_SERVER["argv"][1];
   $php = trim(`which $php`);
   if (!$php) {
      die(__FILE__.": Given PHP interpreter not in your %PATH!\n");
   }
   return $php;
}

function get_manual() {
   $dir = $_SERVER["argv"][2];
   if (!is_dir($dir) || !file_exists("$dir/function.print.html")) {
      die(__FILE__.": PHP manual does not live under '$dir'.\n");
   }
   return $dir;
}
function get_lynx() {
   ($html = `which w3m`) or ($html = `which lynx`);
   if (!($html = trim($html))) {
      die(__FILE__.": lynx or w3m required.\n");
   }
   return $html;
}
function tmp() {
   global $MANY;
   $tmp = "/tmp/upgrade.php.doctest.tmp";
   $tmpdir = $tmp;
   if ($MANY) { @unlink($tmpdir); @mkdir($tmpdir); }
   return $tmp;
}

   #-- generate list of really emulated functions
function simulated() {
   global $php;
   $simulated = `echo '<?php echo serialize(get_defined_functions()); ?>' | $php -q`;
   $simulated = unserialize($simulated);
   return $simulated["internal"];
   
}


function func2fn($func) {
   global $dir;
  $fn = "$dir/function.".strtr($func, "_", "-").".html";
  if (file_exists($fn)) { return $fn; }
}


function example_scripts($func) {
   global $html;
   $fn = func2fn($func);
   $text = `$html -dump $fn`;
   preg_match_all("/<\?php(.+?)\?".">/ms", $text, $uu);
   return($uu[1]);
}


function add_output(&$scr) {
               #-- fix output-less scripts: find last assigned-to variable name
               if (!strpos($scr, "echo") && !strpos($scr, "print")) {
                  if (preg_match('/^.+(\$[_\w\d\[\"\'\]]+)\s*=/s', $scr, $uu)) {
                     $scr .= "\n\n#-- auto-added\nprint_r($uu[1]);\n";
                  }
                  else {
                     $scr .= "\n\n#-- this script gives no useful output, or does it?";
                  }
               }
               #-- fix compatibility to older PHP versions
               $scr = preg_replace('/(\s)private(\s\$)/', '$1var$2', $scr);
}
function up_version(&$scr, $func) {
   $scr = preg_replace("/$func/", "up_$func", $scr);
}

function print_script($scr) {
   global $i, $PAUSE;
               #-- output sample script text from doc
               if ($i) {
                  echo "\n++++++++++++++++++++++++++++\n\n";
                  sleep($PAUSE);
               }
               echo "\033[1;30m<?php $scr\n?".">\033[0;37m\n";
}

?>

Changes to doc/devtools/listemu.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/local/bin/php -qC
<?php
/*
   Prints the list of emulated functions.
*/

#-- basedir
$dir = realpath(dirname(__FILE__) . "/../");

#-- grep for function definitions
$text = "";
$text .= implode("", file("$dir/upgrade.php"));
$text .= implode("", file("$dir/ext/php40array.php"));
$text .= implode("", file("$dir/ext/bcmath.php"));

$text .= implode("", file("$dir/ext/gettext.php"));
$text .= implode("", file("$dir/ext/mime.php"));
$text .= implode("", file("$dir/ext/old.php"));
$text .= implode("", file("$dir/ext/posix.php"));
$text .= implode("", file("$dir/ext/ctype.php"));
$text .= implode("", file("$dir/ext/unfinished/odbc.php"));
if (preg_match_all("/function[ ]+([_\w\d]+)\s*\(/", $text, $uu)) {
   $list = array_unique($uu[1]);
}

#-- print
echo "Following functions can be emulated currently:\n";
foreach ($list as $func) {
   echo "· $func\n";
}

?>
|






|

|
<
|
|
|
>
|
|
<
<
<
<











1
2
3
4
5
6
7
8
9
10

11
12
13
14
15
16




17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/php -q
<?php
/*
   Prints the list of emulated functions.
*/

#-- basedir
$dir = dirname(dirname(dirname(__FILE__)));

#-- read files

$text = file_get_contents("$dir/upgrade.php");
foreach (glob("$dir/ext/*.php") as $add) {
   $text .= file_get_contents($add);
}

#-- grep




if (preg_match_all("/function[ ]+([_\w\d]+)\s*\(/", $text, $uu)) {
   $list = array_unique($uu[1]);
}

#-- print
echo "Following functions can be emulated currently:\n";
foreach ($list as $func) {
   echo "· $func\n";
}

?>

Changes to doc/runtest.

1
2
3
4
5
6
7
8
9


10
11
12
13
14
15
16
#!/usr/bin/php -qC
<?php
#
# doesn't yet run a choosen PHP interpreter,
# and auto_prepend_file doesn't seem to work either
#


#-- Use the emulation, Luke.


$T = new PEAR_RunTest();
$T->ini_overwrites[] = "auto_prepend_file=".dirname(dirname(__FILE__))."/upgrade.php";
// $T->_php = "php412";


#-- *.phpt file list
$files = $_SERVER["argv"];









>
>







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/php -qC
<?php
#
# doesn't yet run a choosen PHP interpreter,
# and auto_prepend_file doesn't seem to work either
#


#-- Use the emulation, Luke.
define("DETAILED", 0);
error_reporting(E_ERROR|E_WARNING);
$T = new PEAR_RunTest();
$T->ini_overwrites[] = "auto_prepend_file=".dirname(dirname(__FILE__))."/upgrade.php";
// $T->_php = "php412";


#-- *.phpt file list
$files = $_SERVER["argv"];

Changes to upgrade.php.

241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256


/**
 * @stub  Cannot be emulated correctly, but let's try.
 *
 */
if (!function_exists("header_remove")) {
   function header_remove($name) {
      if ($name = preg_replace("/[^-_.\w\d]+/", "", $name)) header("$name: \t");
      // Apache1.3? removed duplettes, empty header overrides previous.
      // ONLY if case was identical to previous header() call. (Very uncertain for applications which need to resort to such code smell.)
   }
}










|
|







241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256


/**
 * @stub  Cannot be emulated correctly, but let's try.
 *
 */
if (!function_exists("header_remove")) {
   function header_remove($name="") {
      if (strlen($name) and ($name = preg_replace("/[^-_.\w\d]+/", "", $name))) header("$name: \t");
      // Apache1.3? removed duplettes, empty header overrides previous.
      // ONLY if case was identical to previous header() call. (Very uncertain for applications which need to resort to such code smell.)
   }
}



792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
 * Should return associative array with last error message.
 *
 */
if (!function_exists("error_get_last")) {
   function error_get_last() {
      return array(
         "type" => 0,
         "message" => $GLOBALS[php_errormsg],
         "file" => "unknonw",
         "line" => 0,
      );
   }
}









|







792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
 * Should return associative array with last error message.
 *
 */
if (!function_exists("error_get_last")) {
   function error_get_last() {
      return array(
         "type" => 0,
         "message" => $GLOBALS["php_errormsg"],
         "file" => "unknonw",
         "line" => 0,
      );
   }
}


1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
      $delay = $t - time();
      if ($delay < 0) {
         trigger_error("time_sleep_until: timestamp in the past", E_USER_WARNING);
         return false;
      }
      else {
         sleep((int)$delay);
         usleep(($delay - floor($delay)) * 1000000);
         return true;
      }
   }
}










|







1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
      $delay = $t - time();
      if ($delay < 0) {
         trigger_error("time_sleep_until: timestamp in the past", E_USER_WARNING);
         return false;
      }
      else {
         sleep((int)$delay);
         #usleep(($delay - floor($delay)) * 1000000);
         return true;
      }
   }
}