]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/functions-install.php
Removed closing PHP tag on include files
[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 ^(.*)$ '.$path.'/yourls-loader.php [L]',\r
63                 '</IfModule>',\r
64         );\r
65         \r
66         $filename = YOURLS_ABSPATH.'/.htaccess';\r
67         \r
68         return ( yourls_insert_with_markers( $filename, 'YOURLS', $content ) );\r
69 }\r
70 \r
71 // Inserts $insertion (text in an array of lines) into $filename (.htaccess) between BEGIN/END $marker block. Returns bool. Stolen from WP\r
72 function yourls_insert_with_markers( $filename, $marker, $insertion ) {\r
73         if ( !file_exists( $filename ) || is_writeable( $filename ) ) {\r
74                 if ( !file_exists( $filename ) ) {\r
75                         $markerdata = '';\r
76                 } else {\r
77                         $markerdata = explode( "\n", implode( '', file( $filename ) ) );\r
78                 }\r
79 \r
80                 if ( !$f = @fopen( $filename, 'w' ) )\r
81                         return false;\r
82 \r
83                 $foundit = false;\r
84                 if ( $markerdata ) {\r
85                         $state = true;\r
86                         foreach ( $markerdata as $n => $markerline ) {\r
87                                 if ( strpos( $markerline, '# BEGIN ' . $marker) !== false )\r
88                                         $state = false;\r
89                                 if ( $state ) {\r
90                                         if ( $n + 1 < count( $markerdata ) )\r
91                                                 fwrite( $f, "{$markerline}\n" );\r
92                                         else\r
93                                                 fwrite( $f, "{$markerline}" );\r
94                                 }\r
95                                 if ( strpos( $markerline, '# END ' . $marker ) !== false ) {\r
96                                         fwrite( $f, "# BEGIN {$marker}\n" );\r
97                                         if ( is_array( $insertion ) )\r
98                                                 foreach ( $insertion as $insertline )\r
99                                                         fwrite( $f, "{$insertline}\n" );\r
100                                         fwrite( $f, "# END {$marker}\n" );\r
101                                         $state = true;\r
102                                         $foundit = true;\r
103                                 }\r
104                         }\r
105                 }\r
106                 if (!$foundit) {\r
107                         fwrite( $f, "\n\n# BEGIN {$marker}\n" );\r
108                         foreach ( $insertion as $insertline )\r
109                                 fwrite( $f, "{$insertline}\n" );\r
110                         fwrite( $f, "# END {$marker}\n\n" );\r
111                 }\r
112                 fclose( $f );\r
113                 return true;\r
114         } else {\r
115                 return false;\r
116         }\r
117 }\r
118 \r
119 // Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )\r
120 function yourls_create_sql_tables() {\r
121         global $ydb;\r
122         \r
123         $error_msg = array();\r
124         $success_msg = array();\r
125 \r
126         // Create Table Query\r
127         $create_tables = array();\r
128         $create_tables[YOURLS_DB_TABLE_URL] =\r
129                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('.\r
130                 '`keyword` varchar(200) BINARY NOT NULL,'.\r
131                 '`url` text BINARY NOT NULL,'.\r
132                 '`title` text CHARACTER SET utf8,'.\r
133                 '`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,'.\r
134                 '`ip` VARCHAR(41) NOT NULL,'.\r
135                 '`clicks` INT(10) UNSIGNED NOT NULL,'.\r
136                 ' PRIMARY KEY  (`keyword`),'.\r
137                 ' KEY `timestamp` (`timestamp`),'.\r
138                 ' KEY `ip` (`ip`)'.\r
139                 ');';\r
140 \r
141         $create_tables[YOURLS_DB_TABLE_OPTIONS] = \r
142                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.\r
143                 '`option_id` bigint(20) unsigned NOT NULL auto_increment,'.\r
144                 '`option_name` varchar(64) NOT NULL default "",'.\r
145                 '`option_value` longtext NOT NULL,'.\r
146                 'PRIMARY KEY  (`option_id`,`option_name`),'.\r
147                 'KEY `option_name` (`option_name`)'.\r
148                 ') AUTO_INCREMENT=1 ;';\r
149                 \r
150         $create_tables[YOURLS_DB_TABLE_LOG] = \r
151                 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.\r
152                 '`click_id` int(11) NOT NULL auto_increment,'.\r
153                 '`click_time` datetime NOT NULL,'.\r
154                 '`shorturl` varchar(200) BINARY NOT NULL,'.\r
155                 '`referrer` varchar(200) NOT NULL,'.\r
156                 '`user_agent` varchar(255) NOT NULL,'.\r
157                 '`ip_address` varchar(41) NOT NULL,'.\r
158                 '`country_code` char(2) NOT NULL,'.\r
159                 'PRIMARY KEY  (`click_id`),'.\r
160                 'KEY `shorturl` (`shorturl`)'.\r
161                 ') AUTO_INCREMENT=1 ;';\r
162 \r
163 \r
164         $create_table_count = 0;\r
165         \r
166         $ydb->show_errors = true;\r
167         \r
168         // Create tables\r
169         foreach ( $create_tables as $table_name => $table_query ) {\r
170                 $ydb->query( $table_query );\r
171                 $create_success = $ydb->query( "SHOW TABLES LIKE '$table_name'" );\r
172                 if( $create_success ) {\r
173                         $create_table_count++;\r
174                         $success_msg[] = "Table '$table_name' created."; \r
175                 } else {\r
176                         $error_msg[] = "Error creating table '$table_name'."; \r
177                 }\r
178         }\r
179                 \r
180         // Insert data into tables\r
181         yourls_update_option( 'version', YOURLS_VERSION );\r
182         yourls_update_option( 'db_version', YOURLS_DB_VERSION );\r
183         yourls_update_option( 'next_id', 1 );\r
184         \r
185         // Insert sample links\r
186         yourls_insert_link_in_db( 'http://planetozh.com/blog/', 'ozhblog', 'planetOzh: Ozh\' blog' );\r
187         yourls_insert_link_in_db( 'http://ozh.org/', 'ozh', 'ozh.org' );\r
188         yourls_insert_link_in_db( 'http://yourls.org/', 'yourls', 'YOURLS: Your Own URL Shortener' );\r
189                 \r
190         // Check results of operations\r
191         if ( sizeof($create_tables) == $create_table_count ) {\r
192                 $success_msg[] = 'YOURLS tables successfully created.';\r
193         } else {\r
194                 $error_msg[] = "Error creating YOURLS tables."; \r
195         }\r
196 \r
197         return array( 'success' => $success_msg, 'error' => $error_msg );\r
198 }\r