]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-install.php
Fix end of line
[Github/YOURLS.git] / includes / functions-install.php
1 <?php
2
3 /**
4  * Check if mod_rewrite is enabled. Note: unused, not reliable enough.
5  *
6  */
7 function yourls_check_mod_rewrite() {
8         return yourls_apache_mod_loaded( 'mod_rewrite' );
9 }
10
11 /**
12  * Check if extension cURL is enabled
13  *
14  */
15 function yourls_check_curl() {
16         return function_exists( 'curl_init' );
17 }
18
19 /**
20  * Check if server has MySQL 4.1+
21  *
22  */
23 function yourls_check_database_version() {
24         global $ydb;
25         return ( version_compare( '4.1', $ydb->mysql_version() ) <= 0 );
26 }
27
28 /**
29  * Check if PHP > 4.3
30  *
31  */
32 function yourls_check_php_version() {
33         return ( version_compare( '4.3', phpversion() ) <= 0 );
34 }
35
36 /**
37  * Check if server is an Apache
38  *
39  */
40 function yourls_is_apache() {
41         return (
42            strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) !== false
43         || strpos( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false
44         );
45 }
46
47 /**
48  * Check if server is running IIS
49  *
50  */
51 function yourls_is_iis() {
52         return ( strpos( $_SERVER['SERVER_SOFTWARE'], 'IIS' ) !== false );
53 }
54
55 /**
56  * Check if module exists in Apache config. Input string eg 'mod_rewrite', return true or $default. Stolen from WordPress
57  *
58  */
59 function yourls_apache_mod_loaded( $mod, $default = false ) {
60         if ( !yourls_is_apache() )
61                 return false;
62
63         if ( function_exists( 'apache_get_modules' ) ) {
64                 $mods = apache_get_modules();
65                 if ( in_array( $mod, $mods ) )
66                         return true;
67         } elseif ( function_exists( 'phpinfo' ) ) {
68                         ob_start();
69                         phpinfo( 8 );
70                         $phpinfo = ob_get_clean();
71                         if ( false !== strpos( $phpinfo, $mod ) )
72                                 return true;
73         }
74         return $default;
75 }
76
77 /**
78  * Create .htaccess or web.config. Returns boolean
79  *
80  */
81 function yourls_create_htaccess() {
82         $host = parse_url( YOURLS_SITE );
83         $path = ( isset( $host['path'] ) ? $host['path'] : '' );
84
85     if ( yourls_is_iis() ) {
86                 // Prepare content for a web.config file
87             $content = array(
88             '<?'.'xml version="1.0" encoding="UTF-8"?>',
89             '<configuration>', 
90             '    <system.webServer>',
91             '        <rewrite>',
92             '            <rules>',
93             '                <rule name="YOURLS" stopProcessing="true">',
94             '                    <match url="^(.*)$" ignoreCase="false" />',
95             '                    <conditions>',
96             '                        <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />',
97             '                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />',
98             '                    </conditions>',
99             '                    <action type="Rewrite" url="'.$path.'/yourls-loader.php" appendQueryString="true" />',
100             '                </rule>',
101             '            </rules>',
102             '        </rewrite>',
103             '    </system.webServer>',
104             '</configuration>',
105             );
106         
107             $filename = YOURLS_ABSPATH.'/web.config';
108                 $marker = 'none';
109
110     } else {
111                 // Prepare content for a .htaccess file
112             $content = array(
113                     '<IfModule mod_rewrite.c>',
114                     'RewriteEngine On',
115                     'RewriteBase '.$path.'/',
116                     'RewriteCond %{REQUEST_FILENAME} !-f',
117                     'RewriteCond %{REQUEST_FILENAME} !-d',
118                     'RewriteRule ^.*$ '.$path.'/yourls-loader.php [L]',
119                     '</IfModule>',
120             );
121         
122             $filename = YOURLS_ABSPATH.'/.htaccess';
123                 $marker = 'YOURLS';
124                 
125         }
126         
127         return ( yourls_insert_with_markers( $filename, $marker, $content ) );
128 }
129
130 /**
131  * Inserts $insertion (text in an array of lines) into $filename (.htaccess) between BEGIN/END $marker block. Returns bool. Stolen from WP
132  *
133  */
134 function yourls_insert_with_markers( $filename, $marker, $insertion ) {
135         if ( !file_exists( $filename ) || is_writeable( $filename ) ) {
136                 if ( !file_exists( $filename ) ) {
137                         $markerdata = '';
138                 } else {
139                         $markerdata = explode( "\n", implode( '', file( $filename ) ) );
140                 }
141
142                 if ( !$f = @fopen( $filename, 'w' ) )
143                         return false;
144
145                 $foundit = false;
146                 if ( $markerdata ) {
147                         $state = true;
148                         foreach ( $markerdata as $n => $markerline ) {
149                                 if ( strpos( $markerline, '# BEGIN ' . $marker ) !== false )
150                                         $state = false;
151                                 if ( $state ) {
152                                         if ( $n + 1 < count( $markerdata ) )
153                                                 fwrite( $f, "{$markerline}\n" );
154                                         else
155                                                 fwrite( $f, "{$markerline}" );
156                                 }
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" );
165                                         $state = true;
166                                         $foundit = true;
167                                 }
168                         }
169                 }
170                 if ( !$foundit ) {
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" );
177                 }
178                 fclose( $f );
179                 return true;
180         } else {
181                 return false;
182         }
183 }
184
185 /**
186  * Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )
187  *
188  */
189 function yourls_create_sql_tables() {
190         global $ydb;
191         
192         $error_msg = array();
193         $success_msg = array();
194
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`),'.
207                 ' KEY `ip` (`ip`)'.
208                 ');';
209
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 ;';
218                 
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 ;';
231
232
233         $create_table_count = 0;
234         
235         $ydb->show_errors = true;
236         
237         // Create tables
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 ); 
244                 } else {
245                         $error_msg[] = yourls_s( "Error creating table '%s'.", $table_name ); 
246                 }
247         }
248                 
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 );
253         
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' );
258                 
259         // Check results of operations
260         if ( sizeof( $create_tables ) == $create_table_count ) {
261                 $success_msg[] = yourls__( 'YOURLS tables successfully created.' );
262         } else {
263                 $error_msg[] = yourls__( 'Error creating YOURLS tables.' ); 
264         }
265
266         return array( 'success' => $success_msg, 'error' => $error_msg );
267 }
268
269
270 /**
271  * Toggle maintenance mode. Inspired from WP. Returns true for success, false otherwise
272  *
273  */
274 function yourls_maintenance_mode( $maintenance = true ) {
275
276         $file = YOURLS_ABSPATH . '/.maintenance' ;
277
278         // Turn maintenance mode on : create .maintenance file
279         if ( (bool)$maintenance ) {
280                 if ( ! ( $fp = @fopen( $file, 'w' ) ) )
281                         return false;
282                 
283                 $maintenance_string = '<?php $maintenance_start = ' . time() . '; ?>';
284                 @fwrite( $fp, $maintenance_string );
285                 @fclose( $fp );
286                 @chmod( $file, 0644 ); // Read and write for owner, read for everybody else
287
288                 // Not sure why the fwrite would fail if the fopen worked... Just in case
289                 return( is_readable( $file ) );
290                 
291         // Turn maintenance mode off : delete the .maintenance file
292         } else {
293                 return @unlink($file);
294         }
295 }