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: 88 135 65.2 %
Date: 2014-12-13 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-2014 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          71 : PHP_FUNCTION(header)
      40             : {
      41          71 :         zend_bool rep = 1;
      42          71 :         sapi_header_line ctr = {0};
      43             :         size_t len;
      44             : 
      45          71 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bl", &ctr.line,
      46             :                                 &len, &rep, &ctr.response_code) == FAILURE)
      47           0 :                 return;
      48             : 
      49          71 :         ctr.line_len = (uint)len;
      50          71 :         sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD, &ctr TSRMLS_CC);
      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() TSRMLS_CC, "|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 TSRMLS_CC);
      67             : }
      68             : /* }}} */
      69             : 
      70       20406 : PHPAPI int php_header(TSRMLS_D)
      71             : {
      72       20406 :         if (sapi_send_headers(TSRMLS_C)==FAILURE || SG(request_info).headers_only) {
      73           0 :                 return 0; /* don't allow output */
      74             :         } else {
      75       20406 :                 return 1; /* allow output */
      76             :         }
      77             : }
      78             : 
      79             : 
      80          11 : 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 TSRMLS_DC)
      81             : {
      82             :         char *cookie;
      83          11 :         size_t len=sizeof("Set-Cookie: ");
      84             :         zend_string *dt;
      85          11 :         sapi_header_line ctr = {0};
      86             :         int result;
      87          11 :         zend_string *encoded_value = NULL;
      88             : 
      89          11 :         if (name && strpbrk(name, "=,; \t\r\n\013\014") != NULL) {   /* man isspace for \013 and \014 */
      90           0 :                 zend_error( E_WARNING, "Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'" );
      91           0 :                 return FAILURE;
      92             :         }
      93             : 
      94          11 :         if (!url_encode && value && strpbrk(value, ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
      95           0 :                 zend_error( E_WARNING, "Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
      96           0 :                 return FAILURE;
      97             :         }
      98             : 
      99          11 :         len += name_len;
     100          21 :         if (value && url_encode) {
     101          10 :                 encoded_value = php_url_encode(value, value_len);
     102          10 :                 len += encoded_value->len;
     103           1 :         } else if (value) {
     104           0 :                 encoded_value = zend_string_init(value, value_len, 0);
     105           0 :                 len += encoded_value->len;
     106             :         }
     107             : 
     108          11 :         if (path) {
     109           4 :                 len += path_len;
     110             :         }
     111          11 :         if (domain) {
     112           3 :                 len += domain_len;
     113             :         }
     114             : 
     115          11 :         cookie = emalloc(len + 100);
     116             : 
     117          11 :         if (value && value_len == 0) {
     118             :                 /*
     119             :                  * MSIE doesn't delete a cookie when you set it to a null value
     120             :                  * so in order to force cookies to be deleted, even on MSIE, we
     121             :                  * pick an expiry date in the past
     122             :                  */
     123           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 TSRMLS_CC);
     124           0 :                 snprintf(cookie, len + 100, "Set-Cookie: %s=deleted; expires=%s; Max-Age=0", name, dt->val);
     125             :                 zend_string_free(dt);
     126             :         } else {
     127          11 :                 snprintf(cookie, len + 100, "Set-Cookie: %s=%s", name, value ? encoded_value->val : "");
     128          11 :                 if (expires > 0) {
     129             :                         const char *p;
     130             :                         char tsdelta[13];
     131           3 :                         strlcat(cookie, COOKIE_EXPIRES, len + 100);
     132           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 TSRMLS_CC);
     133             :                         /* check to make sure that the year does not exceed 4 digits in length */
     134           6 :                         p = zend_memrchr(dt->val, '-', dt->len);
     135           3 :                         if (!p || *(p + 5) != ' ') {
     136             :                                 zend_string_free(dt);
     137           0 :                                 efree(cookie);
     138             :                                 zend_string_free(encoded_value);
     139           0 :                                 zend_error(E_WARNING, "Expiry date cannot have a year greater than 9999");
     140           0 :                                 return FAILURE;
     141             :                         }
     142           3 :                         strlcat(cookie, dt->val, len + 100);
     143             :                         zend_string_free(dt);
     144             : 
     145           3 :                         snprintf(tsdelta, sizeof(tsdelta), ZEND_LONG_FMT, (zend_long) difftime(expires, time(NULL)));
     146           3 :                         strlcat(cookie, COOKIE_MAX_AGE, len + 100);
     147           3 :                         strlcat(cookie, tsdelta, len + 100);
     148             :                 }
     149             :         }
     150             : 
     151          11 :         if (encoded_value) {
     152             :                 zend_string_free(encoded_value);
     153             :         }
     154             : 
     155          11 :         if (path && path_len > 0) {
     156           1 :                 strlcat(cookie, COOKIE_PATH, len + 100);
     157           1 :                 strlcat(cookie, path, len + 100);
     158             :         }
     159          11 :         if (domain && domain_len > 0) {
     160           1 :                 strlcat(cookie, COOKIE_DOMAIN, len + 100);
     161           1 :                 strlcat(cookie, domain, len + 100);
     162             :         }
     163          11 :         if (secure) {
     164           1 :                 strlcat(cookie, COOKIE_SECURE, len + 100);
     165             :         }
     166          11 :         if (httponly) {
     167           1 :                 strlcat(cookie, COOKIE_HTTPONLY, len + 100);
     168             :         }
     169             : 
     170          11 :         ctr.line = cookie;
     171          11 :         ctr.line_len = (uint)strlen(cookie);
     172             : 
     173          11 :         result = sapi_header_op(SAPI_HEADER_ADD, &ctr TSRMLS_CC);
     174          11 :         efree(cookie);
     175          11 :         return result;
     176             : }
     177             : 
     178             : 
     179             : /* php_set_cookie(name, value, expires, path, domain, secure) */
     180             : /* {{{ proto bool setcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
     181             :    Send a cookie */
     182          11 : PHP_FUNCTION(setcookie)
     183             : {
     184          11 :         char *name, *value = NULL, *path = NULL, *domain = NULL;
     185          11 :         zend_long expires = 0;
     186          11 :         zend_bool secure = 0, httponly = 0;
     187          11 :         size_t name_len, value_len = 0, path_len = 0, domain_len = 0;
     188             : 
     189          11 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
     190             :                                                           &name_len, &value, &value_len, &expires, &path,
     191             :                                                           &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
     192           0 :                 return;
     193             :         }
     194             : 
     195          11 :         if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 1, httponly TSRMLS_CC) == SUCCESS) {
     196          11 :                 RETVAL_TRUE;
     197             :         } else {
     198           0 :                 RETVAL_FALSE;
     199             :         }
     200             : }
     201             : /* }}} */
     202             : 
     203             : /* {{{ proto bool setrawcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
     204             :    Send a cookie with no url encoding of the value */
     205           0 : PHP_FUNCTION(setrawcookie)
     206             : {
     207           0 :         char *name, *value = NULL, *path = NULL, *domain = NULL;
     208           0 :         zend_long expires = 0;
     209           0 :         zend_bool secure = 0, httponly = 0;
     210           0 :         size_t name_len, value_len = 0, path_len = 0, domain_len = 0;
     211             : 
     212           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
     213             :                                                           &name_len, &value, &value_len, &expires, &path,
     214             :                                                           &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
     215           0 :                 return;
     216             :         }
     217             : 
     218           0 :         if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 0, httponly TSRMLS_CC) == SUCCESS) {
     219           0 :                 RETVAL_TRUE;
     220             :         } else {
     221           0 :                 RETVAL_FALSE;
     222             :         }
     223             : }
     224             : /* }}} */
     225             : 
     226             : 
     227             : /* {{{ proto bool headers_sent([string &$file [, int &$line]])
     228             :    Returns true if headers have already been sent, false otherwise */
     229           4 : PHP_FUNCTION(headers_sent)
     230             : {
     231           4 :         zval *arg1 = NULL, *arg2 = NULL;
     232           4 :         const char *file="";
     233           4 :         int line=0;
     234             : 
     235           4 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z/z/", &arg1, &arg2) == FAILURE)
     236           0 :                 return;
     237             : 
     238           4 :         if (SG(headers_sent)) {
     239           2 :                 line = php_output_get_start_lineno(TSRMLS_C);
     240           2 :                 file = php_output_get_start_filename(TSRMLS_C);
     241             :         }
     242             : 
     243           4 :         switch(ZEND_NUM_ARGS()) {
     244             :         case 2:
     245           0 :                 zval_dtor(arg2);
     246           0 :                 ZVAL_LONG(arg2, line);
     247             :         case 1:
     248           0 :                 zval_dtor(arg1);
     249           0 :                 if (file) {
     250           0 :                         ZVAL_STRING(arg1, file);
     251             :                 } else {
     252           0 :                         ZVAL_EMPTY_STRING(arg1);
     253             :                 }
     254             :                 break;
     255             :         }
     256             : 
     257           4 :         if (SG(headers_sent)) {
     258           2 :                 RETURN_TRUE;
     259             :         } else {
     260           2 :                 RETURN_FALSE;
     261             :         }
     262             : }
     263             : /* }}} */
     264             : 
     265             : /* {{{ php_head_apply_header_list_to_hash
     266             :    Turn an llist of sapi_header_struct headers into a numerically indexed zval hash */
     267          21 : static void php_head_apply_header_list_to_hash(void *data, void *arg TSRMLS_DC)
     268             : {
     269          21 :         sapi_header_struct *sapi_header = (sapi_header_struct *)data;
     270             : 
     271          21 :         if (arg && sapi_header) {
     272          21 :                 add_next_index_string((zval *)arg, (char *)(sapi_header->header));
     273             :         }
     274          21 : }
     275             : 
     276             : /* {{{ proto array headers_list(void)
     277             :    Return list of headers to be sent / already sent */
     278           8 : PHP_FUNCTION(headers_list)
     279             : {
     280           8 :         if (zend_parse_parameters_none() == FAILURE) {
     281           0 :                 return;
     282             :         }
     283             : 
     284           8 :         if (!&SG(sapi_headers).headers) {
     285             :                 RETURN_FALSE;
     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 TSRMLS_CC);
     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() TSRMLS_CC, "|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 Sat, 13 Dec 2014 06:16:23 +0000 (5 days ago)

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