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