/** * Add configuration data * * @param string $name Variable name * @param string $property Variable property name * @param string $value Variable value * @param string $module Module name * @param string $reg_exp Variable value restriction * @param string $doamin_id Doamin restriction * * @return boolean True on success, false on fail */ function add_conf($name, $property, $value, $module = '', $reg_exp = '', $domain_id = 1) { require_once VIVVO_FS_FRAMEWORK . 'vivvo_preference.php'; if (!$this->_post_master) { $this->set__post_master(); } $config = new preferences(); $config->set_variable_name($name); $config->set_variable_property($property); $config->set_variable_value($value); $config->set_module($module); $config->set_reg_exp($reg_exp); $config->set_domain_id($domain_id); $this->_post_master->set_data_object($config); $result = $this->_post_master->sql_insert(); vivvo_cache::get_instance()->delete('configuration'); return $result; }
/** * Add/edit cron job * * @param array|string $time_mask Unix crontab time mask/array @see vivvo_cron_manager::create_time_mask * @param string $file File containg the script * @param string $class Class name * @param string $method Method/function name * @param array $arguments Arguments for cron function */ function cron_job($time_mask, $file, $class, $method, $arguments = array()) { if (is_array($time_mask)) { $time_mask = $this->create_time_mask($time_mask); } $cron_list = new vivvo_cron_list(); $cron_job = $cron_list->get_cron_job_by_hash(md5($file . $class . $method . serialize($arguments))); if ($cron_job) { $pm = new vivvo_post_master(); $cron_job->set_time_mask($time_mask); $cron_job->set_nextrun(0); $pm->set_data_object($cron_job); $pm->sql_update(); } else { $pm = new vivvo_post_master(); $cron_job = new vivvo_cron(); $cron_job->set_time_mask($time_mask); $cron_job->set_file($file); $cron_job->set_class($class); $cron_job->set_method($method); $cron_job->set_arguments(serialize($arguments)); $cron_job->set_hash(md5($file . $class . $method . serialize($arguments))); $pm->set_data_object($cron_job); $pm->sql_insert(); } }
function register_url($url, $file, $url_hanlder, $content_handler = '') { if (!preg_match('/[^a-zA-Z\\d\\-_\\.%~]/i', $url) && !vivvo_lite_site::get_instance()->is_registered_url($url) && !empty($file) && !empty($url_hanlder)) { require_once VIVVO_FS_FRAMEWORK . 'vivvo_post.php'; require_once VIVVO_FS_FRAMEWORK . 'vivvo_preference.php'; $config = new preferences(); $pm = new vivvo_post_master(); $config->set_variable_name($url); $config->set_variable_property('file'); $config->set_variable_value($file); $config->set_module('url_modules'); $config->set_domain_id(1); $pm->set_data_object($config); $pm->sql_insert(); $config->set_variable_name($url); $config->set_variable_property('url_handler_function'); $config->set_variable_value($url_hanlder); $config->set_module('url_modules'); $config->set_domain_id(1); $pm->set_data_object($config); $pm->sql_insert(); if (!empty($content_handler)) { $config->set_variable_name($url); $config->set_variable_property('content_handler_function'); $config->set_variable_value($content_handler); $config->set_module('url_modules'); $config->set_domain_id(1); $pm->set_data_object($config); $pm->sql_insert(); } return true; } return false; }
/** * Insert all objects from list to databas * * @param vivvo_post_master $post_master * * @return bool succes */ function sql_insert_list($post_master) { $result = true; if (!empty($this->list)) { foreach ($this->list as $k => $v) { $result = $result && $post_master->sql_insert($this->list[$k]); } } return $result; }
/** * Saves auto-draft of an article * * @param int $article_id * @param string $title * @param string $abstract * @param string $body * @param string $time * @param int $user * @return ArticlesRevisions|false */ public static function save_auto_draft($article_id, $title, $abstract, $body, $time, $user) { $sm = vivvo_lite_site::get_instance(); // remove all other auto-drafts for this arrticle (if any), as there should be only one $sm->get_db()->exec('DELETE FROM ' . VIVVO_DB_PREFIX . "articles_revisions\n\t\t\t\t WHERE article_id = {$article_id} AND type = 1"); $revision = new ArticlesRevisions($sm, array('article_id' => $article_id, 'version' => 0, 'title' => $title, 'abstract' => $abstract, 'body' => $body, 'created_time' => $time, 'creator_id' => $user, 'type' => 1)); class_exists('vivvo_post_master') or (require VIVVO_FS_FRAMEWORK . 'vivvo_post.php'); $post_master = new vivvo_post_master($sm); $post_master->set_data_object($revision); if ($post_master->sql_insert()) { return $revision; } return false; }
function get_articles() { $sm =& $this->_site_manager; require_once VIVVO_FS_INSTALL_ROOT . '/lib/simplepie/simplepie.php'; $feed_r = new SimplePie(); $feed_r->enable_cache(false); $feed_r->set_feed_url($this->get_feed()); $feed_r->init(); foreach ($feed_r->get_items() as $item) { $status = true; $guid = $item->get_item_tags('', 'guid'); $guid = $guid[0]['data']; if ($guid != '') { $sql = 'SELECT count( * ) as count FROM ' . VIVVO_DB_PREFIX . 'Articles WHERE feed_data = \'' . md5($guid) . '\' LIMIT 1'; $feed_data = md5($guid); } else { $sql = 'SELECT count( * ) as count FROM ' . VIVVO_DB_PREFIX . 'Articles WHERE feed_data = \'' . md5($item->get_title() . $item->get_permalink()) . '\'LIMIT 1'; $feed_data = md5($item->get_title() . $item->get_permalink()); } $res =& $sm->_db->query($sql); $row = $res->fetchRow(MDB2_FETCHMODE_ASSOC); if ($row['count']) { $status = false; } if ($status) { require_once VIVVO_FS_INSTALL_ROOT . '/lib/vivvo/core/Articles.class.php'; require_once VIVVO_FS_FRAMEWORK . 'vivvo_post.php'; $article = new Articles($sm); if ($this->get_author() != '') { $author = $this->get_author(); } elseif ($item->get_author() != '') { $author = $item->get_author(); } else { preg_match('/^http:\\/\\/(www\\.)?([^\\/]+)/', $item->get_permalink(), $author); $author = $author[2]; } if (VIVVO_PLUGIN_FEED_IMPORTER_FRIENDLY == 1) { $sefriendly_url = strtolower(preg_replace('/[^a-zA-Z\\d\\-]/i', '_', $item->get_title())); $keywords = preg_split("/[\\s_]+/", $sefriendly_url, VIVVO_PLUGIN_FEED_IMPORTER_MAX_WORD_NUM + 1, PREG_SPLIT_NO_EMPTY); $output_string = ''; for ($i = 0; $i < VIVVO_PLUGIN_FEED_IMPORTER_MAX_WORD_NUM; $i++) { $output_string .= '_' . $keywords[$i]; } $sefriendly_url = trim($output_string, "_"); //If sefriendly exists $sql = 'SELECT count( * ) as count FROM ' . VIVVO_DB_PREFIX . 'Articles WHERE sefriendly = \'' . secure_sql($sefriendly_url) . '\' LIMIT 1'; $res =& $sm->_db->query($sql); $row = $res->fetchRow(MDB2_FETCHMODE_ASSOC); if ($row['count']) { $sefriendly_url = ''; } } else { $sefriendly_url = ''; } $sql = 'SELECT max( order_num ) as max FROM ' . VIVVO_DB_PREFIX . 'Articles'; $res =& $sm->_db->query($sql); if ($row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) { $order_num = $row['max'] + 1; } else { $order_num = 1; } $data = array('category_id' => $this->category_id, 'user_id' => VIVVO_PLUGIN_FEED_IMPORTER_USER_ID, 'author' => $author, 'title' => $item->get_title(), 'created' => $item->get_date('Y-m-d H:i:s'), 'body' => $item->get_description(), 'status' => VIVVO_PLUGIN_FEED_IMPORTER_STATUS, 'sefriendly' => $sefriendly_url, 'link' => $item->get_permalink(), 'order_num' => $order_num, 'feed_data' => $feed_data); $article->populate($data, true); $post_master = new vivvo_post_master($sm); $post_master->set_data_object($article); if ($post_master->sql_insert()) { $this->set_count($this->get_count() + 1); } } } $this->set_favicon($feed_r->get_favicon()); }
/** * Imports articles from feed * * @return array Number of imported (index: 0) and updated (index: 1) articles */ public function import_articles() { require_once VIVVO_FS_INSTALL_ROOT . 'lib/simplepie/simplepie.php'; require_once VIVVO_FS_INSTALL_ROOT . 'lib/vivvo/core/Articles.class.php'; require_once VIVVO_FS_FRAMEWORK . 'vivvo_post.php'; $sm = vivvo_lite_site::get_instance(); $db = $sm->get_db(); $post_master = new vivvo_post_master($sm); $simplepie = new SimplePie(); $simplepie->enable_cache(false); $simplepie->set_feed_url($feed_url = $this->get_feed()); $simplepie->enable_order_by_date(true); @$simplepie->init(); if ($simplepie->error()) { return array(0, 0); } $now = date('Y-m-d H:i:00', $now_ts = time()); $count_added = 0; $count_updated = 0; $imported = array(); if (VIVVO_PLUGIN_FEED_IMPORTER_AUTO_DELETE) { $auto_delete_ts = VIVVO_PLUGIN_FEED_IMPORTER_AUTO_DELETE * 86400; } else { $auto_delete_ts = false; } if (VIVVO_PLUGIN_FEED_IMPORTER_AUTO_ARCHIVE) { $auto_archive_ts = VIVVO_PLUGIN_FEED_IMPORTER_AUTO_ARCHIVE * 86400; } else { $auto_archive_ts = false; } foreach ($simplepie->get_items() as $item) { if (($item_datetime = $item->get_date('Y-m-d H:i:00')) != null) { $item_datetime_ts = strtotime($item_datetime); // make sure not to import articles which should be deleted or archived if ($auto_delete_ts and $now_ts - $item_datetime_ts > $auto_delete_ts or $auto_archive_ts and $now_ts - $item_datetime_ts > $auto_archive_ts) { continue; } } $guid = $item->get_item_tags('', 'guid'); $guid = $guid[0]['data']; if (!$guid and !($guid = $item->get_title() . $item->get_permalink())) { continue; // can't determine reliable unique identifier } $feed_item_id = md5($feed_url . $guid); if (in_array($feed_item_id, $imported)) { continue; // already imported this one, feed has duplicate items? } $res = $db->query('SELECT id, created FROM ' . VIVVO_DB_PREFIX . "articles WHERE feed_item_id = '{$feed_item_id}' LIMIT 1"); if (PEAR::isError($res)) { continue; } $update = false; if ($res->numRows() and $row = $res->fetchRow(MDB2_FETCHMODE_ASSOC)) { if (VIVVO_PLUGIN_FEED_IMPORTER_UPDATE_ARTICLES and $item_datetime != null and time($row['created']) < $item_datetime_ts) { $update = true; } else { $res->free(); continue; // timestamp not changed consider content is the same too... } } $res->free(); $imported[] = $feed_item_id; if (!($author = $this->get_author()) and !($author = $item->get_author())) { if (preg_match('/^[^:]+:\\/\\/(www\\.)?([^\\/]+)/', $item->get_permalink(), $author)) { $author = $author[2]; } else { $author = ''; } } $article = new Articles($sm, array('category_id' => $this->category_id, 'user_id' => VIVVO_PLUGIN_FEED_IMPORTER_USER_ID, 'author' => $author, 'title' => $title = $item->get_title(), 'created' => $item_datetime ? $item_datetime : $now, 'body' => $item->get_description(), 'status' => VIVVO_PLUGIN_FEED_IMPORTER_STATUS, 'sefriendly' => make_sefriendly($title), 'link' => $item->get_permalink(), 'show_comment' => VIVVO_PLUGIN_FEED_IMPORTER_SHOW_COMMENT, 'feed_item_id' => $feed_item_id)); $post_master->set_data_object($article); if ($update) { $article->set_id($row['id']); $post_master->sql_update() and $count_updated++; } elseif ($post_master->sql_insert()) { $count_added++; } } $this->set_favicon($simplepie->get_favicon()); $this->set_count($this->get_count() + $count_added); if (VIVVO_PLUGIN_FEED_IMPORTER_USE_LOGO and $this->get_category() and $image_url = $simplepie->get_image_url() and preg_replace('/_\\d+(\\.[^.]+)$/', '$1', $this->category->get_image()) != ($basename = basename($image_url))) { class_exists('HTTP_Request2') or (require VIVVO_FS_INSTALL_ROOT . 'lib/vivvo/framework/PEAR/HTTP/Request2.php'); try { $request = new HTTP_Request2($image_url); $response = $request->send(); if ($response->getStatus() == 200) { $file_contents = $response->getBody(); $basename = $sm->get_file_manager()->random_file_name($basename); file_put_contents(VIVVO_FS_INSTALL_ROOT . VIVVO_FS_FILES_DIR . $basename, $file_contents); $this->category->set_image($basename); $post_master->set_data_object($this->category); $post_master->sql_update(); } } catch (Exception $e) { if (defined('VIVVO_CRONJOB_MODE')) { echo 'exception: ' . $e->getMessage() . PHP_EOL; } } } return array($count_added, $count_updated); }