Ejemplo n.º 1
0
function put_item_in_cache(&$item, $update = false)
{
    if ($item["rendered-hash"] != hash("md5", $item["body"]) or $item["rendered-hash"] == "" or $item["rendered-html"] == "" or get_config("system", "ignore_cache")) {
        // The function "redir_private_images" changes the body.
        // I'm not sure if we should store it permanently, so we save the old value.
        $body = $item["body"];
        $a = get_app();
        redir_private_images($a, $item);
        $item["rendered-html"] = prepare_text($item["body"]);
        $item["rendered-hash"] = hash("md5", $item["body"]);
        $item["body"] = $body;
        if ($update and $item["id"] != 0) {
            q("UPDATE `item` SET `rendered-html` = '%s', `rendered-hash` = '%s' WHERE `id` = %d", dbesc($item["rendered-html"]), dbesc($item["rendered-hash"]), intval($item["id"]));
        }
    }
}
Ejemplo n.º 2
0
 /**
  * Given an item array, convert the body element from bbcode to html and add smilie icons.
  * If attach is true, also add icons for item attachments
  *
  * @param array $item
  * @param boolean $attach
  * @return string item body html
  * @hook prepare_body_init item array before any work
  * @hook prepare_body ('item'=>item array, 'html'=>body string) after first bbcode to html
  * @hook prepare_body_final ('item'=>item array, 'html'=>body string) after attach icons and blockquote special case handling (spoiler, author)
  */
 function prepare_body(&$item, $attach = false, $preview = false)
 {
     $a = get_app();
     call_hooks('prepare_body_init', $item);
     $searchpath = $a->get_baseurl() . "/search?tag=";
     $tags = array();
     $hashtags = array();
     $mentions = array();
     if (!get_config('system', 'suppress_tags')) {
         $taglist = q("SELECT `type`, `term`, `url` FROM `term` WHERE `otype` = %d AND `oid` = %d AND `type` IN (%d, %d) ORDER BY `tid`", intval(TERM_OBJ_POST), intval($item['id']), intval(TERM_HASHTAG), intval(TERM_MENTION));
         foreach ($taglist as $tag) {
             if ($tag["url"] == "") {
                 $tag["url"] = $searchpath . strtolower($tag["term"]);
             }
             if ($tag["type"] == TERM_HASHTAG) {
                 $hashtags[] = "#<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
                 $prefix = "#";
             } elseif ($tag["type"] == TERM_MENTION) {
                 $mentions[] = "@<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
                 $prefix = "@";
             }
             $tags[] = $prefix . "<a href=\"" . $tag["url"] . "\" target=\"_blank\">" . $tag["term"] . "</a>";
         }
     }
     $item['tags'] = $tags;
     $item['hashtags'] = $hashtags;
     $item['mentions'] = $mentions;
     //$cachefile = get_cachefile($item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']));
     $cachefile = get_cachefile($item["guid"] . "-" . hash("md5", $item['body']));
     if ($cachefile != '') {
         if (file_exists($cachefile)) {
             $stamp1 = microtime(true);
             $s = file_get_contents($cachefile);
             $a->save_timestamp($stamp1, "file");
         } else {
             redir_private_images($a, $item);
             $s = prepare_text($item['body']);
             $stamp1 = microtime(true);
             file_put_contents($cachefile, $s);
             $a->save_timestamp($stamp1, "file");
             logger('prepare_body: put item ' . $item["id"] . ' into cachefile ' . $cachefile);
         }
     } else {
         redir_private_images($a, $item);
         $s = prepare_text($item['body']);
     }
     require_once "mod/proxy.php";
     $s = proxy_parse_html($s);
     $prep_arr = array('item' => $item, 'html' => $s, 'preview' => $preview);
     call_hooks('prepare_body', $prep_arr);
     $s = $prep_arr['html'];
     if (!$attach) {
         // Replace the blockquotes with quotes that are used in mails
         $mailquote = '<blockquote type="cite" class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">';
         $s = str_replace(array('<blockquote>', '<blockquote class="spoiler">', '<blockquote class="author">'), array($mailquote, $mailquote, $mailquote), $s);
         return $s;
     }
     $as = '';
     $vhead = false;
     $arr = explode('[/attach],', $item['attach']);
     if (count($arr)) {
         $as .= '<div class="body-attach">';
         foreach ($arr as $r) {
             $matches = false;
             $icon = '';
             $cnt = preg_match_all('|\\[attach\\]href=\\"(.*?)\\" length=\\"(.*?)\\" type=\\"(.*?)\\" title=\\"(.*?)\\"|', $r, $matches, PREG_SET_ORDER);
             if ($cnt) {
                 foreach ($matches as $mtch) {
                     $mime = $mtch[3];
                     if (local_user() == $item['uid'] && $item['contact-id'] != $a->contact['id'] && $item['network'] == NETWORK_DFRN) {
                         $the_url = $a->get_baseurl() . '/redir/' . $item['contact-id'] . '?f=1&url=' . $mtch[1];
                     } else {
                         $the_url = $mtch[1];
                     }
                     if (strpos($mime, 'video') !== false) {
                         if (!$vhead) {
                             $vhead = true;
                             $a->page['htmlhead'] .= replace_macros(get_markup_template('videos_head.tpl'), array('$baseurl' => $a->get_baseurl()));
                             $a->page['end'] .= replace_macros(get_markup_template('videos_end.tpl'), array('$baseurl' => $a->get_baseurl()));
                         }
                         $id = end(explode('/', $the_url));
                         $as .= replace_macros(get_markup_template('video_top.tpl'), array('$video' => array('id' => $id, 'title' => t('View Video'), 'src' => $the_url, 'mime' => $mime)));
                     }
                     $filetype = strtolower(substr($mime, 0, strpos($mime, '/')));
                     if ($filetype) {
                         $filesubtype = strtolower(substr($mime, strpos($mime, '/') + 1));
                         $filesubtype = str_replace('.', '-', $filesubtype);
                     } else {
                         $filetype = 'unkn';
                         $filesubtype = 'unkn';
                     }
                     $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
                     /*$icontype = strtolower(substr($mtch[3],0,strpos($mtch[3],'/')));
                     		switch($icontype) {
                     			case 'video':
                     			case 'audio':
                     			case 'image':
                     			case 'text':
                     				$icon = '<div class="attachtype icon s22 type-' . $icontype . '"></div>';
                     				break;
                     			default:
                     				$icon = '<div class="attachtype icon s22 type-unkn"></div>';
                     				break;
                     		}*/
                     $title = strlen(trim($mtch[4])) ? escape_tags(trim($mtch[4])) : escape_tags($mtch[1]);
                     $title .= ' ' . $mtch[2] . ' ' . t('bytes');
                     $as .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="_blank" >' . $icon . '</a>';
                 }
             }
         }
         $as .= '<div class="clear"></div></div>';
     }
     $s = $s . $as;
     // Look for spoiler
     $spoilersearch = '<blockquote class="spoiler">';
     // Remove line breaks before the spoiler
     while (strpos($s, "\n" . $spoilersearch) !== false) {
         $s = str_replace("\n" . $spoilersearch, $spoilersearch, $s);
     }
     while (strpos($s, "<br />" . $spoilersearch) !== false) {
         $s = str_replace("<br />" . $spoilersearch, $spoilersearch, $s);
     }
     while (strpos($s, $spoilersearch) !== false) {
         $pos = strpos($s, $spoilersearch);
         $rnd = random_string(8);
         $spoilerreplace = '<br /> <span id="spoiler-wrap-' . $rnd . '" style="white-space:nowrap;" class="fakelink" onclick="openClose(\'spoiler-' . $rnd . '\');">' . sprintf(t('Click to open/close')) . '</span>' . '<blockquote class="spoiler" id="spoiler-' . $rnd . '" style="display: none;">';
         $s = substr($s, 0, $pos) . $spoilerreplace . substr($s, $pos + strlen($spoilersearch));
     }
     // Look for quote with author
     $authorsearch = '<blockquote class="author">';
     while (strpos($s, $authorsearch) !== false) {
         $pos = strpos($s, $authorsearch);
         $rnd = random_string(8);
         $authorreplace = '<br /> <span id="author-wrap-' . $rnd . '" style="white-space:nowrap;" class="fakelink" onclick="openClose(\'author-' . $rnd . '\');">' . sprintf(t('Click to open/close')) . '</span>' . '<blockquote class="author" id="author-' . $rnd . '" style="display: block;">';
         $s = substr($s, 0, $pos) . $authorreplace . substr($s, $pos + strlen($authorsearch));
     }
     // replace friendica image url size with theme preference
     if (x($a->theme_info, 'item_image_size')) {
         $ps = $a->theme_info['item_image_size'];
         $s = preg_replace('|(<img[^>]+src="[^"]+/photo/[0-9a-f]+)-[0-9]|', "\$1-" . $ps, $s);
     }
     $prep_arr = array('item' => $item, 'html' => $s);
     call_hooks('prepare_body_final', $prep_arr);
     return $prep_arr['html'];
 }
Ejemplo n.º 3
0
 function prepare_body($item, $attach = false)
 {
     $a = get_app();
     call_hooks('prepare_body_init', $item);
     //$cachefile = get_cachefile($item["guid"]."-".strtotime($item["edited"])."-".hash("crc32", $item['body']));
     $cachefile = get_cachefile($item["guid"] . "-" . hash("md5", $item['body']));
     if ($cachefile != '') {
         if (file_exists($cachefile)) {
             $stamp1 = microtime(true);
             $s = file_get_contents($cachefile);
             $a->save_timestamp($stamp1, "file");
         } else {
             redir_private_images($a, $item);
             $s = prepare_text($item['body']);
             $stamp1 = microtime(true);
             file_put_contents($cachefile, $s);
             $a->save_timestamp($stamp1, "file");
             logger('prepare_body: put item ' . $item["id"] . ' into cachefile ' . $cachefile);
         }
     } else {
         redir_private_images($a, $item);
         $s = prepare_text($item['body']);
     }
     $prep_arr = array('item' => $item, 'html' => $s);
     call_hooks('prepare_body', $prep_arr);
     $s = $prep_arr['html'];
     if (!$attach) {
         // Replace the blockquotes with quotes that are used in mails
         $mailquote = '<blockquote type="cite" class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">';
         $s = str_replace(array('<blockquote>', '<blockquote class="spoiler">', '<blockquote class="author">'), array($mailquote, $mailquote, $mailquote), $s);
         return $s;
     }
     $arr = explode('[/attach],', $item['attach']);
     if (count($arr)) {
         $s .= '<div class="body-attach">';
         foreach ($arr as $r) {
             $matches = false;
             $icon = '';
             $cnt = preg_match_all('|\\[attach\\]href=\\"(.*?)\\" length=\\"(.*?)\\" type=\\"(.*?)\\" title=\\"(.*?)\\"|', $r, $matches, PREG_SET_ORDER);
             if ($cnt) {
                 foreach ($matches as $mtch) {
                     $filetype = strtolower(substr($mtch[3], 0, strpos($mtch[3], '/')));
                     if ($filetype) {
                         $filesubtype = strtolower(substr($mtch[3], strpos($mtch[3], '/') + 1));
                         $filesubtype = str_replace('.', '-', $filesubtype);
                     } else {
                         $filetype = 'unkn';
                         $filesubtype = 'unkn';
                     }
                     $icon = '<div class="attachtype icon s22 type-' . $filetype . ' subtype-' . $filesubtype . '"></div>';
                     /*$icontype = strtolower(substr($mtch[3],0,strpos($mtch[3],'/')));
                     		switch($icontype) {
                     			case 'video':
                     			case 'audio':
                     			case 'image':
                     			case 'text':
                     				$icon = '<div class="attachtype icon s22 type-' . $icontype . '"></div>';
                     				break;
                     			default:
                     				$icon = '<div class="attachtype icon s22 type-unkn"></div>';
                     				break;
                     		}*/
                     $title = strlen(trim($mtch[4])) ? escape_tags(trim($mtch[4])) : escape_tags($mtch[1]);
                     $title .= ' ' . $mtch[2] . ' ' . t('bytes');
                     if (local_user() == $item['uid'] && $item['contact-id'] != $a->contact['id'] && $item['network'] == NETWORK_DFRN) {
                         $the_url = $a->get_baseurl() . '/redir/' . $item['contact-id'] . '?f=1&url=' . $mtch[1];
                     } else {
                         $the_url = $mtch[1];
                     }
                     $s .= '<a href="' . strip_tags($the_url) . '" title="' . $title . '" class="attachlink" target="external-link" >' . $icon . '</a>';
                 }
             }
         }
         $s .= '<div class="clear"></div></div>';
     }
     // Look for spoiler
     $spoilersearch = '<blockquote class="spoiler">';
     // Remove line breaks before the spoiler
     while (strpos($s, "\n" . $spoilersearch) !== false) {
         $s = str_replace("\n" . $spoilersearch, $spoilersearch, $s);
     }
     while (strpos($s, "<br />" . $spoilersearch) !== false) {
         $s = str_replace("<br />" . $spoilersearch, $spoilersearch, $s);
     }
     while (strpos($s, $spoilersearch) !== false) {
         $pos = strpos($s, $spoilersearch);
         $rnd = random_string(8);
         $spoilerreplace = '<br /> <span id="spoiler-wrap-' . $rnd . '" style="white-space:nowrap;" class="fakelink" onclick="openClose(\'spoiler-' . $rnd . '\');">' . sprintf(t('Click to open/close')) . '</span>' . '<blockquote class="spoiler" id="spoiler-' . $rnd . '" style="display: none;">';
         $s = substr($s, 0, $pos) . $spoilerreplace . substr($s, $pos + strlen($spoilersearch));
     }
     // Look for quote with author
     $authorsearch = '<blockquote class="author">';
     while (strpos($s, $authorsearch) !== false) {
         $pos = strpos($s, $authorsearch);
         $rnd = random_string(8);
         $authorreplace = '<br /> <span id="author-wrap-' . $rnd . '" style="white-space:nowrap;" class="fakelink" onclick="openClose(\'author-' . $rnd . '\');">' . sprintf(t('Click to open/close')) . '</span>' . '<blockquote class="author" id="author-' . $rnd . '" style="display: block;">';
         $s = substr($s, 0, $pos) . $authorreplace . substr($s, $pos + strlen($authorsearch));
     }
     $prep_arr = array('item' => $item, 'html' => $s);
     call_hooks('prepare_body_final', $prep_arr);
     return $prep_arr['html'];
 }