Ejemplo n.º 1
0
 public function items()
 {
     if ($this->input->is_ajax_request()) {
         $this->readerself_library->set_template('_json');
         $this->readerself_library->set_content_type('application/json');
         $content = array();
     } else {
         $this->readerself_library->set_template('_plain');
         $this->readerself_library->set_content_type('text/plain');
         $content = '';
     }
     if ($this->input->is_cli_request() && !$this->config->item('refresh_by_cron')) {
         $content .= 'Refresh by cron disabled' . "\n";
     } else {
         include_once 'thirdparty/simplepie/autoloader.php';
         include_once 'thirdparty/simplepie/idn/idna_convert.class.php';
         if ($this->config->item('facebook/enabled')) {
             include_once 'thirdparty/facebook/autoload.php';
             $fb = new Facebook\Facebook(array('app_id' => $this->config->item('facebook/id'), 'app_secret' => $this->config->item('facebook/secret')));
             $fbApp = $fb->getApp();
             $accessToken = $fbApp->getAccessToken();
         }
         $query = $this->db->query('SELECT fed.* FROM ' . $this->db->dbprefix('feeds') . ' AS fed WHERE fed.fed_nextcrawl IS NULL OR fed.fed_nextcrawl <= ? GROUP BY fed.fed_id HAVING (SELECT COUNT(DISTINCT(sub.mbr_id)) FROM ' . $this->db->dbprefix('subscriptions') . ' AS sub WHERE sub.fed_id = fed.fed_id) > 0', array(date('Y-m-d H:i:s')));
         if ($query->num_rows() > 0) {
             $microtime_start = microtime(1);
             $errors = 0;
             foreach ($query->result() as $fed) {
                 $parse_url = parse_url($fed->fed_link);
                 if (isset($parse_url['host']) == 1 && $parse_url['host'] == 'www.facebook.com' && $this->config->item('facebook/enabled')) {
                     try {
                         $parts = explode('/', $parse_url['path']);
                         $total_parts = count($parts);
                         $last_part = $parts[$total_parts - 1];
                         $request = new Facebook\FacebookRequest($fbApp, $accessToken, 'GET', $last_part . '?fields=link,name,about');
                         $response = $fb->getClient()->sendRequest($request);
                         $result = $response->getDecodedBody();
                         $request = new Facebook\FacebookRequest($fbApp, $accessToken, 'GET', $last_part . '?fields=feed{created_time,id,message,story,full_picture,place,type,status_type,link}');
                         $response = $fb->getClient()->sendRequest($request);
                         $posts = $response->getDecodedBody();
                         $this->readerself_library->crawl_items_facebook($fed->fed_id, $posts['feed']['data']);
                         $lastitem = $this->db->query('SELECT itm.itm_datecreated FROM ' . $this->db->dbprefix('items') . ' AS itm WHERE itm.fed_id = ? GROUP BY itm.itm_id ORDER BY itm.itm_id DESC LIMIT 0,1', array($fed->fed_id))->row();
                         $this->db->set('fed_title', $result['name']);
                         $this->db->set('fed_url', $result['link']);
                         $this->db->set('fed_link', $result['link']);
                         if (isset($parse_url['host']) == 1) {
                             $this->db->set('fed_host', $parse_url['host']);
                         }
                         $this->db->set('fed_description', $result['about']);
                         $this->db->set('fed_lasterror', '');
                         $this->db->set('fed_lastcrawl', date('Y-m-d H:i:s'));
                         if ($lastitem) {
                             $nextcrawl = '';
                             //older than 96 hours, next crawl in 12 hours
                             if ($lastitem->itm_datecreated < date('Y-m-d H:i:s', time() - 3600 * 24 * 96)) {
                                 $nextcrawl = date('Y-m-d H:i:s', time() + 3600 * 12);
                                 //older than 48 hours, next crawl in 6 hours
                             } else {
                                 if ($lastitem->itm_datecreated < date('Y-m-d H:i:s', time() - 3600 * 48)) {
                                     $nextcrawl = date('Y-m-d H:i:s', time() + 3600 * 6);
                                     //older than 24 hours, next crawl in 3 hours
                                 } else {
                                     if ($lastitem->itm_datecreated < date('Y-m-d H:i:s', time() - 3600 * 24)) {
                                         $nextcrawl = date('Y-m-d H:i:s', time() + 3600 * 3);
                                     }
                                 }
                             }
                             $this->db->set('fed_nextcrawl', $nextcrawl);
                         }
                         $this->db->where('fed_id', $fed->fed_id);
                         $this->db->update('feeds');
                     } catch (Facebook\Exceptions\FacebookResponseException $e) {
                         $errors++;
                         $this->db->set('fed_lasterror', 'Graph returned an error: ' . $e->getMessage());
                         $this->db->set('fed_lastcrawl', date('Y-m-d H:i:s'));
                         $this->db->where('fed_id', $fed->fed_id);
                         $this->db->update('feeds');
                     } catch (Facebook\Exceptions\FacebookSDKException $e) {
                         $errors++;
                         $this->db->set('fed_lasterror', 'Facebook SDK returned an error: ' . $e->getMessage());
                         $this->db->set('fed_lastcrawl', date('Y-m-d H:i:s'));
                         $this->db->where('fed_id', $fed->fed_id);
                         $this->db->update('feeds');
                     }
                 } else {
                     $sp_feed = new SimplePie();
                     $sp_feed->set_feed_url(convert_to_ascii($fed->fed_link));
                     $sp_feed->enable_cache(false);
                     $sp_feed->set_timeout(5);
                     $sp_feed->force_feed(true);
                     $sp_feed->init();
                     $sp_feed->handle_content_type();
                     if ($sp_feed->error()) {
                         $errors++;
                         $this->db->set('fed_lasterror', $sp_feed->error());
                         $this->db->set('fed_lastcrawl', date('Y-m-d H:i:s'));
                         $this->db->where('fed_id', $fed->fed_id);
                         $this->db->update('feeds');
                     } else {
                         $this->readerself_library->crawl_items($fed->fed_id, $sp_feed->get_items());
                         $lastitem = $this->db->query('SELECT itm.itm_datecreated FROM ' . $this->db->dbprefix('items') . ' AS itm WHERE itm.fed_id = ? GROUP BY itm.itm_id ORDER BY itm.itm_id DESC LIMIT 0,1', array($fed->fed_id))->row();
                         $parse_url = parse_url($sp_feed->get_link());
                         $this->db->set('fed_title', $sp_feed->get_title());
                         $this->db->set('fed_url', $sp_feed->get_link());
                         $this->db->set('fed_link', $sp_feed->subscribe_url());
                         if (isset($parse_url['host']) == 1) {
                             $this->db->set('fed_host', $parse_url['host']);
                         }
                         if ($sp_feed->get_type() & SIMPLEPIE_TYPE_RSS_ALL) {
                             $this->db->set('fed_type', 'rss');
                         } else {
                             if ($sp_feed->get_type() & SIMPLEPIE_TYPE_ATOM_ALL) {
                                 $this->db->set('fed_type', 'atom');
                             }
                         }
                         if ($sp_feed->get_image_url()) {
                             $this->db->set('fed_image', $sp_feed->get_image_url());
                         }
                         $this->db->set('fed_description', $sp_feed->get_description());
                         $this->db->set('fed_lasterror', '');
                         $this->db->set('fed_lastcrawl', date('Y-m-d H:i:s'));
                         if ($lastitem) {
                             $nextcrawl = '';
                             //older than 96 hours, next crawl in 12 hours
                             if ($lastitem->itm_datecreated < date('Y-m-d H:i:s', time() - 3600 * 24 * 96)) {
                                 $nextcrawl = date('Y-m-d H:i:s', time() + 3600 * 12);
                                 //older than 48 hours, next crawl in 6 hours
                             } else {
                                 if ($lastitem->itm_datecreated < date('Y-m-d H:i:s', time() - 3600 * 48)) {
                                     $nextcrawl = date('Y-m-d H:i:s', time() + 3600 * 6);
                                     //older than 24 hours, next crawl in 3 hours
                                 } else {
                                     if ($lastitem->itm_datecreated < date('Y-m-d H:i:s', time() - 3600 * 24)) {
                                         $nextcrawl = date('Y-m-d H:i:s', time() + 3600 * 3);
                                     }
                                 }
                             }
                             $this->db->set('fed_nextcrawl', $nextcrawl);
                         }
                         $this->db->where('fed_id', $fed->fed_id);
                         $this->db->update('feeds');
                     }
                     $sp_feed->__destruct();
                     unset($sp_feed);
                 }
             }
             $this->db->set('crr_time', microtime(1) - $microtime_start);
             if (function_exists('memory_get_peak_usage')) {
                 $this->db->set('crr_memory', memory_get_peak_usage());
             }
             $this->db->set('crr_feeds', $query->num_rows());
             if ($errors > 0) {
                 $this->db->set('crr_errors', $errors);
             }
             $this->db->set('crr_datecreated', date('Y-m-d H:i:s'));
             $this->db->insert('crawler');
             if ($this->db->dbdriver == 'mysqli') {
                 $this->db->query('OPTIMIZE TABLE categories, connections, enclosures, favorites, feeds, folders, history, items, members, share, subscriptions');
             }
         }
     }
     $this->readerself_library->set_content($content);
 }
Ejemplo n.º 2
0
 public function create()
 {
     if (!$this->axipi_session->userdata('mbr_id')) {
         redirect(base_url() . '?u=' . $this->input->get('u'));
     }
     $data = array();
     $content = array();
     $this->load->library(array('form_validation', 'analyzer_library'));
     if ($this->config->item('folders')) {
         $query = $this->db->query('SELECT flr.* FROM ' . $this->db->dbprefix('folders') . ' AS flr WHERE flr.mbr_id = ? GROUP BY flr.flr_id ORDER BY flr.flr_title ASC', array($this->member->mbr_id));
         $data['folders'] = array();
         $data['folders'][0] = $this->lang->line('no_folder');
         if ($query->num_rows() > 0) {
             foreach ($query->result() as $flr) {
                 $data['folders'][$flr->flr_id] = $flr->flr_title;
             }
         }
     }
     $this->form_validation->set_rules('url', 'lang:url_feed', 'required');
     if ($this->config->item('folders')) {
         $this->form_validation->set_rules('folder', 'lang:folder', 'required');
     }
     $this->form_validation->set_rules('priority', 'lang:priority', 'numeric');
     //$this->form_validation->set_rules('direction', 'lang:direction', '');
     $data['error'] = false;
     $data['feeds'] = array();
     if ($this->input->post('url') && !$this->input->post('analyze_done')) {
         $this->analyzer_library->start($this->input->post('url'));
         $metas = $this->analyzer_library->metas;
         if (count($metas) > 0) {
             $data['feeds'][''] = '-';
             foreach ($metas as $meta) {
                 $add = true;
                 $headers = get_headers($meta['href'], 1);
                 if (isset($headers['Location']) == 1) {
                     $meta['href'] = $headers['Location'];
                     $headers = get_headers($meta['href'], 1);
                     if (isset($headers['Location']) == 1) {
                         $add = false;
                     }
                 }
                 if ($add) {
                     if ($meta['title'] == '') {
                         $data['feeds'][$meta['href']] = $meta['href'];
                     } else {
                         $this->analyzer_library->encoding($meta['title']);
                         $data['feeds'][$meta['href']] = $meta['title'];
                     }
                 }
             }
         }
     }
     if ($this->form_validation->run() == FALSE || count($data['feeds']) > 0) {
         $content = $this->load->view('subscriptions_create', $data, TRUE);
     } else {
         if ($this->config->item('folders')) {
             $folder = false;
             if ($this->input->post('folder')) {
                 $query = $this->db->query('SELECT flr.* FROM ' . $this->db->dbprefix('folders') . ' AS flr WHERE flr.mbr_id = ? AND flr.flr_id = ? GROUP BY flr.flr_id', array($this->member->mbr_id, $this->input->post('folder')));
                 if ($query->num_rows() > 0) {
                     $folder = $this->input->post('folder');
                 }
             }
         }
         $query = $this->db->query('SELECT fed.*, sub.sub_id FROM ' . $this->db->dbprefix('feeds') . ' AS fed LEFT JOIN ' . $this->db->dbprefix('subscriptions') . ' AS sub ON sub.fed_id = fed.fed_id AND sub.mbr_id = ? WHERE fed.fed_link = ? GROUP BY fed.fed_id', array($this->member->mbr_id, $this->input->post('url')));
         if ($query->num_rows() == 0) {
             $parse_url = parse_url($this->input->post('url'));
             if (isset($parse_url['host']) == 1 && $parse_url['host'] == 'instagram.com' && $this->config->item('instagram/enabled')) {
                 if ($this->config->item('instagram/access_token')) {
                     $parts = explode('/', rtrim($parse_url['path'], '/'));
                     $total_parts = count($parts);
                     $last_part = $parts[$total_parts - 1];
                     $result = json_decode(file_get_contents('https://api.instagram.com/v1/users/search?q=' . $last_part . '&count=15&access_token=' . $this->config->item('instagram/access_token')));
                     if (count($result->data) == 0) {
                         $data['error'] = 'User not found';
                     } else {
                         $user_id = false;
                         foreach ($result->data as $user) {
                             if ($user->username == $last_part) {
                                 $user_id = $user->id;
                                 break;
                             }
                         }
                         if (!$user_id) {
                             $data['error'] = 'User not found';
                         } else {
                             $result = json_decode(file_get_contents('https://api.instagram.com/v1/users/' . $user_id . '?access_token=' . $this->config->item('instagram/access_token')));
                             $this->db->set('fed_title', 'Instagram @' . $result->data->username);
                             $this->db->set('fed_url', $this->input->post('url'));
                             $this->db->set('fed_description', $result->data->bio);
                             $this->db->set('fed_image', $result->data->profile_picture);
                             $this->db->set('fed_link', $this->input->post('url'));
                             if (isset($parse_url['host']) == 1) {
                                 $this->db->set('fed_host', $parse_url['host']);
                             }
                             $this->db->set('fed_lastcrawl', date('Y-m-d H:i:s'));
                             $this->db->set('fed_datecreated', date('Y-m-d H:i:s'));
                             $this->db->insert('feeds');
                             $fed_id = $this->db->insert_id();
                             $this->db->set('mbr_id', $this->member->mbr_id);
                             $this->db->set('fed_id', $fed_id);
                             if ($this->config->item('folders')) {
                                 if ($folder) {
                                     $this->db->set('flr_id', $folder);
                                 }
                             }
                             $this->db->set('sub_priority', $this->input->post('priority'));
                             $this->db->set('sub_direction', $this->input->post('direction'));
                             $this->db->set('sub_datecreated', date('Y-m-d H:i:s'));
                             $this->db->insert('subscriptions');
                             $sub_id = $this->db->insert_id();
                             $result = json_decode(file_get_contents('https://api.instagram.com/v1/users/' . $user_id . '/media/recent?access_token=' . $this->config->item('instagram/access_token')));
                             $this->readerself_library->crawl_items_instagram($fed_id, $result->data);
                         }
                     }
                 }
             } else {
                 if (isset($parse_url['host']) == 1 && $parse_url['host'] == 'www.facebook.com' && $this->config->item('facebook/enabled')) {
                     include_once 'thirdparty/facebook/autoload.php';
                     $fb = new Facebook\Facebook(array('app_id' => $this->config->item('facebook/id'), 'app_secret' => $this->config->item('facebook/secret')));
                     $fbApp = $fb->getApp();
                     $accessToken = $fbApp->getAccessToken();
                     try {
                         $parts = explode('/', rtrim($parse_url['path'], '/'));
                         $total_parts = count($parts);
                         $last_part = $parts[$total_parts - 1];
                         $request = new Facebook\FacebookRequest($fbApp, $accessToken, 'GET', $last_part . '?fields=link,name,about');
                         $response = $fb->getClient()->sendRequest($request);
                         $result = $response->getDecodedBody();
                         $this->db->set('fed_title', $result['name']);
                         $this->db->set('fed_url', $result['link']);
                         $this->db->set('fed_description', $result['about']);
                         $this->db->set('fed_link', $result['link']);
                         if (isset($parse_url['host']) == 1) {
                             $this->db->set('fed_host', $parse_url['host']);
                         }
                         $this->db->set('fed_lastcrawl', date('Y-m-d H:i:s'));
                         $this->db->set('fed_datecreated', date('Y-m-d H:i:s'));
                         $this->db->insert('feeds');
                         $fed_id = $this->db->insert_id();
                         $this->db->set('mbr_id', $this->member->mbr_id);
                         $this->db->set('fed_id', $fed_id);
                         if ($this->config->item('folders')) {
                             if ($folder) {
                                 $this->db->set('flr_id', $folder);
                             }
                         }
                         $this->db->set('sub_priority', $this->input->post('priority'));
                         $this->db->set('sub_direction', $this->input->post('direction'));
                         $this->db->set('sub_datecreated', date('Y-m-d H:i:s'));
                         $this->db->insert('subscriptions');
                         $sub_id = $this->db->insert_id();
                         $request = new Facebook\FacebookRequest($fbApp, $accessToken, 'GET', $last_part . '?fields=feed{created_time,id,message,story,full_picture,place,type,status_type,link}');
                         $response = $fb->getClient()->sendRequest($request);
                         $posts = $response->getDecodedBody();
                         $this->readerself_library->crawl_items_facebook($fed_id, $posts['feed']['data']);
                         redirect(base_url() . 'subscriptions/read/' . $sub_id);
                     } catch (Facebook\Exceptions\FacebookResponseException $e) {
                         $data['error'] = 'Graph returned an error: ' . $e->getMessage();
                     } catch (Facebook\Exceptions\FacebookSDKException $e) {
                         $data['error'] = 'Facebook SDK returned an error: ' . $e->getMessage();
                     }
                 } else {
                     include_once 'thirdparty/simplepie/autoloader.php';
                     include_once 'thirdparty/simplepie/idn/idna_convert.class.php';
                     $sp_feed = new SimplePie();
                     $sp_feed->set_feed_url(convert_to_ascii($this->input->post('url')));
                     $sp_feed->enable_cache(false);
                     $sp_feed->set_timeout(60);
                     $sp_feed->force_feed(true);
                     $sp_feed->init();
                     $sp_feed->handle_content_type();
                     if ($sp_feed->error()) {
                         $data['error'] = $sp_feed->error();
                     } else {
                         $parse_url = parse_url($sp_feed->get_link());
                         $this->db->set('fed_title', $sp_feed->get_title());
                         $this->db->set('fed_url', $sp_feed->get_link());
                         $this->db->set('fed_description', $sp_feed->get_description());
                         $this->db->set('fed_link', $sp_feed->subscribe_url());
                         if (isset($parse_url['host']) == 1) {
                             $this->db->set('fed_host', $parse_url['host']);
                         }
                         $this->db->set('fed_lastcrawl', date('Y-m-d H:i:s'));
                         $this->db->set('fed_datecreated', date('Y-m-d H:i:s'));
                         $this->db->insert('feeds');
                         $fed_id = $this->db->insert_id();
                         $this->db->set('mbr_id', $this->member->mbr_id);
                         $this->db->set('fed_id', $fed_id);
                         if ($this->config->item('folders')) {
                             if ($folder) {
                                 $this->db->set('flr_id', $folder);
                             }
                         }
                         $this->db->set('sub_priority', $this->input->post('priority'));
                         $this->db->set('sub_direction', $this->input->post('direction'));
                         $this->db->set('sub_datecreated', date('Y-m-d H:i:s'));
                         $this->db->insert('subscriptions');
                         $sub_id = $this->db->insert_id();
                         $data['sub_id'] = $sub_id;
                         $data['fed_title'] = $sp_feed->get_title();
                         $this->readerself_library->crawl_items($fed_id, $sp_feed->get_items());
                     }
                     $sp_feed->__destruct();
                     unset($sp_feed);
                 }
             }
         } else {
             $fed = $query->row();
             if (!$fed->sub_id) {
                 $this->db->set('mbr_id', $this->member->mbr_id);
                 $this->db->set('fed_id', $fed->fed_id);
                 if ($this->config->item('folders')) {
                     if ($folder) {
                         $this->db->set('flr_id', $folder);
                     }
                 }
                 $this->db->set('sub_priority', $this->input->post('priority'));
                 $this->db->set('sub_direction', $this->input->post('direction'));
                 $this->db->set('sub_datecreated', date('Y-m-d H:i:s'));
                 $this->db->insert('subscriptions');
                 $sub_id = $this->db->insert_id();
             } else {
                 $sub_id = $fed->sub_id;
             }
             $data['sub_id'] = $sub_id;
             $data['fed_title'] = $fed->fed_title;
         }
         if ($data['error']) {
             $content = $this->load->view('subscriptions_create', $data, TRUE);
         } else {
             redirect(base_url() . 'subscriptions/read/' . $sub_id);
         }
     }
     $this->readerself_library->set_content($content);
 }