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
8 // Check if extension cURL is enabled
\r
9 function yourls_check_curl() {
\r
10 return function_exists('curl_init');
\r
13 // Check if server has MySQL 4.1+
\r
14 function yourls_check_database_version() {
\r
16 return ( version_compare( '4.1', $ydb->mysql_version() ) <= 0 );
\r
19 // Check if PHP > 4.3
\r
20 function yourls_check_php_version() {
\r
21 return ( version_compare( '4.3', phpversion() ) <= 0 );
\r
24 // Check if server is an Apache
\r
25 function yourls_is_apache() {
\r
27 strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false
\r
28 || strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false
\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
37 if ( function_exists('apache_get_modules') ) {
\r
38 $mods = apache_get_modules();
\r
39 if ( in_array($mod, $mods) )
\r
41 } elseif ( function_exists('phpinfo') ) {
\r
44 $phpinfo = ob_get_clean();
\r
45 if ( false !== strpos($phpinfo, $mod) )
\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
57 '<IfModule mod_rewrite.c>',
\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
68 $filename = dirname(dirname(__FILE__)).'/.htaccess';
\r
70 return ( yourls_insert_with_markers( $filename, 'YOURLS', $content ) );
\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
79 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
\r
82 if ( !$f = @fopen( $filename, 'w' ) )
\r
86 if ( $markerdata ) {
\r
88 foreach ( $markerdata as $n => $markerline ) {
\r
89 if (strpos($markerline, '# BEGIN ' . $marker) !== false)
\r
92 if ( $n + 1 < count( $markerdata ) )
\r
93 fwrite( $f, "{$markerline}\n" );
\r
95 fwrite( $f, "{$markerline}" );
\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
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
121 // Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )
\r
122 function yourls_create_sql_tables() {
\r
125 $error_msg = array();
\r
126 $success_msg = array();
\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
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
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
165 $create_table_count = 0;
\r
167 $ydb->show_errors = true;
\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
177 $error_msg[] = "Error creating table '$table_name'.";
\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
186 // Check results of operations
\r
187 if ( sizeof($create_tables) == $create_table_count ) {
\r
188 $success_msg[] = 'YOURLS tables successfully created.';
\r
190 $error_msg[] = "Error creating YOURLS tables.";
\r
193 return array( 'success' => $success_msg, 'error' => $error_msg );
\r