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 extension cURL is enabled
15 function yourls_check_curl() {
16 return function_exists( 'curl_init' );
20 * Check if server has MySQL 4.1+
23 function yourls_check_database_version() {
25 return ( version_compare( '4.1', $ydb->mysql_version() ) <= 0 );
32 function yourls_check_php_version() {
33 return ( version_compare( '4.3', phpversion() ) <= 0 );
37 * Check if server is an Apache
40 function yourls_is_apache() {
42 strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) !== false
43 || strpos( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false
48 * Check if server is running IIS
51 function yourls_is_iis() {
52 return ( strpos( $_SERVER['SERVER_SOFTWARE'], 'IIS' ) !== false );
56 * Check if module exists in Apache config. Input string eg 'mod_rewrite', return true or $default. Stolen from WordPress
59 function yourls_apache_mod_loaded( $mod, $default = false ) {
60 if ( !yourls_is_apache() )
63 if ( function_exists( 'apache_get_modules' ) ) {
64 $mods = apache_get_modules();
65 if ( in_array( $mod, $mods ) )
67 } elseif ( function_exists( 'phpinfo' ) ) {
70 $phpinfo = ob_get_clean();
71 if ( false !== strpos( $phpinfo, $mod ) )
78 * Create .htaccess or web.config. Returns boolean
81 function yourls_create_htaccess() {
82 $host = parse_url( YOURLS_SITE );
83 $path = ( isset( $host['path'] ) ? $host['path'] : '' );
85 if ( yourls_is_iis() ) {
86 // Prepare content for a web.config file
88 '<?'.'xml version="1.0" encoding="UTF-8"?>',
90 ' <system.webServer>',
93 ' <rule name="YOURLS" stopProcessing="true">',
94 ' <match url="^(.*)$" ignoreCase="false" />',
96 ' <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />',
97 ' <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />',
99 ' <action type="Rewrite" url="'.$path.'/yourls-loader.php" appendQueryString="true" />',
103 ' </system.webServer>',
107 $filename = YOURLS_ABSPATH.'/web.config';
111 // Prepare content for a .htaccess file
113 '<IfModule mod_rewrite.c>',
115 'RewriteBase '.$path.'/',
116 'RewriteCond %{REQUEST_FILENAME} !-f',
117 'RewriteCond %{REQUEST_FILENAME} !-d',
118 'RewriteRule ^.*$ '.$path.'/yourls-loader.php [L]',
122 $filename = YOURLS_ABSPATH.'/.htaccess';
127 return ( yourls_insert_with_markers( $filename, $marker, $content ) );
131 * Inserts $insertion (text in an array of lines) into $filename (.htaccess) between BEGIN/END $marker block. Returns bool. Stolen from WP
134 function yourls_insert_with_markers( $filename, $marker, $insertion ) {
135 if ( !file_exists( $filename ) || is_writeable( $filename ) ) {
136 if ( !file_exists( $filename ) ) {
139 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
142 if ( !$f = @fopen( $filename, 'w' ) )
148 foreach ( $markerdata as $n => $markerline ) {
149 if ( strpos( $markerline, '# BEGIN ' . $marker ) !== false )
152 if ( $n + 1 < count( $markerdata ) )
153 fwrite( $f, "{$markerline}\n" );
155 fwrite( $f, "{$markerline}" );
157 if ( strpos( $markerline, '# END ' . $marker ) !== false ) {
158 if ( $marker != 'none' )
159 fwrite( $f, "# BEGIN {$marker}\n" );
160 if ( is_array( $insertion ) )
161 foreach ( $insertion as $insertline )
162 fwrite( $f, "{$insertline}\n" );
163 if ( $marker != 'none' )
164 fwrite( $f, "# END {$marker}\n" );
171 if ( $marker != 'none' )
172 fwrite( $f, "\n\n# BEGIN {$marker}\n" );
173 foreach ( $insertion as $insertline )
174 fwrite( $f, "{$insertline}\n" );
175 if ( $marker != 'none' )
176 fwrite( $f, "# END {$marker}\n\n" );
186 * Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )
189 function yourls_create_sql_tables() {
192 $error_msg = array();
193 $success_msg = array();
195 // Create Table Query
196 $create_tables = array();
197 $create_tables[YOURLS_DB_TABLE_URL] =
198 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('.
199 '`keyword` varchar(200) BINARY NOT NULL,'.
200 '`url` text BINARY NOT NULL,'.
201 '`title` text CHARACTER SET utf8,'.
202 '`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,'.
203 '`ip` VARCHAR(41) NOT NULL,'.
204 '`clicks` INT(10) UNSIGNED NOT NULL,'.
205 ' PRIMARY KEY (`keyword`),'.
206 ' KEY `timestamp` (`timestamp`),'.
210 $create_tables[YOURLS_DB_TABLE_OPTIONS] =
211 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.
212 '`option_id` bigint(20) unsigned NOT NULL auto_increment,'.
213 '`option_name` varchar(64) NOT NULL default "",'.
214 '`option_value` longtext NOT NULL,'.
215 'PRIMARY KEY (`option_id`,`option_name`),'.
216 'KEY `option_name` (`option_name`)'.
217 ') AUTO_INCREMENT=1 ;';
219 $create_tables[YOURLS_DB_TABLE_LOG] =
220 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.
221 '`click_id` int(11) NOT NULL auto_increment,'.
222 '`click_time` datetime NOT NULL,'.
223 '`shorturl` varchar(200) BINARY NOT NULL,'.
224 '`referrer` varchar(200) NOT NULL,'.
225 '`user_agent` varchar(255) NOT NULL,'.
226 '`ip_address` varchar(41) NOT NULL,'.
227 '`country_code` char(2) NOT NULL,'.
228 'PRIMARY KEY (`click_id`),'.
229 'KEY `shorturl` (`shorturl`)'.
230 ') AUTO_INCREMENT=1 ;';
233 $create_table_count = 0;
235 $ydb->show_errors = true;
238 foreach ( $create_tables as $table_name => $table_query ) {
239 $ydb->query( $table_query );
240 $create_success = $ydb->query( "SHOW TABLES LIKE '$table_name'" );
241 if( $create_success ) {
242 $create_table_count++;
243 $success_msg[] = yourls_s( "Table '%s' created.", $table_name );
245 $error_msg[] = yourls_s( "Error creating table '%s'.", $table_name );
249 // Insert data into tables
250 yourls_update_option( 'version', YOURLS_VERSION );
251 yourls_update_option( 'db_version', YOURLS_DB_VERSION );
252 yourls_update_option( 'next_id', 1 );
254 // Insert sample links
255 yourls_insert_link_in_db( 'http://planetozh.com/blog/', 'ozhblog', 'planetOzh: Ozh\' blog' );
256 yourls_insert_link_in_db( 'http://ozh.org/', 'ozh', 'ozh.org' );
257 yourls_insert_link_in_db( 'http://yourls.org/', 'yourls', 'YOURLS: Your Own URL Shortener' );
259 // Check results of operations
260 if ( sizeof( $create_tables ) == $create_table_count ) {
261 $success_msg[] = yourls__( 'YOURLS tables successfully created.' );
263 $error_msg[] = yourls__( 'Error creating YOURLS tables.' );
266 return array( 'success' => $success_msg, 'error' => $error_msg );
271 * Toggle maintenance mode. Inspired from WP. Returns true for success, false otherwise
274 function yourls_maintenance_mode( $maintenance = true ) {
276 $file = YOURLS_ABSPATH . '/.maintenance' ;
278 // Turn maintenance mode on : create .maintenance file
279 if ( (bool)$maintenance ) {
280 if ( ! ( $fp = @fopen( $file, 'w' ) ) )
283 $maintenance_string = '<?php $maintenance_start = ' . time() . '; ?>';
284 @fwrite( $fp, $maintenance_string );
286 @chmod( $file, 0644 ); // Read and write for owner, read for everybody else
288 // Not sure why the fwrite would fail if the fopen worked... Just in case
289 return( is_readable( $file ) );
291 // Turn maintenance mode off : delete the .maintenance file
293 return @unlink($file);