老程序重獲新生,在PHP7環境下用PageCookery搭建自己的微博

很多年前,互聯網上還有着大量的獨立博客或微博,分享些技術或者生活故事。大概是2015年開始,移動互聯網發展速度有了質的飛躍,這些獨立博客也就逐漸淡出了人們的視野。而當年那些風靡全球的開源博客程序,也在逐漸停止更新。PC安裝好的樣子

PageCookery是當年比較成熟的一款微博程序了,在還是PHP5的時代,也算是一款佳作了。不過它的更新停在了0.9.9版本。最近想在本地服務器上搭建起來懷懷舊,卻發現在今天的PHP7上各種報錯。於是開始了下面的改造工程。

先直接訪問index.php試試。
放棄了,全是報錯,一條一條改吧。


Warning: Use of undefined constant DATABASE_HOST - assumed 'DATABASE_HOST' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.php on line 12

Warning: Use of undefined constant DATABASE_USER - assumed 'DATABASE_USER' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.php on line 16

Warning: Use of undefined constant DATABASE_PSSWORD - assumed 'DATABASE_PSSWORD' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.php on line 20

Warning: Use of undefined constant DATABASE_DB_NAME - assumed 'DATABASE_DB_NAME' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.php on line 24

Warning: Use of undefined constant SITE_NAME - assumed 'SITE_NAME' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.php on line 32

Warning: Use of undefined constant BASE_URL - assumed 'BASE_URL' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.php on line 36

Warning: Use of undefined constant COOKERY_FRAMEWORK_DIR - assumed 'COOKERY_FRAMEWORK_DIR' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.php on line 44

Warning: Use of undefined constant DEBUG - assumed 'DEBUG' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.php on line 46

Warning: Use of undefined constant COOKIE_PREFIX - assumed 'COOKIE_PREFIX' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.php on line 48

Warning: Use of undefined constant WP_ENABLED - assumed 'WP_ENABLED' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.wordpress.php on line 2

Warning: Use of undefined constant WP_URL - assumed 'WP_URL' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.wordpress.php on line 3

Warning: Use of undefined constant WP_USERNAME - assumed 'WP_USERNAME' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.wordpress.php on line 4

Warning: Use of undefined constant WP_PASSWORD - assumed 'WP_PASSWORD' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.wordpress.php on line 5

Warning: Use of undefined constant WP_CATEGORY_ID - assumed 'WP_CATEGORY_ID' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.wordpress.php on line 6

Warning: Use of undefined constant PG_LANG - assumed 'PG_LANG' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\config.language.php on line 2

Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in D:\xampp\htdocs\PC\init.php on line 61

Fatal error: 'continue' not in the 'loop' or 'switch' context in D:\xampp\htdocs\PC\framework\CI\database\DB.php on line 61

Use of undefined constant ,這句大家比較熟悉,舊版本定義常量可以不加引號的,補充上就解決了。例如對config.php裏面的define進行修改,原代碼如下:(請注意PC需要手動修改config裏面的mysql數據庫信息,請一併修改)

// 數據庫主機
// Database host
define(DATABASE_HOST, 'localhost');

// 數據庫用戶名
// Database username
define(DATABASE_USER, 'root');

// 數據庫密碼
// Database password
define(DATABASE_PSSWORD, 'root');

// 數據庫名稱
// Database name
define(DATABASE_DB_NAME, 'microblog');


############ 站點配置 ############
############ WEBSITE SETTINGS ############

// 網站名稱
// Website name
define(SITE_NAME, 'PageCookery Microblog');

// 微博訪問路徑, 請以 / 結尾
// Website visit URL, End up with /
define(BASE_URL, 'http://127.0.0.1/microblog/');

修改後如下:


// 數據庫主機
// Database host
define("DATABASE_HOST", 'localhost');

// 數據庫用戶名
// Database username
define("DATABASE_USER", 'root');

// 數據庫密碼
// Database password
define("DATABASE_PSSWORD", '');

// 數據庫名稱
// Database name
define("DATABASE_DB_NAME", 'microblog');

define("BASEPATH", 'framework/CI/');
############ 站點配置 ############
############ WEBSITE SETTINGS ############

// 網站名稱
// Website name
define("SITE_NAME", 'PageCookery Microblog');

// 微博訪問路徑, 請以 / 結尾
// Website visit URL, End up with /
define("BASE_URL", 'http://localhost:8000/pagecookery/');

注意到我增加了一行。

define("BASEPATH", 'framework/CI/');

這是因爲這個版本使用的CI框架已經很老了,對於新的MySQL數據庫支持不是很好,需要一併更換CI框架的文件,推進使用3.1.1版本。而新版CI框架需要定義BASEPATH。
之後請依次對config.wordpress.php、config.language.php、文件中的define進行替換。
注意到,index.php中還有幾行代碼用於寫入“config.language.php”文件,因此需要一併替換。
原代碼:

$lang_config = "<?php\r\ndefine(PG_LANG, '" . Format::Safe($_POST['language'], TRUE) . "');";
$lang_config = "<?php\r\ndefine(PG_LANG, '" . Format::Safe(str_replace('.', '', $_GET['PG_LANG']), TRUE) . "');";
$wordpress_config = "<?php\r\ndefine(WP_ENABLED, TRUE);\r\ndefine(WP_URL, '" . Format::Safe($_POST['wp_url'], TRUE) . "');\r\ndefine(WP_USERNAME, '" . Format::Safe($_POST['wp_username'], TRUE) . "');\r\ndefine(WP_PASSWORD, '" . Format::Safe($_POST['wp_password']) . "');\r\ndefine(WP_CATEGORY_ID, " . intval($_POST['wp_category_id']) . ");";
$wordpress_config = "<?php\r\ndefine(WP_ENABLED, FALSE);\r\ndefine(WP_URL, '');\r\ndefine(WP_USERNAME, '');\r\ndefine(WP_PASSWORD, '');\r\ndefine(WP_CATEGORY_ID, 0);";

替換後爲

$lang_config = "<?php\r\ndefine(\"PG_LANG\", '" . Format::Safe($_POST['language'], TRUE) . "');";
$lang_config = "<?php\r\ndefine(\"PG_LANG\", '" . Format::Safe(str_replace('.', '', $_GET['PG_LANG']), TRUE) . "');";
$wordpress_config = "<?php\r\ndefine(\"WP_ENABLED\", TRUE);\r\ndefine(\"WP_URL\", '" . Format::Safe($_POST['wp_url'], TRUE) . "');\r\ndefine(\"WP_USERNAME\", '" . Format::Safe($_POST['wp_username'], TRUE) . "');\r\ndefine(\"WP_PASSWORD\", '" . Format::Safe($_POST['wp_password']) . "');\r\ndefine(\"WP_CATEGORY_ID\", " . intval($_POST['wp_category_id']) . ");";
$wordpress_config = "<?php\r\ndefine(\"WP_ENABLED\", FALSE);\r\ndefine(\"WP_URL\", '');\r\ndefine(\"WP_USERNAME\", '');\r\ndefine(\"WP_PASSWORD\", '');\r\ndefine(\"WP_CATEGORY_ID\", 0);";

刷新一下,清爽多了。

Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in D:\xampp\htdocs\PC\init.php on line 61

Fatal error: 'continue' not in the 'loop' or 'switch' context in D:\xampp\htdocs\PC\framework\CI\database\DB.php on line 61

下面看init.php,由於__autoload被棄用,應對下面的代碼進行修改。

function __autoload($class_name)
{
	if (in_array(strtolower($class_name), array('cron', 'format', 'helper', 'image', 'iplocation', 'pages', 'rssparser', 'template', 'timer', 'upload', 'weiboclient', 'weibooauth')))
	{
		require_once(COOKERY_FRAMEWORK_DIR . 'lib/class_' . strtolower($class_name) . '.php');
	}
}

修改爲如下的代碼:

spl_autoload_register('autoload');

function autoload($class_name)
{
	if (in_array(strtolower($class_name), array('cron', 'format', 'helper', 'image', 'iplocation', 'pages', 'rssparser', 'template', 'timer', 'upload', 'weiboclient', 'weibooauth')))
	{
		require_once(COOKERY_FRAMEWORK_DIR . 'lib/class_' . strtolower($class_name) . '.php');
	}
} 

下面的致命錯誤,只需要對CI框架文件進行整體替換就可以了。
刷新一下。不容易啊,可以進入安裝界面了。
在這裏插入圖片描述
選擇語言,填寫管理員用戶名密碼。
在這裏插入圖片描述
卡住了,刷新一下。

Warning: Use of undefined constant TIMENOW - assumed 'TIMENOW' (this will throw an Error in a future version of PHP) in D:\xampp\htdocs\PC\global.php on line 24

Fatal error: Uncaught Error: Call to undefined function log_message() in D:\xampp\htdocs\PC\framework\CI\database\DB_driver.php:375 Stack trace: #0 D:\xampp\htdocs\PC\framework\CI\database\DB.php(201): CI_DB_driver->__construct(Array) #1 D:\xampp\htdocs\PC\global.php(52): DB(Array) #2 D:\xampp\htdocs\PC\index.php(9): require_once('D:\\xampp\\htdocs...') #3 {main} thrown in D:\xampp\htdocs\PC\framework\CI\database\DB_driver.php on line 375

又報錯了。第一條同之前的方案,修改define的引號。第二條,將報錯代碼註釋掉即可。
再次刷新,新的錯誤出現了。

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in D:\xampp\htdocs\PC\lib\class_template.php on line 160

這是因爲/e被棄用了,修改lib\class_template.php文件的如下行:

$template = preg_replace("/\{template (.+?)\}/e", "load_template('\\1')", $template);

修改爲:

$template = preg_replace_callback("/\{template (.+?)\}/", function($matches) { return load_template($matches[1]); }, $template);

刷新,又報錯了。

Warning: preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead in D:\xampp\htdocs\PC\lib\functions.php on line 19

同理,修改lib\functions.php 文件的

$file_content = preg_replace("/\{template (.+?)\}/e", "load_template('\\1')", $file_content);

$file_content = preg_replace_callback("/\{template (.+?)\}/", function($matches) { return load_template($matches[1]); }, $file_content);

在這裏插入圖片描述
大功告成了。
此外,這個IE版本提示放在今天已經不合適了,新版的edge和chrome瀏覽器仍會提示IE版本過低,可以在template/header.html中移去下面的代碼。

<!--[if lt IE 7]>
	{template no_support_ie6}
<![endif]-->

上述修改後的版本我重新整理了一下,版本號修改爲了0.9.10。歡迎大家一起討論。
PageCookery 0.9.9原版下載
0.9.10稍後放出

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章