4 * Check if server has MySQL 5.0+
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( '5.0', $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( '5.2', 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>',
87 ' <requestFiltering allowDoubleEscaping="true" />',
91 ' <rule name="YOURLS" stopProcessing="true">',
92 ' <match url="^(.*)$" ignoreCase="false" />',
94 ' <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />',
95 ' <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />',
97 ' <action type="Rewrite" url="'.$path.'/yourls-loader.php" appendQueryString="true" />',
101 ' </system.webServer>',
105 $filename = YOURLS_ABSPATH.'/web.config';
109 // Prepare content for a .htaccess file
111 '<IfModule mod_rewrite.c>',
113 'RewriteBase '.$path.'/',
114 'RewriteCond %{REQUEST_FILENAME} !-f',
115 'RewriteCond %{REQUEST_FILENAME} !-d',
116 'RewriteRule ^.*$ '.$path.'/yourls-loader.php [L]',
120 $filename = YOURLS_ABSPATH.'/.htaccess';
125 return ( yourls_insert_with_markers( $filename, $marker, $content ) );
129 * Insert text into a file between BEGIN/END markers, return bool. Stolen from WP
131 * Inserts an array of strings into a file (eg .htaccess ), placing it between
132 * BEGIN and END markers. Replaces existing marked info. Retains surrounding
133 * data. Creates file if none exists.
137 * @param string $filename
138 * @param string $marker
139 * @param array $insertion
140 * @return bool True on write success, false on failure.
142 function yourls_insert_with_markers( $filename, $marker, $insertion ) {
143 if ( !file_exists( $filename ) || is_writeable( $filename ) ) {
144 if ( !file_exists( $filename ) ) {
147 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
150 if ( !$f = @fopen( $filename, 'w' ) )
156 foreach ( $markerdata as $n => $markerline ) {
157 if ( strpos( $markerline, '# BEGIN ' . $marker ) !== false )
160 if ( $n + 1 < count( $markerdata ) )
161 fwrite( $f, "{$markerline}\n" );
163 fwrite( $f, "{$markerline}" );
165 if ( strpos( $markerline, '# END ' . $marker ) !== false ) {
166 if ( $marker != 'none' )
167 fwrite( $f, "# BEGIN {$marker}\n" );
168 if ( is_array( $insertion ) )
169 foreach ( $insertion as $insertline )
170 fwrite( $f, "{$insertline}\n" );
171 if ( $marker != 'none' )
172 fwrite( $f, "# END {$marker}\n" );
179 if ( $marker != 'none' )
180 fwrite( $f, "\n\n# BEGIN {$marker}\n" );
181 foreach ( $insertion as $insertline )
182 fwrite( $f, "{$insertline}\n" );
183 if ( $marker != 'none' )
184 fwrite( $f, "# END {$marker}\n\n" );
194 * Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )
197 * @return array An array like array( 'success' => array of success strings, 'errors' => array of error strings )
199 function yourls_create_sql_tables() {
200 // Allow plugins (most likely a custom db.php layer in user dir) to short-circuit the whole function
201 $pre = yourls_apply_filter( 'shunt_yourls_create_sql_tables', null );
202 // your filter function should return an array of ( 'success' => $success_msg, 'error' => $error_msg ), see below
203 if ( null !== $pre ) {
209 $error_msg = array();
210 $success_msg = array();
212 // Create Table Query
213 $create_tables = array();
214 $create_tables[YOURLS_DB_TABLE_URL] =
215 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('.
216 '`keyword` varchar(200) BINARY NOT NULL,'.
217 '`url` text BINARY NOT NULL,'.
218 '`title` text CHARACTER SET utf8,'.
219 '`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,'.
220 '`ip` VARCHAR(41) NOT NULL,'.
221 '`clicks` INT(10) UNSIGNED NOT NULL,'.
222 ' PRIMARY KEY (`keyword`),'.
223 ' KEY `timestamp` (`timestamp`),'.
227 $create_tables[YOURLS_DB_TABLE_OPTIONS] =
228 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.
229 '`option_id` bigint(20) unsigned NOT NULL auto_increment,'.
230 '`option_name` varchar(64) NOT NULL default "",'.
231 '`option_value` longtext NOT NULL,'.
232 'PRIMARY KEY (`option_id`,`option_name`),'.
233 'KEY `option_name` (`option_name`)'.
234 ') AUTO_INCREMENT=1 ;';
236 $create_tables[YOURLS_DB_TABLE_LOG] =
237 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.
238 '`click_id` int(11) NOT NULL auto_increment,'.
239 '`click_time` datetime NOT NULL,'.
240 '`shorturl` varchar(200) BINARY NOT NULL,'.
241 '`referrer` varchar(200) NOT NULL,'.
242 '`user_agent` varchar(255) NOT NULL,'.
243 '`ip_address` varchar(41) NOT NULL,'.
244 '`country_code` char(2) NOT NULL,'.
245 'PRIMARY KEY (`click_id`),'.
246 'KEY `shorturl` (`shorturl`)'.
247 ') AUTO_INCREMENT=1 ;';
250 $create_table_count = 0;
252 $ydb->show_errors = true;
255 foreach ( $create_tables as $table_name => $table_query ) {
256 $ydb->query( $table_query );
257 $create_success = $ydb->query( "SHOW TABLES LIKE '$table_name'" );
258 if( $create_success ) {
259 $create_table_count++;
260 $success_msg[] = yourls_s( "Table '%s' created.", $table_name );
262 $error_msg[] = yourls_s( "Error creating table '%s'.", $table_name );
266 // Initializes the option table
267 if( !yourls_initialize_options() )
268 $error_msg[] = yourls__( 'Could not initialize options' );
270 // Insert sample links
271 if( !yourls_insert_sample_links() )
272 $error_msg[] = yourls__( 'Could not insert sample short URLs' );
274 // Check results of operations
275 if ( sizeof( $create_tables ) == $create_table_count ) {
276 $success_msg[] = yourls__( 'YOURLS tables successfully created.' );
278 $error_msg[] = yourls__( 'Error creating YOURLS tables.' );
281 return array( 'success' => $success_msg, 'error' => $error_msg );
285 * Initializes the option table
287 * Each yourls_update_option() returns either true on success (option updated) or false on failure (new value == old value, or
288 * for some reason it could not save to DB).
289 * Since true & true & true = 1, we cast it to boolean type to return true (or false)
294 function yourls_initialize_options() {
296 yourls_update_option( 'version', YOURLS_VERSION )
297 & yourls_update_option( 'db_version', YOURLS_DB_VERSION )
298 & yourls_update_option( 'next_id', 1 )
303 * Populates the URL table with a few sample links
308 function yourls_insert_sample_links() {
309 $link1 = yourls_add_new_link( 'http://blog.yourls.org/', 'yourlsblog', 'YOURLS\' Blog' );
310 $link2 = yourls_add_new_link( 'http://yourls.org/', 'yourls', 'YOURLS: Your Own URL Shortener' );
311 $link3 = yourls_add_new_link( 'http://ozh.org/', 'ozh', 'ozh.org' );
313 $link1['status'] == 'success'
314 & $link2['status'] == 'success'
315 & $link3['status'] == 'success'
321 * Toggle maintenance mode. Inspired from WP. Returns true for success, false otherwise
324 function yourls_maintenance_mode( $maintenance = true ) {
326 $file = YOURLS_ABSPATH . '/.maintenance' ;
328 // Turn maintenance mode on : create .maintenance file
329 if ( (bool)$maintenance ) {
330 if ( ! ( $fp = @fopen( $file, 'w' ) ) )
333 $maintenance_string = '<?php $maintenance_start = ' . time() . '; ?>';
334 @fwrite( $fp, $maintenance_string );
336 @chmod( $file, 0644 ); // Read and write for owner, read for everybody else
338 // Not sure why the fwrite would fail if the fopen worked... Just in case
339 return( is_readable( $file ) );
341 // Turn maintenance mode off : delete the .maintenance file
343 return @unlink($file);