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 = preg_replace( '/[^0-9.]/', '', $ydb->mysql_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 );
26 function yourls_check_php_version() {
27 return ( version_compare( '4.3', phpversion() ) <= 0 );
31 * Check if server is an Apache
34 function yourls_is_apache() {
35 if( !array_key_exists( 'SERVER_SOFTWARE', $_SERVER ) )
38 strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) !== false
39 || strpos( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false
44 * Check if server is running IIS
47 function yourls_is_iis() {
48 return ( array_key_exists( 'SERVER_SOFTWARE', $_SERVER ) ? ( strpos( $_SERVER['SERVER_SOFTWARE'], 'IIS' ) !== false ) : false );
53 * Create .htaccess or web.config. Returns boolean
56 function yourls_create_htaccess() {
57 $host = parse_url( YOURLS_SITE );
58 $path = ( isset( $host['path'] ) ? $host['path'] : '' );
60 if ( yourls_is_iis() ) {
61 // Prepare content for a web.config file
63 '<?'.'xml version="1.0" encoding="UTF-8"?>',
65 ' <system.webServer>',
68 ' <rule name="YOURLS" stopProcessing="true">',
69 ' <match url="^(.*)$" ignoreCase="false" />',
71 ' <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />',
72 ' <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />',
74 ' <action type="Rewrite" url="'.$path.'/yourls-loader.php" appendQueryString="true" />',
78 ' </system.webServer>',
82 $filename = YOURLS_ABSPATH.'/web.config';
86 // Prepare content for a .htaccess file
88 '<IfModule mod_rewrite.c>',
90 'RewriteBase '.$path.'/',
91 'RewriteCond %{REQUEST_FILENAME} !-f',
92 'RewriteCond %{REQUEST_FILENAME} !-d',
93 'RewriteRule ^.*$ '.$path.'/yourls-loader.php [L]',
97 $filename = YOURLS_ABSPATH.'/.htaccess';
102 return ( yourls_insert_with_markers( $filename, $marker, $content ) );
106 * Inserts $insertion (text in an array of lines) into $filename (.htaccess) between BEGIN/END $marker block. Returns bool. Stolen from WP
109 function yourls_insert_with_markers( $filename, $marker, $insertion ) {
110 if ( !file_exists( $filename ) || is_writeable( $filename ) ) {
111 if ( !file_exists( $filename ) ) {
114 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
117 if ( !$f = @fopen( $filename, 'w' ) )
123 foreach ( $markerdata as $n => $markerline ) {
124 if ( strpos( $markerline, '# BEGIN ' . $marker ) !== false )
127 if ( $n + 1 < count( $markerdata ) )
128 fwrite( $f, "{$markerline}\n" );
130 fwrite( $f, "{$markerline}" );
132 if ( strpos( $markerline, '# END ' . $marker ) !== false ) {
133 if ( $marker != 'none' )
134 fwrite( $f, "# BEGIN {$marker}\n" );
135 if ( is_array( $insertion ) )
136 foreach ( $insertion as $insertline )
137 fwrite( $f, "{$insertline}\n" );
138 if ( $marker != 'none' )
139 fwrite( $f, "# END {$marker}\n" );
146 if ( $marker != 'none' )
147 fwrite( $f, "\n\n# BEGIN {$marker}\n" );
148 foreach ( $insertion as $insertline )
149 fwrite( $f, "{$insertline}\n" );
150 if ( $marker != 'none' )
151 fwrite( $f, "# END {$marker}\n\n" );
161 * Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )
164 function yourls_create_sql_tables() {
167 $error_msg = array();
168 $success_msg = array();
170 // Create Table Query
171 $create_tables = array();
172 $create_tables[YOURLS_DB_TABLE_URL] =
173 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('.
174 '`keyword` varchar(200) BINARY NOT NULL,'.
175 '`url` text BINARY NOT NULL,'.
176 '`title` text CHARACTER SET utf8,'.
177 '`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,'.
178 '`ip` VARCHAR(41) NOT NULL,'.
179 '`clicks` INT(10) UNSIGNED NOT NULL,'.
180 ' PRIMARY KEY (`keyword`),'.
181 ' KEY `timestamp` (`timestamp`),'.
185 $create_tables[YOURLS_DB_TABLE_OPTIONS] =
186 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.
187 '`option_id` bigint(20) unsigned NOT NULL auto_increment,'.
188 '`option_name` varchar(64) NOT NULL default "",'.
189 '`option_value` longtext NOT NULL,'.
190 'PRIMARY KEY (`option_id`,`option_name`),'.
191 'KEY `option_name` (`option_name`)'.
192 ') AUTO_INCREMENT=1 ;';
194 $create_tables[YOURLS_DB_TABLE_LOG] =
195 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.
196 '`click_id` int(11) NOT NULL auto_increment,'.
197 '`click_time` datetime NOT NULL,'.
198 '`shorturl` varchar(200) BINARY NOT NULL,'.
199 '`referrer` varchar(200) NOT NULL,'.
200 '`user_agent` varchar(255) NOT NULL,'.
201 '`ip_address` varchar(41) NOT NULL,'.
202 '`country_code` char(2) NOT NULL,'.
203 'PRIMARY KEY (`click_id`),'.
204 'KEY `shorturl` (`shorturl`)'.
205 ') AUTO_INCREMENT=1 ;';
208 $create_table_count = 0;
210 $ydb->show_errors = true;
213 foreach ( $create_tables as $table_name => $table_query ) {
214 $ydb->query( $table_query );
215 $create_success = $ydb->query( "SHOW TABLES LIKE '$table_name'" );
216 if( $create_success ) {
217 $create_table_count++;
218 $success_msg[] = yourls_s( "Table '%s' created.", $table_name );
220 $error_msg[] = yourls_s( "Error creating table '%s'.", $table_name );
224 // Insert data into tables
225 yourls_update_option( 'version', YOURLS_VERSION );
226 yourls_update_option( 'db_version', YOURLS_DB_VERSION );
227 yourls_update_option( 'next_id', 1 );
229 // Insert sample links
230 yourls_insert_link_in_db( 'http://planetozh.com/blog/', 'ozhblog', 'planetOzh: Ozh\' blog' );
231 yourls_insert_link_in_db( 'http://ozh.org/', 'ozh', 'ozh.org' );
232 yourls_insert_link_in_db( 'http://yourls.org/', 'yourls', 'YOURLS: Your Own URL Shortener' );
234 // Check results of operations
235 if ( sizeof( $create_tables ) == $create_table_count ) {
236 $success_msg[] = yourls__( 'YOURLS tables successfully created.' );
238 $error_msg[] = yourls__( 'Error creating YOURLS tables.' );
241 return array( 'success' => $success_msg, 'error' => $error_msg );
246 * Toggle maintenance mode. Inspired from WP. Returns true for success, false otherwise
249 function yourls_maintenance_mode( $maintenance = true ) {
251 $file = YOURLS_ABSPATH . '/.maintenance' ;
253 // Turn maintenance mode on : create .maintenance file
254 if ( (bool)$maintenance ) {
255 if ( ! ( $fp = @fopen( $file, 'w' ) ) )
258 $maintenance_string = '<?php $maintenance_start = ' . time() . '; ?>';
259 @fwrite( $fp, $maintenance_string );
261 @chmod( $file, 0644 ); // Read and write for owner, read for everybody else
263 // Not sure why the fwrite would fail if the fopen worked... Just in case
264 return( is_readable( $file ) );
266 // Turn maintenance mode off : delete the .maintenance file
268 return @unlink($file);