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: 34 121 28.1 %
Date: 2014-04-08 Functions: 6 9 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 5                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2013 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             : #include "safe_mode.h"
      35             : 
      36             : 
      37             : /* Implementation of the language Header() function */
      38             : /* {{{ proto void header(string header [, bool replace, [int http_response_code]])
      39             :    Sends a raw HTTP header */
      40          44 : PHP_FUNCTION(header)
      41             : {
      42          44 :         zend_bool rep = 1;
      43          44 :         sapi_header_line ctr = {0};
      44             :         
      45          44 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|bl", &ctr.line,
      46             :                                 &ctr.line_len, &rep, &ctr.response_code) == FAILURE)
      47           0 :                 return;
      48             :         
      49          44 :         sapi_header_op(rep ? SAPI_HEADER_REPLACE:SAPI_HEADER_ADD, &ctr TSRMLS_CC);
      50             : }
      51             : /* }}} */
      52             : 
      53             : /* {{{ proto void header_remove([string name])
      54             :    Removes an HTTP header previously set using header() */
      55          10 : PHP_FUNCTION(header_remove)
      56             : {
      57          10 :         sapi_header_line ctr = {0};
      58             : 
      59          10 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &ctr.line,
      60             :                                   &ctr.line_len) == FAILURE)
      61           0 :                 return;
      62             : 
      63          10 :         sapi_header_op(ZEND_NUM_ARGS() == 0 ? SAPI_HEADER_DELETE_ALL : SAPI_HEADER_DELETE, &ctr TSRMLS_CC);
      64             : }
      65             : /* }}} */
      66             : 
      67       12661 : PHPAPI int php_header(TSRMLS_D)
      68             : {
      69       12661 :         if (sapi_send_headers(TSRMLS_C)==FAILURE || SG(request_info).headers_only) {
      70           0 :                 return 0; /* don't allow output */
      71             :         } else {
      72       12661 :                 return 1; /* allow output */
      73             :         }
      74             : }
      75             : 
      76             : 
      77           0 : PHPAPI int php_setcookie(char *name, int name_len, char *value, int value_len, time_t expires, char *path, int path_len, char *domain, int domain_len, int secure, int url_encode, int httponly TSRMLS_DC)
      78             : {
      79           0 :         char *cookie, *encoded_value = NULL;
      80           0 :         int len=sizeof("Set-Cookie: ");
      81             :         char *dt;
      82           0 :         sapi_header_line ctr = {0};
      83             :         int result;
      84             :         
      85           0 :         if (name && strpbrk(name, "=,; \t\r\n\013\014") != NULL) {   /* man isspace for \013 and \014 */
      86           0 :                 zend_error( E_WARNING, "Cookie names cannot contain any of the following '=,; \\t\\r\\n\\013\\014'" );
      87           0 :                 return FAILURE;
      88             :         }
      89             : 
      90           0 :         if (!url_encode && value && strpbrk(value, ",; \t\r\n\013\014") != NULL) { /* man isspace for \013 and \014 */
      91           0 :                 zend_error( E_WARNING, "Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014'" );
      92           0 :                 return FAILURE;
      93             :         }
      94             : 
      95           0 :         len += name_len;
      96           0 :         if (value && url_encode) {
      97             :                 int encoded_value_len;
      98             : 
      99           0 :                 encoded_value = php_url_encode(value, value_len, &encoded_value_len);
     100           0 :                 len += encoded_value_len;
     101           0 :         } else if ( value ) {
     102           0 :                 encoded_value = estrdup(value);
     103           0 :                 len += value_len;
     104             :         }
     105           0 :         if (path) {
     106           0 :                 len += path_len;
     107             :         }
     108           0 :         if (domain) {
     109           0 :                 len += domain_len;
     110             :         }
     111             : 
     112           0 :         cookie = emalloc(len + 100);
     113             : 
     114           0 :         if (value && value_len == 0) {
     115             :                 /* 
     116             :                  * MSIE doesn't delete a cookie when you set it to a null value
     117             :                  * so in order to force cookies to be deleted, even on MSIE, we
     118             :                  * pick an expiry date in the past
     119             :                  */
     120           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);
     121           0 :                 snprintf(cookie, len + 100, "Set-Cookie: %s=deleted; expires=%s", name, dt);
     122           0 :                 efree(dt);
     123             :         } else {
     124           0 :                 snprintf(cookie, len + 100, "Set-Cookie: %s=%s", name, value ? encoded_value : "");
     125           0 :                 if (expires > 0) {
     126             :                         char *p;
     127           0 :                         strlcat(cookie, "; expires=", len + 100);
     128           0 :                         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);
     129             :                         /* check to make sure that the year does not exceed 4 digits in length */
     130           0 :                         p = zend_memrchr(dt, '-', strlen(dt));
     131           0 :                         if (!p || *(p + 5) != ' ') {
     132           0 :                                 efree(dt);
     133           0 :                                 efree(cookie);
     134           0 :                                 efree(encoded_value);
     135           0 :                                 zend_error(E_WARNING, "Expiry date cannot have a year greater then 9999");
     136           0 :                                 return FAILURE;
     137             :                         }
     138           0 :                         strlcat(cookie, dt, len + 100);
     139           0 :                         efree(dt);
     140             :                 }
     141             :         }
     142             : 
     143           0 :         if (encoded_value) {
     144           0 :                 efree(encoded_value);
     145             :         }
     146             : 
     147           0 :         if (path && path_len > 0) {
     148           0 :                 strlcat(cookie, "; path=", len + 100);
     149           0 :                 strlcat(cookie, path, len + 100);
     150             :         }
     151           0 :         if (domain && domain_len > 0) {
     152           0 :                 strlcat(cookie, "; domain=", len + 100);
     153           0 :                 strlcat(cookie, domain, len + 100);
     154             :         }
     155           0 :         if (secure) {
     156           0 :                 strlcat(cookie, "; secure", len + 100);
     157             :         }
     158           0 :         if (httponly) {
     159           0 :                 strlcat(cookie, "; httponly", len + 100);
     160             :         }
     161             : 
     162           0 :         ctr.line = cookie;
     163           0 :         ctr.line_len = strlen(cookie);
     164             : 
     165           0 :         result = sapi_header_op(SAPI_HEADER_ADD, &ctr TSRMLS_CC);
     166           0 :         efree(cookie);
     167           0 :         return result;
     168             : }
     169             : 
     170             : 
     171             : /* php_set_cookie(name, value, expires, path, domain, secure) */
     172             : /* {{{ proto bool setcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
     173             :    Send a cookie */
     174           0 : PHP_FUNCTION(setcookie)
     175             : {
     176           0 :         char *name, *value = NULL, *path = NULL, *domain = NULL;
     177           0 :         long expires = 0;
     178           0 :         zend_bool secure = 0, httponly = 0;
     179           0 :         int name_len, value_len = 0, path_len = 0, domain_len = 0;
     180             : 
     181           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
     182             :                                                           &name_len, &value, &value_len, &expires, &path,
     183             :                                                           &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
     184           0 :                 return;
     185             :         }
     186             : 
     187           0 :         if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 1, httponly TSRMLS_CC) == SUCCESS) {
     188           0 :                 RETVAL_TRUE;
     189             :         } else {
     190           0 :                 RETVAL_FALSE;
     191             :         }
     192             : }
     193             : /* }}} */
     194             : 
     195             : /* {{{ proto bool setrawcookie(string name [, string value [, int expires [, string path [, string domain [, bool secure[, bool httponly]]]]]])
     196             :    Send a cookie with no url encoding of the value */
     197           0 : PHP_FUNCTION(setrawcookie)
     198             : {
     199           0 :         char *name, *value = NULL, *path = NULL, *domain = NULL;
     200           0 :         long expires = 0;
     201           0 :         zend_bool secure = 0, httponly = 0;
     202           0 :         int name_len, value_len = 0, path_len = 0, domain_len = 0;
     203             : 
     204           0 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|slssbb", &name,
     205             :                                                           &name_len, &value, &value_len, &expires, &path,
     206             :                                                           &path_len, &domain, &domain_len, &secure, &httponly) == FAILURE) {
     207           0 :                 return;
     208             :         }
     209             : 
     210           0 :         if (php_setcookie(name, name_len, value, value_len, expires, path, path_len, domain, domain_len, secure, 0, httponly TSRMLS_CC) == SUCCESS) {
     211           0 :                 RETVAL_TRUE;
     212             :         } else {
     213           0 :                 RETVAL_FALSE;
     214             :         }
     215             : }
     216             : /* }}} */
     217             : 
     218             : 
     219             : /* {{{ proto bool headers_sent([string &$file [, int &$line]])
     220             :    Returns true if headers have already been sent, false otherwise */
     221           3 : PHP_FUNCTION(headers_sent)
     222             : {
     223           3 :         zval *arg1 = NULL, *arg2 = NULL;
     224           3 :         char *file="";
     225           3 :         int line=0;
     226             : 
     227           3 :         if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zz", &arg1, &arg2) == FAILURE)
     228           0 :                 return;
     229             : 
     230           3 :         if (SG(headers_sent)) {
     231           2 :                 line = php_get_output_start_lineno(TSRMLS_C);
     232           2 :                 file = php_get_output_start_filename(TSRMLS_C);
     233             :         }
     234             : 
     235           3 :         switch(ZEND_NUM_ARGS()) {
     236             :         case 2:
     237           0 :                 zval_dtor(arg2);
     238           0 :                 ZVAL_LONG(arg2, line);
     239             :         case 1:
     240           0 :                 zval_dtor(arg1);
     241           0 :                 if (file) { 
     242           0 :                         ZVAL_STRING(arg1, file, 1);
     243             :                 } else {
     244           0 :                         ZVAL_STRING(arg1, "", 1);
     245             :                 }       
     246             :                 break;
     247             :         }
     248             : 
     249           3 :         if (SG(headers_sent)) {
     250           2 :                 RETURN_TRUE;
     251             :         } else {
     252           1 :                 RETURN_FALSE;
     253             :         }
     254             : }
     255             : /* }}} */
     256             : 
     257             : /* {{{ php_head_apply_header_list_to_hash
     258             :    Turn an llist of sapi_header_struct headers into a numerically indexed zval hash */
     259           6 : static void php_head_apply_header_list_to_hash(void *data, void *arg TSRMLS_DC)
     260             : {
     261           6 :         sapi_header_struct *sapi_header = (sapi_header_struct *)data;
     262             : 
     263           6 :         if (arg && sapi_header) {
     264           6 :                 add_next_index_string((zval *)arg, (char *)(sapi_header->header), 1);
     265             :         }
     266           6 : }
     267             : 
     268             : /* {{{ proto array headers_list(void)
     269             :    Return list of headers to be sent / already sent */
     270           6 : PHP_FUNCTION(headers_list)
     271             : {
     272           6 :         if (zend_parse_parameters_none() == FAILURE) {
     273           0 :                 return;
     274             :         }
     275             : 
     276           6 :         if (!&SG(sapi_headers).headers) {
     277             :                 RETURN_FALSE;
     278             :         }
     279           6 :         array_init(return_value);
     280           6 :         zend_llist_apply_with_argument(&SG(sapi_headers).headers, php_head_apply_header_list_to_hash, return_value TSRMLS_CC);
     281             : }
     282             : /* }}} */
     283             : 
     284             : /*
     285             :  * Local variables:
     286             :  * tab-width: 4
     287             :  * c-basic-offset: 4
     288             :  * vim600: sw=4 ts=4 fdm=marker
     289             :  * vim<600: sw=4 ts=4 * End:
     290             :  */

Generated by: LCOV version 1.10

Generated at Tue, 08 Apr 2014 11:59:46 +0000 (7 days ago)

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