コード例 #1
0
 function metaWeblog_getPost($args)
 {
     global $DB;
     global $session;
     $out = array();
     list($post_id, $username, $password) = $args;
     $item = new item();
     $item->load(intval($post_id));
     $website_id = $item->website;
     $website = new website();
     $website->load($website_id);
     // check auth
     if (metaWeblog_userAllowed($username, $password, $website_id)) {
         if ($item->embedding == 1) {
             $link = nvweb_source_url('structure', $item->category, $session['lang']);
         } else {
             $link = $item->link($session['lang']);
         }
         $category = new structure();
         $category->load($item->category);
         $content = $item->dictionary[$session['lang']]['section-main'];
         $content = nvweb_template_fix_download_paths($content);
         if (empty($item->date_to_display)) {
             $item->date_to_display = $item->date_created;
         }
         $out = array("postid" => $item->id, "userid" => $item->author, "dateCreated" => new IXR_Date($item->date_to_display), "category" => $item->category, "title" => $item->dictionary[$session['lang']]['title'], "description" => $content, "url" => $link, "permalink" => $link, "mt_keywords" => $item->dictionary[$session['lang']]['tags']);
     } else {
         $out = new IXR_Error(401, "User not allowed.");
     }
     return $out;
 }
コード例 #2
0
/**
 * Apply some template tweaks to improve Navigate CMS theme developing experience like:
 *
 * <ul>
 * <li>Guess absolute paths to images, stylesheets, videos and scripts (even on urls without http, "//")</li>
 * <li>Convert &lt;a rel="video"&gt; and &lt;a rel="audio"&gt;  to &lt;video&gt; and &lt;audio&gt; tags</li>
 * <li>Process &lt;img&gt; tags to generate optimized images</li>
 * <li>Add Navigate CMS content default styles</li>
 * </ul>
 *
 * @param $html string Original HTML template content
 * @return string HTML template tweaked
 */
function nvweb_template_tweaks($html)
{
    global $website;
    // apply some tweaks to the generated html code
    // tweak 1: try to make absolute all image, css and script paths not starting by http
    if (!empty($website->theme)) {
        $website_absolute_path = NAVIGATE_URL . '/themes/' . $website->theme;
    } else {
        $website_absolute_path = $website->absolute_path(false);
    }
    // stylesheets
    $tags = nvweb_tags_extract($html, 'link', NULL, true, 'UTF-8');
    foreach ($tags as $tag) {
        if (!isset($tag['attributes']['href'])) {
            continue;
        }
        if (substr($tag['attributes']['href'], 0, 7) != 'http://' && substr($tag['attributes']['href'], 0, 8) != 'https://') {
            // treat "//" paths (without http or https)
            if (substr($tag['attributes']['href'], 0, 2) == '//') {
                $src = $website->protocol . substr($tag['attributes']['href'], 2);
            } else {
                $src = $website_absolute_path . '/' . $tag['attributes']['href'];
            }
            $tag['new'] = '<link href="' . $src . '" ';
            foreach ($tag['attributes'] as $name => $value) {
                if ($name != 'href') {
                    $tag['new'] .= $name . '="' . $value . '" ';
                }
            }
            $tag['new'] .= '/>';
            $html = str_replace($tag['full_tag'], $tag['new'], $html);
        }
    }
    // scripts
    $tags = nvweb_tags_extract($html, 'script', NULL, true, 'UTF-8');
    foreach ($tags as $tag) {
        if (!isset($tag['attributes']['src'])) {
            continue;
        }
        if (substr($tag['attributes']['src'], 0, 7) != 'http://' && substr($tag['attributes']['src'], 0, 8) != 'https://') {
            if (substr($tag['attributes']['src'], 0, 2) == '//') {
                $src = $website->protocol . substr($tag['attributes']['src'], 2);
            } else {
                $src = $website_absolute_path . '/' . $tag['attributes']['src'];
            }
            $tag['new'] = '<script src="' . $src . '" ';
            foreach ($tag['attributes'] as $name => $value) {
                if ($name != 'src') {
                    $tag['new'] .= $name . '="' . $value . '" ';
                }
            }
            $tag['new'] .= '></script>';
            $html = str_replace($tag['full_tag'], $tag['new'], $html);
        }
    }
    // poster attribute (<video>)
    $tags = nvweb_tags_extract($html, array('video'), false, true, 'UTF-8');
    foreach ($tags as $tag) {
        if (!isset($tag['attributes']['poster'])) {
            continue;
        }
        if (substr($tag['attributes']['poster'], 0, 7) != 'http://' && substr($tag['attributes']['poster'], 0, 8) != 'https://') {
            if (substr($tag['attributes']['poster'], 0, 2) == '//') {
                $src = $website->protocol . substr($tag['attributes']['poster'], 2);
            } else {
                $src = $website_absolute_path . '/' . $tag['attributes']['poster'];
            }
            $tag['new'] = '<video poster="' . $src . '" ';
            foreach ($tag['attributes'] as $name => $value) {
                if ($name != 'poster') {
                    $tag['new'] .= $name . '="' . $value . '" ';
                }
            }
            $tag['new'] .= '>' . $tag['contents'] . '</video>';
            $html = str_replace($tag['full_tag'], $tag['new'], $html);
        }
    }
    // sources (video, audio)
    $tags = nvweb_tags_extract($html, array('video', 'audio'), false, true, 'UTF-8');
    foreach ($tags as $tag) {
        $tag_sources = nvweb_tags_extract($tag['contents'], 'source', true, true, 'UTF-8');
        foreach ($tag_sources as $source) {
            if (!isset($source['attributes']['src'])) {
                continue;
            }
            if (substr($source['attributes']['src'], 0, 7) != 'http://' && substr($source['attributes']['src'], 0, 8) != 'https://') {
                if (substr($source['attributes']['src'], 0, 2) == '//') {
                    $src = $website->protocol . substr($source['attributes']['src'], 2);
                } else {
                    $src = $website_absolute_path . '/' . $source['attributes']['src'];
                }
                $source['new'] = '<source src="' . $src . '" ';
                foreach ($source['attributes'] as $name => $value) {
                    if ($name != 'poster') {
                        $source['new'] .= $name . '="' . $value . '" ';
                    }
                }
                $source['new'] .= '>' . $source['contents'] . '</source>';
                $html = str_replace($source['full_tag'], $source['new'], $html);
            }
        }
    }
    // images
    $tags = nvweb_tags_extract($html, 'img', NULL, true, 'UTF-8');
    foreach ($tags as $tag) {
        if (isset($tag['attributes']['srcset'])) {
            $srcset_old = explode(",", $tag['attributes']['srcset']);
            $srcset_new = array();
            foreach ($srcset_old as $src) {
                $src = trim($src);
                $src = explode(" ", $src);
                if (substr($src[0], 0, 7) != 'http://' && substr($src[0], 0, 8) != 'https://') {
                    if (substr($src[0], 0, 2) == '//') {
                        $src[0] = $website->protocol . substr($src[0], 2);
                    } else {
                        $src[0] = $website_absolute_path . '/' . $src[0];
                    }
                }
                $srcset_new[] = implode(" ", $src);
            }
            $tag['new'] = '<img ';
            foreach ($tag['attributes'] as $name => $value) {
                if ($name != 'srcset') {
                    $tag['new'] .= $name . '="' . $value . '" ';
                } else {
                    $tag['new'] .= 'srcset="' . implode(", ", $srcset_new) . '" ';
                }
            }
            $tag['new'] .= '/>';
            $html = str_replace($tag['full_tag'], $tag['new'], $html);
        }
        if (!isset($tag['attributes']['src'])) {
            continue;
        }
        if (substr($tag['attributes']['src'], 0, 7) != 'http://' && substr($tag['attributes']['src'], 0, 8) != 'https://' && substr($tag['attributes']['src'], 0, 5) != 'data:') {
            if (substr($tag['attributes']['src'], 0, 2) == '//') {
                $src = $website->protocol . substr($tag['attributes']['src'], 2);
            } else {
                $src = $website_absolute_path . '/' . $tag['attributes']['src'];
            }
            $tag['new'] = '<img src="' . $src . '" ';
            foreach ($tag['attributes'] as $name => $value) {
                if ($name != 'src') {
                    $tag['new'] .= $name . '="' . $value . '" ';
                }
            }
            $tag['new'] .= '/>';
            $html = str_replace($tag['full_tag'], $tag['new'], $html);
        }
    }
    // replace any "navigate_download.php" request for "/object" request
    $html = nvweb_template_fix_download_paths($html);
    // tweak 2: convert <a rel="video"> to <video> and <a rel="audio"> to <audio> tags
    $tags = nvweb_tags_extract($html, 'a', NULL, true, 'UTF-8');
    foreach ($tags as $tag) {
        if ($tag['attributes']['rel'] == 'video' && $tag['attributes']['navigate'] == 'navigate') {
            $preload = 'metadata';
            if (!empty($tag['attributes']['preload'])) {
                $preload = $tag['attributes']['preload'];
            }
            $content = array();
            $content[] = '<video controls="controls" preload="' . $preload . '">';
            $content[] = '	<source src="' . $tag['attributes']['href'] . '" />';
            $content[] = '	' . $tag['full_tag'];
            $content[] = '</video>';
            $html = str_replace($tag['full_tag'], implode("\n", $content), $html);
        }
        if ($tag['attributes']['rel'] == 'audio' && $tag['attributes']['navigate'] == 'navigate') {
            $preload = 'metadata';
            if (!empty($tag['attributes']['preload'])) {
                $preload = $tag['attributes']['preload'];
            }
            $content = array();
            $content[] = '<audio controls="controls" preload="' . $preload . '">';
            $content[] = '	<source src="' . $tag['attributes']['href'] . '" type="' . $tag['attributes']['type'] . '" />';
            $content[] = '	' . $tag['full_tag'];
            $content[] = '</audio>';
            $html = str_replace($tag['full_tag'], implode("\n", $content), $html);
        }
    }
    // tweak 3: let navigate generate a resized image/thumbnail if width/height is given in the img tag
    $tags = nvweb_tags_extract($html, 'img', NULL, true, 'UTF-8');
    foreach ($tags as $tag) {
        if (!isset($tag['attributes']['src'])) {
            continue;
        }
        $src = $tag['attributes']['src'];
        $tag['new'] = '';
        foreach ($tag['attributes'] as $name => $value) {
            if ($name != 'src') {
                $tag['new'] .= $name . '="' . $value . '" ';
            }
            // width attribute, the image is retrieved from a dynamic source, the width is not expressed in percentage, the width is not already given in the url parameters
            if ($name == 'width' && strpos($src, '?') !== false && strpos($value, "%") === false && strpos($src, "&width=") === false) {
                $src .= '&width=' . $value;
            }
            if ($name == 'height' && strpos($src, '?') !== false && strpos($value, "%") === false && strpos($src, "&height=") === false) {
                $src .= '&height=' . $value;
            }
        }
        $tag['new'] = '<img src="' . $src . '" ' . $tag['new'] . '/>';
        $html = str_replace($tag['full_tag'], $tag['new'], $html);
    }
    // tweak 4: add Navigate CMS content default styles
    $default_css = file_get_contents(NAVIGATE_PATH . '/css/tools/tinymce.defaults.css');
    $default_css = str_replace(array("\n", "\r", "\\s\\s", "  "), " ", $default_css);
    $default_css = substr($default_css, strpos($default_css, '/* nvweb */') + 11);
    $default_css = '<style type="text/css">' . $default_css . '</style>';
    $html = str_replace('</title>', "</title>\n\t" . $default_css . "\n", $html);
    return $html;
}