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: 91 137 66.4 %
Date: 2015-05-21 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       20702 : PHPAPI int php_header(void)
      71             : {
      72       20702 :         if (sapi_send_headers()==FAILURE || SG(request_info).headers_only) {
      73           0 :                 return 0; /* don't allow output */
      74             :         } else {
      75       20702 :                 return 1; /* allow output */
      76             :         }
      77             : }
      78             : 
      79             : 
      80          12 : PHPAPI int php_setcookie(char *name, size_t name_len, char *value, size_t value_len, time_t expires, char *path, size_t path_len, char *domain, size_t domain_len, int secure, int url_encode, int httponly)
      81             : {
      82             :         char *cookie;
      83          12 :         size_t len=sizeof("Set-Cookie: ");
      84             :         zend_string *dt;
      85          12 :         sapi_header_line ctr = {0};
      86             :         int result;
      87          12 :         zend_string *encoded_value = NULL;
      88             : 
      89          12 :         if (!name_len) {
      90           1 :                 zend_error( E_WARNING, "Cookie names must not be empty" );
      91           1 :                 return FAILURE;
      92          11 :         } else if (name && strpbrk(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          11 :         if (!url_encode && value && strpbrk(value, ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
      98           0 :                 zend_error( E_WARNING, "Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
      99           0 :                 return FAILURE;
     100             :         }
     101             : 
     102          11 :         len += name_len;
     103          21 :         if (value && url_encode) {
     104          10 :                 encoded_value = php_url_encode(value, value_len);
     105          10 :                 len += encoded_value->len;
     106           1 :         } else if (value) {
     107           0 :                 encoded_value = zend_string_init(value, value_len, 0);
     108           0 :                 len += encoded_value->len;
     109             :         }
     110             : 
     111          11 :         if (path) {
     112           4 :                 len += path_len;
     113             :         }
     114          11 :         if (domain) {
     115           3 :                 len += domain_len;
     116             :         }
     117             : 
     118          11 :         cookie = emalloc(len + 100);
     119             : 
     120          11 :         if (value && value_len == 0) {
     121             :                 /*
     122             :                  * MSIE doesn't delete a cookie when you set it to a null value
     123             :                  * so in order to force cookies to be deleted, even on MSIE, we
     124             :                  * pick an expiry date in the past
     125             :                  */
     126           0 :                 dt = php_format_date("D, d-M-Y H:i:s T", sizeof("D, d-M-Y H:i:s T")-1, 1, 0);
     127           0 :                 snprintf(cookie, len + 100, "Set-Cookie: %s=deleted; expires=%s; Max-Age=0", name, dt->val);
     128             :                 zend_string_free(dt);
     129             :         } else {
     130          11 :                 snprintf(cookie, len + 100, "Set-Cookie: %s=%s", name, value ? encoded_value->val : "");
     131          11 :                 if (expires > 0) {
     132             :                         const char *p;
     133             :                         char tsdelta[13];
     134           3 :                         strlcat(cookie, COOKIE_EXPIRES, len + 100);
     135           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);
     136             :                         /* check to make sure that the year does not exceed 4 digits in length */
     137           6 :                         p = zend_memrchr(dt->val, '-', dt->len);
     138           3 :                         if (!p || *(p + 5) != ' ') {
     139             :                                 zend_string_free(dt);
     140           0 :                                 efree(cookie);
     141             :                                 zend_string_free(encoded_value);
     142           0 :                                 zend_error(E_WARNING, "Expiry date cannot have a year greater than 9999");
     143           0 :                                 return FAILURE;
     144             :                         }
     145           3 :                         strlcat(cookie, dt->val, len + 100);
     146             :                         zend_string_free(dt);
     147             : 
     148           3 :                         snprintf(tsdelta, sizeof(tsdelta), ZEND_LONG_FMT, (zend_long) difftime(expires, time(NULL)));
     149           3 :                         strlcat(cookie, COOKIE_MAX_AGE, len + 100);
     150           3 :                         strlcat(cookie, tsdelta, len + 100);
     151             :                 }
     152             :         }
     153             : 
     154          11 :         if (encoded_value) {
     155             :                 zend_string_free(encoded_value);
     156             :         }
     157             : 
     158          11 :         if (path && path_len > 0) {
     159           1 :                 strlcat(cookie, COOKIE_PATH, len + 100);
     160           1 :                 strlcat(cookie, path, len + 100);
     161             :         }
     162          11 :         if (domain && domain_len > 0) {
     163           1 :                 strlcat(cookie, COOKIE_DOMAIN, len + 100);
     164           1 :                 strlcat(cookie, domain, len + 100);
     165             :         }
     166          11 :         if (secure) {
     167           1 :                 strlcat(cookie, COOKIE_SECURE, len + 100);
     168             :         }
     169          11 :         if (httponly) {
     170           1 :                 strlcat(cookie, COOKIE_HTTPONLY, len + 100);
     171             :         }
     172             : 
     173          11 :         ctr.line = cookie;
     174          11 :         ctr.line_len = (uint)strlen(cookie);
     175             : 
     176          11 :         result = sapi_header_op(SAPI_HEADER_ADD, &ctr);
     177          11 :         efree(cookie);
     178          11 :         return result;
     179             : }
     180             : 
     181             : 
     182             : /* php_set_cookie(name, value, expires, path, domain, secure) */
     183             : /* {{{ proto bool setcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
     184             :    Send a cookie */
     185          12 : PHP_FUNCTION(setcookie)
     186             : {
     187          12 :         char *name, *value = NULL, *path = NULL, *domain = NULL;
     188          12 :         zend_long expires = 0;
     189          12 :         zend_bool secure = 0, httponly = 0;
     190          12 :         size_t name_len, value_len = 0, path_len = 0, domain_len = 0;
     191             : 
     192          12 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|slssbb", &name,
     193             :                                                           &name_len, &value, &value_len, &expires, &path,
     194             :                                                           &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
     195           0 :                 return;
     196             :         }
     197             : 
     198          12 :         if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 1, httponly) == SUCCESS) {
     199          11 :                 RETVAL_TRUE;
     200             :         } else {
     201           1 :                 RETVAL_FALSE;
     202             :         }
     203             : }
     204             : /* }}} */
     205             : 
     206             : /* {{{ proto bool setrawcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
     207             :    Send a cookie with no url encoding of the value */
     208           0 : PHP_FUNCTION(setrawcookie)
     209             : {
     210           0 :         char *name, *value = NULL, *path = NULL, *domain = NULL;
     211           0 :         zend_long expires = 0;
     212           0 :         zend_bool secure = 0, httponly = 0;
     213           0 :         size_t name_len, value_len = 0, path_len = 0, domain_len = 0;
     214             : 
     215           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|slssbb", &name,
     216             :                                                           &name_len, &value, &value_len, &expires, &path,
     217             :                                                           &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
     218           0 :                 return;
     219             :         }
     220             : 
     221           0 :         if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 0, httponly) == SUCCESS) {
     222           0 :                 RETVAL_TRUE;
     223             :         } else {
     224           0 :                 RETVAL_FALSE;
     225             :         }
     226             : }
     227             : /* }}} */
     228             : 
     229             : 
     230             : /* {{{ proto bool headers_sent([string &$file [, int &$line]])
     231             :    Returns true if headers have already been sent, false otherwise */
     232           4 : PHP_FUNCTION(headers_sent)
     233             : {
     234           4 :         zval *arg1 = NULL, *arg2 = NULL;
     235           4 :         const char *file="";
     236           4 :         int line=0;
     237             : 
     238           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z/z/", &arg1, &arg2) == FAILURE)
     239           0 :                 return;
     240             : 
     241           4 :         if (SG(headers_sent)) {
     242           2 :                 line = php_output_get_start_lineno();
     243           2 :                 file = php_output_get_start_filename();
     244             :         }
     245             : 
     246           4 :         switch(ZEND_NUM_ARGS()) {
     247             :         case 2:
     248           0 :                 zval_dtor(arg2);
     249           0 :                 ZVAL_LONG(arg2, line);
     250             :         case 1:
     251           0 :                 zval_dtor(arg1);
     252           0 :                 if (file) {
     253           0 :                         ZVAL_STRING(arg1, file);
     254             :                 } else {
     255           0 :                         ZVAL_EMPTY_STRING(arg1);
     256             :                 }
     257             :                 break;
     258             :         }
     259             : 
     260           4 :         if (SG(headers_sent)) {
     261           2 :                 RETURN_TRUE;
     262             :         } else {
     263           2 :                 RETURN_FALSE;
     264             :         }
     265             : }
     266             : /* }}} */
     267             : 
     268             : /* {{{ php_head_apply_header_list_to_hash
     269             :    Turn an llist of sapi_header_struct headers into a numerically indexed zval hash */
     270          21 : static void php_head_apply_header_list_to_hash(void *data, void *arg)
     271             : {
     272          21 :         sapi_header_struct *sapi_header = (sapi_header_struct *)data;
     273             : 
     274          21 :         if (arg && sapi_header) {
     275          21 :                 add_next_index_string((zval *)arg, (char *)(sapi_header->header));
     276             :         }
     277          21 : }
     278             : 
     279             : /* {{{ proto array headers_list(void)
     280             :    Return list of headers to be sent / already sent */
     281           8 : PHP_FUNCTION(headers_list)
     282             : {
     283           8 :         if (zend_parse_parameters_none() == FAILURE) {
     284           0 :                 return;
     285             :         }
     286             : 
     287           8 :         array_init(return_value);
     288           8 :         zend_llist_apply_with_argument(&SG(sapi_headers).headers, php_head_apply_header_list_to_hash, return_value);
     289             : }
     290             : /* }}} */
     291             : 
     292             : /* {{{ proto long http_response_code([int response_code])
     293             :    Sets a response code, or returns the current HTTP response code */
     294           0 : PHP_FUNCTION(http_response_code)
     295             : {
     296           0 :         zend_long response_code = 0;
     297             : 
     298           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &response_code) == FAILURE) {
     299           0 :                 return;
     300             :         }
     301             : 
     302           0 :         if (response_code)
     303             :         {
     304             :                 zend_long old_response_code;
     305             : 
     306           0 :                 old_response_code = SG(sapi_headers).http_response_code;
     307           0 :                 SG(sapi_headers).http_response_code = (int)response_code;
     308             : 
     309           0 :                 if (old_response_code) {
     310           0 :                         RETURN_LONG(old_response_code);
     311             :                 }
     312             : 
     313           0 :                 RETURN_TRUE;
     314             :         }
     315             : 
     316           0 :         if (!SG(sapi_headers).http_response_code) {
     317           0 :                 RETURN_FALSE;
     318             :         }
     319             : 
     320           0 :         RETURN_LONG(SG(sapi_headers).http_response_code);
     321             : }
     322             : /* }}} */
     323             : 
     324             : /*
     325             :  * Local variables:
     326             :  * tab-width: 4
     327             :  * c-basic-offset: 4
     328             :  * vim600: sw=4 ts=4 fdm=marker
     329             :  * vim<600: sw=4 ts=4 * End:
     330             :  */

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:06 +0000 (42 hours ago)

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