4 * Check if server has MySQL 4.1+
7 function yourls_check_database_version() {
10 // Attempt to get MySQL server version, check result and if error count increased
11 $num_errors1 = count( $ydb->captured_errors );
12 $version = yourls_get_database_version();
13 $num_errors2 = count( $ydb->captured_errors );
15 if( $version == NULL || ( $num_errors2 > $num_errors1 ) ) {
16 yourls_die( yourls__( 'Incorrect DB config, or could not connect to DB' ), yourls__( 'Fatal error' ), 503 );
19 return ( version_compare( '4.1', $version ) <= 0 );
25 * The regex removes everything that's not a number at the start of the string, or remove anything that's not a number and what
27 * 'omgmysql-5.5-ubuntu-4.20' => '5.5'
28 * 'mysql5.5-ubuntu-4.20' => '5.5'
29 * '5.5-ubuntu-4.20' => '5.5'
30 * '5.5-beta2' => '5.5'
34 * @return string sanitized DB version
36 function yourls_get_database_version() {
39 return preg_replace( '/(^[^0-9]*)|[^0-9.].*/', '', $ydb->mysql_version() );
46 function yourls_check_php_version() {
47 return ( version_compare( '4.3', phpversion() ) <= 0 );
51 * Check if server is an Apache
54 function yourls_is_apache() {
55 if( !array_key_exists( 'SERVER_SOFTWARE', $_SERVER ) )
58 strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) !== false
59 || strpos( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false
64 * Check if server is running IIS
67 function yourls_is_iis() {
68 return ( array_key_exists( 'SERVER_SOFTWARE', $_SERVER ) ? ( strpos( $_SERVER['SERVER_SOFTWARE'], 'IIS' ) !== false ) : false );
73 * Create .htaccess or web.config. Returns boolean
76 function yourls_create_htaccess() {
77 $host = parse_url( YOURLS_SITE );
78 $path = ( isset( $host['path'] ) ? $host['path'] : '' );
80 if ( yourls_is_iis() ) {
81 // Prepare content for a web.config file
83 '<?'.'xml version="1.0" encoding="UTF-8"?>',
85 ' <system.webServer>',
88 ' <rule name="YOURLS" stopProcessing="true">',
89 ' <match url="^(.*)$" ignoreCase="false" />',
91 ' <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />',
92 ' <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />',
94 ' <action type="Rewrite" url="'.$path.'/yourls-loader.php" appendQueryString="true" />',
98 ' </system.webServer>',
102 $filename = YOURLS_ABSPATH.'/web.config';
106 // Prepare content for a .htaccess file
108 '<IfModule mod_rewrite.c>',
110 'RewriteBase '.$path.'/',
111 'RewriteCond %{REQUEST_FILENAME} !-f',
112 'RewriteCond %{REQUEST_FILENAME} !-d',
113 'RewriteRule ^.*$ '.$path.'/yourls-loader.php [L]',
117 $filename = YOURLS_ABSPATH.'/.htaccess';
122 return ( yourls_insert_with_markers( $filename, $marker, $content ) );
126 * Inserts $insertion (text in an array of lines) into $filename (.htaccess) between BEGIN/END $marker block. Returns bool. Stolen from WP
129 function yourls_insert_with_markers( $filename, $marker, $insertion ) {
130 if ( !file_exists( $filename ) || is_writeable( $filename ) ) {
131 if ( !file_exists( $filename ) ) {
134 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
137 if ( !$f = @fopen( $filename, 'w' ) )
143 foreach ( $markerdata as $n => $markerline ) {
144 if ( strpos( $markerline, '# BEGIN ' . $marker ) !== false )
147 if ( $n + 1 < count( $markerdata ) )
148 fwrite( $f, "{$markerline}\n" );
150 fwrite( $f, "{$markerline}" );
152 if ( strpos( $markerline, '# END ' . $marker ) !== false ) {
153 if ( $marker != 'none' )
154 fwrite( $f, "# BEGIN {$marker}\n" );
155 if ( is_array( $insertion ) )
156 foreach ( $insertion as $insertline )
157 fwrite( $f, "{$insertline}\n" );
158 if ( $marker != 'none' )
159 fwrite( $f, "# END {$marker}\n" );
166 if ( $marker != 'none' )
167 fwrite( $f, "\n\n# BEGIN {$marker}\n" );
168 foreach ( $insertion as $insertline )
169 fwrite( $f, "{$insertline}\n" );
170 if ( $marker != 'none' )
171 fwrite( $f, "# END {$marker}\n\n" );
181 * Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )
184 function yourls_create_sql_tables() {
187 $error_msg = array();
188 $success_msg = array();
190 // Create Table Query
191 $create_tables = array();
192 $create_tables[YOURLS_DB_TABLE_URL] =
193 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('.
194 '`keyword` varchar(200) BINARY NOT NULL,'.
195 '`url` text BINARY NOT NULL,'.
196 '`title` text CHARACTER SET utf8,'.
197 '`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,'.
198 '`ip` VARCHAR(41) NOT NULL,'.
199 '`clicks` INT(10) UNSIGNED NOT NULL,'.
200 ' PRIMARY KEY (`keyword`),'.
201 ' KEY `timestamp` (`timestamp`),'.
205 $create_tables[YOURLS_DB_TABLE_OPTIONS] =
206 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.
207 '`option_id` bigint(20) unsigned NOT NULL auto_increment,'.
208 '`option_name` varchar(64) NOT NULL default "",'.
209 '`option_value` longtext NOT NULL,'.
210 'PRIMARY KEY (`option_id`,`option_name`),'.
211 'KEY `option_name` (`option_name`)'.
212 ') AUTO_INCREMENT=1 ;';
214 $create_tables[YOURLS_DB_TABLE_LOG] =
215 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.
216 '`click_id` int(11) NOT NULL auto_increment,'.
217 '`click_time` datetime NOT NULL,'.
218 '`shorturl` varchar(200) BINARY NOT NULL,'.
219 '`referrer` varchar(200) NOT NULL,'.
220 '`user_agent` varchar(255) NOT NULL,'.
221 '`ip_address` varchar(41) NOT NULL,'.
222 '`country_code` char(2) NOT NULL,'.
223 'PRIMARY KEY (`click_id`),'.
224 'KEY `shorturl` (`shorturl`)'.
225 ') AUTO_INCREMENT=1 ;';
228 $create_table_count = 0;
230 $ydb->show_errors = true;
233 foreach ( $create_tables as $table_name => $table_query ) {
234 $ydb->query( $table_query );
235 $create_success = $ydb->query( "SHOW TABLES LIKE '$table_name'" );
236 if( $create_success ) {
237 $create_table_count++;
238 $success_msg[] = yourls_s( "Table '%s' created.", $table_name );
240 $error_msg[] = yourls_s( "Error creating table '%s'.", $table_name );
244 // Insert data into tables
245 yourls_update_option( 'version', YOURLS_VERSION );
246 yourls_update_option( 'db_version', YOURLS_DB_VERSION );
247 yourls_update_option( 'next_id', 1 );
249 // Insert sample links
250 yourls_insert_link_in_db( 'http://planetozh.com/blog/', 'ozhblog', 'planetOzh: Ozh\' blog' );
251 yourls_insert_link_in_db( 'http://ozh.org/', 'ozh', 'ozh.org' );
252 yourls_insert_link_in_db( 'http://yourls.org/', 'yourls', 'YOURLS: Your Own URL Shortener' );
254 // Check results of operations
255 if ( sizeof( $create_tables ) == $create_table_count ) {
256 $success_msg[] = yourls__( 'YOURLS tables successfully created.' );
258 $error_msg[] = yourls__( 'Error creating YOURLS tables.' );
261 return array( 'success' => $success_msg, 'error' => $error_msg );
266 * Toggle maintenance mode. Inspired from WP. Returns true for success, false otherwise
269 function yourls_maintenance_mode( $maintenance = true ) {
271 $file = YOURLS_ABSPATH . '/.maintenance' ;
273 // Turn maintenance mode on : create .maintenance file
274 if ( (bool)$maintenance ) {
275 if ( ! ( $fp = @fopen( $file, 'w' ) ) )
278 $maintenance_string = '<?php $maintenance_start = ' . time() . '; ?>';
279 @fwrite( $fp, $maintenance_string );
281 @chmod( $file, 0644 ); // Read and write for owner, read for everybody else
283 // Not sure why the fwrite would fail if the fopen worked... Just in case
284 return( is_readable( $file ) );
286 // Turn maintenance mode off : delete the .maintenance file
288 return @unlink($file);