Skip to content
Snippets Groups Projects
motd.php 1.72 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?php
    $motd_file = "/etc/motd";
    
    function decode_entities($text, $quote_style = ENT_COMPAT) {
       if (function_exists('html_entity_decode')) {
           $text = html_entity_decode($text, $quote_style, 'ISO-8859-1'); // NOTE: UTF-8 does not work!
       }
       else {
           $trans_tbl = get_html_translation_table(HTML_ENTITIES, $quote_style);
           $trans_tbl = array_flip($trans_tbl);
           $text = strtr($text, $trans_tbl);
       }
       $text = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $text);
       $text = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $text);
       return $text;
    }
    
    $news = $DB->GetAll("SELECT * FROM news WHERE sticky=true AND expiry > now() ORDER BY date DESC");
    if (count($news)<3) {
      $news = $DB->GetAll("(SELECT * FROM news WHERE sticky=false AND expiry > now() ORDER by date DESC LIMIT ".(3-count($news)).") UNION SELECT * FROM news WHERE sticky=true AND expiry > now() ORDER BY date DESC");
    }
    
    
    $motd = "----------------------------- MESSAGE OF THE DAY ------------------------------\n\n";
    for ($i=count($news)-1;$i>-1;$i--) {
    $nitem = str_replace("\n", "", $news[$i]);
    $nitem = str_replace("\r", "", $nitem);
    $motd .= "  ".$nitem['title']."\n";
    $body = str_replace("</p>", "\n\n    ", $nitem['body']);
    $body = str_replace("<br />", "\n    ", $body);
    $body = str_replace("<li>", "  * ", $body);
    $body = str_replace("</li>", "\n    ", $body);
    $body = str_replace("&nbsp;", " ", $body);
    $body = strip_tags($body);
    $body = decode_entities($body);
    $body = wordwrap($body, 75, "\n    ", 1);
    $body = rtrim($body);
    $motd .="    ".$body."\n";
    $motd .= str_pad($nitem['author'], 78, " ", STR_PAD_LEFT)."\n\n";
    }
    $motd .= "---------------------------- [ http://sucs.org/ ] -----------------------------\n";
    
    file_put_contents($motd_file, $motd);
    ?>