diff options
author | Iván Ávalos <avalos@disroot.org> | 2023-09-02 01:25:27 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2023-09-02 01:25:27 -0600 |
commit | 5d3e1971edc6ab314947c42f32e2d3df3444404f (patch) | |
tree | 7dbba55d1a54b013d61e4ce4b60556951ab30bc1 /lib/functions.php | |
parent | 17c9687eba66fc47b3038c1715aa773b5eaf8cf7 (diff) | |
download | microblog-master.tar.gz microblog-master.tar.bz2 microblog-master.zip |
Diffstat (limited to 'lib/functions.php')
-rw-r--r-- | lib/functions.php | 138 |
1 files changed, 0 insertions, 138 deletions
diff --git a/lib/functions.php b/lib/functions.php index 608d473..70bbe17 100644 --- a/lib/functions.php +++ b/lib/functions.php @@ -459,144 +459,6 @@ function filter_tags($html) { return strip_tags($html, $allowed); } -/* function that pings the official micro.blog endpoint for feed refreshes */ -function ping_microblog() { - global $config; - $ping_url = 'https://micro.blog/ping'; - $feed_url = $config['url'].'/feed/json'; - - $ch = curl_init($ping_url); - curl_setopt($ch, CURLOPT_POST, true); - curl_setopt($ch, CURLOPT_POSTFIELDS, 'url='.urlencode($feed_url)); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_HEADER, true); - curl_setopt($ch, CURLOPT_NOBODY, true); - curl_setopt($ch, CURLOPT_TIMEOUT, 10); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - - $response = curl_exec($ch); - $status = curl_getinfo($ch, CURLINFO_HTTP_CODE); - - return ($status == 200) ? true : false; -} - -function rebuild_feeds($amount=10) { - - $posts = db_select_posts(NOW+60, $amount, 'desc'); - - rebuild_json_feed($posts); - rebuild_atom_feed($posts); -} - -function rebuild_json_feed($posts=[]) { - global $config; - - if (!file_exists(ROOT.DS.'feed')) { - mkdir(ROOT.DS.'feed', 0755); - } - - $filename = ROOT.DS.'feed'.DS.'feed.json'; - - $feed = array( - 'version' => 'https://jsonfeed.org/version/1', - 'title' => 'status updates by '.$config['microblog_account'], - 'description' => '', - 'home_page_url' => $config['url'], - 'feed_url' => $config['url'].'/feed/feed.json', - 'user_comment' => '', - 'favicon' => '', - 'author' => array('name' => $config['microblog_account']), - 'items' => array() - ); - - $post_ids = array_column($posts, 'id'); - $attached_files = db_get_attached_files($post_ids); - - foreach($posts as $post) { - - // $attachments = db_get_attached_files($post['id']); - $attachments = !empty($attached_files[$post['id']]) ? $attached_files[$post['id']] : []; - $post_attachments = []; - if(!empty($attachments)) { - foreach ($attachments as $a) { - $post_attachments[] = [ - 'url' => $config['url'] .'/'. get_file_path($a), - 'mime_type' => $a['file_mime_type'], - 'size_in_bytes' => $a['file_size'] - ]; - } - } - - $post_images = array_filter($post_attachments, function($v) { - return strpos($v['mime_type'], 'image') === 0; - }); - - $feed['items'][] = array( - 'id' => ($post['post_guid'] ? 'urn:uuid:'.$post['post_guid'] : $config['url'].'/'.$post['id']), - 'url' => $config['url'].'/'.$post['id'], - 'title' => '', - 'content_html' => $post['post_content'], - 'date_published' => gmdate('Y-m-d\TH:i:s\Z', $post['post_timestamp']), - 'image' => !empty($post_images) ? $post_images[0]['url'] : '', - 'attachments' => $post_attachments - ); - } - - if(file_put_contents($filename, json_encode($feed, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES))) { - return true; - } else return false; -} - -function rebuild_atom_feed($posts=[]) { - global $config; - - if (!file_exists(ROOT.DS.'feed')) { - mkdir(ROOT.DS.'feed', 0755); - } - - $filename = ROOT.DS.'feed'.DS.'feed.xml'; - - $feed = '<?xml version="1.0" encoding="UTF-8" ?'.'>'.NL; - $feed .= '<feed xmlns="http://www.w3.org/2005/Atom">'.NL; - $feed .= '<author><name>'.$config['microblog_account'].'</name></author>'.NL; - $feed .= '<title>status updates by '.$config['microblog_account'].'</title>'.NL; - $feed .= '<id>'.$config['url'].'</id>'.NL; - $feed .= '<updated>'.gmdate('Y-m-d\TH:i:s\Z').'</updated>'.NL; - - $post_ids = array_column($posts, 'id'); - $attached_files = db_get_attached_files($post_ids); - - foreach($posts as $post) { - - $post_images = !empty($attached_files[$post['id']]) ? $attached_files[$post['id']] : []; - - $published = gmdate('Y-m-d\TH:i:s\Z', $post['post_timestamp']); - $updated = ($post['post_edited'] > $post['post_timestamp']) ? gmdate('Y-m-d\TH:i:s\Z', $post['post_edited']) : $published; - - $feed .= '<entry>'.NL; - $feed .= '<title type="text">'.date('Y-m-d H:i', $post['post_timestamp']).'</title>'.NL; - $feed .= '<link rel="alternate" type="text/html" href="'.$config['url'].'/'.$post['id'].'" />'.NL; - $feed .= '<id>'.($post['post_guid'] ? 'urn:uuid:'.$post['post_guid'] : $config['url'].'/'.$post['id']).'</id>'.NL; - $feed .= '<updated>'.$updated.'</updated>'.NL; - $feed .= '<published>'.$published.'</published>'.NL; - - if(!empty($post_images)) { - // todo: render attached images - $feed .= '<content type="text">'.$post['post_content'].'</content>'.NL; - } else { - $feed .= '<content type="text">'.$post['post_content'].'</content>'.NL; - } - - $feed .= '</entry>'.NL; - } - - $feed .= '</feed>'; - - if(file_put_contents($filename, $feed)) { - return true; - } else return false; -} - function uuidv4($data = null) { // https://stackoverflow.com/a/15875555/3625228 $data = $data ?? openssl_random_pseudo_bytes(16); |