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 ^(.*)$ '.$path.'/yourls-loader.php [L]',
\r
66 $filename = YOURLS_ABSPATH.'/.htaccess';
\r
68 return ( yourls_insert_with_markers( $filename, 'YOURLS', $content ) );
\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
77 $markerdata = explode( "\n", implode( '', file( $filename ) ) );
\r
80 if ( !$f = @fopen( $filename, 'w' ) )
\r
84 if ( $markerdata ) {
\r
86 foreach ( $markerdata as $n => $markerline ) {
\r
87 if ( strpos( $markerline, '# BEGIN ' . $marker) !== false )
\r
90 if ( $n + 1 < count( $markerdata ) )
\r
91 fwrite( $f, "{$markerline}\n" );
\r
93 fwrite( $f, "{$markerline}" );
\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
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
119 // Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )
\r
120 function yourls_create_sql_tables() {
\r
123 $error_msg = array();
\r
124 $success_msg = array();
\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
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
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
164 $create_table_count = 0;
\r
166 $ydb->show_errors = true;
\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
176 $error_msg[] = "Error creating table '$table_name'.";
\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
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
190 // Check results of operations
\r
191 if ( sizeof($create_tables) == $create_table_count ) {
\r
192 $success_msg[] = 'YOURLS tables successfully created.';
\r
194 $error_msg[] = "Error creating YOURLS tables.";
\r
197 return array( 'success' => $success_msg, 'error' => $error_msg );
\r