Advertisement
ovizii

CleanUp WPMU

Feb 13th, 2012
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.80 KB | None | 0 0
  1. <?php
  2. /**********************************************************
  3.   WPMU Cleanup
  4.  
  5. This script checks the WPMU database for blogs that have
  6. not been updated for X days, then sends a notice to the
  7. blogowner that the blog will be deleted after Y days.
  8.  
  9. It also automatically deletes blogs that have not been
  10. updated for Y days, then sends an email to the blogowner.
  11.  
  12. On both occasions, the script will send a report with
  13. details on warned and deleted blogs to the administrator.
  14.  
  15. This script has *NOT* been programmed as a plugin since
  16. it is intended to be executed by a cronjob. As it is now
  17. it should be run once every 7 days, but you can configure
  18. it to do otherwise.
  19.  
  20. This script is *NOT* a final version and should be used
  21. with caution and only if you know what you are doing. I
  22. recommend testing it on a testsite rather than a livesite.
  23.  
  24. Please feel free to comment at p(a)studinski.dk.
  25.  
  26.   TODO:
  27.  
  28. - CSV/TXT-file with list of sent notices with timestamps.
  29.   - Either of two functions in this regard:
  30.     - Make sure bloggers only receive one warning.
  31.     - Make the "x days until deletion"-message variable.
  32. - Authentication for users that wishes to run it manually.
  33. - Your suggestions?
  34. **********************************************************/
  35.  
  36. /* SETTINGS */
  37.  
  38. $mysql_server               = 'localhost';
  39. $mysql_username             = 'my_user';
  40. $mysql_password             = 'my_pass';
  41. $mysql_database             = 'my_db';
  42.  
  43. $options['email_from']      = 'suport@zice.ro'; // Email-address used for sending notices.
  44. $options['email_subject']   = 'Blogul tau pe zice.ro';
  45. $options['mail_report']     = 'suport@zice.ro'; // Reports will be sent to this address.
  46. $options['timetomail']      = 180; // Number of days blog can be inactive before receiving notice of deletion.
  47. $options['timetodeletion']  = 365; // Number of days blog can be inactive before deletion.
  48. $options['dateformat']      = 'd-m-Y'; // Dateformat used in emails.
  49.  
  50. $options['noticemessage']    = 'Draga utilizator zice.ro,' . "\n\n" . 'Blogul tau nu a fost updatat in %timetomail% zile. Acest mesaj este trimis pentru a iti aduce aminte ca blogul tau va fi sters daca nu este updatat timp de %timetodeletion% zile.' . "\n\n" . 'Daca doresti sa-ti pastrezi blogul te rugam sa te loghezi si sa iti updatezi blogul înainte de %date%. te poti loga aici:' . "\n\n" . '%loginurl%' . "\n\n" . 'Cu stima,' . "\n\n" . 'echipa zice.ro.';
  51. $options['deletemessage']    = 'Draga utilizator zice.ro,' . "\n\n" . 'Pentru ca nu ti-ai updatat blogul timp de %timetodeletion% zile a fost sters automat din reteaua zice.ro.' . "\n\n" . 'Contul tau de utilizator nu a fost sters, deci daca o sa doresti vreodata sa-ti faci alt blog esti binevenit sa te loghezi aici pentru ati crea un nou blog:' . "\n\n" . 'http://zice.ro/wp-login.php' . "\n\n" . 'Cu stima,' . "\n\n" . 'Echipa zice.ro.';
  52.  
  53. /********************/
  54. /*** STOP EDITING ***/
  55. /********************/
  56.  
  57. $options['unix_timetomail']     = $options['timetomail'] * 24 * 60 * 60;
  58. $options['unix_timetodeletion'] = $options['timetodeletion'] * 24 * 60 * 60;
  59. $options['date']                = date($options['dateformat'], (($options['timetodeletion'] - $options['timetomail']) * 24 * 60 * 60) + time());
  60.  
  61. // Connect to database
  62. $db =   mysql_connect($mysql_server,
  63.                        $mysql_username,
  64.                        $mysql_password);
  65.                            
  66.         mysql_select_db($mysql_database, $db);
  67.              
  68. // Get blogs
  69. $sql = 'SELECT blog_id,last_updated FROM wp_blogs';
  70. $sql = mysql_query($sql);
  71.  
  72.         while( $row = mysql_fetch_assoc($sql) ) :
  73.        
  74.             $blogs_raw[$row['blog_id']] = strtotime($row['last_updated']);
  75.            
  76.         endwhile;
  77.        
  78.         // Get blogs for deletion notice
  79.         foreach( $blogs_raw as $key => $value ) :
  80.  
  81.             $time = time() - $value;
  82.        
  83.             if( $time >= $options['unix_timetomail'] ) :
  84.            
  85.                 $blogs_mail[] = array('blogid'  => $key,
  86.                                       'email'   => '',
  87.                                       'siteurl' => '');
  88.            
  89.             endif;
  90.            
  91.         endforeach;
  92.        
  93.                 // Get userdata for deletion notice
  94.                 for($i = 0; $i < sizeof($blogs_mail); $i++) :
  95.  
  96.                     $str = 'admin_email';              
  97.                     $sql = mysql_query("SELECT option_value FROM wp_" . $blogs_mail[$i]['blogid'] . "_options WHERE option_name = '$str'");
  98.                    
  99.                         while ( $row = mysql_fetch_assoc($sql) ) :
  100.                        
  101.                             $blogs_mail[$i]['email'] = $row['option_value'];
  102.                            
  103.                         endwhile;
  104.                        
  105.                     $str = 'siteurl';
  106.                     $sql = mysql_query("SELECT option_value FROM wp_" . $blogs_mail[$i]['blogid'] . "_options WHERE option_name = '$str'");
  107.                    
  108.                         while ( $row = mysql_fetch_assoc($sql) ) :
  109.                        
  110.                             $blogs_mail[$i]['siteurl'] = $row['option_value'];
  111.                            
  112.                         endwhile;
  113.                        
  114.                 endfor;
  115.                
  116.                         $cronmsg = 'Following blogs have been noticed of deletion:' . "\n\n";
  117.                         // Create and send warning notice to users
  118.                         for($i = 0; $i < sizeof($blogs_mail); $i++) :
  119.                        
  120.                             $message = str_replace('%timetomail%', $options['timetomail'], $options['noticemessage']);
  121.                             $message = str_replace('%timetodeletion%', $options['timetodeletion'], $message);
  122.                             $message = str_replace('%date%', $options['date'], $message);
  123.                             $message = str_replace('%loginurl%', $blogs_mail[$i]['siteurl'] . 'wp-login.php', $message);
  124.                            
  125.                             $to      = $blogs_mail[$i]['email'];
  126.                             $subject = $options['email_subject'];
  127.                             $headers = 'From: ' . $options['email_from'] . "\r\n" .
  128.                                        'Reply-To: ' . $options['email_from'] . "\r\n" .
  129.                                        'X-Mailer: PHP/' . phpversion();
  130.  
  131.                             mail($to, $subject, $message, $headers);
  132.                            
  133.                             $cronmsg .= $blogs_mail[$i]['siteurl'] . "\n";
  134.                            
  135.                         endfor;
  136.                         mail($options['mail_report'], 'Report - Sent notices', $cronmsg);
  137.                
  138.  
  139.        
  140.         // Get blogs for deletion
  141.         foreach( $blogs_raw as $key => $value ) :
  142.        
  143.             $time = time() - $value;
  144.            
  145.             if( $time >= $options['unix_timetodeletion'] ) :
  146.            
  147.                 $blogs_delete[] = array('blogid'  => $key,
  148.                                         'email'   => '',
  149.                                         'siteurl' => '');
  150.                
  151.             endif;
  152.            
  153.         endforeach;
  154.        
  155.                 // Get userdata for deletion notice
  156.                 for($i = 0; $i < sizeof($blogs_delete); $i++) :
  157.  
  158.                     $str = 'admin_email';              
  159.                     $sql = mysql_query("SELECT option_value FROM wp_" . $blogs_delete[$i]['blogid'] . "_options WHERE option_name = '$str'");
  160.                    
  161.                         while ( $row = mysql_fetch_assoc($sql) ) :
  162.                        
  163.                             $blogs_delete[$i]['email'] = $row['option_value'];
  164.                            
  165.                         endwhile;
  166.                        
  167.                     $str = 'siteurl';
  168.                     $sql = mysql_query("SELECT option_value FROM wp_" . $blogs_mail[$i]['blogid'] . "_options WHERE option_name = '$str'");
  169.                    
  170.                         while ( $row = mysql_fetch_assoc($sql) ) :
  171.                        
  172.                             $blogs_delete[$i]['siteurl'] = $row['option_value'];
  173.                            
  174.                         endwhile;
  175.  
  176.                 endfor;
  177.                
  178.                         // Delete tables
  179.                         // $cronmsg = 'Following blogs have been deleted:' . "\n\n";
  180.                         for($i = 0; $i < sizeof($blogs_delete); $i++) :
  181.                        
  182.                             $prefix = 'wp_' . $blogs_delete[$i]['blogid'] . '_';
  183.                        
  184.                             $sql_categories     = 'DROP TABLE IF EXISTS ' . $prefix . 'categories';
  185.                             $sql_comments       = 'DROP TABLE IF EXISTS ' . $prefix . 'comments';
  186.                             $sql_link2cat       = 'DROP TABLE IF EXISTS ' . $prefix . 'link2cat';
  187.                             $sql_linkcategories = 'DROP TABLE IF EXISTS ' . $prefix . 'linkcategories';
  188.                             $sql_links          = 'DROP TABLE IF EXISTS ' . $prefix . 'links';
  189.                             $sql_options        = 'DROP TABLE IF EXISTS ' . $prefix . 'options';
  190.                             $sql_post2cat       = 'DROP TABLE IF EXISTS ' . $prefix . 'post2cat';
  191.                             $sql_postmeta       = 'DROP TABLE IF EXISTS ' . $prefix . 'postmeta';
  192.                             $sql_posts          = 'DROP TABLE IF EXISTS ' . $prefix . 'posts';
  193.                             $sql_blogid         = $blogs_delete[$i]['blogid'];
  194.                                                        
  195.                             mysql_query($sql_categories) or die('Error: ' . mysql_error());
  196.                             mysql_query($sql_comments) or die('Error: ' . mysql_error());
  197.                             mysql_query($sql_link2cat) or die('Error: ' . mysql_error());
  198.                             mysql_query($sql_linkcategories) or die('Error: ' . mysql_error());
  199.                             mysql_query($sql_links) or die('Error: ' . mysql_error());
  200.                             mysql_query($sql_options) or die('Error: ' . mysql_error());
  201.                             mysql_query($sql_post2cat) or die('Error: ' . mysql_error());
  202.                             mysql_query($sql_postmeta) or die('Error: ' . mysql_error());
  203.                             mysql_query("DELETE FROM wp_blogs WHERE blog_id = '$sql_blogid'") or die('Error: ' . mysql_error());
  204.                            
  205.                             // Send mail
  206.                             $message = str_replace('%timetodeletion%', $options['timetodeletion'], $options['deletemessage']);
  207.                            
  208.                             $to      = $blogs_delete[$i]['email'];
  209.                             $subject = $options['email_subject'];
  210.                             $headers = 'From: ' . $options['email_from'] . "\r\n" .
  211.                                        'Reply-To: ' . $options['email_from'] . "\r\n" .
  212.                                        'X-Mailer: PHP/' . phpversion();
  213.  
  214.                             mail($to, $subject, $message, $headers);
  215.                            
  216.                             $cronmsg .= $blogs_delete[$i]['siteurl'] . "\n";
  217.                            
  218.                         endfor;
  219.                         mail($options['mail_report'], 'Report - Deleted blogs', $cronmsg);
  220.        
  221. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement