4 * Check if mod_rewrite is enabled. Note: unused, not reliable enough.
7 function yourls_check_mod_rewrite() {
8 return yourls_apache_mod_loaded( 'mod_rewrite' );
12 * Check if server has MySQL 4.1+
15 function yourls_check_database_version() {
17 $version = $ydb->mysql_version();
18 // Check there was actually a connection to the DB
19 if( count( $ydb->captured_errors ) ) {
20 yourls_die ( yourls__( 'Incorrect DB config, or could not connect to DB' ), yourls__( 'Fatal error' ), 503 );
22 return ( version_compare( '4.1', $version ) <= 0 );
29 function yourls_check_php_version() {
30 return ( version_compare( '4.3', phpversion() ) <= 0 );
34 * Check if server is an Apache
37 function yourls_is_apache() {
39 strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) !== false
40 || strpos( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false
45 * Check if server is running IIS
48 function yourls_is_iis() {
49 return ( strpos( $_SERVER['SERVER_SOFTWARE'], 'IIS' ) !== false );
53 * Check if module exists in Apache config. Input string eg 'mod_rewrite', return true or $default. Stolen from WordPress
56 function yourls_apache_mod_loaded( $mod, $default = false ) {
57 if ( !yourls_is_apache() )
60 if ( function_exists( 'apache_get_modules' ) ) {
61 $mods = apache_get_modules();
62 if ( in_array( $mod, $mods ) )
64 } elseif ( function_exists( 'phpinfo' ) ) {
67 $phpinfo = ob_get_clean();
68 if ( false !== strpos( $phpinfo, $mod ) )
75 * Create .htaccess or web.config. Returns boolean
78 function yourls_create_htaccess() {
79 $host = parse_url( YOURLS_SITE );
80 $path = ( isset( $host['path'] ) ? $host['path'] : '' );
82 if ( yourls_is_iis() ) {
83 // Prepare content for a web.config file
85 '<?'.'xml version="1.0" encoding="UTF-8"?>',
87 ' <system.webServer>',
90 ' <rule name="YOURLS" stopProcessing="true">',
91 ' <match url="^(.*)$" ignoreCase="false" />',
93 ' <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />',
94 ' <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />',
96 ' <action type="Rewrite" url="'.$path.'/yourls-loader.php" appendQueryString="true" />',
100 ' </system.webServer>',
104 $filename = YOURLS_ABSPATH.'/web.config';
108 // Prepare content for a .htaccess file
110 '<IfModule mod_rewrite.c>',
112 'RewriteBase '.$path.'/',
113 'RewriteCond %{REQUEST_FILENAME} !-f',
114 'RewriteCond %{REQUEST_FILENAME} !-d',
115 'RewriteRule ^.*$ '.$path.'/yourls-loader.php [L]',
119 $filename = YOURLS_ABSPATH.'/.htaccess';
124 return ( yourls_insert_with_markers( $filename, $marker, $content ) );
128 * Inserts $insertion (text in an array of lines) into $filename (.htaccess) between BEGIN/END $marker block. Returns bool. Stolen from WP
131 function yourls_insert_with_markers( $filename, $marker, $insertion ) {
132 if ( !file_exists( $filename ) || is_writeable( $filename ) ) {
133 if ( !file_exists( $filename ) ) {
136 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
139 if ( !$f = @fopen( $filename, 'w' ) )
145 foreach ( $markerdata as $n => $markerline ) {
146 if ( strpos( $markerline, '# BEGIN ' . $marker ) !== false )
149 if ( $n + 1 < count( $markerdata ) )
150 fwrite( $f, "{$markerline}\n" );
152 fwrite( $f, "{$markerline}" );
154 if ( strpos( $markerline, '# END ' . $marker ) !== false ) {
155 if ( $marker != 'none' )
156 fwrite( $f, "# BEGIN {$marker}\n" );
157 if ( is_array( $insertion ) )
158 foreach ( $insertion as $insertline )
159 fwrite( $f, "{$insertline}\n" );
160 if ( $marker != 'none' )
161 fwrite( $f, "# END {$marker}\n" );
168 if ( $marker != 'none' )
169 fwrite( $f, "\n\n# BEGIN {$marker}\n" );
170 foreach ( $insertion as $insertline )
171 fwrite( $f, "{$insertline}\n" );
172 if ( $marker != 'none' )
173 fwrite( $f, "# END {$marker}\n\n" );
183 * Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )
186 function yourls_create_sql_tables() {
189 $error_msg = array();
190 $success_msg = array();
192 // Create Table Query
193 $create_tables = array();
194 $create_tables[YOURLS_DB_TABLE_URL] =
195 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('.
196 '`keyword` varchar(200) BINARY NOT NULL,'.
197 '`url` text BINARY NOT NULL,'.
198 '`title` text CHARACTER SET utf8,'.
199 '`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,'.
200 '`ip` VARCHAR(41) NOT NULL,'.
201 '`clicks` INT(10) UNSIGNED NOT NULL,'.
202 ' PRIMARY KEY (`keyword`),'.
203 ' KEY `timestamp` (`timestamp`),'.
207 $create_tables[YOURLS_DB_TABLE_OPTIONS] =
208 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.
209 '`option_id` bigint(20) unsigned NOT NULL auto_increment,'.
210 '`option_name` varchar(64) NOT NULL default "",'.
211 '`option_value` longtext NOT NULL,'.
212 'PRIMARY KEY (`option_id`,`option_name`),'.
213 'KEY `option_name` (`option_name`)'.
214 ') AUTO_INCREMENT=1 ;';
216 $create_tables[YOURLS_DB_TABLE_LOG] =
217 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.
218 '`click_id` int(11) NOT NULL auto_increment,'.
219 '`click_time` datetime NOT NULL,'.
220 '`shorturl` varchar(200) BINARY NOT NULL,'.
221 '`referrer` varchar(200) NOT NULL,'.
222 '`user_agent` varchar(255) NOT NULL,'.
223 '`ip_address` varchar(41) NOT NULL,'.
224 '`country_code` char(2) NOT NULL,'.
225 'PRIMARY KEY (`click_id`),'.
226 'KEY `shorturl` (`shorturl`)'.
227 ') AUTO_INCREMENT=1 ;';
230 $create_table_count = 0;
232 $ydb->show_errors = true;
235 foreach ( $create_tables as $table_name => $table_query ) {
236 $ydb->query( $table_query );
237 $create_success = $ydb->query( "SHOW TABLES LIKE '$table_name'" );
238 if( $create_success ) {
239 $create_table_count++;
240 $success_msg[] = yourls_s( "Table '%s' created.", $table_name );
242 $error_msg[] = yourls_s( "Error creating table '%s'.", $table_name );
246 // Insert data into tables
247 yourls_update_option( 'version', YOURLS_VERSION );
248 yourls_update_option( 'db_version', YOURLS_DB_VERSION );
249 yourls_update_option( 'next_id', 1 );
251 // Insert sample links
252 yourls_insert_link_in_db( 'http://planetozh.com/blog/', 'ozhblog', 'planetOzh: Ozh\' blog' );
253 yourls_insert_link_in_db( 'http://ozh.org/', 'ozh', 'ozh.org' );
254 yourls_insert_link_in_db( 'http://yourls.org/', 'yourls', 'YOURLS: Your Own URL Shortener' );
256 // Check results of operations
257 if ( sizeof( $create_tables ) == $create_table_count ) {
258 $success_msg[] = yourls__( 'YOURLS tables successfully created.' );
260 $error_msg[] = yourls__( 'Error creating YOURLS tables.' );
263 return array( 'success' => $success_msg, 'error' => $error_msg );
268 * Toggle maintenance mode. Inspired from WP. Returns true for success, false otherwise
271 function yourls_maintenance_mode( $maintenance = true ) {
273 $file = YOURLS_ABSPATH . '/.maintenance' ;
275 // Turn maintenance mode on : create .maintenance file
276 if ( (bool)$maintenance ) {
277 if ( ! ( $fp = @fopen( $file, 'w' ) ) )
280 $maintenance_string = '<?php $maintenance_start = ' . time() . '; ?>';
281 @fwrite( $fp, $maintenance_string );
283 @chmod( $file, 0644 ); // Read and write for owner, read for everybody else
285 // Not sure why the fwrite would fail if the fopen worked... Just in case
286 return( is_readable( $file ) );
288 // Turn maintenance mode off : delete the .maintenance file
290 return @unlink($file);