Coverage Report

Created: 2022-10-14 11:20

/src/php-src/main/main.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | http://www.php.net/license/3_01.txt                                  |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Authors: Andi Gutmans <andi@php.net>                                 |
14
   |          Rasmus Lerdorf <rasmus@lerdorf.on.ca>                       |
15
   |          Zeev Suraski <zeev@php.net>                                 |
16
   +----------------------------------------------------------------------+
17
*/
18
19
/* {{{ includes
20
 */
21
22
#define ZEND_INCLUDE_FULL_WINDOWS_HEADERS
23
24
#include "php.h"
25
#include <stdio.h>
26
#include <fcntl.h>
27
#ifdef PHP_WIN32
28
#include "win32/time.h"
29
#include "win32/signal.h"
30
#include "win32/php_win32_globals.h"
31
#include "win32/winutil.h"
32
#include <process.h>
33
#endif
34
#if HAVE_SYS_TIME_H
35
#include <sys/time.h>
36
#endif
37
#if HAVE_UNISTD_H
38
#include <unistd.h>
39
#endif
40
41
#include <signal.h>
42
#include <locale.h>
43
#include "zend.h"
44
#include "zend_types.h"
45
#include "zend_extensions.h"
46
#include "php_ini.h"
47
#include "php_globals.h"
48
#include "php_main.h"
49
#include "php_syslog.h"
50
#include "fopen_wrappers.h"
51
#include "ext/standard/php_standard.h"
52
#include "ext/standard/php_string.h"
53
#include "ext/date/php_date.h"
54
#include "php_variables.h"
55
#include "ext/standard/credits.h"
56
#ifdef PHP_WIN32
57
#include <io.h>
58
#include "win32/php_registry.h"
59
#include "ext/standard/flock_compat.h"
60
#endif
61
#include "php_syslog.h"
62
#include "Zend/zend_exceptions.h"
63
64
#if PHP_SIGCHILD
65
#include <sys/types.h>
66
#include <sys/wait.h>
67
#endif
68
69
#include "zend_compile.h"
70
#include "zend_execute.h"
71
#include "zend_highlight.h"
72
#include "zend_extensions.h"
73
#include "zend_ini.h"
74
#include "zend_dtrace.h"
75
76
#include "php_content_types.h"
77
#include "php_ticks.h"
78
#include "php_streams.h"
79
#include "php_open_temporary_file.h"
80
81
#include "SAPI.h"
82
#include "rfc1867.h"
83
84
#include "ext/standard/html_tables.h"
85
/* }}} */
86
87
PHPAPI int (*php_register_internal_extensions_func)(void) = php_register_internal_extensions;
88
89
#ifndef ZTS
90
php_core_globals core_globals;
91
#else
92
PHPAPI int core_globals_id;
93
PHPAPI size_t core_globals_offset;
94
#endif
95
96
0
#define SAFE_FILENAME(f) ((f)?(f):"-")
97
98
/* {{{ PHP_INI_MH
99
 */
100
static PHP_INI_MH(OnSetFacility)
101
3.54k
{
102
3.54k
  const char *facility = ZSTR_VAL(new_value);
103
104
3.54k
#ifdef LOG_AUTH
105
3.54k
  if (!strcmp(facility, "LOG_AUTH") || !strcmp(facility, "auth") || !strcmp(facility, "security")) {
106
0
    PG(syslog_facility) = LOG_AUTH;
107
0
    return SUCCESS;
108
0
  }
109
3.54k
#endif
110
3.54k
#ifdef LOG_AUTHPRIV
111
3.54k
  if (!strcmp(facility, "LOG_AUTHPRIV") || !strcmp(facility, "authpriv")) {
112
0
    PG(syslog_facility) = LOG_AUTHPRIV;
113
0
    return SUCCESS;
114
0
  }
115
3.54k
#endif
116
3.54k
#ifdef LOG_CRON
117
3.54k
  if (!strcmp(facility, "LOG_CRON") || !strcmp(facility, "cron")) {
118
0
    PG(syslog_facility) = LOG_CRON;
119
0
    return SUCCESS;
120
0
  }
121
3.54k
#endif
122
3.54k
#ifdef LOG_DAEMON
123
3.54k
  if (!strcmp(facility, "LOG_DAEMON") || !strcmp(facility, "daemon")) {
124
0
    PG(syslog_facility) = LOG_DAEMON;
125
0
    return SUCCESS;
126
0
  }
127
3.54k
#endif
128
3.54k
#ifdef LOG_FTP
129
3.54k
  if (!strcmp(facility, "LOG_FTP") || !strcmp(facility, "ftp")) {
130
0
    PG(syslog_facility) = LOG_FTP;
131
0
    return SUCCESS;
132
0
  }
133
3.54k
#endif
134
3.54k
#ifdef LOG_KERN
135
3.54k
  if (!strcmp(facility, "LOG_KERN") || !strcmp(facility, "kern")) {
136
0
    PG(syslog_facility) = LOG_KERN;
137
0
    return SUCCESS;
138
0
  }
139
3.54k
#endif
140
3.54k
#ifdef LOG_LPR
141
3.54k
  if (!strcmp(facility, "LOG_LPR") || !strcmp(facility, "lpr")) {
142
0
    PG(syslog_facility) = LOG_LPR;
143
0
    return SUCCESS;
144
0
  }
145
3.54k
#endif
146
3.54k
#ifdef LOG_MAIL
147
3.54k
  if (!strcmp(facility, "LOG_MAIL") || !strcmp(facility, "mail")) {
148
0
    PG(syslog_facility) = LOG_MAIL;
149
0
    return SUCCESS;
150
0
  }
151
3.54k
#endif
152
#ifdef LOG_INTERNAL_MARK
153
  if (!strcmp(facility, "LOG_INTERNAL_MARK") || !strcmp(facility, "mark")) {
154
    PG(syslog_facility) = LOG_INTERNAL_MARK;
155
    return SUCCESS;
156
  }
157
#endif
158
3.54k
#ifdef LOG_NEWS
159
3.54k
  if (!strcmp(facility, "LOG_NEWS") || !strcmp(facility, "news")) {
160
0
    PG(syslog_facility) = LOG_NEWS;
161
0
    return SUCCESS;
162
0
  }
163
3.54k
#endif
164
3.54k
#ifdef LOG_SYSLOG
165
3.54k
  if (!strcmp(facility, "LOG_SYSLOG") || !strcmp(facility, "syslog")) {
166
0
    PG(syslog_facility) = LOG_SYSLOG;
167
0
    return SUCCESS;
168
0
  }
169
3.54k
#endif
170
3.54k
#ifdef LOG_USER
171
3.54k
  if (!strcmp(facility, "LOG_USER") || !strcmp(facility, "user")) {
172
3.54k
    PG(syslog_facility) = LOG_USER;
173
3.54k
    return SUCCESS;
174
3.54k
  }
175
0
#endif
176
0
#ifdef LOG_UUCP
177
0
  if (!strcmp(facility, "LOG_UUCP") || !strcmp(facility, "uucp")) {
178
0
    PG(syslog_facility) = LOG_UUCP;
179
0
    return SUCCESS;
180
0
  }
181
0
#endif
182
0
#ifdef LOG_LOCAL0
183
0
  if (!strcmp(facility, "LOG_LOCAL0") || !strcmp(facility, "local0")) {
184
0
    PG(syslog_facility) = LOG_LOCAL0;
185
0
    return SUCCESS;
186
0
  }
187
0
#endif
188
0
#ifdef LOG_LOCAL1
189
0
  if (!strcmp(facility, "LOG_LOCAL1") || !strcmp(facility, "local1")) {
190
0
    PG(syslog_facility) = LOG_LOCAL1;
191
0
    return SUCCESS;
192
0
  }
193
0
#endif
194
0
#ifdef LOG_LOCAL2
195
0
  if (!strcmp(facility, "LOG_LOCAL2") || !strcmp(facility, "local2")) {
196
0
    PG(syslog_facility) = LOG_LOCAL2;
197
0
    return SUCCESS;
198
0
  }
199
0
#endif
200
0
#ifdef LOG_LOCAL3
201
0
  if (!strcmp(facility, "LOG_LOCAL3") || !strcmp(facility, "local3")) {
202
0
    PG(syslog_facility) = LOG_LOCAL3;
203
0
    return SUCCESS;
204
0
  }
205
0
#endif
206
0
#ifdef LOG_LOCAL4
207
0
  if (!strcmp(facility, "LOG_LOCAL4") || !strcmp(facility, "local4")) {
208
0
    PG(syslog_facility) = LOG_LOCAL4;
209
0
    return SUCCESS;
210
0
  }
211
0
#endif
212
0
#ifdef LOG_LOCAL5
213
0
  if (!strcmp(facility, "LOG_LOCAL5") || !strcmp(facility, "local5")) {
214
0
    PG(syslog_facility) = LOG_LOCAL5;
215
0
    return SUCCESS;
216
0
  }
217
0
#endif
218
0
#ifdef LOG_LOCAL6
219
0
  if (!strcmp(facility, "LOG_LOCAL6") || !strcmp(facility, "local6")) {
220
0
    PG(syslog_facility) = LOG_LOCAL6;
221
0
    return SUCCESS;
222
0
  }
223
0
#endif
224
0
#ifdef LOG_LOCAL7
225
0
  if (!strcmp(facility, "LOG_LOCAL7") || !strcmp(facility, "local7")) {
226
0
    PG(syslog_facility) = LOG_LOCAL7;
227
0
    return SUCCESS;
228
0
  }
229
0
#endif
230
231
0
  return FAILURE;
232
0
}
233
/* }}} */
234
235
/* {{{ PHP_INI_MH
236
 */
237
static PHP_INI_MH(OnSetPrecision)
238
3.54k
{
239
3.54k
  zend_long i;
240
241
3.54k
  ZEND_ATOL(i, ZSTR_VAL(new_value));
242
3.54k
  if (i >= -1) {
243
3.54k
    EG(precision) = i;
244
3.54k
    return SUCCESS;
245
0
  } else {
246
0
    return FAILURE;
247
0
  }
248
3.54k
}
249
/* }}} */
250
251
/* {{{ PHP_INI_MH
252
 */
253
static PHP_INI_MH(OnSetSerializePrecision)
254
3.54k
{
255
3.54k
  zend_long i;
256
257
3.54k
  ZEND_ATOL(i, ZSTR_VAL(new_value));
258
3.54k
  if (i >= -1) {
259
3.54k
    PG(serialize_precision) = i;
260
3.54k
    return SUCCESS;
261
0
  } else {
262
0
    return FAILURE;
263
0
  }
264
3.54k
}
265
/* }}} */
266
267
268
/* {{{ PHP_INI_MH
269
 */
270
static PHP_INI_MH(OnChangeMemoryLimit)
271
3.54k
{
272
3.54k
  if (new_value) {
273
3.54k
    PG(memory_limit) = zend_atol(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
274
0
  } else {
275
0
    PG(memory_limit) = Z_L(1)<<30;    /* effectively, no limit */
276
0
  }
277
3.54k
  return zend_set_memory_limit(PG(memory_limit));
278
3.54k
}
279
/* }}} */
280
281
/* {{{ PHP_INI_MH
282
 */
283
static PHP_INI_MH(OnSetLogFilter)
284
3.54k
{
285
3.54k
  const char *filter = ZSTR_VAL(new_value);
286
287
3.54k
  if (!strcmp(filter, "all")) {
288
0
    PG(syslog_filter) = PHP_SYSLOG_FILTER_ALL;
289
0
    return SUCCESS;
290
0
  }
291
3.54k
  if (!strcmp(filter, "no-ctrl")) {
292
3.54k
    PG(syslog_filter) = PHP_SYSLOG_FILTER_NO_CTRL;
293
3.54k
    return SUCCESS;
294
3.54k
  }
295
0
  if (!strcmp(filter, "ascii")) {
296
0
    PG(syslog_filter) = PHP_SYSLOG_FILTER_ASCII;
297
0
    return SUCCESS;
298
0
  }
299
0
  if (!strcmp(filter, "raw")) {
300
0
    PG(syslog_filter) = PHP_SYSLOG_FILTER_RAW;
301
0
    return SUCCESS;
302
0
  }
303
304
0
  return FAILURE;
305
0
}
306
/* }}} */
307
308
/* {{{ php_disable_functions
309
 */
310
static void php_disable_functions(void)
311
3.54k
{
312
3.54k
  char *s = NULL, *e;
313
314
3.54k
  if (!*(INI_STR("disable_functions"))) {
315
3.54k
    return;
316
3.54k
  }
317
318
0
  e = PG(disable_functions) = strdup(INI_STR("disable_functions"));
319
0
  if (e == NULL) {
320
0
    return;
321
0
  }
322
0
  while (*e) {
323
0
    switch (*e) {
324
0
      case ' ':
325
0
      case ',':
326
0
        if (s) {
327
0
          *e = '\0';
328
0
          zend_disable_function(s, e-s);
329
0
          s = NULL;
330
0
        }
331
0
        break;
332
0
      default:
333
0
        if (!s) {
334
0
          s = e;
335
0
        }
336
0
        break;
337
0
    }
338
0
    e++;
339
0
  }
340
0
  if (s) {
341
0
    zend_disable_function(s, e-s);
342
0
  }
343
0
}
344
/* }}} */
345
346
/* {{{ php_disable_classes
347
 */
348
static void php_disable_classes(void)
349
3.54k
{
350
3.54k
  char *s = NULL, *e;
351
352
3.54k
  if (!*(INI_STR("disable_classes"))) {
353
3.54k
    return;
354
3.54k
  }
355
356
0
  e = PG(disable_classes) = strdup(INI_STR("disable_classes"));
357
358
0
  while (*e) {
359
0
    switch (*e) {
360
0
      case ' ':
361
0
      case ',':
362
0
        if (s) {
363
0
          *e = '\0';
364
0
          zend_disable_class(s, e-s);
365
0
          s = NULL;
366
0
        }
367
0
        break;
368
0
      default:
369
0
        if (!s) {
370
0
          s = e;
371
0
        }
372
0
        break;
373
0
    }
374
0
    e++;
375
0
  }
376
0
  if (s) {
377
0
    zend_disable_class(s, e-s);
378
0
  }
379
0
}
380
/* }}} */
381
382
/* {{{ php_binary_init
383
 */
384
static void php_binary_init(void)
385
3.54k
{
386
3.54k
  char *binary_location = NULL;
387
#ifdef PHP_WIN32
388
  binary_location = (char *)malloc(MAXPATHLEN);
389
  if (binary_location && GetModuleFileName(0, binary_location, MAXPATHLEN) == 0) {
390
    free(binary_location);
391
    PG(php_binary) = NULL;
392
  }
393
#else
394
3.54k
  if (sapi_module.executable_location) {
395
0
    binary_location = (char *)malloc(MAXPATHLEN);
396
0
    if (binary_location && !strchr(sapi_module.executable_location, '/')) {
397
0
      char *envpath, *path;
398
0
      int found = 0;
399
400
0
      if ((envpath = getenv("PATH")) != NULL) {
401
0
        char *search_dir, search_path[MAXPATHLEN];
402
0
        char *last = NULL;
403
0
        zend_stat_t s;
404
405
0
        path = estrdup(envpath);
406
0
        search_dir = php_strtok_r(path, ":", &last);
407
408
0
        while (search_dir) {
409
0
          snprintf(search_path, MAXPATHLEN, "%s/%s", search_dir, sapi_module.executable_location);
410
0
          if (VCWD_REALPATH(search_path, binary_location) && !VCWD_ACCESS(binary_location, X_OK) && VCWD_STAT(binary_location, &s) == 0 && S_ISREG(s.st_mode)) {
411
0
            found = 1;
412
0
            break;
413
0
          }
414
0
          search_dir = php_strtok_r(NULL, ":", &last);
415
0
        }
416
0
        efree(path);
417
0
      }
418
0
      if (!found) {
419
0
        free(binary_location);
420
0
        binary_location = NULL;
421
0
      }
422
0
    } else if (!VCWD_REALPATH(sapi_module.executable_location, binary_location) || VCWD_ACCESS(binary_location, X_OK)) {
423
0
      free(binary_location);
424
0
      binary_location = NULL;
425
0
    }
426
0
  }
427
3.54k
#endif
428
3.54k
  PG(php_binary) = binary_location;
429
3.54k
}
430
/* }}} */
431
432
/* {{{ PHP_INI_MH
433
 */
434
static PHP_INI_MH(OnUpdateTimeout)
435
3.54k
{
436
3.54k
  if (stage==PHP_INI_STAGE_STARTUP) {
437
    /* Don't set a timeout on startup, only per-request */
438
3.54k
    ZEND_ATOL(EG(timeout_seconds), ZSTR_VAL(new_value));
439
3.54k
    return SUCCESS;
440
3.54k
  }
441
0
  zend_unset_timeout();
442
0
  ZEND_ATOL(EG(timeout_seconds), ZSTR_VAL(new_value));
443
0
  zend_set_timeout(EG(timeout_seconds), 0);
444
0
  return SUCCESS;
445
0
}
446
/* }}} */
447
448
/* {{{ php_get_display_errors_mode() helper function
449
 */
450
static zend_uchar php_get_display_errors_mode(char *value, size_t value_length)
451
3.54k
{
452
3.54k
  zend_uchar mode;
453
454
3.54k
  if (!value) {
455
0
    return PHP_DISPLAY_ERRORS_STDOUT;
456
0
  }
457
458
3.54k
  if (value_length == 2 && !strcasecmp("on", value)) {
459
0
    mode = PHP_DISPLAY_ERRORS_STDOUT;
460
3.54k
  } else if (value_length == 3 && !strcasecmp("yes", value)) {
461
0
    mode = PHP_DISPLAY_ERRORS_STDOUT;
462
3.54k
  } else if (value_length == 4 && !strcasecmp("true", value)) {
463
0
    mode = PHP_DISPLAY_ERRORS_STDOUT;
464
3.54k
  } else if (value_length == 6 && !strcasecmp(value, "stderr")) {
465
0
    mode = PHP_DISPLAY_ERRORS_STDERR;
466
3.54k
  } else if (value_length == 6 && !strcasecmp(value, "stdout")) {
467
0
    mode = PHP_DISPLAY_ERRORS_STDOUT;
468
3.54k
  } else {
469
3.54k
    ZEND_ATOL(mode, value);
470
3.54k
    if (mode && mode != PHP_DISPLAY_ERRORS_STDOUT && mode != PHP_DISPLAY_ERRORS_STDERR) {
471
0
      mode = PHP_DISPLAY_ERRORS_STDOUT;
472
0
    }
473
3.54k
  }
474
475
3.54k
  return mode;
476
3.54k
}
477
/* }}} */
478
479
/* {{{ PHP_INI_MH
480
 */
481
static PHP_INI_MH(OnUpdateDisplayErrors)
482
3.54k
{
483
3.54k
  PG(display_errors) = php_get_display_errors_mode(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
484
485
3.54k
  return SUCCESS;
486
3.54k
}
487
/* }}} */
488
489
/* {{{ PHP_INI_DISP
490
 */
491
static PHP_INI_DISP(display_errors_mode)
492
{
493
  zend_uchar mode;
494
  bool cgi_or_cli;
495
  size_t tmp_value_length;
496
  char *tmp_value;
497
498
  if (type == ZEND_INI_DISPLAY_ORIG && ini_entry->modified) {
499
    tmp_value = (ini_entry->orig_value ? ZSTR_VAL(ini_entry->orig_value) : NULL );
500
    tmp_value_length = (ini_entry->orig_value? ZSTR_LEN(ini_entry->orig_value) : 0);
501
  } else if (ini_entry->value) {
502
    tmp_value = ZSTR_VAL(ini_entry->value);
503
    tmp_value_length = ZSTR_LEN(ini_entry->value);
504
  } else {
505
    tmp_value = NULL;
506
    tmp_value_length = 0;
507
  }
508
509
  mode = php_get_display_errors_mode(tmp_value, tmp_value_length);
510
511
  /* Display 'On' for other SAPIs instead of STDOUT or STDERR */
512
  cgi_or_cli = (!strcmp(sapi_module.name, "cli") || !strcmp(sapi_module.name, "cgi") || !strcmp(sapi_module.name, "phpdbg"));
513
514
  switch (mode) {
515
    case PHP_DISPLAY_ERRORS_STDERR:
516
      if (cgi_or_cli ) {
517
        PUTS("STDERR");
518
      } else {
519
        PUTS("On");
520
      }
521
      break;
522
523
    case PHP_DISPLAY_ERRORS_STDOUT:
524
      if (cgi_or_cli ) {
525
        PUTS("STDOUT");
526
      } else {
527
        PUTS("On");
528
      }
529
      break;
530
531
    default:
532
      PUTS("Off");
533
      break;
534
  }
535
}
536
/* }}} */
537
538
7.08k
PHPAPI const char *php_get_internal_encoding() {
539
7.08k
  if (PG(internal_encoding) && PG(internal_encoding)[0]) {
540
0
    return PG(internal_encoding);
541
7.08k
  } else if (SG(default_charset) && SG(default_charset)[0]) {
542
7.08k
    return SG(default_charset);
543
7.08k
  }
544
0
  return "UTF-8";
545
0
}
546
547
7.08k
PHPAPI const char *php_get_input_encoding() {
548
7.08k
  if (PG(input_encoding) && PG(input_encoding)[0]) {
549
0
    return PG(input_encoding);
550
7.08k
  } else if (SG(default_charset) && SG(default_charset)[0]) {
551
7.08k
    return SG(default_charset);
552
7.08k
  }
553
0
  return "UTF-8";
554
0
}
555
556
7.08k
PHPAPI const char *php_get_output_encoding() {
557
7.08k
  if (PG(output_encoding) && PG(output_encoding)[0]) {
558
0
    return PG(output_encoding);
559
7.08k
  } else if (SG(default_charset) && SG(default_charset)[0]) {
560
7.08k
    return SG(default_charset);
561
7.08k
  }
562
0
  return "UTF-8";
563
0
}
564
565
PHPAPI void (*php_internal_encoding_changed)(void) = NULL;
566
567
/* {{{ PHP_INI_MH
568
 */
569
static PHP_INI_MH(OnUpdateDefaultCharset)
570
3.54k
{
571
3.54k
  OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
572
3.54k
  if (php_internal_encoding_changed) {
573
0
    php_internal_encoding_changed();
574
0
  }
575
3.54k
  if (new_value) {
576
#ifdef PHP_WIN32
577
    php_win32_cp_do_update(ZSTR_VAL(new_value));
578
#endif
579
3.54k
  }
580
3.54k
  return SUCCESS;
581
3.54k
}
582
/* }}} */
583
584
/* {{{ PHP_INI_MH
585
 */
586
static PHP_INI_MH(OnUpdateInternalEncoding)
587
3.54k
{
588
3.54k
  OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
589
3.54k
  if (php_internal_encoding_changed) {
590
0
    php_internal_encoding_changed();
591
0
  }
592
3.54k
  if (new_value) {
593
#ifdef PHP_WIN32
594
    php_win32_cp_do_update(ZSTR_VAL(new_value));
595
#endif
596
0
  }
597
3.54k
  return SUCCESS;
598
3.54k
}
599
/* }}} */
600
601
/* {{{ PHP_INI_MH
602
 */
603
static PHP_INI_MH(OnUpdateInputEncoding)
604
3.54k
{
605
3.54k
  OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
606
3.54k
  if (php_internal_encoding_changed) {
607
0
    php_internal_encoding_changed();
608
0
  }
609
3.54k
  if (new_value) {
610
#ifdef PHP_WIN32
611
    php_win32_cp_do_update(NULL);
612
#endif
613
0
  }
614
3.54k
  return SUCCESS;
615
3.54k
}
616
/* }}} */
617
618
/* {{{ PHP_INI_MH
619
 */
620
static PHP_INI_MH(OnUpdateOutputEncoding)
621
3.54k
{
622
3.54k
  OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
623
3.54k
  if (php_internal_encoding_changed) {
624
0
    php_internal_encoding_changed();
625
0
  }
626
3.54k
  if (new_value) {
627
#ifdef PHP_WIN32
628
    php_win32_cp_do_update(NULL);
629
#endif
630
0
  }
631
3.54k
  return SUCCESS;
632
3.54k
}
633
/* }}} */
634
635
/* {{{ PHP_INI_MH
636
 */
637
static PHP_INI_MH(OnUpdateErrorLog)
638
3.54k
{
639
  /* Only do the safemode/open_basedir check at runtime */
640
3.54k
  if ((stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) && new_value && strcmp(ZSTR_VAL(new_value), "syslog")) {
641
0
    if (PG(open_basedir) && php_check_open_basedir(ZSTR_VAL(new_value))) {
642
0
      return FAILURE;
643
0
    }
644
3.54k
  }
645
3.54k
  OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
646
3.54k
  return SUCCESS;
647
3.54k
}
648
/* }}} */
649
650
/* {{{ PHP_INI_MH
651
 */
652
static PHP_INI_MH(OnUpdateMailLog)
653
3.54k
{
654
  /* Only do the safemode/open_basedir check at runtime */
655
3.54k
  if ((stage == PHP_INI_STAGE_RUNTIME || stage == PHP_INI_STAGE_HTACCESS) && new_value) {
656
0
    if (PG(open_basedir) && php_check_open_basedir(ZSTR_VAL(new_value))) {
657
0
      return FAILURE;
658
0
    }
659
3.54k
  }
660
3.54k
  OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
661
3.54k
  return SUCCESS;
662
3.54k
}
663
/* }}} */
664
665
/* {{{ PHP_INI_MH
666
 */
667
static PHP_INI_MH(OnChangeMailForceExtra)
668
3.54k
{
669
  /* Don't allow changing it in htaccess */
670
3.54k
  if (stage == PHP_INI_STAGE_HTACCESS) {
671
0
      return FAILURE;
672
0
  }
673
3.54k
  return SUCCESS;
674
3.54k
}
675
/* }}} */
676
677
/* defined in browscap.c */
678
PHP_INI_MH(OnChangeBrowscap);
679
680
681
/* Need to be read from the environment (?):
682
 * PHP_AUTO_PREPEND_FILE
683
 * PHP_AUTO_APPEND_FILE
684
 * PHP_DOCUMENT_ROOT
685
 * PHP_USER_DIR
686
 * PHP_INCLUDE_PATH
687
 */
688
689
 /* Windows use the internal mail */
690
#if defined(PHP_WIN32)
691
# define DEFAULT_SENDMAIL_PATH NULL
692
#else
693
# define DEFAULT_SENDMAIL_PATH PHP_PROG_SENDMAIL " -t -i"
694
#endif
695
696
/* {{{ PHP_INI
697
 */
698
PHP_INI_BEGIN()
699
  PHP_INI_ENTRY_EX("highlight.comment",   HL_COMMENT_COLOR, PHP_INI_ALL,  NULL,     php_ini_color_displayer_cb)
700
  PHP_INI_ENTRY_EX("highlight.default",   HL_DEFAULT_COLOR, PHP_INI_ALL,  NULL,     php_ini_color_displayer_cb)
701
  PHP_INI_ENTRY_EX("highlight.html",      HL_HTML_COLOR,    PHP_INI_ALL,  NULL,     php_ini_color_displayer_cb)
702
  PHP_INI_ENTRY_EX("highlight.keyword",   HL_KEYWORD_COLOR, PHP_INI_ALL,  NULL,     php_ini_color_displayer_cb)
703
  PHP_INI_ENTRY_EX("highlight.string",    HL_STRING_COLOR,  PHP_INI_ALL,  NULL,     php_ini_color_displayer_cb)
704
705
  STD_PHP_INI_ENTRY_EX("display_errors",    "1",    PHP_INI_ALL,    OnUpdateDisplayErrors,  display_errors,     php_core_globals, core_globals, display_errors_mode)
706
  STD_PHP_INI_BOOLEAN("display_startup_errors", "1",  PHP_INI_ALL,    OnUpdateBool,     display_startup_errors, php_core_globals, core_globals)
707
  STD_PHP_INI_BOOLEAN("enable_dl",      "1",    PHP_INI_SYSTEM,   OnUpdateBool,     enable_dl,        php_core_globals, core_globals)
708
  STD_PHP_INI_BOOLEAN("expose_php",     "1",    PHP_INI_SYSTEM,   OnUpdateBool,     expose_php,       php_core_globals, core_globals)
709
  STD_PHP_INI_ENTRY("docref_root",      "",     PHP_INI_ALL,    OnUpdateString,     docref_root,      php_core_globals, core_globals)
710
  STD_PHP_INI_ENTRY("docref_ext",       "",     PHP_INI_ALL,    OnUpdateString,     docref_ext,       php_core_globals, core_globals)
711
  STD_PHP_INI_BOOLEAN("html_errors",      "1",    PHP_INI_ALL,    OnUpdateBool,     html_errors,      php_core_globals, core_globals)
712
  STD_PHP_INI_BOOLEAN("xmlrpc_errors",    "0",    PHP_INI_SYSTEM,   OnUpdateBool,     xmlrpc_errors,      php_core_globals, core_globals)
713
  STD_PHP_INI_ENTRY("xmlrpc_error_number",  "0",    PHP_INI_ALL,    OnUpdateLong,     xmlrpc_error_number,  php_core_globals, core_globals)
714
  STD_PHP_INI_ENTRY("max_input_time",     "-1", PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateLong,     max_input_time, php_core_globals, core_globals)
715
  STD_PHP_INI_BOOLEAN("ignore_user_abort",  "0",    PHP_INI_ALL,    OnUpdateBool,     ignore_user_abort,    php_core_globals, core_globals)
716
  STD_PHP_INI_BOOLEAN("implicit_flush",   "0",    PHP_INI_ALL,    OnUpdateBool,     implicit_flush,     php_core_globals, core_globals)
717
  STD_PHP_INI_BOOLEAN("log_errors",     "0",    PHP_INI_ALL,    OnUpdateBool,     log_errors,       php_core_globals, core_globals)
718
  STD_PHP_INI_ENTRY("log_errors_max_len",  "1024",    PHP_INI_ALL,    OnUpdateLong,     log_errors_max_len,   php_core_globals, core_globals)
719
  STD_PHP_INI_BOOLEAN("ignore_repeated_errors", "0",  PHP_INI_ALL,    OnUpdateBool,     ignore_repeated_errors, php_core_globals, core_globals)
720
  STD_PHP_INI_BOOLEAN("ignore_repeated_source", "0",  PHP_INI_ALL,    OnUpdateBool,     ignore_repeated_source, php_core_globals, core_globals)
721
  STD_PHP_INI_BOOLEAN("report_memleaks",    "1",    PHP_INI_ALL,    OnUpdateBool,     report_memleaks,    php_core_globals, core_globals)
722
  STD_PHP_INI_BOOLEAN("report_zend_debug",  "1",    PHP_INI_ALL,    OnUpdateBool,     report_zend_debug,    php_core_globals, core_globals)
723
  STD_PHP_INI_ENTRY("output_buffering",   "0",    PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateLong, output_buffering,   php_core_globals, core_globals)
724
  STD_PHP_INI_ENTRY("output_handler",     NULL,   PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateString, output_handler,   php_core_globals, core_globals)
725
  STD_PHP_INI_BOOLEAN("register_argc_argv", "1",    PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateBool, register_argc_argv,   php_core_globals, core_globals)
726
  STD_PHP_INI_BOOLEAN("auto_globals_jit",   "1",    PHP_INI_PERDIR|PHP_INI_SYSTEM,  OnUpdateBool, auto_globals_jit, php_core_globals, core_globals)
727
  STD_PHP_INI_BOOLEAN("short_open_tag", DEFAULT_SHORT_OPEN_TAG, PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateBool,     short_tags,       zend_compiler_globals,  compiler_globals)
728
729
  STD_PHP_INI_ENTRY("unserialize_callback_func",  NULL, PHP_INI_ALL,    OnUpdateString,     unserialize_callback_func,  php_core_globals, core_globals)
730
  STD_PHP_INI_ENTRY("serialize_precision",  "-1", PHP_INI_ALL,    OnSetSerializePrecision,      serialize_precision,  php_core_globals, core_globals)
731
  STD_PHP_INI_ENTRY("arg_separator.output", "&",    PHP_INI_ALL,    OnUpdateStringUnempty,  arg_separator.output, php_core_globals, core_globals)
732
  STD_PHP_INI_ENTRY("arg_separator.input",  "&",    PHP_INI_SYSTEM|PHP_INI_PERDIR,  OnUpdateStringUnempty,  arg_separator.input,  php_core_globals, core_globals)
733
734
  STD_PHP_INI_ENTRY("auto_append_file",   NULL,   PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateString,     auto_append_file,   php_core_globals, core_globals)
735
  STD_PHP_INI_ENTRY("auto_prepend_file",    NULL,   PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateString,     auto_prepend_file,    php_core_globals, core_globals)
736
  STD_PHP_INI_ENTRY("doc_root",       NULL,   PHP_INI_SYSTEM,   OnUpdateStringUnempty,  doc_root,       php_core_globals, core_globals)
737
  STD_PHP_INI_ENTRY("default_charset",    PHP_DEFAULT_CHARSET,  PHP_INI_ALL,  OnUpdateDefaultCharset,     default_charset,    sapi_globals_struct, sapi_globals)
738
  STD_PHP_INI_ENTRY("default_mimetype",   SAPI_DEFAULT_MIMETYPE,  PHP_INI_ALL,  OnUpdateString,     default_mimetype,   sapi_globals_struct, sapi_globals)
739
  STD_PHP_INI_ENTRY("internal_encoding",    NULL,     PHP_INI_ALL,  OnUpdateInternalEncoding, internal_encoding,  php_core_globals, core_globals)
740
  STD_PHP_INI_ENTRY("input_encoding",     NULL,     PHP_INI_ALL,  OnUpdateInputEncoding,        input_encoding,   php_core_globals, core_globals)
741
  STD_PHP_INI_ENTRY("output_encoding",    NULL,     PHP_INI_ALL,  OnUpdateOutputEncoding,       output_encoding,  php_core_globals, core_globals)
742
  STD_PHP_INI_ENTRY("error_log",        NULL,   PHP_INI_ALL,    OnUpdateErrorLog,     error_log,        php_core_globals, core_globals)
743
  STD_PHP_INI_ENTRY("extension_dir",      PHP_EXTENSION_DIR,    PHP_INI_SYSTEM,   OnUpdateStringUnempty,  extension_dir,      php_core_globals, core_globals)
744
  STD_PHP_INI_ENTRY("sys_temp_dir",     NULL,   PHP_INI_SYSTEM,   OnUpdateStringUnempty,  sys_temp_dir,     php_core_globals, core_globals)
745
  STD_PHP_INI_ENTRY("include_path",     PHP_INCLUDE_PATH,   PHP_INI_ALL,    OnUpdateStringUnempty,  include_path,     php_core_globals, core_globals)
746
  PHP_INI_ENTRY("max_execution_time",     "30",   PHP_INI_ALL,      OnUpdateTimeout)
747
  STD_PHP_INI_ENTRY("open_basedir",     NULL,   PHP_INI_ALL,    OnUpdateBaseDir,      open_basedir,     php_core_globals, core_globals)
748
749
  STD_PHP_INI_BOOLEAN("file_uploads",     "1",    PHP_INI_SYSTEM,   OnUpdateBool,     file_uploads,     php_core_globals, core_globals)
750
  STD_PHP_INI_ENTRY("upload_max_filesize",  "2M",   PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateLong,     upload_max_filesize,  php_core_globals, core_globals)
751
  STD_PHP_INI_ENTRY("post_max_size",      "8M",   PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateLong,     post_max_size,      sapi_globals_struct,sapi_globals)
752
  STD_PHP_INI_ENTRY("upload_tmp_dir",     NULL,   PHP_INI_SYSTEM,   OnUpdateStringUnempty,  upload_tmp_dir,     php_core_globals, core_globals)
753
  STD_PHP_INI_ENTRY("max_input_nesting_level", "64",    PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateLongGEZero, max_input_nesting_level,      php_core_globals, core_globals)
754
  STD_PHP_INI_ENTRY("max_input_vars",     "1000",   PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateLongGEZero, max_input_vars,           php_core_globals, core_globals)
755
756
  STD_PHP_INI_ENTRY("user_dir",       NULL,   PHP_INI_SYSTEM,   OnUpdateString,     user_dir,       php_core_globals, core_globals)
757
  STD_PHP_INI_ENTRY("variables_order",    "EGPCS",  PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateStringUnempty,  variables_order,    php_core_globals, core_globals)
758
  STD_PHP_INI_ENTRY("request_order",      NULL,   PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateString, request_order,    php_core_globals, core_globals)
759
760
  STD_PHP_INI_ENTRY("error_append_string",  NULL,   PHP_INI_ALL,    OnUpdateString,     error_append_string,  php_core_globals, core_globals)
761
  STD_PHP_INI_ENTRY("error_prepend_string", NULL,   PHP_INI_ALL,    OnUpdateString,     error_prepend_string, php_core_globals, core_globals)
762
763
  PHP_INI_ENTRY("SMTP",           "localhost",PHP_INI_ALL,    NULL)
764
  PHP_INI_ENTRY("smtp_port",          "25",   PHP_INI_ALL,    NULL)
765
  STD_PHP_INI_BOOLEAN("mail.add_x_header",      "0",    PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateBool,     mail_x_header,      php_core_globals, core_globals)
766
  STD_PHP_INI_ENTRY("mail.log",         NULL,   PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnUpdateMailLog,      mail_log,     php_core_globals, core_globals)
767
  PHP_INI_ENTRY("browscap",         NULL,   PHP_INI_SYSTEM,   OnChangeBrowscap)
768
  PHP_INI_ENTRY("memory_limit",       "128M",   PHP_INI_ALL,    OnChangeMemoryLimit)
769
  PHP_INI_ENTRY("precision",          "14",   PHP_INI_ALL,    OnSetPrecision)
770
  PHP_INI_ENTRY("sendmail_from",        NULL,   PHP_INI_ALL,    NULL)
771
  PHP_INI_ENTRY("sendmail_path",  DEFAULT_SENDMAIL_PATH,  PHP_INI_SYSTEM,   NULL)
772
  PHP_INI_ENTRY("mail.force_extra_parameters",NULL,   PHP_INI_SYSTEM|PHP_INI_PERDIR,    OnChangeMailForceExtra)
773
  PHP_INI_ENTRY("disable_functions",      "",     PHP_INI_SYSTEM,   NULL)
774
  PHP_INI_ENTRY("disable_classes",      "",     PHP_INI_SYSTEM,   NULL)
775
  PHP_INI_ENTRY("max_file_uploads",     "20",     PHP_INI_SYSTEM|PHP_INI_PERDIR,    NULL)
776
777
  STD_PHP_INI_BOOLEAN("allow_url_fopen",    "1",    PHP_INI_SYSTEM,   OnUpdateBool,   allow_url_fopen,    php_core_globals,   core_globals)
778
  STD_PHP_INI_BOOLEAN("allow_url_include",  "0",    PHP_INI_SYSTEM,   OnUpdateBool,   allow_url_include,    php_core_globals,   core_globals)
779
  STD_PHP_INI_BOOLEAN("enable_post_data_reading", "1",  PHP_INI_SYSTEM|PHP_INI_PERDIR,  OnUpdateBool, enable_post_data_reading, php_core_globals, core_globals)
780
781
  STD_PHP_INI_ENTRY("realpath_cache_size",  "4096K",  PHP_INI_SYSTEM,   OnUpdateLong, realpath_cache_size_limit,  virtual_cwd_globals,  cwd_globals)
782
  STD_PHP_INI_ENTRY("realpath_cache_ttl",   "120",    PHP_INI_SYSTEM,   OnUpdateLong, realpath_cache_ttl,     virtual_cwd_globals,  cwd_globals)
783
784
  STD_PHP_INI_ENTRY("user_ini.filename",    ".user.ini",  PHP_INI_SYSTEM,   OnUpdateString,   user_ini_filename,  php_core_globals,   core_globals)
785
  STD_PHP_INI_ENTRY("user_ini.cache_ttl",   "300",      PHP_INI_SYSTEM,   OnUpdateLong,   user_ini_cache_ttl, php_core_globals,   core_globals)
786
  STD_PHP_INI_ENTRY("hard_timeout",     "2",      PHP_INI_SYSTEM,   OnUpdateLong,   hard_timeout,   zend_executor_globals,  executor_globals)
787
#ifdef PHP_WIN32
788
  STD_PHP_INI_BOOLEAN("windows.show_crt_warning",   "0",    PHP_INI_ALL,    OnUpdateBool,     windows_show_crt_warning,     php_core_globals, core_globals)
789
#endif
790
  STD_PHP_INI_ENTRY("syslog.facility",    "LOG_USER",   PHP_INI_SYSTEM,   OnSetFacility,    syslog_facility,  php_core_globals,   core_globals)
791
  STD_PHP_INI_ENTRY("syslog.ident",   "php",      PHP_INI_SYSTEM,   OnUpdateString,   syslog_ident,   php_core_globals,   core_globals)
792
  STD_PHP_INI_ENTRY("syslog.filter",    "no-ctrl",    PHP_INI_ALL,    OnSetLogFilter,   syslog_filter,    php_core_globals,     core_globals)
793
PHP_INI_END()
794
/* }}} */
795
796
/* True globals (no need for thread safety */
797
/* But don't make them a single int bitfield */
798
static int module_initialized = 0;
799
static int module_startup = 1;
800
static int module_shutdown = 0;
801
802
/* {{{ php_during_module_startup */
803
static int php_during_module_startup(void)
804
0
{
805
0
  return module_startup;
806
0
}
807
/* }}} */
808
809
/* {{{ php_during_module_shutdown */
810
static int php_during_module_shutdown(void)
811
0
{
812
0
  return module_shutdown;
813
0
}
814
/* }}} */
815
816
/* {{{ php_get_module_initialized
817
 */
818
PHPAPI int php_get_module_initialized(void)
819
0
{
820
0
  return module_initialized;
821
0
}
822
/* }}} */
823
824
/* {{{ php_log_err_with_severity
825
 */
826
PHPAPI ZEND_COLD void php_log_err_with_severity(const char *log_message, int syslog_type_int)
827
0
{
828
0
  int fd = -1;
829
0
  time_t error_time;
830
831
0
  if (PG(in_error_log)) {
832
    /* prevent recursive invocation */
833
0
    return;
834
0
  }
835
0
  PG(in_error_log) = 1;
836
837
  /* Try to use the specified logging location. */
838
0
  if (PG(error_log) != NULL) {
839
0
#ifdef HAVE_SYSLOG_H
840
0
    if (!strcmp(PG(error_log), "syslog")) {
841
0
      php_syslog(syslog_type_int, "%s", log_message);
842
0
      PG(in_error_log) = 0;
843
0
      return;
844
0
    }
845
0
#endif
846
0
    fd = VCWD_OPEN_MODE(PG(error_log), O_CREAT | O_APPEND | O_WRONLY, 0644);
847
0
    if (fd != -1) {
848
0
      char *tmp;
849
0
      size_t len;
850
0
      zend_string *error_time_str;
851
852
0
      time(&error_time);
853
#ifdef ZTS
854
      if (!php_during_module_startup()) {
855
        error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 1);
856
      } else {
857
        error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 0);
858
      }
859
#else
860
0
      error_time_str = php_format_date("d-M-Y H:i:s e", 13, error_time, 1);
861
0
#endif
862
0
      len = spprintf(&tmp, 0, "[%s] %s%s", ZSTR_VAL(error_time_str), log_message, PHP_EOL);
863
#ifdef PHP_WIN32
864
      php_flock(fd, 2);
865
      /* XXX should eventually write in a loop if len > UINT_MAX */
866
      php_ignore_value(write(fd, tmp, (unsigned)len));
867
#else
868
0
      php_ignore_value(write(fd, tmp, len));
869
0
#endif
870
0
      efree(tmp);
871
0
      zend_string_free(error_time_str);
872
0
      close(fd);
873
0
      PG(in_error_log) = 0;
874
0
      return;
875
0
    }
876
0
  }
877
878
  /* Otherwise fall back to the default logging location, if we have one */
879
880
0
  if (sapi_module.log_message) {
881
0
    sapi_module.log_message(log_message, syslog_type_int);
882
0
  }
883
0
  PG(in_error_log) = 0;
884
0
}
885
/* }}} */
886
887
/* {{{ php_write
888
   wrapper for modules to use PHPWRITE */
889
PHPAPI size_t php_write(void *buf, size_t size)
890
0
{
891
0
  return PHPWRITE(buf, size);
892
0
}
893
/* }}} */
894
895
/* {{{ php_printf
896
 */
897
PHPAPI size_t php_printf(const char *format, ...)
898
0
{
899
0
  va_list args;
900
0
  size_t ret;
901
0
  char *buffer;
902
0
  size_t size;
903
904
0
  va_start(args, format);
905
0
  size = vspprintf(&buffer, 0, format, args);
906
0
  ret = PHPWRITE(buffer, size);
907
0
  efree(buffer);
908
0
  va_end(args);
909
910
0
  return ret;
911
0
}
912
/* }}} */
913
914
/* {{{ php_printf_unchecked
915
 */
916
PHPAPI size_t php_printf_unchecked(const char *format, ...)
917
0
{
918
0
  va_list args;
919
0
  size_t ret;
920
0
  char *buffer;
921
0
  size_t size;
922
923
0
  va_start(args, format);
924
0
  size = vspprintf(&buffer, 0, format, args);
925
0
  ret = PHPWRITE(buffer, size);
926
0
  efree(buffer);
927
0
  va_end(args);
928
929
0
  return ret;
930
0
}
931
/* }}} */
932
933
0
static zend_string *escape_html(const char *buffer, size_t buffer_len) {
934
0
  zend_string *result = php_escape_html_entities_ex(
935
0
    (const unsigned char *) buffer, buffer_len, 0, ENT_COMPAT,
936
    /* charset_hint */ NULL, /* double_encode */ 1, /* quiet */ 1);
937
0
  if (!result || ZSTR_LEN(result) == 0) {
938
    /* Retry with substituting invalid chars on fail. */
939
0
    result = php_escape_html_entities_ex(
940
0
      (const unsigned char *) buffer, buffer_len, 0, ENT_COMPAT | ENT_HTML_SUBSTITUTE_ERRORS,
941
      /* charset_hint */ NULL, /* double_encode */ 1, /* quiet */ 1);
942
0
  }
943
0
  return result;
944
0
}
945
946
/* {{{ php_verror */
947
/* php_verror is called from php_error_docref<n> functions.
948
 * Its purpose is to unify error messages and automatically generate clickable
949
 * html error messages if corresponding ini setting (html_errors) is activated.
950
 * See: CODING_STANDARDS.md for details.
951
 */
952
PHPAPI ZEND_COLD void php_verror(const char *docref, const char *params, int type, const char *format, va_list args)
953
0
{
954
0
  zend_string *replace_buffer = NULL, *replace_origin = NULL;
955
0
  char *buffer = NULL, *docref_buf = NULL, *target = NULL;
956
0
  char *docref_target = "", *docref_root = "";
957
0
  char *p;
958
0
  int buffer_len = 0;
959
0
  const char *space = "";
960
0
  const char *class_name = "";
961
0
  const char *function;
962
0
  int origin_len;
963
0
  char *origin;
964
0
  zend_string *message;
965
0
  int is_function = 0;
966
967
  /* get error text into buffer and escape for html if necessary */
968
0
  buffer_len = (int)vspprintf(&buffer, 0, format, args);
969
970
0
  if (PG(html_errors)) {
971
0
    replace_buffer = escape_html(buffer, buffer_len);
972
0
    efree(buffer);
973
974
0
    if (replace_buffer) {
975
0
      buffer = ZSTR_VAL(replace_buffer);
976
0
      buffer_len = (int)ZSTR_LEN(replace_buffer);
977
0
    } else {
978
0
      buffer = "";
979
0
      buffer_len = 0;
980
0
    }
981
0
  }
982
983
  /* which function caused the problem if any at all */
984
0
  if (php_during_module_startup()) {
985
0
    function = "PHP Startup";
986
0
  } else if (php_during_module_shutdown()) {
987
0
    function = "PHP Shutdown";
988
0
  } else if (EG(current_execute_data) &&
989
0
        EG(current_execute_data)->func &&
990
0
        ZEND_USER_CODE(EG(current_execute_data)->func->common.type) &&
991
0
        EG(current_execute_data)->opline &&
992
0
        EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL
993
0
  ) {
994
0
    switch (EG(current_execute_data)->opline->extended_value) {
995
0
      case ZEND_EVAL:
996
0
        function = "eval";
997
0
        is_function = 1;
998
0
        break;
999
0
      case ZEND_INCLUDE:
1000
0
        function = "include";
1001
0
        is_function = 1;
1002
0
        break;
1003
0
      case ZEND_INCLUDE_ONCE:
1004
0
        function = "include_once";
1005
0
        is_function = 1;
1006
0
        break;
1007
0
      case ZEND_REQUIRE:
1008
0
        function = "require";
1009
0
        is_function = 1;
1010
0
        break;
1011
0
      case ZEND_REQUIRE_ONCE:
1012
0
        function = "require_once";
1013
0
        is_function = 1;
1014
0
        break;
1015
0
      default:
1016
0
        function = "Unknown";
1017
0
    }
1018
0
  } else {
1019
0
    function = get_active_function_name();
1020
0
    if (!function || !strlen(function)) {
1021
0
      function = "Unknown";
1022
0
    } else {
1023
0
      is_function = 1;
1024
0
      class_name = get_active_class_name(&space);
1025
0
    }
1026
0
  }
1027
1028
  /* if we still have memory then format the origin */
1029
0
  if (is_function) {
1030
0
    origin_len = (int)spprintf(&origin, 0, "%s%s%s(%s)", class_name, space, function, params);
1031
0
  } else {
1032
0
    origin_len = (int)spprintf(&origin, 0, "%s", function);
1033
0
  }
1034
1035
0
  if (PG(html_errors)) {
1036
0
    replace_origin = escape_html(origin, origin_len);
1037
0
    efree(origin);
1038
0
    origin = ZSTR_VAL(replace_origin);
1039
0
  }
1040
1041
  /* origin and buffer available, so lets come up with the error message */
1042
0
  if (docref && docref[0] == '#') {
1043
0
    docref_target = strchr(docref, '#');
1044
0
    docref = NULL;
1045
0
  }
1046
1047
  /* no docref given but function is known (the default) */
1048
0
  if (!docref && is_function) {
1049
0
    int doclen;
1050
0
    while (*function == '_') {
1051
0
      function++;
1052
0
    }
1053
0
    if (space[0] == '\0') {
1054
0
      doclen = (int)spprintf(&docref_buf, 0, "function.%s", function);
1055
0
    } else {
1056
0
      doclen = (int)spprintf(&docref_buf, 0, "%s.%s", class_name, function);
1057
0
    }
1058
0
    while((p = strchr(docref_buf, '_')) != NULL) {
1059
0
      *p = '-';
1060
0
    }
1061
0
    docref = php_strtolower(docref_buf, doclen);
1062
0
  }
1063
1064
  /* we have a docref for a function AND
1065
   * - we show errors in html mode AND
1066
   * - the user wants to see the links
1067
   */
1068
0
  if (docref && is_function && PG(html_errors) && strlen(PG(docref_root))) {
1069
0
    if (strncmp(docref, "http://", 7)) {
1070
      /* We don't have 'http://' so we use docref_root */
1071
1072
0
      char *ref;  /* temp copy for duplicated docref */
1073
1074
0
      docref_root = PG(docref_root);
1075
1076
0
      ref = estrdup(docref);
1077
0
      if (docref_buf) {
1078
0
        efree(docref_buf);
1079
0
      }
1080
0
      docref_buf = ref;
1081
      /* strip of the target if any */
1082
0
      p = strrchr(ref, '#');
1083
0
      if (p) {
1084
0
        target = estrdup(p);
1085
0
        if (target) {
1086
0
          docref_target = target;
1087
0
          *p = '\0';
1088
0
        }
1089
0
      }
1090
      /* add the extension if it is set in ini */
1091
0
      if (PG(docref_ext) && strlen(PG(docref_ext))) {
1092
0
        spprintf(&docref_buf, 0, "%s%s", ref, PG(docref_ext));
1093
0
        efree(ref);
1094
0
      }
1095
0
      docref = docref_buf;
1096
0
    }
1097
    /* display html formatted or only show the additional links */
1098
0
    if (PG(html_errors)) {
1099
0
      message = zend_strpprintf(0, "%s [<a href='%s%s%s'>%s</a>]: %s", origin, docref_root, docref, docref_target, docref, buffer);
1100
0
    } else {
1101
0
      message = zend_strpprintf(0, "%s [%s%s%s]: %s", origin, docref_root, docref, docref_target, buffer);
1102
0
    }
1103
0
    if (target) {
1104
0
      efree(target);
1105
0
    }
1106
0
  } else {
1107
0
    message = zend_strpprintf(0, "%s: %s", origin, buffer);
1108
0
  }
1109
0
  if (replace_origin) {
1110
0
    zend_string_free(replace_origin);
1111
0
  } else {
1112
0
    efree(origin);
1113
0
  }
1114
0
  if (docref_buf) {
1115
0
    efree(docref_buf);
1116
0
  }
1117
1118
0
  if (replace_buffer) {
1119
0
    zend_string_free(replace_buffer);
1120
0
  } else {
1121
0
    efree(buffer);
1122
0
  }
1123
1124
0
  zend_error_zstr(type, message);
1125
0
  zend_string_release(message);
1126
0
}
1127
/* }}} */
1128
1129
/* {{{ php_error_docref */
1130
/* Generate an error which links to docref or the php.net documentation if docref is NULL */
1131
PHPAPI ZEND_COLD void php_error_docref(const char *docref, int type, const char *format, ...)
1132
0
{
1133
0
  va_list args;
1134
1135
0
  va_start(args, format);
1136
0
  php_verror(docref, "", type, format, args);
1137
0
  va_end(args);
1138
0
}
1139
/* }}} */
1140
1141
/* {{{ php_error_docref1 */
1142
/* See: CODING_STANDARDS.md for details. */
1143
PHPAPI ZEND_COLD void php_error_docref1(const char *docref, const char *param1, int type, const char *format, ...)
1144
0
{
1145
0
  va_list args;
1146
1147
0
  va_start(args, format);
1148
0
  php_verror(docref, param1, type, format, args);
1149
0
  va_end(args);
1150
0
}
1151
/* }}} */
1152
1153
/* {{{ php_error_docref2 */
1154
/* See: CODING_STANDARDS.md for details. */
1155
PHPAPI ZEND_COLD void php_error_docref2(const char *docref, const char *param1, const char *param2, int type, const char *format, ...)
1156
0
{
1157
0
  char *params;
1158
0
  va_list args;
1159
1160
0
  spprintf(&params, 0, "%s,%s", param1, param2);
1161
0
  va_start(args, format);
1162
0
  php_verror(docref, params ? params : "...", type, format, args);
1163
0
  va_end(args);
1164
0
  if (params) {
1165
0
    efree(params);
1166
0
  }
1167
0
}
1168
/* }}} */
1169
1170
#ifdef PHP_WIN32
1171
PHPAPI ZEND_COLD void php_win32_docref2_from_error(DWORD error, const char *param1, const char *param2) {
1172
  char *buf = php_win32_error_to_msg(error);
1173
  size_t buf_len;
1174
1175
  buf_len = strlen(buf);
1176
  if (buf_len >= 2) {
1177
    buf[buf_len - 1] = '\0';
1178
    buf[buf_len - 2] = '\0';
1179
  }
1180
  php_error_docref2(NULL, param1, param2, E_WARNING, "%s (code: %lu)", buf, error);
1181
  php_win32_error_msg_free(buf);
1182
}
1183
#endif
1184
1185
/* {{{ php_html_puts */
1186
PHPAPI void php_html_puts(const char *str, size_t size)
1187
0
{
1188
0
  zend_html_puts(str, size);
1189
0
}
1190
/* }}} */
1191
1192
493k
static void clear_last_error() {
1193
493k
  if (PG(last_error_message)) {
1194
106k
    zend_string_release(PG(last_error_message));
1195
106k
    PG(last_error_message) = NULL;
1196
106k
  }
1197
493k
  if (PG(last_error_file)) {
1198
106k
    free(PG(last_error_file));
1199
106k
    PG(last_error_file) = NULL;
1200
106k
  }
1201
493k
}
1202
1203
/* {{{ php_error_cb
1204
 extended error handling function */
1205
static ZEND_COLD void php_error_cb(int orig_type, const char *error_filename, const uint32_t error_lineno, zend_string *message)
1206
106k
{
1207
106k
  zend_bool display;
1208
106k
  int type = orig_type & E_ALL;
1209
1210
  /* check for repeated errors to be ignored */
1211
106k
  if (PG(ignore_repeated_errors) && PG(last_error_message)) {
1212
    /* no check for PG(last_error_file) is needed since it cannot
1213
     * be NULL if PG(last_error_message) is not NULL */
1214
0
    if (zend_string_equals(PG(last_error_message), message)
1215
0
      || (!PG(ignore_repeated_source)
1216
0
        && ((PG(last_error_lineno) != (int)error_lineno)
1217
0
          || strcmp(PG(last_error_file), error_filename)))) {
1218
0
      display = 1;
1219
0
    } else {
1220
0
      display = 0;
1221
0
    }
1222
106k
  } else {
1223
106k
    display = 1;
1224
106k
  }
1225
1226
  /* according to error handling mode, throw exception or show it */
1227
106k
  if (EG(error_handling) == EH_THROW) {
1228
0
    switch (type) {
1229
0
      case E_ERROR:
1230
0
      case E_CORE_ERROR:
1231
0
      case E_COMPILE_ERROR:
1232
0
      case E_USER_ERROR:
1233
0
      case E_PARSE:
1234
        /* fatal errors are real errors and cannot be made exceptions */
1235
0
        break;
1236
0
      case E_STRICT:
1237
0
      case E_DEPRECATED:
1238
0
      case E_USER_DEPRECATED:
1239
        /* for the sake of BC to old damaged code */
1240
0
        break;
1241
0
      case E_NOTICE:
1242
0
      case E_USER_NOTICE:
1243
        /* notices are no errors and are not treated as such like E_WARNINGS */
1244
0
        break;
1245
0
      default:
1246
        /* throw an exception if we are in EH_THROW mode
1247
         * but DO NOT overwrite a pending exception
1248
         */
1249
0
        if (!EG(exception)) {
1250
0
          zend_throw_error_exception(EG(exception_class), message, 0, type);
1251
0
        }
1252
0
        return;
1253
106k
    }
1254
106k
  }
1255
1256
  /* store the error if it has changed */
1257
106k
  if (display) {
1258
106k
    clear_last_error();
1259
106k
    if (!error_filename) {
1260
0
      error_filename = "Unknown";
1261
0
    }
1262
106k
    PG(last_error_type) = type;
1263
106k
    PG(last_error_message) = zend_string_copy(message);
1264
106k
    PG(last_error_file) = strdup(error_filename);
1265
106k
    PG(last_error_lineno) = error_lineno;
1266
106k
  }
1267
1268
  /* display/log the error if necessary */
1269
106k
  if (display && ((EG(error_reporting) & type) || (type & E_CORE))
1270
0
    && (PG(log_errors) || PG(display_errors) || (!module_initialized))) {
1271
0
    char *error_type_str;
1272
0
    int syslog_type_int = LOG_NOTICE;
1273
1274
0
    switch (type) {
1275
0
      case E_ERROR:
1276
0
      case E_CORE_ERROR:
1277
0
      case E_COMPILE_ERROR:
1278
0
      case E_USER_ERROR:
1279
0
        error_type_str = "Fatal error";
1280
0
        syslog_type_int = LOG_ERR;
1281
0
        break;
1282
0
      case E_RECOVERABLE_ERROR:
1283
0
        error_type_str = "Recoverable fatal error";
1284
0
        syslog_type_int = LOG_ERR;
1285
0
        break;
1286
0
      case E_WARNING:
1287
0
      case E_CORE_WARNING:
1288
0
      case E_COMPILE_WARNING:
1289
0
      case E_USER_WARNING:
1290
0
        error_type_str = "Warning";
1291
0
        syslog_type_int = LOG_WARNING;
1292
0
        break;
1293
0
      case E_PARSE:
1294
0
        error_type_str = "Parse error";
1295
0
        syslog_type_int = LOG_ERR;
1296
0
        break;
1297
0
      case E_NOTICE:
1298
0
      case E_USER_NOTICE:
1299
0
        error_type_str = "Notice";
1300
0
        syslog_type_int = LOG_NOTICE;
1301
0
        break;
1302
0
      case E_STRICT:
1303
0
        error_type_str = "Strict Standards";
1304
0
        syslog_type_int = LOG_INFO;
1305
0
        break;
1306
0
      case E_DEPRECATED:
1307
0
      case E_USER_DEPRECATED:
1308
0
        error_type_str = "Deprecated";
1309
0
        syslog_type_int = LOG_INFO;
1310
0
        break;
1311
0
      default:
1312
0
        error_type_str = "Unknown error";
1313
0
        break;
1314
0
    }
1315
1316
0
    if (!module_initialized || PG(log_errors)) {
1317
0
      char *log_buffer;
1318
#ifdef PHP_WIN32
1319
      if (type == E_CORE_ERROR || type == E_CORE_WARNING) {
1320
        syslog(LOG_ALERT, "PHP %s: %s (%s)", error_type_str, ZSTR_VAL(message), GetCommandLine());
1321
      }
1322
#endif
1323
0
      spprintf(&log_buffer, 0, "PHP %s:  %s in %s on line %" PRIu32, error_type_str, ZSTR_VAL(message), error_filename, error_lineno);
1324
0
      php_log_err_with_severity(log_buffer, syslog_type_int);
1325
0
      efree(log_buffer);
1326
0
    }
1327
1328
0
    if (PG(display_errors) && ((module_initialized && !PG(during_request_startup)) || (PG(display_startup_errors)))) {
1329
0
      if (PG(xmlrpc_errors)) {
1330
0
        php_printf("<?xml version=\"1.0\"?><methodResponse><fault><value><struct><member><name>faultCode</name><value><int>" ZEND_LONG_FMT "</int></value></member><member><name>faultString</name><value><string>%s:%s in %s on line %" PRIu32 "</string></value></member></struct></value></fault></methodResponse>", PG(xmlrpc_error_number), error_type_str, ZSTR_VAL(message), error_filename, error_lineno);
1331
0
      } else {
1332
0
        char *prepend_string = INI_STR("error_prepend_string");
1333
0
        char *append_string = INI_STR("error_append_string");
1334
1335
0
        if (PG(html_errors)) {
1336
0
          if (type == E_ERROR || type == E_PARSE) {
1337
0
            zend_string *buf = escape_html(ZSTR_VAL(message), ZSTR_LEN(message));
1338
0
            php_printf("%s<br />\n<b>%s</b>:  %s in <b>%s</b> on line <b>%" PRIu32 "</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(buf), error_filename, error_lineno, STR_PRINT(append_string));
1339
0
            zend_string_free(buf);
1340
0
          } else {
1341
0
            php_printf("%s<br />\n<b>%s</b>:  %s in <b>%s</b> on line <b>%" PRIu32 "</b><br />\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(message), error_filename, error_lineno, STR_PRINT(append_string));
1342
0
          }
1343
0
        } else {
1344
          /* Write CLI/CGI errors to stderr if display_errors = "stderr" */
1345
0
          if ((!strcmp(sapi_module.name, "cli") || !strcmp(sapi_module.name, "cgi") || !strcmp(sapi_module.name, "phpdbg")) &&
1346
0
            PG(display_errors) == PHP_DISPLAY_ERRORS_STDERR
1347
0
          ) {
1348
0
            fprintf(stderr, "%s: %s in %s on line %" PRIu32 "\n", error_type_str, ZSTR_VAL(message), error_filename, error_lineno);
1349
#ifdef PHP_WIN32
1350
            fflush(stderr);
1351
#endif
1352
0
          } else {
1353
0
            php_printf("%s\n%s: %s in %s on line %" PRIu32 "\n%s", STR_PRINT(prepend_string), error_type_str, ZSTR_VAL(message), error_filename, error_lineno, STR_PRINT(append_string));
1354
0
          }
1355
0
        }
1356
0
      }
1357
0
    }
1358
#if ZEND_DEBUG
1359
    if (PG(report_zend_debug)) {
1360
      zend_bool trigger_break;
1361
1362
      switch (type) {
1363
        case E_ERROR:
1364
        case E_CORE_ERROR:
1365
        case E_COMPILE_ERROR:
1366
        case E_USER_ERROR:
1367
          trigger_break=1;
1368
          break;
1369
        default:
1370
          trigger_break=0;
1371
          break;
1372
      }
1373
      zend_output_debug_string(trigger_break, "%s(%" PRIu32 ") : %s - %s", error_filename, error_lineno, error_type_str, ZSTR_VAL(message));
1374
    }
1375
#endif
1376
0
  }
1377
1378
  /* Bail out if we can't recover */
1379
106k
  switch (type) {
1380
0
    case E_CORE_ERROR:
1381
0
      if(!module_initialized) {
1382
        /* bad error in module startup - no way we can live with this */
1383
0
        exit(-2);
1384
0
      }
1385
    /* no break - intentionally */
1386
955
    case E_ERROR:
1387
955
    case E_RECOVERABLE_ERROR:
1388
955
    case E_PARSE:
1389
17.8k
    case E_COMPILE_ERROR:
1390
17.8k
    case E_USER_ERROR:
1391
17.8k
      EG(exit_status) = 255;
1392
17.8k
      if (module_initialized) {
1393
17.8k
        if (!PG(display_errors) &&
1394
0
            !SG(headers_sent) &&
1395
0
          SG(sapi_headers).http_response_code == 200
1396
0
        ) {
1397
0
          sapi_header_line ctr = {0};
1398
1399
0
          ctr.line = "HTTP/1.0 500 Internal Server Error";
1400
0
          ctr.line_len = sizeof("HTTP/1.0 500 Internal Server Error") - 1;
1401
0
          sapi_header_op(SAPI_HEADER_REPLACE, &ctr);
1402
0
        }
1403
        /* the parser would return 1 (failure), we can bail out nicely */
1404
17.8k
        if (!(orig_type & E_DONT_BAIL)) {
1405
          /* restore memory limit */
1406
17.7k
          zend_set_memory_limit(PG(memory_limit));
1407
17.7k
          zend_objects_store_mark_destructed(&EG(objects_store));
1408
17.7k
          zend_bailout();
1409
0
          return;
1410
98
        }
1411
17.8k
      }
1412
98
      break;
1413
106k
  }
1414
106k
}
1415
/* }}} */
1416
1417
/* {{{ php_get_current_user
1418
 */
1419
PHPAPI char *php_get_current_user(void)
1420
0
{
1421
0
  zend_stat_t *pstat;
1422
1423
0
  if (SG(request_info).current_user) {
1424
0
    return SG(request_info).current_user;
1425
0
  }
1426
1427
  /* FIXME: I need to have this somehow handled if
1428
  USE_SAPI is defined, because cgi will also be
1429
  interfaced in USE_SAPI */
1430
1431
0
  pstat = sapi_get_stat();
1432
1433
0
  if (!pstat) {
1434
0
    return "";
1435
0
  } else {
1436
#ifdef PHP_WIN32
1437
    char *name = php_win32_get_username();
1438
    int len;
1439
1440
    if (!name) {
1441
      return "";
1442
    }
1443
    len = (int)strlen(name);
1444
    name[len] = '\0';
1445
    SG(request_info).current_user_length = len;
1446
    SG(request_info).current_user = estrndup(name, len);
1447
    free(name);
1448
    return SG(request_info).current_user;
1449
#else
1450
0
    struct passwd *pwd;
1451
#if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
1452
    struct passwd _pw;
1453
    struct passwd *retpwptr = NULL;
1454
    int pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
1455
    char *pwbuf;
1456
1457
    if (pwbuflen < 1) {
1458
      return "";
1459
    }
1460
    pwbuf = emalloc(pwbuflen);
1461
    if (getpwuid_r(pstat->st_uid, &_pw, pwbuf, pwbuflen, &retpwptr) != 0) {
1462
      efree(pwbuf);
1463
      return "";
1464
    }
1465
    if (retpwptr == NULL) {
1466
      efree(pwbuf);
1467
      return "";
1468
    }
1469
    pwd = &_pw;
1470
#else
1471
0
    if ((pwd=getpwuid(pstat->st_uid))==NULL) {
1472
0
      return "";
1473
0
    }
1474
0
#endif
1475
0
    SG(request_info).current_user_length = strlen(pwd->pw_name);
1476
0
    SG(request_info).current_user = estrndup(pwd->pw_name, SG(request_info).current_user_length);
1477
#if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
1478
    efree(pwbuf);
1479
#endif
1480
0
    return SG(request_info).current_user;
1481
0
#endif
1482
0
  }
1483
0
}
1484
/* }}} */
1485
1486
/* {{{ proto bool set_time_limit(int seconds)
1487
   Sets the maximum time a script can run */
1488
PHP_FUNCTION(set_time_limit)
1489
0
{
1490
0
  zend_long new_timeout;
1491
0
  char *new_timeout_str;
1492
0
  int new_timeout_strlen;
1493
0
  zend_string *key;
1494
1495
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &new_timeout) == FAILURE) {
1496
0
    RETURN_THROWS();
1497
0
  }
1498
1499
0
  new_timeout_strlen = (int)zend_spprintf(&new_timeout_str, 0, ZEND_LONG_FMT, new_timeout);
1500
1501
0
  key = zend_string_init("max_execution_time", sizeof("max_execution_time")-1, 0);
1502
0
  if (zend_alter_ini_entry_chars_ex(key, new_timeout_str, new_timeout_strlen, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0) == SUCCESS) {
1503
0
    RETVAL_TRUE;
1504
0
  } else {
1505
0
    RETVAL_FALSE;
1506
0
  }
1507
0
  zend_string_release_ex(key, 0);
1508
0
  efree(new_timeout_str);
1509
0
}
1510
/* }}} */
1511
1512
/* {{{ php_fopen_wrapper_for_zend
1513
 */
1514
static FILE *php_fopen_wrapper_for_zend(const char *filename, zend_string **opened_path)
1515
0
{
1516
0
  return php_stream_open_wrapper_as_file((char *)filename, "rb", USE_PATH|IGNORE_URL_WIN|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE, opened_path);
1517
0
}
1518
/* }}} */
1519
1520
static void php_zend_stream_closer(void *handle) /* {{{ */
1521
0
{
1522
0
  php_stream_close((php_stream*)handle);
1523
0
}
1524
/* }}} */
1525
1526
static size_t php_zend_stream_fsizer(void *handle) /* {{{ */
1527
0
{
1528
0
  php_stream *stream = handle;
1529
0
  php_stream_statbuf ssb;
1530
1531
  /* File size reported by stat() may be inaccurate if stream filters are used.
1532
   * TODO: Should stat() be generally disabled if filters are used? */
1533
0
  if (stream->readfilters.head) {
1534
0
    return 0;
1535
0
  }
1536
1537
0
  if (php_stream_stat(stream, &ssb) == 0) {
1538
0
    return ssb.sb.st_size;
1539
0
  }
1540
0
  return 0;
1541
0
}
1542
/* }}} */
1543
1544
static int php_stream_open_for_zend(const char *filename, zend_file_handle *handle) /* {{{ */
1545
0
{
1546
0
  return php_stream_open_for_zend_ex(filename, handle, USE_PATH|REPORT_ERRORS|STREAM_OPEN_FOR_INCLUDE);
1547
0
}
1548
/* }}} */
1549
1550
PHPAPI int php_stream_open_for_zend_ex(const char *filename, zend_file_handle *handle, int mode) /* {{{ */
1551
0
{
1552
0
  zend_string *opened_path;
1553
0
  php_stream *stream = php_stream_open_wrapper((char *)filename, "rb", mode, &opened_path);
1554
0
  if (stream) {
1555
0
    memset(handle, 0, sizeof(zend_file_handle));
1556
0
    handle->type = ZEND_HANDLE_STREAM;
1557
0
    handle->filename = (char*)filename;
1558
0
    handle->opened_path = opened_path;
1559
0
    handle->handle.stream.handle  = stream;
1560
0
    handle->handle.stream.reader  = (zend_stream_reader_t)_php_stream_read;
1561
0
    handle->handle.stream.fsizer  = php_zend_stream_fsizer;
1562
0
    handle->handle.stream.isatty  = 0;
1563
0
    handle->handle.stream.closer = php_zend_stream_closer;
1564
    /* suppress warning if this stream is not explicitly closed */
1565
0
    php_stream_auto_cleanup(stream);
1566
    /* Disable buffering to avoid double buffering between PHP and Zend streams. */
1567
0
    php_stream_set_option(stream, PHP_STREAM_OPTION_READ_BUFFER, PHP_STREAM_BUFFER_NONE, NULL);
1568
1569
0
    return SUCCESS;
1570
0
  }
1571
0
  return FAILURE;
1572
0
}
1573
/* }}} */
1574
1575
static zend_string *php_resolve_path_for_zend(const char *filename, size_t filename_len) /* {{{ */
1576
0
{
1577
0
  return php_resolve_path(filename, filename_len, PG(include_path));
1578
0
}
1579
/* }}} */
1580
1581
/* {{{ php_get_configuration_directive_for_zend
1582
 */
1583
static zval *php_get_configuration_directive_for_zend(zend_string *name)
1584
449k
{
1585
449k
  return cfg_get_entry_ex(name);
1586
449k
}
1587
/* }}} */
1588
1589
/* {{{ php_free_request_globals
1590
 */
1591
static void php_free_request_globals(void)
1592
383k
{
1593
383k
  clear_last_error();
1594
383k
  if (PG(php_sys_temp_dir)) {
1595
0
    efree(PG(php_sys_temp_dir));
1596
0
    PG(php_sys_temp_dir) = NULL;
1597
0
  }
1598
383k
}
1599
/* }}} */
1600
1601
/* {{{ php_message_handler_for_zend
1602
 */
1603
static ZEND_COLD void php_message_handler_for_zend(zend_long message, const void *data)
1604
0
{
1605
0
  switch (message) {
1606
0
    case ZMSG_FAILED_INCLUDE_FOPEN:
1607
0
      php_error_docref("function.include", E_WARNING, "Failed opening '%s' for inclusion (include_path='%s')", php_strip_url_passwd((char *) data), STR_PRINT(PG(include_path)));
1608
0
      break;
1609
0
    case ZMSG_FAILED_REQUIRE_FOPEN:
1610
0
      zend_throw_error(NULL, "Failed opening required '%s' (include_path='%s')", php_strip_url_passwd((char *) data), STR_PRINT(PG(include_path)));
1611
0
      break;
1612
0
    case ZMSG_FAILED_HIGHLIGHT_FOPEN:
1613
0
      php_error_docref(NULL, E_WARNING, "Failed opening '%s' for highlighting", php_strip_url_passwd((char *) data));
1614
0
      break;
1615
0
    case ZMSG_MEMORY_LEAK_DETECTED:
1616
0
    case ZMSG_MEMORY_LEAK_REPEATED:
1617
#if ZEND_DEBUG
1618
      if (EG(error_reporting) & E_WARNING) {
1619
        char memory_leak_buf[1024];
1620
1621
        if (message==ZMSG_MEMORY_LEAK_DETECTED) {
1622
          zend_leak_info *t = (zend_leak_info *) data;
1623
1624
          snprintf(memory_leak_buf, 512, "%s(%" PRIu32 ") :  Freeing " ZEND_ADDR_FMT " (%zu bytes), script=%s\n", t->filename, t->lineno, (size_t)t->addr, t->size, SAFE_FILENAME(SG(request_info).path_translated));
1625
          if (t->orig_filename) {
1626
            char relay_buf[512];
1627
1628
            snprintf(relay_buf, 512, "%s(%" PRIu32 ") : Actual location (location was relayed)\n", t->orig_filename, t->orig_lineno);
1629
            strlcat(memory_leak_buf, relay_buf, sizeof(memory_leak_buf));
1630
          }
1631
        } else {
1632
          unsigned long leak_count = (zend_uintptr_t) data;
1633
1634
          snprintf(memory_leak_buf, 512, "Last leak repeated %lu time%s\n", leak_count, (leak_count>1?"s":""));
1635
        }
1636
# if defined(PHP_WIN32)
1637
        if (IsDebuggerPresent()) {
1638
          OutputDebugString(memory_leak_buf);
1639
        } else {
1640
          fprintf(stderr, "%s", memory_leak_buf);
1641
        }
1642
# else
1643
        fprintf(stderr, "%s", memory_leak_buf);
1644
# endif
1645
      }
1646
#endif
1647
0
      break;
1648
0
    case ZMSG_MEMORY_LEAKS_GRAND_TOTAL:
1649
#if ZEND_DEBUG
1650
      if (EG(error_reporting) & E_WARNING) {
1651
        char memory_leak_buf[512];
1652
1653
        snprintf(memory_leak_buf, 512, "=== Total %d memory leaks detected ===\n", *((uint32_t *) data));
1654
# if defined(PHP_WIN32)
1655
        if (IsDebuggerPresent()) {
1656
          OutputDebugString(memory_leak_buf);
1657
        } else {
1658
          fprintf(stderr, "%s", memory_leak_buf);
1659
        }
1660
# else
1661
        fprintf(stderr, "%s", memory_leak_buf);
1662
# endif
1663
      }
1664
#endif
1665
0
      break;
1666
0
    case ZMSG_LOG_SCRIPT_NAME: {
1667
0
        struct tm *ta, tmbuf;
1668
0
        time_t curtime;
1669
0
        char *datetime_str, asctimebuf[52];
1670
0
        char memory_leak_buf[4096];
1671
1672
0
        time(&curtime);
1673
0
        ta = php_localtime_r(&curtime, &tmbuf);
1674
0
        datetime_str = php_asctime_r(ta, asctimebuf);
1675
0
        if (datetime_str) {
1676
0
          datetime_str[strlen(datetime_str)-1]=0; /* get rid of the trailing newline */
1677
0
          snprintf(memory_leak_buf, sizeof(memory_leak_buf), "[%s]  Script:  '%s'\n", datetime_str, SAFE_FILENAME(SG(request_info).path_translated));
1678
0
        } else {
1679
0
          snprintf(memory_leak_buf, sizeof(memory_leak_buf), "[null]  Script:  '%s'\n", SAFE_FILENAME(SG(request_info).path_translated));
1680
0
        }
1681
# if defined(PHP_WIN32)
1682
        if (IsDebuggerPresent()) {
1683
          OutputDebugString(memory_leak_buf);
1684
        } else {
1685
          fprintf(stderr, "%s", memory_leak_buf);
1686
        }
1687
# else
1688
0
        fprintf(stderr, "%s", memory_leak_buf);
1689
0
# endif
1690
0
      }
1691
0
      break;
1692
0
  }
1693
0
}
1694
/* }}} */
1695
1696
1697
void php_on_timeout(int seconds)
1698
0
{
1699
0
  PG(connection_status) |= PHP_CONNECTION_TIMEOUT;
1700
0
}
1701
1702
#if PHP_SIGCHILD
1703
/* {{{ sigchld_handler
1704
 */
1705
static void sigchld_handler(int apar)
1706
{
1707
  int errno_save = errno;
1708
1709
  while (waitpid(-1, NULL, WNOHANG) > 0);
1710
  signal(SIGCHLD, sigchld_handler);
1711
1712
  errno = errno_save;
1713
}
1714
/* }}} */
1715
#endif
1716
1717
/* {{{ php_request_startup
1718
 */
1719
int php_request_startup(void)
1720
383k
{
1721
383k
  int retval = SUCCESS;
1722
1723
383k
  zend_interned_strings_activate();
1724
1725
#ifdef HAVE_DTRACE
1726
  DTRACE_REQUEST_STARTUP(SAFE_FILENAME(SG(request_info).path_translated), SAFE_FILENAME(SG(request_info).request_uri), (char *)SAFE_FILENAME(SG(request_info).request_method));
1727
#endif /* HAVE_DTRACE */
1728
1729
#ifdef PHP_WIN32
1730
# if defined(ZTS)
1731
  _configthreadlocale(_ENABLE_PER_THREAD_LOCALE);
1732
# endif
1733
  PG(com_initialized) = 0;
1734
#endif
1735
1736
#if PHP_SIGCHILD
1737
  signal(SIGCHLD, sigchld_handler);
1738
#endif
1739
1740
383k
  zend_try {
1741
383k
    PG(in_error_log) = 0;
1742
383k
    PG(during_request_startup) = 1;
1743
1744
383k
    php_output_activate();
1745
1746
    /* initialize global variables */
1747
383k
    PG(modules_activated) = 0;
1748
383k
    PG(header_is_being_sent) = 0;
1749
383k
    PG(connection_status) = PHP_CONNECTION_NORMAL;
1750
383k
    PG(in_user_include) = 0;
1751
1752
383k
    zend_activate();
1753
383k
    sapi_activate();
1754
1755
383k
#ifdef ZEND_SIGNALS
1756
383k
    zend_signal_activate();
1757
383k
#endif
1758
1759
383k
    if (PG(max_input_time) == -1) {
1760
383k
      zend_set_timeout(EG(timeout_seconds), 1);
1761
0
    } else {
1762
0
      zend_set_timeout(PG(max_input_time), 1);
1763
0
    }
1764
1765
    /* Disable realpath cache if an open_basedir is set */
1766
383k
    if (PG(open_basedir) && *PG(open_basedir)) {
1767
0
      CWDG(realpath_cache_size_limit) = 0;
1768
0
    }
1769
1770
383k
    if (PG(expose_php)) {
1771
383k
      sapi_add_header(SAPI_PHP_VERSION_HEADER, sizeof(SAPI_PHP_VERSION_HEADER)-1, 1);
1772
383k
    }
1773
1774
383k
    if (PG(output_handler) && PG(output_handler)[0]) {
1775
0
      zval oh;
1776
1777
0
      ZVAL_STRING(&oh, PG(output_handler));
1778
0
      php_output_start_user(&oh, 0, PHP_OUTPUT_HANDLER_STDFLAGS);
1779
0
      zval_ptr_dtor(&oh);
1780
383k
    } else if (PG(output_buffering)) {
1781
0
      php_output_start_user(NULL, PG(output_buffering) > 1 ? PG(output_buffering) : 0, PHP_OUTPUT_HANDLER_STDFLAGS);
1782
383k
    } else if (PG(implicit_flush)) {
1783
383k
      php_output_set_implicit_flush(1);
1784
383k
    }
1785
1786
    /* We turn this off in php_execute_script() */
1787
    /* PG(during_request_startup) = 0; */
1788
1789
383k
    php_hash_environment();
1790
383k
    zend_activate_modules();
1791
383k
    PG(modules_activated)=1;
1792
383k
  } zend_catch {
1793
0
    retval = FAILURE;
1794
0
  } zend_end_try();
1795
1796
383k
  SG(sapi_started) = 1;
1797
1798
383k
  return retval;
1799
383k
}
1800
/* }}} */
1801
1802
/* {{{ php_request_shutdown
1803
 */
1804
void php_request_shutdown(void *dummy)
1805
383k
{
1806
383k
  zend_bool report_memleaks;
1807
1808
383k
  EG(flags) |= EG_FLAGS_IN_SHUTDOWN;
1809
1810
383k
  report_memleaks = PG(report_memleaks);
1811
1812
  /* EG(current_execute_data) points into nirvana and therefore cannot be safely accessed
1813
   * inside zend_executor callback functions.
1814
   */
1815
383k
  EG(current_execute_data) = NULL;
1816
1817
383k
  php_deactivate_ticks();
1818
1819
  /* 1. Call all possible shutdown functions registered with register_shutdown_function() */
1820
383k
  if (PG(modules_activated)) {
1821
383k
    php_call_shutdown_functions();
1822
383k
  }
1823
1824
  /* 2. Call all possible __destruct() functions */
1825
383k
  zend_try {
1826
383k
    zend_call_destructors();
1827
383k
  } zend_end_try();
1828
1829
  /* 3. Flush all output buffers */
1830
383k
  zend_try {
1831
383k
    zend_bool send_buffer = SG(request_info).headers_only ? 0 : 1;
1832
1833
383k
    if (CG(unclean_shutdown) && PG(last_error_type) == E_ERROR &&
1834
955
      (size_t)PG(memory_limit) < zend_memory_usage(1)
1835
0
    ) {
1836
0
      send_buffer = 0;
1837
0
    }
1838
1839
383k
    if (!send_buffer) {
1840
0
      php_output_discard_all();
1841
383k
    } else {
1842
383k
      php_output_end_all();
1843
383k
    }
1844
383k
  } zend_end_try();
1845
1846
  /* 4. Reset max_execution_time (no longer executing php code after response sent) */
1847
383k
  zend_try {
1848
383k
    zend_unset_timeout();
1849
383k
  } zend_end_try();
1850
1851
  /* 5. Call all extensions RSHUTDOWN functions */
1852
383k
  if (PG(modules_activated)) {
1853
383k
    zend_deactivate_modules();
1854
383k
  }
1855
1856
  /* 6. Shutdown output layer (send the set HTTP headers, cleanup output handlers, etc.) */
1857
383k
  zend_try {
1858
383k
    php_output_deactivate();
1859
383k
  } zend_end_try();
1860
1861
  /* 7. Free shutdown functions */
1862
383k
  if (PG(modules_activated)) {
1863
383k
    php_free_shutdown_functions();
1864
383k
  }
1865
1866
  /* 8. Destroy super-globals */
1867
383k
  zend_try {
1868
383k
    int i;
1869
1870
2.68M
    for (i=0; i<NUM_TRACK_VARS; i++) {
1871
2.30M
      zval_ptr_dtor(&PG(http_globals)[i]);
1872
2.30M
    }
1873
383k
  } zend_end_try();
1874
1875
  /* 9. Shutdown scanner/executor/compiler and restore ini entries */
1876
383k
  zend_deactivate();
1877
1878
  /* 10. free request-bound globals */
1879
383k
  php_free_request_globals();
1880
1881
  /* 11. Call all extensions post-RSHUTDOWN functions */
1882
383k
  zend_try {
1883
383k
    zend_post_deactivate_modules();
1884
383k
  } zend_end_try();
1885
1886
  /* 12. SAPI related shutdown (free stuff) */
1887
383k
  zend_try {
1888
383k
    sapi_deactivate();
1889
383k
  } zend_end_try();
1890
1891
  /* 13. free virtual CWD memory */
1892
383k
  virtual_cwd_deactivate();
1893
1894
  /* 14. Destroy stream hashes */
1895
383k
  zend_try {
1896
383k
    php_shutdown_stream_hashes();
1897
383k
  } zend_end_try();
1898
1899
  /* 15. Free Willy (here be crashes) */
1900
383k
  zend_interned_strings_deactivate();
1901
383k
  zend_try {
1902
383k
    shutdown_memory_manager(CG(unclean_shutdown) || !report_memleaks, 0);
1903
383k
  } zend_end_try();
1904
1905
  /* 16. Deactivate Zend signals */
1906
383k
#ifdef ZEND_SIGNALS
1907
383k
  zend_signal_deactivate();
1908
383k
#endif
1909
1910
#ifdef PHP_WIN32
1911
  if (PG(com_initialized)) {
1912
    CoUninitialize();
1913
    PG(com_initialized) = 0;
1914
  }
1915
#endif
1916
1917
#ifdef HAVE_DTRACE
1918
  DTRACE_REQUEST_SHUTDOWN(SAFE_FILENAME(SG(request_info).path_translated), SAFE_FILENAME(SG(request_info).request_uri), (char *)SAFE_FILENAME(SG(request_info).request_method));
1919
#endif /* HAVE_DTRACE */
1920
383k
}
1921
/* }}} */
1922
1923
/* {{{ php_com_initialize
1924
 */
1925
PHPAPI void php_com_initialize(void)
1926
0
{
1927
#ifdef PHP_WIN32
1928
  if (!PG(com_initialized)) {
1929
    if (CoInitialize(NULL) == S_OK) {
1930
      PG(com_initialized) = 1;
1931
    }
1932
  }
1933
#endif
1934
0
}
1935
/* }}} */
1936
1937
#ifdef ZTS
1938
/* {{{ core_globals_ctor
1939
 */
1940
static void core_globals_ctor(php_core_globals *core_globals)
1941
{
1942
  memset(core_globals, 0, sizeof(*core_globals));
1943
  php_startup_ticks();
1944
}
1945
/* }}} */
1946
#endif
1947
1948
/* {{{ core_globals_dtor
1949
 */
1950
static void core_globals_dtor(php_core_globals *core_globals)
1951
0
{
1952
  /* These should have been freed earlier. */
1953
0
  ZEND_ASSERT(!core_globals->last_error_message);
1954
0
  ZEND_ASSERT(!core_globals->last_error_file);
1955
1956
0
  if (core_globals->disable_functions) {
1957
0
    free(core_globals->disable_functions);
1958
0
  }
1959
0
  if (core_globals->disable_classes) {
1960
0
    free(core_globals->disable_classes);
1961
0
  }
1962
0
  if (core_globals->php_binary) {
1963
0
    free(core_globals->php_binary);
1964
0
  }
1965
1966
0
  php_shutdown_ticks();
1967
0
}
1968
/* }}} */
1969
1970
0
PHP_MINFO_FUNCTION(php_core) { /* {{{ */
1971
0
  php_info_print_table_start();
1972
0
  php_info_print_table_row(2, "PHP Version", PHP_VERSION);
1973
0
  php_info_print_table_end();
1974
0
  DISPLAY_INI_ENTRIES();
1975
0
}
1976
/* }}} */
1977
1978
/* {{{ php_register_extensions
1979
 */
1980
int php_register_extensions(zend_module_entry * const * ptr, int count)
1981
3.54k
{
1982
3.54k
  zend_module_entry * const * end = ptr + count;
1983
1984
35.4k
  while (ptr < end) {
1985
31.8k
    if (*ptr) {
1986
31.8k
      if (zend_register_internal_module(*ptr)==NULL) {
1987
0
        return FAILURE;
1988
0
      }
1989
31.8k
    }
1990
31.8k
    ptr++;
1991
31.8k
  }
1992
3.54k
  return SUCCESS;
1993
3.54k
}
1994
1995
/* A very long time ago php_module_startup() was refactored in a way
1996
 * which broke calling it with more than one additional module.
1997
 * This alternative to php_register_extensions() works around that
1998
 * by walking the shallower structure.
1999
 *
2000
 * See algo: https://bugs.php.net/bug.php?id=63159
2001
 */
2002
static int php_register_extensions_bc(zend_module_entry *ptr, int count)
2003
3.54k
{
2004
3.54k
  while (count--) {
2005
0
    if (zend_register_internal_module(ptr++) == NULL) {
2006
0
      return FAILURE;
2007
0
    }
2008
0
  }
2009
3.54k
  return SUCCESS;
2010
3.54k
}
2011
/* }}} */
2012
2013
#ifdef PHP_WIN32
2014
static _invalid_parameter_handler old_invalid_parameter_handler;
2015
2016
void dummy_invalid_parameter_handler(
2017
    const wchar_t *expression,
2018
    const wchar_t *function,
2019
    const wchar_t *file,
2020
    unsigned int   line,
2021
    uintptr_t      pEwserved)
2022
{
2023
  static int called = 0;
2024
  char buf[1024];
2025
  int len;
2026
2027
  if (!called) {
2028
      if(PG(windows_show_crt_warning)) {
2029
      called = 1;
2030
      if (function) {
2031
        if (file) {
2032
          len = _snprintf(buf, sizeof(buf)-1, "Invalid parameter detected in CRT function '%ws' (%ws:%u)", function, file, line);
2033
        } else {
2034
          len = _snprintf(buf, sizeof(buf)-1, "Invalid parameter detected in CRT function '%ws'", function);
2035
        }
2036
      } else {
2037
        len = _snprintf(buf, sizeof(buf)-1, "Invalid CRT parameter detected (function not known)");
2038
      }
2039
      zend_error(E_WARNING, "%s", buf);
2040
      called = 0;
2041
    }
2042
  }
2043
}
2044
#endif
2045
2046
/* {{{ php_module_startup
2047
 */
2048
int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_modules, uint32_t num_additional_modules)
2049
3.54k
{
2050
3.54k
  zend_utility_functions zuf;
2051
3.54k
  zend_utility_values zuv;
2052
3.54k
  int retval = SUCCESS, module_number=0;  /* for REGISTER_INI_ENTRIES() */
2053
3.54k
  char *php_os;
2054
3.54k
  zend_module_entry *module;
2055
2056
#ifdef PHP_WIN32
2057
  WORD wVersionRequested = MAKEWORD(2, 0);
2058
  WSADATA wsaData;
2059
2060
  php_os = "WINNT";
2061
2062
  old_invalid_parameter_handler =
2063
    _set_invalid_parameter_handler(dummy_invalid_parameter_handler);
2064
  if (old_invalid_parameter_handler != NULL) {
2065
    _set_invalid_parameter_handler(old_invalid_parameter_handler);
2066
  }
2067
2068
  /* Disable the message box for assertions.*/
2069
  _CrtSetReportMode(_CRT_ASSERT, 0);
2070
#else
2071
3.54k
  php_os = PHP_OS;
2072
3.54k
#endif
2073
2074
#ifdef ZTS
2075
  (void)ts_resource(0);
2076
#endif
2077
2078
#ifdef PHP_WIN32
2079
  if (!php_win32_init_random_bytes()) {
2080
    fprintf(stderr, "\ncrypt algorithm provider initialization failed\n");
2081
    return FAILURE;
2082
  }
2083
#endif
2084
2085
3.54k
  module_shutdown = 0;
2086
3.54k
  module_startup = 1;
2087
3.54k
  sapi_initialize_empty_request();
2088
3.54k
  sapi_activate();
2089
2090
3.54k
  if (module_initialized) {
2091
0
    return SUCCESS;
2092
0
  }
2093
2094
3.54k
  sapi_module = *sf;
2095
2096
3.54k
  php_output_startup();
2097
2098
#ifdef ZTS
2099
  ts_allocate_fast_id(&core_globals_id, &core_globals_offset, sizeof(php_core_globals), (ts_allocate_ctor) core_globals_ctor, (ts_allocate_dtor) core_globals_dtor);
2100
#ifdef PHP_WIN32
2101
  ts_allocate_id(&php_win32_core_globals_id, sizeof(php_win32_core_globals), (ts_allocate_ctor) php_win32_core_globals_ctor, (ts_allocate_dtor) php_win32_core_globals_dtor);
2102
#endif
2103
#else
2104
3.54k
  memset(&core_globals, 0, sizeof(core_globals));
2105
3.54k
  php_startup_ticks();
2106
3.54k
#endif
2107
3.54k
  gc_globals_ctor();
2108
2109
3.54k
  zuf.error_function = php_error_cb;
2110
3.54k
  zuf.printf_function = php_printf;
2111
3.54k
  zuf.write_function = php_output_write;
2112
3.54k
  zuf.fopen_function = php_fopen_wrapper_for_zend;
2113
3.54k
  zuf.message_handler = php_message_handler_for_zend;
2114
3.54k
  zuf.get_configuration_directive = php_get_configuration_directive_for_zend;
2115
3.54k
  zuf.ticks_function = php_run_ticks;
2116
3.54k
  zuf.on_timeout = php_on_timeout;
2117
3.54k
  zuf.stream_open_function = php_stream_open_for_zend;
2118
3.54k
  zuf.printf_to_smart_string_function = php_printf_to_smart_string;
2119
3.54k
  zuf.printf_to_smart_str_function = php_printf_to_smart_str;
2120
3.54k
  zuf.getenv_function = sapi_getenv;
2121
3.54k
  zuf.resolve_path_function = php_resolve_path_for_zend;
2122
3.54k
  zend_startup(&zuf);
2123
3.54k
  zend_update_current_locale();
2124
2125
3.54k
#if HAVE_TZSET
2126
3.54k
  tzset();
2127
3.54k
#endif
2128
2129
#ifdef PHP_WIN32
2130
# if PHP_LINKER_MAJOR == 14
2131
  /* Extend for other CRT if needed. */
2132
#  if PHP_DEBUG
2133
#   define PHP_VCRUNTIME "vcruntime140d.dll"
2134
#  else
2135
#   define PHP_VCRUNTIME "vcruntime140.dll"
2136
#  endif
2137
  char *img_err;
2138
  if (!php_win32_crt_compatible(PHP_VCRUNTIME, &img_err)) {
2139
    php_error(E_CORE_WARNING, img_err);
2140
    efree(img_err);
2141
    return FAILURE;
2142
  }
2143
#  undef PHP_VCRUNTIME
2144
# endif
2145
2146
  /* start up winsock services */
2147
  if (WSAStartup(wVersionRequested, &wsaData) != 0) {
2148
    php_printf("\nwinsock.dll unusable. %d\n", WSAGetLastError());
2149
    return FAILURE;
2150
  }
2151
  php_win32_signal_ctrl_handler_init();
2152
#endif
2153
2154
3.54k
  le_index_ptr = zend_register_list_destructors_ex(NULL, NULL, "index pointer", 0);
2155
2156
  /* Register constants */
2157
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_VERSION", PHP_VERSION, sizeof(PHP_VERSION)-1, CONST_PERSISTENT | CONST_CS);
2158
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_MAJOR_VERSION", PHP_MAJOR_VERSION, CONST_PERSISTENT | CONST_CS);
2159
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_MINOR_VERSION", PHP_MINOR_VERSION, CONST_PERSISTENT | CONST_CS);
2160
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_RELEASE_VERSION", PHP_RELEASE_VERSION, CONST_PERSISTENT | CONST_CS);
2161
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_EXTRA_VERSION", PHP_EXTRA_VERSION, sizeof(PHP_EXTRA_VERSION) - 1, CONST_PERSISTENT | CONST_CS);
2162
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_VERSION_ID", PHP_VERSION_ID, CONST_PERSISTENT | CONST_CS);
2163
#ifdef ZTS
2164
  REGISTER_MAIN_LONG_CONSTANT("PHP_ZTS", 1, CONST_PERSISTENT | CONST_CS);
2165
#else
2166
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_ZTS", 0, CONST_PERSISTENT | CONST_CS);
2167
3.54k
#endif
2168
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_DEBUG", PHP_DEBUG, CONST_PERSISTENT | CONST_CS);
2169
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_OS", php_os, strlen(php_os), CONST_PERSISTENT | CONST_CS);
2170
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_OS_FAMILY", PHP_OS_FAMILY, sizeof(PHP_OS_FAMILY)-1, CONST_PERSISTENT | CONST_CS);
2171
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_SAPI", sapi_module.name, strlen(sapi_module.name), CONST_PERSISTENT | CONST_CS | CONST_NO_FILE_CACHE);
2172
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("DEFAULT_INCLUDE_PATH", PHP_INCLUDE_PATH, sizeof(PHP_INCLUDE_PATH)-1, CONST_PERSISTENT | CONST_CS);
2173
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PEAR_INSTALL_DIR", PEAR_INSTALLDIR, sizeof(PEAR_INSTALLDIR)-1, CONST_PERSISTENT | CONST_CS);
2174
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PEAR_EXTENSION_DIR", PHP_EXTENSION_DIR, sizeof(PHP_EXTENSION_DIR)-1, CONST_PERSISTENT | CONST_CS);
2175
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_EXTENSION_DIR", PHP_EXTENSION_DIR, sizeof(PHP_EXTENSION_DIR)-1, CONST_PERSISTENT | CONST_CS);
2176
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_PREFIX", PHP_PREFIX, sizeof(PHP_PREFIX)-1, CONST_PERSISTENT | CONST_CS);
2177
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINDIR", PHP_BINDIR, sizeof(PHP_BINDIR)-1, CONST_PERSISTENT | CONST_CS);
2178
3.54k
#ifndef PHP_WIN32
2179
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_MANDIR", PHP_MANDIR, sizeof(PHP_MANDIR)-1, CONST_PERSISTENT | CONST_CS);
2180
3.54k
#endif
2181
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_LIBDIR", PHP_LIBDIR, sizeof(PHP_LIBDIR)-1, CONST_PERSISTENT | CONST_CS);
2182
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_DATADIR", PHP_DATADIR, sizeof(PHP_DATADIR)-1, CONST_PERSISTENT | CONST_CS);
2183
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_SYSCONFDIR", PHP_SYSCONFDIR, sizeof(PHP_SYSCONFDIR)-1, CONST_PERSISTENT | CONST_CS);
2184
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_LOCALSTATEDIR", PHP_LOCALSTATEDIR, sizeof(PHP_LOCALSTATEDIR)-1, CONST_PERSISTENT | CONST_CS);
2185
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_PATH", PHP_CONFIG_FILE_PATH, strlen(PHP_CONFIG_FILE_PATH), CONST_PERSISTENT | CONST_CS);
2186
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_CONFIG_FILE_SCAN_DIR", PHP_CONFIG_FILE_SCAN_DIR, sizeof(PHP_CONFIG_FILE_SCAN_DIR)-1, CONST_PERSISTENT | CONST_CS);
2187
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_SHLIB_SUFFIX", PHP_SHLIB_SUFFIX, sizeof(PHP_SHLIB_SUFFIX)-1, CONST_PERSISTENT | CONST_CS);
2188
3.54k
  REGISTER_MAIN_STRINGL_CONSTANT("PHP_EOL", PHP_EOL, sizeof(PHP_EOL)-1, CONST_PERSISTENT | CONST_CS);
2189
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_MAXPATHLEN", MAXPATHLEN, CONST_PERSISTENT | CONST_CS);
2190
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_INT_MAX", ZEND_LONG_MAX, CONST_PERSISTENT | CONST_CS);
2191
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_INT_MIN", ZEND_LONG_MIN, CONST_PERSISTENT | CONST_CS);
2192
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_INT_SIZE", SIZEOF_ZEND_LONG, CONST_PERSISTENT | CONST_CS);
2193
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_FD_SETSIZE", FD_SETSIZE, CONST_PERSISTENT | CONST_CS);
2194
3.54k
  REGISTER_MAIN_LONG_CONSTANT("PHP_FLOAT_DIG", DBL_DIG, CONST_PERSISTENT | CONST_CS);
2195
3.54k
  REGISTER_MAIN_DOUBLE_CONSTANT("PHP_FLOAT_EPSILON", DBL_EPSILON, CONST_PERSISTENT | CONST_CS);
2196
3.54k
  REGISTER_MAIN_DOUBLE_CONSTANT("PHP_FLOAT_MAX", DBL_MAX, CONST_PERSISTENT | CONST_CS);
2197
3.54k
  REGISTER_MAIN_DOUBLE_CONSTANT("PHP_FLOAT_MIN", DBL_MIN, CONST_PERSISTENT | CONST_CS);
2198
2199
#ifdef PHP_WIN32
2200
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_MAJOR",      EG(windows_version_info).dwMajorVersion, CONST_PERSISTENT | CONST_CS);
2201
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_MINOR",      EG(windows_version_info).dwMinorVersion, CONST_PERSISTENT | CONST_CS);
2202
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_BUILD",      EG(windows_version_info).dwBuildNumber, CONST_PERSISTENT | CONST_CS);
2203
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_PLATFORM",   EG(windows_version_info).dwPlatformId, CONST_PERSISTENT | CONST_CS);
2204
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_SP_MAJOR",   EG(windows_version_info).wServicePackMajor, CONST_PERSISTENT | CONST_CS);
2205
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_SP_MINOR",   EG(windows_version_info).wServicePackMinor, CONST_PERSISTENT | CONST_CS);
2206
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_SUITEMASK",  EG(windows_version_info).wSuiteMask, CONST_PERSISTENT | CONST_CS);
2207
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_VERSION_PRODUCTTYPE", EG(windows_version_info).wProductType, CONST_PERSISTENT | CONST_CS);
2208
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_DOMAIN_CONTROLLER", VER_NT_DOMAIN_CONTROLLER, CONST_PERSISTENT | CONST_CS);
2209
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_SERVER", VER_NT_SERVER, CONST_PERSISTENT | CONST_CS);
2210
  REGISTER_MAIN_LONG_CONSTANT("PHP_WINDOWS_NT_WORKSTATION", VER_NT_WORKSTATION, CONST_PERSISTENT | CONST_CS);
2211
#endif
2212
2213
3.54k
  php_binary_init();
2214
3.54k
  if (PG(php_binary)) {
2215
0
    REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINARY", PG(php_binary), strlen(PG(php_binary)), CONST_PERSISTENT | CONST_CS | CONST_NO_FILE_CACHE);
2216
3.54k
  } else {
2217
3.54k
    REGISTER_MAIN_STRINGL_CONSTANT("PHP_BINARY", "", 0, CONST_PERSISTENT | CONST_CS | CONST_NO_FILE_CACHE);
2218
3.54k
  }
2219
2220
3.54k
  php_output_register_constants();
2221
3.54k
  php_rfc1867_register_constants();
2222
2223
  /* this will read in php.ini, set up the configuration parameters,
2224
     load zend extensions and register php function extensions
2225
     to be loaded later */
2226
3.54k
  if (php_init_config() == FAILURE) {
2227
0
    return FAILURE;
2228
0
  }
2229
2230
  /* Register PHP core ini entries */
2231
3.54k
  REGISTER_INI_ENTRIES();
2232
2233
  /* Register Zend ini entries */
2234
3.54k
  zend_register_standard_ini_entries();
2235
2236
#ifdef ZEND_WIN32
2237
  /* Until the current ini values was setup, the current cp is 65001.
2238
    If the actual ini values are different, some stuff needs to be updated.
2239
    It concerns at least main_cwd_state and there might be more. As we're
2240
    still in the startup phase, lets use the chance and reinit the relevant
2241
    item according to the current codepage. Still, if ini_set() is used
2242
    later on, a more intelligent way to update such stuff is needed.
2243
    Startup/shutdown routines could involve touching globals and thus
2244
    can't always be used on demand. */
2245
  if (!php_win32_cp_use_unicode()) {
2246
    virtual_cwd_main_cwd_init(1);
2247
  }
2248
#endif
2249
2250
  /* Disable realpath cache if an open_basedir is set */
2251
3.54k
  if (PG(open_basedir) && *PG(open_basedir)) {
2252
0
    CWDG(realpath_cache_size_limit) = 0;
2253
0
  }
2254
2255
3.54k
  PG(have_called_openlog) = 0;
2256
2257
  /* initialize stream wrappers registry
2258
   * (this uses configuration parameters from php.ini)
2259
   */
2260
3.54k
  if (php_init_stream_wrappers(module_number) == FAILURE)  {
2261
0
    php_printf("PHP:  Unable to initialize stream url wrappers.\n");
2262
0
    return FAILURE;
2263
0
  }
2264
2265
3.54k
  zuv.html_errors = 1;
2266
3.54k
  php_startup_auto_globals();
2267
3.54k
  zend_set_utility_values(&zuv);
2268
3.54k
  php_startup_sapi_content_types();
2269
2270
  /* startup extensions statically compiled in */
2271
3.54k
  if (php_register_internal_extensions_func() == FAILURE) {
2272
0
    php_printf("Unable to start builtin modules\n");
2273
0
    return FAILURE;
2274
0
  }
2275
2276
  /* start additional PHP extensions */
2277
3.54k
  php_register_extensions_bc(additional_modules, num_additional_modules);
2278
2279
  /* load and startup extensions compiled as shared objects (aka DLLs)
2280
     as requested by php.ini entries
2281
     these are loaded after initialization of internal extensions
2282
     as extensions *might* rely on things from ext/standard
2283
     which is always an internal extension and to be initialized
2284
     ahead of all other internals
2285
   */
2286
3.54k
  php_ini_register_extensions();
2287
3.54k
  zend_startup_modules();
2288
2289
  /* start Zend extensions */
2290
3.54k
  zend_startup_extensions();
2291
2292
3.54k
  zend_collect_module_handlers();
2293
2294
  /* register additional functions */
2295
3.54k
  if (sapi_module.additional_functions) {
2296
0
    if ((module = zend_hash_str_find_ptr(&module_registry, "standard", sizeof("standard")-1)) != NULL) {
2297
0
      EG(current_module) = module;
2298
0
      zend_register_functions(NULL, sapi_module.additional_functions, NULL, MODULE_PERSISTENT);
2299
0
      EG(current_module) = NULL;
2300
0
    }
2301
0
  }
2302
2303
  /* disable certain classes and functions as requested by php.ini */
2304
3.54k
  php_disable_functions();
2305
3.54k
  php_disable_classes();
2306
2307
  /* make core report what it should */
2308
3.54k
  if ((module = zend_hash_str_find_ptr(&module_registry, "core", sizeof("core")-1)) != NULL) {
2309
3.54k
    module->version = PHP_VERSION;
2310
3.54k
    module->info_func = PHP_MINFO(php_core);
2311
3.54k
  }
2312
2313
3.54k
  module_initialized = 1;
2314
2315
3.54k
  if (zend_post_startup() != SUCCESS) {
2316
0
    return FAILURE;
2317
0
  }
2318
2319
  /* Check for deprecated directives */
2320
  /* NOTE: If you add anything here, remember to add it to build/Makefile.global! */
2321
3.54k
  {
2322
3.54k
    struct {
2323
3.54k
      const long error_level;
2324
3.54k
      const char *phrase;
2325
3.54k
      const char *directives[18]; /* Remember to change this if the number of directives change */
2326
3.54k
    } directives[2] = {
2327
3.54k
      {
2328
3.54k
        E_DEPRECATED,
2329
3.54k
        "Directive '%s' is deprecated",
2330
3.54k
        {
2331
3.54k
          "allow_url_include",
2332
3.54k
          NULL
2333
3.54k
        }
2334
3.54k
      },
2335
3.54k
      {
2336
3.54k
        E_CORE_ERROR,
2337
3.54k
        "Directive '%s' is no longer available in PHP",
2338
3.54k
        {
2339
3.54k
          "allow_call_time_pass_reference",
2340
3.54k
          "asp_tags",
2341
3.54k
          "define_syslog_variables",
2342
3.54k
          "highlight.bg",
2343
3.54k
          "magic_quotes_gpc",
2344
3.54k
          "magic_quotes_runtime",
2345
3.54k
          "magic_quotes_sybase",
2346
3.54k
          "register_globals",
2347
3.54k
          "register_long_arrays",
2348
3.54k
          "safe_mode",
2349
3.54k
          "safe_mode_gid",
2350
3.54k
          "safe_mode_include_dir",
2351
3.54k
          "safe_mode_exec_dir",
2352
3.54k
          "safe_mode_allowed_env_vars",
2353
3.54k
          "safe_mode_protected_env_vars",
2354
3.54k
          "zend.ze1_compatibility_mode",
2355
3.54k
          "track_errors",
2356
3.54k
          NULL
2357
3.54k
        }
2358
3.54k
      }
2359
3.54k
    };
2360
2361
3.54k
    unsigned int i;
2362
2363
3.54k
    zend_try {
2364
      /* 2 = Count of deprecation structs */
2365
10.6k
      for (i = 0; i < 2; i++) {
2366
7.08k
        const char **p = directives[i].directives;
2367
2368
70.8k
        while(*p) {
2369
63.7k
          zend_long value;
2370
2371
63.7k
          if (cfg_get_long((char*)*p, &value) == SUCCESS && value) {
2372
0
            zend_error(directives[i].error_level, directives[i].phrase, *p);
2373
0
          }
2374
2375
63.7k
          ++p;
2376
63.7k
        }
2377
7.08k
      }
2378
3.54k
    } zend_catch {
2379
0
      retval = FAILURE;
2380
0
    } zend_end_try();
2381
3.54k
  }
2382
2383
3.54k
  virtual_cwd_deactivate();
2384
2385
3.54k
  sapi_deactivate();
2386
3.54k
  module_startup = 0;
2387
2388
  /* Don't leak errors from startup into the per-request phase. */
2389
3.54k
  clear_last_error();
2390
3.54k
  shutdown_memory_manager(1, 0);
2391
3.54k
  virtual_cwd_activate();
2392
2393
3.54k
  zend_interned_strings_switch_storage(1);
2394
2395
#if ZEND_RC_DEBUG
2396
  zend_rc_debug = 1;
2397
#endif
2398
2399
  /* we're done */
2400
3.54k
  return retval;
2401
3.54k
}
2402
/* }}} */
2403
2404
/* {{{ php_module_shutdown_wrapper
2405
 */
2406
int php_module_shutdown_wrapper(sapi_module_struct *sapi_globals)
2407
0
{
2408
0
  php_module_shutdown();
2409
0
  return SUCCESS;
2410
0
}
2411
/* }}} */
2412
2413
/* {{{ php_module_shutdown
2414
 */
2415
void php_module_shutdown(void)
2416
0
{
2417
0
  int module_number=0;  /* for UNREGISTER_INI_ENTRIES() */
2418
2419
0
  module_shutdown = 1;
2420
2421
0
  if (!module_initialized) {
2422
0
    return;
2423
0
  }
2424
2425
0
  zend_interned_strings_switch_storage(0);
2426
2427
#if ZEND_RC_DEBUG
2428
  zend_rc_debug = 0;
2429
#endif
2430
2431
#ifdef PHP_WIN32
2432
  (void)php_win32_shutdown_random_bytes();
2433
  php_win32_signal_ctrl_handler_shutdown();
2434
#endif
2435
2436
0
  sapi_flush();
2437
2438
0
  zend_shutdown();
2439
2440
#ifdef PHP_WIN32
2441
  /*close winsock */
2442
  WSACleanup();
2443
#endif
2444
2445
  /* Destroys filter & transport registries too */
2446
0
  php_shutdown_stream_wrappers(module_number);
2447
2448
0
  UNREGISTER_INI_ENTRIES();
2449
2450
  /* close down the ini config */
2451
0
  php_shutdown_config();
2452
0
  clear_last_error();
2453
2454
0
#ifndef ZTS
2455
0
  zend_ini_shutdown();
2456
0
  shutdown_memory_manager(CG(unclean_shutdown), 1);
2457
#else
2458
  zend_ini_global_shutdown();
2459
#endif
2460
2461
0
  php_output_shutdown();
2462
2463
0
#ifndef ZTS
2464
0
  zend_interned_strings_dtor();
2465
0
#endif
2466
2467
0
  if (zend_post_shutdown_cb) {
2468
0
    void (*cb)(void) = zend_post_shutdown_cb;
2469
2470
0
    zend_post_shutdown_cb = NULL;
2471
0
    cb();
2472
0
  }
2473
2474
0
  module_initialized = 0;
2475
2476
0
#ifndef ZTS
2477
0
  core_globals_dtor(&core_globals);
2478
0
  gc_globals_dtor();
2479
#else
2480
  ts_free_id(core_globals_id);
2481
#endif
2482
2483
#ifdef PHP_WIN32
2484
  if (old_invalid_parameter_handler == NULL) {
2485
    _set_invalid_parameter_handler(old_invalid_parameter_handler);
2486
  }
2487
#endif
2488
0
}
2489
/* }}} */
2490
2491
/* {{{ php_execute_script
2492
 */
2493
PHPAPI int php_execute_script(zend_file_handle *primary_file)
2494
0
{
2495
0
  zend_file_handle *prepend_file_p, *append_file_p;
2496
0
  zend_file_handle prepend_file, append_file;
2497
#ifdef HAVE_BROKEN_GETCWD
2498
  volatile int old_cwd_fd = -1;
2499
#else
2500
0
  char *old_cwd;
2501
0
  ALLOCA_FLAG(use_heap)
2502
0
#endif
2503
0
  int retval = 0;
2504
2505
0
  EG(exit_status) = 0;
2506
0
#ifndef HAVE_BROKEN_GETCWD
2507
0
# define OLD_CWD_SIZE 4096
2508
0
  old_cwd = do_alloca(OLD_CWD_SIZE, use_heap);
2509
0
  old_cwd[0] = '\0';
2510
0
#endif
2511
2512
0
  zend_try {
2513
0
    char realfile[MAXPATHLEN];
2514
2515
#ifdef PHP_WIN32
2516
    if(primary_file->filename) {
2517
      UpdateIniFromRegistry((char*)primary_file->filename);
2518
    }
2519
#endif
2520
2521
0
    PG(during_request_startup) = 0;
2522
2523
0
    if (primary_file->filename && !(SG(options) & SAPI_OPTION_NO_CHDIR)) {
2524
#ifdef HAVE_BROKEN_GETCWD
2525
      /* this looks nasty to me */
2526
      old_cwd_fd = open(".", 0);
2527
#else
2528
0
      php_ignore_value(VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1));
2529
0
#endif
2530
0
      VCWD_CHDIR_FILE(primary_file->filename);
2531
0
    }
2532
2533
    /* Only lookup the real file path and add it to the included_files list if already opened
2534
     *   otherwise it will get opened and added to the included_files list in zend_execute_scripts
2535
     */
2536
0
    if (primary_file->filename &&
2537
0
        strcmp("Standard input code", primary_file->filename) &&
2538
0
      primary_file->opened_path == NULL &&
2539
0
      primary_file->type != ZEND_HANDLE_FILENAME
2540
0
    ) {
2541
0
      if (expand_filepath(primary_file->filename, realfile)) {
2542
0
        primary_file->opened_path = zend_string_init(realfile, strlen(realfile), 0);
2543
0
        zend_hash_add_empty_element(&EG(included_files), primary_file->opened_path);
2544
0
      }
2545
0
    }
2546
2547
0
    if (PG(auto_prepend_file) && PG(auto_prepend_file)[0]) {
2548
0
      zend_stream_init_filename(&prepend_file, PG(auto_prepend_file));
2549
0
      prepend_file_p = &prepend_file;
2550
0
    } else {
2551
0
      prepend_file_p = NULL;
2552
0
    }
2553
2554
0
    if (PG(auto_append_file) && PG(auto_append_file)[0]) {
2555
0
      zend_stream_init_filename(&append_file, PG(auto_append_file));
2556
0
      append_file_p = &append_file;
2557
0
    } else {
2558
0
      append_file_p = NULL;
2559
0
    }
2560
0
    if (PG(max_input_time) != -1) {
2561
#ifdef PHP_WIN32
2562
      zend_unset_timeout();
2563
#endif
2564
0
      zend_set_timeout(INI_INT("max_execution_time"), 0);
2565
0
    }
2566
2567
    /*
2568
       If cli primary file has shebang line and there is a prepend file,
2569
       the `skip_shebang` will be used by prepend file but not primary file,
2570
       save it and restore after prepend file been executed.
2571
     */
2572
0
    if (CG(skip_shebang) && prepend_file_p) {
2573
0
      CG(skip_shebang) = 0;
2574
0
      if (zend_execute_scripts(ZEND_REQUIRE, NULL, 1, prepend_file_p) == SUCCESS) {
2575
0
        CG(skip_shebang) = 1;
2576
0
        retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 2, primary_file, append_file_p) == SUCCESS);
2577
0
      }
2578
0
    } else {
2579
0
      retval = (zend_execute_scripts(ZEND_REQUIRE, NULL, 3, prepend_file_p, primary_file, append_file_p) == SUCCESS);
2580
0
    }
2581
0
  } zend_end_try();
2582
2583
0
  if (EG(exception)) {
2584
0
    zend_try {
2585
0
      zend_exception_error(EG(exception), E_ERROR);
2586
0
    } zend_end_try();
2587
0
  }
2588
2589
#ifdef HAVE_BROKEN_GETCWD
2590
  if (old_cwd_fd != -1) {
2591
    fchdir(old_cwd_fd);
2592
    close(old_cwd_fd);
2593
  }
2594
#else
2595
0
  if (old_cwd[0] != '\0') {
2596
0
    php_ignore_value(VCWD_CHDIR(old_cwd));
2597
0
  }
2598
0
  free_alloca(old_cwd, use_heap);
2599
0
#endif
2600
0
  return retval;
2601
0
}
2602
/* }}} */
2603
2604
/* {{{ php_execute_simple_script
2605
 */
2606
PHPAPI int php_execute_simple_script(zend_file_handle *primary_file, zval *ret)
2607
0
{
2608
0
  char *old_cwd;
2609
0
  ALLOCA_FLAG(use_heap)
2610
2611
0
  EG(exit_status) = 0;
2612
0
#define OLD_CWD_SIZE 4096
2613
0
  old_cwd = do_alloca(OLD_CWD_SIZE, use_heap);
2614
0
  old_cwd[0] = '\0';
2615
2616
0
  zend_try {
2617
#ifdef PHP_WIN32
2618
    if(primary_file->filename) {
2619
      UpdateIniFromRegistry((char*)primary_file->filename);
2620
    }
2621
#endif
2622
2623
0
    PG(during_request_startup) = 0;
2624
2625
0
    if (primary_file->filename && !(SG(options) & SAPI_OPTION_NO_CHDIR)) {
2626
0
      php_ignore_value(VCWD_GETCWD(old_cwd, OLD_CWD_SIZE-1));
2627
0
      VCWD_CHDIR_FILE(primary_file->filename);
2628
0
    }
2629
0
    zend_execute_scripts(ZEND_REQUIRE, ret, 1, primary_file);
2630
0
  } zend_end_try();
2631
2632
0
  if (old_cwd[0] != '\0') {
2633
0
    php_ignore_value(VCWD_CHDIR(old_cwd));
2634
0
  }
2635
2636
0
  free_alloca(old_cwd, use_heap);
2637
0
  return EG(exit_status);
2638
0
}
2639
/* }}} */
2640
2641
/* {{{ php_handle_aborted_connection
2642
 */
2643
PHPAPI void php_handle_aborted_connection(void)
2644
0
{
2645
2646
0
  PG(connection_status) = PHP_CONNECTION_ABORTED;
2647
0
  php_output_set_status(PHP_OUTPUT_DISABLED);
2648
2649
0
  if (!PG(ignore_user_abort)) {
2650
0
    zend_bailout();
2651
0
  }
2652
0
}
2653
/* }}} */
2654
2655
/* {{{ php_handle_auth_data
2656
 */
2657
PHPAPI int php_handle_auth_data(const char *auth)
2658
{
2659
  int ret = -1;
2660
2661
  if (auth && auth[0] != '\0' && strncmp(auth, "Basic ", 6) == 0) {
2662
    char *pass;
2663
    zend_string *user;
2664
2665
    user = php_base64_decode((const unsigned char*)auth + 6, strlen(auth) - 6);
2666
    if (user) {
2667
      pass = strchr(ZSTR_VAL(user), ':');
2668
      if (pass) {
2669
        *pass++ = '\0';
2670
        SG(request_info).auth_user = estrndup(ZSTR_VAL(user), ZSTR_LEN(user));
2671
        SG(request_info).auth_password = estrdup(pass);
2672
        ret = 0;
2673
      }
2674
      zend_string_free(user);
2675
    }
2676
  }
2677
2678
  if (ret == -1) {
2679
    SG(request_info).auth_user = SG(request_info).auth_password = NULL;
2680
  } else {
2681
    SG(request_info).auth_digest = NULL;
2682
  }
2683
2684
  if (ret == -1 && auth && auth[0] != '\0' && strncmp(auth, "Digest ", 7) == 0) {
2685
    SG(request_info).auth_digest = estrdup(auth + 7);
2686
    ret = 0;
2687
  }
2688
2689
  if (ret == -1) {
2690
    SG(request_info).auth_digest = NULL;
2691
  }
2692
2693
  return ret;
2694
}
2695
/* }}} */
2696
2697
/* {{{ php_lint_script
2698
 */
2699
PHPAPI int php_lint_script(zend_file_handle *file)
2700
0
{
2701
0
  zend_op_array *op_array;
2702
0
  int retval = FAILURE;
2703
2704
0
  zend_try {
2705
0
    op_array = zend_compile_file(file, ZEND_INCLUDE);
2706
0
    zend_destroy_file_handle(file);
2707
2708
0
    if (op_array) {
2709
0
      destroy_op_array(op_array);
2710
0
      efree(op_array);
2711
0
      retval = SUCCESS;
2712
0
    }
2713
0
  } zend_end_try();
2714
0
  if (EG(exception)) {
2715
0
    zend_exception_error(EG(exception), E_ERROR);
2716
0
  }
2717
2718
0
  return retval;
2719
0
}
2720
/* }}} */
2721
2722
#ifdef ZTS
2723
/* {{{ php_reserve_tsrm_memory
2724
 */
2725
PHPAPI void php_reserve_tsrm_memory(void)
2726
{
2727
  tsrm_reserve(
2728
    TSRM_ALIGNED_SIZE(sizeof(zend_compiler_globals)) +
2729
    TSRM_ALIGNED_SIZE(sizeof(zend_executor_globals)) +
2730
    TSRM_ALIGNED_SIZE(sizeof(zend_php_scanner_globals)) +
2731
    TSRM_ALIGNED_SIZE(sizeof(zend_ini_scanner_globals)) +
2732
    TSRM_ALIGNED_SIZE(sizeof(virtual_cwd_globals)) +
2733
#ifdef ZEND_SIGNALS
2734
    TSRM_ALIGNED_SIZE(sizeof(zend_signal_globals_t)) +
2735
#endif
2736
    TSRM_ALIGNED_SIZE(zend_mm_globals_size()) +
2737
    TSRM_ALIGNED_SIZE(zend_gc_globals_size()) +
2738
    TSRM_ALIGNED_SIZE(sizeof(php_core_globals)) +
2739
    TSRM_ALIGNED_SIZE(sizeof(sapi_globals_struct))
2740
  );
2741
}
2742
/* }}} */
2743
2744
/* {{{ php_tsrm_startup
2745
 */
2746
PHPAPI int php_tsrm_startup(void)
2747
{
2748
  int ret = tsrm_startup(1, 1, 0, NULL);
2749
  php_reserve_tsrm_memory();
2750
  (void)ts_resource(0);
2751
  return ret;
2752
}
2753
/* }}} */
2754
#endif