]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-install.php
Fixing install procedure that was crappily broken
[Github/YOURLS.git] / includes / functions-install.php
1 <?php\r
2 \r
3 // Check if YOURLS is installed\r
4 function yourls_is_installed() {\r
5         static $is_installed = false;\r
6         if (!$is_installed) {\r
7                 global $ydb;\r
8                 if( YOURLS_VERSION == '1.3-RC1' ) {\r
9                         $is_installed = $ydb->get_var('SELECT next_id FROM '.YOURLS_DB_TABLE_NEXTDEC);\r
10                 } else {\r
11                         $is_installed = yourls_get_option( 'YOURLS_VERSION' );\r
12                 }\r
13         }\r
14         return (bool)$is_installed;\r
15 }\r
16 \r
17 // Check if mod_rewrite is enabled\r
18 function yourls_check_mod_rewrite() {\r
19         return yourls_apache_mod_loaded('mod_rewrite');\r
20 }\r
21 \r
22 // Check if extension cURL is enabled\r
23 function yourls_check_curl() {\r
24         return function_exists('curl_init');\r
25 }\r
26 \r
27 // Check if extension BC Math is enabled\r
28 function yourls_check_bcmath() {\r
29         return function_exists('bccomp');\r
30 }\r
31 \r
32 // Check if server has MySQL 4.1+\r
33 function yourls_check_database_version() {\r
34         global $ydb;\r
35         return ( version_compare( '4.1', $ydb->mysql_version() ) <= 0 );\r
36 }\r
37 \r
38 // Check if PHP > 4.3\r
39 function yourls_check_php_version() {\r
40         return ( version_compare( '4.3', phpversion() ) <= 0 );\r
41 }\r
42 \r
43 // Check if server is an Apache\r
44 function yourls_is_apache() {\r
45         return (\r
46            strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false\r
47         || strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false\r
48         );\r
49 }\r
50 \r
51 // Check if module exists in Apache config. Input string eg 'mod_rewrite', return true or $default\r
52 function yourls_apache_mod_loaded($mod, $default = false) {\r
53         if ( !yourls_is_apache() )\r
54                 return false;\r
55 \r
56         if ( function_exists('apache_get_modules') ) {\r
57                 $mods = apache_get_modules();\r
58                 if ( in_array($mod, $mods) )\r
59                         return true;\r
60         } elseif ( function_exists('phpinfo') ) {\r
61                         ob_start();\r
62                         phpinfo(8);\r
63                         $phpinfo = ob_get_clean();\r
64                         if ( false !== strpos($phpinfo, $mod) )\r
65                                 return true;\r
66         }\r
67         return $default;\r
68 }\r
69 \r
70 // Create .htaccess. Returns boolean\r
71 function yourls_create_htaccess() {\r
72         $host = parse_url( YOURLS_SITE );\r
73         $path = ( isset( $host['path'] ) ? $host['path'] : '' );\r
74 \r
75         $content = array(\r
76                 '<IfModule mod_rewrite.c>',\r
77                 'RewriteEngine On',\r
78                 'RewriteBase '.$path.'/',\r
79                 'RewriteCond %{REQUEST_FILENAME} !-f',\r
80                 'RewriteCond %{REQUEST_FILENAME} !-d',\r
81                 'RewriteRule ^([0-9A-Za-z]+)/?$ '.$path.'/yourls-go.php?id=$1 [L]',\r
82                 'RewriteRule ^([0-9A-Za-z]+)\+/?$ '.$path.'/yourls-infos.php?id=$1 [L]',\r
83                 '</IfModule>',\r
84         );\r
85         \r
86         $filename = dirname(dirname(__FILE__)).'/.htaccess';\r
87         \r
88         return ( yourls_insert_with_markers( $filename, 'YOURLS', $content ) );\r
89 }\r
90 \r
91 // Inserts $insertion (text in an array of lines) into $filename (.htaccess) between BEGIN/END $marker block. Returns bool. Stolen from WP\r
92 function yourls_insert_with_markers( $filename, $marker, $insertion ) {\r
93         if (!file_exists( $filename ) || is_writeable( $filename ) ) {\r
94                 if (!file_exists( $filename ) ) {\r
95                         $markerdata = '';\r
96                 } else {\r
97                         $markerdata = explode( "\n", implode( '', file( $filename ) ) );\r
98                 }\r
99 \r
100                 if ( !$f = @fopen( $filename, 'w' ) )\r
101                         return false;\r
102 \r
103                 $foundit = false;\r
104                 if ( $markerdata ) {\r
105                         $state = true;\r
106                         foreach ( $markerdata as $n => $markerline ) {\r
107                                 if (strpos($markerline, '# BEGIN ' . $marker) !== false)\r
108                                         $state = false;\r
109                                 if ( $state ) {\r
110                                         if ( $n + 1 < count( $markerdata ) )\r
111                                                 fwrite( $f, "{$markerline}\n" );\r
112                                         else\r
113                                                 fwrite( $f, "{$markerline}" );\r
114                                 }\r
115                                 if (strpos($markerline, '# END ' . $marker) !== false) {\r
116                                         fwrite( $f, "# BEGIN {$marker}\n" );\r
117                                         if ( is_array( $insertion ))\r
118                                                 foreach ( $insertion as $insertline )\r
119                                                         fwrite( $f, "{$insertline}\n" );\r
120                                         fwrite( $f, "# END {$marker}\n" );\r
121                                         $state = true;\r
122                                         $foundit = true;\r
123                                 }\r
124                         }\r
125                 }\r
126                 if (!$foundit) {\r
127                         fwrite( $f, "\n\n# BEGIN {$marker}\n" );\r
128                         foreach ( $insertion as $insertline )\r
129                                 fwrite( $f, "{$insertline}\n" );\r
130                         fwrite( $f, "# END {$marker}\n\n" );\r
131                 }\r
132                 fclose( $f );\r
133                 return true;\r
134         } else {\r
135                 return false;\r
136         }\r
137 }\r
138 \r
139 // Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )\r
140 function yourls_create_sql_tables() {\r
141         global $ydb;\r
142         \r
143         $error_msg = array();\r
144         $success_msg = array();\r
145 \r
146         // Create Table Query\r
147         $create_tables = array();\r
148         $create_tables[YOURLS_DB_TABLE_URL] =\r
149                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('.\r
150                 '`keyword` varchar(200) NOT NULL,'.\r
151                 '`url` text NOT NULL,'.\r
152                 '`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,'.\r
153                 '`ip` VARCHAR(41) NOT NULL,'.\r
154                 '`clicks` INT(10) UNSIGNED NOT NULL,'.\r
155                 'PRIMARY KEY  (`keyword`)'.\r
156                 ');';\r
157 \r
158         $create_tables[YOURLS_DB_TABLE_OPTIONS] = \r
159                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.\r
160                 '`option_id` bigint(20) unsigned NOT NULL auto_increment,'.\r
161                 '`option_name` varchar(64) NOT NULL default "",'.\r
162                 '`option_value` longtext NOT NULL,'.\r
163                 'PRIMARY KEY  (`option_id`,`option_name`),'.\r
164                 'KEY `option_name` (`option_name`)'.\r
165                 ') AUTO_INCREMENT=1 ;';\r
166                 \r
167         $create_tables[YOURLS_DB_TABLE_LOG] = \r
168                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.\r
169                 '`click_id` int(11) NOT NULL auto_increment,'.\r
170                 '`click_time` datetime NOT NULL,'.\r
171                 '`shorturl` varchar(200) NOT NULL,'.\r
172                 '`referrer` varchar(200) NOT NULL,'.\r
173                 '`user_agent` varchar(255) NOT NULL,'.\r
174                 '`ip_address` varchar(41) NOT NULL,'.\r
175                 '`country_code` char(2) NOT NULL,'.\r
176                 'PRIMARY KEY  (`click_id`),'.\r
177                 'KEY `shorturl` (`shorturl`,`referrer`,`country_code`)'.\r
178                 ') AUTO_INCREMENT=1 ;';\r
179 \r
180 \r
181         $create_table_count = 0;\r
182         \r
183         $ydb->show_errors = true;\r
184         \r
185         // Create tables\r
186         foreach ( $create_tables as $table_name => $table_query ) {\r
187                 $ydb->query($table_query);\r
188                 $create_success = $ydb->query("SHOW TABLES LIKE '$table_name'");\r
189                 if($create_success) {\r
190                         $create_table_count++;\r
191                         $success_msg[] = "Table '$table_name' created."; \r
192                 } else {\r
193                         $error_msg[] = "Error creating table '$table_name'."; \r
194                 }\r
195         }\r
196                 \r
197         // Insert data into tables\r
198         yourls_update_option( 'version', YOURLS_VERSION );\r
199         yourls_update_option( 'db_version', YOURLS_DB_VERSION );\r
200         yourls_update_option( 'next_id', 1 );\r
201         \r
202         // Check results of operations\r
203         if ( sizeof($create_tables) == $create_table_count ) {\r
204                 $success_msg[] = 'YOURLS tables successfully created.';\r
205         } else {\r
206                 $error_msg[] = "Error creating YOURLS tables."; \r
207         }\r
208 \r
209         return array( 'success' => $success_msg, 'error' => $error_msg );\r
210 }\r
211 ?>\r