Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

Commit 2421319

Browse files
committed
👌 more improvement on code base to support PHP 8
1 parent cb41e44 commit 2421319

File tree

6 files changed

+131
-129
lines changed

6 files changed

+131
-129
lines changed

framework/core/CodeIgniter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
* @var string
5656
*
5757
*/
58-
const CI_VERSION = '3.1.11.1';
58+
const CI_VERSION = '3.1.12';
5959

6060
/*
6161
* ------------------------------------------------------

framework/core/Output.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,13 @@ public function get_content_type()
300300
public function get_header($header)
301301
{
302302
// Combine headers already sent with our batched headers
303+
$headers = array();
304+
foreach ($this->headers as $value)
305+
{
306+
$headers[] = $value[0];
307+
}
303308
$headers = array_merge(
304-
// We only need [x][0] from our multi-dimensional array
305-
array_map('array_shift', $this->headers),
309+
$headers,
306310
headers_list()
307311
);
308312

@@ -412,7 +416,7 @@ public function cache($time)
412416
* @param string $output Output data override
413417
* @return void
414418
*/
415-
public function _display($output = '')
419+
public function _display($output = NULL)
416420
{
417421
// Note: We use load_class() because we can't use $CI =& get_instance()
418422
// since this function is sometimes called by the caching mechanism,
@@ -429,7 +433,7 @@ public function _display($output = '')
429433
// --------------------------------------------------------------------
430434

431435
// Set the output data
432-
if ($output === '')
436+
if ($output === NULL)
433437
{
434438
$output =& $this->final_output;
435439
}
@@ -502,7 +506,7 @@ public function _display($output = '')
502506

503507
echo $output;
504508
log_message('info', 'Final output sent to browser');
505-
log_message('debug', 'Total execution time: '.$elapsed);
509+
log_message('info', 'Total execution time: '.$elapsed);
506510
return;
507511
}
508512

@@ -539,7 +543,7 @@ public function _display($output = '')
539543
}
540544

541545
log_message('info', 'Final output sent to browser');
542-
log_message('debug', 'Total execution time: '.$elapsed);
546+
log_message('info', 'Total execution time: '.$elapsed);
543547
}
544548

545549
// --------------------------------------------------------------------
@@ -554,7 +558,7 @@ public function _write_cache($output)
554558
{
555559
$CI =& get_instance();
556560
$path = $CI->config->item('cache_path');
557-
$cache_path = ($path === '') ? APPPATH.'cache/' : $path;
561+
$cache_path = ($path === '') ? APPPATH.'cache'.DIRECTORY_SEPARATOR : rtrim($path, '/\\').DIRECTORY_SEPARATOR;
558562

559563
if ( ! is_dir($cache_path) OR ! is_really_writable($cache_path))
560564
{
@@ -563,7 +567,7 @@ public function _write_cache($output)
563567
}
564568

565569
$uri = $CI->config->item('base_url')
566-
.$CI->config->item('index_page')
570+
.$CI->config->slash_item('index_page')
567571
.$CI->uri->uri_string();
568572

569573
if (($cache_query_string = $CI->config->item('cache_query_string')) && ! empty($_SERVER['QUERY_STRING']))
@@ -658,7 +662,7 @@ public function _display_cache(&$CFG, &$URI)
658662
$cache_path = ($CFG->item('cache_path') === '') ? APPPATH.'cache/' : $CFG->item('cache_path');
659663

660664
// Build the file path. The file name is an MD5 hash of the full URI
661-
$uri = $CFG->item('base_url').$CFG->item('index_page').$URI->uri_string;
665+
$uri = $CFG->item('base_url').$CFG->slash_item('index_page').$URI->uri_string;
662666

663667
if (($cache_query_string = $CFG->item('cache_query_string')) && ! empty($_SERVER['QUERY_STRING']))
664668
{
@@ -761,7 +765,7 @@ public function delete_cache($uri = '')
761765
}
762766
}
763767

764-
$cache_path .= md5($CI->config->item('base_url').$CI->config->item('index_page').ltrim($uri, '/'));
768+
$cache_path .= md5($CI->config->item('base_url').$CI->config->slash_item('index_page').ltrim($uri, '/'));
765769

766770
if ( ! @unlink($cache_path))
767771
{
@@ -829,9 +833,6 @@ protected static function substr($str, $start, $length = NULL)
829833
{
830834
if (self::$func_overload)
831835
{
832-
// mb_substr($str, $start, null, '8bit') returns an empty
833-
// string on PHP 5.3
834-
isset($length) OR $length = ($start >= 0 ? self::strlen($str) - $start : -$start);
835836
return mb_substr($str, $start, $length, '8bit');
836837
}
837838

framework/core/Security.php

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,35 @@ public function csrf_set_cookie()
272272
return FALSE;
273273
}
274274

275-
setcookie(
276-
$this->_csrf_cookie_name,
277-
$this->_csrf_hash,
278-
$expire,
279-
config_item('cookie_path'),
280-
config_item('cookie_domain'),
281-
$secure_cookie,
282-
config_item('cookie_httponly')
283-
);
275+
if (is_php('7.3'))
276+
{
277+
setcookie(
278+
$this->_csrf_cookie_name,
279+
$this->_csrf_hash,
280+
array(
281+
'expires' => $expire,
282+
'path' => config_item('cookie_path'),
283+
'domain' => config_item('cookie_domain'),
284+
'secure' => $secure_cookie,
285+
'httponly' => config_item('cookie_httponly'),
286+
'samesite' => 'Strict'
287+
)
288+
);
289+
}
290+
else
291+
{
292+
$domain = trim(config_item('cookie_domain'));
293+
header('Set-Cookie: '.$this->_csrf_cookie_name.'='.$this->_csrf_hash
294+
.'; Expires='.gmdate('D, d-M-Y H:i:s T', $expire)
295+
.'; Max-Age='.$this->_csrf_expire
296+
.'; Path='.rawurlencode(config_item('cookie_path'))
297+
.($domain === '' ? '' : '; Domain='.$domain)
298+
.($secure_cookie ? '; Secure' : '')
299+
.(config_item('cookie_httponly') ? '; HttpOnly' : '')
300+
.'; SameSite=Strict'
301+
);
302+
}
303+
284304
log_message('info', 'CSRF cookie sent');
285305

286306
return $this;

framework/database/DB_driver.php

Lines changed: 22 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ public function __construct($params)
380380
/**
381381
* Initialize Database Settings
382382
*
383-
* @return bool
383+
* @return void
384+
* @throws RuntimeException In case of failure
384385
*/
385386
public function initialize()
386387
{
@@ -392,7 +393,7 @@ public function initialize()
392393
*/
393394
if ($this->conn_id)
394395
{
395-
return TRUE;
396+
return;
396397
}
397398

398399
// ----------------------------------------------------------------
@@ -429,19 +430,9 @@ public function initialize()
429430
// We still don't have a connection?
430431
if ( ! $this->conn_id)
431432
{
432-
log_message('error', 'Unable to connect to the database');
433-
434-
if ($this->db_debug)
435-
{
436-
$this->display_error('db_unable_to_connect');
437-
}
438-
439-
return FALSE;
433+
throw new RuntimeException('Unable to connect to the database.');
440434
}
441435
}
442-
443-
// Now we set the character set and that's all
444-
return $this->db_set_charset($this->char_set);
445436
}
446437

447438
// --------------------------------------------------------------------
@@ -516,31 +507,6 @@ public function error()
516507

517508
// --------------------------------------------------------------------
518509

519-
/**
520-
* Set client character set
521-
*
522-
* @param string
523-
* @return bool
524-
*/
525-
public function db_set_charset($charset)
526-
{
527-
if (method_exists($this, '_db_set_charset') && ! $this->_db_set_charset($charset))
528-
{
529-
log_message('error', 'Unable to set database connection charset: '.$charset);
530-
531-
if ($this->db_debug)
532-
{
533-
$this->display_error('db_unable_to_set_charset', $charset);
534-
}
535-
536-
return FALSE;
537-
}
538-
539-
return TRUE;
540-
}
541-
542-
// --------------------------------------------------------------------
543-
544510
/**
545511
* The name of the platform in use (mysql, mssql, etc...)
546512
*
@@ -634,7 +600,6 @@ public function query($sql, $binds = FALSE, $return_object = NULL)
634600
// cached query if it exists
635601
if ($this->cache_on === TRUE && $return_object === TRUE && $this->_cache_init())
636602
{
637-
$this->load_rdriver();
638603
if (FALSE !== ($cache = $this->CACHE->read($sql)))
639604
{
640605
return $cache;
@@ -718,9 +683,9 @@ public function query($sql, $binds = FALSE, $return_object = NULL)
718683
return TRUE;
719684
}
720685

721-
// Load and instantiate the result driver
722-
$driver = $this->load_rdriver();
723-
$RES = new $driver($this);
686+
// Instantiate the driver-specific result class
687+
$driver = 'CI_DB_'.$this->dbdriver.'_result';
688+
$RES = new $driver($this);
724689

725690
// Is query caching enabled? If so, we'll serialize the
726691
// result object and save it to a cache file.
@@ -749,26 +714,6 @@ public function query($sql, $binds = FALSE, $return_object = NULL)
749714

750715
// --------------------------------------------------------------------
751716

752-
/**
753-
* Load the result drivers
754-
*
755-
* @return string the name of the result class
756-
*/
757-
public function load_rdriver()
758-
{
759-
$driver = 'CI_DB_'.$this->dbdriver.'_result';
760-
761-
if ( ! class_exists($driver, FALSE))
762-
{
763-
require_once(BASEPATH.'database/DB_result.php');
764-
require_once(BASEPATH.'database/drivers/'.$this->dbdriver.'/'.$this->dbdriver.'_result.php');
765-
}
766-
767-
return $driver;
768-
}
769-
770-
// --------------------------------------------------------------------
771-
772717
/**
773718
* Simple Query
774719
* This is a simplified version of the query() function. Internally
@@ -780,14 +725,7 @@ public function load_rdriver()
780725
*/
781726
public function simple_query($sql)
782727
{
783-
if ( ! $this->conn_id)
784-
{
785-
if ( ! $this->initialize())
786-
{
787-
return FALSE;
788-
}
789-
}
790-
728+
empty($this->conn_id) && $this->initialize();
791729
return $this->_execute($sql);
792730
}
793731

@@ -887,7 +825,7 @@ public function trans_status()
887825
{
888826
return $this->_trans_status;
889827
}
890-
828+
891829
// --------------------------------------------------------------------
892830

893831
/**
@@ -1389,10 +1327,11 @@ public function field_data($table)
13891327
*
13901328
* This function escapes column and table names
13911329
*
1392-
* @param mixed
1330+
* @param mixed $item Identifier to escape
1331+
* @param bool $split Whether to split identifiers when a dot is encountered
13931332
* @return mixed
13941333
*/
1395-
public function escape_identifiers($item)
1334+
public function escape_identifiers($item, $split = TRUE)
13961335
{
13971336
if ($this->_escape_char === '' OR empty($item) OR in_array($item, $this->_reserved_identifiers))
13981337
{
@@ -1413,22 +1352,22 @@ public function escape_identifiers($item)
14131352
return $item;
14141353
}
14151354

1416-
static $preg_ec = array();
1355+
static $preg_ec;
14171356

14181357
if (empty($preg_ec))
14191358
{
14201359
if (is_array($this->_escape_char))
14211360
{
14221361
$preg_ec = array(
1423-
preg_quote($this->_escape_char[0], '/'),
1424-
preg_quote($this->_escape_char[1], '/'),
1362+
preg_quote($this->_escape_char[0]),
1363+
preg_quote($this->_escape_char[1]),
14251364
$this->_escape_char[0],
14261365
$this->_escape_char[1]
14271366
);
14281367
}
14291368
else
14301369
{
1431-
$preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char, '/');
1370+
$preg_ec[0] = $preg_ec[1] = preg_quote($this->_escape_char);
14321371
$preg_ec[2] = $preg_ec[3] = $this->_escape_char;
14331372
}
14341373
}
@@ -1437,11 +1376,13 @@ public function escape_identifiers($item)
14371376
{
14381377
if (strpos($item, '.'.$id) !== FALSE)
14391378
{
1440-
return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?\./i', $preg_ec[2].'$1'.$preg_ec[3].'.', $item);
1379+
return preg_replace('#'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?\.#i', $preg_ec[2].'$1'.$preg_ec[3].'.', $item);
14411380
}
14421381
}
14431382

1444-
return preg_replace('/'.$preg_ec[0].'?([^'.$preg_ec[1].'\.]+)'.$preg_ec[1].'?(\.)?/i', $preg_ec[2].'$1'.$preg_ec[3].'$2', $item);
1383+
$dot = ($split !== FALSE) ? '\.' : '';
1384+
1385+
return preg_replace('#'.$preg_ec[0].'?([^'.$preg_ec[1].$dot.']+)'.$preg_ec[1].'?(\.)?#i', $preg_ec[2].'$1'.$preg_ec[3].'$2', $item);
14451386
}
14461387

14471388
// --------------------------------------------------------------------
@@ -1855,14 +1796,14 @@ public function protect_identifiers($item, $prefix_single = FALSE, $protect_iden
18551796
if ($offset = strripos($item, ' AS '))
18561797
{
18571798
$alias = ($protect_identifiers)
1858-
? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4))
1799+
? substr($item, $offset, 4).$this->escape_identifiers(substr($item, $offset + 4), FALSE)
18591800
: substr($item, $offset);
18601801
$item = substr($item, 0, $offset);
18611802
}
18621803
elseif ($offset = strrpos($item, ' '))
18631804
{
18641805
$alias = ($protect_identifiers)
1865-
? ' '.$this->escape_identifiers(substr($item, $offset + 1))
1806+
? ' '.$this->escape_identifiers(substr($item, $offset + 1), FALSE)
18661807
: substr($item, $offset);
18671808
$item = substr($item, 0, $offset);
18681809
}

framework/database/drivers/pdo/pdo_driver.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ public function db_connect($persistent = FALSE)
131131
$this->options[PDO::ATTR_PERSISTENT] = TRUE;
132132
}
133133

134+
// From PHP8.0, default PDO::ATTR_ERRMODE is changed
135+
// from PDO::ERRMODE_SILENT to PDO::ERRMODE_EXCEPTION
136+
// as https://wiki.php.net/rfc/pdo_default_errmode
137+
if ( ! isset($this->options[PDO::ATTR_ERRMODE]))
138+
{
139+
$this->options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_SILENT;
140+
}
141+
134142
try
135143
{
136144
return new PDO($this->dsn, $this->username, $this->password, $this->options);

0 commit comments

Comments
 (0)