From a1afbdffcdda3c7a6f52937bc074fbd67c98ab78 Mon Sep 17 00:00:00 2001 From: ozhozh Date: Fri, 28 Aug 2009 12:29:29 +0000 Subject: [PATCH] YOURLS installer: - rewrote admin/install.php - added includes/functions-install.php - misc CSS & images git-svn-id: http://yourls.googlecode.com/svn/trunk@96 12232710-3e20-11de-b438-597f59cd7555 --- admin/install.php | 120 ++++++++------------ css/style.css | 25 +++- images/accept.png | Bin 0 -> 781 bytes images/cancel.png | Bin 0 -> 587 bytes images/error.png | Bin 0 -> 666 bytes includes/functions-install.php | 201 +++++++++++++++++++++++++++++++++ 6 files changed, 274 insertions(+), 72 deletions(-) create mode 100644 images/accept.png create mode 100644 images/cancel.png create mode 100644 images/error.png create mode 100644 includes/functions-install.php diff --git a/admin/install.php b/admin/install.php index a070ac8..548a9b5 100644 --- a/admin/install.php +++ b/admin/install.php @@ -1,71 +1,53 @@ mod_rewrite not found. Check this with your server admin.'; -// Insert Initial Records -$insert_queries = array(); -$insert_queries[] = 'INSERT INTO '.YOURLS_DB_TABLE_NEXTDEC.' VALUES (1)'; +if ( !yourls_check_database_version() ) + $error[] = 'MySQL version is too old. Ask your server admin for an upgrade.'; -// Check Whether YOURLS Is Installed -$ydb->show_errors = false; -$is_installed = $ydb->get_var('SELECT next_id FROM '.YOURLS_DB_TABLE_NEXTDEC); +if ( !yourls_check_php_version() ) + $error[] = 'PHP version is too old. Ask your server admin for an upgrade.'; +// Check additional stuff +if ( !yourls_check_curl() ) + $warning[] = 'PHP extension cURL is not installed. This server won\'t be able to use the remote API'; -## Install YOURLS -if ( isset($_REQUEST['install']) && !$is_installed ) { - $create_table_count = 0; - $insert_query_count = 0; - foreach($create_tables as $table_name => $table_query) { - $ydb->query($table_query); - $create_success = $ydb->query("SHOW TABLES LIKE '$table_name'"); - if($create_success) { - $create_table_count++; - $success_msg[] = "Table '$table_name' created."; - } else { - $error_msg[] = "Error creating table '$table_name'."; - } - } - foreach($insert_queries as $insert_query) { - $insert_success = $ydb->query($insert_query); - if($insert_success) { - $insert_query_count++; - $success_msg[] = 'Query '.$insert_query_count.'/'.sizeof($insert_queries).' executed successfully.'; - } else { - $error_msg[] = 'Error executing '.$insert_query_count.'/'.sizeof($insert_queries).'.'; - } - } - if(sizeof($create_tables) == $create_table_count && sizeof($insert_queries) == $insert_query_count) { - $success_msg[] = 'YOURLS successfully installed.'; +if ( !yourls_check_bcmath() ) + $warning[] = 'PHP extension BC Math is not installed. Config setting YOURLS_URL_CONVERT forced to 36'; + +// Is YOURLS already installed ? +if ( yourls_is_installed() ) + $error[] = 'YOURLS already installed.'; + +// Start install if possible and needed +if ( isset($_REQUEST['install']) && count( $error ) == 0 ) { + // Create/update .htaccess file + if ( yourls_create_htaccess() ) { + $success[] = 'File .htaccess successfully created/updated.'; } else { - $error_msg[] = "Error installing YOURLS."; - } -} else { - if( $is_installed != NULL ) { - $error_msg[] = 'YOURLS has already been installed.'; + $error[] = 'Could not write file .htaccess in YOURLS root directory. You will have to do it manually.'; } + + // Create SQL tables + $install = yourls_create_sql_tables(); + if ( isset( $install['error'] ) ) + $error = array_merge( $error, $install['error'] ); + if ( isset( $install['success'] ) ) + $success = array_merge( $success, $install['success'] ); } -yourls_html_head( 'install' ); +// Start output +yourls_html_head( 'install' ); ?>
@@ -73,27 +55,23 @@ YOURLS

0) { - echo '

'; - foreach($error_msg as $error) { - echo $error.'
'; + // Print errors, warnings and success messages + foreach ( array ('error', 'warning', 'success') as $info ) { + if ( count( $$info ) > 0 ) { + echo "

    "; + foreach( $$info as $msg ) { + echo '
  • '.$msg."
  • \n"; + } + echo '
'; } - echo '

'; } - // Print out any success messages - if(sizeof($success_msg) > 0) { - echo '

'; - foreach($success_msg as $success) { - echo $success.'
'; - } - echo '

'; - } - // Display install button - if($is_installed == NULL && !isset($_REQUEST['install'])) { + + // Display install button or link to admin area if applicable + if( !yourls_is_installed() && !isset($_REQUEST['install']) ) { echo '

 

'; } else { - echo '

 

» YOURS Administration Page

'; + if( count($error) == 0 ) + echo '

 

» YOURS Administration Page

'; } ?>
diff --git a/css/style.css b/css/style.css index 17beb74..57a631c 100644 --- a/css/style.css +++ b/css/style.css @@ -95,6 +95,9 @@ tr:hover td.actions input{ .error { color: red; } +.warning { + color: orange; +} .success { color: green; } @@ -103,15 +106,35 @@ tr:hover td.actions input{ text-align: center; } #login { - width: 210px; + width: 300px; margin: 200px auto 0px auto; } +#login img { + margin-left:50px; +} #login p{ font-weight: bold; } #login .text { width: 100%; } +#login ul { + padding-left:0px; + list-style-type:none; + text-indent:0; +} +#login ul li { + padding:0 0 5px 20px; +} +#login ul.error li { + background:transparent url(../images/cancel.png) top left no-repeat; +} +#login ul.warning li { + background:transparent url(../images/error.png) top left no-repeat; +} +#login ul.success li { + background:transparent url(../images/accept.png) top left no-repeat; +} #shareboxes, #tweet { overflow:hidden; } diff --git a/images/accept.png b/images/accept.png new file mode 100644 index 0000000000000000000000000000000000000000..89c8129a490b329f3165f32fa0781701aab417ea GIT binary patch literal 781 zcmV+o1M>WdP)4-QibtN)VXQDpczE`xXAkUjh%RI>;okxb7K@0kpyQ1k_Y(|Oe7$m(^ zNYX>mI||sUbmn+c3<&FnE=4u#()KBS^SH8e)Qs5i!#lY=$-1gbH6VluzU=m=EP78&5vQ z-?+fFP-G2l&l_QzYealK$;1Rl?FkzXR&Jv@fBPNjCr#AYRyJ7UJQ0v#?)7Ott=>3`#-pV!7>9}>Q1jL)H6h&gkP@3nI=+F3nA~M>u#(n* z8T!#8oEw&-mED4!h4s!N@Jo3S7N&Q6%6l3}nlcd~X@>;uelvPsSkXIgg~e+^T1zSf z3SNj(5%jK~i8@b;CFS^-G}e*;M)Q6>s#cP zI`Y#S($G6W`W@NI5g|L-MKl0Zmu$m^(0~^Lwo5OO~d#(vPfzeOSYYtbpBV}~vsBnU!_?2tr-P=|^T zED%wc9ezHgW@NMb!^uT_|SvCpFLJylbx zY%bpaTGI8IYXMN$9w<3j9VkA~NYOKEQXsj?6a9_hcwfU$acAhJhB)zb_w@MVUEy@S zX&I>K-R!bhu3?(6bHWIg$HEl7{9g>>&l_qdd+UYb(1~BCo9LptNq&8>!yoJ3Ui(i5 zRJ|XnYBklL!{@$-7=3mJ>P@1c=7Oc79e-V7yf+%lD2!I;Y&nXBZ>=B!5?CB>LvEx6 znI%n)qqi$#X#wKB(U7XP2P=+4{b@j#r%9-K(8UqtSDk>0UKzf*HM9yqMZ1D!$2MdZ zR=`U>0zhOH1XqN?nY@AQqB7)Fp4{v&dKXvb43hZKvnN8;Po;+jY*}~*Z|W9Q0W%{D z^T}Cc<|r(Su=1K=P5>Z4 zg`et&Va}tdzBS-G-ZcO)zCWpJvGQwrHZ`@wpM420ac@bI5~KkTFfGEM3sPWO8co4^fI6lPnA)Y{ef%@{+SnoUk0+dW+*{8WvF8}}l07*qoM6N<$g7cXs A&j0`b literal 0 HcmV?d00001 diff --git a/includes/functions-install.php b/includes/functions-install.php new file mode 100644 index 0000000..bbeee76 --- /dev/null +++ b/includes/functions-install.php @@ -0,0 +1,201 @@ +get_var('SELECT next_id FROM '.YOURLS_DB_TABLE_NEXTDEC); + } + return (bool)$is_installed; +} + +// Check if mod_rewrite is enabled +function yourls_check_mod_rewrite() { + return yourls_apache_mod_loaded('mod_rewrite'); +} + +// Check if extension cURL is enabled +function yourls_check_curl() { + return function_exists('curl_init'); +} + +// Check if extension BC Math is enabled +function yourls_check_bcmath() { + return function_exists('bccomp'); +} + +// Check if server has MySQL 4.1+ +function yourls_check_database_version() { + global $ydb; + return ( version_compare( '4.1', $ydb->mysql_version() ) <= 0 ); +} + +// Check if PHP > 4.3 +function yourls_check_php_version() { + return ( version_compare( '4.3', phpversion() ) <= 0 ); +} + +// Check if server is an Apache +function yourls_is_apache() { + return ( + strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false + || strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false + ); +} + +// Check if module exists in Apache config. Input string eg 'mod_rewrite', return true or $default +function yourls_apache_mod_loaded($mod, $default = false) { + if ( !yourls_is_apache() ) + return false; + + if ( function_exists('apache_get_modules') ) { + $mods = apache_get_modules(); + if ( in_array($mod, $mods) ) + return true; + } elseif ( function_exists('phpinfo') ) { + ob_start(); + phpinfo(8); + $phpinfo = ob_get_clean(); + if ( false !== strpos($phpinfo, $mod) ) + return true; + } + return $default; +} + +// Create .htaccess. Returns boolean +function yourls_create_htaccess() { + $host = parse_url( YOURLS_SITE ); + $path = ( isset( $host['path'] ) ? $host['path'] : '' ); + + $content = array( + '', + 'RewriteEngine On', + 'RewriteBase '.$path.'/', + 'RewriteCond %{REQUEST_FILENAME} !-f', + 'RewriteCond %{REQUEST_FILENAME} !-d', + 'RewriteRule ^([0-9A-Za-z]+)/?$ '.$path.'/yourls-go.php?id=$1 [L]', + '', + ); + + $filename = dirname(dirname(__FILE__)).'/.htaccess'; + + return ( yourls_insert_with_markers( $filename, 'YOURLS', $content ) ); +} + +// Inserts $insertion (text in an array of lines) into $filename (.htaccess) between BEGIN/END $marker block. Returns bool. Stolen from WP +function yourls_insert_with_markers( $filename, $marker, $insertion ) { + if (!file_exists( $filename ) || is_writeable( $filename ) ) { + if (!file_exists( $filename ) ) { + $markerdata = ''; + } else { + $markerdata = explode( "\n", implode( '', file( $filename ) ) ); + } + + if ( !$f = @fopen( $filename, 'w' ) ) + return false; + + $foundit = false; + if ( $markerdata ) { + $state = true; + foreach ( $markerdata as $n => $markerline ) { + if (strpos($markerline, '# BEGIN ' . $marker) !== false) + $state = false; + if ( $state ) { + if ( $n + 1 < count( $markerdata ) ) + fwrite( $f, "{$markerline}\n" ); + else + fwrite( $f, "{$markerline}" ); + } + if (strpos($markerline, '# END ' . $marker) !== false) { + fwrite( $f, "# BEGIN {$marker}\n" ); + if ( is_array( $insertion )) + foreach ( $insertion as $insertline ) + fwrite( $f, "{$insertline}\n" ); + fwrite( $f, "# END {$marker}\n" ); + $state = true; + $foundit = true; + } + } + } + if (!$foundit) { + fwrite( $f, "\n# BEGIN {$marker}\n" ); + foreach ( $insertion as $insertline ) + fwrite( $f, "{$insertline}\n" ); + fwrite( $f, "# END {$marker}\n" ); + } + fclose( $f ); + return true; + } else { + return false; + } +} + +// Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings ) +function yourls_create_sql_tables() { + global $ydb; + + $error_msg = array(); + $success_msg = array(); + + // Create Table Query + $create_tables = array(); + $create_tables[YOURLS_DB_TABLE_URL] = + 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('. + '`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,'. + '`url` VARCHAR(200) NOT NULL,'. + '`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,'. + '`ip` VARCHAR(41) NOT NULL,'. + '`clicks` INT(10) UNSIGNED NOT NULL,'. + 'PRIMARY KEY (`id`)'. + ') ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;'; + + $create_tables[YOURLS_DB_TABLE_NEXTDEC] = + 'CREATE TABLE `'.YOURLS_DB_TABLE_NEXTDEC.'` ('. + '`next_id` BIGINT NOT NULL ,'. + 'PRIMARY KEY (`next_id`)'. + ') ENGINE = MYISAM ;'; + + // Insert Initial Records + $insert_queries = array(); + $insert_queries[] = 'INSERT INTO '.YOURLS_DB_TABLE_NEXTDEC.' VALUES (1)'; + + $create_table_count = 0; + $insert_query_count = 0; + + //$ydb->show_errors = false; + + // Create tables + foreach ( $create_tables as $table_name => $table_query ) { + $ydb->query($table_query); + $create_success = $ydb->query("SHOW TABLES LIKE '$table_name'"); + if($create_success) { + $create_table_count++; + $success_msg[] = "Table '$table_name' created."; + } else { + $error_msg[] = "Error creating table '$table_name'."; + } + } + + // Insert data into tables + foreach ( $insert_queries as $insert_query ) { + $insert_success = $ydb->query( $insert_query ); + if( $insert_success ) { + $insert_query_count++; + $success_msg[] = 'Query '.$insert_query_count.'/'.sizeof($insert_queries).' executed successfully.'; + } else { + $error_msg[] = 'Error executing '.$insert_query_count.'/'.sizeof($insert_queries).'.'; + } + } + + // Check results of operations + if ( sizeof($create_tables) == $create_table_count && sizeof($insert_queries) == $insert_query_count ) { + $success_msg[] = 'YOURLS tables successfully created.'; + } else { + $error_msg[] = "Error creating YOURLS tables."; + } + + return array( 'success' => $success_msg, 'error' => $error_msg ); +} + +?> -- 2.45.0