/** * Tries to add a favatar or pavatar (depending on the given mode) to the comment. * * @param array eventdata the data given by the event * @param int cache hours for fetching images from cache * @param string mode has to be 'P' for Pavatar or 'F' for Favatar loading. * * @return boolean true, if Avatar was found and added to the comment buffer */ function fetchPFavatar(&$eventData, $mode = "F") { require_once S9Y_PEAR_PATH . 'HTTP/Request.php'; global $serendipity; $default = $this->getDefaultImageConfiguration(); $url = $eventData['url']; if (empty($url)) { return false; } $favicon = false; $this->log($mode . " - Trying to fetch for {$url}"); // Try to get the URL $parts = @parse_url($url); if (!is_array($parts)) { return false; } $ip = @gethostbyname($parts['host']); if (!$ip || $ip == $parts['host']) { return false; } $this->log($mode . " - URL ok."); $cache_file = $this->getCacheFilePath($eventData); // Load icon url detected in last run if (isset($this->avatarConfiguration['img_url_' . $mode])) { $favicon = $this->avatarConfiguration['img_url_' . $mode]; $this->log($mode . " - using last run url: {$favicon}"); } if ($favicon === false) { // use optimization for localhost $islocalhost = $_SERVER['HTTP_HOST'] == $parts['host']; if (function_exists('serendipity_request_start')) { serendipity_request_start(); } // Evaluate URL of P/Favatar $req = new HTTP_Request($url, array('allowRedirects' => true, 'maxRedirects' => 3)); $favicon = false; // code 200: OK, code 30x: REDIRECTION $responses = "/(200 OK)|(30[0-9] Found)/"; // |(30[0-9] Moved) if (!$islocalhost && (PEAR::isError($req->sendRequest()) || preg_match($responses, $req->getResponseCode()))) { // nothing to do, $favicon = false; $this->log($mode . " - Error fetching {$url}: " . $req->getResponseCode()); } else { $pavatarHeaderIcon = $req->getResponseHeader("X-Pavatar"); $fContent = $req->getResponseBody(); if ($mode == 'P' && !empty($pavatarHeaderIcon)) { $faviconURL = $pavatarHeaderIcon; $this->log("Found x-pavatar in head: {$faviconURL}"); } else { if (!$islocalhost && ($mode == 'P' && preg_match('/<link[^>]+rel="pavatar"[^>]+?href="([^"]+?)"/si', $fContent, $matches)) || $mode == 'F' && preg_match('/<link[^>]+rel="(?:shortcut )?icon"[^>]+?href="([^"]+?)"/si', $fContent, $matches)) { // Attempt to grab an avatar link from their webpage url $linkUrl = function_exists('serendipity_entity_decode') ? serendipity_entity_decode($matches[1]) : html_entity_decode($matches[1], ENT_COMPAT, LANG_CHARSET); if (substr($linkUrl, 0, 1) == '/') { if ($urlParts = parse_url($url)) { $faviconURL = $urlParts['scheme'] . '://' . $urlParts['host'] . $linkUrl; } } else { if (substr($linkUrl, 0, 7) == 'http://' || substr($linkUrl, 0, 8) == 'https://') { $faviconURL = $linkUrl; } else { if (substr($url, -1, 1) == '/') { $faviconURL = $url . $linkUrl; } else { $faviconURL = $url . '/' . $linkUrl; } } } $this->log($mode . " - Found link rel to url {$faviconURL}"); } else { // If unsuccessful, attempt to "guess" the favicon location $urlParts = parse_url($url); $faviconURL = $urlParts['scheme'] . '://' . $urlParts['host'] . ($mode == 'F' ? '/favicon.ico' : '/pavatar.png'); $this->log($mode . " - Not found link rel, guessing {$faviconURL}"); } } // Split image URL and check if image is available using a fast and timed out socket: $url_parts = @parse_url($faviconURL); if (!is_array($url_parts)) { $url_parts = array(); } if (!empty($url_parts['path'])) { $documentpath = $url_parts['path']; } else { $documentpath = '/'; } if (!empty($url_parts['query'])) { $documentpath .= '?' . $url_parts["query"]; } if (empty($url_parts['port'])) { $url_parts['port'] = '80'; } if (!empty($url_parts['host'])) { $socket = @fsockopen($url_parts['host'], $url_parts['port'], $errno, $errstr, 30); if ($socket) { fwrite($socket, "HEAD " . $documentpath . " HTTP/1.0\r\nHost: {$url_parts['host']}\r\n\r\n"); $http_response = fgets($socket, 25); $this->log($mode . ' Testing server ' . $url_parts['host'] . " dopath: {$documentpath} - HEAD Response: {$http_response}"); if (preg_match($responses, $http_response)) { $favicon = $faviconURL; } fclose($socket); } } // Remember the last result of the P/Favatar search $this->avatarConfiguration['img_url_' . $mode] = $favicon; } if (function_exists('serendipity_request_end')) { serendipity_request_end(); } } // if favicon url not loaded from cache if (!empty($favicon)) { $this->log($mode . " - found at: {$favicon}"); return $this->saveAndResponseAvatar($eventData, $favicon); } else { return false; } }
function import() { global $serendipity; // Save this so we can return it to its original value at the end of this method. $noautodiscovery = isset($serendipity['noautodiscovery']) ? $serendipity['noautodiscovery'] : false; if ($this->data['autodiscovery'] == 'false') { $serendipity['noautodiscovery'] = 1; } $this->getTransTable(); $this->data['prefix'] = serendipity_db_escape_string($this->data['prefix']); $users = array(); $entries = array(); if (!extension_loaded('mysqli')) { return MYSQL_REQUIRED; } $gdb = @mysqli_connect($this->data['host'], $this->data['user'], $this->data['pass']); if (!$gdb || mysqli_connect_error()) { return sprintf(COULDNT_CONNECT, serendipity_specialchars($this->data['host'])); } if (!@mysqli_select_db($gdb, $this->data['name'])) { return sprintf(COULDNT_SELECT_DB, mysqli_error($gdb)); } /* Users */ $res = @$this->nativeQuery("SELECT ID_MEMBER AS ID,\r\n memberName AS user_login,\r\n passwd AS user_pass,\r\n emailAddress AS user_email,\r\n ID_GROUP AS user_level\r\n FROM {$this->data['prefix']}members\r\n WHERE is_activated = 1", $gdb); if (!$res) { return sprintf(COULDNT_SELECT_USER_INFO, mysqli_error($gdb)); } for ($x = 0, $max_x = mysqli_num_rows($res); $x < $max_x; $x++) { $users[$x] = mysqli_fetch_assoc($res); $data = array('right_publish' => 1, 'realname' => $users[$x]['user_login'], 'username' => $users[$x]['user_login'], 'email' => $users[$x]['user_email'], 'userlevel' => $users[$x]['user_level'] == 1 ? USERLEVEL_ADMIN : USERLEVEL_EDITO, 'password' => $users[$x]['user_pass']); // MD5 compatible if ($serendipity['serendipityUserlevel'] < $data['userlevel']) { $data['userlevel'] = $serendipity['serendipityUserlevel']; } serendipity_db_insert('authors', $this->strtrRecursive($data)); echo mysqli_error(); $users[$x]['authorid'] = serendipity_db_insert_id('authors', 'authorid'); } /* Categories */ $res = @$this->nativeQuery("SELECT ID_CAT AS cat_ID,\r\n name AS cat_name\r\n FROM {$this->data['prefix']}categories", $gdb); if (!$res) { return sprintf(COULDNT_SELECT_CATEGORY_INFO, mysqli_error($gdb)); } // Get all the info we need for ($x = 0, $max_x = mysqli_num_rows($res); $x < $max_x; $x++) { $parent_categories[] = mysqli_fetch_assoc($res); } for ($x = 0, $max_x = sizeof($parent_categories); $x < $max_x; $x++) { $cat = array('category_name' => $parent_categories[$x]['cat_name'], 'category_description' => '', 'parentid' => 0, 'category_left' => 0, 'category_right' => 0); serendipity_db_insert('category', $this->strtrRecursive($cat)); $parent_categories[$x]['categoryid'] = serendipity_db_insert_id('category', 'categoryid'); } /* Categories */ $res = @$this->nativeQuery("SELECT ID_BOARD AS cat_ID,\r\n ID_CAT AS parent_cat_id,\r\n name AS cat_name,\r\n description AS category_description\r\n FROM {$this->data['prefix']}boards ORDER BY boardOrder;", $gdb); if (!$res) { return sprintf(COULDNT_SELECT_CATEGORY_INFO, mysqli_error($gdb)); } // Get all the info we need for ($x = 0, $max_x = mysqli_num_rows($res); $x < $max_x; $x++) { $categories[] = mysqli_fetch_assoc($res); } // Insert all categories as top level (we need to know everyone's ID before we can represent the hierarchy). for ($x = 0, $max_x = sizeof($categories); $x < $max_x; $x++) { $pcatid = 0; foreach ($parent_categories as $pcat) { if ($pcat['cat_ID'] == $categories[$x]['parent_cat_id']) { $pcatid = $pcat['cat_ID']; break; } } $cat = array('category_name' => $categories[$x]['cat_name'], 'category_description' => $categories[$x]['category_description'], 'parentid' => $pcatid, 'category_left' => 0, 'category_right' => 0); serendipity_db_insert('category', $this->strtrRecursive($cat)); $categories[$x]['categoryid'] = serendipity_db_insert_id('category', 'categoryid'); } serendipity_rebuildCategoryTree(); /* Entries */ $res = @$this->nativeQuery("SELECT\r\n\r\n tm.subject AS post_subject,\r\n t.ID_MEMBER_STARTED AS topic_poster,\r\n t.ID_BOARD AS forum_id,\r\n tm.posterTime AS post_time,\r\n tm.body AS post_text,\r\n t.ID_TOPIC AS topic_id,\r\n t.ID_FIRST_MSG AS post_id,\r\n t.numReplies AS ccount\r\n\r\n FROM {$this->data['prefix']}topics AS t\r\n JOIN {$this->data['prefix']}messages AS tm\r\n ON tm.ID_MSG = t.ID_FIRST_MSG\r\n\r\n GROUP BY t.ID_TOPIC", $gdb); if (!$res) { return sprintf(COULDNT_SELECT_ENTRY_INFO, mysqli_error($gdb)); } for ($x = 0, $max_x = mysqli_num_rows($res); $x < $max_x; $x++) { $entries[$x] = mysqli_fetch_assoc($res); $entry = array('title' => $this->decode($entries[$x]['post_subject']), 'isdraft' => 'false', 'allow_comments' => 'true', 'timestamp' => $entries[$x]['post_time'], 'body' => $this->strtr($entries[$x]['post_text']), 'extended' => ''); $entry['authorid'] = ''; $entry['author'] = ''; foreach ($users as $user) { if ($user['ID'] == $entries[$x]['topic_poster']) { $entry['authorid'] = $user['authorid']; $entry['author'] = $user['user_login']; break; } } if (!is_int($entries[$x]['entryid'] = serendipity_updertEntry($entry))) { return $entries[$x]['entryid']; } /* Entry/category */ foreach ($categories as $category) { if ($category['cat_ID'] == $entries[$x]['forum_id']) { $data = array('entryid' => $entries[$x]['entryid'], 'categoryid' => $category['categoryid']); serendipity_db_insert('entrycat', $this->strtrRecursive($data)); break; } } $topic_id = $entries[$x]['topic_id']; // Store original ID, we might need it at some point. serendipity_db_insert('entryproperties', array('entryid' => $entries[$x]['entryid'], 'property' => 'foreign_import_id', 'value' => $entries[$x]['topic_id'])); // Convert SMF tags $t_res = @$this->nativeQuery("SELECT t.tag\r\n FROM {$this->data['prefix']}tags_log AS tl\r\n JOIN {$this->data['prefix']}tags AS t\r\n ON tl.ID_TAG = t.ID_TAG\r\n WHERE tl.ID_TOPIC = {$topic_id}\r\n AND t.approved = 1", $gdb); if (mysqli_num_rows($t_res) > 0) { while ($a = mysqli_fetch_assoc($t_res)) { serendipity_db_insert('entrytags', array('entryid' => $entries[$x]['entryid'], 'tag' => $t_res['tag'])); } } /* Comments */ $c_res = @$this->nativeQuery("SELECT\r\n tm.subject AS post_subject,\r\n tm.body AS post_text,\r\n tm.ID_MSG AS post_id,\r\n tm.posterTime AS post_time,\r\n tm.ID_BOARD AS forum_id,\r\n tm.posterName AS poster_name,\r\n tm.posterEmail AS poster_email\r\n\r\n FROM {$this->data['prefix']}topics AS t\r\n JOIN {$this->data['prefix']}messages AS tm\r\n ON tm.ID_TOPIC = t.ID_TOPIC\r\n WHERE t.ID_TOPIC = {$topic_id}\r\n ", $gdb); if (!$c_res) { return sprintf(COULDNT_SELECT_COMMENT_INFO, mysqli_error($gdb)); } while ($a = mysqli_fetch_assoc($c_res)) { if ($a['post_id'] == $entries[$x]['post_id']) { continue; } $author = $a['poster_name']; $mail = $a['poster_email']; $url = ''; foreach ($users as $user) { if ($user['ID'] == $a['poster_id']) { $author = $user['user_login']; $mail = $user['user_email']; $url = $user['user_url']; break; } } $a['post_text'] = serendipity_entity_decode($a['post_text']); $comment = array('entry_id ' => $entries[$x]['entryid'], 'parent_id' => 0, 'timestamp' => $a['post_time'], 'author' => $author, 'email' => $mail, 'url' => $url, 'ip' => '', 'status' => 'approved', 'body' => $a['post_text'], 'subscribed' => 'false', 'type' => 'NORMAL'); serendipity_db_insert('comments', $this->strtrRecursive($comment)); $cid = serendipity_db_insert_id('comments', 'id'); serendipity_approveComment($cid, $entries[$x]['entryid'], true); } } $serendipity['noautodiscovery'] = $noautodiscovery; // That was fun. return true; }
/** * Strips any unneeded code from trackback / pingback bodies returning pure (UTF8) text. */ function trackback_body_strip($body) { // replace non breakable space with normal space: $body = str_replace(' ', ' ', $body); // strip html entities and tags. $body = serendipity_entity_decode(strip_tags($body), ENT_COMPAT, LANG_CHARSET); // replace whitespace with single space $body = preg_replace('@\\s+@s', ' ', $body); return $body; }
function clean_htmlspecialchars($given, $quote_style = ENT_QUOTES) { return '<' . $given[1] . $given[2] . $given[3] . '=' . $given[4] . serendipity_specialchars(serendipity_entity_decode($given[5], $quote_style), $quote_style) . $given[6]; }