PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/standard - head.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 92 136 67.6 %
Date: 2015-08-29 Functions: 8 10 80.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Rasmus Lerdorf <rasmus@lerdorf.on.ca>                        |
      16             :    +----------------------------------------------------------------------+
      17             :  */
      18             : /* $Id$ */
      19             : 
      20             : #include <stdio.h>
      21             : #include "php.h"
      22             : #include "ext/standard/php_standard.h"
      23             : #include "ext/date/php_date.h"
      24             : #include "SAPI.h"
      25             : #include "php_main.h"
      26             : #include "head.h"
      27             : #ifdef TM_IN_SYS_TIME
      28             : #include <sys/time.h>
      29             : #else
      30             : #include <time.h>
      31             : #endif
      32             : 
      33             : #include "php_globals.h"
      34             : 
      35             : 
      36             : /* Implementation of the language Header() function */
      37             : /* {{{ proto void header(string header [, bool replace, [int http_response_code]])
      38             :    Sends a raw HTTP header */
      39          68 : PHP_FUNCTION(header)
      40             : {
      41          68 :         zend_bool rep = 1;
      42          68 :         sapi_header_line ctr = {0};
      43             :         size_t len;
      44             : 
      45          68 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|bl", &ctr.line,
      46             :                                 &len, &rep, &ctr.response_code) == FAILURE)
      47           0 :                 return;
      48             : 
      49          68 :         ctr.line_len = (uint)len;
      50          68 :         sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD, &ctr);
      51             : }
      52             : /* }}} */
      53             : 
      54             : /* {{{ proto void header_remove([string name])
      55             :    Removes an HTTP header previously set using header() */
      56          10 : PHP_FUNCTION(header_remove)
      57             : {
      58          10 :         sapi_header_line ctr = {0};
      59          10 :         size_t len = 0;
      60             : 
      61          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &ctr.line,
      62             :                                   &len) == FAILURE)
      63           0 :                 return;
      64             : 
      65          10 :         ctr.line_len = (uint)len;
      66          10 :         sapi_header_op(ZEND_NUM_ARGS() == 0 ? SAPI_HEADER_DELETE_ALL : SAPI_HEADER_DELETE, &ctr);
      67             : }
      68             : /* }}} */
      69             : 
      70       21077 : PHPAPI int php_header(void)
      71             : {
      72       21077 :         if (sapi_send_headers()==FAILURE || SG(request_info).headers_only) {
      73           0 :                 return 0; /* don't allow output */
      74             :         } else {
      75       21077 :                 return 1; /* allow output */
      76             :         }
      77             : }
      78             : 
      79             : 
      80          13 : PHPAPI int php_setcookie(zend_string *name, zend_string *value, time_t expires, zend_string *path, zend_string *domain, int secure, int url_encode, int httponly)
      81             : {
      82             :         char *cookie;
      83          13 :         size_t len = sizeof("Set-Cookie: ");
      84             :         zend_string *dt;
      85          13 :         sapi_header_line ctr = {0};
      86             :         int result;
      87          13 :         zend_string *encoded_value = NULL;
      88             : 
      89          13 :         if (!ZSTR_LEN(name)) {
      90           1 :                 zend_error( E_WARNING, "Cookie names must not be empty" );
      91           1 :                 return FAILURE;
      92          12 :         } else if (strpbrk(ZSTR_VAL(name), "=,; \t\r\n\013\014") != NULL) {   /* man isspace for \013 and \014 */
      93           0 :                 zend_error(E_WARNING, "Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'" );
      94           0 :                 return FAILURE;
      95             :         }
      96             : 
      97          12 :         if (!url_encode && value &&
      98           0 :                         strpbrk(ZSTR_VAL(value), ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
      99           0 :                 zend_error(E_WARNING, "Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
     100           0 :                 return FAILURE;
     101             :         }
     102             : 
     103          12 :         len += ZSTR_LEN(name);
     104          12 :         if (value) {
     105          11 :                 if (url_encode) {
     106          11 :                         encoded_value = php_url_encode(ZSTR_VAL(value), ZSTR_LEN(value));
     107          11 :                         len += ZSTR_LEN(encoded_value);
     108             :                 } else {
     109           0 :                         encoded_value = zend_string_copy(value);
     110           0 :                         len += ZSTR_LEN(encoded_value);
     111             :                 }
     112             :         }
     113             : 
     114          12 :         if (path) {
     115           4 :                 len += ZSTR_LEN(path);
     116             :         }
     117          12 :         if (domain) {
     118           3 :                 len += ZSTR_LEN(domain);
     119             :         }
     120             : 
     121          12 :         cookie = emalloc(len + 100);
     122             : 
     123          14 :         if (value == NULL || ZSTR_LEN(value) == 0) {
     124             :                 /*
     125             :                  * MSIE doesn't delete a cookie when you set it to a null value
     126             :                  * so in order to force cookies to be deleted, even on MSIE, we
     127             :                  * pick an expiry date in the past
     128             :                  */
     129           2 :                 dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, 1, 0);
     130           2 :                 snprintf(cookie, len + 100, "Set-Cookie: %s=deleted; expires=%s; Max-Age=0", ZSTR_VAL(name), ZSTR_VAL(dt));
     131             :                 zend_string_free(dt);
     132             :         } else {
     133          10 :                 snprintf(cookie, len + 100, "Set-Cookie: %s=%s", ZSTR_VAL(name), value ? ZSTR_VAL(encoded_value) : "");
     134          10 :                 if (expires > 0) {
     135             :                         const char *p;
     136             :                         char tsdelta[13];
     137           3 :                         strlcat(cookie, COOKIE_EXPIRES, len + 100);
     138           3 :                         dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, expires, 0);
     139             :                         /* check to make sure that the year does not exceed 4 digits in length */
     140           6 :                         p = zend_memrchr(ZSTR_VAL(dt), '-', ZSTR_LEN(dt));
     141           3 :                         if (!p || *(p + 5) != ' ') {
     142             :                                 zend_string_free(dt);
     143           0 :                                 efree(cookie);
     144             :                                 zend_string_release(encoded_value);
     145           0 :                                 zend_error(E_WARNING, "Expiry date cannot have a year greater than 9999");
     146           0 :                                 return FAILURE;
     147             :                         }
     148           3 :                         strlcat(cookie, ZSTR_VAL(dt), len + 100);
     149             :                         zend_string_free(dt);
     150             : 
     151           3 :                         snprintf(tsdelta, sizeof(tsdelta), ZEND_LONG_FMT, (zend_long) difftime(expires, time(NULL)));
     152           3 :                         strlcat(cookie, COOKIE_MAX_AGE, len + 100);
     153           3 :                         strlcat(cookie, tsdelta, len + 100);
     154             :                 }
     155             :         }
     156             : 
     157          12 :         if (encoded_value) {
     158             :                 zend_string_release(encoded_value);
     159             :         }
     160             : 
     161          12 :         if (path && ZSTR_LEN(path)) {
     162           1 :                 strlcat(cookie, COOKIE_PATH, len + 100);
     163           1 :                 strlcat(cookie, ZSTR_VAL(path), len + 100);
     164             :         }
     165          12 :         if (domain && ZSTR_LEN(domain)) {
     166           1 :                 strlcat(cookie, COOKIE_DOMAIN, len + 100);
     167           1 :                 strlcat(cookie, ZSTR_VAL(domain), len + 100);
     168             :         }
     169          12 :         if (secure) {
     170           1 :                 strlcat(cookie, COOKIE_SECURE, len + 100);
     171             :         }
     172          12 :         if (httponly) {
     173           1 :                 strlcat(cookie, COOKIE_HTTPONLY, len + 100);
     174             :         }
     175             : 
     176          12 :         ctr.line = cookie;
     177          12 :         ctr.line_len = (uint)strlen(cookie);
     178             : 
     179          12 :         result = sapi_header_op(SAPI_HEADER_ADD, &ctr);
     180          12 :         efree(cookie);
     181          12 :         return result;
     182             : }
     183             : 
     184             : 
     185             : /* php_set_cookie(name, value, expires, path, domain, secure) */
     186             : /* {{{ proto bool setcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
     187             :    Send a cookie */
     188          13 : PHP_FUNCTION(setcookie)
     189             : {
     190          13 :         zend_string *name, *value = NULL, *path = NULL, *domain = NULL;
     191          13 :         zend_long expires = 0;
     192          13 :         zend_bool secure = 0, httponly = 0;
     193             : 
     194          13 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|SlSSbb",
     195             :                                 &name, &value, &expires, &path, &domain, &secure, &httponly) == FAILURE) {
     196           0 :                 return;
     197             :         }
     198             : 
     199          13 :         if (php_setcookie(name, value, expires, path, domain, secure, 1, httponly) == SUCCESS) {
     200          12 :                 RETVAL_TRUE;
     201             :         } else {
     202           1 :                 RETVAL_FALSE;
     203             :         }
     204             : }
     205             : /* }}} */
     206             : 
     207             : /* {{{ proto bool setrawcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
     208             :    Send a cookie with no url encoding of the value */
     209           0 : PHP_FUNCTION(setrawcookie)
     210             : {
     211           0 :         zend_string *name, *value = NULL, *path = NULL, *domain = NULL;
     212           0 :         zend_long expires = 0;
     213           0 :         zend_bool secure = 0, httponly = 0;
     214             : 
     215           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|SlSSbb",
     216             :                                 &name, &value, &expires, &path, &domain, &secure, &httponly) == FAILURE) {
     217           0 :                 return;
     218             :         }
     219             : 
     220           0 :         if (php_setcookie(name, value, expires, path, domain, secure, 0, httponly) == SUCCESS) {
     221           0 :                 RETVAL_TRUE;
     222             :         } else {
     223           0 :                 RETVAL_FALSE;
     224             :         }
     225             : }
     226             : /* }}} */
     227             : 
     228             : 
     229             : /* {{{ proto bool headers_sent([string &$file [, int &$line]])
     230             :    Returns true if headers have already been sent, false otherwise */
     231           4 : PHP_FUNCTION(headers_sent)
     232             : {
     233           4 :         zval *arg1 = NULL, *arg2 = NULL;
     234           4 :         const char *file="";
     235           4 :         int line=0;
     236             : 
     237           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z/z/", &arg1, &arg2) == FAILURE)
     238           0 :                 return;
     239             : 
     240           4 :         if (SG(headers_sent)) {
     241           2 :                 line = php_output_get_start_lineno();
     242           2 :                 file = php_output_get_start_filename();
     243             :         }
     244             : 
     245           4 :         switch(ZEND_NUM_ARGS()) {
     246             :         case 2:
     247           0 :                 zval_dtor(arg2);
     248           0 :                 ZVAL_LONG(arg2, line);
     249             :         case 1:
     250           0 :                 zval_dtor(arg1);
     251           0 :                 if (file) {
     252           0 :                         ZVAL_STRING(arg1, file);
     253             :                 } else {
     254           0 :                         ZVAL_EMPTY_STRING(arg1);
     255             :                 }
     256             :                 break;
     257             :         }
     258             : 
     259           4 :         if (SG(headers_sent)) {
     260           2 :                 RETURN_TRUE;
     261             :         } else {
     262           2 :                 RETURN_FALSE;
     263             :         }
     264             : }
     265             : /* }}} */
     266             : 
     267             : /* {{{ php_head_apply_header_list_to_hash
     268             :    Turn an llist of sapi_header_struct headers into a numerically indexed zval hash */
     269          22 : static void php_head_apply_header_list_to_hash(void *data, void *arg)
     270             : {
     271          22 :         sapi_header_struct *sapi_header = (sapi_header_struct *)data;
     272             : 
     273          22 :         if (arg && sapi_header) {
     274          22 :                 add_next_index_string((zval *)arg, (char *)(sapi_header->header));
     275             :         }
     276          22 : }
     277             : 
     278             : /* {{{ proto array headers_list(void)
     279             :    Return list of headers to be sent / already sent */
     280           8 : PHP_FUNCTION(headers_list)
     281             : {
     282           8 :         if (zend_parse_parameters_none() == FAILURE) {
     283           0 :                 return;
     284             :         }
     285             : 
     286           8 :         array_init(return_value);
     287           8 :         zend_llist_apply_with_argument(&SG(sapi_headers).headers, php_head_apply_header_list_to_hash, return_value);
     288             : }
     289             : /* }}} */
     290             : 
     291             : /* {{{ proto long http_response_code([int response_code])
     292             :    Sets a response code, or returns the current HTTP response code */
     293           0 : PHP_FUNCTION(http_response_code)
     294             : {
     295           0 :         zend_long response_code = 0;
     296             : 
     297           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &response_code) == FAILURE) {
     298           0 :                 return;
     299             :         }
     300             : 
     301           0 :         if (response_code)
     302             :         {
     303             :                 zend_long old_response_code;
     304             : 
     305           0 :                 old_response_code = SG(sapi_headers).http_response_code;
     306           0 :                 SG(sapi_headers).http_response_code = (int)response_code;
     307             : 
     308           0 :                 if (old_response_code) {
     309           0 :                         RETURN_LONG(old_response_code);
     310             :                 }
     311             : 
     312           0 :                 RETURN_TRUE;
     313             :         }
     314             : 
     315           0 :         if (!SG(sapi_headers).http_response_code) {
     316           0 :                 RETURN_FALSE;
     317             :         }
     318             : 
     319           0 :         RETURN_LONG(SG(sapi_headers).http_response_code);
     320             : }
     321             : /* }}} */
     322             : 
     323             : /*
     324             :  * Local variables:
     325             :  * tab-width: 4
     326             :  * c-basic-offset: 4
     327             :  * vim600: sw=4 ts=4 fdm=marker
     328             :  * vim<600: sw=4 ts=4 * End:
     329             :  */

Generated by: LCOV version 1.10

Generated at Sat, 29 Aug 2015 10:22:25 +0000 (4 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.