/** * Create a new model. * IF creation is successful, the browser will be redirected to the 'view' page. */ public function actionAjaxCreate() { function cut_str($string, $sublen, $start = 0, $code = 'UTF-8') { if ($code == 'UTF-8') { $pa = "/[-]|[�-�][�-�]|�[�-�][�-�]|[�-�][�-�][�-�]|�[�-�][�-�][�-�]|[�-�][�-�][�-�][�-�]/"; preg_match_all($pa, $string, $t_string); if (count($t_string[0]) - $start > $sublen) { return join('', array_slice($t_string[0], $start, $sublen)) . ".."; } return join('', array_slice($t_string[0], $start, $sublen)); } else { $start = $start * 2; $sublen = $sublen * 2; $strlen = strlen($string); $tmpstr = ''; for ($i = 0; $i < $strlen; $i++) { if ($i >= $start && $i < $start + $sublen) { if (ord(substr($string, $i, 1)) > 129) { $tmpstr .= substr($string, $i, 2); } else { $tmpstr .= substr($string, $i, 1); } } if (ord(substr($string, $i, 1)) > 129) { $i++; } } if (strlen($tmpstr) < $strlen) { $tmpstr .= ".."; } return $tmpstr; } } function file_get_contents_utf8($fn) { $content = file_get_contents($fn); return mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content, 'big5, UTF-8, ISO-8859-1')); } function getMetaTitle($content) { //$pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui"; $pattern = "/<title>(.+)<\\/title>/siU"; /* $pattern = "<meta\s+name=['\"]??title['\"]??\s+content=['\"]??(.+)['\"]??\s*\/?>"; */ if (preg_match($pattern, $content, $match)) { return $match[1]; } else { return false; } } function getMetaDesc($content) { $pattern = "<meta\\s+name=['\"]??description['\"]??\\s+content=['\"]??(.+)['\"]??\\s*\\/?>"; // Remove it when done if (preg_match("/{$pattern}/siU", $content, $match)) { return $match[1]; } else { return false; } } $model = new Post(); // Check for valid. $this->performAjaxValidation($model); // Create Tag Model $tag_model = new Tag(); // Retrieve category list $category_list = array(); $category_list['category_id'] = CHtml::listData($this->portlets['category'], 'id', 'name'); // Need to speed up // Extract html content $urlData = array(); if (isset($_POST['url'])) { $url = $_POST['url']; $urlData['url'] = $url; // First phrase $content = @file_get_contents_utf8($url); $urlDataFlag = false; if (($urlData['title'] = getMetaTitle($content)) == false) { $urlDataFlag = true; } if (($urlData['description'] = getMetaDesc($content)) == false) { $urlDataFlag = true; } // $urlData['title'] = getMetaTitle($content); // $urlData['description'] = getMetaDesc($content); // Second phrase if ($urlDataFlag) { $boilerpipeUrl = "http://boilerpipe-web.appspot.com/extract?url={$url}&output=json"; $struHTML = json_decode(file_get_contents($boilerpipeUrl)); if ($struHTML->{'status'} === "success") { $urlData['title'] = $struHTML->{'response'}->{'title'}; $urlData['description'] = $struHTML->{'response'}->{'content'}; } // Handle error msg } // Cut str $urlData['title'] = cut_str($urlData['title'], 20, 0); $urlData['description'] = cut_str($urlData['description'], 80, 0); } $this->renderPartial('ajaxCreate', array('urlData' => $urlData, 'model' => $model, 'category_list' => $category_list, 'tag_model' => $tag_model), false, true); }
<?php echo META_CONTENT_KEY . "\n"; ?> <?php echo META_LINK_RSS . "\n"; ?> <?php echo META_LINK_ATOM . "\n"; ?> <?php echo META_SITEMAP . "\n"; ?> <!-- TITLE --> <title><?php echo getMetaTitle(); ?> </title> <!-- FAVICON --> <link rel="shortcut icon" type="image/x-icon" href="<?php echo LINK_FAVICON; ?> " /> <link rel="apple-touch-icon" href="<?php echo LINK_APPLE_ICON; ?> " /> <!-- BASE CSS -->