Show
Ignore:
Timestamp:
08/23/07 14:45:10 (1 year ago)
Author:
donncha
Message:

Moved mu-plugins stuff into wpmu-functions.php

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-includes/wpmu-functions.php

    r1036 r1043  
    15141514} 
    15151515 
     1516/* Misc functions */ 
     1517 
     1518function is_upload_too_big( $file ) { 
     1519        if( filesize($file[ 'file' ]) > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) ) 
     1520                $file[ 'error' ] = sprintf(__('This file is too big. Files must be less than %1$s Kb in size.<br />'), get_site_option( 'fileupload_maxk', 1500 ) ); 
     1521        if( upload_is_user_over_quota( false ) ) { 
     1522                $file[ 'error' ] = __('You have used your space quota. Please delete files before uploading.<br />'); 
     1523        } 
     1524        return $file; 
     1525} 
     1526add_filter( 'wp_handle_upload', 'is_upload_too_big' ); 
     1527 
     1528function fix_upload_details( $uploads ) { 
     1529        $uploads[ 'url' ] = str_replace( UPLOADS, "files", $uploads[ 'url' ] ); 
     1530        return $uploads; 
     1531} 
     1532add_filter( "upload_dir", "fix_upload_details" ); 
     1533 
     1534 
     1535function get_dirsize($directory) { 
     1536        $size = 0; 
     1537        if(substr($directory,-1) == '/') $directory = substr($directory,0,-1); 
     1538        if(!file_exists($directory) || !is_dir($directory) || !is_readable($directory)) return false; 
     1539        if($handle = opendir($directory)) { 
     1540                while(($file = readdir($handle)) !== false) { 
     1541                        $path = $directory.'/'.$file; 
     1542                        if($file != '.' && $file != '..') { 
     1543                                if(is_file($path)) { 
     1544                                        $size += filesize($path); 
     1545                                } elseif(is_dir($path)) { 
     1546                                        $handlesize = get_dirsize($path); 
     1547                                        if($handlesize >= 0) { 
     1548                                                $size += $handlesize; 
     1549                                        } else { 
     1550                                                return false; 
     1551                                        } 
     1552                                } 
     1553                        } 
     1554                } 
     1555                closedir($handle); 
     1556        } 
     1557        return $size; 
     1558} 
     1559 
     1560function upload_is_user_over_quota( $echo = true ) { 
     1561        global $wpdb; 
     1562         
     1563        // Default space allowed is 10 MB  
     1564        $spaceAllowed = get_site_option("blog_upload_space"); 
     1565        if(empty($spaceAllowed) || !is_numeric($spaceAllowed)) $spaceAllowed = 10; 
     1566         
     1567        $dirName = constant( "ABSPATH" ) . constant( "UPLOADS" ); 
     1568        $size = get_dirsize($dirName) / 1024 / 1024; 
     1569         
     1570        if( ($spaceAllowed-$size) < 0 ) {  
     1571                if( $echo ) 
     1572                        _e( "Sorry, you have used your space allocation. Please delete some files to upload more files." ); //No space left 
     1573                return true; 
     1574        } else { 
     1575                return false; 
     1576        } 
     1577} 
     1578add_action( 'upload_files_upload', 'upload_is_user_over_quota' ); 
     1579add_action( 'upload_files_browse', 'upload_is_user_over_quota' ); 
     1580add_action( 'upload_files_browse-all', 'upload_is_user_over_quota' ); 
     1581 
     1582function check_upload_mimes($mimes) { 
     1583        $site_exts = explode( " ", get_site_option( "upload_filetypes" ) ); 
     1584        foreach ( $site_exts as $ext ) 
     1585                foreach ( $mimes as $ext_pattern => $mime ) 
     1586                        if ( preg_match("/$ext_pattern/", $ext) ) 
     1587                                $site_mimes[$ext_pattern] = $mime; 
     1588        return $site_mimes; 
     1589} 
     1590add_filter('upload_mimes', 'check_upload_mimes'); 
     1591 
     1592add_filter('the_title', 'wp_filter_kses'); 
     1593 
     1594function update_posts_count( $post_id ) { 
     1595        global $wpdb; 
     1596        $post_id = intval( $post_id ); 
     1597        $c = $wpdb->get_var( "SELECT count(*) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type='post'" ); 
     1598        update_option( "post_count", $c ); 
     1599} 
     1600add_action( "publish_post", "update_posts_count" ); 
     1601 
     1602function wpmu_log_new_registrations( $blog_id, $user_id ) { 
     1603        global $wpdb; 
     1604        $user = new WP_User($user_id); 
     1605        $email = $wpdb->escape($user->user_email); 
     1606        $IP = preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] ); 
     1607        $wpdb->query( "INSERT INTO {$wpdb->registration_log} ( email , IP , blog_id, date_registered ) VALUES ( '{$email}', '{$IP}', '{$blog_id}', NOW( ))" ); 
     1608} 
     1609 
     1610add_action( "wpmu_new_blog" ,"wpmu_log_new_registrations", 10, 2 ); 
     1611 
     1612function scriptaculous_admin_loader() { 
     1613                wp_enqueue_script('scriptaculous'); 
     1614} 
     1615add_action( 'admin_print_scripts', 'scriptaculous_admin_loader' ); 
     1616 
     1617function fix_import_form_size( $size ) { 
     1618        if( upload_is_user_over_quota( false ) == false ) 
     1619                return 0; 
     1620        $dirName = constant( "ABSPATH" ) . constant( "UPLOADS" ); 
     1621        $dirsize = get_dirsize($dirName) / 1024; 
     1622        if( $size > $dirsize ) { 
     1623                return $dirsize; 
     1624        } else { 
     1625                return $size; 
     1626        } 
     1627} 
     1628add_filter( 'import_upload_size_limit', 'fix_import_form_size' ); 
     1629 
     1630if ( !function_exists('graceful_fail') ) : 
     1631function graceful_fail( $message ) { 
     1632        die(' 
     1633<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
     1634<html xmlns="http://www.w3.org/1999/xhtml"><head profile="http://gmpg.org/xfn/11"> 
     1635<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
     1636<title>Error!</title> 
     1637<style type="text/css"> 
     1638img { 
     1639        border: 0; 
     1640} 
     1641body { 
     1642line-height: 1.6em; font-family: Georgia, serif; width: 390px; margin: auto; 
     1643text-align: center; 
     1644} 
     1645.message { 
     1646        font-size: 22px; 
     1647        width: 350px; 
     1648        margin: auto; 
     1649} 
     1650</style> 
     1651</head> 
     1652<body> 
     1653<p class="message">' . $message . '</p> 
     1654</body> 
     1655</html> 
     1656        '); 
     1657} 
     1658endif; 
     1659 
     1660/* Delete blog */ 
     1661 
     1662class delete_blog { 
     1663 
     1664        function delete_blog() { 
     1665                $this->reallydeleteblog = false; 
     1666                add_action('admin_menu', array(&$this, 'admin_menu')); 
     1667                add_action('admin_footer', array(&$this, 'admin_footer')); 
     1668        } 
     1669 
     1670        function admin_footer() { 
     1671                global $wpdb; 
     1672 
     1673                if( $this->reallydeleteblog == true ) { 
     1674                        wpmu_delete_blog( $wpdb->blogid );  
     1675                } 
     1676        } 
     1677 
     1678        function admin_menu() { 
     1679                add_submenu_page('options-general.php', __('Delete Blog'), __('Delete Blog'), 'manage_options', 'delete-blog', array(&$this, 'plugin_content')); 
     1680        } 
     1681 
     1682        function plugin_content() { 
     1683                global $wpdb, $current_blog, $current_site; 
     1684                $this->delete_blog_hash = get_settings('delete_blog_hash'); 
     1685                echo '<div class="wrap"><h2>' . __('Delete Blog') . '</h2>'; 
     1686                if( $_POST[ 'action' ] == "deleteblog" && $_POST[ 'confirmdelete' ] == '1' ) { 
     1687                        $hash = substr( md5( $_SERVER[ 'REQUEST_URI' ] . time() ), 0, 6 ); 
     1688                        update_option( "delete_blog_hash", $hash ); 
     1689                        $url_delete = get_option( "siteurl" ) . "/wp-admin/options-general.php?page=delete-blog&h=" . $hash; 
     1690                        $msg = __("Dear User, 
     1691You recently clicked the 'Delete Blog' link on your blog and filled in a  
     1692form on that page. 
     1693If you really want to delete your blog, click the link below. You will not 
     1694be asked to confirm again so only click this link if you are 100% certain: 
     1695URL_DELETE 
     1696 
     1697If you delete your blog, please consider opening a new blog here 
     1698some time in the future! (But remember your current blog and username  
     1699are gone forever.) 
     1700 
     1701Thanks for using the site, 
     1702Webmaster 
     1703SITE_NAME 
     1704"); 
     1705                        $msg = str_replace( "URL_DELETE", $url_delete, $msg ); 
     1706                        $msg = str_replace( "SITE_NAME", $current_site->site_name, $msg ); 
     1707                        wp_mail( get_option( "admin_email" ), "[ " . get_option( "blogname" ) . " ] ".__("Delete My Blog"), $msg ); 
     1708                        ?> 
     1709                        <p><?php _e('Thank you. Please check your email for a link to confirm your action. Your blog will not be deleted until this link is clicked.') ?></p> 
     1710                        <?php 
     1711                } elseif( isset( $_GET[ 'h' ] ) && $_GET[ 'h' ] != '' && get_option('delete_blog_hash') != false ) { 
     1712                        if( get_option('delete_blog_hash') == $_GET[ 'h' ] ) { 
     1713                                $this->reallydeleteblog = true; 
     1714                                echo "<p>" . sprintf(__('Thank you for using %s, your blog has been deleted. Happy trails to you until we meet again.'), $current_site->site_name) . "</p>"; 
     1715                        } else { 
     1716                                $this->reallydeleteblog = false; 
     1717                                echo "<p>" . __("I'm sorry, the link you clicked is stale. Please select another option.") . "</p>"; 
     1718                        } 
     1719                } else { 
    15161720?> 
     1721                        <p><?php printf(__('If you do not want to use your %s blog any more, you can delete it using the form below. When you click <strong>Delete My Blog</strong> you will be sent an email with a link in it. Click on this link to delete your blog.'), $current_site->site_name); ?></p> 
     1722                        <p><?php _e('Remember, once deleted your blog cannot be restored.') ?></p> 
     1723                        <form method='post' name='deletedirect'> 
     1724                        <input type="hidden" name="page" value="<?php echo $_GET['page'] ?>" /> 
     1725                        <input type='hidden' name='action' value='deleteblog' /> 
     1726                        <p><input id='confirmdelete' type='checkbox' name='confirmdelete' value='1' /> <label for='confirmdelete'><strong><?php printf( __("I'm sure I want to permanently disable my blog, and I am aware I can never get it back or use %s again."), $current_blog->domain); ?></strong></label></p> 
     1727                        <p class="submit"><input type='submit' value='<?php _e('Delete My Blog Permanently &raquo;') ?>' /></p> 
     1728                        </form> 
     1729<?php 
     1730                } 
     1731                echo "</div>"; 
     1732        } 
     1733} 
     1734 
     1735$delete_blog_obj = new delete_blog(); 
     1736 
     1737/* Dashboard Switcher */ 
     1738 
     1739add_action( 'admin_print_scripts', 'switcher_scripts' ); 
     1740 
     1741function switcher_scripts() { 
     1742        wp_enqueue_script('jquery'); 
     1743} 
     1744 
     1745 
     1746function switcher_css() { 
     1747?> 
     1748<style type="text/css"> 
     1749#switchermenu a { 
     1750        font-size: 20px; 
     1751        padding: 0 1.5em 0 10px; 
     1752        display: block; 
     1753        color: #c3def1; 
     1754} 
     1755 
     1756#switchermenu a:hover { 
     1757        background: #1a70b4; 
     1758        color: #fff; 
     1759} 
     1760 
     1761#switchermenu li { 
     1762        margin: 0; 
     1763        padding: 2px; 
     1764} 
     1765 
     1766#switchermenu { 
     1767        display: none; 
     1768        list-style: none; 
     1769        margin: 0; 
     1770        padding: 0; 
     1771        overflow: hidden; 
     1772        border-top: 1px solid #1a70b4; 
     1773        border-left: 1px solid #1a70b4; 
     1774        position: absolute; 
     1775        left: 0; 
     1776        top: 1em; 
     1777        background: #14568a; 
     1778        z-index: 1; 
     1779} 
     1780</style> 
     1781<script type="text/javascript"> 
     1782jQuery( function($) { 
     1783var switchTime; 
     1784var w = false; 
     1785var h = $( '#blog-title' ) 
     1786        .css({ 
     1787                background: 'transparent url( ../wp-content/mu-plugins/bullet_arrow_down.gif ) no-repeat scroll 100% .2em', 
     1788                padding: '0 25px 2px 5px', 
     1789                cursor: 'pointer', 
     1790                border: '1px solid #14568a' 
     1791        }) 
     1792        .parent().css( { position: 'relative' }).end() 
     1793        .append( $('#switchermenu') ) 
     1794        .hover( function() { 
     1795                $(this).css({ border: '1px solid #1a70b4'}); 
     1796                switchTime = window.setTimeout( function() { 
     1797                        $('#switchermenu').fadeIn('fast').css( 'top', h ).find('a').width( w = w ? w : $('#switchermenu').width() ); 
     1798                }, 300 ); 
     1799        }, function() { 
     1800                window.clearTimeout( switchTime ); 
     1801                $(this).css({ border: '1px solid #14568a' }) ; 
     1802                $('#switchermenu').hide(); 
     1803        }) 
     1804        .height() - 3; 
     1805}); 
     1806</script> 
     1807<?php 
     1808} 
     1809add_action( "admin_head", "switcher_css" ); 
     1810 
     1811function add_switcher() { 
     1812        global $current_user; 
     1813        $out = '<h1><span id="blog-title">' . wptexturize(get_bloginfo(("name"))) . '</span><span id="viewsite">(<a href="' . get_option("home") . "/" . '">' . __("View site &raquo;") . '</a>)</span></h1>'; 
     1814        $out .= '<ul id="switchermenu">'; 
     1815        $blogs = get_blogs_of_user($current_user->ID); 
     1816        if ( ! empty($blogs) ) foreach ( $blogs as $blog ) { 
     1817                $out .= '<li><a href="http://' . $blog->domain . $blog->path . 'wp-admin/">' . addslashes( $blog->blogname ) . '</a></li>'; 
     1818        } 
     1819        $out .= "</ul>"; 
     1820        ?> 
     1821        <script type="text/javascript"> 
     1822        document.getElementById('wphead').innerHTML = '<?php echo $out ?>' 
     1823        </script> 
     1824        <?php 
     1825} 
     1826add_action( 'admin_footer', 'add_switcher' ); 
     1827 
     1828/* Global Categories */ 
     1829 
     1830function global_categories( $cat_ID ) { 
     1831        global $wpdb; 
     1832 
     1833        $cat_ID = intval( $cat_ID ); 
     1834        $c = $wpdb->get_row( "SELECT * FROM $wpdb->categories WHERE cat_ID = '$cat_ID'" ); 
     1835 
     1836        $global_category = $wpdb->get_row( "SELECT * FROM $wpdb->sitecategories WHERE category_nicename = '" . $wpdb->escape( $c->category_nicename ) . "'" ); 
     1837 
     1838        if ( $global_category ) { 
     1839                $global_id = $global_category->cat_ID; 
     1840        } else { 
     1841                $wpdb->query( "INSERT INTO $wpdb->sitecategories ( cat_name, category_nicename ) VALUES ( '" . $wpdb->escape( $c->cat_name ) . "', '" . $wpdb->escape( $c->category_nicename ) . "' )" ); 
     1842                $global_id = $wpdb->insert_id; 
     1843        } 
     1844        $wpdb->query( "UPDATE $wpdb->categories SET cat_ID = '$global_id' WHERE cat_id = '$cat_ID'" ); 
     1845        $wpdb->query( "UPDATE $wpdb->categories SET category_parent = '$global_id' WHERE category_parent = '$cat_ID'" ); 
     1846        $wpdb->query( "UPDATE $wpdb->post2cat SET category_id = '$global_id' WHERE category_id = '$cat_ID'" ); 
     1847        $wpdb->query( "UPDATE $wpdb->link2cat SET category_id = '$global_id' WHERE category_id = '$cat_ID'" ); 
     1848        wp_cache_delete($cat_ID, 'category'); 
     1849        wp_cache_delete($global_id, 'category'); 
     1850        wp_cache_delete('all_category_ids', 'category'); 
     1851 
     1852        do_action('update_cat_id', $global_id, $cat_ID); 
     1853 
     1854        return $global_id; 
     1855} 
     1856 
     1857add_filter( 'cat_id_filter', 'global_categories' ); 
     1858 
     1859/* Pluggable */ 
     1860 
     1861function wp_login($username, $password, $already_md5 = false) { 
     1862        global $wpdb, $error, $current_user; 
     1863 
     1864        $username = strtolower($username); 
     1865 
     1866        if ( !$username ) 
     1867                return false; 
     1868 
     1869        if ( !$password ) { 
     1870                $error = __('<strong>Error</strong>: The password field is empty.'); 
     1871                return false; 
     1872        } 
     1873 
     1874        if ($current_user->data->user_login == $username) 
     1875                return true; 
     1876 
     1877        $login = get_userdatabylogin($username); 
     1878 
     1879        if (!$login) { 
     1880                if( is_site_admin( $username ) ) { 
     1881                        unset( $login ); 
     1882                        $userdetails = get_userdatabylogin( $username ); 
     1883                        $login->user_login = $username; 
     1884                        $login->user_pass = $userdetails->user_pass; 
     1885                } else { 
     1886                        $admins = get_admin_users_for_domain(); 
     1887                        reset( $admins ); 
     1888                        while( list( $key, $val ) = each( $admins ) )  
     1889                        {  
     1890                                if( $val[ 'user_login' ] == $username ) { 
     1891                                        unset( $login ); 
     1892                                        $login->user_login = $username; 
     1893                                        $login->user_pass  = $val[ 'user_pass' ]; 
     1894                                } 
     1895                        } 
     1896                } 
     1897        } 
     1898        if (!$login) { 
     1899                $error = __('<strong>Error</strong>: Wrong username.'); 
     1900                return false; 
     1901        } else { 
     1902                if( is_site_admin( $username ) == false && ( $primary_blog = get_usermeta( $login->ID, "primary_blog" ) ) ) { 
     1903                        $details = get_blog_details( $primary_blog ); 
     1904                        if( is_object( $details ) && $details->archived == 1 || $details->spam == 1 || $details->deleted == 1 ) { 
     1905                                $error = __('<strong>Error</strong>: Blog suspended.'); 
     1906                                return false; 
     1907                        } 
     1908                } 
     1909                // If the password is already_md5, it has been double hashed. 
     1910                // Otherwise, it is plain text. 
     1911                if ( ($already_md5 && $login->user_login == $username && md5($login->user_pass) == $password) || ($login->user_login == $username && $login->user_pass == md5($password)) ) { 
     1912                        return true; 
     1913                } else { 
     1914                        $error = __('<strong>Error</strong>: Incorrect password.'); 
     1915                        $pwd = ''; 
     1916                        return false; 
     1917                } 
     1918        } 
     1919} 
     1920 
     1921function get_userdata( $user_id ) { 
     1922        global $wpdb, $cache_userdata, $wpmuBaseTablePrefix; 
     1923        $user_id = (int) $user_id; 
     1924        if ( $user_id == 0 ) 
     1925                return false; 
     1926 
     1927        $user = wp_cache_get($user_id, 'users'); 
     1928        $user_level = $wpmuBaseTablePrefix . $wpdb->blogid . '_user_level'; 
     1929        if ( $user && is_site_admin( $user->user_login ) ) { 
     1930                $user->$user_level = 10; 
     1931                $user->user_level = 10; 
     1932                $cap_key = $wpdb->prefix . 'capabilities'; 
     1933                $user->{$cap_key} = array( 'administrator' => '1' ); 
     1934                return $user; 
     1935        } elseif ( $user ) { 
     1936                return $user; 
     1937        } 
     1938 
     1939        if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE ID = '$user_id'") ) 
     1940                return false; 
     1941 
     1942        $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user_id' /* pluggable get_userdata */"); 
     1943 
     1944        if ($metavalues) { 
     1945                foreach ( $metavalues as $meta ) { 
     1946                        @ $value = unserialize($meta->meta_value); 
     1947                        if ($value === FALSE) 
     1948                                $value = $meta->meta_value; 
     1949                        $user->{$meta->meta_key} = $value; 
     1950 
     1951                        // We need to set user_level from meta, not row 
     1952                        if ( $wpdb->prefix . 'user_level' == $meta->meta_key ) 
     1953                                $user->user_level = $meta->meta_value; 
     1954                } // end foreach 
     1955        } //end if 
     1956 
     1957        if( is_site_admin( $user->user_login ) == true ) { 
     1958            $user->user_level = 10; 
     1959            $cap_key = $wpdb->prefix . 'capabilities'; 
     1960            $user->{$cap_key} = array( 'administrator' => '1' ); 
     1961        } 
     1962 
     1963        wp_cache_add($user_id, $user, 'users'); 
     1964        wp_cache_add($user->user_login, $user, 'userlogins'); 
     1965 
     1966        return $user; 
     1967} 
     1968 
     1969function get_userdatabylogin($user_login) { 
     1970        global $wpdb; 
     1971        $user_login = sanitize_user( $user_login ); 
     1972 
     1973        if ( empty( $user_login ) ) 
     1974                return false; 
     1975                 
     1976        $userdata = wp_cache_get($user_login, 'userlogins'); 
     1977        if( $userdata && is_site_admin( $user_login ) == true ) { 
     1978                $userdata->user_level = 10; 
     1979                $cap_key = $wpdb->prefix . 'capabilities'; 
     1980                $userdata->{$cap_key} = array( 'administrator' => '1' ); 
     1981                return $userdata; 
     1982        } elseif( $userdata ) 
     1983                return $userdata; 
     1984 
     1985        if ( !$user = $wpdb->get_row("SELECT * FROM $wpdb->users WHERE user_login = '$user_login'") ) 
     1986                return false; 
     1987 
     1988        $metavalues = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = '$user->ID'"); 
     1989 
     1990        if ($metavalues) { 
     1991                foreach ( $metavalues as $meta ) { 
     1992                        @ $value = unserialize($meta->meta_value); 
     1993                        if ($value === FALSE) 
     1994                                $value = $meta->meta_value; 
     1995                        $user->{$meta->meta_key} = $value; 
     1996 
     1997                        // We need to set user_level from meta, not row 
     1998                        if ( $wpdb->prefix . 'user_level' == $meta->meta_key ) 
     1999                                $user->user_level = $meta->meta_value; 
     2000                } 
     2001        } 
     2002        if( is_site_admin( $user_login ) == true ) { 
     2003                $user->user_level = 10; 
     2004                $cap_key = $wpdb->prefix . 'capabilities'; 
     2005                $user->{$cap_key} = array( 'administrator' => '1' ); 
     2006        } 
     2007 
     2008        wp_cache_add($user->ID, $user, 'users'); 
     2009        wp_cache_add($user->user_login, $user, 'userlogins'); 
     2010 
     2011        return $user; 
     2012 
     2013} 
     2014 
     2015?>