| [ Index ] |
PHP Cross Reference of YOURLS |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Check if we have PDO installed, returns bool 5 * 6 * @since 1.7.3 7 * @return bool 8 */ 9 function yourls_check_PDO() { 10 return extension_loaded('pdo'); 11 } 12 13 /** 14 * Check if server has MySQL 5.5+ 15 * 16 * In the unlikely event that someone is running MariaDB older than 10, install will probably fail, 17 * but there is no reliable way to check for MariaDB version 18 * 19 * @return bool 20 */ 21 function yourls_check_database_version(): bool { 22 return ( version_compare( '5.5', yourls_get_database_version() ) <= 0 ); 23 } 24 25 /** 26 * Get DB server version 27 * 28 * @since 1.7 29 * @return string sanitized DB version 30 */ 31 function yourls_get_database_version() { 32 // Allow plugins to short-circuit the whole function 33 $pre = yourls_apply_filter( 'shunt_get_database_version', yourls_shunt_default() ); 34 if ( yourls_shunt_default() !== $pre ) { 35 return $pre; 36 } 37 38 return yourls_sanitize_version(yourls_get_db('read-get_database_version')->mysql_version()); 39 } 40 41 /** 42 * Check if PHP > 8.1 43 * 44 * @return bool 45 */ 46 function yourls_check_php_version(): bool { 47 return version_compare( PHP_VERSION, '8.1.0', '>=' ); 48 } 49 50 /** 51 * Check if server is an Apache 52 * 53 * @return bool 54 */ 55 function yourls_is_apache() { 56 if( !array_key_exists( 'SERVER_SOFTWARE', $_SERVER ) ) 57 return false; 58 return ( 59 strpos( $_SERVER['SERVER_SOFTWARE'], 'Apache' ) !== false 60 || strpos( $_SERVER['SERVER_SOFTWARE'], 'LiteSpeed' ) !== false 61 ); 62 } 63 64 /** 65 * Check if server is running IIS 66 * 67 * @return bool 68 */ 69 function yourls_is_iis() { 70 return ( array_key_exists( 'SERVER_SOFTWARE', $_SERVER ) ? ( strpos( $_SERVER['SERVER_SOFTWARE'], 'IIS' ) !== false ) : false ); 71 } 72 73 74 /** 75 * Create .htaccess or web.config. Returns boolean 76 * 77 * @return bool 78 */ 79 function yourls_create_htaccess() { 80 $host = parse_url( yourls_get_yourls_site() ); 81 $path = ( isset( $host['path'] ) ? $host['path'] : '' ); 82 83 if ( yourls_is_iis() ) { 84 // Prepare content for a web.config file 85 $content = array( 86 '<?'.'xml version="1.0" encoding="UTF-8"?>', 87 '<configuration>', 88 ' <system.webServer>', 89 ' <security>', 90 ' <requestFiltering allowDoubleEscaping="true" />', 91 ' </security>', 92 ' <rewrite>', 93 ' <rules>', 94 ' <rule name="YOURLS" stopProcessing="true">', 95 ' <match url="^(.*)$" ignoreCase="false" />', 96 ' <conditions>', 97 ' <add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />', 98 ' <add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />', 99 ' </conditions>', 100 ' <action type="Rewrite" url="'.$path.'/yourls-loader.php" appendQueryString="true" />', 101 ' </rule>', 102 ' </rules>', 103 ' </rewrite>', 104 ' </system.webServer>', 105 '</configuration>', 106 ); 107 108 $filename = YOURLS_ABSPATH.'/web.config'; 109 $marker = 'none'; 110 111 } else { 112 // Prepare content for a .htaccess file 113 $content = array( 114 '<IfModule mod_rewrite.c>', 115 'RewriteEngine On', 116 'RewriteBase '.$path.'/', 117 'RewriteCond %{REQUEST_FILENAME} !-f', 118 'RewriteCond %{REQUEST_FILENAME} !-d', 119 'RewriteRule ^.*$ '.$path.'/yourls-loader.php [L]', 120 '</IfModule>', 121 ); 122 123 $filename = YOURLS_ABSPATH.'/.htaccess'; 124 $marker = 'YOURLS'; 125 126 } 127 128 return ( yourls_insert_with_markers( $filename, $marker, $content ) ); 129 } 130 131 /** 132 * Insert text into a file between BEGIN/END markers, return bool. Stolen from WP 133 * 134 * Inserts an array of strings into a file (eg .htaccess ), placing it between 135 * BEGIN and END markers. Replaces existing marked info. Retains surrounding 136 * data. Creates file if none exists. 137 * 138 * @since 1.3 139 * 140 * @param string $filename 141 * @param string $marker 142 * @param array $insertion 143 * @return bool True on write success, false on failure. 144 */ 145 function yourls_insert_with_markers( $filename, $marker, $insertion ) { 146 if ( !file_exists( $filename ) || is_writeable( $filename ) ) { 147 if ( !file_exists( $filename ) ) { 148 $markerdata = ''; 149 } else { 150 $markerdata = explode( "\n", implode( '', file( $filename ) ) ); 151 } 152 153 if ( !$f = @fopen( $filename, 'w' ) ) 154 return false; 155 156 $foundit = false; 157 if ( $markerdata ) { 158 $state = true; 159 foreach ( $markerdata as $n => $markerline ) { 160 if ( strpos( $markerline, '# BEGIN ' . $marker ) !== false ) 161 $state = false; 162 if ( $state ) { 163 if ( $n + 1 < count( $markerdata ) ) 164 fwrite( $f, "{$markerline}\n" ); 165 else 166 fwrite( $f, "{$markerline}" ); 167 } 168 if ( strpos( $markerline, '# END ' . $marker ) !== false ) { 169 if ( $marker != 'none' ) 170 fwrite( $f, "# BEGIN {$marker}\n" ); 171 if ( is_array( $insertion ) ) 172 foreach ( $insertion as $insertline ) 173 fwrite( $f, "{$insertline}\n" ); 174 if ( $marker != 'none' ) 175 fwrite( $f, "# END {$marker}\n" ); 176 $state = true; 177 $foundit = true; 178 } 179 } 180 } 181 if ( !$foundit ) { 182 if ( $marker != 'none' ) 183 fwrite( $f, "\n\n# BEGIN {$marker}\n" ); 184 foreach ( $insertion as $insertline ) 185 fwrite( $f, "{$insertline}\n" ); 186 if ( $marker != 'none' ) 187 fwrite( $f, "# END {$marker}\n\n" ); 188 } 189 fclose( $f ); 190 return true; 191 } else { 192 return false; 193 } 194 } 195 196 /** 197 * Create MySQL tables. Return array( 'success' => array of success strings, 'errors' => array of error strings ) 198 * 199 * @since 1.3 200 * @return array An array like array( 'success' => array of success strings, 'errors' => array of error strings ) 201 */ 202 function yourls_create_sql_tables(): array { 203 // Allow plugins (most likely a custom db.php layer in user dir) to short-circuit the whole function 204 $pre = yourls_apply_filter( 'shunt_yourls_create_sql_tables', yourls_shunt_default() ); 205 // your filter function should return an array of ( 'success' => $success_msg, 'error' => $error_msg ), see below 206 if ( yourls_shunt_default() !== $pre ) { 207 return $pre; 208 } 209 210 $ydb = yourls_get_db('write-create_sql_tables'); 211 212 $error_msg = array(); 213 $success_msg = array(); 214 215 // Create Table Query 216 $create_tables = array(); 217 $create_tables[YOURLS_DB_TABLE_URL] = 218 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_URL.'` ('. 219 '`keyword` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL DEFAULT \'\','. 220 '`url` text CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,'. 221 '`title` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,'. 222 '`timestamp` timestamp NOT NULL DEFAULT current_timestamp(),'. 223 '`ip` varchar(41) COLLATE utf8mb4_unicode_ci NOT NULL,'. 224 '`clicks` int(10) unsigned NOT NULL,'. 225 'PRIMARY KEY (`keyword`),'. 226 'KEY `ip` (`ip`),'. 227 'KEY `timestamp` (`timestamp`),'. 228 'KEY `url_idx` (`url`(30))'. 229 ') DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;'; 230 231 $create_tables[YOURLS_DB_TABLE_OPTIONS] = 232 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_OPTIONS.'` ('. 233 '`option_id` bigint(20) unsigned NOT NULL auto_increment,'. 234 '`option_name` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL default \'\','. 235 '`option_value` longtext COLLATE utf8mb4_unicode_ci NOT NULL,'. 236 'PRIMARY KEY (`option_id`,`option_name`),'. 237 'KEY `option_name` (`option_name`)'. 238 ') AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;'; 239 240 $create_tables[YOURLS_DB_TABLE_LOG] = 241 'CREATE TABLE IF NOT EXISTS `'.YOURLS_DB_TABLE_LOG.'` ('. 242 '`click_id` int(11) NOT NULL auto_increment,'. 243 '`click_time` datetime NOT NULL,'. 244 '`shorturl` varchar(100) BINARY NOT NULL,'. 245 '`referrer` varchar(200) NOT NULL,'. 246 '`user_agent` varchar(255) NOT NULL,'. 247 '`ip_address` varchar(41) NOT NULL,'. 248 '`country_code` char(2) NOT NULL,'. 249 'PRIMARY KEY (`click_id`),'. 250 'KEY `shorturl` (`shorturl`)'. 251 ') AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;'; 252 253 254 $create_table_count = 0; 255 256 // Make install process verbose to help troubleshoot installation issues 257 $debug = yourls_get_debug_mode(); 258 yourls_debug_mode(true); 259 260 // Create tables 261 foreach ( $create_tables as $table_name => $table_query ) { 262 $ydb->perform( $table_query ); 263 $create_success = $ydb->fetchAffected( "SHOW TABLES LIKE '$table_name'" ); 264 if( $create_success ) { 265 $create_table_count++; 266 $success_msg[] = yourls_s( "Table '%s' created.", $table_name ); 267 } else { 268 $error_msg[] = yourls_s( "Error creating table '%s'.", $table_name ); 269 } 270 } 271 272 // Initializes the option table 273 if( !yourls_initialize_options() ) 274 $error_msg[] = yourls__( 'Could not initialize options' ); 275 276 // Insert sample links 277 if( !yourls_insert_sample_links() ) 278 $error_msg[] = yourls__( 'Could not insert sample short URLs' ); 279 280 // Check results of operations 281 if ( sizeof( $create_tables ) == $create_table_count ) { 282 $success_msg[] = yourls__( 'YOURLS tables successfully created.' ); 283 } else { 284 $error_msg[] = yourls__( 'Error creating YOURLS tables.' ); 285 } 286 287 // Restore debug mode to its original value 288 yourls_debug_mode( $debug ); 289 290 return array( 'success' => $success_msg, 'error' => $error_msg ); 291 } 292 293 /** 294 * Initializes the option table 295 * 296 * Each yourls_update_option() returns either true on success (option updated) or false on failure (new value == old value, or 297 * for some reason it could not save to DB). 298 * Since true & true & true = 1, we cast it to boolean type to return true (or false) 299 * 300 * @since 1.7 301 * @return bool 302 */ 303 function yourls_initialize_options() { 304 return ( bool ) ( 305 yourls_update_option( 'version', YOURLS_VERSION ) 306 & yourls_update_option( 'db_version', YOURLS_DB_VERSION ) 307 & yourls_update_option( 'next_id', 1 ) 308 & yourls_update_option( 'active_plugins', array() ) 309 ); 310 } 311 312 /** 313 * Populates the URL table with a few sample links 314 * 315 * @since 1.7 316 * @return bool 317 */ 318 function yourls_insert_sample_links() { 319 $link1 = yourls_add_new_link( 'https://blog.yourls.org/', 'yourlsblog', 'YOURLS\' Blog' ); 320 $link2 = yourls_add_new_link( 'https://yourls.org/', 'yourls', 'YOURLS: Your Own URL Shortener' ); 321 $link3 = yourls_add_new_link( 'https://ozh.org/', 'ozh', 'ozh.org' ); 322 return ( bool ) ( 323 $link1['status'] == 'success' 324 & $link2['status'] == 'success' 325 & $link3['status'] == 'success' 326 ); 327 } 328 329 330 /** 331 * Toggle maintenance mode. Inspired from WP. Returns true for success, false otherwise 332 * 333 * @param bool $maintenance True to enable, false to disable 334 * @return bool True on success, false on failure 335 */ 336 function yourls_maintenance_mode( $maintenance = true ) { 337 338 $file = YOURLS_ABSPATH . '/.maintenance' ; 339 340 // Turn maintenance mode on : create .maintenance file 341 if ( (bool)$maintenance ) { 342 if ( ! ( $fp = @fopen( $file, 'w' ) ) ) 343 return false; 344 345 $maintenance_string = '<?php $maintenance_start = ' . time() . '; ?>'; 346 @fwrite( $fp, $maintenance_string ); 347 @fclose( $fp ); 348 @chmod( $file, 0644 ); // Read and write for owner, read for everybody else 349 350 // Not sure why the fwrite would fail if the fopen worked... Just in case 351 return( is_readable( $file ) ); 352 353 // Turn maintenance mode off : delete the .maintenance file 354 } else { 355 return @unlink($file); 356 } 357 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Jun 18 05:10:24 2026 | Cross-referenced by PHPXref 0.7.1 |