PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions-install.php

http://yourls.googlecode.com/
PHP | 295 lines | 203 code | 38 blank | 54 comment | 36 complexity | 5f4046474a4cbfd72b1e0ab313b68fd7 MD5 | raw file
  1. <?php
  2. /**
  3. * Check if mod_rewrite is enabled. Note: unused, not reliable enough.
  4. *
  5. */
  6. function yourls_check_mod_rewrite() {
  7. return yourls_apache_mod_loaded( 'mod_rewrite' );
  8. }
  9. /**
  10. * Check if extension cURL is enabled
  11. *
  12. */
  13. function yourls_check_curl() {
  14. return function_exists( 'curl_init' );
  15. }
  16. /**
  17. * Check if server has MySQL 4.1+
  18. *
  19. */
  20. function yourls_check_database_version() {
  21. global $ydb;
  22. return ( version_compare( '4.1', $ydb->mysql_version() ) <= 0 );
  23. }
  24. /**
  25. * Check if PHP > 4.3
  26. *
  27. */
  28. function yourls_check_php_version() {
  29. return ( version_compare( '4.3', phpversion() ) <= 0 );
  30. }
  31. /**
  32. * Check if server is an Apache
  33. *
  34. */
  35. function yourls_is_apache() {
  36. return (
  37. strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) !== false
  38. || strpos( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false
  39. );
  40. }
  41. /**
  42. * Check if server is running IIS
  43. *
  44. */
  45. function yourls_is_iis() {
  46. return ( strpos( $_SERVER['SERVER_SOFTWARE'], 'IIS' ) !== false );
  47. }
  48. /**
  49. * Check if module exists in Apache config. Input string eg 'mod_rewrite', return true or $default. Stolen from WordPress
  50. *
  51. */
  52. function yourls_apache_mod_loaded( $mod, $default = false ) {
  53. if ( !yourls_is_apache() )
  54. return false;
  55. if ( function_exists( 'apache_get_modules' ) ) {
  56. $mods = apache_get_modules();
  57. if ( in_array( $mod, $mods ) )
  58. return true;
  59. } elseif ( function_exists( 'phpinfo' ) ) {
  60. ob_start();
  61. phpinfo( 8 );
  62. $phpinfo = ob_get_clean();
  63. if ( false !== strpos( $phpinfo, $mod ) )
  64. return true;
  65. }
  66. return $default;
  67. }
  68. /**
  69. * Create .htaccess or web.config. Returns boolean
  70. *
  71. */
  72. function yourls_create_htaccess() {
  73. $host = parse_url( YOURLS_SITE );
  74. $path = ( isset( $host['path'] ) ? $host['path'] : '' );
  75. if ( yourls_is_iis() ) {
  76. // Prepare content for a web.config file
  77. $content = array(
  78. '<?'.'xml version="1.0" encoding="UTF-8"?>',
  79. '<configuration>',
  80. ' <system.webServer>',
  81. ' <rewrite>',
  82. ' <rules>',
  83. ' <rule name="YOURLS" stopProcessing="true">',
  84. ' <match url="^(.*)$" ignoreCase="false" />',
  85. ' <conditions>',
  86. ' <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />',
  87. ' <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />',
  88. ' </conditions>',
  89. ' <action type="Rewrite" url="'.$path.'/yourls-loader.php" appendQueryString="true" />',
  90. ' </rule>',
  91. ' </rules>',
  92. ' </rewrite>',
  93. ' </system.webServer>',
  94. '</configuration>',
  95. );
  96. $filename = YOURLS_ABSPATH.'/web.config';
  97. $marker = 'none';
  98. } else {
  99. // Prepare content for a .htaccess file
  100. $content = array(
  101. '<IfModule mod_rewrite.c>',
  102. 'RewriteEngine On',
  103. 'RewriteBase '.$path.'/',
  104. 'RewriteCond %{REQUEST_FILENAME} !-f',
  105. 'RewriteCond %{REQUEST_FILENAME} !-d',
  106. 'RewriteRule ^.*$ '.$path.'/yourls-loader.php [L]',
  107. '</IfModule>',
  108. );
  109. $filename = YOURLS_ABSPATH.'/.htaccess';
  110. $marker = 'YOURLS';
  111. }
  112. return ( yourls_insert_with_markers( $filename, $marker, $content ) );
  113. }
  114. /**
  115. * Inserts $insertion (text in an array of lines) into $filename (.htaccess) between BEGIN/END $marker block. Returns bool. Stolen from WP
  116. *
  117. */
  118. function yourls_insert_with_markers( $filename, $marker, $insertion ) {
  119. if ( !file_exists( $filename ) || is_writeable( $filename ) ) {
  120. if ( !file_exists( $filename ) ) {
  121. $markerdata = '';
  122. } else {
  123. $markerdata = explode( "\n", implode( '', file( $filename ) ) );
  124. }
  125. if ( !$f = @fopen( $filename, 'w' ) )
  126. return false;
  127. $foundit = false;
  128. if ( $markerdata ) {
  129. $state = true;
  130. foreach ( $markerdata as $n => $markerline ) {
  131. if ( strpos( $markerline, '# BEGIN ' . $marker ) !== false )
  132. $state = false;
  133. if ( $state ) {
  134. if ( $n + 1 < count( $markerdata ) )
  135. fwrite( $f, "{$markerline}\n" );
  136. else
  137. fwrite( $f, "{$markerline}" );
  138. }
  139. if ( strpos( $markerline, '# END ' . $marker ) !== false ) {
  140. if ( $marker != 'none' )
  141. fwrite( $f, "# BEGIN {$marker}\n" );
  142. if ( is_array( $insertion ) )
  143. foreach ( $insertion as $insertline )
  144. fwrite( $f, "{$insertline}\n" );
  145. if ( $marker != 'none' )
  146. fwrite( $f, "# END {$marker}\n" );
  147. $state = true;
  148. $foundit = true;
  149. }
  150. }
  151. }
  152. if ( !$foundit ) {
  153. if ( $marker != 'none' )
  154. fwrite( $f, "\n\n# BEGIN {$marker}\n" );
  155. foreach ( $insertion as $insertline )
  156. fwrite( $f, "{$insertline}\n" );
  157. if ( $marker != 'none' )
  158. fwrite( $f, "# END {$marker}\n\n" );
  159. }
  160. fclose( $f );
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. }
  166. /**
  167. * Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings )
  168. *
  169. */
  170. function yourls_create_sql_tables() {
  171. global $ydb;
  172. $error_msg = array();
  173. $success_msg = array();
  174. // Create Table Query
  175. $create_tables = array();
  176. $create_tables[YOURLS_DB_TABLE_URL] =
  177. 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('.
  178. '`keyword` varchar(200) BINARY NOT NULL,'.
  179. '`url` text BINARY NOT NULL,'.
  180. '`title` text CHARACTER SET utf8,'.
  181. '`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,'.
  182. '`ip` VARCHAR(41) NOT NULL,'.
  183. '`clicks` INT(10) UNSIGNED NOT NULL,'.
  184. ' PRIMARY KEY (`keyword`),'.
  185. ' KEY `timestamp` (`timestamp`),'.
  186. ' KEY `ip` (`ip`)'.
  187. ');';
  188. $create_tables[YOURLS_DB_TABLE_OPTIONS] =
  189. 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('.
  190. '`option_id` bigint(20) unsigned NOT NULL auto_increment,'.
  191. '`option_name` varchar(64) NOT NULL default "",'.
  192. '`option_value` longtext NOT NULL,'.
  193. 'PRIMARY KEY (`option_id`,`option_name`),'.
  194. 'KEY `option_name` (`option_name`)'.
  195. ') AUTO_INCREMENT=1 ;';
  196. $create_tables[YOURLS_DB_TABLE_LOG] =
  197. 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('.
  198. '`click_id` int(11) NOT NULL auto_increment,'.
  199. '`click_time` datetime NOT NULL,'.
  200. '`shorturl` varchar(200) BINARY NOT NULL,'.
  201. '`referrer` varchar(200) NOT NULL,'.
  202. '`user_agent` varchar(255) NOT NULL,'.
  203. '`ip_address` varchar(41) NOT NULL,'.
  204. '`country_code` char(2) NOT NULL,'.
  205. 'PRIMARY KEY (`click_id`),'.
  206. 'KEY `shorturl` (`shorturl`)'.
  207. ') AUTO_INCREMENT=1 ;';
  208. $create_table_count = 0;
  209. $ydb->show_errors = true;
  210. // Create tables
  211. foreach ( $create_tables as $table_name => $table_query ) {
  212. $ydb->query( $table_query );
  213. $create_success = $ydb->query( "SHOW TABLES LIKE '$table_name'" );
  214. if( $create_success ) {
  215. $create_table_count++;
  216. $success_msg[] = yourls_s( "Table '%s' created.", $table_name );
  217. } else {
  218. $error_msg[] = yourls_s( "Error creating table '%s'.", $table_name );
  219. }
  220. }
  221. // Insert data into tables
  222. yourls_update_option( 'version', YOURLS_VERSION );
  223. yourls_update_option( 'db_version', YOURLS_DB_VERSION );
  224. yourls_update_option( 'next_id', 1 );
  225. // Insert sample links
  226. yourls_insert_link_in_db( 'http://planetozh.com/blog/', 'ozhblog', 'planetOzh: Ozh\' blog' );
  227. yourls_insert_link_in_db( 'http://ozh.org/', 'ozh', 'ozh.org' );
  228. yourls_insert_link_in_db( 'http://yourls.org/', 'yourls', 'YOURLS: Your Own URL Shortener' );
  229. // Check results of operations
  230. if ( sizeof( $create_tables ) == $create_table_count ) {
  231. $success_msg[] = yourls__( 'YOURLS tables successfully created.' );
  232. } else {
  233. $error_msg[] = yourls__( 'Error creating YOURLS tables.' );
  234. }
  235. return array( 'success' => $success_msg, 'error' => $error_msg );
  236. }
  237. /**
  238. * Toggle maintenance mode. Inspired from WP. Returns true for success, false otherwise
  239. *
  240. */
  241. function yourls_maintenance_mode( $maintenance = true ) {
  242. $file = YOURLS_ABSPATH . '/.maintenance' ;
  243. // Turn maintenance mode on : create .maintenance file
  244. if ( (bool)$maintenance ) {
  245. if ( ! ( $fp = @fopen( $file, 'w' ) ) )
  246. return false;
  247. $maintenance_string = '<?php $maintenance_start = ' . time() . '; ?>';
  248. @fwrite( $fp, $maintenance_string );
  249. @fclose( $fp );
  250. @chmod( $file, 0644 ); // Read and write for owner, read for everybody else
  251. // Not sure why the fwrite would fail if the fopen worked... Just in case
  252. return( is_readable( $file ) );
  253. // Turn maintenance mode off : delete the .maintenance file
  254. } else {
  255. return @unlink($file);
  256. }
  257. }