Advertisement
Guest User

GVA Sammelpost (Textpattern)

a guest
Mar 5th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.17 KB | None | 0 0
  1. if(@txpinterface == 'admin') {
  2.   add_privs('gva_sammelpost','1,2');
  3.   register_tab('extensions', 'gva_sammelpost', "Sammelpost");
  4.   register_callback('gva_sammelpost_page', 'gva_sammelpost');
  5.   global $gva_sammelpost;
  6. }
  7.  
  8.  
  9. // Prepare tweets and auto-detect hyperlinks, hashtags and usernames
  10. function preparetweet($ret) {
  11.   $ret = preg_replace_callback('#(?<=[\s>])(\()?([\w]+?://(?:[\w\\x80-\\xff\#$%&~/\-=?@\[\](+]|[.,;:](?![\s<])|(?(1)\)(?![\s<])|\)))+)#is', 'clickable_url', $ret);
  12.   $ret = preg_replace_callback('|(#[a-zA-Z0-9_äöüÄÖÜß]*)|is', 'clickable_hashtag', $ret);
  13.   $ret = preg_replace_callback('|(@[a-zA-Z0-9_äöüÄÖÜß]*)|is', 'clickable_username', $ret);
  14.   return trim($ret);  
  15. }
  16.  
  17. function clickable_hashtag($matches) {
  18.   $origin = trim($matches[0]);
  19.   return '<a target="_blank" class="hashtag" href="http://search.twitter.com/search?q=&tag='.substr($origin, 1).'">'.$origin.'</a>';
  20. }
  21.  
  22. function clickable_username($matches) {
  23.   $origin = trim($matches[0]);
  24.   return '<a target="_blank" class="username" href="http://twitter.com/'.substr($origin, 1).'">'.$origin.'</a>';
  25. }
  26.  
  27. function clickable_url($matches) {
  28.   $url = $matches[2];
  29.   if ( empty($url) )
  30.   return $matches[0];
  31.   return $matches[1] . "<a class='link' href=\"$url\">$url</a>";
  32. }
  33.  
  34. // Turn date to relative string
  35. function reldate($input, $now = false) {
  36.     if (!$now) { $now = time(); }
  37.     $diff = $now - $input;
  38.     if ($diff < 3600) {
  39.         $value = floor($diff / 60);
  40.         $unit = "Minute";
  41.         $unit2 = "Minuten";
  42.     } elseif ($diff < 86400) {
  43.         $value = floor($diff / 3600);
  44.         $unit = "Stunde";
  45.         $unit2 = "Stunden";
  46.     } else {
  47.         $value = floor($diff / 86400);
  48.         $unit = "Tag";
  49.         $unit2 = "Tagen";
  50.     }
  51.     if ($value == 1) {    
  52.         return "vor ".$value." ".$unit;
  53.     } else {
  54.         return "vor ".$value." ".$unit2;
  55.     }
  56. }
  57.  
  58.  
  59. function gva_sammelpost() {
  60.   global $prefs;
  61.   $thismonday = getmonday(time());
  62.   $lastmonday = getmonday(time()-604800);
  63.  
  64.   if ($thismonday > $prefs['gva_last_monday']) {
  65.     safe_update("txp_prefs", "val = '".$thismonday."'", "name = 'gva_last_monday' and prefs_id = 1" );
  66.  
  67.     if ($postdata = build_sammelpost($thismonday, $lastmonday)) {
  68.  
  69. extract($postdata);
  70.  
  71.     safe_insert("textpattern",
  72.       "Title          = '$title',
  73.      Body            = '$text',
  74.      Body_html       = '$text',
  75.      Status          = '4',
  76.      Posted          = '$posted',
  77.      LastMod         = '$posted',
  78.      AuthorID        = 'Turbo-G',
  79.      Section         = 'weblog',
  80.      Category1       = 'sammelpost',
  81.      textile_body    = '$text',
  82.      url_title       = '$urltitle',
  83.      Annotate        = '1',
  84.      AnnotateInvite  = 'Kommentare',
  85.      uid             = '".md5(uniqid(rand(),true))."',
  86.      feed_time       = '$posted'");
  87.     }
  88.   }
  89.   return '<!-- Sammelpost wurde ausgefuehrt -->';
  90. }
  91.  
  92.  
  93. function build_sammelpost($thismonday, $lastmonday) {
  94.   global $prefs;
  95.   require_once ($prefs['file_base_path'].'/simplepie.inc');
  96.   $feed = new SimplePie("http://pipes.yahoo.com/pipes/pipe.run?_id=4415799d9a31a393138dcbc5a197ff2e&_render=rss", $prefs['file_base_path'].'/cache/',60);
  97.   $feed->handle_content_type();
  98.  
  99.   $posttext = "";
  100.   foreach ($feed->get_items() as $item) {
  101.  
  102.     $itemtime = strtotime($item->get_date());
  103.     if ($itemtime >= $lastmonday && $itemtime < $thismonday) {
  104.  
  105. // twitterkram
  106. if (substr($item->get_permalink(),0,18) == "http://twitter.com") {
  107.  
  108.  
  109. $posttext .= '<li>'.preparetweet($item->get_title()).'<br>(<a href="'.$item->get_permalink().'">'.reldate(strtotime($item->get_date()))."</a>)</li>\n";
  110.  
  111. // non-twitterkram
  112. } else {
  113.       $posttext .= '<li><a href="'.$item->get_permalink().'">'.$item->get_title()."</a><br />\n";
  114.  
  115.       if ($item->get_description() != $item->get_title()) {
  116.         $summary = substr(strip_tags($item->get_description()), 0, 140);
  117.         if ($summary != $item->get_description()) {
  118.           $posttext .= $summary."&hellip;</li>\n";
  119.         } else {
  120.           $posttext .= $summary."</li>\n";
  121.         }
  122.       }
  123.     }
  124.   }
  125. }
  126.  
  127.   $postdata = array();
  128.   if (!empty($posttext)) {
  129.     $posttext = '<p>Netzfundstücke vom '.date("d.m.", $lastmonday).' bis zum '.date("d.m.", $thismonday).':</p><ul class="sammelpost">'.$posttext."</ul>";
  130.     $postdata['title'] = 'Wochenlinks';
  131.     $postdata['posted'] = date("Y-m-d H:i:s", $thismonday+(60*60));
  132.     $postdata['urltitle'] = 'wochenlinks-'.date("Y-m-d", $thismonday);
  133.     $postdata['text'] = str_replace("'", "\'", $posttext);
  134.     return $postdata;
  135.   } else {
  136.     return false;
  137.   }
  138. }
  139.  
  140. function gva_sammelpost_page() {
  141.   global $step;
  142.   require_privs('gva_sammelpost');
  143.   pagetop('Sammelpost');
  144.   var_dump(build_sammelpost(getmonday(time()+500000), getmonday(time()-604800)));
  145. }
  146.  
  147. function getmonday($timestamp) {
  148.   $dayoffset = (int) date('w', $timestamp) - 1;
  149.   if ($dayoffset == -1) $dayoffset = 6; // Sonntag ist 0, ganz doof
  150.   $houroffset = (int) date('H', $timestamp);
  151.   $minoffset = (int) date('i', $timestamp);
  152.   $secoffset = (int) date('s', $timestamp);
  153.   return ($timestamp - ($dayoffset*24*60*60) - ($houroffset*60*60) - ($minoffset*60) - $secoffset);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement